blob: af02320012c9e12f95e4339cc7e4cb146f47a2d6 [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 Russell707d92fa2007-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
Avi Kivity6aa8b732006-12-10 02:21:36 -080095#define CR8_RESEVED_BITS (~0x0fULL)
96#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;
269 for (i = 0; i < kvm->nvcpus; ++i) {
270 vcpu = &kvm->vcpus[i];
271 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
272 continue;
273 cpu = vcpu->cpu;
274 if (cpu != -1 && cpu != raw_smp_processor_id())
275 if (!cpu_isset(cpu, cpus)) {
276 cpu_set(cpu, cpus);
277 ++needed;
278 }
279 }
280
281 /*
282 * We really want smp_call_function_mask() here. But that's not
283 * available, so ipi all cpus in parallel and wait for them
284 * to complete.
285 */
286 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
287 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
288 while (atomic_read(&completed) != needed) {
289 cpu_relax();
290 barrier();
291 }
292}
293
Avi Kivityf17abe92007-02-21 19:28:04 +0200294static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295{
296 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
297 int i;
298
299 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200300 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301
Eddie Dong74906342007-06-19 18:05:03 +0300302 kvm_io_bus_init(&kvm->pio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 spin_lock_init(&kvm->lock);
304 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400305 kvm_io_bus_init(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
307 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
308
309 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800310 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800311 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312 vcpu->mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313 }
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000314 spin_lock(&kvm_lock);
315 list_add(&kvm->vm_list, &vm_list);
316 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200317 return kvm;
318}
319
320static int kvm_dev_open(struct inode *inode, struct file *filp)
321{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 return 0;
323}
324
325/*
326 * Free any memory in @free but not in @dont.
327 */
328static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
329 struct kvm_memory_slot *dont)
330{
331 int i;
332
333 if (!dont || free->phys_mem != dont->phys_mem)
334 if (free->phys_mem) {
335 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800336 if (free->phys_mem[i])
337 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 vfree(free->phys_mem);
339 }
340
341 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
342 vfree(free->dirty_bitmap);
343
Al Viro8b6d44c2007-02-09 16:38:40 +0000344 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000346 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347}
348
349static void kvm_free_physmem(struct kvm *kvm)
350{
351 int i;
352
353 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000354 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355}
356
Avi Kivity039576c2007-03-20 12:46:50 +0200357static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
358{
359 int i;
360
361 for (i = 0; i < 2; ++i)
362 if (vcpu->pio.guest_pages[i]) {
363 __free_page(vcpu->pio.guest_pages[i]);
364 vcpu->pio.guest_pages[i] = NULL;
365 }
366}
367
Avi Kivity7b53aa52007-06-05 12:17:03 +0300368static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
369{
370 if (!vcpu->vmcs)
371 return;
372
373 vcpu_load(vcpu);
374 kvm_mmu_unload(vcpu);
375 vcpu_put(vcpu);
376}
377
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
379{
Avi Kivitybccf2152007-02-21 18:04:26 +0200380 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800381 return;
382
Avi Kivitybccf2152007-02-21 18:04:26 +0200383 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800385 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800386 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200387 free_page((unsigned long)vcpu->run);
388 vcpu->run = NULL;
Avi Kivity039576c2007-03-20 12:46:50 +0200389 free_page((unsigned long)vcpu->pio_data);
390 vcpu->pio_data = NULL;
391 free_pio_guest_pages(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392}
393
394static void kvm_free_vcpus(struct kvm *kvm)
395{
396 unsigned int i;
397
Avi Kivity7b53aa52007-06-05 12:17:03 +0300398 /*
399 * Unpin any mmu pages first.
400 */
401 for (i = 0; i < KVM_MAX_VCPUS; ++i)
402 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 for (i = 0; i < KVM_MAX_VCPUS; ++i)
404 kvm_free_vcpu(&kvm->vcpus[i]);
405}
406
407static int kvm_dev_release(struct inode *inode, struct file *filp)
408{
Avi Kivityf17abe92007-02-21 19:28:04 +0200409 return 0;
410}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411
Avi Kivityf17abe92007-02-21 19:28:04 +0200412static void kvm_destroy_vm(struct kvm *kvm)
413{
Avi Kivity133de902007-02-12 00:54:44 -0800414 spin_lock(&kvm_lock);
415 list_del(&kvm->vm_list);
416 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300417 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400418 kvm_io_bus_destroy(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 kvm_free_vcpus(kvm);
420 kvm_free_physmem(kvm);
421 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200422}
423
424static int kvm_vm_release(struct inode *inode, struct file *filp)
425{
426 struct kvm *kvm = filp->private_data;
427
428 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 return 0;
430}
431
432static void inject_gp(struct kvm_vcpu *vcpu)
433{
434 kvm_arch_ops->inject_gp(vcpu, 0);
435}
436
Avi Kivity1342d352007-01-05 16:36:39 -0800437/*
438 * Load the pae pdptrs. Return true is they are all valid.
439 */
440static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441{
442 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800443 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 int i;
445 u64 pdpte;
446 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800447 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300448 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449
450 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300451 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
452 /* FIXME: !page - emulate? 0xff? */
453 pdpt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
Avi Kivity1342d352007-01-05 16:36:39 -0800455 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 for (i = 0; i < 4; ++i) {
457 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800458 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
459 ret = 0;
460 goto out;
461 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 }
463
Avi Kivity1342d352007-01-05 16:36:39 -0800464 for (i = 0; i < 4; ++i)
465 vcpu->pdptrs[i] = pdpt[offset + i];
466
467out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 kunmap_atomic(pdpt, KM_USER0);
469 spin_unlock(&vcpu->kvm->lock);
470
Avi Kivity1342d352007-01-05 16:36:39 -0800471 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472}
473
474void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
475{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000476 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
478 cr0, vcpu->cr0);
479 inject_gp(vcpu);
480 return;
481 }
482
Rusty Russell707d92fa2007-07-17 23:19:08 +1000483 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
485 inject_gp(vcpu);
486 return;
487 }
488
Rusty Russell707d92fa2007-07-17 23:19:08 +1000489 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
491 "and a clear PE flag\n");
492 inject_gp(vcpu);
493 return;
494 }
495
Rusty Russell707d92fa2007-07-17 23:19:08 +1000496 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 if ((vcpu->shadow_efer & EFER_LME)) {
499 int cs_db, cs_l;
500
501 if (!is_pae(vcpu)) {
502 printk(KERN_DEBUG "set_cr0: #GP, start paging "
503 "in long mode while PAE is disabled\n");
504 inject_gp(vcpu);
505 return;
506 }
507 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
508 if (cs_l) {
509 printk(KERN_DEBUG "set_cr0: #GP, start paging "
510 "in long mode while CS.L == 1\n");
511 inject_gp(vcpu);
512 return;
513
514 }
515 } else
516#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800517 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
519 "reserved bits\n");
520 inject_gp(vcpu);
521 return;
522 }
523
524 }
525
526 kvm_arch_ops->set_cr0(vcpu, cr0);
527 vcpu->cr0 = cr0;
528
529 spin_lock(&vcpu->kvm->lock);
530 kvm_mmu_reset_context(vcpu);
531 spin_unlock(&vcpu->kvm->lock);
532 return;
533}
534EXPORT_SYMBOL_GPL(set_cr0);
535
536void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
537{
538 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
539}
540EXPORT_SYMBOL_GPL(lmsw);
541
542void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
543{
Rusty Russell66aee912007-07-17 23:34:16 +1000544 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
546 inject_gp(vcpu);
547 return;
548 }
549
Avi Kivitya9058ec2006-12-29 16:49:37 -0800550 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000551 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
553 "in long mode\n");
554 inject_gp(vcpu);
555 return;
556 }
Rusty Russell66aee912007-07-17 23:34:16 +1000557 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800558 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
560 inject_gp(vcpu);
561 }
562
Rusty Russell66aee912007-07-17 23:34:16 +1000563 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
565 inject_gp(vcpu);
566 return;
567 }
568 kvm_arch_ops->set_cr4(vcpu, cr4);
569 spin_lock(&vcpu->kvm->lock);
570 kvm_mmu_reset_context(vcpu);
571 spin_unlock(&vcpu->kvm->lock);
572}
573EXPORT_SYMBOL_GPL(set_cr4);
574
575void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
576{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800577 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000578 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
580 inject_gp(vcpu);
581 return;
582 }
583 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000584 if (is_pae(vcpu)) {
585 if (cr3 & CR3_PAE_RESERVED_BITS) {
586 printk(KERN_DEBUG
587 "set_cr3: #GP, reserved bits\n");
588 inject_gp(vcpu);
589 return;
590 }
591 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
592 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
593 "reserved bits\n");
594 inject_gp(vcpu);
595 return;
596 }
597 } else {
598 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
599 printk(KERN_DEBUG
600 "set_cr3: #GP, reserved bits\n");
601 inject_gp(vcpu);
602 return;
603 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604 }
605 }
606
607 vcpu->cr3 = cr3;
608 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800609 /*
610 * Does the new cr3 value map to physical memory? (Note, we
611 * catch an invalid cr3 even in real-mode, because it would
612 * cause trouble later on when we turn on paging anyway.)
613 *
614 * A real CPU would silently accept an invalid cr3 and would
615 * attempt to use it - with largely undefined (and often hard
616 * to debug) behavior on the guest side.
617 */
618 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
619 inject_gp(vcpu);
620 else
621 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 spin_unlock(&vcpu->kvm->lock);
623}
624EXPORT_SYMBOL_GPL(set_cr3);
625
626void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
627{
628 if ( cr8 & CR8_RESEVED_BITS) {
629 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
630 inject_gp(vcpu);
631 return;
632 }
633 vcpu->cr8 = cr8;
634}
635EXPORT_SYMBOL_GPL(set_cr8);
636
637void fx_init(struct kvm_vcpu *vcpu)
638{
639 struct __attribute__ ((__packed__)) fx_image_s {
640 u16 control; //fcw
641 u16 status; //fsw
642 u16 tag; // ftw
643 u16 opcode; //fop
644 u64 ip; // fpu ip
645 u64 operand;// fpu dp
646 u32 mxcsr;
647 u32 mxcsr_mask;
648
649 } *fx_image;
650
651 fx_save(vcpu->host_fx_image);
652 fpu_init();
653 fx_save(vcpu->guest_fx_image);
654 fx_restore(vcpu->host_fx_image);
655
656 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
657 fx_image->mxcsr = 0x1f80;
658 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
659 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
660}
661EXPORT_SYMBOL_GPL(fx_init);
662
663/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 * Allocate some memory and give it an address in the guest physical address
665 * space.
666 *
667 * Discontiguous memory is allowed, mostly for framebuffers.
668 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200669static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
670 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671{
672 int r;
673 gfn_t base_gfn;
674 unsigned long npages;
675 unsigned long i;
676 struct kvm_memory_slot *memslot;
677 struct kvm_memory_slot old, new;
678 int memory_config_version;
679
680 r = -EINVAL;
681 /* General sanity checks */
682 if (mem->memory_size & (PAGE_SIZE - 1))
683 goto out;
684 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
685 goto out;
686 if (mem->slot >= KVM_MEMORY_SLOTS)
687 goto out;
688 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
689 goto out;
690
691 memslot = &kvm->memslots[mem->slot];
692 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
693 npages = mem->memory_size >> PAGE_SHIFT;
694
695 if (!npages)
696 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
697
698raced:
699 spin_lock(&kvm->lock);
700
701 memory_config_version = kvm->memory_config_version;
702 new = old = *memslot;
703
704 new.base_gfn = base_gfn;
705 new.npages = npages;
706 new.flags = mem->flags;
707
708 /* Disallow changing a memory slot's size. */
709 r = -EINVAL;
710 if (npages && old.npages && npages != old.npages)
711 goto out_unlock;
712
713 /* Check for overlaps */
714 r = -EEXIST;
715 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
716 struct kvm_memory_slot *s = &kvm->memslots[i];
717
718 if (s == memslot)
719 continue;
720 if (!((base_gfn + npages <= s->base_gfn) ||
721 (base_gfn >= s->base_gfn + s->npages)))
722 goto out_unlock;
723 }
724 /*
725 * Do memory allocations outside lock. memory_config_version will
726 * detect any races.
727 */
728 spin_unlock(&kvm->lock);
729
730 /* Deallocate if slot is being removed */
731 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000732 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
734 /* Free page dirty bitmap if unneeded */
735 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000736 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
738 r = -ENOMEM;
739
740 /* Allocate if a slot is being created */
741 if (npages && !new.phys_mem) {
742 new.phys_mem = vmalloc(npages * sizeof(struct page *));
743
744 if (!new.phys_mem)
745 goto out_free;
746
747 memset(new.phys_mem, 0, npages * sizeof(struct page *));
748 for (i = 0; i < npages; ++i) {
749 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
750 | __GFP_ZERO);
751 if (!new.phys_mem[i])
752 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200753 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800754 }
755 }
756
757 /* Allocate page dirty bitmap if needed */
758 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
759 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
760
761 new.dirty_bitmap = vmalloc(dirty_bytes);
762 if (!new.dirty_bitmap)
763 goto out_free;
764 memset(new.dirty_bitmap, 0, dirty_bytes);
765 }
766
767 spin_lock(&kvm->lock);
768
769 if (memory_config_version != kvm->memory_config_version) {
770 spin_unlock(&kvm->lock);
771 kvm_free_physmem_slot(&new, &old);
772 goto raced;
773 }
774
775 r = -EAGAIN;
776 if (kvm->busy)
777 goto out_unlock;
778
779 if (mem->slot >= kvm->nmemslots)
780 kvm->nmemslots = mem->slot + 1;
781
782 *memslot = new;
783 ++kvm->memory_config_version;
784
Avi Kivity90cb0522007-07-17 13:04:56 +0300785 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
786 kvm_flush_remote_tlbs(kvm);
787
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 spin_unlock(&kvm->lock);
789
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 kvm_free_physmem_slot(&old, &new);
791 return 0;
792
793out_unlock:
794 spin_unlock(&kvm->lock);
795out_free:
796 kvm_free_physmem_slot(&new, &old);
797out:
798 return r;
799}
800
801/*
802 * Get (and clear) the dirty memory log for a memory slot.
803 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200804static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
805 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806{
807 struct kvm_memory_slot *memslot;
808 int r, i;
809 int n;
810 unsigned long any = 0;
811
812 spin_lock(&kvm->lock);
813
814 /*
815 * Prevent changes to guest memory configuration even while the lock
816 * is not taken.
817 */
818 ++kvm->busy;
819 spin_unlock(&kvm->lock);
820 r = -EINVAL;
821 if (log->slot >= KVM_MEMORY_SLOTS)
822 goto out;
823
824 memslot = &kvm->memslots[log->slot];
825 r = -ENOENT;
826 if (!memslot->dirty_bitmap)
827 goto out;
828
Uri Lublincd1a4a92007-02-22 16:43:09 +0200829 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830
Uri Lublincd1a4a92007-02-22 16:43:09 +0200831 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 any = memslot->dirty_bitmap[i];
833
834 r = -EFAULT;
835 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
836 goto out;
837
Avi Kivity90cb0522007-07-17 13:04:56 +0300838 spin_lock(&kvm->lock);
839 kvm_mmu_slot_remove_write_access(kvm, log->slot);
840 kvm_flush_remote_tlbs(kvm);
841 memset(memslot->dirty_bitmap, 0, n);
842 spin_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
844 r = 0;
845
846out:
847 spin_lock(&kvm->lock);
848 --kvm->busy;
849 spin_unlock(&kvm->lock);
850 return r;
851}
852
Avi Kivitye8207542007-03-30 16:54:30 +0300853/*
854 * Set a new alias region. Aliases map a portion of physical memory into
855 * another portion. This is useful for memory windows, for example the PC
856 * VGA region.
857 */
858static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
859 struct kvm_memory_alias *alias)
860{
861 int r, n;
862 struct kvm_mem_alias *p;
863
864 r = -EINVAL;
865 /* General sanity checks */
866 if (alias->memory_size & (PAGE_SIZE - 1))
867 goto out;
868 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
869 goto out;
870 if (alias->slot >= KVM_ALIAS_SLOTS)
871 goto out;
872 if (alias->guest_phys_addr + alias->memory_size
873 < alias->guest_phys_addr)
874 goto out;
875 if (alias->target_phys_addr + alias->memory_size
876 < alias->target_phys_addr)
877 goto out;
878
879 spin_lock(&kvm->lock);
880
881 p = &kvm->aliases[alias->slot];
882 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
883 p->npages = alias->memory_size >> PAGE_SHIFT;
884 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
885
886 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
887 if (kvm->aliases[n - 1].npages)
888 break;
889 kvm->naliases = n;
890
Avi Kivity90cb0522007-07-17 13:04:56 +0300891 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300892
Avi Kivitye8207542007-03-30 16:54:30 +0300893 spin_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300894
895 return 0;
896
897out:
898 return r;
899}
900
901static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
902{
903 int i;
904 struct kvm_mem_alias *alias;
905
906 for (i = 0; i < kvm->naliases; ++i) {
907 alias = &kvm->aliases[i];
908 if (gfn >= alias->base_gfn
909 && gfn < alias->base_gfn + alias->npages)
910 return alias->target_gfn + gfn - alias->base_gfn;
911 }
912 return gfn;
913}
914
915static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916{
917 int i;
918
919 for (i = 0; i < kvm->nmemslots; ++i) {
920 struct kvm_memory_slot *memslot = &kvm->memslots[i];
921
922 if (gfn >= memslot->base_gfn
923 && gfn < memslot->base_gfn + memslot->npages)
924 return memslot;
925 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000926 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927}
Avi Kivitye8207542007-03-30 16:54:30 +0300928
929struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
930{
931 gfn = unalias_gfn(kvm, gfn);
932 return __gfn_to_memslot(kvm, gfn);
933}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Avi Kivity954bbbc2007-03-30 14:02:32 +0300935struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
936{
937 struct kvm_memory_slot *slot;
938
Avi Kivitye8207542007-03-30 16:54:30 +0300939 gfn = unalias_gfn(kvm, gfn);
940 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300941 if (!slot)
942 return NULL;
943 return slot->phys_mem[gfn - slot->base_gfn];
944}
945EXPORT_SYMBOL_GPL(gfn_to_page);
946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
948{
949 int i;
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300950 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 unsigned long rel_gfn;
952
953 for (i = 0; i < kvm->nmemslots; ++i) {
954 memslot = &kvm->memslots[i];
955
956 if (gfn >= memslot->base_gfn
957 && gfn < memslot->base_gfn + memslot->npages) {
958
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300959 if (!memslot->dirty_bitmap)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 return;
961
962 rel_gfn = gfn - memslot->base_gfn;
963
964 /* avoid RMW */
965 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
966 set_bit(rel_gfn, memslot->dirty_bitmap);
967 return;
968 }
969 }
970}
971
972static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300973 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 unsigned int bytes,
975 struct x86_emulate_ctxt *ctxt)
976{
977 struct kvm_vcpu *vcpu = ctxt->vcpu;
978 void *data = val;
979
980 while (bytes) {
981 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
982 unsigned offset = addr & (PAGE_SIZE-1);
983 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
984 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300985 struct page *page;
986 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
988 if (gpa == UNMAPPED_GVA)
989 return X86EMUL_PROPAGATE_FAULT;
990 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300991 page = gfn_to_page(vcpu->kvm, pfn);
992 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300994 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Avi Kivity954bbbc2007-03-30 14:02:32 +0300996 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997
Avi Kivity954bbbc2007-03-30 14:02:32 +0300998 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999
1000 bytes -= tocopy;
1001 data += tocopy;
1002 addr += tocopy;
1003 }
1004
1005 return X86EMUL_CONTINUE;
1006}
1007
1008static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001009 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 unsigned int bytes,
1011 struct x86_emulate_ctxt *ctxt)
1012{
1013 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1014 addr, bytes);
1015 return X86EMUL_UNHANDLEABLE;
1016}
1017
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001018static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1019 gpa_t addr)
1020{
1021 /*
1022 * Note that its important to have this wrapper function because
1023 * in the very near future we will be checking for MMIOs against
1024 * the LAPIC as well as the general MMIO bus
1025 */
1026 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1027}
1028
Eddie Dong74906342007-06-19 18:05:03 +03001029static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1030 gpa_t addr)
1031{
1032 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1033}
1034
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001036 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 unsigned int bytes,
1038 struct x86_emulate_ctxt *ctxt)
1039{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001040 struct kvm_vcpu *vcpu = ctxt->vcpu;
1041 struct kvm_io_device *mmio_dev;
1042 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043
1044 if (vcpu->mmio_read_completed) {
1045 memcpy(val, vcpu->mmio_data, bytes);
1046 vcpu->mmio_read_completed = 0;
1047 return X86EMUL_CONTINUE;
1048 } else if (emulator_read_std(addr, val, bytes, ctxt)
1049 == X86EMUL_CONTINUE)
1050 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001051
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001052 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1053 if (gpa == UNMAPPED_GVA)
1054 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001056 /*
1057 * Is this MMIO handled locally?
1058 */
1059 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1060 if (mmio_dev) {
1061 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1062 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001064
1065 vcpu->mmio_needed = 1;
1066 vcpu->mmio_phys_addr = gpa;
1067 vcpu->mmio_size = bytes;
1068 vcpu->mmio_is_write = 0;
1069
1070 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071}
1072
Avi Kivityda4a00f2007-01-05 16:36:44 -08001073static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001074 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001075{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001076 struct page *page;
1077 void *virt;
Avi Kivity09072da2007-05-01 14:16:52 +03001078 unsigned offset = offset_in_page(gpa);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001079
1080 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1081 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001082 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1083 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001084 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001085 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001086 virt = kmap_atomic(page, KM_USER0);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001087 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1088 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001089 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001090 return 1;
1091}
1092
Avi Kivityb0fcd902007-07-22 18:48:54 +03001093static int emulator_write_emulated_onepage(unsigned long addr,
1094 const void *val,
1095 unsigned int bytes,
1096 struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001098 struct kvm_vcpu *vcpu = ctxt->vcpu;
1099 struct kvm_io_device *mmio_dev;
1100 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101
Avi Kivityc9047f52007-04-17 10:53:22 +03001102 if (gpa == UNMAPPED_GVA) {
1103 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001105 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106
Avi Kivityda4a00f2007-01-05 16:36:44 -08001107 if (emulator_write_phys(vcpu, gpa, val, bytes))
1108 return X86EMUL_CONTINUE;
1109
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001110 /*
1111 * Is this MMIO handled locally?
1112 */
1113 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1114 if (mmio_dev) {
1115 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1116 return X86EMUL_CONTINUE;
1117 }
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 vcpu->mmio_needed = 1;
1120 vcpu->mmio_phys_addr = gpa;
1121 vcpu->mmio_size = bytes;
1122 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001123 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124
1125 return X86EMUL_CONTINUE;
1126}
1127
Avi Kivityb0fcd902007-07-22 18:48:54 +03001128static int emulator_write_emulated(unsigned long addr,
1129 const void *val,
1130 unsigned int bytes,
1131 struct x86_emulate_ctxt *ctxt)
1132{
1133 /* Crossing a page boundary? */
1134 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1135 int rc, now;
1136
1137 now = -addr & ~PAGE_MASK;
1138 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1139 if (rc != X86EMUL_CONTINUE)
1140 return rc;
1141 addr += now;
1142 val += now;
1143 bytes -= now;
1144 }
1145 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1146}
1147
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001149 const void *old,
1150 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 unsigned int bytes,
1152 struct x86_emulate_ctxt *ctxt)
1153{
1154 static int reported;
1155
1156 if (!reported) {
1157 reported = 1;
1158 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1159 }
1160 return emulator_write_emulated(addr, new, bytes, ctxt);
1161}
1162
1163static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1164{
1165 return kvm_arch_ops->get_segment_base(vcpu, seg);
1166}
1167
1168int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1169{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 return X86EMUL_CONTINUE;
1171}
1172
1173int emulate_clts(struct kvm_vcpu *vcpu)
1174{
Avi Kivity399badf2007-01-05 16:36:38 -08001175 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176
Rusty Russell707d92fa2007-07-17 23:19:08 +10001177 cr0 = vcpu->cr0 & ~X86_CR0_TS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 kvm_arch_ops->set_cr0(vcpu, cr0);
1179 return X86EMUL_CONTINUE;
1180}
1181
1182int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1183{
1184 struct kvm_vcpu *vcpu = ctxt->vcpu;
1185
1186 switch (dr) {
1187 case 0 ... 3:
1188 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1189 return X86EMUL_CONTINUE;
1190 default:
1191 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1192 __FUNCTION__, dr);
1193 return X86EMUL_UNHANDLEABLE;
1194 }
1195}
1196
1197int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1198{
1199 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1200 int exception;
1201
1202 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1203 if (exception) {
1204 /* FIXME: better handling */
1205 return X86EMUL_UNHANDLEABLE;
1206 }
1207 return X86EMUL_CONTINUE;
1208}
1209
1210static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1211{
1212 static int reported;
1213 u8 opcodes[4];
1214 unsigned long rip = ctxt->vcpu->rip;
1215 unsigned long rip_linear;
1216
1217 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1218
1219 if (reported)
1220 return;
1221
1222 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1223
1224 printk(KERN_ERR "emulation failed but !mmio_needed?"
1225 " rip %lx %02x %02x %02x %02x\n",
1226 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1227 reported = 1;
1228}
1229
1230struct x86_emulate_ops emulate_ops = {
1231 .read_std = emulator_read_std,
1232 .write_std = emulator_write_std,
1233 .read_emulated = emulator_read_emulated,
1234 .write_emulated = emulator_write_emulated,
1235 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1236};
1237
1238int emulate_instruction(struct kvm_vcpu *vcpu,
1239 struct kvm_run *run,
1240 unsigned long cr2,
1241 u16 error_code)
1242{
1243 struct x86_emulate_ctxt emulate_ctxt;
1244 int r;
1245 int cs_db, cs_l;
1246
Avi Kivitye7df56e2007-03-14 15:54:54 +02001247 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 kvm_arch_ops->cache_regs(vcpu);
1249
1250 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1251
1252 emulate_ctxt.vcpu = vcpu;
1253 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1254 emulate_ctxt.cr2 = cr2;
1255 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1256 ? X86EMUL_MODE_REAL : cs_l
1257 ? X86EMUL_MODE_PROT64 : cs_db
1258 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1259
1260 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1261 emulate_ctxt.cs_base = 0;
1262 emulate_ctxt.ds_base = 0;
1263 emulate_ctxt.es_base = 0;
1264 emulate_ctxt.ss_base = 0;
1265 } else {
1266 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1267 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1268 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1269 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1270 }
1271
1272 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1273 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1274
1275 vcpu->mmio_is_write = 0;
1276 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1277
1278 if ((r || vcpu->mmio_is_write) && run) {
1279 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1280 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1281 run->mmio.len = vcpu->mmio_size;
1282 run->mmio.is_write = vcpu->mmio_is_write;
1283 }
1284
1285 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001286 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1287 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288 if (!vcpu->mmio_needed) {
1289 report_emulation_failure(&emulate_ctxt);
1290 return EMULATE_FAIL;
1291 }
1292 return EMULATE_DO_MMIO;
1293 }
1294
1295 kvm_arch_ops->decache_regs(vcpu);
1296 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1297
Avi Kivity02c83202007-04-29 15:02:17 +03001298 if (vcpu->mmio_is_write) {
1299 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001301 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302
1303 return EMULATE_DONE;
1304}
1305EXPORT_SYMBOL_GPL(emulate_instruction);
1306
Avi Kivityd3bef152007-06-05 15:53:05 +03001307int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1308{
1309 if (vcpu->irq_summary)
1310 return 1;
1311
1312 vcpu->run->exit_reason = KVM_EXIT_HLT;
1313 ++vcpu->stat.halt_exits;
1314 return 0;
1315}
1316EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1317
Avi Kivity270fd9b2007-02-19 14:37:47 +02001318int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1319{
1320 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1321
Dor Laor9b22bf52007-02-19 16:44:49 +02001322 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001323 ret = -KVM_EINVAL;
1324#ifdef CONFIG_X86_64
1325 if (is_long_mode(vcpu)) {
1326 nr = vcpu->regs[VCPU_REGS_RAX];
1327 a0 = vcpu->regs[VCPU_REGS_RDI];
1328 a1 = vcpu->regs[VCPU_REGS_RSI];
1329 a2 = vcpu->regs[VCPU_REGS_RDX];
1330 a3 = vcpu->regs[VCPU_REGS_RCX];
1331 a4 = vcpu->regs[VCPU_REGS_R8];
1332 a5 = vcpu->regs[VCPU_REGS_R9];
1333 } else
1334#endif
1335 {
1336 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1337 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1338 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1339 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1340 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1341 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1342 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1343 }
1344 switch (nr) {
1345 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001346 run->hypercall.args[0] = a0;
1347 run->hypercall.args[1] = a1;
1348 run->hypercall.args[2] = a2;
1349 run->hypercall.args[3] = a3;
1350 run->hypercall.args[4] = a4;
1351 run->hypercall.args[5] = a5;
1352 run->hypercall.ret = ret;
1353 run->hypercall.longmode = is_long_mode(vcpu);
1354 kvm_arch_ops->decache_regs(vcpu);
1355 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001356 }
1357 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001358 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001359 return 1;
1360}
1361EXPORT_SYMBOL_GPL(kvm_hypercall);
1362
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1364{
1365 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1366}
1367
1368void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1369{
1370 struct descriptor_table dt = { limit, base };
1371
1372 kvm_arch_ops->set_gdt(vcpu, &dt);
1373}
1374
1375void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1376{
1377 struct descriptor_table dt = { limit, base };
1378
1379 kvm_arch_ops->set_idt(vcpu, &dt);
1380}
1381
1382void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1383 unsigned long *rflags)
1384{
1385 lmsw(vcpu, msw);
1386 *rflags = kvm_arch_ops->get_rflags(vcpu);
1387}
1388
1389unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1390{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001391 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 switch (cr) {
1393 case 0:
1394 return vcpu->cr0;
1395 case 2:
1396 return vcpu->cr2;
1397 case 3:
1398 return vcpu->cr3;
1399 case 4:
1400 return vcpu->cr4;
1401 default:
1402 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1403 return 0;
1404 }
1405}
1406
1407void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1408 unsigned long *rflags)
1409{
1410 switch (cr) {
1411 case 0:
1412 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1413 *rflags = kvm_arch_ops->get_rflags(vcpu);
1414 break;
1415 case 2:
1416 vcpu->cr2 = val;
1417 break;
1418 case 3:
1419 set_cr3(vcpu, val);
1420 break;
1421 case 4:
1422 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1423 break;
1424 default:
1425 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1426 }
1427}
1428
Ingo Molnar102d8322007-02-19 14:37:47 +02001429/*
1430 * Register the para guest with the host:
1431 */
1432static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1433{
1434 struct kvm_vcpu_para_state *para_state;
1435 hpa_t para_state_hpa, hypercall_hpa;
1436 struct page *para_state_page;
1437 unsigned char *hypercall;
1438 gpa_t hypercall_gpa;
1439
1440 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1441 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1442
1443 /*
1444 * Needs to be page aligned:
1445 */
1446 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1447 goto err_gp;
1448
1449 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1450 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1451 if (is_error_hpa(para_state_hpa))
1452 goto err_gp;
1453
Uri Lublinab51a432007-02-21 18:25:21 +02001454 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001455 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1456 para_state = kmap_atomic(para_state_page, KM_USER0);
1457
1458 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1459 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1460
1461 para_state->host_version = KVM_PARA_API_VERSION;
1462 /*
1463 * We cannot support guests that try to register themselves
1464 * with a newer API version than the host supports:
1465 */
1466 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1467 para_state->ret = -KVM_EINVAL;
1468 goto err_kunmap_skip;
1469 }
1470
1471 hypercall_gpa = para_state->hypercall_gpa;
1472 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1473 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1474 if (is_error_hpa(hypercall_hpa)) {
1475 para_state->ret = -KVM_EINVAL;
1476 goto err_kunmap_skip;
1477 }
1478
1479 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1480 vcpu->para_state_page = para_state_page;
1481 vcpu->para_state_gpa = para_state_gpa;
1482 vcpu->hypercall_gpa = hypercall_gpa;
1483
Uri Lublinab51a432007-02-21 18:25:21 +02001484 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001485 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1486 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1487 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1488 kunmap_atomic(hypercall, KM_USER1);
1489
1490 para_state->ret = 0;
1491err_kunmap_skip:
1492 kunmap_atomic(para_state, KM_USER0);
1493 return 0;
1494err_gp:
1495 return 1;
1496}
1497
Avi Kivity3bab1f52006-12-29 16:49:48 -08001498int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1499{
1500 u64 data;
1501
1502 switch (msr) {
1503 case 0xc0010010: /* SYSCFG */
1504 case 0xc0010015: /* HWCR */
1505 case MSR_IA32_PLATFORM_ID:
1506 case MSR_IA32_P5_MC_ADDR:
1507 case MSR_IA32_P5_MC_TYPE:
1508 case MSR_IA32_MC0_CTL:
1509 case MSR_IA32_MCG_STATUS:
1510 case MSR_IA32_MCG_CAP:
1511 case MSR_IA32_MC0_MISC:
1512 case MSR_IA32_MC0_MISC+4:
1513 case MSR_IA32_MC0_MISC+8:
1514 case MSR_IA32_MC0_MISC+12:
1515 case MSR_IA32_MC0_MISC+16:
1516 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001517 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001518 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001519 /* MTRR registers */
1520 case 0xfe:
1521 case 0x200 ... 0x2ff:
1522 data = 0;
1523 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001524 case 0xcd: /* fsb frequency */
1525 data = 3;
1526 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001527 case MSR_IA32_APICBASE:
1528 data = vcpu->apic_base;
1529 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001530 case MSR_IA32_MISC_ENABLE:
1531 data = vcpu->ia32_misc_enable_msr;
1532 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001533#ifdef CONFIG_X86_64
1534 case MSR_EFER:
1535 data = vcpu->shadow_efer;
1536 break;
1537#endif
1538 default:
1539 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1540 return 1;
1541 }
1542 *pdata = data;
1543 return 0;
1544}
1545EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1546
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547/*
1548 * Reads an msr value (of 'msr_index') into 'pdata'.
1549 * Returns 0 on success, non-0 otherwise.
1550 * Assumes vcpu_load() was already called.
1551 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001552int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553{
1554 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1555}
1556
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001557#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558
Avi Kivity3bab1f52006-12-29 16:49:48 -08001559static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 if (efer & EFER_RESERVED_BITS) {
1562 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1563 efer);
1564 inject_gp(vcpu);
1565 return;
1566 }
1567
1568 if (is_paging(vcpu)
1569 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1570 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1571 inject_gp(vcpu);
1572 return;
1573 }
1574
Avi Kivity7725f0b2006-12-13 00:34:01 -08001575 kvm_arch_ops->set_efer(vcpu, efer);
1576
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 efer &= ~EFER_LMA;
1578 efer |= vcpu->shadow_efer & EFER_LMA;
1579
1580 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582
1583#endif
1584
Avi Kivity3bab1f52006-12-29 16:49:48 -08001585int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1586{
1587 switch (msr) {
1588#ifdef CONFIG_X86_64
1589 case MSR_EFER:
1590 set_efer(vcpu, data);
1591 break;
1592#endif
1593 case MSR_IA32_MC0_STATUS:
1594 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1595 __FUNCTION__, data);
1596 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001597 case MSR_IA32_MCG_STATUS:
1598 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1599 __FUNCTION__, data);
1600 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001601 case MSR_IA32_UCODE_REV:
1602 case MSR_IA32_UCODE_WRITE:
1603 case 0x200 ... 0x2ff: /* MTRRs */
1604 break;
1605 case MSR_IA32_APICBASE:
1606 vcpu->apic_base = data;
1607 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001608 case MSR_IA32_MISC_ENABLE:
1609 vcpu->ia32_misc_enable_msr = data;
1610 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001611 /*
1612 * This is the 'probe whether the host is KVM' logic:
1613 */
1614 case MSR_KVM_API_MAGIC:
1615 return vcpu_register_para(vcpu, data);
1616
Avi Kivity3bab1f52006-12-29 16:49:48 -08001617 default:
1618 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1619 return 1;
1620 }
1621 return 0;
1622}
1623EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1624
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625/*
1626 * Writes msr value into into the appropriate "register".
1627 * Returns 0 on success, non-0 otherwise.
1628 * Assumes vcpu_load() was already called.
1629 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001630int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631{
1632 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1633}
1634
1635void kvm_resched(struct kvm_vcpu *vcpu)
1636{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001637 if (!need_resched())
1638 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 vcpu_put(vcpu);
1640 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001641 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642}
1643EXPORT_SYMBOL_GPL(kvm_resched);
1644
1645void load_msrs(struct vmx_msr_entry *e, int n)
1646{
1647 int i;
1648
1649 for (i = 0; i < n; ++i)
1650 wrmsrl(e[i].index, e[i].data);
1651}
1652EXPORT_SYMBOL_GPL(load_msrs);
1653
1654void save_msrs(struct vmx_msr_entry *e, int n)
1655{
1656 int i;
1657
1658 for (i = 0; i < n; ++i)
1659 rdmsrl(e[i].index, e[i].data);
1660}
1661EXPORT_SYMBOL_GPL(save_msrs);
1662
Avi Kivity06465c52007-02-28 20:46:53 +02001663void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1664{
1665 int i;
1666 u32 function;
1667 struct kvm_cpuid_entry *e, *best;
1668
1669 kvm_arch_ops->cache_regs(vcpu);
1670 function = vcpu->regs[VCPU_REGS_RAX];
1671 vcpu->regs[VCPU_REGS_RAX] = 0;
1672 vcpu->regs[VCPU_REGS_RBX] = 0;
1673 vcpu->regs[VCPU_REGS_RCX] = 0;
1674 vcpu->regs[VCPU_REGS_RDX] = 0;
1675 best = NULL;
1676 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1677 e = &vcpu->cpuid_entries[i];
1678 if (e->function == function) {
1679 best = e;
1680 break;
1681 }
1682 /*
1683 * Both basic or both extended?
1684 */
1685 if (((e->function ^ function) & 0x80000000) == 0)
1686 if (!best || e->function > best->function)
1687 best = e;
1688 }
1689 if (best) {
1690 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1691 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1692 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1693 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1694 }
1695 kvm_arch_ops->decache_regs(vcpu);
1696 kvm_arch_ops->skip_emulated_instruction(vcpu);
1697}
1698EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1699
Avi Kivity039576c2007-03-20 12:46:50 +02001700static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001701{
Avi Kivity039576c2007-03-20 12:46:50 +02001702 void *p = vcpu->pio_data;
1703 void *q;
1704 unsigned bytes;
1705 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1706
1707 kvm_arch_ops->vcpu_put(vcpu);
1708 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1709 PAGE_KERNEL);
1710 if (!q) {
1711 kvm_arch_ops->vcpu_load(vcpu);
1712 free_pio_guest_pages(vcpu);
1713 return -ENOMEM;
1714 }
1715 q += vcpu->pio.guest_page_offset;
1716 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1717 if (vcpu->pio.in)
1718 memcpy(q, p, bytes);
1719 else
1720 memcpy(p, q, bytes);
1721 q -= vcpu->pio.guest_page_offset;
1722 vunmap(q);
1723 kvm_arch_ops->vcpu_load(vcpu);
1724 free_pio_guest_pages(vcpu);
1725 return 0;
1726}
1727
1728static int complete_pio(struct kvm_vcpu *vcpu)
1729{
1730 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001731 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001732 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001733
1734 kvm_arch_ops->cache_regs(vcpu);
1735
1736 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001737 if (io->in)
1738 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001739 io->size);
1740 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001741 if (io->in) {
1742 r = pio_copy_data(vcpu);
1743 if (r) {
1744 kvm_arch_ops->cache_regs(vcpu);
1745 return r;
1746 }
1747 }
1748
Avi Kivity46fc1472007-02-22 19:39:30 +02001749 delta = 1;
1750 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001751 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001752 /*
1753 * The size of the register should really depend on
1754 * current address size.
1755 */
1756 vcpu->regs[VCPU_REGS_RCX] -= delta;
1757 }
Avi Kivity039576c2007-03-20 12:46:50 +02001758 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001759 delta = -delta;
1760 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001761 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001762 vcpu->regs[VCPU_REGS_RDI] += delta;
1763 else
1764 vcpu->regs[VCPU_REGS_RSI] += delta;
1765 }
1766
Avi Kivity46fc1472007-02-22 19:39:30 +02001767 kvm_arch_ops->decache_regs(vcpu);
1768
Avi Kivity039576c2007-03-20 12:46:50 +02001769 io->count -= io->cur_count;
1770 io->cur_count = 0;
1771
1772 if (!io->count)
1773 kvm_arch_ops->skip_emulated_instruction(vcpu);
1774 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001775}
1776
Eddie Dong65619eb2007-07-17 11:52:33 +03001777static void kernel_pio(struct kvm_io_device *pio_dev,
1778 struct kvm_vcpu *vcpu,
1779 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001780{
1781 /* TODO: String I/O for in kernel device */
1782
1783 if (vcpu->pio.in)
1784 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1785 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001786 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001787 else
1788 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1789 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001790 pd);
1791}
1792
1793static void pio_string_write(struct kvm_io_device *pio_dev,
1794 struct kvm_vcpu *vcpu)
1795{
1796 struct kvm_pio_request *io = &vcpu->pio;
1797 void *pd = vcpu->pio_data;
1798 int i;
1799
1800 for (i = 0; i < io->cur_count; i++) {
1801 kvm_iodevice_write(pio_dev, io->port,
1802 io->size,
1803 pd);
1804 pd += io->size;
1805 }
Eddie Dong74906342007-06-19 18:05:03 +03001806}
1807
Avi Kivity039576c2007-03-20 12:46:50 +02001808int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1809 int size, unsigned long count, int string, int down,
1810 gva_t address, int rep, unsigned port)
1811{
1812 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001813 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001814 int nr_pages = 1;
1815 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001816 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001817
1818 vcpu->run->exit_reason = KVM_EXIT_IO;
1819 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1820 vcpu->run->io.size = size;
1821 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1822 vcpu->run->io.count = count;
1823 vcpu->run->io.port = port;
1824 vcpu->pio.count = count;
1825 vcpu->pio.cur_count = count;
1826 vcpu->pio.size = size;
1827 vcpu->pio.in = in;
Eddie Dong74906342007-06-19 18:05:03 +03001828 vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001829 vcpu->pio.string = string;
1830 vcpu->pio.down = down;
1831 vcpu->pio.guest_page_offset = offset_in_page(address);
1832 vcpu->pio.rep = rep;
1833
Eddie Dong74906342007-06-19 18:05:03 +03001834 pio_dev = vcpu_find_pio_dev(vcpu, port);
Avi Kivity039576c2007-03-20 12:46:50 +02001835 if (!string) {
1836 kvm_arch_ops->cache_regs(vcpu);
1837 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1838 kvm_arch_ops->decache_regs(vcpu);
Eddie Dong74906342007-06-19 18:05:03 +03001839 if (pio_dev) {
Eddie Dong65619eb2007-07-17 11:52:33 +03001840 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
Eddie Dong74906342007-06-19 18:05:03 +03001841 complete_pio(vcpu);
1842 return 1;
1843 }
Avi Kivity039576c2007-03-20 12:46:50 +02001844 return 0;
1845 }
1846
1847 if (!count) {
1848 kvm_arch_ops->skip_emulated_instruction(vcpu);
1849 return 1;
1850 }
1851
1852 now = min(count, PAGE_SIZE / size);
1853
1854 if (!down)
1855 in_page = PAGE_SIZE - offset_in_page(address);
1856 else
1857 in_page = offset_in_page(address) + size;
1858 now = min(count, (unsigned long)in_page / size);
1859 if (!now) {
1860 /*
1861 * String I/O straddles page boundary. Pin two guest pages
1862 * so that we satisfy atomicity constraints. Do just one
1863 * transaction to avoid complexity.
1864 */
1865 nr_pages = 2;
1866 now = 1;
1867 }
1868 if (down) {
1869 /*
1870 * String I/O in reverse. Yuck. Kill the guest, fix later.
1871 */
1872 printk(KERN_ERR "kvm: guest string pio down\n");
1873 inject_gp(vcpu);
1874 return 1;
1875 }
1876 vcpu->run->io.count = now;
1877 vcpu->pio.cur_count = now;
1878
1879 for (i = 0; i < nr_pages; ++i) {
1880 spin_lock(&vcpu->kvm->lock);
1881 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1882 if (page)
1883 get_page(page);
1884 vcpu->pio.guest_pages[i] = page;
1885 spin_unlock(&vcpu->kvm->lock);
1886 if (!page) {
1887 inject_gp(vcpu);
1888 free_pio_guest_pages(vcpu);
1889 return 1;
1890 }
1891 }
1892
Eddie Dong65619eb2007-07-17 11:52:33 +03001893 if (!vcpu->pio.in) {
1894 /* string PIO write */
1895 ret = pio_copy_data(vcpu);
1896 if (ret >= 0 && pio_dev) {
1897 pio_string_write(pio_dev, vcpu);
1898 complete_pio(vcpu);
1899 if (vcpu->pio.count == 0)
1900 ret = 1;
1901 }
1902 } else if (pio_dev)
1903 printk(KERN_ERR "no string pio read support yet, "
1904 "port %x size %d count %ld\n",
1905 port, size, count);
1906
1907 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001908}
1909EXPORT_SYMBOL_GPL(kvm_setup_pio);
1910
Avi Kivitybccf2152007-02-21 18:04:26 +02001911static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001914 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915
Avi Kivitybccf2152007-02-21 18:04:26 +02001916 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917
Avi Kivity1961d272007-03-05 19:46:05 +02001918 if (vcpu->sigset_active)
1919 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1920
Dor Laor54810342007-02-12 00:54:39 -08001921 /* re-sync apic's tpr */
1922 vcpu->cr8 = kvm_run->cr8;
1923
Avi Kivity02c83202007-04-29 15:02:17 +03001924 if (vcpu->pio.cur_count) {
1925 r = complete_pio(vcpu);
1926 if (r)
1927 goto out;
1928 }
1929
1930 if (vcpu->mmio_needed) {
1931 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1932 vcpu->mmio_read_completed = 1;
1933 vcpu->mmio_needed = 0;
1934 r = emulate_instruction(vcpu, kvm_run,
1935 vcpu->mmio_fault_cr2, 0);
1936 if (r == EMULATE_DO_MMIO) {
1937 /*
1938 * Read-modify-write. Back to userspace.
1939 */
1940 kvm_run->exit_reason = KVM_EXIT_MMIO;
1941 r = 0;
1942 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001943 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 }
1945
Avi Kivity8eb7d332007-03-04 14:17:08 +02001946 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001947 kvm_arch_ops->cache_regs(vcpu);
1948 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1949 kvm_arch_ops->decache_regs(vcpu);
1950 }
1951
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 r = kvm_arch_ops->run(vcpu, kvm_run);
1953
Avi Kivity039576c2007-03-20 12:46:50 +02001954out:
Avi Kivity1961d272007-03-05 19:46:05 +02001955 if (vcpu->sigset_active)
1956 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1957
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958 vcpu_put(vcpu);
1959 return r;
1960}
1961
Avi Kivitybccf2152007-02-21 18:04:26 +02001962static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1963 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964{
Avi Kivitybccf2152007-02-21 18:04:26 +02001965 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966
1967 kvm_arch_ops->cache_regs(vcpu);
1968
1969 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1970 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1971 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1972 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1973 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1974 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1975 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1976 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001977#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1979 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1980 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1981 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1982 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1983 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1984 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1985 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1986#endif
1987
1988 regs->rip = vcpu->rip;
1989 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1990
1991 /*
1992 * Don't leak debug flags in case they were set for guest debugging
1993 */
1994 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1995 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1996
1997 vcpu_put(vcpu);
1998
1999 return 0;
2000}
2001
Avi Kivitybccf2152007-02-21 18:04:26 +02002002static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2003 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004{
Avi Kivitybccf2152007-02-21 18:04:26 +02002005 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006
2007 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2008 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2009 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2010 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2011 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2012 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2013 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2014 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002015#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2017 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2018 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2019 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2020 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2021 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2022 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2023 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2024#endif
2025
2026 vcpu->rip = regs->rip;
2027 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2028
2029 kvm_arch_ops->decache_regs(vcpu);
2030
2031 vcpu_put(vcpu);
2032
2033 return 0;
2034}
2035
2036static void get_segment(struct kvm_vcpu *vcpu,
2037 struct kvm_segment *var, int seg)
2038{
2039 return kvm_arch_ops->get_segment(vcpu, var, seg);
2040}
2041
Avi Kivitybccf2152007-02-21 18:04:26 +02002042static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2043 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045 struct descriptor_table dt;
2046
Avi Kivitybccf2152007-02-21 18:04:26 +02002047 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048
2049 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2050 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2051 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2052 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2053 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2054 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2055
2056 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2057 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2058
2059 kvm_arch_ops->get_idt(vcpu, &dt);
2060 sregs->idt.limit = dt.limit;
2061 sregs->idt.base = dt.base;
2062 kvm_arch_ops->get_gdt(vcpu, &dt);
2063 sregs->gdt.limit = dt.limit;
2064 sregs->gdt.base = dt.base;
2065
Anthony Liguori25c4c272007-04-27 09:29:21 +03002066 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 sregs->cr0 = vcpu->cr0;
2068 sregs->cr2 = vcpu->cr2;
2069 sregs->cr3 = vcpu->cr3;
2070 sregs->cr4 = vcpu->cr4;
2071 sregs->cr8 = vcpu->cr8;
2072 sregs->efer = vcpu->shadow_efer;
2073 sregs->apic_base = vcpu->apic_base;
2074
2075 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2076 sizeof sregs->interrupt_bitmap);
2077
2078 vcpu_put(vcpu);
2079
2080 return 0;
2081}
2082
2083static void set_segment(struct kvm_vcpu *vcpu,
2084 struct kvm_segment *var, int seg)
2085{
2086 return kvm_arch_ops->set_segment(vcpu, var, seg);
2087}
2088
Avi Kivitybccf2152007-02-21 18:04:26 +02002089static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2090 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092 int mmu_reset_needed = 0;
2093 int i;
2094 struct descriptor_table dt;
2095
Avi Kivitybccf2152007-02-21 18:04:26 +02002096 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 dt.limit = sregs->idt.limit;
2099 dt.base = sregs->idt.base;
2100 kvm_arch_ops->set_idt(vcpu, &dt);
2101 dt.limit = sregs->gdt.limit;
2102 dt.base = sregs->gdt.base;
2103 kvm_arch_ops->set_gdt(vcpu, &dt);
2104
2105 vcpu->cr2 = sregs->cr2;
2106 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2107 vcpu->cr3 = sregs->cr3;
2108
2109 vcpu->cr8 = sregs->cr8;
2110
2111 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002112#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2114#endif
2115 vcpu->apic_base = sregs->apic_base;
2116
Anthony Liguori25c4c272007-04-27 09:29:21 +03002117 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002118
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002120 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121
2122 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2123 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002124 if (!is_long_mode(vcpu) && is_pae(vcpu))
2125 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126
2127 if (mmu_reset_needed)
2128 kvm_mmu_reset_context(vcpu);
2129
2130 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2131 sizeof vcpu->irq_pending);
2132 vcpu->irq_summary = 0;
2133 for (i = 0; i < NR_IRQ_WORDS; ++i)
2134 if (vcpu->irq_pending[i])
2135 __set_bit(i, &vcpu->irq_summary);
2136
Avi Kivity024aa1c2007-03-21 13:44:58 +02002137 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2138 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2139 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2140 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2141 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2142 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2143
2144 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2145 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2146
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 vcpu_put(vcpu);
2148
2149 return 0;
2150}
2151
2152/*
2153 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2154 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002155 *
2156 * This list is modified at module load time to reflect the
2157 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158 */
2159static u32 msrs_to_save[] = {
2160 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2161 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002162#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2164#endif
2165 MSR_IA32_TIME_STAMP_COUNTER,
2166};
2167
Michael Riepebf591b22006-12-22 01:05:36 -08002168static unsigned num_msrs_to_save;
2169
Avi Kivity6f00e682007-01-26 00:56:40 -08002170static u32 emulated_msrs[] = {
2171 MSR_IA32_MISC_ENABLE,
2172};
2173
Michael Riepebf591b22006-12-22 01:05:36 -08002174static __init void kvm_init_msr_list(void)
2175{
2176 u32 dummy[2];
2177 unsigned i, j;
2178
2179 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2180 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2181 continue;
2182 if (j < i)
2183 msrs_to_save[j] = msrs_to_save[i];
2184 j++;
2185 }
2186 num_msrs_to_save = j;
2187}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188
2189/*
2190 * Adapt set_msr() to msr_io()'s calling convention
2191 */
2192static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2193{
Avi Kivity35f3f282007-07-17 14:20:30 +03002194 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195}
2196
2197/*
2198 * Read or write a bunch of msrs. All parameters are kernel addresses.
2199 *
2200 * @return number of msrs set successfully.
2201 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002202static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 struct kvm_msr_entry *entries,
2204 int (*do_msr)(struct kvm_vcpu *vcpu,
2205 unsigned index, u64 *data))
2206{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 int i;
2208
Avi Kivitybccf2152007-02-21 18:04:26 +02002209 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210
2211 for (i = 0; i < msrs->nmsrs; ++i)
2212 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2213 break;
2214
2215 vcpu_put(vcpu);
2216
2217 return i;
2218}
2219
2220/*
2221 * Read or write a bunch of msrs. Parameters are user addresses.
2222 *
2223 * @return number of msrs set successfully.
2224 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002225static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 int (*do_msr)(struct kvm_vcpu *vcpu,
2227 unsigned index, u64 *data),
2228 int writeback)
2229{
2230 struct kvm_msrs msrs;
2231 struct kvm_msr_entry *entries;
2232 int r, n;
2233 unsigned size;
2234
2235 r = -EFAULT;
2236 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2237 goto out;
2238
2239 r = -E2BIG;
2240 if (msrs.nmsrs >= MAX_IO_MSRS)
2241 goto out;
2242
2243 r = -ENOMEM;
2244 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2245 entries = vmalloc(size);
2246 if (!entries)
2247 goto out;
2248
2249 r = -EFAULT;
2250 if (copy_from_user(entries, user_msrs->entries, size))
2251 goto out_free;
2252
Avi Kivitybccf2152007-02-21 18:04:26 +02002253 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002254 if (r < 0)
2255 goto out_free;
2256
2257 r = -EFAULT;
2258 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2259 goto out_free;
2260
2261 r = n;
2262
2263out_free:
2264 vfree(entries);
2265out:
2266 return r;
2267}
2268
2269/*
2270 * Translate a guest virtual address to a guest physical address.
2271 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002272static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2273 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274{
2275 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 gpa_t gpa;
2277
Avi Kivitybccf2152007-02-21 18:04:26 +02002278 vcpu_load(vcpu);
2279 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2281 tr->physical_address = gpa;
2282 tr->valid = gpa != UNMAPPED_GVA;
2283 tr->writeable = 1;
2284 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002285 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 vcpu_put(vcpu);
2287
2288 return 0;
2289}
2290
Avi Kivitybccf2152007-02-21 18:04:26 +02002291static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2292 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 if (irq->irq < 0 || irq->irq >= 256)
2295 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002296 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002297
2298 set_bit(irq->irq, vcpu->irq_pending);
2299 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2300
2301 vcpu_put(vcpu);
2302
2303 return 0;
2304}
2305
Avi Kivitybccf2152007-02-21 18:04:26 +02002306static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2307 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 int r;
2310
Avi Kivitybccf2152007-02-21 18:04:26 +02002311 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
2313 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2314
2315 vcpu_put(vcpu);
2316
2317 return r;
2318}
2319
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002320static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2321 unsigned long address,
2322 int *type)
2323{
2324 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2325 unsigned long pgoff;
2326 struct page *page;
2327
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002328 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002329 if (pgoff == 0)
2330 page = virt_to_page(vcpu->run);
2331 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2332 page = virt_to_page(vcpu->pio_data);
2333 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002334 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002335 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002336 if (type != NULL)
2337 *type = VM_FAULT_MINOR;
2338
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002339 return page;
2340}
2341
2342static struct vm_operations_struct kvm_vcpu_vm_ops = {
2343 .nopage = kvm_vcpu_nopage,
2344};
2345
2346static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2347{
2348 vma->vm_ops = &kvm_vcpu_vm_ops;
2349 return 0;
2350}
2351
Avi Kivitybccf2152007-02-21 18:04:26 +02002352static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2353{
2354 struct kvm_vcpu *vcpu = filp->private_data;
2355
2356 fput(vcpu->kvm->filp);
2357 return 0;
2358}
2359
2360static struct file_operations kvm_vcpu_fops = {
2361 .release = kvm_vcpu_release,
2362 .unlocked_ioctl = kvm_vcpu_ioctl,
2363 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002364 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002365};
2366
2367/*
2368 * Allocates an inode for the vcpu.
2369 */
2370static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2371{
2372 int fd, r;
2373 struct inode *inode;
2374 struct file *file;
2375
Avi Kivityd6d28162007-06-28 08:38:16 -04002376 r = anon_inode_getfd(&fd, &inode, &file,
2377 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2378 if (r)
2379 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002380 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002381 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002382}
2383
Avi Kivityc5ea7662007-02-20 18:41:05 +02002384/*
2385 * Creates some virtual cpus. Good luck creating more than one.
2386 */
2387static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2388{
2389 int r;
2390 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002391 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002392
2393 r = -EINVAL;
2394 if (!valid_vcpu(n))
2395 goto out;
2396
2397 vcpu = &kvm->vcpus[n];
Qing Hedad37952007-07-12 12:33:56 +03002398 vcpu->vcpu_id = n;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002399
2400 mutex_lock(&vcpu->mutex);
2401
2402 if (vcpu->vmcs) {
2403 mutex_unlock(&vcpu->mutex);
2404 return -EEXIST;
2405 }
2406
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002407 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2408 r = -ENOMEM;
2409 if (!page)
2410 goto out_unlock;
2411 vcpu->run = page_address(page);
2412
Avi Kivity039576c2007-03-20 12:46:50 +02002413 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2414 r = -ENOMEM;
2415 if (!page)
2416 goto out_free_run;
2417 vcpu->pio_data = page_address(page);
2418
Avi Kivityc5ea7662007-02-20 18:41:05 +02002419 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2420 FX_IMAGE_ALIGN);
2421 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
Avi Kivityd917a6b2007-04-12 13:03:01 +03002422 vcpu->cr0 = 0x10;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002423
2424 r = kvm_arch_ops->vcpu_create(vcpu);
2425 if (r < 0)
2426 goto out_free_vcpus;
2427
2428 r = kvm_mmu_create(vcpu);
2429 if (r < 0)
2430 goto out_free_vcpus;
2431
2432 kvm_arch_ops->vcpu_load(vcpu);
2433 r = kvm_mmu_setup(vcpu);
2434 if (r >= 0)
2435 r = kvm_arch_ops->vcpu_setup(vcpu);
2436 vcpu_put(vcpu);
2437
2438 if (r < 0)
2439 goto out_free_vcpus;
2440
Avi Kivitybccf2152007-02-21 18:04:26 +02002441 r = create_vcpu_fd(vcpu);
2442 if (r < 0)
2443 goto out_free_vcpus;
2444
Avi Kivity39c3b862007-06-07 19:11:53 +03002445 spin_lock(&kvm_lock);
2446 if (n >= kvm->nvcpus)
2447 kvm->nvcpus = n + 1;
2448 spin_unlock(&kvm_lock);
2449
Avi Kivitybccf2152007-02-21 18:04:26 +02002450 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002451
2452out_free_vcpus:
2453 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002454out_free_run:
2455 free_page((unsigned long)vcpu->run);
2456 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002457out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002458 mutex_unlock(&vcpu->mutex);
2459out:
2460 return r;
2461}
2462
Eddie Dong2cc51562007-05-21 07:28:09 +03002463static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2464{
2465 u64 efer;
2466 int i;
2467 struct kvm_cpuid_entry *e, *entry;
2468
2469 rdmsrl(MSR_EFER, efer);
2470 entry = NULL;
2471 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2472 e = &vcpu->cpuid_entries[i];
2473 if (e->function == 0x80000001) {
2474 entry = e;
2475 break;
2476 }
2477 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002478 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002479 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002480 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002481 }
2482}
2483
Avi Kivity06465c52007-02-28 20:46:53 +02002484static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2485 struct kvm_cpuid *cpuid,
2486 struct kvm_cpuid_entry __user *entries)
2487{
2488 int r;
2489
2490 r = -E2BIG;
2491 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2492 goto out;
2493 r = -EFAULT;
2494 if (copy_from_user(&vcpu->cpuid_entries, entries,
2495 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2496 goto out;
2497 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002498 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002499 return 0;
2500
2501out:
2502 return r;
2503}
2504
Avi Kivity1961d272007-03-05 19:46:05 +02002505static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2506{
2507 if (sigset) {
2508 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2509 vcpu->sigset_active = 1;
2510 vcpu->sigset = *sigset;
2511 } else
2512 vcpu->sigset_active = 0;
2513 return 0;
2514}
2515
Avi Kivityb8836732007-04-01 16:34:31 +03002516/*
2517 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2518 * we have asm/x86/processor.h
2519 */
2520struct fxsave {
2521 u16 cwd;
2522 u16 swd;
2523 u16 twd;
2524 u16 fop;
2525 u64 rip;
2526 u64 rdp;
2527 u32 mxcsr;
2528 u32 mxcsr_mask;
2529 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2530#ifdef CONFIG_X86_64
2531 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2532#else
2533 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2534#endif
2535};
2536
2537static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2538{
2539 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2540
2541 vcpu_load(vcpu);
2542
2543 memcpy(fpu->fpr, fxsave->st_space, 128);
2544 fpu->fcw = fxsave->cwd;
2545 fpu->fsw = fxsave->swd;
2546 fpu->ftwx = fxsave->twd;
2547 fpu->last_opcode = fxsave->fop;
2548 fpu->last_ip = fxsave->rip;
2549 fpu->last_dp = fxsave->rdp;
2550 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2551
2552 vcpu_put(vcpu);
2553
2554 return 0;
2555}
2556
2557static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2558{
2559 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2560
2561 vcpu_load(vcpu);
2562
2563 memcpy(fxsave->st_space, fpu->fpr, 128);
2564 fxsave->cwd = fpu->fcw;
2565 fxsave->swd = fpu->fsw;
2566 fxsave->twd = fpu->ftwx;
2567 fxsave->fop = fpu->last_opcode;
2568 fxsave->rip = fpu->last_ip;
2569 fxsave->rdp = fpu->last_dp;
2570 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2571
2572 vcpu_put(vcpu);
2573
2574 return 0;
2575}
2576
Avi Kivitybccf2152007-02-21 18:04:26 +02002577static long kvm_vcpu_ioctl(struct file *filp,
2578 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579{
Avi Kivitybccf2152007-02-21 18:04:26 +02002580 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002581 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 int r = -EINVAL;
2583
2584 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002585 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002586 r = -EINVAL;
2587 if (arg)
2588 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002589 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591 case KVM_GET_REGS: {
2592 struct kvm_regs kvm_regs;
2593
Avi Kivitybccf2152007-02-21 18:04:26 +02002594 memset(&kvm_regs, 0, sizeof kvm_regs);
2595 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 if (r)
2597 goto out;
2598 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002599 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 goto out;
2601 r = 0;
2602 break;
2603 }
2604 case KVM_SET_REGS: {
2605 struct kvm_regs kvm_regs;
2606
2607 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002608 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002610 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611 if (r)
2612 goto out;
2613 r = 0;
2614 break;
2615 }
2616 case KVM_GET_SREGS: {
2617 struct kvm_sregs kvm_sregs;
2618
Avi Kivitybccf2152007-02-21 18:04:26 +02002619 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2620 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 if (r)
2622 goto out;
2623 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002624 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 goto out;
2626 r = 0;
2627 break;
2628 }
2629 case KVM_SET_SREGS: {
2630 struct kvm_sregs kvm_sregs;
2631
2632 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002633 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002635 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 if (r)
2637 goto out;
2638 r = 0;
2639 break;
2640 }
2641 case KVM_TRANSLATE: {
2642 struct kvm_translation tr;
2643
2644 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002645 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002647 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 if (r)
2649 goto out;
2650 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002651 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 goto out;
2653 r = 0;
2654 break;
2655 }
2656 case KVM_INTERRUPT: {
2657 struct kvm_interrupt irq;
2658
2659 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002660 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002662 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663 if (r)
2664 goto out;
2665 r = 0;
2666 break;
2667 }
2668 case KVM_DEBUG_GUEST: {
2669 struct kvm_debug_guest dbg;
2670
2671 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002672 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002674 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 if (r)
2676 goto out;
2677 r = 0;
2678 break;
2679 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002680 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002681 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002682 break;
2683 case KVM_SET_MSRS:
2684 r = msr_io(vcpu, argp, do_set_msr, 0);
2685 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002686 case KVM_SET_CPUID: {
2687 struct kvm_cpuid __user *cpuid_arg = argp;
2688 struct kvm_cpuid cpuid;
2689
2690 r = -EFAULT;
2691 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2692 goto out;
2693 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2694 if (r)
2695 goto out;
2696 break;
2697 }
Avi Kivity1961d272007-03-05 19:46:05 +02002698 case KVM_SET_SIGNAL_MASK: {
2699 struct kvm_signal_mask __user *sigmask_arg = argp;
2700 struct kvm_signal_mask kvm_sigmask;
2701 sigset_t sigset, *p;
2702
2703 p = NULL;
2704 if (argp) {
2705 r = -EFAULT;
2706 if (copy_from_user(&kvm_sigmask, argp,
2707 sizeof kvm_sigmask))
2708 goto out;
2709 r = -EINVAL;
2710 if (kvm_sigmask.len != sizeof sigset)
2711 goto out;
2712 r = -EFAULT;
2713 if (copy_from_user(&sigset, sigmask_arg->sigset,
2714 sizeof sigset))
2715 goto out;
2716 p = &sigset;
2717 }
2718 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2719 break;
2720 }
Avi Kivityb8836732007-04-01 16:34:31 +03002721 case KVM_GET_FPU: {
2722 struct kvm_fpu fpu;
2723
2724 memset(&fpu, 0, sizeof fpu);
2725 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2726 if (r)
2727 goto out;
2728 r = -EFAULT;
2729 if (copy_to_user(argp, &fpu, sizeof fpu))
2730 goto out;
2731 r = 0;
2732 break;
2733 }
2734 case KVM_SET_FPU: {
2735 struct kvm_fpu fpu;
2736
2737 r = -EFAULT;
2738 if (copy_from_user(&fpu, argp, sizeof fpu))
2739 goto out;
2740 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2741 if (r)
2742 goto out;
2743 r = 0;
2744 break;
2745 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002746 default:
2747 ;
2748 }
2749out:
2750 return r;
2751}
2752
2753static long kvm_vm_ioctl(struct file *filp,
2754 unsigned int ioctl, unsigned long arg)
2755{
2756 struct kvm *kvm = filp->private_data;
2757 void __user *argp = (void __user *)arg;
2758 int r = -EINVAL;
2759
2760 switch (ioctl) {
2761 case KVM_CREATE_VCPU:
2762 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2763 if (r < 0)
2764 goto out;
2765 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 case KVM_SET_MEMORY_REGION: {
2767 struct kvm_memory_region kvm_mem;
2768
2769 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002770 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002772 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 if (r)
2774 goto out;
2775 break;
2776 }
2777 case KVM_GET_DIRTY_LOG: {
2778 struct kvm_dirty_log log;
2779
2780 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002781 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002783 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 if (r)
2785 goto out;
2786 break;
2787 }
Avi Kivitye8207542007-03-30 16:54:30 +03002788 case KVM_SET_MEMORY_ALIAS: {
2789 struct kvm_memory_alias alias;
2790
2791 r = -EFAULT;
2792 if (copy_from_user(&alias, argp, sizeof alias))
2793 goto out;
2794 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2795 if (r)
2796 goto out;
2797 break;
2798 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002799 default:
2800 ;
2801 }
2802out:
2803 return r;
2804}
2805
2806static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2807 unsigned long address,
2808 int *type)
2809{
2810 struct kvm *kvm = vma->vm_file->private_data;
2811 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002812 struct page *page;
2813
Avi Kivityf17abe92007-02-21 19:28:04 +02002814 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002815 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002816 if (!page)
2817 return NOPAGE_SIGBUS;
2818 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002819 if (type != NULL)
2820 *type = VM_FAULT_MINOR;
2821
Avi Kivityf17abe92007-02-21 19:28:04 +02002822 return page;
2823}
2824
2825static struct vm_operations_struct kvm_vm_vm_ops = {
2826 .nopage = kvm_vm_nopage,
2827};
2828
2829static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2830{
2831 vma->vm_ops = &kvm_vm_vm_ops;
2832 return 0;
2833}
2834
2835static struct file_operations kvm_vm_fops = {
2836 .release = kvm_vm_release,
2837 .unlocked_ioctl = kvm_vm_ioctl,
2838 .compat_ioctl = kvm_vm_ioctl,
2839 .mmap = kvm_vm_mmap,
2840};
2841
2842static int kvm_dev_ioctl_create_vm(void)
2843{
2844 int fd, r;
2845 struct inode *inode;
2846 struct file *file;
2847 struct kvm *kvm;
2848
Avi Kivityf17abe92007-02-21 19:28:04 +02002849 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002850 if (IS_ERR(kvm))
2851 return PTR_ERR(kvm);
2852 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2853 if (r) {
2854 kvm_destroy_vm(kvm);
2855 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002856 }
2857
Avi Kivitybccf2152007-02-21 18:04:26 +02002858 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002859
Avi Kivityf17abe92007-02-21 19:28:04 +02002860 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002861}
2862
2863static long kvm_dev_ioctl(struct file *filp,
2864 unsigned int ioctl, unsigned long arg)
2865{
2866 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002867 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002868
2869 switch (ioctl) {
2870 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002871 r = -EINVAL;
2872 if (arg)
2873 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002874 r = KVM_API_VERSION;
2875 break;
2876 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002877 r = -EINVAL;
2878 if (arg)
2879 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002880 r = kvm_dev_ioctl_create_vm();
2881 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002883 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 struct kvm_msr_list msr_list;
2885 unsigned n;
2886
2887 r = -EFAULT;
2888 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2889 goto out;
2890 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002891 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2893 goto out;
2894 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002895 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 goto out;
2897 r = -EFAULT;
2898 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002899 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002901 if (copy_to_user(user_msr_list->indices
2902 + num_msrs_to_save * sizeof(u32),
2903 &emulated_msrs,
2904 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2905 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002907 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002909 case KVM_CHECK_EXTENSION:
2910 /*
2911 * No extensions defined at present.
2912 */
2913 r = 0;
2914 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002915 case KVM_GET_VCPU_MMAP_SIZE:
2916 r = -EINVAL;
2917 if (arg)
2918 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002919 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002920 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 default:
2922 ;
2923 }
2924out:
2925 return r;
2926}
2927
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928static struct file_operations kvm_chardev_ops = {
2929 .open = kvm_dev_open,
2930 .release = kvm_dev_release,
2931 .unlocked_ioctl = kvm_dev_ioctl,
2932 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933};
2934
2935static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002936 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937 "kvm",
2938 &kvm_chardev_ops,
2939};
2940
Avi Kivity774c47f2007-02-12 00:54:47 -08002941/*
2942 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2943 * cached on it.
2944 */
2945static void decache_vcpus_on_cpu(int cpu)
2946{
2947 struct kvm *vm;
2948 struct kvm_vcpu *vcpu;
2949 int i;
2950
2951 spin_lock(&kvm_lock);
2952 list_for_each_entry(vm, &vm_list, vm_list)
2953 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2954 vcpu = &vm->vcpus[i];
2955 /*
2956 * If the vcpu is locked, then it is running on some
2957 * other cpu and therefore it is not cached on the
2958 * cpu in question.
2959 *
2960 * If it's not locked, check the last cpu it executed
2961 * on.
2962 */
2963 if (mutex_trylock(&vcpu->mutex)) {
2964 if (vcpu->cpu == cpu) {
2965 kvm_arch_ops->vcpu_decache(vcpu);
2966 vcpu->cpu = -1;
2967 }
2968 mutex_unlock(&vcpu->mutex);
2969 }
2970 }
2971 spin_unlock(&kvm_lock);
2972}
2973
Avi Kivity1b6c0162007-05-24 13:03:52 +03002974static void hardware_enable(void *junk)
2975{
2976 int cpu = raw_smp_processor_id();
2977
2978 if (cpu_isset(cpu, cpus_hardware_enabled))
2979 return;
2980 cpu_set(cpu, cpus_hardware_enabled);
2981 kvm_arch_ops->hardware_enable(NULL);
2982}
2983
2984static void hardware_disable(void *junk)
2985{
2986 int cpu = raw_smp_processor_id();
2987
2988 if (!cpu_isset(cpu, cpus_hardware_enabled))
2989 return;
2990 cpu_clear(cpu, cpus_hardware_enabled);
2991 decache_vcpus_on_cpu(cpu);
2992 kvm_arch_ops->hardware_disable(NULL);
2993}
2994
Avi Kivity774c47f2007-02-12 00:54:47 -08002995static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2996 void *v)
2997{
2998 int cpu = (long)v;
2999
3000 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003001 case CPU_DYING:
3002 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003003 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3004 cpu);
3005 hardware_disable(NULL);
3006 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003007 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003008 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003009 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3010 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003011 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003012 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003013 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003014 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003015 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3016 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003017 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003018 break;
3019 }
3020 return NOTIFY_OK;
3021}
3022
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003023static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3024 void *v)
3025{
3026 if (val == SYS_RESTART) {
3027 /*
3028 * Some (well, at least mine) BIOSes hang on reboot if
3029 * in vmx root mode.
3030 */
3031 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3032 on_each_cpu(hardware_disable, NULL, 0, 1);
3033 }
3034 return NOTIFY_OK;
3035}
3036
3037static struct notifier_block kvm_reboot_notifier = {
3038 .notifier_call = kvm_reboot,
3039 .priority = 0,
3040};
3041
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003042void kvm_io_bus_init(struct kvm_io_bus *bus)
3043{
3044 memset(bus, 0, sizeof(*bus));
3045}
3046
3047void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3048{
3049 int i;
3050
3051 for (i = 0; i < bus->dev_count; i++) {
3052 struct kvm_io_device *pos = bus->devs[i];
3053
3054 kvm_iodevice_destructor(pos);
3055 }
3056}
3057
3058struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3059{
3060 int i;
3061
3062 for (i = 0; i < bus->dev_count; i++) {
3063 struct kvm_io_device *pos = bus->devs[i];
3064
3065 if (pos->in_range(pos, addr))
3066 return pos;
3067 }
3068
3069 return NULL;
3070}
3071
3072void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3073{
3074 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3075
3076 bus->devs[bus->dev_count++] = dev;
3077}
3078
Avi Kivity774c47f2007-02-12 00:54:47 -08003079static struct notifier_block kvm_cpu_notifier = {
3080 .notifier_call = kvm_cpu_hotplug,
3081 .priority = 20, /* must be > scheduler priority */
3082};
3083
Avi Kivity1165f5f2007-04-19 17:27:43 +03003084static u64 stat_get(void *_offset)
3085{
3086 unsigned offset = (long)_offset;
3087 u64 total = 0;
3088 struct kvm *kvm;
3089 struct kvm_vcpu *vcpu;
3090 int i;
3091
3092 spin_lock(&kvm_lock);
3093 list_for_each_entry(kvm, &vm_list, vm_list)
3094 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3095 vcpu = &kvm->vcpus[i];
3096 total += *(u32 *)((void *)vcpu + offset);
3097 }
3098 spin_unlock(&kvm_lock);
3099 return total;
3100}
3101
3102static void stat_set(void *offset, u64 val)
3103{
3104}
3105
3106DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3107
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108static __init void kvm_init_debug(void)
3109{
3110 struct kvm_stats_debugfs_item *p;
3111
Al Viro8b6d44c2007-02-09 16:38:40 +00003112 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003114 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3115 (void *)(long)p->offset,
3116 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117}
3118
3119static void kvm_exit_debug(void)
3120{
3121 struct kvm_stats_debugfs_item *p;
3122
3123 for (p = debugfs_entries; p->name; ++p)
3124 debugfs_remove(p->dentry);
3125 debugfs_remove(debugfs_dir);
3126}
3127
Avi Kivity59ae6c62007-02-12 00:54:48 -08003128static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3129{
Avi Kivity4267c412007-05-24 13:09:41 +03003130 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003131 return 0;
3132}
3133
3134static int kvm_resume(struct sys_device *dev)
3135{
Avi Kivity4267c412007-05-24 13:09:41 +03003136 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003137 return 0;
3138}
3139
3140static struct sysdev_class kvm_sysdev_class = {
3141 set_kset_name("kvm"),
3142 .suspend = kvm_suspend,
3143 .resume = kvm_resume,
3144};
3145
3146static struct sys_device kvm_sysdev = {
3147 .id = 0,
3148 .cls = &kvm_sysdev_class,
3149};
3150
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151hpa_t bad_page_address;
3152
3153int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3154{
3155 int r;
3156
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003157 if (kvm_arch_ops) {
3158 printk(KERN_ERR "kvm: already loaded the other module\n");
3159 return -EEXIST;
3160 }
3161
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003162 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 printk(KERN_ERR "kvm: no hardware support\n");
3164 return -EOPNOTSUPP;
3165 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003166 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 printk(KERN_ERR "kvm: disabled by bios\n");
3168 return -EOPNOTSUPP;
3169 }
3170
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003171 kvm_arch_ops = ops;
3172
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 r = kvm_arch_ops->hardware_setup();
3174 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003175 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176
Avi Kivity1b6c0162007-05-24 13:03:52 +03003177 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003178 r = register_cpu_notifier(&kvm_cpu_notifier);
3179 if (r)
3180 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 register_reboot_notifier(&kvm_reboot_notifier);
3182
Avi Kivity59ae6c62007-02-12 00:54:48 -08003183 r = sysdev_class_register(&kvm_sysdev_class);
3184 if (r)
3185 goto out_free_2;
3186
3187 r = sysdev_register(&kvm_sysdev);
3188 if (r)
3189 goto out_free_3;
3190
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 kvm_chardev_ops.owner = module;
3192
3193 r = misc_register(&kvm_dev);
3194 if (r) {
3195 printk (KERN_ERR "kvm: misc device register failed\n");
3196 goto out_free;
3197 }
3198
3199 return r;
3200
3201out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003202 sysdev_unregister(&kvm_sysdev);
3203out_free_3:
3204 sysdev_class_unregister(&kvm_sysdev_class);
3205out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003207 unregister_cpu_notifier(&kvm_cpu_notifier);
3208out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003209 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003211out:
3212 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 return r;
3214}
3215
3216void kvm_exit_arch(void)
3217{
3218 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003219 sysdev_unregister(&kvm_sysdev);
3220 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003222 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003223 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003225 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226}
3227
3228static __init int kvm_init(void)
3229{
3230 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003231 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232
Avi Kivityb5a33a72007-04-15 16:31:09 +03003233 r = kvm_mmu_module_init();
3234 if (r)
3235 goto out4;
3236
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 kvm_init_debug();
3238
Michael Riepebf591b22006-12-22 01:05:36 -08003239 kvm_init_msr_list();
3240
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3242 r = -ENOMEM;
3243 goto out;
3244 }
3245
3246 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3247 memset(__va(bad_page_address), 0, PAGE_SIZE);
3248
Avi Kivity58e690e2007-02-26 16:29:43 +02003249 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250
3251out:
3252 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003253 kvm_mmu_module_exit();
3254out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 return r;
3256}
3257
3258static __exit void kvm_exit(void)
3259{
3260 kvm_exit_debug();
3261 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003262 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263}
3264
3265module_init(kvm_init)
3266module_exit(kvm_exit)
3267
3268EXPORT_SYMBOL_GPL(kvm_init_arch);
3269EXPORT_SYMBOL_GPL(kvm_exit_arch);