blob: 20947462f40185dc9aef08b6d48c89e1776d74cf [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
57#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080058
59static struct kvm_stats_debugfs_item {
60 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030061 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080062 struct dentry *dentry;
63} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030064 { "pf_fixed", STAT_OFFSET(pf_fixed) },
65 { "pf_guest", STAT_OFFSET(pf_guest) },
66 { "tlb_flush", STAT_OFFSET(tlb_flush) },
67 { "invlpg", STAT_OFFSET(invlpg) },
68 { "exits", STAT_OFFSET(exits) },
69 { "io_exits", STAT_OFFSET(io_exits) },
70 { "mmio_exits", STAT_OFFSET(mmio_exits) },
71 { "signal_exits", STAT_OFFSET(signal_exits) },
72 { "irq_window", STAT_OFFSET(irq_window_exits) },
73 { "halt_exits", STAT_OFFSET(halt_exits) },
74 { "request_irq", STAT_OFFSET(request_irq_exits) },
75 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030076 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030077 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030078 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080079};
80
81static struct dentry *debugfs_dir;
82
83#define MAX_IO_MSRS 256
84
Rusty Russell707d92f2007-07-17 23:19:08 +100085#define CR0_RESERVED_BITS \
86 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
87 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
88 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100089#define CR4_RESERVED_BITS \
90 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
91 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
92 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
93 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
94
Rusty Russell7075bc82007-07-17 23:37:17 +100095#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096#define EFER_RESERVED_BITS 0xfffffffffffff2fe
97
Avi Kivity05b3e0c2006-12-13 00:33:45 -080098#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080099// LDT or TSS descriptor in the GDT. 16 bytes.
100struct segment_descriptor_64 {
101 struct segment_descriptor s;
102 u32 base_higher;
103 u32 pad_zero;
104};
105
106#endif
107
Avi Kivitybccf2152007-02-21 18:04:26 +0200108static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
109 unsigned long arg);
110
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111unsigned long segment_base(u16 selector)
112{
113 struct descriptor_table gdt;
114 struct segment_descriptor *d;
115 unsigned long table_base;
116 typedef unsigned long ul;
117 unsigned long v;
118
119 if (selector == 0)
120 return 0;
121
122 asm ("sgdt %0" : "=m"(gdt));
123 table_base = gdt.base;
124
125 if (selector & 4) { /* from ldt */
126 u16 ldt_selector;
127
128 asm ("sldt %0" : "=g"(ldt_selector));
129 table_base = segment_base(ldt_selector);
130 }
131 d = (struct segment_descriptor *)(table_base + (selector & ~7));
132 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800133#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800134 if (d->system == 0
135 && (d->type == 2 || d->type == 9 || d->type == 11))
136 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
137#endif
138 return v;
139}
140EXPORT_SYMBOL_GPL(segment_base);
141
James Morris5aacf0c2006-12-22 01:04:55 -0800142static inline int valid_vcpu(int n)
143{
144 return likely(n >= 0 && n < KVM_MAX_VCPUS);
145}
146
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200147int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
148 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149{
150 unsigned char *host_buf = dest;
151 unsigned long req_size = size;
152
153 while (size) {
154 hpa_t paddr;
155 unsigned now;
156 unsigned offset;
157 hva_t guest_buf;
158
159 paddr = gva_to_hpa(vcpu, addr);
160
161 if (is_error_hpa(paddr))
162 break;
163
164 guest_buf = (hva_t)kmap_atomic(
165 pfn_to_page(paddr >> PAGE_SHIFT),
166 KM_USER0);
167 offset = addr & ~PAGE_MASK;
168 guest_buf |= offset;
169 now = min(size, PAGE_SIZE - offset);
170 memcpy(host_buf, (void*)guest_buf, now);
171 host_buf += now;
172 addr += now;
173 size -= now;
174 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
175 }
176 return req_size - size;
177}
178EXPORT_SYMBOL_GPL(kvm_read_guest);
179
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200180int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
181 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182{
183 unsigned char *host_buf = data;
184 unsigned long req_size = size;
185
186 while (size) {
187 hpa_t paddr;
188 unsigned now;
189 unsigned offset;
190 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200191 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192
193 paddr = gva_to_hpa(vcpu, addr);
194
195 if (is_error_hpa(paddr))
196 break;
197
Uri Lublinab51a432007-02-21 18:25:21 +0200198 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
199 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200 guest_buf = (hva_t)kmap_atomic(
201 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
202 offset = addr & ~PAGE_MASK;
203 guest_buf |= offset;
204 now = min(size, PAGE_SIZE - offset);
205 memcpy((void*)guest_buf, host_buf, now);
206 host_buf += now;
207 addr += now;
208 size -= now;
209 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
210 }
211 return req_size - size;
212}
213EXPORT_SYMBOL_GPL(kvm_write_guest);
214
Avi Kivity7702fd12007-06-14 16:27:40 +0300215void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
216{
217 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
218 return;
219
220 vcpu->guest_fpu_loaded = 1;
221 fx_save(vcpu->host_fx_image);
222 fx_restore(vcpu->guest_fx_image);
223}
224EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
225
226void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
227{
228 if (!vcpu->guest_fpu_loaded)
229 return;
230
231 vcpu->guest_fpu_loaded = 0;
232 fx_save(vcpu->guest_fx_image);
233 fx_restore(vcpu->host_fx_image);
234}
235EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
236
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237/*
238 * Switches to specified vcpu, until a matching vcpu_put()
239 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200240static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241{
Avi Kivitybccf2152007-02-21 18:04:26 +0200242 mutex_lock(&vcpu->mutex);
243 kvm_arch_ops->vcpu_load(vcpu);
244}
245
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246static void vcpu_put(struct kvm_vcpu *vcpu)
247{
248 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249 mutex_unlock(&vcpu->mutex);
250}
251
Avi Kivityd9e368d2007-06-07 19:18:30 +0300252static void ack_flush(void *_completed)
253{
254 atomic_t *completed = _completed;
255
256 atomic_inc(completed);
257}
258
259void kvm_flush_remote_tlbs(struct kvm *kvm)
260{
261 int i, cpu, needed;
262 cpumask_t cpus;
263 struct kvm_vcpu *vcpu;
264 atomic_t completed;
265
266 atomic_set(&completed, 0);
267 cpus_clear(cpus);
268 needed = 0;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000269 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
270 vcpu = kvm->vcpus[i];
271 if (!vcpu)
272 continue;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300273 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
274 continue;
275 cpu = vcpu->cpu;
276 if (cpu != -1 && cpu != raw_smp_processor_id())
277 if (!cpu_isset(cpu, cpus)) {
278 cpu_set(cpu, cpus);
279 ++needed;
280 }
281 }
282
283 /*
284 * We really want smp_call_function_mask() here. But that's not
285 * available, so ipi all cpus in parallel and wait for them
286 * to complete.
287 */
288 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
289 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
290 while (atomic_read(&completed) != needed) {
291 cpu_relax();
292 barrier();
293 }
294}
295
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000296int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
297{
298 struct page *page;
299 int r;
300
301 mutex_init(&vcpu->mutex);
302 vcpu->cpu = -1;
303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 vcpu->kvm = kvm;
305 vcpu->vcpu_id = id;
306
307 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
308 if (!page) {
309 r = -ENOMEM;
310 goto fail;
311 }
312 vcpu->run = page_address(page);
313
314 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
315 if (!page) {
316 r = -ENOMEM;
317 goto fail_free_run;
318 }
319 vcpu->pio_data = page_address(page);
320
321 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
322 FX_IMAGE_ALIGN);
323 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
324
325 r = kvm_mmu_create(vcpu);
326 if (r < 0)
327 goto fail_free_pio_data;
328
329 return 0;
330
331fail_free_pio_data:
332 free_page((unsigned long)vcpu->pio_data);
333fail_free_run:
334 free_page((unsigned long)vcpu->run);
335fail:
336 return -ENOMEM;
337}
338EXPORT_SYMBOL_GPL(kvm_vcpu_init);
339
340void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
341{
342 kvm_mmu_destroy(vcpu);
343 free_page((unsigned long)vcpu->pio_data);
344 free_page((unsigned long)vcpu->run);
345}
346EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
347
Avi Kivityf17abe92007-02-21 19:28:04 +0200348static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349{
350 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351
352 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200353 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354
Eddie Dong74906342007-06-19 18:05:03 +0300355 kvm_io_bus_init(&kvm->pio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 spin_lock_init(&kvm->lock);
357 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400358 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000359 spin_lock(&kvm_lock);
360 list_add(&kvm->vm_list, &vm_list);
361 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200362 return kvm;
363}
364
365static int kvm_dev_open(struct inode *inode, struct file *filp)
366{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 return 0;
368}
369
370/*
371 * Free any memory in @free but not in @dont.
372 */
373static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
374 struct kvm_memory_slot *dont)
375{
376 int i;
377
378 if (!dont || free->phys_mem != dont->phys_mem)
379 if (free->phys_mem) {
380 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800381 if (free->phys_mem[i])
382 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 vfree(free->phys_mem);
384 }
385
386 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
387 vfree(free->dirty_bitmap);
388
Al Viro8b6d44c2007-02-09 16:38:40 +0000389 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000391 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392}
393
394static void kvm_free_physmem(struct kvm *kvm)
395{
396 int i;
397
398 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000399 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400}
401
Avi Kivity039576c2007-03-20 12:46:50 +0200402static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
403{
404 int i;
405
406 for (i = 0; i < 2; ++i)
407 if (vcpu->pio.guest_pages[i]) {
408 __free_page(vcpu->pio.guest_pages[i]);
409 vcpu->pio.guest_pages[i] = NULL;
410 }
411}
412
Avi Kivity7b53aa52007-06-05 12:17:03 +0300413static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
414{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300415 vcpu_load(vcpu);
416 kvm_mmu_unload(vcpu);
417 vcpu_put(vcpu);
418}
419
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420static void kvm_free_vcpus(struct kvm *kvm)
421{
422 unsigned int i;
423
Avi Kivity7b53aa52007-06-05 12:17:03 +0300424 /*
425 * Unpin any mmu pages first.
426 */
427 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000428 if (kvm->vcpus[i])
429 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
430 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
431 if (kvm->vcpus[i]) {
432 kvm_arch_ops->vcpu_free(kvm->vcpus[i]);
433 kvm->vcpus[i] = NULL;
434 }
435 }
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437}
438
439static int kvm_dev_release(struct inode *inode, struct file *filp)
440{
Avi Kivityf17abe92007-02-21 19:28:04 +0200441 return 0;
442}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443
Avi Kivityf17abe92007-02-21 19:28:04 +0200444static void kvm_destroy_vm(struct kvm *kvm)
445{
Avi Kivity133de902007-02-12 00:54:44 -0800446 spin_lock(&kvm_lock);
447 list_del(&kvm->vm_list);
448 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300449 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400450 kvm_io_bus_destroy(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 kvm_free_vcpus(kvm);
452 kvm_free_physmem(kvm);
453 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200454}
455
456static int kvm_vm_release(struct inode *inode, struct file *filp)
457{
458 struct kvm *kvm = filp->private_data;
459
460 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 return 0;
462}
463
464static void inject_gp(struct kvm_vcpu *vcpu)
465{
466 kvm_arch_ops->inject_gp(vcpu, 0);
467}
468
Avi Kivity1342d352007-01-05 16:36:39 -0800469/*
470 * Load the pae pdptrs. Return true is they are all valid.
471 */
472static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473{
474 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800475 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800478 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300479 struct page *page;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000480 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481
482 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300483 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
Rusty Russellc820c2a2007-07-25 13:29:51 +1000484 if (!page) {
485 ret = 0;
486 goto out;
487 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488
Rusty Russellc820c2a2007-07-25 13:29:51 +1000489 pdpt = kmap_atomic(page, KM_USER0);
490 memcpy(pdpte, pdpt+offset, sizeof(pdpte));
491 kunmap_atomic(pdpt, KM_USER0);
492
493 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
494 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800495 ret = 0;
496 goto out;
497 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000499 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500
Rusty Russellc820c2a2007-07-25 13:29:51 +1000501 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800502out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 spin_unlock(&vcpu->kvm->lock);
504
Avi Kivity1342d352007-01-05 16:36:39 -0800505 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506}
507
508void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
509{
Rusty Russell707d92f2007-07-17 23:19:08 +1000510 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
512 cr0, vcpu->cr0);
513 inject_gp(vcpu);
514 return;
515 }
516
Rusty Russell707d92f2007-07-17 23:19:08 +1000517 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
519 inject_gp(vcpu);
520 return;
521 }
522
Rusty Russell707d92f2007-07-17 23:19:08 +1000523 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
525 "and a clear PE flag\n");
526 inject_gp(vcpu);
527 return;
528 }
529
Rusty Russell707d92f2007-07-17 23:19:08 +1000530 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800531#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800532 if ((vcpu->shadow_efer & EFER_LME)) {
533 int cs_db, cs_l;
534
535 if (!is_pae(vcpu)) {
536 printk(KERN_DEBUG "set_cr0: #GP, start paging "
537 "in long mode while PAE is disabled\n");
538 inject_gp(vcpu);
539 return;
540 }
541 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
542 if (cs_l) {
543 printk(KERN_DEBUG "set_cr0: #GP, start paging "
544 "in long mode while CS.L == 1\n");
545 inject_gp(vcpu);
546 return;
547
548 }
549 } else
550#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800551 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
553 "reserved bits\n");
554 inject_gp(vcpu);
555 return;
556 }
557
558 }
559
560 kvm_arch_ops->set_cr0(vcpu, cr0);
561 vcpu->cr0 = cr0;
562
563 spin_lock(&vcpu->kvm->lock);
564 kvm_mmu_reset_context(vcpu);
565 spin_unlock(&vcpu->kvm->lock);
566 return;
567}
568EXPORT_SYMBOL_GPL(set_cr0);
569
570void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
571{
572 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
573}
574EXPORT_SYMBOL_GPL(lmsw);
575
576void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
577{
Rusty Russell66aee912007-07-17 23:34:16 +1000578 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
580 inject_gp(vcpu);
581 return;
582 }
583
Avi Kivitya9058ec2006-12-29 16:49:37 -0800584 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000585 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
587 "in long mode\n");
588 inject_gp(vcpu);
589 return;
590 }
Rusty Russell66aee912007-07-17 23:34:16 +1000591 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800592 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
594 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000595 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 }
597
Rusty Russell66aee912007-07-17 23:34:16 +1000598 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
600 inject_gp(vcpu);
601 return;
602 }
603 kvm_arch_ops->set_cr4(vcpu, cr4);
604 spin_lock(&vcpu->kvm->lock);
605 kvm_mmu_reset_context(vcpu);
606 spin_unlock(&vcpu->kvm->lock);
607}
608EXPORT_SYMBOL_GPL(set_cr4);
609
610void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
611{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800612 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000613 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
615 inject_gp(vcpu);
616 return;
617 }
618 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000619 if (is_pae(vcpu)) {
620 if (cr3 & CR3_PAE_RESERVED_BITS) {
621 printk(KERN_DEBUG
622 "set_cr3: #GP, reserved bits\n");
623 inject_gp(vcpu);
624 return;
625 }
626 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
627 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
628 "reserved bits\n");
629 inject_gp(vcpu);
630 return;
631 }
632 } else {
633 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
634 printk(KERN_DEBUG
635 "set_cr3: #GP, reserved bits\n");
636 inject_gp(vcpu);
637 return;
638 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 }
640 }
641
642 vcpu->cr3 = cr3;
643 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800644 /*
645 * Does the new cr3 value map to physical memory? (Note, we
646 * catch an invalid cr3 even in real-mode, because it would
647 * cause trouble later on when we turn on paging anyway.)
648 *
649 * A real CPU would silently accept an invalid cr3 and would
650 * attempt to use it - with largely undefined (and often hard
651 * to debug) behavior on the guest side.
652 */
653 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
654 inject_gp(vcpu);
655 else
656 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657 spin_unlock(&vcpu->kvm->lock);
658}
659EXPORT_SYMBOL_GPL(set_cr3);
660
661void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
662{
Rusty Russell7075bc82007-07-17 23:37:17 +1000663 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
665 inject_gp(vcpu);
666 return;
667 }
668 vcpu->cr8 = cr8;
669}
670EXPORT_SYMBOL_GPL(set_cr8);
671
672void fx_init(struct kvm_vcpu *vcpu)
673{
674 struct __attribute__ ((__packed__)) fx_image_s {
675 u16 control; //fcw
676 u16 status; //fsw
677 u16 tag; // ftw
678 u16 opcode; //fop
679 u64 ip; // fpu ip
680 u64 operand;// fpu dp
681 u32 mxcsr;
682 u32 mxcsr_mask;
683
684 } *fx_image;
685
686 fx_save(vcpu->host_fx_image);
687 fpu_init();
688 fx_save(vcpu->guest_fx_image);
689 fx_restore(vcpu->host_fx_image);
690
691 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
692 fx_image->mxcsr = 0x1f80;
693 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
694 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
695}
696EXPORT_SYMBOL_GPL(fx_init);
697
698/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699 * Allocate some memory and give it an address in the guest physical address
700 * space.
701 *
702 * Discontiguous memory is allowed, mostly for framebuffers.
703 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200704static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
705 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706{
707 int r;
708 gfn_t base_gfn;
709 unsigned long npages;
710 unsigned long i;
711 struct kvm_memory_slot *memslot;
712 struct kvm_memory_slot old, new;
713 int memory_config_version;
714
715 r = -EINVAL;
716 /* General sanity checks */
717 if (mem->memory_size & (PAGE_SIZE - 1))
718 goto out;
719 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
720 goto out;
721 if (mem->slot >= KVM_MEMORY_SLOTS)
722 goto out;
723 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
724 goto out;
725
726 memslot = &kvm->memslots[mem->slot];
727 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
728 npages = mem->memory_size >> PAGE_SHIFT;
729
730 if (!npages)
731 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
732
733raced:
734 spin_lock(&kvm->lock);
735
736 memory_config_version = kvm->memory_config_version;
737 new = old = *memslot;
738
739 new.base_gfn = base_gfn;
740 new.npages = npages;
741 new.flags = mem->flags;
742
743 /* Disallow changing a memory slot's size. */
744 r = -EINVAL;
745 if (npages && old.npages && npages != old.npages)
746 goto out_unlock;
747
748 /* Check for overlaps */
749 r = -EEXIST;
750 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
751 struct kvm_memory_slot *s = &kvm->memslots[i];
752
753 if (s == memslot)
754 continue;
755 if (!((base_gfn + npages <= s->base_gfn) ||
756 (base_gfn >= s->base_gfn + s->npages)))
757 goto out_unlock;
758 }
759 /*
760 * Do memory allocations outside lock. memory_config_version will
761 * detect any races.
762 */
763 spin_unlock(&kvm->lock);
764
765 /* Deallocate if slot is being removed */
766 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000767 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
769 /* Free page dirty bitmap if unneeded */
770 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000771 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772
773 r = -ENOMEM;
774
775 /* Allocate if a slot is being created */
776 if (npages && !new.phys_mem) {
777 new.phys_mem = vmalloc(npages * sizeof(struct page *));
778
779 if (!new.phys_mem)
780 goto out_free;
781
782 memset(new.phys_mem, 0, npages * sizeof(struct page *));
783 for (i = 0; i < npages; ++i) {
784 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
785 | __GFP_ZERO);
786 if (!new.phys_mem[i])
787 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200788 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 }
790 }
791
792 /* Allocate page dirty bitmap if needed */
793 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
794 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
795
796 new.dirty_bitmap = vmalloc(dirty_bytes);
797 if (!new.dirty_bitmap)
798 goto out_free;
799 memset(new.dirty_bitmap, 0, dirty_bytes);
800 }
801
802 spin_lock(&kvm->lock);
803
804 if (memory_config_version != kvm->memory_config_version) {
805 spin_unlock(&kvm->lock);
806 kvm_free_physmem_slot(&new, &old);
807 goto raced;
808 }
809
810 r = -EAGAIN;
811 if (kvm->busy)
812 goto out_unlock;
813
814 if (mem->slot >= kvm->nmemslots)
815 kvm->nmemslots = mem->slot + 1;
816
817 *memslot = new;
818 ++kvm->memory_config_version;
819
Avi Kivity90cb0522007-07-17 13:04:56 +0300820 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
821 kvm_flush_remote_tlbs(kvm);
822
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 spin_unlock(&kvm->lock);
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 kvm_free_physmem_slot(&old, &new);
826 return 0;
827
828out_unlock:
829 spin_unlock(&kvm->lock);
830out_free:
831 kvm_free_physmem_slot(&new, &old);
832out:
833 return r;
834}
835
836/*
837 * Get (and clear) the dirty memory log for a memory slot.
838 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200839static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
840 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841{
842 struct kvm_memory_slot *memslot;
843 int r, i;
844 int n;
845 unsigned long any = 0;
846
847 spin_lock(&kvm->lock);
848
849 /*
850 * Prevent changes to guest memory configuration even while the lock
851 * is not taken.
852 */
853 ++kvm->busy;
854 spin_unlock(&kvm->lock);
855 r = -EINVAL;
856 if (log->slot >= KVM_MEMORY_SLOTS)
857 goto out;
858
859 memslot = &kvm->memslots[log->slot];
860 r = -ENOENT;
861 if (!memslot->dirty_bitmap)
862 goto out;
863
Uri Lublincd1a4a92007-02-22 16:43:09 +0200864 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865
Uri Lublincd1a4a92007-02-22 16:43:09 +0200866 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 any = memslot->dirty_bitmap[i];
868
869 r = -EFAULT;
870 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
871 goto out;
872
Avi Kivity90cb0522007-07-17 13:04:56 +0300873 spin_lock(&kvm->lock);
874 kvm_mmu_slot_remove_write_access(kvm, log->slot);
875 kvm_flush_remote_tlbs(kvm);
876 memset(memslot->dirty_bitmap, 0, n);
877 spin_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
879 r = 0;
880
881out:
882 spin_lock(&kvm->lock);
883 --kvm->busy;
884 spin_unlock(&kvm->lock);
885 return r;
886}
887
Avi Kivitye8207542007-03-30 16:54:30 +0300888/*
889 * Set a new alias region. Aliases map a portion of physical memory into
890 * another portion. This is useful for memory windows, for example the PC
891 * VGA region.
892 */
893static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
894 struct kvm_memory_alias *alias)
895{
896 int r, n;
897 struct kvm_mem_alias *p;
898
899 r = -EINVAL;
900 /* General sanity checks */
901 if (alias->memory_size & (PAGE_SIZE - 1))
902 goto out;
903 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
904 goto out;
905 if (alias->slot >= KVM_ALIAS_SLOTS)
906 goto out;
907 if (alias->guest_phys_addr + alias->memory_size
908 < alias->guest_phys_addr)
909 goto out;
910 if (alias->target_phys_addr + alias->memory_size
911 < alias->target_phys_addr)
912 goto out;
913
914 spin_lock(&kvm->lock);
915
916 p = &kvm->aliases[alias->slot];
917 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
918 p->npages = alias->memory_size >> PAGE_SHIFT;
919 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
920
921 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
922 if (kvm->aliases[n - 1].npages)
923 break;
924 kvm->naliases = n;
925
Avi Kivity90cb0522007-07-17 13:04:56 +0300926 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300927
Avi Kivitye8207542007-03-30 16:54:30 +0300928 spin_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300929
930 return 0;
931
932out:
933 return r;
934}
935
936static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
937{
938 int i;
939 struct kvm_mem_alias *alias;
940
941 for (i = 0; i < kvm->naliases; ++i) {
942 alias = &kvm->aliases[i];
943 if (gfn >= alias->base_gfn
944 && gfn < alias->base_gfn + alias->npages)
945 return alias->target_gfn + gfn - alias->base_gfn;
946 }
947 return gfn;
948}
949
950static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951{
952 int i;
953
954 for (i = 0; i < kvm->nmemslots; ++i) {
955 struct kvm_memory_slot *memslot = &kvm->memslots[i];
956
957 if (gfn >= memslot->base_gfn
958 && gfn < memslot->base_gfn + memslot->npages)
959 return memslot;
960 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000961 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962}
Avi Kivitye8207542007-03-30 16:54:30 +0300963
964struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
965{
966 gfn = unalias_gfn(kvm, gfn);
967 return __gfn_to_memslot(kvm, gfn);
968}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Avi Kivity954bbbc2007-03-30 14:02:32 +0300970struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
971{
972 struct kvm_memory_slot *slot;
973
Avi Kivitye8207542007-03-30 16:54:30 +0300974 gfn = unalias_gfn(kvm, gfn);
975 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300976 if (!slot)
977 return NULL;
978 return slot->phys_mem[gfn - slot->base_gfn];
979}
980EXPORT_SYMBOL_GPL(gfn_to_page);
981
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
983{
984 int i;
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300985 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned long rel_gfn;
987
988 for (i = 0; i < kvm->nmemslots; ++i) {
989 memslot = &kvm->memslots[i];
990
991 if (gfn >= memslot->base_gfn
992 && gfn < memslot->base_gfn + memslot->npages) {
993
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300994 if (!memslot->dirty_bitmap)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 return;
996
997 rel_gfn = gfn - memslot->base_gfn;
998
999 /* avoid RMW */
1000 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1001 set_bit(rel_gfn, memslot->dirty_bitmap);
1002 return;
1003 }
1004 }
1005}
1006
1007static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001008 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 unsigned int bytes,
1010 struct x86_emulate_ctxt *ctxt)
1011{
1012 struct kvm_vcpu *vcpu = ctxt->vcpu;
1013 void *data = val;
1014
1015 while (bytes) {
1016 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1017 unsigned offset = addr & (PAGE_SIZE-1);
1018 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1019 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001020 struct page *page;
1021 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
1023 if (gpa == UNMAPPED_GVA)
1024 return X86EMUL_PROPAGATE_FAULT;
1025 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001026 page = gfn_to_page(vcpu->kvm, pfn);
1027 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001029 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Avi Kivity954bbbc2007-03-30 14:02:32 +03001031 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032
Avi Kivity954bbbc2007-03-30 14:02:32 +03001033 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034
1035 bytes -= tocopy;
1036 data += tocopy;
1037 addr += tocopy;
1038 }
1039
1040 return X86EMUL_CONTINUE;
1041}
1042
1043static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001044 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045 unsigned int bytes,
1046 struct x86_emulate_ctxt *ctxt)
1047{
1048 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1049 addr, bytes);
1050 return X86EMUL_UNHANDLEABLE;
1051}
1052
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001053static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1054 gpa_t addr)
1055{
1056 /*
1057 * Note that its important to have this wrapper function because
1058 * in the very near future we will be checking for MMIOs against
1059 * the LAPIC as well as the general MMIO bus
1060 */
1061 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1062}
1063
Eddie Dong74906342007-06-19 18:05:03 +03001064static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1065 gpa_t addr)
1066{
1067 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1068}
1069
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001071 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 unsigned int bytes,
1073 struct x86_emulate_ctxt *ctxt)
1074{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001075 struct kvm_vcpu *vcpu = ctxt->vcpu;
1076 struct kvm_io_device *mmio_dev;
1077 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078
1079 if (vcpu->mmio_read_completed) {
1080 memcpy(val, vcpu->mmio_data, bytes);
1081 vcpu->mmio_read_completed = 0;
1082 return X86EMUL_CONTINUE;
1083 } else if (emulator_read_std(addr, val, bytes, ctxt)
1084 == X86EMUL_CONTINUE)
1085 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001086
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001087 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1088 if (gpa == UNMAPPED_GVA)
1089 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001091 /*
1092 * Is this MMIO handled locally?
1093 */
1094 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1095 if (mmio_dev) {
1096 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1097 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001099
1100 vcpu->mmio_needed = 1;
1101 vcpu->mmio_phys_addr = gpa;
1102 vcpu->mmio_size = bytes;
1103 vcpu->mmio_is_write = 0;
1104
1105 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106}
1107
Avi Kivityda4a00f2007-01-05 16:36:44 -08001108static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001109 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001110{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001111 struct page *page;
1112 void *virt;
1113
1114 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1115 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001116 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1117 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001118 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001119 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001120 virt = kmap_atomic(page, KM_USER0);
Shaohua Life551882007-07-23 14:51:39 +08001121 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001122 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001123 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001124 return 1;
1125}
1126
Avi Kivityb0fcd902007-07-22 18:48:54 +03001127static int emulator_write_emulated_onepage(unsigned long addr,
1128 const void *val,
1129 unsigned int bytes,
1130 struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001132 struct kvm_vcpu *vcpu = ctxt->vcpu;
1133 struct kvm_io_device *mmio_dev;
1134 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135
Avi Kivityc9047f52007-04-17 10:53:22 +03001136 if (gpa == UNMAPPED_GVA) {
1137 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001139 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140
Avi Kivityda4a00f2007-01-05 16:36:44 -08001141 if (emulator_write_phys(vcpu, gpa, val, bytes))
1142 return X86EMUL_CONTINUE;
1143
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001144 /*
1145 * Is this MMIO handled locally?
1146 */
1147 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1148 if (mmio_dev) {
1149 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1150 return X86EMUL_CONTINUE;
1151 }
1152
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 vcpu->mmio_needed = 1;
1154 vcpu->mmio_phys_addr = gpa;
1155 vcpu->mmio_size = bytes;
1156 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001157 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158
1159 return X86EMUL_CONTINUE;
1160}
1161
Avi Kivityb0fcd902007-07-22 18:48:54 +03001162static int emulator_write_emulated(unsigned long addr,
1163 const void *val,
1164 unsigned int bytes,
1165 struct x86_emulate_ctxt *ctxt)
1166{
1167 /* Crossing a page boundary? */
1168 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1169 int rc, now;
1170
1171 now = -addr & ~PAGE_MASK;
1172 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1173 if (rc != X86EMUL_CONTINUE)
1174 return rc;
1175 addr += now;
1176 val += now;
1177 bytes -= now;
1178 }
1179 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1180}
1181
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001183 const void *old,
1184 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 unsigned int bytes,
1186 struct x86_emulate_ctxt *ctxt)
1187{
1188 static int reported;
1189
1190 if (!reported) {
1191 reported = 1;
1192 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1193 }
1194 return emulator_write_emulated(addr, new, bytes, ctxt);
1195}
1196
1197static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1198{
1199 return kvm_arch_ops->get_segment_base(vcpu, seg);
1200}
1201
1202int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1203{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204 return X86EMUL_CONTINUE;
1205}
1206
1207int emulate_clts(struct kvm_vcpu *vcpu)
1208{
Avi Kivity399badf2007-01-05 16:36:38 -08001209 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210
Rusty Russell707d92f2007-07-17 23:19:08 +10001211 cr0 = vcpu->cr0 & ~X86_CR0_TS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 kvm_arch_ops->set_cr0(vcpu, cr0);
1213 return X86EMUL_CONTINUE;
1214}
1215
1216int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1217{
1218 struct kvm_vcpu *vcpu = ctxt->vcpu;
1219
1220 switch (dr) {
1221 case 0 ... 3:
1222 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1223 return X86EMUL_CONTINUE;
1224 default:
1225 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1226 __FUNCTION__, dr);
1227 return X86EMUL_UNHANDLEABLE;
1228 }
1229}
1230
1231int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1232{
1233 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1234 int exception;
1235
1236 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1237 if (exception) {
1238 /* FIXME: better handling */
1239 return X86EMUL_UNHANDLEABLE;
1240 }
1241 return X86EMUL_CONTINUE;
1242}
1243
1244static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1245{
1246 static int reported;
1247 u8 opcodes[4];
1248 unsigned long rip = ctxt->vcpu->rip;
1249 unsigned long rip_linear;
1250
1251 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1252
1253 if (reported)
1254 return;
1255
1256 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1257
1258 printk(KERN_ERR "emulation failed but !mmio_needed?"
1259 " rip %lx %02x %02x %02x %02x\n",
1260 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1261 reported = 1;
1262}
1263
1264struct x86_emulate_ops emulate_ops = {
1265 .read_std = emulator_read_std,
1266 .write_std = emulator_write_std,
1267 .read_emulated = emulator_read_emulated,
1268 .write_emulated = emulator_write_emulated,
1269 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1270};
1271
1272int emulate_instruction(struct kvm_vcpu *vcpu,
1273 struct kvm_run *run,
1274 unsigned long cr2,
1275 u16 error_code)
1276{
1277 struct x86_emulate_ctxt emulate_ctxt;
1278 int r;
1279 int cs_db, cs_l;
1280
Avi Kivitye7df56e2007-03-14 15:54:54 +02001281 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 kvm_arch_ops->cache_regs(vcpu);
1283
1284 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1285
1286 emulate_ctxt.vcpu = vcpu;
1287 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1288 emulate_ctxt.cr2 = cr2;
1289 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1290 ? X86EMUL_MODE_REAL : cs_l
1291 ? X86EMUL_MODE_PROT64 : cs_db
1292 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1293
1294 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1295 emulate_ctxt.cs_base = 0;
1296 emulate_ctxt.ds_base = 0;
1297 emulate_ctxt.es_base = 0;
1298 emulate_ctxt.ss_base = 0;
1299 } else {
1300 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1301 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1302 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1303 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1304 }
1305
1306 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1307 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1308
1309 vcpu->mmio_is_write = 0;
1310 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1311
1312 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001313 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1315 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1316 run->mmio.len = vcpu->mmio_size;
1317 run->mmio.is_write = vcpu->mmio_is_write;
1318 }
1319
1320 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001321 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1322 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 if (!vcpu->mmio_needed) {
1324 report_emulation_failure(&emulate_ctxt);
1325 return EMULATE_FAIL;
1326 }
1327 return EMULATE_DO_MMIO;
1328 }
1329
1330 kvm_arch_ops->decache_regs(vcpu);
1331 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1332
Avi Kivity02c83202007-04-29 15:02:17 +03001333 if (vcpu->mmio_is_write) {
1334 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001336 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337
1338 return EMULATE_DONE;
1339}
1340EXPORT_SYMBOL_GPL(emulate_instruction);
1341
Avi Kivityd3bef152007-06-05 15:53:05 +03001342int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1343{
1344 if (vcpu->irq_summary)
1345 return 1;
1346
1347 vcpu->run->exit_reason = KVM_EXIT_HLT;
1348 ++vcpu->stat.halt_exits;
1349 return 0;
1350}
1351EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1352
Avi Kivity270fd9b2007-02-19 14:37:47 +02001353int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1354{
1355 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1356
Dor Laor9b22bf52007-02-19 16:44:49 +02001357 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001358 ret = -KVM_EINVAL;
1359#ifdef CONFIG_X86_64
1360 if (is_long_mode(vcpu)) {
1361 nr = vcpu->regs[VCPU_REGS_RAX];
1362 a0 = vcpu->regs[VCPU_REGS_RDI];
1363 a1 = vcpu->regs[VCPU_REGS_RSI];
1364 a2 = vcpu->regs[VCPU_REGS_RDX];
1365 a3 = vcpu->regs[VCPU_REGS_RCX];
1366 a4 = vcpu->regs[VCPU_REGS_R8];
1367 a5 = vcpu->regs[VCPU_REGS_R9];
1368 } else
1369#endif
1370 {
1371 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1372 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1373 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1374 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1375 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1376 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1377 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1378 }
1379 switch (nr) {
1380 default:
Jeff Dike519ef352007-07-16 15:24:47 -04001381 run->hypercall.nr = nr;
Avi Kivityb4e63f52007-03-04 13:59:30 +02001382 run->hypercall.args[0] = a0;
1383 run->hypercall.args[1] = a1;
1384 run->hypercall.args[2] = a2;
1385 run->hypercall.args[3] = a3;
1386 run->hypercall.args[4] = a4;
1387 run->hypercall.args[5] = a5;
1388 run->hypercall.ret = ret;
1389 run->hypercall.longmode = is_long_mode(vcpu);
1390 kvm_arch_ops->decache_regs(vcpu);
1391 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001392 }
1393 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001394 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001395 return 1;
1396}
1397EXPORT_SYMBOL_GPL(kvm_hypercall);
1398
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1400{
1401 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1402}
1403
1404void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1405{
1406 struct descriptor_table dt = { limit, base };
1407
1408 kvm_arch_ops->set_gdt(vcpu, &dt);
1409}
1410
1411void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1412{
1413 struct descriptor_table dt = { limit, base };
1414
1415 kvm_arch_ops->set_idt(vcpu, &dt);
1416}
1417
1418void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1419 unsigned long *rflags)
1420{
1421 lmsw(vcpu, msw);
1422 *rflags = kvm_arch_ops->get_rflags(vcpu);
1423}
1424
1425unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1426{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001427 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 switch (cr) {
1429 case 0:
1430 return vcpu->cr0;
1431 case 2:
1432 return vcpu->cr2;
1433 case 3:
1434 return vcpu->cr3;
1435 case 4:
1436 return vcpu->cr4;
1437 default:
1438 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1439 return 0;
1440 }
1441}
1442
1443void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1444 unsigned long *rflags)
1445{
1446 switch (cr) {
1447 case 0:
1448 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1449 *rflags = kvm_arch_ops->get_rflags(vcpu);
1450 break;
1451 case 2:
1452 vcpu->cr2 = val;
1453 break;
1454 case 3:
1455 set_cr3(vcpu, val);
1456 break;
1457 case 4:
1458 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1459 break;
1460 default:
1461 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1462 }
1463}
1464
Ingo Molnar102d8322007-02-19 14:37:47 +02001465/*
1466 * Register the para guest with the host:
1467 */
1468static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1469{
1470 struct kvm_vcpu_para_state *para_state;
1471 hpa_t para_state_hpa, hypercall_hpa;
1472 struct page *para_state_page;
1473 unsigned char *hypercall;
1474 gpa_t hypercall_gpa;
1475
1476 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1477 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1478
1479 /*
1480 * Needs to be page aligned:
1481 */
1482 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1483 goto err_gp;
1484
1485 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1486 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1487 if (is_error_hpa(para_state_hpa))
1488 goto err_gp;
1489
Uri Lublinab51a432007-02-21 18:25:21 +02001490 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001491 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
Shaohua Life551882007-07-23 14:51:39 +08001492 para_state = kmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001493
1494 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1495 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1496
1497 para_state->host_version = KVM_PARA_API_VERSION;
1498 /*
1499 * We cannot support guests that try to register themselves
1500 * with a newer API version than the host supports:
1501 */
1502 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1503 para_state->ret = -KVM_EINVAL;
1504 goto err_kunmap_skip;
1505 }
1506
1507 hypercall_gpa = para_state->hypercall_gpa;
1508 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1509 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1510 if (is_error_hpa(hypercall_hpa)) {
1511 para_state->ret = -KVM_EINVAL;
1512 goto err_kunmap_skip;
1513 }
1514
1515 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1516 vcpu->para_state_page = para_state_page;
1517 vcpu->para_state_gpa = para_state_gpa;
1518 vcpu->hypercall_gpa = hypercall_gpa;
1519
Uri Lublinab51a432007-02-21 18:25:21 +02001520 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001521 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1522 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1523 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1524 kunmap_atomic(hypercall, KM_USER1);
1525
1526 para_state->ret = 0;
1527err_kunmap_skip:
Shaohua Life551882007-07-23 14:51:39 +08001528 kunmap(para_state_page);
Ingo Molnar102d8322007-02-19 14:37:47 +02001529 return 0;
1530err_gp:
1531 return 1;
1532}
1533
Avi Kivity3bab1f52006-12-29 16:49:48 -08001534int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1535{
1536 u64 data;
1537
1538 switch (msr) {
1539 case 0xc0010010: /* SYSCFG */
1540 case 0xc0010015: /* HWCR */
1541 case MSR_IA32_PLATFORM_ID:
1542 case MSR_IA32_P5_MC_ADDR:
1543 case MSR_IA32_P5_MC_TYPE:
1544 case MSR_IA32_MC0_CTL:
1545 case MSR_IA32_MCG_STATUS:
1546 case MSR_IA32_MCG_CAP:
1547 case MSR_IA32_MC0_MISC:
1548 case MSR_IA32_MC0_MISC+4:
1549 case MSR_IA32_MC0_MISC+8:
1550 case MSR_IA32_MC0_MISC+12:
1551 case MSR_IA32_MC0_MISC+16:
1552 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001553 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001554 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001555 /* MTRR registers */
1556 case 0xfe:
1557 case 0x200 ... 0x2ff:
1558 data = 0;
1559 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001560 case 0xcd: /* fsb frequency */
1561 data = 3;
1562 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001563 case MSR_IA32_APICBASE:
1564 data = vcpu->apic_base;
1565 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001566 case MSR_IA32_MISC_ENABLE:
1567 data = vcpu->ia32_misc_enable_msr;
1568 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001569#ifdef CONFIG_X86_64
1570 case MSR_EFER:
1571 data = vcpu->shadow_efer;
1572 break;
1573#endif
1574 default:
1575 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1576 return 1;
1577 }
1578 *pdata = data;
1579 return 0;
1580}
1581EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1582
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583/*
1584 * Reads an msr value (of 'msr_index') into 'pdata'.
1585 * Returns 0 on success, non-0 otherwise.
1586 * Assumes vcpu_load() was already called.
1587 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001588int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589{
1590 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1591}
1592
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001593#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
Avi Kivity3bab1f52006-12-29 16:49:48 -08001595static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 if (efer & EFER_RESERVED_BITS) {
1598 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1599 efer);
1600 inject_gp(vcpu);
1601 return;
1602 }
1603
1604 if (is_paging(vcpu)
1605 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1606 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1607 inject_gp(vcpu);
1608 return;
1609 }
1610
Avi Kivity7725f0b2006-12-13 00:34:01 -08001611 kvm_arch_ops->set_efer(vcpu, efer);
1612
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613 efer &= ~EFER_LMA;
1614 efer |= vcpu->shadow_efer & EFER_LMA;
1615
1616 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
1619#endif
1620
Avi Kivity3bab1f52006-12-29 16:49:48 -08001621int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1622{
1623 switch (msr) {
1624#ifdef CONFIG_X86_64
1625 case MSR_EFER:
1626 set_efer(vcpu, data);
1627 break;
1628#endif
1629 case MSR_IA32_MC0_STATUS:
1630 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1631 __FUNCTION__, data);
1632 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001633 case MSR_IA32_MCG_STATUS:
1634 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1635 __FUNCTION__, data);
1636 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001637 case MSR_IA32_UCODE_REV:
1638 case MSR_IA32_UCODE_WRITE:
1639 case 0x200 ... 0x2ff: /* MTRRs */
1640 break;
1641 case MSR_IA32_APICBASE:
1642 vcpu->apic_base = data;
1643 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001644 case MSR_IA32_MISC_ENABLE:
1645 vcpu->ia32_misc_enable_msr = data;
1646 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001647 /*
1648 * This is the 'probe whether the host is KVM' logic:
1649 */
1650 case MSR_KVM_API_MAGIC:
1651 return vcpu_register_para(vcpu, data);
1652
Avi Kivity3bab1f52006-12-29 16:49:48 -08001653 default:
1654 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1655 return 1;
1656 }
1657 return 0;
1658}
1659EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1660
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661/*
1662 * Writes msr value into into the appropriate "register".
1663 * Returns 0 on success, non-0 otherwise.
1664 * Assumes vcpu_load() was already called.
1665 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001666int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667{
1668 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1669}
1670
1671void kvm_resched(struct kvm_vcpu *vcpu)
1672{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001673 if (!need_resched())
1674 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 vcpu_put(vcpu);
1676 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001677 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678}
1679EXPORT_SYMBOL_GPL(kvm_resched);
1680
Avi Kivity06465c52007-02-28 20:46:53 +02001681void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1682{
1683 int i;
1684 u32 function;
1685 struct kvm_cpuid_entry *e, *best;
1686
1687 kvm_arch_ops->cache_regs(vcpu);
1688 function = vcpu->regs[VCPU_REGS_RAX];
1689 vcpu->regs[VCPU_REGS_RAX] = 0;
1690 vcpu->regs[VCPU_REGS_RBX] = 0;
1691 vcpu->regs[VCPU_REGS_RCX] = 0;
1692 vcpu->regs[VCPU_REGS_RDX] = 0;
1693 best = NULL;
1694 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1695 e = &vcpu->cpuid_entries[i];
1696 if (e->function == function) {
1697 best = e;
1698 break;
1699 }
1700 /*
1701 * Both basic or both extended?
1702 */
1703 if (((e->function ^ function) & 0x80000000) == 0)
1704 if (!best || e->function > best->function)
1705 best = e;
1706 }
1707 if (best) {
1708 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1709 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1710 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1711 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1712 }
1713 kvm_arch_ops->decache_regs(vcpu);
1714 kvm_arch_ops->skip_emulated_instruction(vcpu);
1715}
1716EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1717
Avi Kivity039576c2007-03-20 12:46:50 +02001718static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001719{
Avi Kivity039576c2007-03-20 12:46:50 +02001720 void *p = vcpu->pio_data;
1721 void *q;
1722 unsigned bytes;
1723 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1724
1725 kvm_arch_ops->vcpu_put(vcpu);
1726 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1727 PAGE_KERNEL);
1728 if (!q) {
1729 kvm_arch_ops->vcpu_load(vcpu);
1730 free_pio_guest_pages(vcpu);
1731 return -ENOMEM;
1732 }
1733 q += vcpu->pio.guest_page_offset;
1734 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1735 if (vcpu->pio.in)
1736 memcpy(q, p, bytes);
1737 else
1738 memcpy(p, q, bytes);
1739 q -= vcpu->pio.guest_page_offset;
1740 vunmap(q);
1741 kvm_arch_ops->vcpu_load(vcpu);
1742 free_pio_guest_pages(vcpu);
1743 return 0;
1744}
1745
1746static int complete_pio(struct kvm_vcpu *vcpu)
1747{
1748 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001749 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001750 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001751
1752 kvm_arch_ops->cache_regs(vcpu);
1753
1754 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001755 if (io->in)
1756 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001757 io->size);
1758 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001759 if (io->in) {
1760 r = pio_copy_data(vcpu);
1761 if (r) {
1762 kvm_arch_ops->cache_regs(vcpu);
1763 return r;
1764 }
1765 }
1766
Avi Kivity46fc1472007-02-22 19:39:30 +02001767 delta = 1;
1768 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001769 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001770 /*
1771 * The size of the register should really depend on
1772 * current address size.
1773 */
1774 vcpu->regs[VCPU_REGS_RCX] -= delta;
1775 }
Avi Kivity039576c2007-03-20 12:46:50 +02001776 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001777 delta = -delta;
1778 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001779 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001780 vcpu->regs[VCPU_REGS_RDI] += delta;
1781 else
1782 vcpu->regs[VCPU_REGS_RSI] += delta;
1783 }
1784
Avi Kivity46fc1472007-02-22 19:39:30 +02001785 kvm_arch_ops->decache_regs(vcpu);
1786
Avi Kivity039576c2007-03-20 12:46:50 +02001787 io->count -= io->cur_count;
1788 io->cur_count = 0;
1789
1790 if (!io->count)
1791 kvm_arch_ops->skip_emulated_instruction(vcpu);
1792 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001793}
1794
Eddie Dong65619eb2007-07-17 11:52:33 +03001795static void kernel_pio(struct kvm_io_device *pio_dev,
1796 struct kvm_vcpu *vcpu,
1797 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001798{
1799 /* TODO: String I/O for in kernel device */
1800
1801 if (vcpu->pio.in)
1802 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1803 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001804 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001805 else
1806 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1807 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001808 pd);
1809}
1810
1811static void pio_string_write(struct kvm_io_device *pio_dev,
1812 struct kvm_vcpu *vcpu)
1813{
1814 struct kvm_pio_request *io = &vcpu->pio;
1815 void *pd = vcpu->pio_data;
1816 int i;
1817
1818 for (i = 0; i < io->cur_count; i++) {
1819 kvm_iodevice_write(pio_dev, io->port,
1820 io->size,
1821 pd);
1822 pd += io->size;
1823 }
Eddie Dong74906342007-06-19 18:05:03 +03001824}
1825
Avi Kivity039576c2007-03-20 12:46:50 +02001826int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1827 int size, unsigned long count, int string, int down,
1828 gva_t address, int rep, unsigned port)
1829{
1830 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001831 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001832 int nr_pages = 1;
1833 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001834 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001835
1836 vcpu->run->exit_reason = KVM_EXIT_IO;
1837 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1838 vcpu->run->io.size = size;
1839 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1840 vcpu->run->io.count = count;
1841 vcpu->run->io.port = port;
1842 vcpu->pio.count = count;
1843 vcpu->pio.cur_count = count;
1844 vcpu->pio.size = size;
1845 vcpu->pio.in = in;
Eddie Dong74906342007-06-19 18:05:03 +03001846 vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001847 vcpu->pio.string = string;
1848 vcpu->pio.down = down;
1849 vcpu->pio.guest_page_offset = offset_in_page(address);
1850 vcpu->pio.rep = rep;
1851
Eddie Dong74906342007-06-19 18:05:03 +03001852 pio_dev = vcpu_find_pio_dev(vcpu, port);
Avi Kivity039576c2007-03-20 12:46:50 +02001853 if (!string) {
1854 kvm_arch_ops->cache_regs(vcpu);
1855 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1856 kvm_arch_ops->decache_regs(vcpu);
Eddie Dong74906342007-06-19 18:05:03 +03001857 if (pio_dev) {
Eddie Dong65619eb2007-07-17 11:52:33 +03001858 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
Eddie Dong74906342007-06-19 18:05:03 +03001859 complete_pio(vcpu);
1860 return 1;
1861 }
Avi Kivity039576c2007-03-20 12:46:50 +02001862 return 0;
1863 }
1864
1865 if (!count) {
1866 kvm_arch_ops->skip_emulated_instruction(vcpu);
1867 return 1;
1868 }
1869
1870 now = min(count, PAGE_SIZE / size);
1871
1872 if (!down)
1873 in_page = PAGE_SIZE - offset_in_page(address);
1874 else
1875 in_page = offset_in_page(address) + size;
1876 now = min(count, (unsigned long)in_page / size);
1877 if (!now) {
1878 /*
1879 * String I/O straddles page boundary. Pin two guest pages
1880 * so that we satisfy atomicity constraints. Do just one
1881 * transaction to avoid complexity.
1882 */
1883 nr_pages = 2;
1884 now = 1;
1885 }
1886 if (down) {
1887 /*
1888 * String I/O in reverse. Yuck. Kill the guest, fix later.
1889 */
1890 printk(KERN_ERR "kvm: guest string pio down\n");
1891 inject_gp(vcpu);
1892 return 1;
1893 }
1894 vcpu->run->io.count = now;
1895 vcpu->pio.cur_count = now;
1896
1897 for (i = 0; i < nr_pages; ++i) {
1898 spin_lock(&vcpu->kvm->lock);
1899 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1900 if (page)
1901 get_page(page);
1902 vcpu->pio.guest_pages[i] = page;
1903 spin_unlock(&vcpu->kvm->lock);
1904 if (!page) {
1905 inject_gp(vcpu);
1906 free_pio_guest_pages(vcpu);
1907 return 1;
1908 }
1909 }
1910
Eddie Dong65619eb2007-07-17 11:52:33 +03001911 if (!vcpu->pio.in) {
1912 /* string PIO write */
1913 ret = pio_copy_data(vcpu);
1914 if (ret >= 0 && pio_dev) {
1915 pio_string_write(pio_dev, vcpu);
1916 complete_pio(vcpu);
1917 if (vcpu->pio.count == 0)
1918 ret = 1;
1919 }
1920 } else if (pio_dev)
1921 printk(KERN_ERR "no string pio read support yet, "
1922 "port %x size %d count %ld\n",
1923 port, size, count);
1924
1925 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001926}
1927EXPORT_SYMBOL_GPL(kvm_setup_pio);
1928
Avi Kivitybccf2152007-02-21 18:04:26 +02001929static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001932 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933
Avi Kivitybccf2152007-02-21 18:04:26 +02001934 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935
Avi Kivity1961d272007-03-05 19:46:05 +02001936 if (vcpu->sigset_active)
1937 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1938
Dor Laor54810342007-02-12 00:54:39 -08001939 /* re-sync apic's tpr */
1940 vcpu->cr8 = kvm_run->cr8;
1941
Avi Kivity02c83202007-04-29 15:02:17 +03001942 if (vcpu->pio.cur_count) {
1943 r = complete_pio(vcpu);
1944 if (r)
1945 goto out;
1946 }
1947
1948 if (vcpu->mmio_needed) {
1949 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1950 vcpu->mmio_read_completed = 1;
1951 vcpu->mmio_needed = 0;
1952 r = emulate_instruction(vcpu, kvm_run,
1953 vcpu->mmio_fault_cr2, 0);
1954 if (r == EMULATE_DO_MMIO) {
1955 /*
1956 * Read-modify-write. Back to userspace.
1957 */
Avi Kivity02c83202007-04-29 15:02:17 +03001958 r = 0;
1959 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001960 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 }
1962
Avi Kivity8eb7d332007-03-04 14:17:08 +02001963 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001964 kvm_arch_ops->cache_regs(vcpu);
1965 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1966 kvm_arch_ops->decache_regs(vcpu);
1967 }
1968
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 r = kvm_arch_ops->run(vcpu, kvm_run);
1970
Avi Kivity039576c2007-03-20 12:46:50 +02001971out:
Avi Kivity1961d272007-03-05 19:46:05 +02001972 if (vcpu->sigset_active)
1973 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1974
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975 vcpu_put(vcpu);
1976 return r;
1977}
1978
Avi Kivitybccf2152007-02-21 18:04:26 +02001979static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1980 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981{
Avi Kivitybccf2152007-02-21 18:04:26 +02001982 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983
1984 kvm_arch_ops->cache_regs(vcpu);
1985
1986 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1987 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1988 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1989 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1990 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1991 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1992 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1993 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001994#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1996 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1997 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1998 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1999 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2000 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2001 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2002 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2003#endif
2004
2005 regs->rip = vcpu->rip;
2006 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
2007
2008 /*
2009 * Don't leak debug flags in case they were set for guest debugging
2010 */
2011 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2012 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2013
2014 vcpu_put(vcpu);
2015
2016 return 0;
2017}
2018
Avi Kivitybccf2152007-02-21 18:04:26 +02002019static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2020 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021{
Avi Kivitybccf2152007-02-21 18:04:26 +02002022 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023
2024 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2025 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2026 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2027 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2028 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2029 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2030 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2031 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002032#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2034 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2035 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2036 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2037 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2038 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2039 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2040 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2041#endif
2042
2043 vcpu->rip = regs->rip;
2044 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2045
2046 kvm_arch_ops->decache_regs(vcpu);
2047
2048 vcpu_put(vcpu);
2049
2050 return 0;
2051}
2052
2053static void get_segment(struct kvm_vcpu *vcpu,
2054 struct kvm_segment *var, int seg)
2055{
2056 return kvm_arch_ops->get_segment(vcpu, var, seg);
2057}
2058
Avi Kivitybccf2152007-02-21 18:04:26 +02002059static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2060 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 struct descriptor_table dt;
2063
Avi Kivitybccf2152007-02-21 18:04:26 +02002064 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065
2066 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2067 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2068 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2069 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2070 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2071 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2072
2073 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2074 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2075
2076 kvm_arch_ops->get_idt(vcpu, &dt);
2077 sregs->idt.limit = dt.limit;
2078 sregs->idt.base = dt.base;
2079 kvm_arch_ops->get_gdt(vcpu, &dt);
2080 sregs->gdt.limit = dt.limit;
2081 sregs->gdt.base = dt.base;
2082
Anthony Liguori25c4c272007-04-27 09:29:21 +03002083 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084 sregs->cr0 = vcpu->cr0;
2085 sregs->cr2 = vcpu->cr2;
2086 sregs->cr3 = vcpu->cr3;
2087 sregs->cr4 = vcpu->cr4;
2088 sregs->cr8 = vcpu->cr8;
2089 sregs->efer = vcpu->shadow_efer;
2090 sregs->apic_base = vcpu->apic_base;
2091
2092 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2093 sizeof sregs->interrupt_bitmap);
2094
2095 vcpu_put(vcpu);
2096
2097 return 0;
2098}
2099
2100static void set_segment(struct kvm_vcpu *vcpu,
2101 struct kvm_segment *var, int seg)
2102{
2103 return kvm_arch_ops->set_segment(vcpu, var, seg);
2104}
2105
Avi Kivitybccf2152007-02-21 18:04:26 +02002106static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2107 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 int mmu_reset_needed = 0;
2110 int i;
2111 struct descriptor_table dt;
2112
Avi Kivitybccf2152007-02-21 18:04:26 +02002113 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 dt.limit = sregs->idt.limit;
2116 dt.base = sregs->idt.base;
2117 kvm_arch_ops->set_idt(vcpu, &dt);
2118 dt.limit = sregs->gdt.limit;
2119 dt.base = sregs->gdt.base;
2120 kvm_arch_ops->set_gdt(vcpu, &dt);
2121
2122 vcpu->cr2 = sregs->cr2;
2123 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2124 vcpu->cr3 = sregs->cr3;
2125
2126 vcpu->cr8 = sregs->cr8;
2127
2128 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002129#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2131#endif
2132 vcpu->apic_base = sregs->apic_base;
2133
Anthony Liguori25c4c272007-04-27 09:29:21 +03002134 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002135
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002137 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002138
2139 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2140 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002141 if (!is_long_mode(vcpu) && is_pae(vcpu))
2142 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143
2144 if (mmu_reset_needed)
2145 kvm_mmu_reset_context(vcpu);
2146
2147 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2148 sizeof vcpu->irq_pending);
2149 vcpu->irq_summary = 0;
Rusty Russell9eb829c2007-07-18 13:05:58 +10002150 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151 if (vcpu->irq_pending[i])
2152 __set_bit(i, &vcpu->irq_summary);
2153
Avi Kivity024aa1c2007-03-21 13:44:58 +02002154 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2155 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2156 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2157 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2158 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2159 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2160
2161 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2162 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2163
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 vcpu_put(vcpu);
2165
2166 return 0;
2167}
2168
2169/*
2170 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2171 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002172 *
2173 * This list is modified at module load time to reflect the
2174 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175 */
2176static u32 msrs_to_save[] = {
2177 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2178 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002179#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2181#endif
2182 MSR_IA32_TIME_STAMP_COUNTER,
2183};
2184
Michael Riepebf591b22006-12-22 01:05:36 -08002185static unsigned num_msrs_to_save;
2186
Avi Kivity6f00e682007-01-26 00:56:40 -08002187static u32 emulated_msrs[] = {
2188 MSR_IA32_MISC_ENABLE,
2189};
2190
Michael Riepebf591b22006-12-22 01:05:36 -08002191static __init void kvm_init_msr_list(void)
2192{
2193 u32 dummy[2];
2194 unsigned i, j;
2195
2196 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2197 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2198 continue;
2199 if (j < i)
2200 msrs_to_save[j] = msrs_to_save[i];
2201 j++;
2202 }
2203 num_msrs_to_save = j;
2204}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205
2206/*
2207 * Adapt set_msr() to msr_io()'s calling convention
2208 */
2209static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2210{
Avi Kivity35f3f282007-07-17 14:20:30 +03002211 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212}
2213
2214/*
2215 * Read or write a bunch of msrs. All parameters are kernel addresses.
2216 *
2217 * @return number of msrs set successfully.
2218 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002219static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220 struct kvm_msr_entry *entries,
2221 int (*do_msr)(struct kvm_vcpu *vcpu,
2222 unsigned index, u64 *data))
2223{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 int i;
2225
Avi Kivitybccf2152007-02-21 18:04:26 +02002226 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227
2228 for (i = 0; i < msrs->nmsrs; ++i)
2229 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2230 break;
2231
2232 vcpu_put(vcpu);
2233
2234 return i;
2235}
2236
2237/*
2238 * Read or write a bunch of msrs. Parameters are user addresses.
2239 *
2240 * @return number of msrs set successfully.
2241 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002242static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 int (*do_msr)(struct kvm_vcpu *vcpu,
2244 unsigned index, u64 *data),
2245 int writeback)
2246{
2247 struct kvm_msrs msrs;
2248 struct kvm_msr_entry *entries;
2249 int r, n;
2250 unsigned size;
2251
2252 r = -EFAULT;
2253 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2254 goto out;
2255
2256 r = -E2BIG;
2257 if (msrs.nmsrs >= MAX_IO_MSRS)
2258 goto out;
2259
2260 r = -ENOMEM;
2261 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2262 entries = vmalloc(size);
2263 if (!entries)
2264 goto out;
2265
2266 r = -EFAULT;
2267 if (copy_from_user(entries, user_msrs->entries, size))
2268 goto out_free;
2269
Avi Kivitybccf2152007-02-21 18:04:26 +02002270 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 if (r < 0)
2272 goto out_free;
2273
2274 r = -EFAULT;
2275 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2276 goto out_free;
2277
2278 r = n;
2279
2280out_free:
2281 vfree(entries);
2282out:
2283 return r;
2284}
2285
2286/*
2287 * Translate a guest virtual address to a guest physical address.
2288 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002289static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2290 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291{
2292 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293 gpa_t gpa;
2294
Avi Kivitybccf2152007-02-21 18:04:26 +02002295 vcpu_load(vcpu);
2296 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2298 tr->physical_address = gpa;
2299 tr->valid = gpa != UNMAPPED_GVA;
2300 tr->writeable = 1;
2301 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002302 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303 vcpu_put(vcpu);
2304
2305 return 0;
2306}
2307
Avi Kivitybccf2152007-02-21 18:04:26 +02002308static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2309 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 if (irq->irq < 0 || irq->irq >= 256)
2312 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002313 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314
2315 set_bit(irq->irq, vcpu->irq_pending);
2316 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2317
2318 vcpu_put(vcpu);
2319
2320 return 0;
2321}
2322
Avi Kivitybccf2152007-02-21 18:04:26 +02002323static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2324 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 int r;
2327
Avi Kivitybccf2152007-02-21 18:04:26 +02002328 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329
2330 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2331
2332 vcpu_put(vcpu);
2333
2334 return r;
2335}
2336
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002337static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2338 unsigned long address,
2339 int *type)
2340{
2341 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2342 unsigned long pgoff;
2343 struct page *page;
2344
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002345 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002346 if (pgoff == 0)
2347 page = virt_to_page(vcpu->run);
2348 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2349 page = virt_to_page(vcpu->pio_data);
2350 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002351 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002352 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002353 if (type != NULL)
2354 *type = VM_FAULT_MINOR;
2355
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002356 return page;
2357}
2358
2359static struct vm_operations_struct kvm_vcpu_vm_ops = {
2360 .nopage = kvm_vcpu_nopage,
2361};
2362
2363static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2364{
2365 vma->vm_ops = &kvm_vcpu_vm_ops;
2366 return 0;
2367}
2368
Avi Kivitybccf2152007-02-21 18:04:26 +02002369static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2370{
2371 struct kvm_vcpu *vcpu = filp->private_data;
2372
2373 fput(vcpu->kvm->filp);
2374 return 0;
2375}
2376
2377static struct file_operations kvm_vcpu_fops = {
2378 .release = kvm_vcpu_release,
2379 .unlocked_ioctl = kvm_vcpu_ioctl,
2380 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002381 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002382};
2383
2384/*
2385 * Allocates an inode for the vcpu.
2386 */
2387static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2388{
2389 int fd, r;
2390 struct inode *inode;
2391 struct file *file;
2392
Avi Kivityd6d28162007-06-28 08:38:16 -04002393 r = anon_inode_getfd(&fd, &inode, &file,
2394 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2395 if (r)
2396 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002397 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002398 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002399}
2400
Avi Kivityc5ea7662007-02-20 18:41:05 +02002401/*
2402 * Creates some virtual cpus. Good luck creating more than one.
2403 */
2404static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2405{
2406 int r;
2407 struct kvm_vcpu *vcpu;
2408
Avi Kivityc5ea7662007-02-20 18:41:05 +02002409 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002410 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002411
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002412 vcpu = kvm_arch_ops->vcpu_create(kvm, n);
2413 if (IS_ERR(vcpu))
2414 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002415
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002416 vcpu_load(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002417 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002418 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002419 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002420 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002421
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002422 spin_lock(&kvm->lock);
2423 if (kvm->vcpus[n]) {
2424 r = -EEXIST;
2425 spin_unlock(&kvm->lock);
2426 goto mmu_unload;
2427 }
2428 kvm->vcpus[n] = vcpu;
2429 spin_unlock(&kvm->lock);
2430
2431 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002432 r = create_vcpu_fd(vcpu);
2433 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002434 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002435 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002436
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002437unlink:
2438 spin_lock(&kvm->lock);
2439 kvm->vcpus[n] = NULL;
2440 spin_unlock(&kvm->lock);
2441
2442mmu_unload:
2443 vcpu_load(vcpu);
2444 kvm_mmu_unload(vcpu);
2445 vcpu_put(vcpu);
2446
2447free_vcpu:
2448 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002449 return r;
2450}
2451
Eddie Dong2cc51562007-05-21 07:28:09 +03002452static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2453{
2454 u64 efer;
2455 int i;
2456 struct kvm_cpuid_entry *e, *entry;
2457
2458 rdmsrl(MSR_EFER, efer);
2459 entry = NULL;
2460 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2461 e = &vcpu->cpuid_entries[i];
2462 if (e->function == 0x80000001) {
2463 entry = e;
2464 break;
2465 }
2466 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002467 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002468 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002469 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002470 }
2471}
2472
Avi Kivity06465c52007-02-28 20:46:53 +02002473static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2474 struct kvm_cpuid *cpuid,
2475 struct kvm_cpuid_entry __user *entries)
2476{
2477 int r;
2478
2479 r = -E2BIG;
2480 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2481 goto out;
2482 r = -EFAULT;
2483 if (copy_from_user(&vcpu->cpuid_entries, entries,
2484 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2485 goto out;
2486 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002487 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002488 return 0;
2489
2490out:
2491 return r;
2492}
2493
Avi Kivity1961d272007-03-05 19:46:05 +02002494static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2495{
2496 if (sigset) {
2497 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2498 vcpu->sigset_active = 1;
2499 vcpu->sigset = *sigset;
2500 } else
2501 vcpu->sigset_active = 0;
2502 return 0;
2503}
2504
Avi Kivityb8836732007-04-01 16:34:31 +03002505/*
2506 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2507 * we have asm/x86/processor.h
2508 */
2509struct fxsave {
2510 u16 cwd;
2511 u16 swd;
2512 u16 twd;
2513 u16 fop;
2514 u64 rip;
2515 u64 rdp;
2516 u32 mxcsr;
2517 u32 mxcsr_mask;
2518 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2519#ifdef CONFIG_X86_64
2520 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2521#else
2522 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2523#endif
2524};
2525
2526static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2527{
2528 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2529
2530 vcpu_load(vcpu);
2531
2532 memcpy(fpu->fpr, fxsave->st_space, 128);
2533 fpu->fcw = fxsave->cwd;
2534 fpu->fsw = fxsave->swd;
2535 fpu->ftwx = fxsave->twd;
2536 fpu->last_opcode = fxsave->fop;
2537 fpu->last_ip = fxsave->rip;
2538 fpu->last_dp = fxsave->rdp;
2539 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2540
2541 vcpu_put(vcpu);
2542
2543 return 0;
2544}
2545
2546static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2547{
2548 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2549
2550 vcpu_load(vcpu);
2551
2552 memcpy(fxsave->st_space, fpu->fpr, 128);
2553 fxsave->cwd = fpu->fcw;
2554 fxsave->swd = fpu->fsw;
2555 fxsave->twd = fpu->ftwx;
2556 fxsave->fop = fpu->last_opcode;
2557 fxsave->rip = fpu->last_ip;
2558 fxsave->rdp = fpu->last_dp;
2559 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2560
2561 vcpu_put(vcpu);
2562
2563 return 0;
2564}
2565
Avi Kivitybccf2152007-02-21 18:04:26 +02002566static long kvm_vcpu_ioctl(struct file *filp,
2567 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568{
Avi Kivitybccf2152007-02-21 18:04:26 +02002569 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002570 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 int r = -EINVAL;
2572
2573 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002574 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002575 r = -EINVAL;
2576 if (arg)
2577 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002578 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 case KVM_GET_REGS: {
2581 struct kvm_regs kvm_regs;
2582
Avi Kivitybccf2152007-02-21 18:04:26 +02002583 memset(&kvm_regs, 0, sizeof kvm_regs);
2584 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 if (r)
2586 goto out;
2587 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002588 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 goto out;
2590 r = 0;
2591 break;
2592 }
2593 case KVM_SET_REGS: {
2594 struct kvm_regs kvm_regs;
2595
2596 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002597 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002599 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 if (r)
2601 goto out;
2602 r = 0;
2603 break;
2604 }
2605 case KVM_GET_SREGS: {
2606 struct kvm_sregs kvm_sregs;
2607
Avi Kivitybccf2152007-02-21 18:04:26 +02002608 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2609 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 if (r)
2611 goto out;
2612 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002613 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614 goto out;
2615 r = 0;
2616 break;
2617 }
2618 case KVM_SET_SREGS: {
2619 struct kvm_sregs kvm_sregs;
2620
2621 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002622 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002624 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 if (r)
2626 goto out;
2627 r = 0;
2628 break;
2629 }
2630 case KVM_TRANSLATE: {
2631 struct kvm_translation tr;
2632
2633 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002634 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002636 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 if (r)
2638 goto out;
2639 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002640 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 goto out;
2642 r = 0;
2643 break;
2644 }
2645 case KVM_INTERRUPT: {
2646 struct kvm_interrupt irq;
2647
2648 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002649 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002651 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 if (r)
2653 goto out;
2654 r = 0;
2655 break;
2656 }
2657 case KVM_DEBUG_GUEST: {
2658 struct kvm_debug_guest dbg;
2659
2660 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002661 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002663 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 if (r)
2665 goto out;
2666 r = 0;
2667 break;
2668 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002669 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002670 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002671 break;
2672 case KVM_SET_MSRS:
2673 r = msr_io(vcpu, argp, do_set_msr, 0);
2674 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002675 case KVM_SET_CPUID: {
2676 struct kvm_cpuid __user *cpuid_arg = argp;
2677 struct kvm_cpuid cpuid;
2678
2679 r = -EFAULT;
2680 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2681 goto out;
2682 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2683 if (r)
2684 goto out;
2685 break;
2686 }
Avi Kivity1961d272007-03-05 19:46:05 +02002687 case KVM_SET_SIGNAL_MASK: {
2688 struct kvm_signal_mask __user *sigmask_arg = argp;
2689 struct kvm_signal_mask kvm_sigmask;
2690 sigset_t sigset, *p;
2691
2692 p = NULL;
2693 if (argp) {
2694 r = -EFAULT;
2695 if (copy_from_user(&kvm_sigmask, argp,
2696 sizeof kvm_sigmask))
2697 goto out;
2698 r = -EINVAL;
2699 if (kvm_sigmask.len != sizeof sigset)
2700 goto out;
2701 r = -EFAULT;
2702 if (copy_from_user(&sigset, sigmask_arg->sigset,
2703 sizeof sigset))
2704 goto out;
2705 p = &sigset;
2706 }
2707 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2708 break;
2709 }
Avi Kivityb8836732007-04-01 16:34:31 +03002710 case KVM_GET_FPU: {
2711 struct kvm_fpu fpu;
2712
2713 memset(&fpu, 0, sizeof fpu);
2714 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2715 if (r)
2716 goto out;
2717 r = -EFAULT;
2718 if (copy_to_user(argp, &fpu, sizeof fpu))
2719 goto out;
2720 r = 0;
2721 break;
2722 }
2723 case KVM_SET_FPU: {
2724 struct kvm_fpu fpu;
2725
2726 r = -EFAULT;
2727 if (copy_from_user(&fpu, argp, sizeof fpu))
2728 goto out;
2729 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2730 if (r)
2731 goto out;
2732 r = 0;
2733 break;
2734 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002735 default:
2736 ;
2737 }
2738out:
2739 return r;
2740}
2741
2742static long kvm_vm_ioctl(struct file *filp,
2743 unsigned int ioctl, unsigned long arg)
2744{
2745 struct kvm *kvm = filp->private_data;
2746 void __user *argp = (void __user *)arg;
2747 int r = -EINVAL;
2748
2749 switch (ioctl) {
2750 case KVM_CREATE_VCPU:
2751 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2752 if (r < 0)
2753 goto out;
2754 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 case KVM_SET_MEMORY_REGION: {
2756 struct kvm_memory_region kvm_mem;
2757
2758 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002759 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002761 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 if (r)
2763 goto out;
2764 break;
2765 }
2766 case KVM_GET_DIRTY_LOG: {
2767 struct kvm_dirty_log log;
2768
2769 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002770 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002772 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 if (r)
2774 goto out;
2775 break;
2776 }
Avi Kivitye8207542007-03-30 16:54:30 +03002777 case KVM_SET_MEMORY_ALIAS: {
2778 struct kvm_memory_alias alias;
2779
2780 r = -EFAULT;
2781 if (copy_from_user(&alias, argp, sizeof alias))
2782 goto out;
2783 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2784 if (r)
2785 goto out;
2786 break;
2787 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002788 default:
2789 ;
2790 }
2791out:
2792 return r;
2793}
2794
2795static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2796 unsigned long address,
2797 int *type)
2798{
2799 struct kvm *kvm = vma->vm_file->private_data;
2800 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002801 struct page *page;
2802
Avi Kivityf17abe92007-02-21 19:28:04 +02002803 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002804 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002805 if (!page)
2806 return NOPAGE_SIGBUS;
2807 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002808 if (type != NULL)
2809 *type = VM_FAULT_MINOR;
2810
Avi Kivityf17abe92007-02-21 19:28:04 +02002811 return page;
2812}
2813
2814static struct vm_operations_struct kvm_vm_vm_ops = {
2815 .nopage = kvm_vm_nopage,
2816};
2817
2818static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2819{
2820 vma->vm_ops = &kvm_vm_vm_ops;
2821 return 0;
2822}
2823
2824static struct file_operations kvm_vm_fops = {
2825 .release = kvm_vm_release,
2826 .unlocked_ioctl = kvm_vm_ioctl,
2827 .compat_ioctl = kvm_vm_ioctl,
2828 .mmap = kvm_vm_mmap,
2829};
2830
2831static int kvm_dev_ioctl_create_vm(void)
2832{
2833 int fd, r;
2834 struct inode *inode;
2835 struct file *file;
2836 struct kvm *kvm;
2837
Avi Kivityf17abe92007-02-21 19:28:04 +02002838 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002839 if (IS_ERR(kvm))
2840 return PTR_ERR(kvm);
2841 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2842 if (r) {
2843 kvm_destroy_vm(kvm);
2844 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002845 }
2846
Avi Kivitybccf2152007-02-21 18:04:26 +02002847 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002848
Avi Kivityf17abe92007-02-21 19:28:04 +02002849 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002850}
2851
2852static long kvm_dev_ioctl(struct file *filp,
2853 unsigned int ioctl, unsigned long arg)
2854{
2855 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002856 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002857
2858 switch (ioctl) {
2859 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002860 r = -EINVAL;
2861 if (arg)
2862 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002863 r = KVM_API_VERSION;
2864 break;
2865 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002866 r = -EINVAL;
2867 if (arg)
2868 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002869 r = kvm_dev_ioctl_create_vm();
2870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f366982007-02-09 16:38:35 +00002872 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 struct kvm_msr_list msr_list;
2874 unsigned n;
2875
2876 r = -EFAULT;
2877 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2878 goto out;
2879 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002880 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2882 goto out;
2883 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002884 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 goto out;
2886 r = -EFAULT;
2887 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002888 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002890 if (copy_to_user(user_msr_list->indices
2891 + num_msrs_to_save * sizeof(u32),
2892 &emulated_msrs,
2893 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2894 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002896 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002898 case KVM_CHECK_EXTENSION:
2899 /*
2900 * No extensions defined at present.
2901 */
2902 r = 0;
2903 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002904 case KVM_GET_VCPU_MMAP_SIZE:
2905 r = -EINVAL;
2906 if (arg)
2907 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002908 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002909 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 default:
2911 ;
2912 }
2913out:
2914 return r;
2915}
2916
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917static struct file_operations kvm_chardev_ops = {
2918 .open = kvm_dev_open,
2919 .release = kvm_dev_release,
2920 .unlocked_ioctl = kvm_dev_ioctl,
2921 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922};
2923
2924static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002925 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 "kvm",
2927 &kvm_chardev_ops,
2928};
2929
Avi Kivity774c47f2007-02-12 00:54:47 -08002930/*
2931 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2932 * cached on it.
2933 */
2934static void decache_vcpus_on_cpu(int cpu)
2935{
2936 struct kvm *vm;
2937 struct kvm_vcpu *vcpu;
2938 int i;
2939
2940 spin_lock(&kvm_lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002941 list_for_each_entry(vm, &vm_list, vm_list) {
2942 spin_lock(&vm->lock);
Avi Kivity774c47f2007-02-12 00:54:47 -08002943 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002944 vcpu = vm->vcpus[i];
2945 if (!vcpu)
2946 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08002947 /*
2948 * If the vcpu is locked, then it is running on some
2949 * other cpu and therefore it is not cached on the
2950 * cpu in question.
2951 *
2952 * If it's not locked, check the last cpu it executed
2953 * on.
2954 */
2955 if (mutex_trylock(&vcpu->mutex)) {
2956 if (vcpu->cpu == cpu) {
2957 kvm_arch_ops->vcpu_decache(vcpu);
2958 vcpu->cpu = -1;
2959 }
2960 mutex_unlock(&vcpu->mutex);
2961 }
2962 }
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002963 spin_unlock(&vm->lock);
2964 }
Avi Kivity774c47f2007-02-12 00:54:47 -08002965 spin_unlock(&kvm_lock);
2966}
2967
Avi Kivity1b6c0162007-05-24 13:03:52 +03002968static void hardware_enable(void *junk)
2969{
2970 int cpu = raw_smp_processor_id();
2971
2972 if (cpu_isset(cpu, cpus_hardware_enabled))
2973 return;
2974 cpu_set(cpu, cpus_hardware_enabled);
2975 kvm_arch_ops->hardware_enable(NULL);
2976}
2977
2978static void hardware_disable(void *junk)
2979{
2980 int cpu = raw_smp_processor_id();
2981
2982 if (!cpu_isset(cpu, cpus_hardware_enabled))
2983 return;
2984 cpu_clear(cpu, cpus_hardware_enabled);
2985 decache_vcpus_on_cpu(cpu);
2986 kvm_arch_ops->hardware_disable(NULL);
2987}
2988
Avi Kivity774c47f2007-02-12 00:54:47 -08002989static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2990 void *v)
2991{
2992 int cpu = (long)v;
2993
2994 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002995 case CPU_DYING:
2996 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002997 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2998 cpu);
2999 hardware_disable(NULL);
3000 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003001 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003002 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003003 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3004 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003005 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003006 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003007 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003008 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003009 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3010 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003011 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003012 break;
3013 }
3014 return NOTIFY_OK;
3015}
3016
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003017static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3018 void *v)
3019{
3020 if (val == SYS_RESTART) {
3021 /*
3022 * Some (well, at least mine) BIOSes hang on reboot if
3023 * in vmx root mode.
3024 */
3025 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3026 on_each_cpu(hardware_disable, NULL, 0, 1);
3027 }
3028 return NOTIFY_OK;
3029}
3030
3031static struct notifier_block kvm_reboot_notifier = {
3032 .notifier_call = kvm_reboot,
3033 .priority = 0,
3034};
3035
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003036void kvm_io_bus_init(struct kvm_io_bus *bus)
3037{
3038 memset(bus, 0, sizeof(*bus));
3039}
3040
3041void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3042{
3043 int i;
3044
3045 for (i = 0; i < bus->dev_count; i++) {
3046 struct kvm_io_device *pos = bus->devs[i];
3047
3048 kvm_iodevice_destructor(pos);
3049 }
3050}
3051
3052struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3053{
3054 int i;
3055
3056 for (i = 0; i < bus->dev_count; i++) {
3057 struct kvm_io_device *pos = bus->devs[i];
3058
3059 if (pos->in_range(pos, addr))
3060 return pos;
3061 }
3062
3063 return NULL;
3064}
3065
3066void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3067{
3068 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3069
3070 bus->devs[bus->dev_count++] = dev;
3071}
3072
Avi Kivity774c47f2007-02-12 00:54:47 -08003073static struct notifier_block kvm_cpu_notifier = {
3074 .notifier_call = kvm_cpu_hotplug,
3075 .priority = 20, /* must be > scheduler priority */
3076};
3077
Avi Kivity1165f5f2007-04-19 17:27:43 +03003078static u64 stat_get(void *_offset)
3079{
3080 unsigned offset = (long)_offset;
3081 u64 total = 0;
3082 struct kvm *kvm;
3083 struct kvm_vcpu *vcpu;
3084 int i;
3085
3086 spin_lock(&kvm_lock);
3087 list_for_each_entry(kvm, &vm_list, vm_list)
3088 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003089 vcpu = kvm->vcpus[i];
3090 if (vcpu)
3091 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003092 }
3093 spin_unlock(&kvm_lock);
3094 return total;
3095}
3096
3097static void stat_set(void *offset, u64 val)
3098{
3099}
3100
3101DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3102
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103static __init void kvm_init_debug(void)
3104{
3105 struct kvm_stats_debugfs_item *p;
3106
Al Viro8b6d44c2007-02-09 16:38:40 +00003107 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003109 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3110 (void *)(long)p->offset,
3111 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112}
3113
3114static void kvm_exit_debug(void)
3115{
3116 struct kvm_stats_debugfs_item *p;
3117
3118 for (p = debugfs_entries; p->name; ++p)
3119 debugfs_remove(p->dentry);
3120 debugfs_remove(debugfs_dir);
3121}
3122
Avi Kivity59ae6c62007-02-12 00:54:48 -08003123static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3124{
Avi Kivity4267c412007-05-24 13:09:41 +03003125 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003126 return 0;
3127}
3128
3129static int kvm_resume(struct sys_device *dev)
3130{
Avi Kivity4267c412007-05-24 13:09:41 +03003131 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003132 return 0;
3133}
3134
3135static struct sysdev_class kvm_sysdev_class = {
3136 set_kset_name("kvm"),
3137 .suspend = kvm_suspend,
3138 .resume = kvm_resume,
3139};
3140
3141static struct sys_device kvm_sysdev = {
3142 .id = 0,
3143 .cls = &kvm_sysdev_class,
3144};
3145
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146hpa_t bad_page_address;
3147
3148int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3149{
3150 int r;
3151
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003152 if (kvm_arch_ops) {
3153 printk(KERN_ERR "kvm: already loaded the other module\n");
3154 return -EEXIST;
3155 }
3156
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003157 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 printk(KERN_ERR "kvm: no hardware support\n");
3159 return -EOPNOTSUPP;
3160 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003161 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 printk(KERN_ERR "kvm: disabled by bios\n");
3163 return -EOPNOTSUPP;
3164 }
3165
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003166 kvm_arch_ops = ops;
3167
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 r = kvm_arch_ops->hardware_setup();
3169 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003170 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171
Avi Kivity1b6c0162007-05-24 13:03:52 +03003172 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003173 r = register_cpu_notifier(&kvm_cpu_notifier);
3174 if (r)
3175 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 register_reboot_notifier(&kvm_reboot_notifier);
3177
Avi Kivity59ae6c62007-02-12 00:54:48 -08003178 r = sysdev_class_register(&kvm_sysdev_class);
3179 if (r)
3180 goto out_free_2;
3181
3182 r = sysdev_register(&kvm_sysdev);
3183 if (r)
3184 goto out_free_3;
3185
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 kvm_chardev_ops.owner = module;
3187
3188 r = misc_register(&kvm_dev);
3189 if (r) {
3190 printk (KERN_ERR "kvm: misc device register failed\n");
3191 goto out_free;
3192 }
3193
3194 return r;
3195
3196out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003197 sysdev_unregister(&kvm_sysdev);
3198out_free_3:
3199 sysdev_class_unregister(&kvm_sysdev_class);
3200out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003202 unregister_cpu_notifier(&kvm_cpu_notifier);
3203out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003204 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003206out:
3207 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 return r;
3209}
3210
3211void kvm_exit_arch(void)
3212{
3213 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003214 sysdev_unregister(&kvm_sysdev);
3215 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003217 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003218 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003220 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221}
3222
3223static __init int kvm_init(void)
3224{
3225 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003226 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227
Avi Kivityb5a33a72007-04-15 16:31:09 +03003228 r = kvm_mmu_module_init();
3229 if (r)
3230 goto out4;
3231
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 kvm_init_debug();
3233
Michael Riepebf591b22006-12-22 01:05:36 -08003234 kvm_init_msr_list();
3235
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3237 r = -ENOMEM;
3238 goto out;
3239 }
3240
3241 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3242 memset(__va(bad_page_address), 0, PAGE_SIZE);
3243
Avi Kivity58e690e2007-02-26 16:29:43 +02003244 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245
3246out:
3247 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003248 kvm_mmu_module_exit();
3249out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 return r;
3251}
3252
3253static __exit void kvm_exit(void)
3254{
3255 kvm_exit_debug();
3256 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003257 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258}
3259
3260module_init(kvm_init)
3261module_exit(kvm_exit)
3262
3263EXPORT_SYMBOL_GPL(kvm_init_arch);
3264EXPORT_SYMBOL_GPL(kvm_exit_arch);