blob: 6ad1b04f3099e5e07f055684cad0f59c9460c912 [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"
Avi Kivity6aa8b732006-12-10 02:21:36 -080021
22#include <linux/kvm.h>
23#include <linux/module.h>
24#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/percpu.h>
26#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/miscdevice.h>
29#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/debugfs.h>
32#include <linux/highmem.h>
33#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080034#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080035#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040036#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030037#include <linux/cpumask.h>
38#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040039#include <linux/anon_inodes.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040
Avi Kivitye4956062007-06-28 14:15:57 -040041#include <asm/processor.h>
42#include <asm/msr.h>
43#include <asm/io.h>
44#include <asm/uaccess.h>
45#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
Avi Kivity133de902007-02-12 00:54:44 -080050static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
Avi Kivity1b6c0162007-05-24 13:03:52 +030053static cpumask_t cpus_hardware_enabled;
54
Avi Kivity6aa8b732006-12-10 02:21:36 -080055struct kvm_arch_ops *kvm_arch_ops;
Avi Kivity1165f5f2007-04-19 17:27:43 +030056
Avi Kivity15ad7142007-07-11 18:17:21 +030057static __read_mostly struct preempt_ops kvm_preempt_ops;
58
Avi Kivity1165f5f2007-04-19 17:27:43 +030059#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080060
61static struct kvm_stats_debugfs_item {
62 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030063 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080064 struct dentry *dentry;
65} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030066 { "pf_fixed", STAT_OFFSET(pf_fixed) },
67 { "pf_guest", STAT_OFFSET(pf_guest) },
68 { "tlb_flush", STAT_OFFSET(tlb_flush) },
69 { "invlpg", STAT_OFFSET(invlpg) },
70 { "exits", STAT_OFFSET(exits) },
71 { "io_exits", STAT_OFFSET(io_exits) },
72 { "mmio_exits", STAT_OFFSET(mmio_exits) },
73 { "signal_exits", STAT_OFFSET(signal_exits) },
74 { "irq_window", STAT_OFFSET(irq_window_exits) },
75 { "halt_exits", STAT_OFFSET(halt_exits) },
76 { "request_irq", STAT_OFFSET(request_irq_exits) },
77 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030078 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030079 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030080 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080081};
82
83static struct dentry *debugfs_dir;
84
85#define MAX_IO_MSRS 256
86
Rusty Russell707d92fa2007-07-17 23:19:08 +100087#define CR0_RESERVED_BITS \
88 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
89 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
90 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100091#define CR4_RESERVED_BITS \
92 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
93 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
94 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
95 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
96
Rusty Russell7075bc82007-07-17 23:37:17 +100097#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -080098#define EFER_RESERVED_BITS 0xfffffffffffff2fe
99
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800100#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101// LDT or TSS descriptor in the GDT. 16 bytes.
102struct segment_descriptor_64 {
103 struct segment_descriptor s;
104 u32 base_higher;
105 u32 pad_zero;
106};
107
108#endif
109
Avi Kivitybccf2152007-02-21 18:04:26 +0200110static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
111 unsigned long arg);
112
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113unsigned long segment_base(u16 selector)
114{
115 struct descriptor_table gdt;
116 struct segment_descriptor *d;
117 unsigned long table_base;
118 typedef unsigned long ul;
119 unsigned long v;
120
121 if (selector == 0)
122 return 0;
123
124 asm ("sgdt %0" : "=m"(gdt));
125 table_base = gdt.base;
126
127 if (selector & 4) { /* from ldt */
128 u16 ldt_selector;
129
130 asm ("sldt %0" : "=g"(ldt_selector));
131 table_base = segment_base(ldt_selector);
132 }
133 d = (struct segment_descriptor *)(table_base + (selector & ~7));
134 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800135#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136 if (d->system == 0
137 && (d->type == 2 || d->type == 9 || d->type == 11))
138 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
139#endif
140 return v;
141}
142EXPORT_SYMBOL_GPL(segment_base);
143
James Morris5aacf0c2006-12-22 01:04:55 -0800144static inline int valid_vcpu(int n)
145{
146 return likely(n >= 0 && n < KVM_MAX_VCPUS);
147}
148
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200149int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
150 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151{
152 unsigned char *host_buf = dest;
153 unsigned long req_size = size;
154
155 while (size) {
156 hpa_t paddr;
157 unsigned now;
158 unsigned offset;
159 hva_t guest_buf;
160
161 paddr = gva_to_hpa(vcpu, addr);
162
163 if (is_error_hpa(paddr))
164 break;
165
166 guest_buf = (hva_t)kmap_atomic(
167 pfn_to_page(paddr >> PAGE_SHIFT),
168 KM_USER0);
169 offset = addr & ~PAGE_MASK;
170 guest_buf |= offset;
171 now = min(size, PAGE_SIZE - offset);
172 memcpy(host_buf, (void*)guest_buf, now);
173 host_buf += now;
174 addr += now;
175 size -= now;
176 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
177 }
178 return req_size - size;
179}
180EXPORT_SYMBOL_GPL(kvm_read_guest);
181
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200182int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
183 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184{
185 unsigned char *host_buf = data;
186 unsigned long req_size = size;
187
188 while (size) {
189 hpa_t paddr;
190 unsigned now;
191 unsigned offset;
192 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200193 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194
195 paddr = gva_to_hpa(vcpu, addr);
196
197 if (is_error_hpa(paddr))
198 break;
199
Uri Lublinab51a432007-02-21 18:25:21 +0200200 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
201 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 guest_buf = (hva_t)kmap_atomic(
203 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
204 offset = addr & ~PAGE_MASK;
205 guest_buf |= offset;
206 now = min(size, PAGE_SIZE - offset);
207 memcpy((void*)guest_buf, host_buf, now);
208 host_buf += now;
209 addr += now;
210 size -= now;
211 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
212 }
213 return req_size - size;
214}
215EXPORT_SYMBOL_GPL(kvm_write_guest);
216
Avi Kivity7702fd12007-06-14 16:27:40 +0300217void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
218{
219 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
220 return;
221
222 vcpu->guest_fpu_loaded = 1;
223 fx_save(vcpu->host_fx_image);
224 fx_restore(vcpu->guest_fx_image);
225}
226EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
227
228void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
229{
230 if (!vcpu->guest_fpu_loaded)
231 return;
232
233 vcpu->guest_fpu_loaded = 0;
234 fx_save(vcpu->guest_fx_image);
235 fx_restore(vcpu->host_fx_image);
236}
237EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
238
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239/*
240 * Switches to specified vcpu, until a matching vcpu_put()
241 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200242static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243{
Avi Kivity15ad7142007-07-11 18:17:21 +0300244 int cpu;
245
Avi Kivitybccf2152007-02-21 18:04:26 +0200246 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300247 cpu = get_cpu();
248 preempt_notifier_register(&vcpu->preempt_notifier);
249 kvm_arch_ops->vcpu_load(vcpu, cpu);
250 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200251}
252
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253static void vcpu_put(struct kvm_vcpu *vcpu)
254{
Avi Kivity15ad7142007-07-11 18:17:21 +0300255 preempt_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300257 preempt_notifier_unregister(&vcpu->preempt_notifier);
258 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259 mutex_unlock(&vcpu->mutex);
260}
261
Avi Kivityd9e368d2007-06-07 19:18:30 +0300262static void ack_flush(void *_completed)
263{
264 atomic_t *completed = _completed;
265
266 atomic_inc(completed);
267}
268
269void kvm_flush_remote_tlbs(struct kvm *kvm)
270{
271 int i, cpu, needed;
272 cpumask_t cpus;
273 struct kvm_vcpu *vcpu;
274 atomic_t completed;
275
276 atomic_set(&completed, 0);
277 cpus_clear(cpus);
278 needed = 0;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000279 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
280 vcpu = kvm->vcpus[i];
281 if (!vcpu)
282 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300283 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
284 continue;
285 cpu = vcpu->cpu;
286 if (cpu != -1 && cpu != raw_smp_processor_id())
287 if (!cpu_isset(cpu, cpus)) {
288 cpu_set(cpu, cpus);
289 ++needed;
290 }
291 }
292
293 /*
294 * We really want smp_call_function_mask() here. But that's not
295 * available, so ipi all cpus in parallel and wait for them
296 * to complete.
297 */
298 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
299 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
300 while (atomic_read(&completed) != needed) {
301 cpu_relax();
302 barrier();
303 }
304}
305
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000306int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
307{
308 struct page *page;
309 int r;
310
311 mutex_init(&vcpu->mutex);
312 vcpu->cpu = -1;
313 vcpu->mmu.root_hpa = INVALID_PAGE;
314 vcpu->kvm = kvm;
315 vcpu->vcpu_id = id;
316
317 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
318 if (!page) {
319 r = -ENOMEM;
320 goto fail;
321 }
322 vcpu->run = page_address(page);
323
324 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
325 if (!page) {
326 r = -ENOMEM;
327 goto fail_free_run;
328 }
329 vcpu->pio_data = page_address(page);
330
331 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
332 FX_IMAGE_ALIGN);
333 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
334
335 r = kvm_mmu_create(vcpu);
336 if (r < 0)
337 goto fail_free_pio_data;
338
339 return 0;
340
341fail_free_pio_data:
342 free_page((unsigned long)vcpu->pio_data);
343fail_free_run:
344 free_page((unsigned long)vcpu->run);
345fail:
346 return -ENOMEM;
347}
348EXPORT_SYMBOL_GPL(kvm_vcpu_init);
349
350void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
351{
352 kvm_mmu_destroy(vcpu);
353 free_page((unsigned long)vcpu->pio_data);
354 free_page((unsigned long)vcpu->run);
355}
356EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
357
Avi Kivityf17abe92007-02-21 19:28:04 +0200358static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359{
360 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361
362 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200363 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
Eddie Dong74906342007-06-19 18:05:03 +0300365 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800366 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400368 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000369 spin_lock(&kvm_lock);
370 list_add(&kvm->vm_list, &vm_list);
371 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200372 return kvm;
373}
374
375static int kvm_dev_open(struct inode *inode, struct file *filp)
376{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377 return 0;
378}
379
380/*
381 * Free any memory in @free but not in @dont.
382 */
383static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
384 struct kvm_memory_slot *dont)
385{
386 int i;
387
388 if (!dont || free->phys_mem != dont->phys_mem)
389 if (free->phys_mem) {
390 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800391 if (free->phys_mem[i])
392 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 vfree(free->phys_mem);
394 }
395
396 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
397 vfree(free->dirty_bitmap);
398
Al Viro8b6d44c2007-02-09 16:38:40 +0000399 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000401 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402}
403
404static void kvm_free_physmem(struct kvm *kvm)
405{
406 int i;
407
408 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000409 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410}
411
Avi Kivity039576c2007-03-20 12:46:50 +0200412static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
413{
414 int i;
415
Rusty Russell3077c4512007-07-30 16:41:57 +1000416 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200417 if (vcpu->pio.guest_pages[i]) {
418 __free_page(vcpu->pio.guest_pages[i]);
419 vcpu->pio.guest_pages[i] = NULL;
420 }
421}
422
Avi Kivity7b53aa52007-06-05 12:17:03 +0300423static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
424{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300425 vcpu_load(vcpu);
426 kvm_mmu_unload(vcpu);
427 vcpu_put(vcpu);
428}
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430static void kvm_free_vcpus(struct kvm *kvm)
431{
432 unsigned int i;
433
Avi Kivity7b53aa52007-06-05 12:17:03 +0300434 /*
435 * Unpin any mmu pages first.
436 */
437 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000438 if (kvm->vcpus[i])
439 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
440 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
441 if (kvm->vcpus[i]) {
442 kvm_arch_ops->vcpu_free(kvm->vcpus[i]);
443 kvm->vcpus[i] = NULL;
444 }
445 }
446
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447}
448
449static int kvm_dev_release(struct inode *inode, struct file *filp)
450{
Avi Kivityf17abe92007-02-21 19:28:04 +0200451 return 0;
452}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Avi Kivityf17abe92007-02-21 19:28:04 +0200454static void kvm_destroy_vm(struct kvm *kvm)
455{
Avi Kivity133de902007-02-12 00:54:44 -0800456 spin_lock(&kvm_lock);
457 list_del(&kvm->vm_list);
458 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300459 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400460 kvm_io_bus_destroy(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 kvm_free_vcpus(kvm);
462 kvm_free_physmem(kvm);
463 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200464}
465
466static int kvm_vm_release(struct inode *inode, struct file *filp)
467{
468 struct kvm *kvm = filp->private_data;
469
470 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 return 0;
472}
473
474static void inject_gp(struct kvm_vcpu *vcpu)
475{
476 kvm_arch_ops->inject_gp(vcpu, 0);
477}
478
Avi Kivity1342d352007-01-05 16:36:39 -0800479/*
480 * Load the pae pdptrs. Return true is they are all valid.
481 */
482static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483{
484 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800485 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800488 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300489 struct page *page;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000490 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491
Shaohua Li11ec2802007-07-23 14:51:37 +0800492 mutex_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300493 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
Rusty Russellc820c2a2007-07-25 13:29:51 +1000494 if (!page) {
495 ret = 0;
496 goto out;
497 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498
Rusty Russellc820c2a2007-07-25 13:29:51 +1000499 pdpt = kmap_atomic(page, KM_USER0);
500 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
501 kunmap_atomic(pdpt, KM_USER0);
502
503 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
504 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800505 ret = 0;
506 goto out;
507 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000509 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510
Rusty Russellc820c2a2007-07-25 13:29:51 +1000511 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800512out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800513 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514
Avi Kivity1342d352007-01-05 16:36:39 -0800515 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516}
517
518void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
519{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000520 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
522 cr0, vcpu->cr0);
523 inject_gp(vcpu);
524 return;
525 }
526
Rusty Russell707d92fa2007-07-17 23:19:08 +1000527 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
529 inject_gp(vcpu);
530 return;
531 }
532
Rusty Russell707d92fa2007-07-17 23:19:08 +1000533 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
535 "and a clear PE flag\n");
536 inject_gp(vcpu);
537 return;
538 }
539
Rusty Russell707d92fa2007-07-17 23:19:08 +1000540 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800541#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542 if ((vcpu->shadow_efer & EFER_LME)) {
543 int cs_db, cs_l;
544
545 if (!is_pae(vcpu)) {
546 printk(KERN_DEBUG "set_cr0: #GP, start paging "
547 "in long mode while PAE is disabled\n");
548 inject_gp(vcpu);
549 return;
550 }
551 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
552 if (cs_l) {
553 printk(KERN_DEBUG "set_cr0: #GP, start paging "
554 "in long mode while CS.L == 1\n");
555 inject_gp(vcpu);
556 return;
557
558 }
559 } else
560#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800561 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
563 "reserved bits\n");
564 inject_gp(vcpu);
565 return;
566 }
567
568 }
569
570 kvm_arch_ops->set_cr0(vcpu, cr0);
571 vcpu->cr0 = cr0;
572
Shaohua Li11ec2802007-07-23 14:51:37 +0800573 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800575 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576 return;
577}
578EXPORT_SYMBOL_GPL(set_cr0);
579
580void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
581{
582 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
583}
584EXPORT_SYMBOL_GPL(lmsw);
585
586void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
587{
Rusty Russell66aee912007-07-17 23:34:16 +1000588 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
590 inject_gp(vcpu);
591 return;
592 }
593
Avi Kivitya9058ec2006-12-29 16:49:37 -0800594 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000595 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
597 "in long mode\n");
598 inject_gp(vcpu);
599 return;
600 }
Rusty Russell66aee912007-07-17 23:34:16 +1000601 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800602 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
604 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000605 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606 }
607
Rusty Russell66aee912007-07-17 23:34:16 +1000608 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
610 inject_gp(vcpu);
611 return;
612 }
613 kvm_arch_ops->set_cr4(vcpu, cr4);
Shaohua Li11ec2802007-07-23 14:51:37 +0800614 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800616 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617}
618EXPORT_SYMBOL_GPL(set_cr4);
619
620void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
621{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800622 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000623 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
625 inject_gp(vcpu);
626 return;
627 }
628 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000629 if (is_pae(vcpu)) {
630 if (cr3 & CR3_PAE_RESERVED_BITS) {
631 printk(KERN_DEBUG
632 "set_cr3: #GP, reserved bits\n");
633 inject_gp(vcpu);
634 return;
635 }
636 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
637 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
638 "reserved bits\n");
639 inject_gp(vcpu);
640 return;
641 }
642 } else {
643 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
644 printk(KERN_DEBUG
645 "set_cr3: #GP, reserved bits\n");
646 inject_gp(vcpu);
647 return;
648 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 }
650 }
651
652 vcpu->cr3 = cr3;
Shaohua Li11ec2802007-07-23 14:51:37 +0800653 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800654 /*
655 * Does the new cr3 value map to physical memory? (Note, we
656 * catch an invalid cr3 even in real-mode, because it would
657 * cause trouble later on when we turn on paging anyway.)
658 *
659 * A real CPU would silently accept an invalid cr3 and would
660 * attempt to use it - with largely undefined (and often hard
661 * to debug) behavior on the guest side.
662 */
663 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
664 inject_gp(vcpu);
665 else
666 vcpu->mmu.new_cr3(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800667 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668}
669EXPORT_SYMBOL_GPL(set_cr3);
670
671void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
672{
Rusty Russell7075bc82007-07-17 23:37:17 +1000673 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
675 inject_gp(vcpu);
676 return;
677 }
678 vcpu->cr8 = cr8;
679}
680EXPORT_SYMBOL_GPL(set_cr8);
681
682void fx_init(struct kvm_vcpu *vcpu)
683{
684 struct __attribute__ ((__packed__)) fx_image_s {
685 u16 control; //fcw
686 u16 status; //fsw
687 u16 tag; // ftw
688 u16 opcode; //fop
689 u64 ip; // fpu ip
690 u64 operand;// fpu dp
691 u32 mxcsr;
692 u32 mxcsr_mask;
693
694 } *fx_image;
695
Rusty Russell9bd01502007-07-30 16:29:56 +1000696 /* Initialize guest FPU by resetting ours and saving into guest's */
697 preempt_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698 fx_save(vcpu->host_fx_image);
699 fpu_init();
700 fx_save(vcpu->guest_fx_image);
701 fx_restore(vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000702 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
704 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
705 fx_image->mxcsr = 0x1f80;
706 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
707 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
708}
709EXPORT_SYMBOL_GPL(fx_init);
710
711/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 * Allocate some memory and give it an address in the guest physical address
713 * space.
714 *
715 * Discontiguous memory is allowed, mostly for framebuffers.
716 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200717static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
718 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719{
720 int r;
721 gfn_t base_gfn;
722 unsigned long npages;
723 unsigned long i;
724 struct kvm_memory_slot *memslot;
725 struct kvm_memory_slot old, new;
726 int memory_config_version;
727
728 r = -EINVAL;
729 /* General sanity checks */
730 if (mem->memory_size & (PAGE_SIZE - 1))
731 goto out;
732 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
733 goto out;
734 if (mem->slot >= KVM_MEMORY_SLOTS)
735 goto out;
736 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
737 goto out;
738
739 memslot = &kvm->memslots[mem->slot];
740 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
741 npages = mem->memory_size >> PAGE_SHIFT;
742
743 if (!npages)
744 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
745
746raced:
Shaohua Li11ec2802007-07-23 14:51:37 +0800747 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800748
749 memory_config_version = kvm->memory_config_version;
750 new = old = *memslot;
751
752 new.base_gfn = base_gfn;
753 new.npages = npages;
754 new.flags = mem->flags;
755
756 /* Disallow changing a memory slot's size. */
757 r = -EINVAL;
758 if (npages && old.npages && npages != old.npages)
759 goto out_unlock;
760
761 /* Check for overlaps */
762 r = -EEXIST;
763 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
764 struct kvm_memory_slot *s = &kvm->memslots[i];
765
766 if (s == memslot)
767 continue;
768 if (!((base_gfn + npages <= s->base_gfn) ||
769 (base_gfn >= s->base_gfn + s->npages)))
770 goto out_unlock;
771 }
772 /*
773 * Do memory allocations outside lock. memory_config_version will
774 * detect any races.
775 */
Shaohua Li11ec2802007-07-23 14:51:37 +0800776 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
778 /* Deallocate if slot is being removed */
779 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000780 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781
782 /* Free page dirty bitmap if unneeded */
783 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000784 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785
786 r = -ENOMEM;
787
788 /* Allocate if a slot is being created */
789 if (npages && !new.phys_mem) {
790 new.phys_mem = vmalloc(npages * sizeof(struct page *));
791
792 if (!new.phys_mem)
793 goto out_free;
794
795 memset(new.phys_mem, 0, npages * sizeof(struct page *));
796 for (i = 0; i < npages; ++i) {
797 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
798 | __GFP_ZERO);
799 if (!new.phys_mem[i])
800 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200801 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 }
803 }
804
805 /* Allocate page dirty bitmap if needed */
806 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
807 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
808
809 new.dirty_bitmap = vmalloc(dirty_bytes);
810 if (!new.dirty_bitmap)
811 goto out_free;
812 memset(new.dirty_bitmap, 0, dirty_bytes);
813 }
814
Shaohua Li11ec2802007-07-23 14:51:37 +0800815 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816
817 if (memory_config_version != kvm->memory_config_version) {
Shaohua Li11ec2802007-07-23 14:51:37 +0800818 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 kvm_free_physmem_slot(&new, &old);
820 goto raced;
821 }
822
823 r = -EAGAIN;
824 if (kvm->busy)
825 goto out_unlock;
826
827 if (mem->slot >= kvm->nmemslots)
828 kvm->nmemslots = mem->slot + 1;
829
830 *memslot = new;
831 ++kvm->memory_config_version;
832
Avi Kivity90cb0522007-07-17 13:04:56 +0300833 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
834 kvm_flush_remote_tlbs(kvm);
835
Shaohua Li11ec2802007-07-23 14:51:37 +0800836 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 kvm_free_physmem_slot(&old, &new);
839 return 0;
840
841out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800842 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843out_free:
844 kvm_free_physmem_slot(&new, &old);
845out:
846 return r;
847}
848
849/*
850 * Get (and clear) the dirty memory log for a memory slot.
851 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200852static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
853 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854{
855 struct kvm_memory_slot *memslot;
856 int r, i;
857 int n;
858 unsigned long any = 0;
859
Shaohua Li11ec2802007-07-23 14:51:37 +0800860 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861
862 /*
863 * Prevent changes to guest memory configuration even while the lock
864 * is not taken.
865 */
866 ++kvm->busy;
Shaohua Li11ec2802007-07-23 14:51:37 +0800867 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868 r = -EINVAL;
869 if (log->slot >= KVM_MEMORY_SLOTS)
870 goto out;
871
872 memslot = &kvm->memslots[log->slot];
873 r = -ENOENT;
874 if (!memslot->dirty_bitmap)
875 goto out;
876
Uri Lublincd1a4a92007-02-22 16:43:09 +0200877 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
Uri Lublincd1a4a92007-02-22 16:43:09 +0200879 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 any = memslot->dirty_bitmap[i];
881
882 r = -EFAULT;
883 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
884 goto out;
885
Shaohua Li11ec2802007-07-23 14:51:37 +0800886 mutex_lock(&kvm->lock);
Avi Kivity90cb0522007-07-17 13:04:56 +0300887 kvm_mmu_slot_remove_write_access(kvm, log->slot);
888 kvm_flush_remote_tlbs(kvm);
889 memset(memslot->dirty_bitmap, 0, n);
Shaohua Li11ec2802007-07-23 14:51:37 +0800890 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891
892 r = 0;
893
894out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800895 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896 --kvm->busy;
Shaohua Li11ec2802007-07-23 14:51:37 +0800897 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 return r;
899}
900
Avi Kivitye8207542007-03-30 16:54:30 +0300901/*
902 * Set a new alias region. Aliases map a portion of physical memory into
903 * another portion. This is useful for memory windows, for example the PC
904 * VGA region.
905 */
906static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
907 struct kvm_memory_alias *alias)
908{
909 int r, n;
910 struct kvm_mem_alias *p;
911
912 r = -EINVAL;
913 /* General sanity checks */
914 if (alias->memory_size & (PAGE_SIZE - 1))
915 goto out;
916 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
917 goto out;
918 if (alias->slot >= KVM_ALIAS_SLOTS)
919 goto out;
920 if (alias->guest_phys_addr + alias->memory_size
921 < alias->guest_phys_addr)
922 goto out;
923 if (alias->target_phys_addr + alias->memory_size
924 < alias->target_phys_addr)
925 goto out;
926
Shaohua Li11ec2802007-07-23 14:51:37 +0800927 mutex_lock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300928
929 p = &kvm->aliases[alias->slot];
930 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
931 p->npages = alias->memory_size >> PAGE_SHIFT;
932 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
933
934 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
935 if (kvm->aliases[n - 1].npages)
936 break;
937 kvm->naliases = n;
938
Avi Kivity90cb0522007-07-17 13:04:56 +0300939 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300940
Shaohua Li11ec2802007-07-23 14:51:37 +0800941 mutex_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300942
943 return 0;
944
945out:
946 return r;
947}
948
949static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
950{
951 int i;
952 struct kvm_mem_alias *alias;
953
954 for (i = 0; i < kvm->naliases; ++i) {
955 alias = &kvm->aliases[i];
956 if (gfn >= alias->base_gfn
957 && gfn < alias->base_gfn + alias->npages)
958 return alias->target_gfn + gfn - alias->base_gfn;
959 }
960 return gfn;
961}
962
963static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964{
965 int i;
966
967 for (i = 0; i < kvm->nmemslots; ++i) {
968 struct kvm_memory_slot *memslot = &kvm->memslots[i];
969
970 if (gfn >= memslot->base_gfn
971 && gfn < memslot->base_gfn + memslot->npages)
972 return memslot;
973 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000974 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975}
Avi Kivitye8207542007-03-30 16:54:30 +0300976
977struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
978{
979 gfn = unalias_gfn(kvm, gfn);
980 return __gfn_to_memslot(kvm, gfn);
981}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Avi Kivity954bbbc2007-03-30 14:02:32 +0300983struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
984{
985 struct kvm_memory_slot *slot;
986
Avi Kivitye8207542007-03-30 16:54:30 +0300987 gfn = unalias_gfn(kvm, gfn);
988 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300989 if (!slot)
990 return NULL;
991 return slot->phys_mem[gfn - slot->base_gfn];
992}
993EXPORT_SYMBOL_GPL(gfn_to_page);
994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
996{
997 int i;
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300998 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 unsigned long rel_gfn;
1000
1001 for (i = 0; i < kvm->nmemslots; ++i) {
1002 memslot = &kvm->memslots[i];
1003
1004 if (gfn >= memslot->base_gfn
1005 && gfn < memslot->base_gfn + memslot->npages) {
1006
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001007 if (!memslot->dirty_bitmap)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 return;
1009
1010 rel_gfn = gfn - memslot->base_gfn;
1011
1012 /* avoid RMW */
1013 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1014 set_bit(rel_gfn, memslot->dirty_bitmap);
1015 return;
1016 }
1017 }
1018}
1019
1020static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001021 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 unsigned int bytes,
1023 struct x86_emulate_ctxt *ctxt)
1024{
1025 struct kvm_vcpu *vcpu = ctxt->vcpu;
1026 void *data = val;
1027
1028 while (bytes) {
1029 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1030 unsigned offset = addr & (PAGE_SIZE-1);
1031 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1032 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001033 struct page *page;
1034 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
1036 if (gpa == UNMAPPED_GVA)
1037 return X86EMUL_PROPAGATE_FAULT;
1038 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001039 page = gfn_to_page(vcpu->kvm, pfn);
1040 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001042 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043
Avi Kivity954bbbc2007-03-30 14:02:32 +03001044 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045
Avi Kivity954bbbc2007-03-30 14:02:32 +03001046 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047
1048 bytes -= tocopy;
1049 data += tocopy;
1050 addr += tocopy;
1051 }
1052
1053 return X86EMUL_CONTINUE;
1054}
1055
1056static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001057 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 unsigned int bytes,
1059 struct x86_emulate_ctxt *ctxt)
1060{
1061 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1062 addr, bytes);
1063 return X86EMUL_UNHANDLEABLE;
1064}
1065
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001066static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1067 gpa_t addr)
1068{
1069 /*
1070 * Note that its important to have this wrapper function because
1071 * in the very near future we will be checking for MMIOs against
1072 * the LAPIC as well as the general MMIO bus
1073 */
1074 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1075}
1076
Eddie Dong74906342007-06-19 18:05:03 +03001077static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1078 gpa_t addr)
1079{
1080 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1081}
1082
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001084 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 unsigned int bytes,
1086 struct x86_emulate_ctxt *ctxt)
1087{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001088 struct kvm_vcpu *vcpu = ctxt->vcpu;
1089 struct kvm_io_device *mmio_dev;
1090 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
1092 if (vcpu->mmio_read_completed) {
1093 memcpy(val, vcpu->mmio_data, bytes);
1094 vcpu->mmio_read_completed = 0;
1095 return X86EMUL_CONTINUE;
1096 } else if (emulator_read_std(addr, val, bytes, ctxt)
1097 == X86EMUL_CONTINUE)
1098 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001099
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001100 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1101 if (gpa == UNMAPPED_GVA)
1102 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001104 /*
1105 * Is this MMIO handled locally?
1106 */
1107 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1108 if (mmio_dev) {
1109 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1110 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001112
1113 vcpu->mmio_needed = 1;
1114 vcpu->mmio_phys_addr = gpa;
1115 vcpu->mmio_size = bytes;
1116 vcpu->mmio_is_write = 0;
1117
1118 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
Avi Kivityda4a00f2007-01-05 16:36:44 -08001121static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001122 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001123{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001124 struct page *page;
1125 void *virt;
1126
1127 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1128 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001129 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1130 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001131 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001132 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001133 virt = kmap_atomic(page, KM_USER0);
Shaohua Life5518812007-07-23 14:51:39 +08001134 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001135 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001136 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001137 return 1;
1138}
1139
Avi Kivityb0fcd902007-07-22 18:48:54 +03001140static int emulator_write_emulated_onepage(unsigned long addr,
1141 const void *val,
1142 unsigned int bytes,
1143 struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001145 struct kvm_vcpu *vcpu = ctxt->vcpu;
1146 struct kvm_io_device *mmio_dev;
1147 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148
Avi Kivityc9047f52007-04-17 10:53:22 +03001149 if (gpa == UNMAPPED_GVA) {
1150 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001152 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153
Avi Kivityda4a00f2007-01-05 16:36:44 -08001154 if (emulator_write_phys(vcpu, gpa, val, bytes))
1155 return X86EMUL_CONTINUE;
1156
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001157 /*
1158 * Is this MMIO handled locally?
1159 */
1160 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1161 if (mmio_dev) {
1162 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1163 return X86EMUL_CONTINUE;
1164 }
1165
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 vcpu->mmio_needed = 1;
1167 vcpu->mmio_phys_addr = gpa;
1168 vcpu->mmio_size = bytes;
1169 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001170 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171
1172 return X86EMUL_CONTINUE;
1173}
1174
Avi Kivityb0fcd902007-07-22 18:48:54 +03001175static int emulator_write_emulated(unsigned long addr,
1176 const void *val,
1177 unsigned int bytes,
1178 struct x86_emulate_ctxt *ctxt)
1179{
1180 /* Crossing a page boundary? */
1181 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1182 int rc, now;
1183
1184 now = -addr & ~PAGE_MASK;
1185 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1186 if (rc != X86EMUL_CONTINUE)
1187 return rc;
1188 addr += now;
1189 val += now;
1190 bytes -= now;
1191 }
1192 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1193}
1194
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001196 const void *old,
1197 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 unsigned int bytes,
1199 struct x86_emulate_ctxt *ctxt)
1200{
1201 static int reported;
1202
1203 if (!reported) {
1204 reported = 1;
1205 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1206 }
1207 return emulator_write_emulated(addr, new, bytes, ctxt);
1208}
1209
1210static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1211{
1212 return kvm_arch_ops->get_segment_base(vcpu, seg);
1213}
1214
1215int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1216{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 return X86EMUL_CONTINUE;
1218}
1219
1220int emulate_clts(struct kvm_vcpu *vcpu)
1221{
Avi Kivity399badf2007-01-05 16:36:38 -08001222 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
Rusty Russell707d92fa2007-07-17 23:19:08 +10001224 cr0 = vcpu->cr0 & ~X86_CR0_TS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 kvm_arch_ops->set_cr0(vcpu, cr0);
1226 return X86EMUL_CONTINUE;
1227}
1228
1229int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1230{
1231 struct kvm_vcpu *vcpu = ctxt->vcpu;
1232
1233 switch (dr) {
1234 case 0 ... 3:
1235 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1236 return X86EMUL_CONTINUE;
1237 default:
1238 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1239 __FUNCTION__, dr);
1240 return X86EMUL_UNHANDLEABLE;
1241 }
1242}
1243
1244int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1245{
1246 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1247 int exception;
1248
1249 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1250 if (exception) {
1251 /* FIXME: better handling */
1252 return X86EMUL_UNHANDLEABLE;
1253 }
1254 return X86EMUL_CONTINUE;
1255}
1256
1257static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1258{
1259 static int reported;
1260 u8 opcodes[4];
1261 unsigned long rip = ctxt->vcpu->rip;
1262 unsigned long rip_linear;
1263
1264 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1265
1266 if (reported)
1267 return;
1268
1269 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1270
1271 printk(KERN_ERR "emulation failed but !mmio_needed?"
1272 " rip %lx %02x %02x %02x %02x\n",
1273 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1274 reported = 1;
1275}
1276
1277struct x86_emulate_ops emulate_ops = {
1278 .read_std = emulator_read_std,
1279 .write_std = emulator_write_std,
1280 .read_emulated = emulator_read_emulated,
1281 .write_emulated = emulator_write_emulated,
1282 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1283};
1284
1285int emulate_instruction(struct kvm_vcpu *vcpu,
1286 struct kvm_run *run,
1287 unsigned long cr2,
1288 u16 error_code)
1289{
1290 struct x86_emulate_ctxt emulate_ctxt;
1291 int r;
1292 int cs_db, cs_l;
1293
Avi Kivitye7df56e2007-03-14 15:54:54 +02001294 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 kvm_arch_ops->cache_regs(vcpu);
1296
1297 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1298
1299 emulate_ctxt.vcpu = vcpu;
1300 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1301 emulate_ctxt.cr2 = cr2;
1302 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1303 ? X86EMUL_MODE_REAL : cs_l
1304 ? X86EMUL_MODE_PROT64 : cs_db
1305 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1306
1307 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1308 emulate_ctxt.cs_base = 0;
1309 emulate_ctxt.ds_base = 0;
1310 emulate_ctxt.es_base = 0;
1311 emulate_ctxt.ss_base = 0;
1312 } else {
1313 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1314 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1315 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1316 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1317 }
1318
1319 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1320 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1321
1322 vcpu->mmio_is_write = 0;
1323 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1324
1325 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001326 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1328 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1329 run->mmio.len = vcpu->mmio_size;
1330 run->mmio.is_write = vcpu->mmio_is_write;
1331 }
1332
1333 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001334 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1335 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 if (!vcpu->mmio_needed) {
1337 report_emulation_failure(&emulate_ctxt);
1338 return EMULATE_FAIL;
1339 }
1340 return EMULATE_DO_MMIO;
1341 }
1342
1343 kvm_arch_ops->decache_regs(vcpu);
1344 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1345
Avi Kivity02c83202007-04-29 15:02:17 +03001346 if (vcpu->mmio_is_write) {
1347 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001349 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350
1351 return EMULATE_DONE;
1352}
1353EXPORT_SYMBOL_GPL(emulate_instruction);
1354
Avi Kivityd3bef152007-06-05 15:53:05 +03001355int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1356{
1357 if (vcpu->irq_summary)
1358 return 1;
1359
1360 vcpu->run->exit_reason = KVM_EXIT_HLT;
1361 ++vcpu->stat.halt_exits;
1362 return 0;
1363}
1364EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1365
Avi Kivity270fd9b2007-02-19 14:37:47 +02001366int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1367{
1368 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1369
Dor Laor9b22bf52007-02-19 16:44:49 +02001370 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001371 ret = -KVM_EINVAL;
1372#ifdef CONFIG_X86_64
1373 if (is_long_mode(vcpu)) {
1374 nr = vcpu->regs[VCPU_REGS_RAX];
1375 a0 = vcpu->regs[VCPU_REGS_RDI];
1376 a1 = vcpu->regs[VCPU_REGS_RSI];
1377 a2 = vcpu->regs[VCPU_REGS_RDX];
1378 a3 = vcpu->regs[VCPU_REGS_RCX];
1379 a4 = vcpu->regs[VCPU_REGS_R8];
1380 a5 = vcpu->regs[VCPU_REGS_R9];
1381 } else
1382#endif
1383 {
1384 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1385 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1386 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1387 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1388 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1389 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1390 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1391 }
1392 switch (nr) {
1393 default:
Jeff Dike519ef352007-07-16 15:24:47 -04001394 run->hypercall.nr = nr;
Avi Kivityb4e63f52007-03-04 13:59:30 +02001395 run->hypercall.args[0] = a0;
1396 run->hypercall.args[1] = a1;
1397 run->hypercall.args[2] = a2;
1398 run->hypercall.args[3] = a3;
1399 run->hypercall.args[4] = a4;
1400 run->hypercall.args[5] = a5;
1401 run->hypercall.ret = ret;
1402 run->hypercall.longmode = is_long_mode(vcpu);
1403 kvm_arch_ops->decache_regs(vcpu);
1404 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001405 }
1406 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001407 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001408 return 1;
1409}
1410EXPORT_SYMBOL_GPL(kvm_hypercall);
1411
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1413{
1414 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1415}
1416
1417void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1418{
1419 struct descriptor_table dt = { limit, base };
1420
1421 kvm_arch_ops->set_gdt(vcpu, &dt);
1422}
1423
1424void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1425{
1426 struct descriptor_table dt = { limit, base };
1427
1428 kvm_arch_ops->set_idt(vcpu, &dt);
1429}
1430
1431void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1432 unsigned long *rflags)
1433{
1434 lmsw(vcpu, msw);
1435 *rflags = kvm_arch_ops->get_rflags(vcpu);
1436}
1437
1438unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1439{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001440 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 switch (cr) {
1442 case 0:
1443 return vcpu->cr0;
1444 case 2:
1445 return vcpu->cr2;
1446 case 3:
1447 return vcpu->cr3;
1448 case 4:
1449 return vcpu->cr4;
1450 default:
1451 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1452 return 0;
1453 }
1454}
1455
1456void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1457 unsigned long *rflags)
1458{
1459 switch (cr) {
1460 case 0:
1461 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1462 *rflags = kvm_arch_ops->get_rflags(vcpu);
1463 break;
1464 case 2:
1465 vcpu->cr2 = val;
1466 break;
1467 case 3:
1468 set_cr3(vcpu, val);
1469 break;
1470 case 4:
1471 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1472 break;
1473 default:
1474 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1475 }
1476}
1477
Ingo Molnar102d8322007-02-19 14:37:47 +02001478/*
1479 * Register the para guest with the host:
1480 */
1481static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1482{
1483 struct kvm_vcpu_para_state *para_state;
1484 hpa_t para_state_hpa, hypercall_hpa;
1485 struct page *para_state_page;
1486 unsigned char *hypercall;
1487 gpa_t hypercall_gpa;
1488
1489 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1490 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1491
1492 /*
1493 * Needs to be page aligned:
1494 */
1495 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1496 goto err_gp;
1497
1498 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1499 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1500 if (is_error_hpa(para_state_hpa))
1501 goto err_gp;
1502
Uri Lublinab51a432007-02-21 18:25:21 +02001503 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001504 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
Shaohua Life5518812007-07-23 14:51:39 +08001505 para_state = kmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001506
1507 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1508 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1509
1510 para_state->host_version = KVM_PARA_API_VERSION;
1511 /*
1512 * We cannot support guests that try to register themselves
1513 * with a newer API version than the host supports:
1514 */
1515 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1516 para_state->ret = -KVM_EINVAL;
1517 goto err_kunmap_skip;
1518 }
1519
1520 hypercall_gpa = para_state->hypercall_gpa;
1521 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1522 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1523 if (is_error_hpa(hypercall_hpa)) {
1524 para_state->ret = -KVM_EINVAL;
1525 goto err_kunmap_skip;
1526 }
1527
1528 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1529 vcpu->para_state_page = para_state_page;
1530 vcpu->para_state_gpa = para_state_gpa;
1531 vcpu->hypercall_gpa = hypercall_gpa;
1532
Uri Lublinab51a432007-02-21 18:25:21 +02001533 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001534 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1535 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1536 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1537 kunmap_atomic(hypercall, KM_USER1);
1538
1539 para_state->ret = 0;
1540err_kunmap_skip:
Shaohua Life5518812007-07-23 14:51:39 +08001541 kunmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001542 return 0;
1543err_gp:
1544 return 1;
1545}
1546
Avi Kivity3bab1f52006-12-29 16:49:48 -08001547int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1548{
1549 u64 data;
1550
1551 switch (msr) {
1552 case 0xc0010010: /* SYSCFG */
1553 case 0xc0010015: /* HWCR */
1554 case MSR_IA32_PLATFORM_ID:
1555 case MSR_IA32_P5_MC_ADDR:
1556 case MSR_IA32_P5_MC_TYPE:
1557 case MSR_IA32_MC0_CTL:
1558 case MSR_IA32_MCG_STATUS:
1559 case MSR_IA32_MCG_CAP:
1560 case MSR_IA32_MC0_MISC:
1561 case MSR_IA32_MC0_MISC+4:
1562 case MSR_IA32_MC0_MISC+8:
1563 case MSR_IA32_MC0_MISC+12:
1564 case MSR_IA32_MC0_MISC+16:
1565 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001566 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001567 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001568 /* MTRR registers */
1569 case 0xfe:
1570 case 0x200 ... 0x2ff:
1571 data = 0;
1572 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001573 case 0xcd: /* fsb frequency */
1574 data = 3;
1575 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001576 case MSR_IA32_APICBASE:
1577 data = vcpu->apic_base;
1578 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001579 case MSR_IA32_MISC_ENABLE:
1580 data = vcpu->ia32_misc_enable_msr;
1581 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001582#ifdef CONFIG_X86_64
1583 case MSR_EFER:
1584 data = vcpu->shadow_efer;
1585 break;
1586#endif
1587 default:
1588 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1589 return 1;
1590 }
1591 *pdata = data;
1592 return 0;
1593}
1594EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1595
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596/*
1597 * Reads an msr value (of 'msr_index') into 'pdata'.
1598 * Returns 0 on success, non-0 otherwise.
1599 * Assumes vcpu_load() was already called.
1600 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001601int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
1603 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1604}
1605
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001606#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607
Avi Kivity3bab1f52006-12-29 16:49:48 -08001608static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 if (efer & EFER_RESERVED_BITS) {
1611 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1612 efer);
1613 inject_gp(vcpu);
1614 return;
1615 }
1616
1617 if (is_paging(vcpu)
1618 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1619 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1620 inject_gp(vcpu);
1621 return;
1622 }
1623
Avi Kivity7725f0b2006-12-13 00:34:01 -08001624 kvm_arch_ops->set_efer(vcpu, efer);
1625
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 efer &= ~EFER_LMA;
1627 efer |= vcpu->shadow_efer & EFER_LMA;
1628
1629 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631
1632#endif
1633
Avi Kivity3bab1f52006-12-29 16:49:48 -08001634int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1635{
1636 switch (msr) {
1637#ifdef CONFIG_X86_64
1638 case MSR_EFER:
1639 set_efer(vcpu, data);
1640 break;
1641#endif
1642 case MSR_IA32_MC0_STATUS:
1643 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1644 __FUNCTION__, data);
1645 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001646 case MSR_IA32_MCG_STATUS:
1647 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1648 __FUNCTION__, data);
1649 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001650 case MSR_IA32_UCODE_REV:
1651 case MSR_IA32_UCODE_WRITE:
1652 case 0x200 ... 0x2ff: /* MTRRs */
1653 break;
1654 case MSR_IA32_APICBASE:
1655 vcpu->apic_base = data;
1656 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001657 case MSR_IA32_MISC_ENABLE:
1658 vcpu->ia32_misc_enable_msr = data;
1659 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001660 /*
1661 * This is the 'probe whether the host is KVM' logic:
1662 */
1663 case MSR_KVM_API_MAGIC:
1664 return vcpu_register_para(vcpu, data);
1665
Avi Kivity3bab1f52006-12-29 16:49:48 -08001666 default:
1667 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1668 return 1;
1669 }
1670 return 0;
1671}
1672EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1673
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674/*
1675 * Writes msr value into into the appropriate "register".
1676 * Returns 0 on success, non-0 otherwise.
1677 * Assumes vcpu_load() was already called.
1678 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001679int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680{
1681 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1682}
1683
1684void kvm_resched(struct kvm_vcpu *vcpu)
1685{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001686 if (!need_resched())
1687 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689}
1690EXPORT_SYMBOL_GPL(kvm_resched);
1691
Avi Kivity06465c52007-02-28 20:46:53 +02001692void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1693{
1694 int i;
1695 u32 function;
1696 struct kvm_cpuid_entry *e, *best;
1697
1698 kvm_arch_ops->cache_regs(vcpu);
1699 function = vcpu->regs[VCPU_REGS_RAX];
1700 vcpu->regs[VCPU_REGS_RAX] = 0;
1701 vcpu->regs[VCPU_REGS_RBX] = 0;
1702 vcpu->regs[VCPU_REGS_RCX] = 0;
1703 vcpu->regs[VCPU_REGS_RDX] = 0;
1704 best = NULL;
1705 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1706 e = &vcpu->cpuid_entries[i];
1707 if (e->function == function) {
1708 best = e;
1709 break;
1710 }
1711 /*
1712 * Both basic or both extended?
1713 */
1714 if (((e->function ^ function) & 0x80000000) == 0)
1715 if (!best || e->function > best->function)
1716 best = e;
1717 }
1718 if (best) {
1719 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1720 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1721 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1722 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1723 }
1724 kvm_arch_ops->decache_regs(vcpu);
1725 kvm_arch_ops->skip_emulated_instruction(vcpu);
1726}
1727EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1728
Avi Kivity039576c2007-03-20 12:46:50 +02001729static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001730{
Avi Kivity039576c2007-03-20 12:46:50 +02001731 void *p = vcpu->pio_data;
1732 void *q;
1733 unsigned bytes;
1734 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1735
Avi Kivity039576c2007-03-20 12:46:50 +02001736 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1737 PAGE_KERNEL);
1738 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001739 free_pio_guest_pages(vcpu);
1740 return -ENOMEM;
1741 }
1742 q += vcpu->pio.guest_page_offset;
1743 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1744 if (vcpu->pio.in)
1745 memcpy(q, p, bytes);
1746 else
1747 memcpy(p, q, bytes);
1748 q -= vcpu->pio.guest_page_offset;
1749 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001750 free_pio_guest_pages(vcpu);
1751 return 0;
1752}
1753
1754static int complete_pio(struct kvm_vcpu *vcpu)
1755{
1756 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001757 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001758 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001759
1760 kvm_arch_ops->cache_regs(vcpu);
1761
1762 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001763 if (io->in)
1764 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001765 io->size);
1766 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001767 if (io->in) {
1768 r = pio_copy_data(vcpu);
1769 if (r) {
1770 kvm_arch_ops->cache_regs(vcpu);
1771 return r;
1772 }
1773 }
1774
Avi Kivity46fc1472007-02-22 19:39:30 +02001775 delta = 1;
1776 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001777 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001778 /*
1779 * The size of the register should really depend on
1780 * current address size.
1781 */
1782 vcpu->regs[VCPU_REGS_RCX] -= delta;
1783 }
Avi Kivity039576c2007-03-20 12:46:50 +02001784 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001785 delta = -delta;
1786 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001787 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001788 vcpu->regs[VCPU_REGS_RDI] += delta;
1789 else
1790 vcpu->regs[VCPU_REGS_RSI] += delta;
1791 }
1792
Avi Kivity46fc1472007-02-22 19:39:30 +02001793 kvm_arch_ops->decache_regs(vcpu);
1794
Avi Kivity039576c2007-03-20 12:46:50 +02001795 io->count -= io->cur_count;
1796 io->cur_count = 0;
1797
1798 if (!io->count)
1799 kvm_arch_ops->skip_emulated_instruction(vcpu);
1800 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001801}
1802
Eddie Dong65619eb2007-07-17 11:52:33 +03001803static void kernel_pio(struct kvm_io_device *pio_dev,
1804 struct kvm_vcpu *vcpu,
1805 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001806{
1807 /* TODO: String I/O for in kernel device */
1808
1809 if (vcpu->pio.in)
1810 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1811 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001812 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001813 else
1814 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1815 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001816 pd);
1817}
1818
1819static void pio_string_write(struct kvm_io_device *pio_dev,
1820 struct kvm_vcpu *vcpu)
1821{
1822 struct kvm_pio_request *io = &vcpu->pio;
1823 void *pd = vcpu->pio_data;
1824 int i;
1825
1826 for (i = 0; i < io->cur_count; i++) {
1827 kvm_iodevice_write(pio_dev, io->port,
1828 io->size,
1829 pd);
1830 pd += io->size;
1831 }
Eddie Dong74906342007-06-19 18:05:03 +03001832}
1833
Avi Kivity039576c2007-03-20 12:46:50 +02001834int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1835 int size, unsigned long count, int string, int down,
1836 gva_t address, int rep, unsigned port)
1837{
1838 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001839 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001840 int nr_pages = 1;
1841 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001842 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001843
1844 vcpu->run->exit_reason = KVM_EXIT_IO;
1845 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1846 vcpu->run->io.size = size;
1847 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1848 vcpu->run->io.count = count;
1849 vcpu->run->io.port = port;
1850 vcpu->pio.count = count;
1851 vcpu->pio.cur_count = count;
1852 vcpu->pio.size = size;
1853 vcpu->pio.in = in;
Eddie Dong74906342007-06-19 18:05:03 +03001854 vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001855 vcpu->pio.string = string;
1856 vcpu->pio.down = down;
1857 vcpu->pio.guest_page_offset = offset_in_page(address);
1858 vcpu->pio.rep = rep;
1859
Eddie Dong74906342007-06-19 18:05:03 +03001860 pio_dev = vcpu_find_pio_dev(vcpu, port);
Avi Kivity039576c2007-03-20 12:46:50 +02001861 if (!string) {
1862 kvm_arch_ops->cache_regs(vcpu);
1863 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1864 kvm_arch_ops->decache_regs(vcpu);
Eddie Dong74906342007-06-19 18:05:03 +03001865 if (pio_dev) {
Eddie Dong65619eb2007-07-17 11:52:33 +03001866 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
Eddie Dong74906342007-06-19 18:05:03 +03001867 complete_pio(vcpu);
1868 return 1;
1869 }
Avi Kivity039576c2007-03-20 12:46:50 +02001870 return 0;
1871 }
1872
1873 if (!count) {
1874 kvm_arch_ops->skip_emulated_instruction(vcpu);
1875 return 1;
1876 }
1877
1878 now = min(count, PAGE_SIZE / size);
1879
1880 if (!down)
1881 in_page = PAGE_SIZE - offset_in_page(address);
1882 else
1883 in_page = offset_in_page(address) + size;
1884 now = min(count, (unsigned long)in_page / size);
1885 if (!now) {
1886 /*
1887 * String I/O straddles page boundary. Pin two guest pages
1888 * so that we satisfy atomicity constraints. Do just one
1889 * transaction to avoid complexity.
1890 */
1891 nr_pages = 2;
1892 now = 1;
1893 }
1894 if (down) {
1895 /*
1896 * String I/O in reverse. Yuck. Kill the guest, fix later.
1897 */
1898 printk(KERN_ERR "kvm: guest string pio down\n");
1899 inject_gp(vcpu);
1900 return 1;
1901 }
1902 vcpu->run->io.count = now;
1903 vcpu->pio.cur_count = now;
1904
1905 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001906 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001907 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1908 if (page)
1909 get_page(page);
1910 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001911 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001912 if (!page) {
1913 inject_gp(vcpu);
1914 free_pio_guest_pages(vcpu);
1915 return 1;
1916 }
1917 }
1918
Eddie Dong65619eb2007-07-17 11:52:33 +03001919 if (!vcpu->pio.in) {
1920 /* string PIO write */
1921 ret = pio_copy_data(vcpu);
1922 if (ret >= 0 && pio_dev) {
1923 pio_string_write(pio_dev, vcpu);
1924 complete_pio(vcpu);
1925 if (vcpu->pio.count == 0)
1926 ret = 1;
1927 }
1928 } else if (pio_dev)
1929 printk(KERN_ERR "no string pio read support yet, "
1930 "port %x size %d count %ld\n",
1931 port, size, count);
1932
1933 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001934}
1935EXPORT_SYMBOL_GPL(kvm_setup_pio);
1936
Avi Kivitybccf2152007-02-21 18:04:26 +02001937static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001940 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941
Avi Kivitybccf2152007-02-21 18:04:26 +02001942 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
Avi Kivity1961d272007-03-05 19:46:05 +02001944 if (vcpu->sigset_active)
1945 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1946
Dor Laor54810342007-02-12 00:54:39 -08001947 /* re-sync apic's tpr */
1948 vcpu->cr8 = kvm_run->cr8;
1949
Avi Kivity02c83202007-04-29 15:02:17 +03001950 if (vcpu->pio.cur_count) {
1951 r = complete_pio(vcpu);
1952 if (r)
1953 goto out;
1954 }
1955
1956 if (vcpu->mmio_needed) {
1957 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1958 vcpu->mmio_read_completed = 1;
1959 vcpu->mmio_needed = 0;
1960 r = emulate_instruction(vcpu, kvm_run,
1961 vcpu->mmio_fault_cr2, 0);
1962 if (r == EMULATE_DO_MMIO) {
1963 /*
1964 * Read-modify-write. Back to userspace.
1965 */
Avi Kivity02c83202007-04-29 15:02:17 +03001966 r = 0;
1967 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001968 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 }
1970
Avi Kivity8eb7d332007-03-04 14:17:08 +02001971 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001972 kvm_arch_ops->cache_regs(vcpu);
1973 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1974 kvm_arch_ops->decache_regs(vcpu);
1975 }
1976
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977 r = kvm_arch_ops->run(vcpu, kvm_run);
1978
Avi Kivity039576c2007-03-20 12:46:50 +02001979out:
Avi Kivity1961d272007-03-05 19:46:05 +02001980 if (vcpu->sigset_active)
1981 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1982
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983 vcpu_put(vcpu);
1984 return r;
1985}
1986
Avi Kivitybccf2152007-02-21 18:04:26 +02001987static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1988 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989{
Avi Kivitybccf2152007-02-21 18:04:26 +02001990 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991
1992 kvm_arch_ops->cache_regs(vcpu);
1993
1994 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1995 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1996 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1997 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1998 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1999 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2000 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2001 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002002#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2004 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2005 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2006 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2007 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2008 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2009 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2010 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2011#endif
2012
2013 regs->rip = vcpu->rip;
2014 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
2015
2016 /*
2017 * Don't leak debug flags in case they were set for guest debugging
2018 */
2019 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2020 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2021
2022 vcpu_put(vcpu);
2023
2024 return 0;
2025}
2026
Avi Kivitybccf2152007-02-21 18:04:26 +02002027static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2028 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029{
Avi Kivitybccf2152007-02-21 18:04:26 +02002030 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031
2032 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2033 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2034 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2035 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2036 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2037 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2038 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2039 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002040#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2042 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2043 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2044 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2045 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2046 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2047 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2048 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2049#endif
2050
2051 vcpu->rip = regs->rip;
2052 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2053
2054 kvm_arch_ops->decache_regs(vcpu);
2055
2056 vcpu_put(vcpu);
2057
2058 return 0;
2059}
2060
2061static void get_segment(struct kvm_vcpu *vcpu,
2062 struct kvm_segment *var, int seg)
2063{
2064 return kvm_arch_ops->get_segment(vcpu, var, seg);
2065}
2066
Avi Kivitybccf2152007-02-21 18:04:26 +02002067static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2068 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002069{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070 struct descriptor_table dt;
2071
Avi Kivitybccf2152007-02-21 18:04:26 +02002072 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073
2074 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2075 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2076 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2077 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2078 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2079 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2080
2081 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2082 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2083
2084 kvm_arch_ops->get_idt(vcpu, &dt);
2085 sregs->idt.limit = dt.limit;
2086 sregs->idt.base = dt.base;
2087 kvm_arch_ops->get_gdt(vcpu, &dt);
2088 sregs->gdt.limit = dt.limit;
2089 sregs->gdt.base = dt.base;
2090
Anthony Liguori25c4c272007-04-27 09:29:21 +03002091 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 sregs->cr0 = vcpu->cr0;
2093 sregs->cr2 = vcpu->cr2;
2094 sregs->cr3 = vcpu->cr3;
2095 sregs->cr4 = vcpu->cr4;
2096 sregs->cr8 = vcpu->cr8;
2097 sregs->efer = vcpu->shadow_efer;
2098 sregs->apic_base = vcpu->apic_base;
2099
2100 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2101 sizeof sregs->interrupt_bitmap);
2102
2103 vcpu_put(vcpu);
2104
2105 return 0;
2106}
2107
2108static void set_segment(struct kvm_vcpu *vcpu,
2109 struct kvm_segment *var, int seg)
2110{
2111 return kvm_arch_ops->set_segment(vcpu, var, seg);
2112}
2113
Avi Kivitybccf2152007-02-21 18:04:26 +02002114static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2115 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 int mmu_reset_needed = 0;
2118 int i;
2119 struct descriptor_table dt;
2120
Avi Kivitybccf2152007-02-21 18:04:26 +02002121 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 dt.limit = sregs->idt.limit;
2124 dt.base = sregs->idt.base;
2125 kvm_arch_ops->set_idt(vcpu, &dt);
2126 dt.limit = sregs->gdt.limit;
2127 dt.base = sregs->gdt.base;
2128 kvm_arch_ops->set_gdt(vcpu, &dt);
2129
2130 vcpu->cr2 = sregs->cr2;
2131 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2132 vcpu->cr3 = sregs->cr3;
2133
2134 vcpu->cr8 = sregs->cr8;
2135
2136 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002137#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2139#endif
2140 vcpu->apic_base = sregs->apic_base;
2141
Anthony Liguori25c4c272007-04-27 09:29:21 +03002142 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002143
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002145 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146
2147 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2148 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002149 if (!is_long_mode(vcpu) && is_pae(vcpu))
2150 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151
2152 if (mmu_reset_needed)
2153 kvm_mmu_reset_context(vcpu);
2154
2155 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2156 sizeof vcpu->irq_pending);
2157 vcpu->irq_summary = 0;
Rusty Russell9eb829c2007-07-18 13:05:58 +10002158 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 if (vcpu->irq_pending[i])
2160 __set_bit(i, &vcpu->irq_summary);
2161
Avi Kivity024aa1c2007-03-21 13:44:58 +02002162 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2163 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2164 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2165 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2166 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2167 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2168
2169 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2170 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2171
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 vcpu_put(vcpu);
2173
2174 return 0;
2175}
2176
2177/*
2178 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2179 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002180 *
2181 * This list is modified at module load time to reflect the
2182 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183 */
2184static u32 msrs_to_save[] = {
2185 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2186 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002187#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2189#endif
2190 MSR_IA32_TIME_STAMP_COUNTER,
2191};
2192
Michael Riepebf591b22006-12-22 01:05:36 -08002193static unsigned num_msrs_to_save;
2194
Avi Kivity6f00e682007-01-26 00:56:40 -08002195static u32 emulated_msrs[] = {
2196 MSR_IA32_MISC_ENABLE,
2197};
2198
Michael Riepebf591b22006-12-22 01:05:36 -08002199static __init void kvm_init_msr_list(void)
2200{
2201 u32 dummy[2];
2202 unsigned i, j;
2203
2204 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2205 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2206 continue;
2207 if (j < i)
2208 msrs_to_save[j] = msrs_to_save[i];
2209 j++;
2210 }
2211 num_msrs_to_save = j;
2212}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213
2214/*
2215 * Adapt set_msr() to msr_io()'s calling convention
2216 */
2217static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2218{
Avi Kivity35f3f282007-07-17 14:20:30 +03002219 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220}
2221
2222/*
2223 * Read or write a bunch of msrs. All parameters are kernel addresses.
2224 *
2225 * @return number of msrs set successfully.
2226 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002227static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 struct kvm_msr_entry *entries,
2229 int (*do_msr)(struct kvm_vcpu *vcpu,
2230 unsigned index, u64 *data))
2231{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232 int i;
2233
Avi Kivitybccf2152007-02-21 18:04:26 +02002234 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235
2236 for (i = 0; i < msrs->nmsrs; ++i)
2237 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2238 break;
2239
2240 vcpu_put(vcpu);
2241
2242 return i;
2243}
2244
2245/*
2246 * Read or write a bunch of msrs. Parameters are user addresses.
2247 *
2248 * @return number of msrs set successfully.
2249 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002250static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251 int (*do_msr)(struct kvm_vcpu *vcpu,
2252 unsigned index, u64 *data),
2253 int writeback)
2254{
2255 struct kvm_msrs msrs;
2256 struct kvm_msr_entry *entries;
2257 int r, n;
2258 unsigned size;
2259
2260 r = -EFAULT;
2261 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2262 goto out;
2263
2264 r = -E2BIG;
2265 if (msrs.nmsrs >= MAX_IO_MSRS)
2266 goto out;
2267
2268 r = -ENOMEM;
2269 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2270 entries = vmalloc(size);
2271 if (!entries)
2272 goto out;
2273
2274 r = -EFAULT;
2275 if (copy_from_user(entries, user_msrs->entries, size))
2276 goto out_free;
2277
Avi Kivitybccf2152007-02-21 18:04:26 +02002278 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 if (r < 0)
2280 goto out_free;
2281
2282 r = -EFAULT;
2283 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2284 goto out_free;
2285
2286 r = n;
2287
2288out_free:
2289 vfree(entries);
2290out:
2291 return r;
2292}
2293
2294/*
2295 * Translate a guest virtual address to a guest physical address.
2296 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002297static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2298 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299{
2300 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 gpa_t gpa;
2302
Avi Kivitybccf2152007-02-21 18:04:26 +02002303 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002304 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2306 tr->physical_address = gpa;
2307 tr->valid = gpa != UNMAPPED_GVA;
2308 tr->writeable = 1;
2309 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002310 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 vcpu_put(vcpu);
2312
2313 return 0;
2314}
2315
Avi Kivitybccf2152007-02-21 18:04:26 +02002316static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2317 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 if (irq->irq < 0 || irq->irq >= 256)
2320 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002321 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
2323 set_bit(irq->irq, vcpu->irq_pending);
2324 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2325
2326 vcpu_put(vcpu);
2327
2328 return 0;
2329}
2330
Avi Kivitybccf2152007-02-21 18:04:26 +02002331static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2332 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 int r;
2335
Avi Kivitybccf2152007-02-21 18:04:26 +02002336 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337
2338 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2339
2340 vcpu_put(vcpu);
2341
2342 return r;
2343}
2344
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002345static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2346 unsigned long address,
2347 int *type)
2348{
2349 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2350 unsigned long pgoff;
2351 struct page *page;
2352
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002353 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002354 if (pgoff == 0)
2355 page = virt_to_page(vcpu->run);
2356 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2357 page = virt_to_page(vcpu->pio_data);
2358 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002359 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002360 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002361 if (type != NULL)
2362 *type = VM_FAULT_MINOR;
2363
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002364 return page;
2365}
2366
2367static struct vm_operations_struct kvm_vcpu_vm_ops = {
2368 .nopage = kvm_vcpu_nopage,
2369};
2370
2371static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2372{
2373 vma->vm_ops = &kvm_vcpu_vm_ops;
2374 return 0;
2375}
2376
Avi Kivitybccf2152007-02-21 18:04:26 +02002377static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2378{
2379 struct kvm_vcpu *vcpu = filp->private_data;
2380
2381 fput(vcpu->kvm->filp);
2382 return 0;
2383}
2384
2385static struct file_operations kvm_vcpu_fops = {
2386 .release = kvm_vcpu_release,
2387 .unlocked_ioctl = kvm_vcpu_ioctl,
2388 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002389 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002390};
2391
2392/*
2393 * Allocates an inode for the vcpu.
2394 */
2395static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2396{
2397 int fd, r;
2398 struct inode *inode;
2399 struct file *file;
2400
Avi Kivityd6d28162007-06-28 08:38:16 -04002401 r = anon_inode_getfd(&fd, &inode, &file,
2402 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2403 if (r)
2404 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002405 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002406 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002407}
2408
Avi Kivityc5ea7662007-02-20 18:41:05 +02002409/*
2410 * Creates some virtual cpus. Good luck creating more than one.
2411 */
2412static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2413{
2414 int r;
2415 struct kvm_vcpu *vcpu;
2416
Avi Kivityc5ea7662007-02-20 18:41:05 +02002417 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002418 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002419
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002420 vcpu = kvm_arch_ops->vcpu_create(kvm, n);
2421 if (IS_ERR(vcpu))
2422 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002423
Avi Kivity15ad7142007-07-11 18:17:21 +03002424 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2425
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002426 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002427 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002428 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002429 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002430 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002431
Shaohua Li11ec2802007-07-23 14:51:37 +08002432 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002433 if (kvm->vcpus[n]) {
2434 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002435 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002436 goto mmu_unload;
2437 }
2438 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002439 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002440
2441 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002442 r = create_vcpu_fd(vcpu);
2443 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002444 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002445 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002446
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002447unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002448 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002449 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002450 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002451
2452mmu_unload:
2453 vcpu_load(vcpu);
2454 kvm_mmu_unload(vcpu);
2455 vcpu_put(vcpu);
2456
2457free_vcpu:
2458 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002459 return r;
2460}
2461
Eddie Dong2cc51562007-05-21 07:28:09 +03002462static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2463{
2464 u64 efer;
2465 int i;
2466 struct kvm_cpuid_entry *e, *entry;
2467
2468 rdmsrl(MSR_EFER, efer);
2469 entry = NULL;
2470 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2471 e = &vcpu->cpuid_entries[i];
2472 if (e->function == 0x80000001) {
2473 entry = e;
2474 break;
2475 }
2476 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002477 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002478 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002479 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002480 }
2481}
2482
Avi Kivity06465c52007-02-28 20:46:53 +02002483static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2484 struct kvm_cpuid *cpuid,
2485 struct kvm_cpuid_entry __user *entries)
2486{
2487 int r;
2488
2489 r = -E2BIG;
2490 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2491 goto out;
2492 r = -EFAULT;
2493 if (copy_from_user(&vcpu->cpuid_entries, entries,
2494 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2495 goto out;
2496 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002497 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002498 return 0;
2499
2500out:
2501 return r;
2502}
2503
Avi Kivity1961d272007-03-05 19:46:05 +02002504static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2505{
2506 if (sigset) {
2507 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2508 vcpu->sigset_active = 1;
2509 vcpu->sigset = *sigset;
2510 } else
2511 vcpu->sigset_active = 0;
2512 return 0;
2513}
2514
Avi Kivityb8836732007-04-01 16:34:31 +03002515/*
2516 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2517 * we have asm/x86/processor.h
2518 */
2519struct fxsave {
2520 u16 cwd;
2521 u16 swd;
2522 u16 twd;
2523 u16 fop;
2524 u64 rip;
2525 u64 rdp;
2526 u32 mxcsr;
2527 u32 mxcsr_mask;
2528 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2529#ifdef CONFIG_X86_64
2530 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2531#else
2532 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2533#endif
2534};
2535
2536static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2537{
2538 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2539
2540 vcpu_load(vcpu);
2541
2542 memcpy(fpu->fpr, fxsave->st_space, 128);
2543 fpu->fcw = fxsave->cwd;
2544 fpu->fsw = fxsave->swd;
2545 fpu->ftwx = fxsave->twd;
2546 fpu->last_opcode = fxsave->fop;
2547 fpu->last_ip = fxsave->rip;
2548 fpu->last_dp = fxsave->rdp;
2549 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2550
2551 vcpu_put(vcpu);
2552
2553 return 0;
2554}
2555
2556static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2557{
2558 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2559
2560 vcpu_load(vcpu);
2561
2562 memcpy(fxsave->st_space, fpu->fpr, 128);
2563 fxsave->cwd = fpu->fcw;
2564 fxsave->swd = fpu->fsw;
2565 fxsave->twd = fpu->ftwx;
2566 fxsave->fop = fpu->last_opcode;
2567 fxsave->rip = fpu->last_ip;
2568 fxsave->rdp = fpu->last_dp;
2569 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2570
2571 vcpu_put(vcpu);
2572
2573 return 0;
2574}
2575
Avi Kivitybccf2152007-02-21 18:04:26 +02002576static long kvm_vcpu_ioctl(struct file *filp,
2577 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578{
Avi Kivitybccf2152007-02-21 18:04:26 +02002579 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002580 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 int r = -EINVAL;
2582
2583 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002584 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002585 r = -EINVAL;
2586 if (arg)
2587 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002588 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 case KVM_GET_REGS: {
2591 struct kvm_regs kvm_regs;
2592
Avi Kivitybccf2152007-02-21 18:04:26 +02002593 memset(&kvm_regs, 0, sizeof kvm_regs);
2594 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 if (r)
2596 goto out;
2597 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002598 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599 goto out;
2600 r = 0;
2601 break;
2602 }
2603 case KVM_SET_REGS: {
2604 struct kvm_regs kvm_regs;
2605
2606 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002607 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002609 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 if (r)
2611 goto out;
2612 r = 0;
2613 break;
2614 }
2615 case KVM_GET_SREGS: {
2616 struct kvm_sregs kvm_sregs;
2617
Avi Kivitybccf2152007-02-21 18:04:26 +02002618 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2619 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 if (r)
2621 goto out;
2622 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002623 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 goto out;
2625 r = 0;
2626 break;
2627 }
2628 case KVM_SET_SREGS: {
2629 struct kvm_sregs kvm_sregs;
2630
2631 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002632 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002634 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 if (r)
2636 goto out;
2637 r = 0;
2638 break;
2639 }
2640 case KVM_TRANSLATE: {
2641 struct kvm_translation tr;
2642
2643 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002644 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002646 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 if (r)
2648 goto out;
2649 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002650 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 goto out;
2652 r = 0;
2653 break;
2654 }
2655 case KVM_INTERRUPT: {
2656 struct kvm_interrupt irq;
2657
2658 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002659 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002661 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 if (r)
2663 goto out;
2664 r = 0;
2665 break;
2666 }
2667 case KVM_DEBUG_GUEST: {
2668 struct kvm_debug_guest dbg;
2669
2670 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002671 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002673 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 if (r)
2675 goto out;
2676 r = 0;
2677 break;
2678 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002679 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002680 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002681 break;
2682 case KVM_SET_MSRS:
2683 r = msr_io(vcpu, argp, do_set_msr, 0);
2684 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002685 case KVM_SET_CPUID: {
2686 struct kvm_cpuid __user *cpuid_arg = argp;
2687 struct kvm_cpuid cpuid;
2688
2689 r = -EFAULT;
2690 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2691 goto out;
2692 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2693 if (r)
2694 goto out;
2695 break;
2696 }
Avi Kivity1961d272007-03-05 19:46:05 +02002697 case KVM_SET_SIGNAL_MASK: {
2698 struct kvm_signal_mask __user *sigmask_arg = argp;
2699 struct kvm_signal_mask kvm_sigmask;
2700 sigset_t sigset, *p;
2701
2702 p = NULL;
2703 if (argp) {
2704 r = -EFAULT;
2705 if (copy_from_user(&kvm_sigmask, argp,
2706 sizeof kvm_sigmask))
2707 goto out;
2708 r = -EINVAL;
2709 if (kvm_sigmask.len != sizeof sigset)
2710 goto out;
2711 r = -EFAULT;
2712 if (copy_from_user(&sigset, sigmask_arg->sigset,
2713 sizeof sigset))
2714 goto out;
2715 p = &sigset;
2716 }
2717 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2718 break;
2719 }
Avi Kivityb8836732007-04-01 16:34:31 +03002720 case KVM_GET_FPU: {
2721 struct kvm_fpu fpu;
2722
2723 memset(&fpu, 0, sizeof fpu);
2724 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2725 if (r)
2726 goto out;
2727 r = -EFAULT;
2728 if (copy_to_user(argp, &fpu, sizeof fpu))
2729 goto out;
2730 r = 0;
2731 break;
2732 }
2733 case KVM_SET_FPU: {
2734 struct kvm_fpu fpu;
2735
2736 r = -EFAULT;
2737 if (copy_from_user(&fpu, argp, sizeof fpu))
2738 goto out;
2739 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2740 if (r)
2741 goto out;
2742 r = 0;
2743 break;
2744 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002745 default:
2746 ;
2747 }
2748out:
2749 return r;
2750}
2751
2752static long kvm_vm_ioctl(struct file *filp,
2753 unsigned int ioctl, unsigned long arg)
2754{
2755 struct kvm *kvm = filp->private_data;
2756 void __user *argp = (void __user *)arg;
2757 int r = -EINVAL;
2758
2759 switch (ioctl) {
2760 case KVM_CREATE_VCPU:
2761 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2762 if (r < 0)
2763 goto out;
2764 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 case KVM_SET_MEMORY_REGION: {
2766 struct kvm_memory_region kvm_mem;
2767
2768 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002769 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002771 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 if (r)
2773 goto out;
2774 break;
2775 }
2776 case KVM_GET_DIRTY_LOG: {
2777 struct kvm_dirty_log log;
2778
2779 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002780 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002782 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 if (r)
2784 goto out;
2785 break;
2786 }
Avi Kivitye8207542007-03-30 16:54:30 +03002787 case KVM_SET_MEMORY_ALIAS: {
2788 struct kvm_memory_alias alias;
2789
2790 r = -EFAULT;
2791 if (copy_from_user(&alias, argp, sizeof alias))
2792 goto out;
2793 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2794 if (r)
2795 goto out;
2796 break;
2797 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002798 default:
2799 ;
2800 }
2801out:
2802 return r;
2803}
2804
2805static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2806 unsigned long address,
2807 int *type)
2808{
2809 struct kvm *kvm = vma->vm_file->private_data;
2810 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002811 struct page *page;
2812
Avi Kivityf17abe92007-02-21 19:28:04 +02002813 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002814 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002815 if (!page)
2816 return NOPAGE_SIGBUS;
2817 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002818 if (type != NULL)
2819 *type = VM_FAULT_MINOR;
2820
Avi Kivityf17abe92007-02-21 19:28:04 +02002821 return page;
2822}
2823
2824static struct vm_operations_struct kvm_vm_vm_ops = {
2825 .nopage = kvm_vm_nopage,
2826};
2827
2828static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2829{
2830 vma->vm_ops = &kvm_vm_vm_ops;
2831 return 0;
2832}
2833
2834static struct file_operations kvm_vm_fops = {
2835 .release = kvm_vm_release,
2836 .unlocked_ioctl = kvm_vm_ioctl,
2837 .compat_ioctl = kvm_vm_ioctl,
2838 .mmap = kvm_vm_mmap,
2839};
2840
2841static int kvm_dev_ioctl_create_vm(void)
2842{
2843 int fd, r;
2844 struct inode *inode;
2845 struct file *file;
2846 struct kvm *kvm;
2847
Avi Kivityf17abe92007-02-21 19:28:04 +02002848 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002849 if (IS_ERR(kvm))
2850 return PTR_ERR(kvm);
2851 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2852 if (r) {
2853 kvm_destroy_vm(kvm);
2854 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002855 }
2856
Avi Kivitybccf2152007-02-21 18:04:26 +02002857 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002858
Avi Kivityf17abe92007-02-21 19:28:04 +02002859 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002860}
2861
2862static long kvm_dev_ioctl(struct file *filp,
2863 unsigned int ioctl, unsigned long arg)
2864{
2865 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002866 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002867
2868 switch (ioctl) {
2869 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002870 r = -EINVAL;
2871 if (arg)
2872 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002873 r = KVM_API_VERSION;
2874 break;
2875 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002876 r = -EINVAL;
2877 if (arg)
2878 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002879 r = kvm_dev_ioctl_create_vm();
2880 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002882 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 struct kvm_msr_list msr_list;
2884 unsigned n;
2885
2886 r = -EFAULT;
2887 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2888 goto out;
2889 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002890 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2892 goto out;
2893 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002894 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 goto out;
2896 r = -EFAULT;
2897 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002898 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002900 if (copy_to_user(user_msr_list->indices
2901 + num_msrs_to_save * sizeof(u32),
2902 &emulated_msrs,
2903 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2904 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002906 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002908 case KVM_CHECK_EXTENSION:
2909 /*
2910 * No extensions defined at present.
2911 */
2912 r = 0;
2913 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002914 case KVM_GET_VCPU_MMAP_SIZE:
2915 r = -EINVAL;
2916 if (arg)
2917 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002918 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002919 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 default:
2921 ;
2922 }
2923out:
2924 return r;
2925}
2926
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927static struct file_operations kvm_chardev_ops = {
2928 .open = kvm_dev_open,
2929 .release = kvm_dev_release,
2930 .unlocked_ioctl = kvm_dev_ioctl,
2931 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932};
2933
2934static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002935 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 "kvm",
2937 &kvm_chardev_ops,
2938};
2939
Avi Kivity774c47f2007-02-12 00:54:47 -08002940/*
2941 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2942 * cached on it.
2943 */
2944static void decache_vcpus_on_cpu(int cpu)
2945{
2946 struct kvm *vm;
2947 struct kvm_vcpu *vcpu;
2948 int i;
2949
2950 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08002951 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08002952 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002953 vcpu = vm->vcpus[i];
2954 if (!vcpu)
2955 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08002956 /*
2957 * If the vcpu is locked, then it is running on some
2958 * other cpu and therefore it is not cached on the
2959 * cpu in question.
2960 *
2961 * If it's not locked, check the last cpu it executed
2962 * on.
2963 */
2964 if (mutex_trylock(&vcpu->mutex)) {
2965 if (vcpu->cpu == cpu) {
2966 kvm_arch_ops->vcpu_decache(vcpu);
2967 vcpu->cpu = -1;
2968 }
2969 mutex_unlock(&vcpu->mutex);
2970 }
2971 }
2972 spin_unlock(&kvm_lock);
2973}
2974
Avi Kivity1b6c0162007-05-24 13:03:52 +03002975static void hardware_enable(void *junk)
2976{
2977 int cpu = raw_smp_processor_id();
2978
2979 if (cpu_isset(cpu, cpus_hardware_enabled))
2980 return;
2981 cpu_set(cpu, cpus_hardware_enabled);
2982 kvm_arch_ops->hardware_enable(NULL);
2983}
2984
2985static void hardware_disable(void *junk)
2986{
2987 int cpu = raw_smp_processor_id();
2988
2989 if (!cpu_isset(cpu, cpus_hardware_enabled))
2990 return;
2991 cpu_clear(cpu, cpus_hardware_enabled);
2992 decache_vcpus_on_cpu(cpu);
2993 kvm_arch_ops->hardware_disable(NULL);
2994}
2995
Avi Kivity774c47f2007-02-12 00:54:47 -08002996static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2997 void *v)
2998{
2999 int cpu = (long)v;
3000
3001 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003002 case CPU_DYING:
3003 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003004 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3005 cpu);
3006 hardware_disable(NULL);
3007 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003008 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003009 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003010 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3011 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003012 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003013 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003014 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003015 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003016 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3017 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003018 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003019 break;
3020 }
3021 return NOTIFY_OK;
3022}
3023
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003024static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3025 void *v)
3026{
3027 if (val == SYS_RESTART) {
3028 /*
3029 * Some (well, at least mine) BIOSes hang on reboot if
3030 * in vmx root mode.
3031 */
3032 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3033 on_each_cpu(hardware_disable, NULL, 0, 1);
3034 }
3035 return NOTIFY_OK;
3036}
3037
3038static struct notifier_block kvm_reboot_notifier = {
3039 .notifier_call = kvm_reboot,
3040 .priority = 0,
3041};
3042
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003043void kvm_io_bus_init(struct kvm_io_bus *bus)
3044{
3045 memset(bus, 0, sizeof(*bus));
3046}
3047
3048void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3049{
3050 int i;
3051
3052 for (i = 0; i < bus->dev_count; i++) {
3053 struct kvm_io_device *pos = bus->devs[i];
3054
3055 kvm_iodevice_destructor(pos);
3056 }
3057}
3058
3059struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3060{
3061 int i;
3062
3063 for (i = 0; i < bus->dev_count; i++) {
3064 struct kvm_io_device *pos = bus->devs[i];
3065
3066 if (pos->in_range(pos, addr))
3067 return pos;
3068 }
3069
3070 return NULL;
3071}
3072
3073void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3074{
3075 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3076
3077 bus->devs[bus->dev_count++] = dev;
3078}
3079
Avi Kivity774c47f2007-02-12 00:54:47 -08003080static struct notifier_block kvm_cpu_notifier = {
3081 .notifier_call = kvm_cpu_hotplug,
3082 .priority = 20, /* must be > scheduler priority */
3083};
3084
Avi Kivity1165f5f2007-04-19 17:27:43 +03003085static u64 stat_get(void *_offset)
3086{
3087 unsigned offset = (long)_offset;
3088 u64 total = 0;
3089 struct kvm *kvm;
3090 struct kvm_vcpu *vcpu;
3091 int i;
3092
3093 spin_lock(&kvm_lock);
3094 list_for_each_entry(kvm, &vm_list, vm_list)
3095 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003096 vcpu = kvm->vcpus[i];
3097 if (vcpu)
3098 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003099 }
3100 spin_unlock(&kvm_lock);
3101 return total;
3102}
3103
3104static void stat_set(void *offset, u64 val)
3105{
3106}
3107
3108DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3109
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110static __init void kvm_init_debug(void)
3111{
3112 struct kvm_stats_debugfs_item *p;
3113
Al Viro8b6d44c2007-02-09 16:38:40 +00003114 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003116 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3117 (void *)(long)p->offset,
3118 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119}
3120
3121static void kvm_exit_debug(void)
3122{
3123 struct kvm_stats_debugfs_item *p;
3124
3125 for (p = debugfs_entries; p->name; ++p)
3126 debugfs_remove(p->dentry);
3127 debugfs_remove(debugfs_dir);
3128}
3129
Avi Kivity59ae6c62007-02-12 00:54:48 -08003130static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3131{
Avi Kivity4267c412007-05-24 13:09:41 +03003132 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003133 return 0;
3134}
3135
3136static int kvm_resume(struct sys_device *dev)
3137{
Avi Kivity4267c412007-05-24 13:09:41 +03003138 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003139 return 0;
3140}
3141
3142static struct sysdev_class kvm_sysdev_class = {
3143 set_kset_name("kvm"),
3144 .suspend = kvm_suspend,
3145 .resume = kvm_resume,
3146};
3147
3148static struct sys_device kvm_sysdev = {
3149 .id = 0,
3150 .cls = &kvm_sysdev_class,
3151};
3152
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153hpa_t bad_page_address;
3154
Avi Kivity15ad7142007-07-11 18:17:21 +03003155static inline
3156struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3157{
3158 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3159}
3160
3161static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3162{
3163 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3164
3165 kvm_arch_ops->vcpu_load(vcpu, cpu);
3166}
3167
3168static void kvm_sched_out(struct preempt_notifier *pn,
3169 struct task_struct *next)
3170{
3171 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3172
3173 kvm_arch_ops->vcpu_put(vcpu);
3174}
3175
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3177{
3178 int r;
3179
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003180 if (kvm_arch_ops) {
3181 printk(KERN_ERR "kvm: already loaded the other module\n");
3182 return -EEXIST;
3183 }
3184
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003185 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 printk(KERN_ERR "kvm: no hardware support\n");
3187 return -EOPNOTSUPP;
3188 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003189 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190 printk(KERN_ERR "kvm: disabled by bios\n");
3191 return -EOPNOTSUPP;
3192 }
3193
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003194 kvm_arch_ops = ops;
3195
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 r = kvm_arch_ops->hardware_setup();
3197 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003198 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199
Avi Kivity1b6c0162007-05-24 13:03:52 +03003200 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003201 r = register_cpu_notifier(&kvm_cpu_notifier);
3202 if (r)
3203 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 register_reboot_notifier(&kvm_reboot_notifier);
3205
Avi Kivity59ae6c62007-02-12 00:54:48 -08003206 r = sysdev_class_register(&kvm_sysdev_class);
3207 if (r)
3208 goto out_free_2;
3209
3210 r = sysdev_register(&kvm_sysdev);
3211 if (r)
3212 goto out_free_3;
3213
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 kvm_chardev_ops.owner = module;
3215
3216 r = misc_register(&kvm_dev);
3217 if (r) {
3218 printk (KERN_ERR "kvm: misc device register failed\n");
3219 goto out_free;
3220 }
3221
Avi Kivity15ad7142007-07-11 18:17:21 +03003222 kvm_preempt_ops.sched_in = kvm_sched_in;
3223 kvm_preempt_ops.sched_out = kvm_sched_out;
3224
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 return r;
3226
3227out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003228 sysdev_unregister(&kvm_sysdev);
3229out_free_3:
3230 sysdev_class_unregister(&kvm_sysdev_class);
3231out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003233 unregister_cpu_notifier(&kvm_cpu_notifier);
3234out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003235 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003237out:
3238 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 return r;
3240}
3241
3242void kvm_exit_arch(void)
3243{
3244 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003245 sysdev_unregister(&kvm_sysdev);
3246 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003248 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003249 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003251 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252}
3253
3254static __init int kvm_init(void)
3255{
3256 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003257 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258
Avi Kivityb5a33a72007-04-15 16:31:09 +03003259 r = kvm_mmu_module_init();
3260 if (r)
3261 goto out4;
3262
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 kvm_init_debug();
3264
Michael Riepebf591b22006-12-22 01:05:36 -08003265 kvm_init_msr_list();
3266
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3268 r = -ENOMEM;
3269 goto out;
3270 }
3271
3272 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3273 memset(__va(bad_page_address), 0, PAGE_SIZE);
3274
Avi Kivity58e690e2007-02-26 16:29:43 +02003275 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276
3277out:
3278 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003279 kvm_mmu_module_exit();
3280out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 return r;
3282}
3283
3284static __exit void kvm_exit(void)
3285{
3286 kvm_exit_debug();
3287 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003288 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289}
3290
3291module_init(kvm_init)
3292module_exit(kvm_exit)
3293
3294EXPORT_SYMBOL_GPL(kvm_init_arch);
3295EXPORT_SYMBOL_GPL(kvm_exit_arch);