blob: 5d8febe580de99249e029a1fa5ff31ce32d1907e [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))
Avi Kivity6aa8b732006-12-10 02:21:36 -080089#define LMSW_GUEST_MASK 0x0eULL
90#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
91#define CR8_RESEVED_BITS (~0x0fULL)
92#define EFER_RESERVED_BITS 0xfffffffffffff2fe
93
Avi Kivity05b3e0c2006-12-13 00:33:45 -080094#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080095// LDT or TSS descriptor in the GDT. 16 bytes.
96struct segment_descriptor_64 {
97 struct segment_descriptor s;
98 u32 base_higher;
99 u32 pad_zero;
100};
101
102#endif
103
Avi Kivitybccf2152007-02-21 18:04:26 +0200104static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
105 unsigned long arg);
106
Avi Kivity6aa8b732006-12-10 02:21:36 -0800107unsigned long segment_base(u16 selector)
108{
109 struct descriptor_table gdt;
110 struct segment_descriptor *d;
111 unsigned long table_base;
112 typedef unsigned long ul;
113 unsigned long v;
114
115 if (selector == 0)
116 return 0;
117
118 asm ("sgdt %0" : "=m"(gdt));
119 table_base = gdt.base;
120
121 if (selector & 4) { /* from ldt */
122 u16 ldt_selector;
123
124 asm ("sldt %0" : "=g"(ldt_selector));
125 table_base = segment_base(ldt_selector);
126 }
127 d = (struct segment_descriptor *)(table_base + (selector & ~7));
128 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800129#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130 if (d->system == 0
131 && (d->type == 2 || d->type == 9 || d->type == 11))
132 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
133#endif
134 return v;
135}
136EXPORT_SYMBOL_GPL(segment_base);
137
James Morris5aacf0c2006-12-22 01:04:55 -0800138static inline int valid_vcpu(int n)
139{
140 return likely(n >= 0 && n < KVM_MAX_VCPUS);
141}
142
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200143int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
144 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145{
146 unsigned char *host_buf = dest;
147 unsigned long req_size = size;
148
149 while (size) {
150 hpa_t paddr;
151 unsigned now;
152 unsigned offset;
153 hva_t guest_buf;
154
155 paddr = gva_to_hpa(vcpu, addr);
156
157 if (is_error_hpa(paddr))
158 break;
159
160 guest_buf = (hva_t)kmap_atomic(
161 pfn_to_page(paddr >> PAGE_SHIFT),
162 KM_USER0);
163 offset = addr & ~PAGE_MASK;
164 guest_buf |= offset;
165 now = min(size, PAGE_SIZE - offset);
166 memcpy(host_buf, (void*)guest_buf, now);
167 host_buf += now;
168 addr += now;
169 size -= now;
170 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
171 }
172 return req_size - size;
173}
174EXPORT_SYMBOL_GPL(kvm_read_guest);
175
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200176int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
177 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178{
179 unsigned char *host_buf = data;
180 unsigned long req_size = size;
181
182 while (size) {
183 hpa_t paddr;
184 unsigned now;
185 unsigned offset;
186 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200187 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
189 paddr = gva_to_hpa(vcpu, addr);
190
191 if (is_error_hpa(paddr))
192 break;
193
Uri Lublinab51a432007-02-21 18:25:21 +0200194 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
195 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 guest_buf = (hva_t)kmap_atomic(
197 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
198 offset = addr & ~PAGE_MASK;
199 guest_buf |= offset;
200 now = min(size, PAGE_SIZE - offset);
201 memcpy((void*)guest_buf, host_buf, now);
202 host_buf += now;
203 addr += now;
204 size -= now;
205 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
206 }
207 return req_size - size;
208}
209EXPORT_SYMBOL_GPL(kvm_write_guest);
210
Avi Kivity7702fd12007-06-14 16:27:40 +0300211void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
212{
213 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
214 return;
215
216 vcpu->guest_fpu_loaded = 1;
217 fx_save(vcpu->host_fx_image);
218 fx_restore(vcpu->guest_fx_image);
219}
220EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
221
222void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
223{
224 if (!vcpu->guest_fpu_loaded)
225 return;
226
227 vcpu->guest_fpu_loaded = 0;
228 fx_save(vcpu->guest_fx_image);
229 fx_restore(vcpu->host_fx_image);
230}
231EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
232
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233/*
234 * Switches to specified vcpu, until a matching vcpu_put()
235 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200236static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237{
Avi Kivitybccf2152007-02-21 18:04:26 +0200238 mutex_lock(&vcpu->mutex);
239 kvm_arch_ops->vcpu_load(vcpu);
240}
241
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242static void vcpu_put(struct kvm_vcpu *vcpu)
243{
244 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 mutex_unlock(&vcpu->mutex);
246}
247
Avi Kivityd9e368d2007-06-07 19:18:30 +0300248static void ack_flush(void *_completed)
249{
250 atomic_t *completed = _completed;
251
252 atomic_inc(completed);
253}
254
255void kvm_flush_remote_tlbs(struct kvm *kvm)
256{
257 int i, cpu, needed;
258 cpumask_t cpus;
259 struct kvm_vcpu *vcpu;
260 atomic_t completed;
261
262 atomic_set(&completed, 0);
263 cpus_clear(cpus);
264 needed = 0;
265 for (i = 0; i < kvm->nvcpus; ++i) {
266 vcpu = &kvm->vcpus[i];
267 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
268 continue;
269 cpu = vcpu->cpu;
270 if (cpu != -1 && cpu != raw_smp_processor_id())
271 if (!cpu_isset(cpu, cpus)) {
272 cpu_set(cpu, cpus);
273 ++needed;
274 }
275 }
276
277 /*
278 * We really want smp_call_function_mask() here. But that's not
279 * available, so ipi all cpus in parallel and wait for them
280 * to complete.
281 */
282 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
283 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
284 while (atomic_read(&completed) != needed) {
285 cpu_relax();
286 barrier();
287 }
288}
289
Avi Kivityf17abe92007-02-21 19:28:04 +0200290static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291{
292 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
293 int i;
294
295 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200296 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297
Eddie Dong74906342007-06-19 18:05:03 +0300298 kvm_io_bus_init(&kvm->pio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299 spin_lock_init(&kvm->lock);
300 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400301 kvm_io_bus_init(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
303 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
304
305 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800306 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800307 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 vcpu->mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309 }
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000310 spin_lock(&kvm_lock);
311 list_add(&kvm->vm_list, &vm_list);
312 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200313 return kvm;
314}
315
316static int kvm_dev_open(struct inode *inode, struct file *filp)
317{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318 return 0;
319}
320
321/*
322 * Free any memory in @free but not in @dont.
323 */
324static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
325 struct kvm_memory_slot *dont)
326{
327 int i;
328
329 if (!dont || free->phys_mem != dont->phys_mem)
330 if (free->phys_mem) {
331 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800332 if (free->phys_mem[i])
333 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334 vfree(free->phys_mem);
335 }
336
337 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
338 vfree(free->dirty_bitmap);
339
Al Viro8b6d44c2007-02-09 16:38:40 +0000340 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000342 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343}
344
345static void kvm_free_physmem(struct kvm *kvm)
346{
347 int i;
348
349 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000350 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351}
352
Avi Kivity039576c2007-03-20 12:46:50 +0200353static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
354{
355 int i;
356
357 for (i = 0; i < 2; ++i)
358 if (vcpu->pio.guest_pages[i]) {
359 __free_page(vcpu->pio.guest_pages[i]);
360 vcpu->pio.guest_pages[i] = NULL;
361 }
362}
363
Avi Kivity7b53aa52007-06-05 12:17:03 +0300364static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
365{
366 if (!vcpu->vmcs)
367 return;
368
369 vcpu_load(vcpu);
370 kvm_mmu_unload(vcpu);
371 vcpu_put(vcpu);
372}
373
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
375{
Avi Kivitybccf2152007-02-21 18:04:26 +0200376 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800377 return;
378
Avi Kivitybccf2152007-02-21 18:04:26 +0200379 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800381 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800382 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200383 free_page((unsigned long)vcpu->run);
384 vcpu->run = NULL;
Avi Kivity039576c2007-03-20 12:46:50 +0200385 free_page((unsigned long)vcpu->pio_data);
386 vcpu->pio_data = NULL;
387 free_pio_guest_pages(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388}
389
390static void kvm_free_vcpus(struct kvm *kvm)
391{
392 unsigned int i;
393
Avi Kivity7b53aa52007-06-05 12:17:03 +0300394 /*
395 * Unpin any mmu pages first.
396 */
397 for (i = 0; i < KVM_MAX_VCPUS; ++i)
398 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399 for (i = 0; i < KVM_MAX_VCPUS; ++i)
400 kvm_free_vcpu(&kvm->vcpus[i]);
401}
402
403static int kvm_dev_release(struct inode *inode, struct file *filp)
404{
Avi Kivityf17abe92007-02-21 19:28:04 +0200405 return 0;
406}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407
Avi Kivityf17abe92007-02-21 19:28:04 +0200408static void kvm_destroy_vm(struct kvm *kvm)
409{
Avi Kivity133de902007-02-12 00:54:44 -0800410 spin_lock(&kvm_lock);
411 list_del(&kvm->vm_list);
412 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300413 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400414 kvm_io_bus_destroy(&kvm->mmio_bus);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 kvm_free_vcpus(kvm);
416 kvm_free_physmem(kvm);
417 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200418}
419
420static int kvm_vm_release(struct inode *inode, struct file *filp)
421{
422 struct kvm *kvm = filp->private_data;
423
424 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 return 0;
426}
427
428static void inject_gp(struct kvm_vcpu *vcpu)
429{
430 kvm_arch_ops->inject_gp(vcpu, 0);
431}
432
Avi Kivity1342d352007-01-05 16:36:39 -0800433/*
434 * Load the pae pdptrs. Return true is they are all valid.
435 */
436static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437{
438 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800439 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 int i;
441 u64 pdpte;
442 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800443 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300444 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445
446 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300447 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
448 /* FIXME: !page - emulate? 0xff? */
449 pdpt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450
Avi Kivity1342d352007-01-05 16:36:39 -0800451 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 for (i = 0; i < 4; ++i) {
453 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800454 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
455 ret = 0;
456 goto out;
457 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 }
459
Avi Kivity1342d352007-01-05 16:36:39 -0800460 for (i = 0; i < 4; ++i)
461 vcpu->pdptrs[i] = pdpt[offset + i];
462
463out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 kunmap_atomic(pdpt, KM_USER0);
465 spin_unlock(&vcpu->kvm->lock);
466
Avi Kivity1342d352007-01-05 16:36:39 -0800467 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468}
469
470void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
471{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000472 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
474 cr0, vcpu->cr0);
475 inject_gp(vcpu);
476 return;
477 }
478
Rusty Russell707d92fa2007-07-17 23:19:08 +1000479 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
481 inject_gp(vcpu);
482 return;
483 }
484
Rusty Russell707d92fa2007-07-17 23:19:08 +1000485 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
487 "and a clear PE flag\n");
488 inject_gp(vcpu);
489 return;
490 }
491
Rusty Russell707d92fa2007-07-17 23:19:08 +1000492 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800493#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494 if ((vcpu->shadow_efer & EFER_LME)) {
495 int cs_db, cs_l;
496
497 if (!is_pae(vcpu)) {
498 printk(KERN_DEBUG "set_cr0: #GP, start paging "
499 "in long mode while PAE is disabled\n");
500 inject_gp(vcpu);
501 return;
502 }
503 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
504 if (cs_l) {
505 printk(KERN_DEBUG "set_cr0: #GP, start paging "
506 "in long mode while CS.L == 1\n");
507 inject_gp(vcpu);
508 return;
509
510 }
511 } else
512#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800513 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
515 "reserved bits\n");
516 inject_gp(vcpu);
517 return;
518 }
519
520 }
521
522 kvm_arch_ops->set_cr0(vcpu, cr0);
523 vcpu->cr0 = cr0;
524
525 spin_lock(&vcpu->kvm->lock);
526 kvm_mmu_reset_context(vcpu);
527 spin_unlock(&vcpu->kvm->lock);
528 return;
529}
530EXPORT_SYMBOL_GPL(set_cr0);
531
532void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
533{
534 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
535}
536EXPORT_SYMBOL_GPL(lmsw);
537
538void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
539{
540 if (cr4 & CR4_RESEVED_BITS) {
541 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
542 inject_gp(vcpu);
543 return;
544 }
545
Avi Kivitya9058ec2006-12-29 16:49:37 -0800546 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 if (!(cr4 & CR4_PAE_MASK)) {
548 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
549 "in long mode\n");
550 inject_gp(vcpu);
551 return;
552 }
553 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800554 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
556 inject_gp(vcpu);
557 }
558
559 if (cr4 & CR4_VMXE_MASK) {
560 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
561 inject_gp(vcpu);
562 return;
563 }
564 kvm_arch_ops->set_cr4(vcpu, cr4);
565 spin_lock(&vcpu->kvm->lock);
566 kvm_mmu_reset_context(vcpu);
567 spin_unlock(&vcpu->kvm->lock);
568}
569EXPORT_SYMBOL_GPL(set_cr4);
570
571void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
572{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800573 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200574 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
576 inject_gp(vcpu);
577 return;
578 }
579 } else {
580 if (cr3 & CR3_RESEVED_BITS) {
581 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
582 inject_gp(vcpu);
583 return;
584 }
585 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800586 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
588 "reserved bits\n");
589 inject_gp(vcpu);
590 return;
591 }
592 }
593
594 vcpu->cr3 = cr3;
595 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800596 /*
597 * Does the new cr3 value map to physical memory? (Note, we
598 * catch an invalid cr3 even in real-mode, because it would
599 * cause trouble later on when we turn on paging anyway.)
600 *
601 * A real CPU would silently accept an invalid cr3 and would
602 * attempt to use it - with largely undefined (and often hard
603 * to debug) behavior on the guest side.
604 */
605 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
606 inject_gp(vcpu);
607 else
608 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609 spin_unlock(&vcpu->kvm->lock);
610}
611EXPORT_SYMBOL_GPL(set_cr3);
612
613void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
614{
615 if ( cr8 & CR8_RESEVED_BITS) {
616 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
617 inject_gp(vcpu);
618 return;
619 }
620 vcpu->cr8 = cr8;
621}
622EXPORT_SYMBOL_GPL(set_cr8);
623
624void fx_init(struct kvm_vcpu *vcpu)
625{
626 struct __attribute__ ((__packed__)) fx_image_s {
627 u16 control; //fcw
628 u16 status; //fsw
629 u16 tag; // ftw
630 u16 opcode; //fop
631 u64 ip; // fpu ip
632 u64 operand;// fpu dp
633 u32 mxcsr;
634 u32 mxcsr_mask;
635
636 } *fx_image;
637
638 fx_save(vcpu->host_fx_image);
639 fpu_init();
640 fx_save(vcpu->guest_fx_image);
641 fx_restore(vcpu->host_fx_image);
642
643 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
644 fx_image->mxcsr = 0x1f80;
645 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
646 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
647}
648EXPORT_SYMBOL_GPL(fx_init);
649
650/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 * Allocate some memory and give it an address in the guest physical address
652 * space.
653 *
654 * Discontiguous memory is allowed, mostly for framebuffers.
655 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200656static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
657 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658{
659 int r;
660 gfn_t base_gfn;
661 unsigned long npages;
662 unsigned long i;
663 struct kvm_memory_slot *memslot;
664 struct kvm_memory_slot old, new;
665 int memory_config_version;
666
667 r = -EINVAL;
668 /* General sanity checks */
669 if (mem->memory_size & (PAGE_SIZE - 1))
670 goto out;
671 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
672 goto out;
673 if (mem->slot >= KVM_MEMORY_SLOTS)
674 goto out;
675 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
676 goto out;
677
678 memslot = &kvm->memslots[mem->slot];
679 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
680 npages = mem->memory_size >> PAGE_SHIFT;
681
682 if (!npages)
683 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
684
685raced:
686 spin_lock(&kvm->lock);
687
688 memory_config_version = kvm->memory_config_version;
689 new = old = *memslot;
690
691 new.base_gfn = base_gfn;
692 new.npages = npages;
693 new.flags = mem->flags;
694
695 /* Disallow changing a memory slot's size. */
696 r = -EINVAL;
697 if (npages && old.npages && npages != old.npages)
698 goto out_unlock;
699
700 /* Check for overlaps */
701 r = -EEXIST;
702 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
703 struct kvm_memory_slot *s = &kvm->memslots[i];
704
705 if (s == memslot)
706 continue;
707 if (!((base_gfn + npages <= s->base_gfn) ||
708 (base_gfn >= s->base_gfn + s->npages)))
709 goto out_unlock;
710 }
711 /*
712 * Do memory allocations outside lock. memory_config_version will
713 * detect any races.
714 */
715 spin_unlock(&kvm->lock);
716
717 /* Deallocate if slot is being removed */
718 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000719 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720
721 /* Free page dirty bitmap if unneeded */
722 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000723 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724
725 r = -ENOMEM;
726
727 /* Allocate if a slot is being created */
728 if (npages && !new.phys_mem) {
729 new.phys_mem = vmalloc(npages * sizeof(struct page *));
730
731 if (!new.phys_mem)
732 goto out_free;
733
734 memset(new.phys_mem, 0, npages * sizeof(struct page *));
735 for (i = 0; i < npages; ++i) {
736 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
737 | __GFP_ZERO);
738 if (!new.phys_mem[i])
739 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200740 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 }
742 }
743
744 /* Allocate page dirty bitmap if needed */
745 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
746 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
747
748 new.dirty_bitmap = vmalloc(dirty_bytes);
749 if (!new.dirty_bitmap)
750 goto out_free;
751 memset(new.dirty_bitmap, 0, dirty_bytes);
752 }
753
754 spin_lock(&kvm->lock);
755
756 if (memory_config_version != kvm->memory_config_version) {
757 spin_unlock(&kvm->lock);
758 kvm_free_physmem_slot(&new, &old);
759 goto raced;
760 }
761
762 r = -EAGAIN;
763 if (kvm->busy)
764 goto out_unlock;
765
766 if (mem->slot >= kvm->nmemslots)
767 kvm->nmemslots = mem->slot + 1;
768
769 *memslot = new;
770 ++kvm->memory_config_version;
771
Avi Kivity90cb0522007-07-17 13:04:56 +0300772 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
773 kvm_flush_remote_tlbs(kvm);
774
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 spin_unlock(&kvm->lock);
776
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 kvm_free_physmem_slot(&old, &new);
778 return 0;
779
780out_unlock:
781 spin_unlock(&kvm->lock);
782out_free:
783 kvm_free_physmem_slot(&new, &old);
784out:
785 return r;
786}
787
788/*
789 * Get (and clear) the dirty memory log for a memory slot.
790 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200791static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
792 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793{
794 struct kvm_memory_slot *memslot;
795 int r, i;
796 int n;
797 unsigned long any = 0;
798
799 spin_lock(&kvm->lock);
800
801 /*
802 * Prevent changes to guest memory configuration even while the lock
803 * is not taken.
804 */
805 ++kvm->busy;
806 spin_unlock(&kvm->lock);
807 r = -EINVAL;
808 if (log->slot >= KVM_MEMORY_SLOTS)
809 goto out;
810
811 memslot = &kvm->memslots[log->slot];
812 r = -ENOENT;
813 if (!memslot->dirty_bitmap)
814 goto out;
815
Uri Lublincd1a4a92007-02-22 16:43:09 +0200816 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817
Uri Lublincd1a4a92007-02-22 16:43:09 +0200818 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 any = memslot->dirty_bitmap[i];
820
821 r = -EFAULT;
822 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
823 goto out;
824
Avi Kivity90cb0522007-07-17 13:04:56 +0300825 spin_lock(&kvm->lock);
826 kvm_mmu_slot_remove_write_access(kvm, log->slot);
827 kvm_flush_remote_tlbs(kvm);
828 memset(memslot->dirty_bitmap, 0, n);
829 spin_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830
831 r = 0;
832
833out:
834 spin_lock(&kvm->lock);
835 --kvm->busy;
836 spin_unlock(&kvm->lock);
837 return r;
838}
839
Avi Kivitye8207542007-03-30 16:54:30 +0300840/*
841 * Set a new alias region. Aliases map a portion of physical memory into
842 * another portion. This is useful for memory windows, for example the PC
843 * VGA region.
844 */
845static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
846 struct kvm_memory_alias *alias)
847{
848 int r, n;
849 struct kvm_mem_alias *p;
850
851 r = -EINVAL;
852 /* General sanity checks */
853 if (alias->memory_size & (PAGE_SIZE - 1))
854 goto out;
855 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
856 goto out;
857 if (alias->slot >= KVM_ALIAS_SLOTS)
858 goto out;
859 if (alias->guest_phys_addr + alias->memory_size
860 < alias->guest_phys_addr)
861 goto out;
862 if (alias->target_phys_addr + alias->memory_size
863 < alias->target_phys_addr)
864 goto out;
865
866 spin_lock(&kvm->lock);
867
868 p = &kvm->aliases[alias->slot];
869 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
870 p->npages = alias->memory_size >> PAGE_SHIFT;
871 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
872
873 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
874 if (kvm->aliases[n - 1].npages)
875 break;
876 kvm->naliases = n;
877
Avi Kivity90cb0522007-07-17 13:04:56 +0300878 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300879
Avi Kivitye8207542007-03-30 16:54:30 +0300880 spin_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300881
882 return 0;
883
884out:
885 return r;
886}
887
888static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
889{
890 int i;
891 struct kvm_mem_alias *alias;
892
893 for (i = 0; i < kvm->naliases; ++i) {
894 alias = &kvm->aliases[i];
895 if (gfn >= alias->base_gfn
896 && gfn < alias->base_gfn + alias->npages)
897 return alias->target_gfn + gfn - alias->base_gfn;
898 }
899 return gfn;
900}
901
902static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903{
904 int i;
905
906 for (i = 0; i < kvm->nmemslots; ++i) {
907 struct kvm_memory_slot *memslot = &kvm->memslots[i];
908
909 if (gfn >= memslot->base_gfn
910 && gfn < memslot->base_gfn + memslot->npages)
911 return memslot;
912 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000913 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914}
Avi Kivitye8207542007-03-30 16:54:30 +0300915
916struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
917{
918 gfn = unalias_gfn(kvm, gfn);
919 return __gfn_to_memslot(kvm, gfn);
920}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921
Avi Kivity954bbbc2007-03-30 14:02:32 +0300922struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
923{
924 struct kvm_memory_slot *slot;
925
Avi Kivitye8207542007-03-30 16:54:30 +0300926 gfn = unalias_gfn(kvm, gfn);
927 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300928 if (!slot)
929 return NULL;
930 return slot->phys_mem[gfn - slot->base_gfn];
931}
932EXPORT_SYMBOL_GPL(gfn_to_page);
933
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
935{
936 int i;
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300937 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 unsigned long rel_gfn;
939
940 for (i = 0; i < kvm->nmemslots; ++i) {
941 memslot = &kvm->memslots[i];
942
943 if (gfn >= memslot->base_gfn
944 && gfn < memslot->base_gfn + memslot->npages) {
945
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300946 if (!memslot->dirty_bitmap)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 return;
948
949 rel_gfn = gfn - memslot->base_gfn;
950
951 /* avoid RMW */
952 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
953 set_bit(rel_gfn, memslot->dirty_bitmap);
954 return;
955 }
956 }
957}
958
959static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300960 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961 unsigned int bytes,
962 struct x86_emulate_ctxt *ctxt)
963{
964 struct kvm_vcpu *vcpu = ctxt->vcpu;
965 void *data = val;
966
967 while (bytes) {
968 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
969 unsigned offset = addr & (PAGE_SIZE-1);
970 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
971 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300972 struct page *page;
973 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974
975 if (gpa == UNMAPPED_GVA)
976 return X86EMUL_PROPAGATE_FAULT;
977 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300978 page = gfn_to_page(vcpu->kvm, pfn);
979 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300981 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Avi Kivity954bbbc2007-03-30 14:02:32 +0300983 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Avi Kivity954bbbc2007-03-30 14:02:32 +0300985 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
987 bytes -= tocopy;
988 data += tocopy;
989 addr += tocopy;
990 }
991
992 return X86EMUL_CONTINUE;
993}
994
995static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300996 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 unsigned int bytes,
998 struct x86_emulate_ctxt *ctxt)
999{
1000 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1001 addr, bytes);
1002 return X86EMUL_UNHANDLEABLE;
1003}
1004
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001005static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1006 gpa_t addr)
1007{
1008 /*
1009 * Note that its important to have this wrapper function because
1010 * in the very near future we will be checking for MMIOs against
1011 * the LAPIC as well as the general MMIO bus
1012 */
1013 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1014}
1015
Eddie Dong74906342007-06-19 18:05:03 +03001016static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1017 gpa_t addr)
1018{
1019 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1020}
1021
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001023 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 unsigned int bytes,
1025 struct x86_emulate_ctxt *ctxt)
1026{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001027 struct kvm_vcpu *vcpu = ctxt->vcpu;
1028 struct kvm_io_device *mmio_dev;
1029 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
1031 if (vcpu->mmio_read_completed) {
1032 memcpy(val, vcpu->mmio_data, bytes);
1033 vcpu->mmio_read_completed = 0;
1034 return X86EMUL_CONTINUE;
1035 } else if (emulator_read_std(addr, val, bytes, ctxt)
1036 == X86EMUL_CONTINUE)
1037 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001038
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001039 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1040 if (gpa == UNMAPPED_GVA)
1041 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001043 /*
1044 * Is this MMIO handled locally?
1045 */
1046 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1047 if (mmio_dev) {
1048 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1049 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001051
1052 vcpu->mmio_needed = 1;
1053 vcpu->mmio_phys_addr = gpa;
1054 vcpu->mmio_size = bytes;
1055 vcpu->mmio_is_write = 0;
1056
1057 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058}
1059
Avi Kivityda4a00f2007-01-05 16:36:44 -08001060static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001061 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001062{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001063 struct page *page;
1064 void *virt;
Avi Kivity09072da2007-05-01 14:16:52 +03001065 unsigned offset = offset_in_page(gpa);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001066
1067 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1068 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001069 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1070 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001071 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001072 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001073 virt = kmap_atomic(page, KM_USER0);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001074 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1075 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001076 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001077 return 1;
1078}
1079
Avi Kivityb0fcd902007-07-22 18:48:54 +03001080static int emulator_write_emulated_onepage(unsigned long addr,
1081 const void *val,
1082 unsigned int bytes,
1083 struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001085 struct kvm_vcpu *vcpu = ctxt->vcpu;
1086 struct kvm_io_device *mmio_dev;
1087 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088
Avi Kivityc9047f52007-04-17 10:53:22 +03001089 if (gpa == UNMAPPED_GVA) {
1090 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001092 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093
Avi Kivityda4a00f2007-01-05 16:36:44 -08001094 if (emulator_write_phys(vcpu, gpa, val, bytes))
1095 return X86EMUL_CONTINUE;
1096
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001097 /*
1098 * Is this MMIO handled locally?
1099 */
1100 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1101 if (mmio_dev) {
1102 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1103 return X86EMUL_CONTINUE;
1104 }
1105
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 vcpu->mmio_needed = 1;
1107 vcpu->mmio_phys_addr = gpa;
1108 vcpu->mmio_size = bytes;
1109 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001110 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111
1112 return X86EMUL_CONTINUE;
1113}
1114
Avi Kivityb0fcd902007-07-22 18:48:54 +03001115static int emulator_write_emulated(unsigned long addr,
1116 const void *val,
1117 unsigned int bytes,
1118 struct x86_emulate_ctxt *ctxt)
1119{
1120 /* Crossing a page boundary? */
1121 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1122 int rc, now;
1123
1124 now = -addr & ~PAGE_MASK;
1125 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1126 if (rc != X86EMUL_CONTINUE)
1127 return rc;
1128 addr += now;
1129 val += now;
1130 bytes -= now;
1131 }
1132 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1133}
1134
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001136 const void *old,
1137 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 unsigned int bytes,
1139 struct x86_emulate_ctxt *ctxt)
1140{
1141 static int reported;
1142
1143 if (!reported) {
1144 reported = 1;
1145 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1146 }
1147 return emulator_write_emulated(addr, new, bytes, ctxt);
1148}
1149
1150static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1151{
1152 return kvm_arch_ops->get_segment_base(vcpu, seg);
1153}
1154
1155int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1156{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 return X86EMUL_CONTINUE;
1158}
1159
1160int emulate_clts(struct kvm_vcpu *vcpu)
1161{
Avi Kivity399badf2007-01-05 16:36:38 -08001162 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163
Rusty Russell707d92fa2007-07-17 23:19:08 +10001164 cr0 = vcpu->cr0 & ~X86_CR0_TS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 kvm_arch_ops->set_cr0(vcpu, cr0);
1166 return X86EMUL_CONTINUE;
1167}
1168
1169int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1170{
1171 struct kvm_vcpu *vcpu = ctxt->vcpu;
1172
1173 switch (dr) {
1174 case 0 ... 3:
1175 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1176 return X86EMUL_CONTINUE;
1177 default:
1178 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1179 __FUNCTION__, dr);
1180 return X86EMUL_UNHANDLEABLE;
1181 }
1182}
1183
1184int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1185{
1186 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1187 int exception;
1188
1189 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1190 if (exception) {
1191 /* FIXME: better handling */
1192 return X86EMUL_UNHANDLEABLE;
1193 }
1194 return X86EMUL_CONTINUE;
1195}
1196
1197static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1198{
1199 static int reported;
1200 u8 opcodes[4];
1201 unsigned long rip = ctxt->vcpu->rip;
1202 unsigned long rip_linear;
1203
1204 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1205
1206 if (reported)
1207 return;
1208
1209 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1210
1211 printk(KERN_ERR "emulation failed but !mmio_needed?"
1212 " rip %lx %02x %02x %02x %02x\n",
1213 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1214 reported = 1;
1215}
1216
1217struct x86_emulate_ops emulate_ops = {
1218 .read_std = emulator_read_std,
1219 .write_std = emulator_write_std,
1220 .read_emulated = emulator_read_emulated,
1221 .write_emulated = emulator_write_emulated,
1222 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1223};
1224
1225int emulate_instruction(struct kvm_vcpu *vcpu,
1226 struct kvm_run *run,
1227 unsigned long cr2,
1228 u16 error_code)
1229{
1230 struct x86_emulate_ctxt emulate_ctxt;
1231 int r;
1232 int cs_db, cs_l;
1233
Avi Kivitye7df56e2007-03-14 15:54:54 +02001234 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 kvm_arch_ops->cache_regs(vcpu);
1236
1237 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1238
1239 emulate_ctxt.vcpu = vcpu;
1240 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1241 emulate_ctxt.cr2 = cr2;
1242 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1243 ? X86EMUL_MODE_REAL : cs_l
1244 ? X86EMUL_MODE_PROT64 : cs_db
1245 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1246
1247 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1248 emulate_ctxt.cs_base = 0;
1249 emulate_ctxt.ds_base = 0;
1250 emulate_ctxt.es_base = 0;
1251 emulate_ctxt.ss_base = 0;
1252 } else {
1253 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1254 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1255 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1256 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1257 }
1258
1259 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1260 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1261
1262 vcpu->mmio_is_write = 0;
1263 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1264
1265 if ((r || vcpu->mmio_is_write) && run) {
1266 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1267 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1268 run->mmio.len = vcpu->mmio_size;
1269 run->mmio.is_write = vcpu->mmio_is_write;
1270 }
1271
1272 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001273 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1274 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 if (!vcpu->mmio_needed) {
1276 report_emulation_failure(&emulate_ctxt);
1277 return EMULATE_FAIL;
1278 }
1279 return EMULATE_DO_MMIO;
1280 }
1281
1282 kvm_arch_ops->decache_regs(vcpu);
1283 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1284
Avi Kivity02c83202007-04-29 15:02:17 +03001285 if (vcpu->mmio_is_write) {
1286 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001288 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
1290 return EMULATE_DONE;
1291}
1292EXPORT_SYMBOL_GPL(emulate_instruction);
1293
Avi Kivityd3bef152007-06-05 15:53:05 +03001294int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1295{
1296 if (vcpu->irq_summary)
1297 return 1;
1298
1299 vcpu->run->exit_reason = KVM_EXIT_HLT;
1300 ++vcpu->stat.halt_exits;
1301 return 0;
1302}
1303EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1304
Avi Kivity270fd9b2007-02-19 14:37:47 +02001305int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1306{
1307 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1308
Dor Laor9b22bf52007-02-19 16:44:49 +02001309 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001310 ret = -KVM_EINVAL;
1311#ifdef CONFIG_X86_64
1312 if (is_long_mode(vcpu)) {
1313 nr = vcpu->regs[VCPU_REGS_RAX];
1314 a0 = vcpu->regs[VCPU_REGS_RDI];
1315 a1 = vcpu->regs[VCPU_REGS_RSI];
1316 a2 = vcpu->regs[VCPU_REGS_RDX];
1317 a3 = vcpu->regs[VCPU_REGS_RCX];
1318 a4 = vcpu->regs[VCPU_REGS_R8];
1319 a5 = vcpu->regs[VCPU_REGS_R9];
1320 } else
1321#endif
1322 {
1323 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1324 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1325 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1326 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1327 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1328 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1329 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1330 }
1331 switch (nr) {
1332 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001333 run->hypercall.args[0] = a0;
1334 run->hypercall.args[1] = a1;
1335 run->hypercall.args[2] = a2;
1336 run->hypercall.args[3] = a3;
1337 run->hypercall.args[4] = a4;
1338 run->hypercall.args[5] = a5;
1339 run->hypercall.ret = ret;
1340 run->hypercall.longmode = is_long_mode(vcpu);
1341 kvm_arch_ops->decache_regs(vcpu);
1342 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001343 }
1344 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001345 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001346 return 1;
1347}
1348EXPORT_SYMBOL_GPL(kvm_hypercall);
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1351{
1352 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1353}
1354
1355void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1356{
1357 struct descriptor_table dt = { limit, base };
1358
1359 kvm_arch_ops->set_gdt(vcpu, &dt);
1360}
1361
1362void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1363{
1364 struct descriptor_table dt = { limit, base };
1365
1366 kvm_arch_ops->set_idt(vcpu, &dt);
1367}
1368
1369void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1370 unsigned long *rflags)
1371{
1372 lmsw(vcpu, msw);
1373 *rflags = kvm_arch_ops->get_rflags(vcpu);
1374}
1375
1376unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1377{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001378 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 switch (cr) {
1380 case 0:
1381 return vcpu->cr0;
1382 case 2:
1383 return vcpu->cr2;
1384 case 3:
1385 return vcpu->cr3;
1386 case 4:
1387 return vcpu->cr4;
1388 default:
1389 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1390 return 0;
1391 }
1392}
1393
1394void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1395 unsigned long *rflags)
1396{
1397 switch (cr) {
1398 case 0:
1399 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1400 *rflags = kvm_arch_ops->get_rflags(vcpu);
1401 break;
1402 case 2:
1403 vcpu->cr2 = val;
1404 break;
1405 case 3:
1406 set_cr3(vcpu, val);
1407 break;
1408 case 4:
1409 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1410 break;
1411 default:
1412 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1413 }
1414}
1415
Ingo Molnar102d8322007-02-19 14:37:47 +02001416/*
1417 * Register the para guest with the host:
1418 */
1419static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1420{
1421 struct kvm_vcpu_para_state *para_state;
1422 hpa_t para_state_hpa, hypercall_hpa;
1423 struct page *para_state_page;
1424 unsigned char *hypercall;
1425 gpa_t hypercall_gpa;
1426
1427 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1428 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1429
1430 /*
1431 * Needs to be page aligned:
1432 */
1433 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1434 goto err_gp;
1435
1436 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1437 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1438 if (is_error_hpa(para_state_hpa))
1439 goto err_gp;
1440
Uri Lublinab51a432007-02-21 18:25:21 +02001441 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001442 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1443 para_state = kmap_atomic(para_state_page, KM_USER0);
1444
1445 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1446 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1447
1448 para_state->host_version = KVM_PARA_API_VERSION;
1449 /*
1450 * We cannot support guests that try to register themselves
1451 * with a newer API version than the host supports:
1452 */
1453 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1454 para_state->ret = -KVM_EINVAL;
1455 goto err_kunmap_skip;
1456 }
1457
1458 hypercall_gpa = para_state->hypercall_gpa;
1459 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1460 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1461 if (is_error_hpa(hypercall_hpa)) {
1462 para_state->ret = -KVM_EINVAL;
1463 goto err_kunmap_skip;
1464 }
1465
1466 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1467 vcpu->para_state_page = para_state_page;
1468 vcpu->para_state_gpa = para_state_gpa;
1469 vcpu->hypercall_gpa = hypercall_gpa;
1470
Uri Lublinab51a432007-02-21 18:25:21 +02001471 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001472 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1473 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1474 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1475 kunmap_atomic(hypercall, KM_USER1);
1476
1477 para_state->ret = 0;
1478err_kunmap_skip:
1479 kunmap_atomic(para_state, KM_USER0);
1480 return 0;
1481err_gp:
1482 return 1;
1483}
1484
Avi Kivity3bab1f52006-12-29 16:49:48 -08001485int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1486{
1487 u64 data;
1488
1489 switch (msr) {
1490 case 0xc0010010: /* SYSCFG */
1491 case 0xc0010015: /* HWCR */
1492 case MSR_IA32_PLATFORM_ID:
1493 case MSR_IA32_P5_MC_ADDR:
1494 case MSR_IA32_P5_MC_TYPE:
1495 case MSR_IA32_MC0_CTL:
1496 case MSR_IA32_MCG_STATUS:
1497 case MSR_IA32_MCG_CAP:
1498 case MSR_IA32_MC0_MISC:
1499 case MSR_IA32_MC0_MISC+4:
1500 case MSR_IA32_MC0_MISC+8:
1501 case MSR_IA32_MC0_MISC+12:
1502 case MSR_IA32_MC0_MISC+16:
1503 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001504 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001505 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001506 /* MTRR registers */
1507 case 0xfe:
1508 case 0x200 ... 0x2ff:
1509 data = 0;
1510 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001511 case 0xcd: /* fsb frequency */
1512 data = 3;
1513 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001514 case MSR_IA32_APICBASE:
1515 data = vcpu->apic_base;
1516 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001517 case MSR_IA32_MISC_ENABLE:
1518 data = vcpu->ia32_misc_enable_msr;
1519 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001520#ifdef CONFIG_X86_64
1521 case MSR_EFER:
1522 data = vcpu->shadow_efer;
1523 break;
1524#endif
1525 default:
1526 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1527 return 1;
1528 }
1529 *pdata = data;
1530 return 0;
1531}
1532EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1533
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534/*
1535 * Reads an msr value (of 'msr_index') into 'pdata'.
1536 * Returns 0 on success, non-0 otherwise.
1537 * Assumes vcpu_load() was already called.
1538 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001539int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540{
1541 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1542}
1543
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001544#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545
Avi Kivity3bab1f52006-12-29 16:49:48 -08001546static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 if (efer & EFER_RESERVED_BITS) {
1549 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1550 efer);
1551 inject_gp(vcpu);
1552 return;
1553 }
1554
1555 if (is_paging(vcpu)
1556 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1557 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1558 inject_gp(vcpu);
1559 return;
1560 }
1561
Avi Kivity7725f0b2006-12-13 00:34:01 -08001562 kvm_arch_ops->set_efer(vcpu, efer);
1563
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 efer &= ~EFER_LMA;
1565 efer |= vcpu->shadow_efer & EFER_LMA;
1566
1567 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
1570#endif
1571
Avi Kivity3bab1f52006-12-29 16:49:48 -08001572int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1573{
1574 switch (msr) {
1575#ifdef CONFIG_X86_64
1576 case MSR_EFER:
1577 set_efer(vcpu, data);
1578 break;
1579#endif
1580 case MSR_IA32_MC0_STATUS:
1581 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1582 __FUNCTION__, data);
1583 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001584 case MSR_IA32_MCG_STATUS:
1585 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1586 __FUNCTION__, data);
1587 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001588 case MSR_IA32_UCODE_REV:
1589 case MSR_IA32_UCODE_WRITE:
1590 case 0x200 ... 0x2ff: /* MTRRs */
1591 break;
1592 case MSR_IA32_APICBASE:
1593 vcpu->apic_base = data;
1594 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001595 case MSR_IA32_MISC_ENABLE:
1596 vcpu->ia32_misc_enable_msr = data;
1597 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001598 /*
1599 * This is the 'probe whether the host is KVM' logic:
1600 */
1601 case MSR_KVM_API_MAGIC:
1602 return vcpu_register_para(vcpu, data);
1603
Avi Kivity3bab1f52006-12-29 16:49:48 -08001604 default:
1605 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1606 return 1;
1607 }
1608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1611
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612/*
1613 * Writes msr value into into the appropriate "register".
1614 * Returns 0 on success, non-0 otherwise.
1615 * Assumes vcpu_load() was already called.
1616 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001617int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618{
1619 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1620}
1621
1622void kvm_resched(struct kvm_vcpu *vcpu)
1623{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001624 if (!need_resched())
1625 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626 vcpu_put(vcpu);
1627 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001628 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629}
1630EXPORT_SYMBOL_GPL(kvm_resched);
1631
1632void load_msrs(struct vmx_msr_entry *e, int n)
1633{
1634 int i;
1635
1636 for (i = 0; i < n; ++i)
1637 wrmsrl(e[i].index, e[i].data);
1638}
1639EXPORT_SYMBOL_GPL(load_msrs);
1640
1641void save_msrs(struct vmx_msr_entry *e, int n)
1642{
1643 int i;
1644
1645 for (i = 0; i < n; ++i)
1646 rdmsrl(e[i].index, e[i].data);
1647}
1648EXPORT_SYMBOL_GPL(save_msrs);
1649
Avi Kivity06465c52007-02-28 20:46:53 +02001650void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1651{
1652 int i;
1653 u32 function;
1654 struct kvm_cpuid_entry *e, *best;
1655
1656 kvm_arch_ops->cache_regs(vcpu);
1657 function = vcpu->regs[VCPU_REGS_RAX];
1658 vcpu->regs[VCPU_REGS_RAX] = 0;
1659 vcpu->regs[VCPU_REGS_RBX] = 0;
1660 vcpu->regs[VCPU_REGS_RCX] = 0;
1661 vcpu->regs[VCPU_REGS_RDX] = 0;
1662 best = NULL;
1663 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1664 e = &vcpu->cpuid_entries[i];
1665 if (e->function == function) {
1666 best = e;
1667 break;
1668 }
1669 /*
1670 * Both basic or both extended?
1671 */
1672 if (((e->function ^ function) & 0x80000000) == 0)
1673 if (!best || e->function > best->function)
1674 best = e;
1675 }
1676 if (best) {
1677 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1678 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1679 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1680 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1681 }
1682 kvm_arch_ops->decache_regs(vcpu);
1683 kvm_arch_ops->skip_emulated_instruction(vcpu);
1684}
1685EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1686
Avi Kivity039576c2007-03-20 12:46:50 +02001687static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001688{
Avi Kivity039576c2007-03-20 12:46:50 +02001689 void *p = vcpu->pio_data;
1690 void *q;
1691 unsigned bytes;
1692 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1693
1694 kvm_arch_ops->vcpu_put(vcpu);
1695 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1696 PAGE_KERNEL);
1697 if (!q) {
1698 kvm_arch_ops->vcpu_load(vcpu);
1699 free_pio_guest_pages(vcpu);
1700 return -ENOMEM;
1701 }
1702 q += vcpu->pio.guest_page_offset;
1703 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1704 if (vcpu->pio.in)
1705 memcpy(q, p, bytes);
1706 else
1707 memcpy(p, q, bytes);
1708 q -= vcpu->pio.guest_page_offset;
1709 vunmap(q);
1710 kvm_arch_ops->vcpu_load(vcpu);
1711 free_pio_guest_pages(vcpu);
1712 return 0;
1713}
1714
1715static int complete_pio(struct kvm_vcpu *vcpu)
1716{
1717 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001718 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001719 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001720
1721 kvm_arch_ops->cache_regs(vcpu);
1722
1723 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001724 if (io->in)
1725 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001726 io->size);
1727 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001728 if (io->in) {
1729 r = pio_copy_data(vcpu);
1730 if (r) {
1731 kvm_arch_ops->cache_regs(vcpu);
1732 return r;
1733 }
1734 }
1735
Avi Kivity46fc1472007-02-22 19:39:30 +02001736 delta = 1;
1737 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001738 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001739 /*
1740 * The size of the register should really depend on
1741 * current address size.
1742 */
1743 vcpu->regs[VCPU_REGS_RCX] -= delta;
1744 }
Avi Kivity039576c2007-03-20 12:46:50 +02001745 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001746 delta = -delta;
1747 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001748 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001749 vcpu->regs[VCPU_REGS_RDI] += delta;
1750 else
1751 vcpu->regs[VCPU_REGS_RSI] += delta;
1752 }
1753
Avi Kivity46fc1472007-02-22 19:39:30 +02001754 kvm_arch_ops->decache_regs(vcpu);
1755
Avi Kivity039576c2007-03-20 12:46:50 +02001756 io->count -= io->cur_count;
1757 io->cur_count = 0;
1758
1759 if (!io->count)
1760 kvm_arch_ops->skip_emulated_instruction(vcpu);
1761 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001762}
1763
Eddie Dong65619eb2007-07-17 11:52:33 +03001764static void kernel_pio(struct kvm_io_device *pio_dev,
1765 struct kvm_vcpu *vcpu,
1766 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001767{
1768 /* TODO: String I/O for in kernel device */
1769
1770 if (vcpu->pio.in)
1771 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1772 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001773 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001774 else
1775 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1776 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001777 pd);
1778}
1779
1780static void pio_string_write(struct kvm_io_device *pio_dev,
1781 struct kvm_vcpu *vcpu)
1782{
1783 struct kvm_pio_request *io = &vcpu->pio;
1784 void *pd = vcpu->pio_data;
1785 int i;
1786
1787 for (i = 0; i < io->cur_count; i++) {
1788 kvm_iodevice_write(pio_dev, io->port,
1789 io->size,
1790 pd);
1791 pd += io->size;
1792 }
Eddie Dong74906342007-06-19 18:05:03 +03001793}
1794
Avi Kivity039576c2007-03-20 12:46:50 +02001795int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1796 int size, unsigned long count, int string, int down,
1797 gva_t address, int rep, unsigned port)
1798{
1799 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001800 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001801 int nr_pages = 1;
1802 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001803 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001804
1805 vcpu->run->exit_reason = KVM_EXIT_IO;
1806 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1807 vcpu->run->io.size = size;
1808 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1809 vcpu->run->io.count = count;
1810 vcpu->run->io.port = port;
1811 vcpu->pio.count = count;
1812 vcpu->pio.cur_count = count;
1813 vcpu->pio.size = size;
1814 vcpu->pio.in = in;
Eddie Dong74906342007-06-19 18:05:03 +03001815 vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001816 vcpu->pio.string = string;
1817 vcpu->pio.down = down;
1818 vcpu->pio.guest_page_offset = offset_in_page(address);
1819 vcpu->pio.rep = rep;
1820
Eddie Dong74906342007-06-19 18:05:03 +03001821 pio_dev = vcpu_find_pio_dev(vcpu, port);
Avi Kivity039576c2007-03-20 12:46:50 +02001822 if (!string) {
1823 kvm_arch_ops->cache_regs(vcpu);
1824 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1825 kvm_arch_ops->decache_regs(vcpu);
Eddie Dong74906342007-06-19 18:05:03 +03001826 if (pio_dev) {
Eddie Dong65619eb2007-07-17 11:52:33 +03001827 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
Eddie Dong74906342007-06-19 18:05:03 +03001828 complete_pio(vcpu);
1829 return 1;
1830 }
Avi Kivity039576c2007-03-20 12:46:50 +02001831 return 0;
1832 }
1833
1834 if (!count) {
1835 kvm_arch_ops->skip_emulated_instruction(vcpu);
1836 return 1;
1837 }
1838
1839 now = min(count, PAGE_SIZE / size);
1840
1841 if (!down)
1842 in_page = PAGE_SIZE - offset_in_page(address);
1843 else
1844 in_page = offset_in_page(address) + size;
1845 now = min(count, (unsigned long)in_page / size);
1846 if (!now) {
1847 /*
1848 * String I/O straddles page boundary. Pin two guest pages
1849 * so that we satisfy atomicity constraints. Do just one
1850 * transaction to avoid complexity.
1851 */
1852 nr_pages = 2;
1853 now = 1;
1854 }
1855 if (down) {
1856 /*
1857 * String I/O in reverse. Yuck. Kill the guest, fix later.
1858 */
1859 printk(KERN_ERR "kvm: guest string pio down\n");
1860 inject_gp(vcpu);
1861 return 1;
1862 }
1863 vcpu->run->io.count = now;
1864 vcpu->pio.cur_count = now;
1865
1866 for (i = 0; i < nr_pages; ++i) {
1867 spin_lock(&vcpu->kvm->lock);
1868 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1869 if (page)
1870 get_page(page);
1871 vcpu->pio.guest_pages[i] = page;
1872 spin_unlock(&vcpu->kvm->lock);
1873 if (!page) {
1874 inject_gp(vcpu);
1875 free_pio_guest_pages(vcpu);
1876 return 1;
1877 }
1878 }
1879
Eddie Dong65619eb2007-07-17 11:52:33 +03001880 if (!vcpu->pio.in) {
1881 /* string PIO write */
1882 ret = pio_copy_data(vcpu);
1883 if (ret >= 0 && pio_dev) {
1884 pio_string_write(pio_dev, vcpu);
1885 complete_pio(vcpu);
1886 if (vcpu->pio.count == 0)
1887 ret = 1;
1888 }
1889 } else if (pio_dev)
1890 printk(KERN_ERR "no string pio read support yet, "
1891 "port %x size %d count %ld\n",
1892 port, size, count);
1893
1894 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001895}
1896EXPORT_SYMBOL_GPL(kvm_setup_pio);
1897
Avi Kivitybccf2152007-02-21 18:04:26 +02001898static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001901 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902
Avi Kivitybccf2152007-02-21 18:04:26 +02001903 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904
Avi Kivity1961d272007-03-05 19:46:05 +02001905 if (vcpu->sigset_active)
1906 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1907
Dor Laor54810342007-02-12 00:54:39 -08001908 /* re-sync apic's tpr */
1909 vcpu->cr8 = kvm_run->cr8;
1910
Avi Kivity02c83202007-04-29 15:02:17 +03001911 if (vcpu->pio.cur_count) {
1912 r = complete_pio(vcpu);
1913 if (r)
1914 goto out;
1915 }
1916
1917 if (vcpu->mmio_needed) {
1918 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1919 vcpu->mmio_read_completed = 1;
1920 vcpu->mmio_needed = 0;
1921 r = emulate_instruction(vcpu, kvm_run,
1922 vcpu->mmio_fault_cr2, 0);
1923 if (r == EMULATE_DO_MMIO) {
1924 /*
1925 * Read-modify-write. Back to userspace.
1926 */
1927 kvm_run->exit_reason = KVM_EXIT_MMIO;
1928 r = 0;
1929 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001930 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 }
1932
Avi Kivity8eb7d332007-03-04 14:17:08 +02001933 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001934 kvm_arch_ops->cache_regs(vcpu);
1935 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1936 kvm_arch_ops->decache_regs(vcpu);
1937 }
1938
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 r = kvm_arch_ops->run(vcpu, kvm_run);
1940
Avi Kivity039576c2007-03-20 12:46:50 +02001941out:
Avi Kivity1961d272007-03-05 19:46:05 +02001942 if (vcpu->sigset_active)
1943 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1944
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945 vcpu_put(vcpu);
1946 return r;
1947}
1948
Avi Kivitybccf2152007-02-21 18:04:26 +02001949static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1950 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951{
Avi Kivitybccf2152007-02-21 18:04:26 +02001952 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953
1954 kvm_arch_ops->cache_regs(vcpu);
1955
1956 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1957 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1958 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1959 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1960 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1961 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1962 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1963 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001964#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1966 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1967 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1968 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1969 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1970 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1971 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1972 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1973#endif
1974
1975 regs->rip = vcpu->rip;
1976 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1977
1978 /*
1979 * Don't leak debug flags in case they were set for guest debugging
1980 */
1981 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1982 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1983
1984 vcpu_put(vcpu);
1985
1986 return 0;
1987}
1988
Avi Kivitybccf2152007-02-21 18:04:26 +02001989static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1990 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991{
Avi Kivitybccf2152007-02-21 18:04:26 +02001992 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993
1994 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1995 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1996 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1997 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1998 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1999 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2000 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2001 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002002#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2004 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2005 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2006 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2007 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2008 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2009 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2010 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2011#endif
2012
2013 vcpu->rip = regs->rip;
2014 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2015
2016 kvm_arch_ops->decache_regs(vcpu);
2017
2018 vcpu_put(vcpu);
2019
2020 return 0;
2021}
2022
2023static void get_segment(struct kvm_vcpu *vcpu,
2024 struct kvm_segment *var, int seg)
2025{
2026 return kvm_arch_ops->get_segment(vcpu, var, seg);
2027}
2028
Avi Kivitybccf2152007-02-21 18:04:26 +02002029static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2030 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032 struct descriptor_table dt;
2033
Avi Kivitybccf2152007-02-21 18:04:26 +02002034 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035
2036 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2037 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2038 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2039 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2040 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2041 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2042
2043 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2044 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2045
2046 kvm_arch_ops->get_idt(vcpu, &dt);
2047 sregs->idt.limit = dt.limit;
2048 sregs->idt.base = dt.base;
2049 kvm_arch_ops->get_gdt(vcpu, &dt);
2050 sregs->gdt.limit = dt.limit;
2051 sregs->gdt.base = dt.base;
2052
Anthony Liguori25c4c272007-04-27 09:29:21 +03002053 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 sregs->cr0 = vcpu->cr0;
2055 sregs->cr2 = vcpu->cr2;
2056 sregs->cr3 = vcpu->cr3;
2057 sregs->cr4 = vcpu->cr4;
2058 sregs->cr8 = vcpu->cr8;
2059 sregs->efer = vcpu->shadow_efer;
2060 sregs->apic_base = vcpu->apic_base;
2061
2062 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2063 sizeof sregs->interrupt_bitmap);
2064
2065 vcpu_put(vcpu);
2066
2067 return 0;
2068}
2069
2070static void set_segment(struct kvm_vcpu *vcpu,
2071 struct kvm_segment *var, int seg)
2072{
2073 return kvm_arch_ops->set_segment(vcpu, var, seg);
2074}
2075
Avi Kivitybccf2152007-02-21 18:04:26 +02002076static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2077 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079 int mmu_reset_needed = 0;
2080 int i;
2081 struct descriptor_table dt;
2082
Avi Kivitybccf2152007-02-21 18:04:26 +02002083 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 dt.limit = sregs->idt.limit;
2086 dt.base = sregs->idt.base;
2087 kvm_arch_ops->set_idt(vcpu, &dt);
2088 dt.limit = sregs->gdt.limit;
2089 dt.base = sregs->gdt.base;
2090 kvm_arch_ops->set_gdt(vcpu, &dt);
2091
2092 vcpu->cr2 = sregs->cr2;
2093 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2094 vcpu->cr3 = sregs->cr3;
2095
2096 vcpu->cr8 = sregs->cr8;
2097
2098 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002099#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2101#endif
2102 vcpu->apic_base = sregs->apic_base;
2103
Anthony Liguori25c4c272007-04-27 09:29:21 +03002104 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002107 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108
2109 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2110 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002111 if (!is_long_mode(vcpu) && is_pae(vcpu))
2112 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113
2114 if (mmu_reset_needed)
2115 kvm_mmu_reset_context(vcpu);
2116
2117 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2118 sizeof vcpu->irq_pending);
2119 vcpu->irq_summary = 0;
2120 for (i = 0; i < NR_IRQ_WORDS; ++i)
2121 if (vcpu->irq_pending[i])
2122 __set_bit(i, &vcpu->irq_summary);
2123
Avi Kivity024aa1c2007-03-21 13:44:58 +02002124 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2125 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2126 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2127 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2128 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2129 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2130
2131 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2132 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2133
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134 vcpu_put(vcpu);
2135
2136 return 0;
2137}
2138
2139/*
2140 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2141 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002142 *
2143 * This list is modified at module load time to reflect the
2144 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 */
2146static u32 msrs_to_save[] = {
2147 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2148 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002149#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2151#endif
2152 MSR_IA32_TIME_STAMP_COUNTER,
2153};
2154
Michael Riepebf591b22006-12-22 01:05:36 -08002155static unsigned num_msrs_to_save;
2156
Avi Kivity6f00e682007-01-26 00:56:40 -08002157static u32 emulated_msrs[] = {
2158 MSR_IA32_MISC_ENABLE,
2159};
2160
Michael Riepebf591b22006-12-22 01:05:36 -08002161static __init void kvm_init_msr_list(void)
2162{
2163 u32 dummy[2];
2164 unsigned i, j;
2165
2166 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2167 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2168 continue;
2169 if (j < i)
2170 msrs_to_save[j] = msrs_to_save[i];
2171 j++;
2172 }
2173 num_msrs_to_save = j;
2174}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175
2176/*
2177 * Adapt set_msr() to msr_io()'s calling convention
2178 */
2179static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2180{
Avi Kivity35f3f282007-07-17 14:20:30 +03002181 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182}
2183
2184/*
2185 * Read or write a bunch of msrs. All parameters are kernel addresses.
2186 *
2187 * @return number of msrs set successfully.
2188 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002189static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190 struct kvm_msr_entry *entries,
2191 int (*do_msr)(struct kvm_vcpu *vcpu,
2192 unsigned index, u64 *data))
2193{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 int i;
2195
Avi Kivitybccf2152007-02-21 18:04:26 +02002196 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197
2198 for (i = 0; i < msrs->nmsrs; ++i)
2199 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2200 break;
2201
2202 vcpu_put(vcpu);
2203
2204 return i;
2205}
2206
2207/*
2208 * Read or write a bunch of msrs. Parameters are user addresses.
2209 *
2210 * @return number of msrs set successfully.
2211 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002212static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 int (*do_msr)(struct kvm_vcpu *vcpu,
2214 unsigned index, u64 *data),
2215 int writeback)
2216{
2217 struct kvm_msrs msrs;
2218 struct kvm_msr_entry *entries;
2219 int r, n;
2220 unsigned size;
2221
2222 r = -EFAULT;
2223 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2224 goto out;
2225
2226 r = -E2BIG;
2227 if (msrs.nmsrs >= MAX_IO_MSRS)
2228 goto out;
2229
2230 r = -ENOMEM;
2231 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2232 entries = vmalloc(size);
2233 if (!entries)
2234 goto out;
2235
2236 r = -EFAULT;
2237 if (copy_from_user(entries, user_msrs->entries, size))
2238 goto out_free;
2239
Avi Kivitybccf2152007-02-21 18:04:26 +02002240 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241 if (r < 0)
2242 goto out_free;
2243
2244 r = -EFAULT;
2245 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2246 goto out_free;
2247
2248 r = n;
2249
2250out_free:
2251 vfree(entries);
2252out:
2253 return r;
2254}
2255
2256/*
2257 * Translate a guest virtual address to a guest physical address.
2258 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002259static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2260 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261{
2262 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 gpa_t gpa;
2264
Avi Kivitybccf2152007-02-21 18:04:26 +02002265 vcpu_load(vcpu);
2266 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2268 tr->physical_address = gpa;
2269 tr->valid = gpa != UNMAPPED_GVA;
2270 tr->writeable = 1;
2271 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002272 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 vcpu_put(vcpu);
2274
2275 return 0;
2276}
2277
Avi Kivitybccf2152007-02-21 18:04:26 +02002278static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2279 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281 if (irq->irq < 0 || irq->irq >= 256)
2282 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002283 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284
2285 set_bit(irq->irq, vcpu->irq_pending);
2286 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2287
2288 vcpu_put(vcpu);
2289
2290 return 0;
2291}
2292
Avi Kivitybccf2152007-02-21 18:04:26 +02002293static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2294 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 int r;
2297
Avi Kivitybccf2152007-02-21 18:04:26 +02002298 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299
2300 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2301
2302 vcpu_put(vcpu);
2303
2304 return r;
2305}
2306
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002307static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2308 unsigned long address,
2309 int *type)
2310{
2311 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2312 unsigned long pgoff;
2313 struct page *page;
2314
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002315 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002316 if (pgoff == 0)
2317 page = virt_to_page(vcpu->run);
2318 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2319 page = virt_to_page(vcpu->pio_data);
2320 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002321 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002322 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002323 if (type != NULL)
2324 *type = VM_FAULT_MINOR;
2325
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002326 return page;
2327}
2328
2329static struct vm_operations_struct kvm_vcpu_vm_ops = {
2330 .nopage = kvm_vcpu_nopage,
2331};
2332
2333static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2334{
2335 vma->vm_ops = &kvm_vcpu_vm_ops;
2336 return 0;
2337}
2338
Avi Kivitybccf2152007-02-21 18:04:26 +02002339static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2340{
2341 struct kvm_vcpu *vcpu = filp->private_data;
2342
2343 fput(vcpu->kvm->filp);
2344 return 0;
2345}
2346
2347static struct file_operations kvm_vcpu_fops = {
2348 .release = kvm_vcpu_release,
2349 .unlocked_ioctl = kvm_vcpu_ioctl,
2350 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002351 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002352};
2353
2354/*
2355 * Allocates an inode for the vcpu.
2356 */
2357static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2358{
2359 int fd, r;
2360 struct inode *inode;
2361 struct file *file;
2362
Avi Kivityd6d28162007-06-28 08:38:16 -04002363 r = anon_inode_getfd(&fd, &inode, &file,
2364 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2365 if (r)
2366 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002367 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002368 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002369}
2370
Avi Kivityc5ea7662007-02-20 18:41:05 +02002371/*
2372 * Creates some virtual cpus. Good luck creating more than one.
2373 */
2374static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2375{
2376 int r;
2377 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002378 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002379
2380 r = -EINVAL;
2381 if (!valid_vcpu(n))
2382 goto out;
2383
2384 vcpu = &kvm->vcpus[n];
Qing Hedad37952007-07-12 12:33:56 +03002385 vcpu->vcpu_id = n;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002386
2387 mutex_lock(&vcpu->mutex);
2388
2389 if (vcpu->vmcs) {
2390 mutex_unlock(&vcpu->mutex);
2391 return -EEXIST;
2392 }
2393
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002394 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2395 r = -ENOMEM;
2396 if (!page)
2397 goto out_unlock;
2398 vcpu->run = page_address(page);
2399
Avi Kivity039576c2007-03-20 12:46:50 +02002400 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2401 r = -ENOMEM;
2402 if (!page)
2403 goto out_free_run;
2404 vcpu->pio_data = page_address(page);
2405
Avi Kivityc5ea7662007-02-20 18:41:05 +02002406 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2407 FX_IMAGE_ALIGN);
2408 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
Avi Kivityd917a6b2007-04-12 13:03:01 +03002409 vcpu->cr0 = 0x10;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002410
2411 r = kvm_arch_ops->vcpu_create(vcpu);
2412 if (r < 0)
2413 goto out_free_vcpus;
2414
2415 r = kvm_mmu_create(vcpu);
2416 if (r < 0)
2417 goto out_free_vcpus;
2418
2419 kvm_arch_ops->vcpu_load(vcpu);
2420 r = kvm_mmu_setup(vcpu);
2421 if (r >= 0)
2422 r = kvm_arch_ops->vcpu_setup(vcpu);
2423 vcpu_put(vcpu);
2424
2425 if (r < 0)
2426 goto out_free_vcpus;
2427
Avi Kivitybccf2152007-02-21 18:04:26 +02002428 r = create_vcpu_fd(vcpu);
2429 if (r < 0)
2430 goto out_free_vcpus;
2431
Avi Kivity39c3b862007-06-07 19:11:53 +03002432 spin_lock(&kvm_lock);
2433 if (n >= kvm->nvcpus)
2434 kvm->nvcpus = n + 1;
2435 spin_unlock(&kvm_lock);
2436
Avi Kivitybccf2152007-02-21 18:04:26 +02002437 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002438
2439out_free_vcpus:
2440 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002441out_free_run:
2442 free_page((unsigned long)vcpu->run);
2443 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002444out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002445 mutex_unlock(&vcpu->mutex);
2446out:
2447 return r;
2448}
2449
Eddie Dong2cc51562007-05-21 07:28:09 +03002450static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2451{
2452 u64 efer;
2453 int i;
2454 struct kvm_cpuid_entry *e, *entry;
2455
2456 rdmsrl(MSR_EFER, efer);
2457 entry = NULL;
2458 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2459 e = &vcpu->cpuid_entries[i];
2460 if (e->function == 0x80000001) {
2461 entry = e;
2462 break;
2463 }
2464 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002465 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002466 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002467 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002468 }
2469}
2470
Avi Kivity06465c52007-02-28 20:46:53 +02002471static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2472 struct kvm_cpuid *cpuid,
2473 struct kvm_cpuid_entry __user *entries)
2474{
2475 int r;
2476
2477 r = -E2BIG;
2478 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2479 goto out;
2480 r = -EFAULT;
2481 if (copy_from_user(&vcpu->cpuid_entries, entries,
2482 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2483 goto out;
2484 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002485 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002486 return 0;
2487
2488out:
2489 return r;
2490}
2491
Avi Kivity1961d272007-03-05 19:46:05 +02002492static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2493{
2494 if (sigset) {
2495 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2496 vcpu->sigset_active = 1;
2497 vcpu->sigset = *sigset;
2498 } else
2499 vcpu->sigset_active = 0;
2500 return 0;
2501}
2502
Avi Kivityb8836732007-04-01 16:34:31 +03002503/*
2504 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2505 * we have asm/x86/processor.h
2506 */
2507struct fxsave {
2508 u16 cwd;
2509 u16 swd;
2510 u16 twd;
2511 u16 fop;
2512 u64 rip;
2513 u64 rdp;
2514 u32 mxcsr;
2515 u32 mxcsr_mask;
2516 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2517#ifdef CONFIG_X86_64
2518 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2519#else
2520 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2521#endif
2522};
2523
2524static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2525{
2526 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2527
2528 vcpu_load(vcpu);
2529
2530 memcpy(fpu->fpr, fxsave->st_space, 128);
2531 fpu->fcw = fxsave->cwd;
2532 fpu->fsw = fxsave->swd;
2533 fpu->ftwx = fxsave->twd;
2534 fpu->last_opcode = fxsave->fop;
2535 fpu->last_ip = fxsave->rip;
2536 fpu->last_dp = fxsave->rdp;
2537 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2538
2539 vcpu_put(vcpu);
2540
2541 return 0;
2542}
2543
2544static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2545{
2546 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2547
2548 vcpu_load(vcpu);
2549
2550 memcpy(fxsave->st_space, fpu->fpr, 128);
2551 fxsave->cwd = fpu->fcw;
2552 fxsave->swd = fpu->fsw;
2553 fxsave->twd = fpu->ftwx;
2554 fxsave->fop = fpu->last_opcode;
2555 fxsave->rip = fpu->last_ip;
2556 fxsave->rdp = fpu->last_dp;
2557 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2558
2559 vcpu_put(vcpu);
2560
2561 return 0;
2562}
2563
Avi Kivitybccf2152007-02-21 18:04:26 +02002564static long kvm_vcpu_ioctl(struct file *filp,
2565 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566{
Avi Kivitybccf2152007-02-21 18:04:26 +02002567 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002568 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 int r = -EINVAL;
2570
2571 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002572 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002573 r = -EINVAL;
2574 if (arg)
2575 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002576 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 case KVM_GET_REGS: {
2579 struct kvm_regs kvm_regs;
2580
Avi Kivitybccf2152007-02-21 18:04:26 +02002581 memset(&kvm_regs, 0, sizeof kvm_regs);
2582 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 if (r)
2584 goto out;
2585 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002586 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 goto out;
2588 r = 0;
2589 break;
2590 }
2591 case KVM_SET_REGS: {
2592 struct kvm_regs kvm_regs;
2593
2594 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002595 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002597 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 if (r)
2599 goto out;
2600 r = 0;
2601 break;
2602 }
2603 case KVM_GET_SREGS: {
2604 struct kvm_sregs kvm_sregs;
2605
Avi Kivitybccf2152007-02-21 18:04:26 +02002606 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2607 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 if (r)
2609 goto out;
2610 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002611 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 goto out;
2613 r = 0;
2614 break;
2615 }
2616 case KVM_SET_SREGS: {
2617 struct kvm_sregs kvm_sregs;
2618
2619 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002620 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002622 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 if (r)
2624 goto out;
2625 r = 0;
2626 break;
2627 }
2628 case KVM_TRANSLATE: {
2629 struct kvm_translation tr;
2630
2631 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002632 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002634 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 if (r)
2636 goto out;
2637 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002638 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 goto out;
2640 r = 0;
2641 break;
2642 }
2643 case KVM_INTERRUPT: {
2644 struct kvm_interrupt irq;
2645
2646 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002647 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002649 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 if (r)
2651 goto out;
2652 r = 0;
2653 break;
2654 }
2655 case KVM_DEBUG_GUEST: {
2656 struct kvm_debug_guest dbg;
2657
2658 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002659 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002661 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 if (r)
2663 goto out;
2664 r = 0;
2665 break;
2666 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002667 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002668 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002669 break;
2670 case KVM_SET_MSRS:
2671 r = msr_io(vcpu, argp, do_set_msr, 0);
2672 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002673 case KVM_SET_CPUID: {
2674 struct kvm_cpuid __user *cpuid_arg = argp;
2675 struct kvm_cpuid cpuid;
2676
2677 r = -EFAULT;
2678 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2679 goto out;
2680 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2681 if (r)
2682 goto out;
2683 break;
2684 }
Avi Kivity1961d272007-03-05 19:46:05 +02002685 case KVM_SET_SIGNAL_MASK: {
2686 struct kvm_signal_mask __user *sigmask_arg = argp;
2687 struct kvm_signal_mask kvm_sigmask;
2688 sigset_t sigset, *p;
2689
2690 p = NULL;
2691 if (argp) {
2692 r = -EFAULT;
2693 if (copy_from_user(&kvm_sigmask, argp,
2694 sizeof kvm_sigmask))
2695 goto out;
2696 r = -EINVAL;
2697 if (kvm_sigmask.len != sizeof sigset)
2698 goto out;
2699 r = -EFAULT;
2700 if (copy_from_user(&sigset, sigmask_arg->sigset,
2701 sizeof sigset))
2702 goto out;
2703 p = &sigset;
2704 }
2705 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2706 break;
2707 }
Avi Kivityb8836732007-04-01 16:34:31 +03002708 case KVM_GET_FPU: {
2709 struct kvm_fpu fpu;
2710
2711 memset(&fpu, 0, sizeof fpu);
2712 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2713 if (r)
2714 goto out;
2715 r = -EFAULT;
2716 if (copy_to_user(argp, &fpu, sizeof fpu))
2717 goto out;
2718 r = 0;
2719 break;
2720 }
2721 case KVM_SET_FPU: {
2722 struct kvm_fpu fpu;
2723
2724 r = -EFAULT;
2725 if (copy_from_user(&fpu, argp, sizeof fpu))
2726 goto out;
2727 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2728 if (r)
2729 goto out;
2730 r = 0;
2731 break;
2732 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002733 default:
2734 ;
2735 }
2736out:
2737 return r;
2738}
2739
2740static long kvm_vm_ioctl(struct file *filp,
2741 unsigned int ioctl, unsigned long arg)
2742{
2743 struct kvm *kvm = filp->private_data;
2744 void __user *argp = (void __user *)arg;
2745 int r = -EINVAL;
2746
2747 switch (ioctl) {
2748 case KVM_CREATE_VCPU:
2749 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2750 if (r < 0)
2751 goto out;
2752 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 case KVM_SET_MEMORY_REGION: {
2754 struct kvm_memory_region kvm_mem;
2755
2756 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002757 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002759 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 if (r)
2761 goto out;
2762 break;
2763 }
2764 case KVM_GET_DIRTY_LOG: {
2765 struct kvm_dirty_log log;
2766
2767 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002768 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002770 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 if (r)
2772 goto out;
2773 break;
2774 }
Avi Kivitye8207542007-03-30 16:54:30 +03002775 case KVM_SET_MEMORY_ALIAS: {
2776 struct kvm_memory_alias alias;
2777
2778 r = -EFAULT;
2779 if (copy_from_user(&alias, argp, sizeof alias))
2780 goto out;
2781 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2782 if (r)
2783 goto out;
2784 break;
2785 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002786 default:
2787 ;
2788 }
2789out:
2790 return r;
2791}
2792
2793static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2794 unsigned long address,
2795 int *type)
2796{
2797 struct kvm *kvm = vma->vm_file->private_data;
2798 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002799 struct page *page;
2800
Avi Kivityf17abe92007-02-21 19:28:04 +02002801 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002802 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002803 if (!page)
2804 return NOPAGE_SIGBUS;
2805 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002806 if (type != NULL)
2807 *type = VM_FAULT_MINOR;
2808
Avi Kivityf17abe92007-02-21 19:28:04 +02002809 return page;
2810}
2811
2812static struct vm_operations_struct kvm_vm_vm_ops = {
2813 .nopage = kvm_vm_nopage,
2814};
2815
2816static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2817{
2818 vma->vm_ops = &kvm_vm_vm_ops;
2819 return 0;
2820}
2821
2822static struct file_operations kvm_vm_fops = {
2823 .release = kvm_vm_release,
2824 .unlocked_ioctl = kvm_vm_ioctl,
2825 .compat_ioctl = kvm_vm_ioctl,
2826 .mmap = kvm_vm_mmap,
2827};
2828
2829static int kvm_dev_ioctl_create_vm(void)
2830{
2831 int fd, r;
2832 struct inode *inode;
2833 struct file *file;
2834 struct kvm *kvm;
2835
Avi Kivityf17abe92007-02-21 19:28:04 +02002836 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002837 if (IS_ERR(kvm))
2838 return PTR_ERR(kvm);
2839 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2840 if (r) {
2841 kvm_destroy_vm(kvm);
2842 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002843 }
2844
Avi Kivitybccf2152007-02-21 18:04:26 +02002845 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002846
Avi Kivityf17abe92007-02-21 19:28:04 +02002847 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002848}
2849
2850static long kvm_dev_ioctl(struct file *filp,
2851 unsigned int ioctl, unsigned long arg)
2852{
2853 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002854 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002855
2856 switch (ioctl) {
2857 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002858 r = -EINVAL;
2859 if (arg)
2860 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002861 r = KVM_API_VERSION;
2862 break;
2863 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002864 r = -EINVAL;
2865 if (arg)
2866 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002867 r = kvm_dev_ioctl_create_vm();
2868 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002870 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 struct kvm_msr_list msr_list;
2872 unsigned n;
2873
2874 r = -EFAULT;
2875 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2876 goto out;
2877 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002878 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2880 goto out;
2881 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002882 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883 goto out;
2884 r = -EFAULT;
2885 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002886 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002888 if (copy_to_user(user_msr_list->indices
2889 + num_msrs_to_save * sizeof(u32),
2890 &emulated_msrs,
2891 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2892 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002894 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002896 case KVM_CHECK_EXTENSION:
2897 /*
2898 * No extensions defined at present.
2899 */
2900 r = 0;
2901 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002902 case KVM_GET_VCPU_MMAP_SIZE:
2903 r = -EINVAL;
2904 if (arg)
2905 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002906 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002907 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 default:
2909 ;
2910 }
2911out:
2912 return r;
2913}
2914
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915static struct file_operations kvm_chardev_ops = {
2916 .open = kvm_dev_open,
2917 .release = kvm_dev_release,
2918 .unlocked_ioctl = kvm_dev_ioctl,
2919 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920};
2921
2922static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002923 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 "kvm",
2925 &kvm_chardev_ops,
2926};
2927
Avi Kivity774c47f2007-02-12 00:54:47 -08002928/*
2929 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2930 * cached on it.
2931 */
2932static void decache_vcpus_on_cpu(int cpu)
2933{
2934 struct kvm *vm;
2935 struct kvm_vcpu *vcpu;
2936 int i;
2937
2938 spin_lock(&kvm_lock);
2939 list_for_each_entry(vm, &vm_list, vm_list)
2940 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2941 vcpu = &vm->vcpus[i];
2942 /*
2943 * If the vcpu is locked, then it is running on some
2944 * other cpu and therefore it is not cached on the
2945 * cpu in question.
2946 *
2947 * If it's not locked, check the last cpu it executed
2948 * on.
2949 */
2950 if (mutex_trylock(&vcpu->mutex)) {
2951 if (vcpu->cpu == cpu) {
2952 kvm_arch_ops->vcpu_decache(vcpu);
2953 vcpu->cpu = -1;
2954 }
2955 mutex_unlock(&vcpu->mutex);
2956 }
2957 }
2958 spin_unlock(&kvm_lock);
2959}
2960
Avi Kivity1b6c0162007-05-24 13:03:52 +03002961static void hardware_enable(void *junk)
2962{
2963 int cpu = raw_smp_processor_id();
2964
2965 if (cpu_isset(cpu, cpus_hardware_enabled))
2966 return;
2967 cpu_set(cpu, cpus_hardware_enabled);
2968 kvm_arch_ops->hardware_enable(NULL);
2969}
2970
2971static void hardware_disable(void *junk)
2972{
2973 int cpu = raw_smp_processor_id();
2974
2975 if (!cpu_isset(cpu, cpus_hardware_enabled))
2976 return;
2977 cpu_clear(cpu, cpus_hardware_enabled);
2978 decache_vcpus_on_cpu(cpu);
2979 kvm_arch_ops->hardware_disable(NULL);
2980}
2981
Avi Kivity774c47f2007-02-12 00:54:47 -08002982static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2983 void *v)
2984{
2985 int cpu = (long)v;
2986
2987 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002988 case CPU_DYING:
2989 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002990 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2991 cpu);
2992 hardware_disable(NULL);
2993 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002994 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002995 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002996 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2997 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002998 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002999 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003000 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003001 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003002 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3003 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003004 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003005 break;
3006 }
3007 return NOTIFY_OK;
3008}
3009
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003010static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3011 void *v)
3012{
3013 if (val == SYS_RESTART) {
3014 /*
3015 * Some (well, at least mine) BIOSes hang on reboot if
3016 * in vmx root mode.
3017 */
3018 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3019 on_each_cpu(hardware_disable, NULL, 0, 1);
3020 }
3021 return NOTIFY_OK;
3022}
3023
3024static struct notifier_block kvm_reboot_notifier = {
3025 .notifier_call = kvm_reboot,
3026 .priority = 0,
3027};
3028
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003029void kvm_io_bus_init(struct kvm_io_bus *bus)
3030{
3031 memset(bus, 0, sizeof(*bus));
3032}
3033
3034void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3035{
3036 int i;
3037
3038 for (i = 0; i < bus->dev_count; i++) {
3039 struct kvm_io_device *pos = bus->devs[i];
3040
3041 kvm_iodevice_destructor(pos);
3042 }
3043}
3044
3045struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3046{
3047 int i;
3048
3049 for (i = 0; i < bus->dev_count; i++) {
3050 struct kvm_io_device *pos = bus->devs[i];
3051
3052 if (pos->in_range(pos, addr))
3053 return pos;
3054 }
3055
3056 return NULL;
3057}
3058
3059void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3060{
3061 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3062
3063 bus->devs[bus->dev_count++] = dev;
3064}
3065
Avi Kivity774c47f2007-02-12 00:54:47 -08003066static struct notifier_block kvm_cpu_notifier = {
3067 .notifier_call = kvm_cpu_hotplug,
3068 .priority = 20, /* must be > scheduler priority */
3069};
3070
Avi Kivity1165f5f2007-04-19 17:27:43 +03003071static u64 stat_get(void *_offset)
3072{
3073 unsigned offset = (long)_offset;
3074 u64 total = 0;
3075 struct kvm *kvm;
3076 struct kvm_vcpu *vcpu;
3077 int i;
3078
3079 spin_lock(&kvm_lock);
3080 list_for_each_entry(kvm, &vm_list, vm_list)
3081 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3082 vcpu = &kvm->vcpus[i];
3083 total += *(u32 *)((void *)vcpu + offset);
3084 }
3085 spin_unlock(&kvm_lock);
3086 return total;
3087}
3088
3089static void stat_set(void *offset, u64 val)
3090{
3091}
3092
3093DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3094
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095static __init void kvm_init_debug(void)
3096{
3097 struct kvm_stats_debugfs_item *p;
3098
Al Viro8b6d44c2007-02-09 16:38:40 +00003099 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003101 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3102 (void *)(long)p->offset,
3103 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104}
3105
3106static void kvm_exit_debug(void)
3107{
3108 struct kvm_stats_debugfs_item *p;
3109
3110 for (p = debugfs_entries; p->name; ++p)
3111 debugfs_remove(p->dentry);
3112 debugfs_remove(debugfs_dir);
3113}
3114
Avi Kivity59ae6c62007-02-12 00:54:48 -08003115static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3116{
Avi Kivity4267c412007-05-24 13:09:41 +03003117 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003118 return 0;
3119}
3120
3121static int kvm_resume(struct sys_device *dev)
3122{
Avi Kivity4267c412007-05-24 13:09:41 +03003123 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003124 return 0;
3125}
3126
3127static struct sysdev_class kvm_sysdev_class = {
3128 set_kset_name("kvm"),
3129 .suspend = kvm_suspend,
3130 .resume = kvm_resume,
3131};
3132
3133static struct sys_device kvm_sysdev = {
3134 .id = 0,
3135 .cls = &kvm_sysdev_class,
3136};
3137
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138hpa_t bad_page_address;
3139
3140int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3141{
3142 int r;
3143
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003144 if (kvm_arch_ops) {
3145 printk(KERN_ERR "kvm: already loaded the other module\n");
3146 return -EEXIST;
3147 }
3148
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003149 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 printk(KERN_ERR "kvm: no hardware support\n");
3151 return -EOPNOTSUPP;
3152 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003153 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 printk(KERN_ERR "kvm: disabled by bios\n");
3155 return -EOPNOTSUPP;
3156 }
3157
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003158 kvm_arch_ops = ops;
3159
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 r = kvm_arch_ops->hardware_setup();
3161 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003162 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163
Avi Kivity1b6c0162007-05-24 13:03:52 +03003164 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003165 r = register_cpu_notifier(&kvm_cpu_notifier);
3166 if (r)
3167 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 register_reboot_notifier(&kvm_reboot_notifier);
3169
Avi Kivity59ae6c62007-02-12 00:54:48 -08003170 r = sysdev_class_register(&kvm_sysdev_class);
3171 if (r)
3172 goto out_free_2;
3173
3174 r = sysdev_register(&kvm_sysdev);
3175 if (r)
3176 goto out_free_3;
3177
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 kvm_chardev_ops.owner = module;
3179
3180 r = misc_register(&kvm_dev);
3181 if (r) {
3182 printk (KERN_ERR "kvm: misc device register failed\n");
3183 goto out_free;
3184 }
3185
3186 return r;
3187
3188out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003189 sysdev_unregister(&kvm_sysdev);
3190out_free_3:
3191 sysdev_class_unregister(&kvm_sysdev_class);
3192out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003194 unregister_cpu_notifier(&kvm_cpu_notifier);
3195out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003196 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003198out:
3199 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 return r;
3201}
3202
3203void kvm_exit_arch(void)
3204{
3205 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003206 sysdev_unregister(&kvm_sysdev);
3207 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003209 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003210 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003212 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213}
3214
3215static __init int kvm_init(void)
3216{
3217 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003218 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219
Avi Kivityb5a33a72007-04-15 16:31:09 +03003220 r = kvm_mmu_module_init();
3221 if (r)
3222 goto out4;
3223
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 kvm_init_debug();
3225
Michael Riepebf591b22006-12-22 01:05:36 -08003226 kvm_init_msr_list();
3227
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3229 r = -ENOMEM;
3230 goto out;
3231 }
3232
3233 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3234 memset(__va(bad_page_address), 0, PAGE_SIZE);
3235
Avi Kivity58e690e2007-02-26 16:29:43 +02003236 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237
3238out:
3239 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003240 kvm_mmu_module_exit();
3241out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 return r;
3243}
3244
3245static __exit void kvm_exit(void)
3246{
3247 kvm_exit_debug();
3248 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003249 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250}
3251
3252module_init(kvm_init)
3253module_exit(kvm_exit)
3254
3255EXPORT_SYMBOL_GPL(kvm_init_arch);
3256EXPORT_SYMBOL_GPL(kvm_exit_arch);