blob: 34a571dee514db104c7cdf5eac9aa22c7964a895 [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)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000574 if (cr3 & CR3_L_MODE_RESERVED_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 {
Rusty Russellf802a302007-07-17 23:32:55 +1000580 if (is_pae(vcpu)) {
581 if (cr3 & CR3_PAE_RESERVED_BITS) {
582 printk(KERN_DEBUG
583 "set_cr3: #GP, reserved bits\n");
584 inject_gp(vcpu);
585 return;
586 }
587 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
588 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
589 "reserved bits\n");
590 inject_gp(vcpu);
591 return;
592 }
593 } else {
594 if (cr3 & CR3_NONPAE_RESERVED_BITS) {
595 printk(KERN_DEBUG
596 "set_cr3: #GP, reserved bits\n");
597 inject_gp(vcpu);
598 return;
599 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600 }
601 }
602
603 vcpu->cr3 = cr3;
604 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800605 /*
606 * Does the new cr3 value map to physical memory? (Note, we
607 * catch an invalid cr3 even in real-mode, because it would
608 * cause trouble later on when we turn on paging anyway.)
609 *
610 * A real CPU would silently accept an invalid cr3 and would
611 * attempt to use it - with largely undefined (and often hard
612 * to debug) behavior on the guest side.
613 */
614 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
615 inject_gp(vcpu);
616 else
617 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 spin_unlock(&vcpu->kvm->lock);
619}
620EXPORT_SYMBOL_GPL(set_cr3);
621
622void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
623{
624 if ( cr8 & CR8_RESEVED_BITS) {
625 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
626 inject_gp(vcpu);
627 return;
628 }
629 vcpu->cr8 = cr8;
630}
631EXPORT_SYMBOL_GPL(set_cr8);
632
633void fx_init(struct kvm_vcpu *vcpu)
634{
635 struct __attribute__ ((__packed__)) fx_image_s {
636 u16 control; //fcw
637 u16 status; //fsw
638 u16 tag; // ftw
639 u16 opcode; //fop
640 u64 ip; // fpu ip
641 u64 operand;// fpu dp
642 u32 mxcsr;
643 u32 mxcsr_mask;
644
645 } *fx_image;
646
647 fx_save(vcpu->host_fx_image);
648 fpu_init();
649 fx_save(vcpu->guest_fx_image);
650 fx_restore(vcpu->host_fx_image);
651
652 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
653 fx_image->mxcsr = 0x1f80;
654 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
655 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
656}
657EXPORT_SYMBOL_GPL(fx_init);
658
659/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 * Allocate some memory and give it an address in the guest physical address
661 * space.
662 *
663 * Discontiguous memory is allowed, mostly for framebuffers.
664 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200665static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
666 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667{
668 int r;
669 gfn_t base_gfn;
670 unsigned long npages;
671 unsigned long i;
672 struct kvm_memory_slot *memslot;
673 struct kvm_memory_slot old, new;
674 int memory_config_version;
675
676 r = -EINVAL;
677 /* General sanity checks */
678 if (mem->memory_size & (PAGE_SIZE - 1))
679 goto out;
680 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
681 goto out;
682 if (mem->slot >= KVM_MEMORY_SLOTS)
683 goto out;
684 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
685 goto out;
686
687 memslot = &kvm->memslots[mem->slot];
688 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
689 npages = mem->memory_size >> PAGE_SHIFT;
690
691 if (!npages)
692 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
693
694raced:
695 spin_lock(&kvm->lock);
696
697 memory_config_version = kvm->memory_config_version;
698 new = old = *memslot;
699
700 new.base_gfn = base_gfn;
701 new.npages = npages;
702 new.flags = mem->flags;
703
704 /* Disallow changing a memory slot's size. */
705 r = -EINVAL;
706 if (npages && old.npages && npages != old.npages)
707 goto out_unlock;
708
709 /* Check for overlaps */
710 r = -EEXIST;
711 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
712 struct kvm_memory_slot *s = &kvm->memslots[i];
713
714 if (s == memslot)
715 continue;
716 if (!((base_gfn + npages <= s->base_gfn) ||
717 (base_gfn >= s->base_gfn + s->npages)))
718 goto out_unlock;
719 }
720 /*
721 * Do memory allocations outside lock. memory_config_version will
722 * detect any races.
723 */
724 spin_unlock(&kvm->lock);
725
726 /* Deallocate if slot is being removed */
727 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000728 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
730 /* Free page dirty bitmap if unneeded */
731 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000732 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733
734 r = -ENOMEM;
735
736 /* Allocate if a slot is being created */
737 if (npages && !new.phys_mem) {
738 new.phys_mem = vmalloc(npages * sizeof(struct page *));
739
740 if (!new.phys_mem)
741 goto out_free;
742
743 memset(new.phys_mem, 0, npages * sizeof(struct page *));
744 for (i = 0; i < npages; ++i) {
745 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
746 | __GFP_ZERO);
747 if (!new.phys_mem[i])
748 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200749 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750 }
751 }
752
753 /* Allocate page dirty bitmap if needed */
754 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
755 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
756
757 new.dirty_bitmap = vmalloc(dirty_bytes);
758 if (!new.dirty_bitmap)
759 goto out_free;
760 memset(new.dirty_bitmap, 0, dirty_bytes);
761 }
762
763 spin_lock(&kvm->lock);
764
765 if (memory_config_version != kvm->memory_config_version) {
766 spin_unlock(&kvm->lock);
767 kvm_free_physmem_slot(&new, &old);
768 goto raced;
769 }
770
771 r = -EAGAIN;
772 if (kvm->busy)
773 goto out_unlock;
774
775 if (mem->slot >= kvm->nmemslots)
776 kvm->nmemslots = mem->slot + 1;
777
778 *memslot = new;
779 ++kvm->memory_config_version;
780
Avi Kivity90cb0522007-07-17 13:04:56 +0300781 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
782 kvm_flush_remote_tlbs(kvm);
783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 spin_unlock(&kvm->lock);
785
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 kvm_free_physmem_slot(&old, &new);
787 return 0;
788
789out_unlock:
790 spin_unlock(&kvm->lock);
791out_free:
792 kvm_free_physmem_slot(&new, &old);
793out:
794 return r;
795}
796
797/*
798 * Get (and clear) the dirty memory log for a memory slot.
799 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200800static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
801 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802{
803 struct kvm_memory_slot *memslot;
804 int r, i;
805 int n;
806 unsigned long any = 0;
807
808 spin_lock(&kvm->lock);
809
810 /*
811 * Prevent changes to guest memory configuration even while the lock
812 * is not taken.
813 */
814 ++kvm->busy;
815 spin_unlock(&kvm->lock);
816 r = -EINVAL;
817 if (log->slot >= KVM_MEMORY_SLOTS)
818 goto out;
819
820 memslot = &kvm->memslots[log->slot];
821 r = -ENOENT;
822 if (!memslot->dirty_bitmap)
823 goto out;
824
Uri Lublincd1a4a92007-02-22 16:43:09 +0200825 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826
Uri Lublincd1a4a92007-02-22 16:43:09 +0200827 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 any = memslot->dirty_bitmap[i];
829
830 r = -EFAULT;
831 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
832 goto out;
833
Avi Kivity90cb0522007-07-17 13:04:56 +0300834 spin_lock(&kvm->lock);
835 kvm_mmu_slot_remove_write_access(kvm, log->slot);
836 kvm_flush_remote_tlbs(kvm);
837 memset(memslot->dirty_bitmap, 0, n);
838 spin_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
840 r = 0;
841
842out:
843 spin_lock(&kvm->lock);
844 --kvm->busy;
845 spin_unlock(&kvm->lock);
846 return r;
847}
848
Avi Kivitye8207542007-03-30 16:54:30 +0300849/*
850 * Set a new alias region. Aliases map a portion of physical memory into
851 * another portion. This is useful for memory windows, for example the PC
852 * VGA region.
853 */
854static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
855 struct kvm_memory_alias *alias)
856{
857 int r, n;
858 struct kvm_mem_alias *p;
859
860 r = -EINVAL;
861 /* General sanity checks */
862 if (alias->memory_size & (PAGE_SIZE - 1))
863 goto out;
864 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
865 goto out;
866 if (alias->slot >= KVM_ALIAS_SLOTS)
867 goto out;
868 if (alias->guest_phys_addr + alias->memory_size
869 < alias->guest_phys_addr)
870 goto out;
871 if (alias->target_phys_addr + alias->memory_size
872 < alias->target_phys_addr)
873 goto out;
874
875 spin_lock(&kvm->lock);
876
877 p = &kvm->aliases[alias->slot];
878 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
879 p->npages = alias->memory_size >> PAGE_SHIFT;
880 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
881
882 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
883 if (kvm->aliases[n - 1].npages)
884 break;
885 kvm->naliases = n;
886
Avi Kivity90cb0522007-07-17 13:04:56 +0300887 kvm_mmu_zap_all(kvm);
Avi Kivitye8207542007-03-30 16:54:30 +0300888
Avi Kivitye8207542007-03-30 16:54:30 +0300889 spin_unlock(&kvm->lock);
Avi Kivitye8207542007-03-30 16:54:30 +0300890
891 return 0;
892
893out:
894 return r;
895}
896
897static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
898{
899 int i;
900 struct kvm_mem_alias *alias;
901
902 for (i = 0; i < kvm->naliases; ++i) {
903 alias = &kvm->aliases[i];
904 if (gfn >= alias->base_gfn
905 && gfn < alias->base_gfn + alias->npages)
906 return alias->target_gfn + gfn - alias->base_gfn;
907 }
908 return gfn;
909}
910
911static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912{
913 int i;
914
915 for (i = 0; i < kvm->nmemslots; ++i) {
916 struct kvm_memory_slot *memslot = &kvm->memslots[i];
917
918 if (gfn >= memslot->base_gfn
919 && gfn < memslot->base_gfn + memslot->npages)
920 return memslot;
921 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000922 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923}
Avi Kivitye8207542007-03-30 16:54:30 +0300924
925struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
926{
927 gfn = unalias_gfn(kvm, gfn);
928 return __gfn_to_memslot(kvm, gfn);
929}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
Avi Kivity954bbbc2007-03-30 14:02:32 +0300931struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
932{
933 struct kvm_memory_slot *slot;
934
Avi Kivitye8207542007-03-30 16:54:30 +0300935 gfn = unalias_gfn(kvm, gfn);
936 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300937 if (!slot)
938 return NULL;
939 return slot->phys_mem[gfn - slot->base_gfn];
940}
941EXPORT_SYMBOL_GPL(gfn_to_page);
942
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
944{
945 int i;
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300946 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 unsigned long rel_gfn;
948
949 for (i = 0; i < kvm->nmemslots; ++i) {
950 memslot = &kvm->memslots[i];
951
952 if (gfn >= memslot->base_gfn
953 && gfn < memslot->base_gfn + memslot->npages) {
954
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300955 if (!memslot->dirty_bitmap)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 return;
957
958 rel_gfn = gfn - memslot->base_gfn;
959
960 /* avoid RMW */
961 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
962 set_bit(rel_gfn, memslot->dirty_bitmap);
963 return;
964 }
965 }
966}
967
968static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300969 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 unsigned int bytes,
971 struct x86_emulate_ctxt *ctxt)
972{
973 struct kvm_vcpu *vcpu = ctxt->vcpu;
974 void *data = val;
975
976 while (bytes) {
977 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
978 unsigned offset = addr & (PAGE_SIZE-1);
979 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
980 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300981 struct page *page;
982 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983
984 if (gpa == UNMAPPED_GVA)
985 return X86EMUL_PROPAGATE_FAULT;
986 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300987 page = gfn_to_page(vcpu->kvm, pfn);
988 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300990 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991
Avi Kivity954bbbc2007-03-30 14:02:32 +0300992 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
Avi Kivity954bbbc2007-03-30 14:02:32 +0300994 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
996 bytes -= tocopy;
997 data += tocopy;
998 addr += tocopy;
999 }
1000
1001 return X86EMUL_CONTINUE;
1002}
1003
1004static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001005 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 unsigned int bytes,
1007 struct x86_emulate_ctxt *ctxt)
1008{
1009 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1010 addr, bytes);
1011 return X86EMUL_UNHANDLEABLE;
1012}
1013
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001014static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1015 gpa_t addr)
1016{
1017 /*
1018 * Note that its important to have this wrapper function because
1019 * in the very near future we will be checking for MMIOs against
1020 * the LAPIC as well as the general MMIO bus
1021 */
1022 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1023}
1024
Eddie Dong74906342007-06-19 18:05:03 +03001025static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1026 gpa_t addr)
1027{
1028 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1029}
1030
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001032 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 unsigned int bytes,
1034 struct x86_emulate_ctxt *ctxt)
1035{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001036 struct kvm_vcpu *vcpu = ctxt->vcpu;
1037 struct kvm_io_device *mmio_dev;
1038 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039
1040 if (vcpu->mmio_read_completed) {
1041 memcpy(val, vcpu->mmio_data, bytes);
1042 vcpu->mmio_read_completed = 0;
1043 return X86EMUL_CONTINUE;
1044 } else if (emulator_read_std(addr, val, bytes, ctxt)
1045 == X86EMUL_CONTINUE)
1046 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001047
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001048 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1049 if (gpa == UNMAPPED_GVA)
1050 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001052 /*
1053 * Is this MMIO handled locally?
1054 */
1055 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1056 if (mmio_dev) {
1057 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1058 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001060
1061 vcpu->mmio_needed = 1;
1062 vcpu->mmio_phys_addr = gpa;
1063 vcpu->mmio_size = bytes;
1064 vcpu->mmio_is_write = 0;
1065
1066 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067}
1068
Avi Kivityda4a00f2007-01-05 16:36:44 -08001069static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001070 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001071{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001072 struct page *page;
1073 void *virt;
Avi Kivity09072da2007-05-01 14:16:52 +03001074 unsigned offset = offset_in_page(gpa);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001075
1076 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1077 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001078 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1079 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001080 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001081 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001082 virt = kmap_atomic(page, KM_USER0);
Avi Kivity7cfa4b02007-07-23 18:33:14 +03001083 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1084 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001085 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001086 return 1;
1087}
1088
Avi Kivityb0fcd902007-07-22 18:48:54 +03001089static int emulator_write_emulated_onepage(unsigned long addr,
1090 const void *val,
1091 unsigned int bytes,
1092 struct x86_emulate_ctxt *ctxt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001094 struct kvm_vcpu *vcpu = ctxt->vcpu;
1095 struct kvm_io_device *mmio_dev;
1096 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097
Avi Kivityc9047f52007-04-17 10:53:22 +03001098 if (gpa == UNMAPPED_GVA) {
1099 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001101 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102
Avi Kivityda4a00f2007-01-05 16:36:44 -08001103 if (emulator_write_phys(vcpu, gpa, val, bytes))
1104 return X86EMUL_CONTINUE;
1105
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001106 /*
1107 * Is this MMIO handled locally?
1108 */
1109 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1110 if (mmio_dev) {
1111 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1112 return X86EMUL_CONTINUE;
1113 }
1114
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 vcpu->mmio_needed = 1;
1116 vcpu->mmio_phys_addr = gpa;
1117 vcpu->mmio_size = bytes;
1118 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001119 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120
1121 return X86EMUL_CONTINUE;
1122}
1123
Avi Kivityb0fcd902007-07-22 18:48:54 +03001124static int emulator_write_emulated(unsigned long addr,
1125 const void *val,
1126 unsigned int bytes,
1127 struct x86_emulate_ctxt *ctxt)
1128{
1129 /* Crossing a page boundary? */
1130 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1131 int rc, now;
1132
1133 now = -addr & ~PAGE_MASK;
1134 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1135 if (rc != X86EMUL_CONTINUE)
1136 return rc;
1137 addr += now;
1138 val += now;
1139 bytes -= now;
1140 }
1141 return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1142}
1143
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001145 const void *old,
1146 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 unsigned int bytes,
1148 struct x86_emulate_ctxt *ctxt)
1149{
1150 static int reported;
1151
1152 if (!reported) {
1153 reported = 1;
1154 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1155 }
1156 return emulator_write_emulated(addr, new, bytes, ctxt);
1157}
1158
1159static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1160{
1161 return kvm_arch_ops->get_segment_base(vcpu, seg);
1162}
1163
1164int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1165{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 return X86EMUL_CONTINUE;
1167}
1168
1169int emulate_clts(struct kvm_vcpu *vcpu)
1170{
Avi Kivity399badf2007-01-05 16:36:38 -08001171 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172
Rusty Russell707d92fa2007-07-17 23:19:08 +10001173 cr0 = vcpu->cr0 & ~X86_CR0_TS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 kvm_arch_ops->set_cr0(vcpu, cr0);
1175 return X86EMUL_CONTINUE;
1176}
1177
1178int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1179{
1180 struct kvm_vcpu *vcpu = ctxt->vcpu;
1181
1182 switch (dr) {
1183 case 0 ... 3:
1184 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1185 return X86EMUL_CONTINUE;
1186 default:
1187 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1188 __FUNCTION__, dr);
1189 return X86EMUL_UNHANDLEABLE;
1190 }
1191}
1192
1193int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1194{
1195 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1196 int exception;
1197
1198 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1199 if (exception) {
1200 /* FIXME: better handling */
1201 return X86EMUL_UNHANDLEABLE;
1202 }
1203 return X86EMUL_CONTINUE;
1204}
1205
1206static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1207{
1208 static int reported;
1209 u8 opcodes[4];
1210 unsigned long rip = ctxt->vcpu->rip;
1211 unsigned long rip_linear;
1212
1213 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1214
1215 if (reported)
1216 return;
1217
1218 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1219
1220 printk(KERN_ERR "emulation failed but !mmio_needed?"
1221 " rip %lx %02x %02x %02x %02x\n",
1222 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1223 reported = 1;
1224}
1225
1226struct x86_emulate_ops emulate_ops = {
1227 .read_std = emulator_read_std,
1228 .write_std = emulator_write_std,
1229 .read_emulated = emulator_read_emulated,
1230 .write_emulated = emulator_write_emulated,
1231 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1232};
1233
1234int emulate_instruction(struct kvm_vcpu *vcpu,
1235 struct kvm_run *run,
1236 unsigned long cr2,
1237 u16 error_code)
1238{
1239 struct x86_emulate_ctxt emulate_ctxt;
1240 int r;
1241 int cs_db, cs_l;
1242
Avi Kivitye7df56e2007-03-14 15:54:54 +02001243 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 kvm_arch_ops->cache_regs(vcpu);
1245
1246 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1247
1248 emulate_ctxt.vcpu = vcpu;
1249 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1250 emulate_ctxt.cr2 = cr2;
1251 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1252 ? X86EMUL_MODE_REAL : cs_l
1253 ? X86EMUL_MODE_PROT64 : cs_db
1254 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1255
1256 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1257 emulate_ctxt.cs_base = 0;
1258 emulate_ctxt.ds_base = 0;
1259 emulate_ctxt.es_base = 0;
1260 emulate_ctxt.ss_base = 0;
1261 } else {
1262 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1263 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1264 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1265 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1266 }
1267
1268 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1269 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1270
1271 vcpu->mmio_is_write = 0;
1272 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1273
1274 if ((r || vcpu->mmio_is_write) && run) {
1275 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1276 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1277 run->mmio.len = vcpu->mmio_size;
1278 run->mmio.is_write = vcpu->mmio_is_write;
1279 }
1280
1281 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001282 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1283 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 if (!vcpu->mmio_needed) {
1285 report_emulation_failure(&emulate_ctxt);
1286 return EMULATE_FAIL;
1287 }
1288 return EMULATE_DO_MMIO;
1289 }
1290
1291 kvm_arch_ops->decache_regs(vcpu);
1292 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1293
Avi Kivity02c83202007-04-29 15:02:17 +03001294 if (vcpu->mmio_is_write) {
1295 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001297 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298
1299 return EMULATE_DONE;
1300}
1301EXPORT_SYMBOL_GPL(emulate_instruction);
1302
Avi Kivityd3bef152007-06-05 15:53:05 +03001303int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1304{
1305 if (vcpu->irq_summary)
1306 return 1;
1307
1308 vcpu->run->exit_reason = KVM_EXIT_HLT;
1309 ++vcpu->stat.halt_exits;
1310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1313
Avi Kivity270fd9b2007-02-19 14:37:47 +02001314int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1315{
1316 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1317
Dor Laor9b22bf52007-02-19 16:44:49 +02001318 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001319 ret = -KVM_EINVAL;
1320#ifdef CONFIG_X86_64
1321 if (is_long_mode(vcpu)) {
1322 nr = vcpu->regs[VCPU_REGS_RAX];
1323 a0 = vcpu->regs[VCPU_REGS_RDI];
1324 a1 = vcpu->regs[VCPU_REGS_RSI];
1325 a2 = vcpu->regs[VCPU_REGS_RDX];
1326 a3 = vcpu->regs[VCPU_REGS_RCX];
1327 a4 = vcpu->regs[VCPU_REGS_R8];
1328 a5 = vcpu->regs[VCPU_REGS_R9];
1329 } else
1330#endif
1331 {
1332 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1333 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1334 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1335 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1336 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1337 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1338 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1339 }
1340 switch (nr) {
1341 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001342 run->hypercall.args[0] = a0;
1343 run->hypercall.args[1] = a1;
1344 run->hypercall.args[2] = a2;
1345 run->hypercall.args[3] = a3;
1346 run->hypercall.args[4] = a4;
1347 run->hypercall.args[5] = a5;
1348 run->hypercall.ret = ret;
1349 run->hypercall.longmode = is_long_mode(vcpu);
1350 kvm_arch_ops->decache_regs(vcpu);
1351 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001352 }
1353 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001354 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001355 return 1;
1356}
1357EXPORT_SYMBOL_GPL(kvm_hypercall);
1358
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1360{
1361 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1362}
1363
1364void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1365{
1366 struct descriptor_table dt = { limit, base };
1367
1368 kvm_arch_ops->set_gdt(vcpu, &dt);
1369}
1370
1371void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1372{
1373 struct descriptor_table dt = { limit, base };
1374
1375 kvm_arch_ops->set_idt(vcpu, &dt);
1376}
1377
1378void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1379 unsigned long *rflags)
1380{
1381 lmsw(vcpu, msw);
1382 *rflags = kvm_arch_ops->get_rflags(vcpu);
1383}
1384
1385unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1386{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001387 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 switch (cr) {
1389 case 0:
1390 return vcpu->cr0;
1391 case 2:
1392 return vcpu->cr2;
1393 case 3:
1394 return vcpu->cr3;
1395 case 4:
1396 return vcpu->cr4;
1397 default:
1398 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1399 return 0;
1400 }
1401}
1402
1403void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1404 unsigned long *rflags)
1405{
1406 switch (cr) {
1407 case 0:
1408 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1409 *rflags = kvm_arch_ops->get_rflags(vcpu);
1410 break;
1411 case 2:
1412 vcpu->cr2 = val;
1413 break;
1414 case 3:
1415 set_cr3(vcpu, val);
1416 break;
1417 case 4:
1418 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1419 break;
1420 default:
1421 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1422 }
1423}
1424
Ingo Molnar102d8322007-02-19 14:37:47 +02001425/*
1426 * Register the para guest with the host:
1427 */
1428static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1429{
1430 struct kvm_vcpu_para_state *para_state;
1431 hpa_t para_state_hpa, hypercall_hpa;
1432 struct page *para_state_page;
1433 unsigned char *hypercall;
1434 gpa_t hypercall_gpa;
1435
1436 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1437 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1438
1439 /*
1440 * Needs to be page aligned:
1441 */
1442 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1443 goto err_gp;
1444
1445 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1446 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1447 if (is_error_hpa(para_state_hpa))
1448 goto err_gp;
1449
Uri Lublinab51a432007-02-21 18:25:21 +02001450 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001451 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1452 para_state = kmap_atomic(para_state_page, KM_USER0);
1453
1454 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1455 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1456
1457 para_state->host_version = KVM_PARA_API_VERSION;
1458 /*
1459 * We cannot support guests that try to register themselves
1460 * with a newer API version than the host supports:
1461 */
1462 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1463 para_state->ret = -KVM_EINVAL;
1464 goto err_kunmap_skip;
1465 }
1466
1467 hypercall_gpa = para_state->hypercall_gpa;
1468 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1469 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1470 if (is_error_hpa(hypercall_hpa)) {
1471 para_state->ret = -KVM_EINVAL;
1472 goto err_kunmap_skip;
1473 }
1474
1475 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1476 vcpu->para_state_page = para_state_page;
1477 vcpu->para_state_gpa = para_state_gpa;
1478 vcpu->hypercall_gpa = hypercall_gpa;
1479
Uri Lublinab51a432007-02-21 18:25:21 +02001480 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001481 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1482 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1483 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1484 kunmap_atomic(hypercall, KM_USER1);
1485
1486 para_state->ret = 0;
1487err_kunmap_skip:
1488 kunmap_atomic(para_state, KM_USER0);
1489 return 0;
1490err_gp:
1491 return 1;
1492}
1493
Avi Kivity3bab1f52006-12-29 16:49:48 -08001494int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1495{
1496 u64 data;
1497
1498 switch (msr) {
1499 case 0xc0010010: /* SYSCFG */
1500 case 0xc0010015: /* HWCR */
1501 case MSR_IA32_PLATFORM_ID:
1502 case MSR_IA32_P5_MC_ADDR:
1503 case MSR_IA32_P5_MC_TYPE:
1504 case MSR_IA32_MC0_CTL:
1505 case MSR_IA32_MCG_STATUS:
1506 case MSR_IA32_MCG_CAP:
1507 case MSR_IA32_MC0_MISC:
1508 case MSR_IA32_MC0_MISC+4:
1509 case MSR_IA32_MC0_MISC+8:
1510 case MSR_IA32_MC0_MISC+12:
1511 case MSR_IA32_MC0_MISC+16:
1512 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001513 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001514 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001515 /* MTRR registers */
1516 case 0xfe:
1517 case 0x200 ... 0x2ff:
1518 data = 0;
1519 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001520 case 0xcd: /* fsb frequency */
1521 data = 3;
1522 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001523 case MSR_IA32_APICBASE:
1524 data = vcpu->apic_base;
1525 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001526 case MSR_IA32_MISC_ENABLE:
1527 data = vcpu->ia32_misc_enable_msr;
1528 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001529#ifdef CONFIG_X86_64
1530 case MSR_EFER:
1531 data = vcpu->shadow_efer;
1532 break;
1533#endif
1534 default:
1535 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1536 return 1;
1537 }
1538 *pdata = data;
1539 return 0;
1540}
1541EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1542
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543/*
1544 * Reads an msr value (of 'msr_index') into 'pdata'.
1545 * Returns 0 on success, non-0 otherwise.
1546 * Assumes vcpu_load() was already called.
1547 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001548int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549{
1550 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1551}
1552
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001553#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554
Avi Kivity3bab1f52006-12-29 16:49:48 -08001555static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 if (efer & EFER_RESERVED_BITS) {
1558 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1559 efer);
1560 inject_gp(vcpu);
1561 return;
1562 }
1563
1564 if (is_paging(vcpu)
1565 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1566 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1567 inject_gp(vcpu);
1568 return;
1569 }
1570
Avi Kivity7725f0b2006-12-13 00:34:01 -08001571 kvm_arch_ops->set_efer(vcpu, efer);
1572
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 efer &= ~EFER_LMA;
1574 efer |= vcpu->shadow_efer & EFER_LMA;
1575
1576 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578
1579#endif
1580
Avi Kivity3bab1f52006-12-29 16:49:48 -08001581int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1582{
1583 switch (msr) {
1584#ifdef CONFIG_X86_64
1585 case MSR_EFER:
1586 set_efer(vcpu, data);
1587 break;
1588#endif
1589 case MSR_IA32_MC0_STATUS:
1590 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1591 __FUNCTION__, data);
1592 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001593 case MSR_IA32_MCG_STATUS:
1594 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1595 __FUNCTION__, data);
1596 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001597 case MSR_IA32_UCODE_REV:
1598 case MSR_IA32_UCODE_WRITE:
1599 case 0x200 ... 0x2ff: /* MTRRs */
1600 break;
1601 case MSR_IA32_APICBASE:
1602 vcpu->apic_base = data;
1603 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001604 case MSR_IA32_MISC_ENABLE:
1605 vcpu->ia32_misc_enable_msr = data;
1606 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001607 /*
1608 * This is the 'probe whether the host is KVM' logic:
1609 */
1610 case MSR_KVM_API_MAGIC:
1611 return vcpu_register_para(vcpu, data);
1612
Avi Kivity3bab1f52006-12-29 16:49:48 -08001613 default:
1614 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1615 return 1;
1616 }
1617 return 0;
1618}
1619EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1620
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621/*
1622 * Writes msr value into into the appropriate "register".
1623 * Returns 0 on success, non-0 otherwise.
1624 * Assumes vcpu_load() was already called.
1625 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001626int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627{
1628 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1629}
1630
1631void kvm_resched(struct kvm_vcpu *vcpu)
1632{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001633 if (!need_resched())
1634 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 vcpu_put(vcpu);
1636 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001637 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638}
1639EXPORT_SYMBOL_GPL(kvm_resched);
1640
1641void load_msrs(struct vmx_msr_entry *e, int n)
1642{
1643 int i;
1644
1645 for (i = 0; i < n; ++i)
1646 wrmsrl(e[i].index, e[i].data);
1647}
1648EXPORT_SYMBOL_GPL(load_msrs);
1649
1650void save_msrs(struct vmx_msr_entry *e, int n)
1651{
1652 int i;
1653
1654 for (i = 0; i < n; ++i)
1655 rdmsrl(e[i].index, e[i].data);
1656}
1657EXPORT_SYMBOL_GPL(save_msrs);
1658
Avi Kivity06465c52007-02-28 20:46:53 +02001659void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1660{
1661 int i;
1662 u32 function;
1663 struct kvm_cpuid_entry *e, *best;
1664
1665 kvm_arch_ops->cache_regs(vcpu);
1666 function = vcpu->regs[VCPU_REGS_RAX];
1667 vcpu->regs[VCPU_REGS_RAX] = 0;
1668 vcpu->regs[VCPU_REGS_RBX] = 0;
1669 vcpu->regs[VCPU_REGS_RCX] = 0;
1670 vcpu->regs[VCPU_REGS_RDX] = 0;
1671 best = NULL;
1672 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1673 e = &vcpu->cpuid_entries[i];
1674 if (e->function == function) {
1675 best = e;
1676 break;
1677 }
1678 /*
1679 * Both basic or both extended?
1680 */
1681 if (((e->function ^ function) & 0x80000000) == 0)
1682 if (!best || e->function > best->function)
1683 best = e;
1684 }
1685 if (best) {
1686 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1687 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1688 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1689 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1690 }
1691 kvm_arch_ops->decache_regs(vcpu);
1692 kvm_arch_ops->skip_emulated_instruction(vcpu);
1693}
1694EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1695
Avi Kivity039576c2007-03-20 12:46:50 +02001696static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001697{
Avi Kivity039576c2007-03-20 12:46:50 +02001698 void *p = vcpu->pio_data;
1699 void *q;
1700 unsigned bytes;
1701 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1702
1703 kvm_arch_ops->vcpu_put(vcpu);
1704 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1705 PAGE_KERNEL);
1706 if (!q) {
1707 kvm_arch_ops->vcpu_load(vcpu);
1708 free_pio_guest_pages(vcpu);
1709 return -ENOMEM;
1710 }
1711 q += vcpu->pio.guest_page_offset;
1712 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1713 if (vcpu->pio.in)
1714 memcpy(q, p, bytes);
1715 else
1716 memcpy(p, q, bytes);
1717 q -= vcpu->pio.guest_page_offset;
1718 vunmap(q);
1719 kvm_arch_ops->vcpu_load(vcpu);
1720 free_pio_guest_pages(vcpu);
1721 return 0;
1722}
1723
1724static int complete_pio(struct kvm_vcpu *vcpu)
1725{
1726 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001727 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001728 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001729
1730 kvm_arch_ops->cache_regs(vcpu);
1731
1732 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001733 if (io->in)
1734 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001735 io->size);
1736 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001737 if (io->in) {
1738 r = pio_copy_data(vcpu);
1739 if (r) {
1740 kvm_arch_ops->cache_regs(vcpu);
1741 return r;
1742 }
1743 }
1744
Avi Kivity46fc1472007-02-22 19:39:30 +02001745 delta = 1;
1746 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001747 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001748 /*
1749 * The size of the register should really depend on
1750 * current address size.
1751 */
1752 vcpu->regs[VCPU_REGS_RCX] -= delta;
1753 }
Avi Kivity039576c2007-03-20 12:46:50 +02001754 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001755 delta = -delta;
1756 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001757 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001758 vcpu->regs[VCPU_REGS_RDI] += delta;
1759 else
1760 vcpu->regs[VCPU_REGS_RSI] += delta;
1761 }
1762
Avi Kivity46fc1472007-02-22 19:39:30 +02001763 kvm_arch_ops->decache_regs(vcpu);
1764
Avi Kivity039576c2007-03-20 12:46:50 +02001765 io->count -= io->cur_count;
1766 io->cur_count = 0;
1767
1768 if (!io->count)
1769 kvm_arch_ops->skip_emulated_instruction(vcpu);
1770 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001771}
1772
Eddie Dong65619eb2007-07-17 11:52:33 +03001773static void kernel_pio(struct kvm_io_device *pio_dev,
1774 struct kvm_vcpu *vcpu,
1775 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001776{
1777 /* TODO: String I/O for in kernel device */
1778
1779 if (vcpu->pio.in)
1780 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1781 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001782 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001783 else
1784 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1785 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001786 pd);
1787}
1788
1789static void pio_string_write(struct kvm_io_device *pio_dev,
1790 struct kvm_vcpu *vcpu)
1791{
1792 struct kvm_pio_request *io = &vcpu->pio;
1793 void *pd = vcpu->pio_data;
1794 int i;
1795
1796 for (i = 0; i < io->cur_count; i++) {
1797 kvm_iodevice_write(pio_dev, io->port,
1798 io->size,
1799 pd);
1800 pd += io->size;
1801 }
Eddie Dong74906342007-06-19 18:05:03 +03001802}
1803
Avi Kivity039576c2007-03-20 12:46:50 +02001804int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1805 int size, unsigned long count, int string, int down,
1806 gva_t address, int rep, unsigned port)
1807{
1808 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001809 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001810 int nr_pages = 1;
1811 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001812 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001813
1814 vcpu->run->exit_reason = KVM_EXIT_IO;
1815 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1816 vcpu->run->io.size = size;
1817 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1818 vcpu->run->io.count = count;
1819 vcpu->run->io.port = port;
1820 vcpu->pio.count = count;
1821 vcpu->pio.cur_count = count;
1822 vcpu->pio.size = size;
1823 vcpu->pio.in = in;
Eddie Dong74906342007-06-19 18:05:03 +03001824 vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001825 vcpu->pio.string = string;
1826 vcpu->pio.down = down;
1827 vcpu->pio.guest_page_offset = offset_in_page(address);
1828 vcpu->pio.rep = rep;
1829
Eddie Dong74906342007-06-19 18:05:03 +03001830 pio_dev = vcpu_find_pio_dev(vcpu, port);
Avi Kivity039576c2007-03-20 12:46:50 +02001831 if (!string) {
1832 kvm_arch_ops->cache_regs(vcpu);
1833 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1834 kvm_arch_ops->decache_regs(vcpu);
Eddie Dong74906342007-06-19 18:05:03 +03001835 if (pio_dev) {
Eddie Dong65619eb2007-07-17 11:52:33 +03001836 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
Eddie Dong74906342007-06-19 18:05:03 +03001837 complete_pio(vcpu);
1838 return 1;
1839 }
Avi Kivity039576c2007-03-20 12:46:50 +02001840 return 0;
1841 }
1842
1843 if (!count) {
1844 kvm_arch_ops->skip_emulated_instruction(vcpu);
1845 return 1;
1846 }
1847
1848 now = min(count, PAGE_SIZE / size);
1849
1850 if (!down)
1851 in_page = PAGE_SIZE - offset_in_page(address);
1852 else
1853 in_page = offset_in_page(address) + size;
1854 now = min(count, (unsigned long)in_page / size);
1855 if (!now) {
1856 /*
1857 * String I/O straddles page boundary. Pin two guest pages
1858 * so that we satisfy atomicity constraints. Do just one
1859 * transaction to avoid complexity.
1860 */
1861 nr_pages = 2;
1862 now = 1;
1863 }
1864 if (down) {
1865 /*
1866 * String I/O in reverse. Yuck. Kill the guest, fix later.
1867 */
1868 printk(KERN_ERR "kvm: guest string pio down\n");
1869 inject_gp(vcpu);
1870 return 1;
1871 }
1872 vcpu->run->io.count = now;
1873 vcpu->pio.cur_count = now;
1874
1875 for (i = 0; i < nr_pages; ++i) {
1876 spin_lock(&vcpu->kvm->lock);
1877 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1878 if (page)
1879 get_page(page);
1880 vcpu->pio.guest_pages[i] = page;
1881 spin_unlock(&vcpu->kvm->lock);
1882 if (!page) {
1883 inject_gp(vcpu);
1884 free_pio_guest_pages(vcpu);
1885 return 1;
1886 }
1887 }
1888
Eddie Dong65619eb2007-07-17 11:52:33 +03001889 if (!vcpu->pio.in) {
1890 /* string PIO write */
1891 ret = pio_copy_data(vcpu);
1892 if (ret >= 0 && pio_dev) {
1893 pio_string_write(pio_dev, vcpu);
1894 complete_pio(vcpu);
1895 if (vcpu->pio.count == 0)
1896 ret = 1;
1897 }
1898 } else if (pio_dev)
1899 printk(KERN_ERR "no string pio read support yet, "
1900 "port %x size %d count %ld\n",
1901 port, size, count);
1902
1903 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001904}
1905EXPORT_SYMBOL_GPL(kvm_setup_pio);
1906
Avi Kivitybccf2152007-02-21 18:04:26 +02001907static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001910 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911
Avi Kivitybccf2152007-02-21 18:04:26 +02001912 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913
Avi Kivity1961d272007-03-05 19:46:05 +02001914 if (vcpu->sigset_active)
1915 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1916
Dor Laor54810342007-02-12 00:54:39 -08001917 /* re-sync apic's tpr */
1918 vcpu->cr8 = kvm_run->cr8;
1919
Avi Kivity02c83202007-04-29 15:02:17 +03001920 if (vcpu->pio.cur_count) {
1921 r = complete_pio(vcpu);
1922 if (r)
1923 goto out;
1924 }
1925
1926 if (vcpu->mmio_needed) {
1927 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1928 vcpu->mmio_read_completed = 1;
1929 vcpu->mmio_needed = 0;
1930 r = emulate_instruction(vcpu, kvm_run,
1931 vcpu->mmio_fault_cr2, 0);
1932 if (r == EMULATE_DO_MMIO) {
1933 /*
1934 * Read-modify-write. Back to userspace.
1935 */
1936 kvm_run->exit_reason = KVM_EXIT_MMIO;
1937 r = 0;
1938 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001939 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 }
1941
Avi Kivity8eb7d332007-03-04 14:17:08 +02001942 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001943 kvm_arch_ops->cache_regs(vcpu);
1944 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1945 kvm_arch_ops->decache_regs(vcpu);
1946 }
1947
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 r = kvm_arch_ops->run(vcpu, kvm_run);
1949
Avi Kivity039576c2007-03-20 12:46:50 +02001950out:
Avi Kivity1961d272007-03-05 19:46:05 +02001951 if (vcpu->sigset_active)
1952 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1953
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 vcpu_put(vcpu);
1955 return r;
1956}
1957
Avi Kivitybccf2152007-02-21 18:04:26 +02001958static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1959 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960{
Avi Kivitybccf2152007-02-21 18:04:26 +02001961 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962
1963 kvm_arch_ops->cache_regs(vcpu);
1964
1965 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1966 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1967 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1968 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1969 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1970 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1971 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1972 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001973#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1975 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1976 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1977 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1978 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1979 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1980 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1981 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1982#endif
1983
1984 regs->rip = vcpu->rip;
1985 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1986
1987 /*
1988 * Don't leak debug flags in case they were set for guest debugging
1989 */
1990 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1991 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1992
1993 vcpu_put(vcpu);
1994
1995 return 0;
1996}
1997
Avi Kivitybccf2152007-02-21 18:04:26 +02001998static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1999 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000{
Avi Kivitybccf2152007-02-21 18:04:26 +02002001 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002
2003 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2004 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2005 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2006 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2007 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2008 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2009 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2010 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002011#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2013 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2014 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2015 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2016 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2017 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2018 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2019 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2020#endif
2021
2022 vcpu->rip = regs->rip;
2023 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2024
2025 kvm_arch_ops->decache_regs(vcpu);
2026
2027 vcpu_put(vcpu);
2028
2029 return 0;
2030}
2031
2032static void get_segment(struct kvm_vcpu *vcpu,
2033 struct kvm_segment *var, int seg)
2034{
2035 return kvm_arch_ops->get_segment(vcpu, var, seg);
2036}
2037
Avi Kivitybccf2152007-02-21 18:04:26 +02002038static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2039 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 struct descriptor_table dt;
2042
Avi Kivitybccf2152007-02-21 18:04:26 +02002043 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044
2045 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2046 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2047 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2048 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2049 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2050 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2051
2052 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2053 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2054
2055 kvm_arch_ops->get_idt(vcpu, &dt);
2056 sregs->idt.limit = dt.limit;
2057 sregs->idt.base = dt.base;
2058 kvm_arch_ops->get_gdt(vcpu, &dt);
2059 sregs->gdt.limit = dt.limit;
2060 sregs->gdt.base = dt.base;
2061
Anthony Liguori25c4c272007-04-27 09:29:21 +03002062 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 sregs->cr0 = vcpu->cr0;
2064 sregs->cr2 = vcpu->cr2;
2065 sregs->cr3 = vcpu->cr3;
2066 sregs->cr4 = vcpu->cr4;
2067 sregs->cr8 = vcpu->cr8;
2068 sregs->efer = vcpu->shadow_efer;
2069 sregs->apic_base = vcpu->apic_base;
2070
2071 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2072 sizeof sregs->interrupt_bitmap);
2073
2074 vcpu_put(vcpu);
2075
2076 return 0;
2077}
2078
2079static void set_segment(struct kvm_vcpu *vcpu,
2080 struct kvm_segment *var, int seg)
2081{
2082 return kvm_arch_ops->set_segment(vcpu, var, seg);
2083}
2084
Avi Kivitybccf2152007-02-21 18:04:26 +02002085static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2086 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 int mmu_reset_needed = 0;
2089 int i;
2090 struct descriptor_table dt;
2091
Avi Kivitybccf2152007-02-21 18:04:26 +02002092 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 dt.limit = sregs->idt.limit;
2095 dt.base = sregs->idt.base;
2096 kvm_arch_ops->set_idt(vcpu, &dt);
2097 dt.limit = sregs->gdt.limit;
2098 dt.base = sregs->gdt.base;
2099 kvm_arch_ops->set_gdt(vcpu, &dt);
2100
2101 vcpu->cr2 = sregs->cr2;
2102 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2103 vcpu->cr3 = sregs->cr3;
2104
2105 vcpu->cr8 = sregs->cr8;
2106
2107 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002108#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2110#endif
2111 vcpu->apic_base = sregs->apic_base;
2112
Anthony Liguori25c4c272007-04-27 09:29:21 +03002113 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002114
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002116 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
2118 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2119 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002120 if (!is_long_mode(vcpu) && is_pae(vcpu))
2121 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
2123 if (mmu_reset_needed)
2124 kvm_mmu_reset_context(vcpu);
2125
2126 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2127 sizeof vcpu->irq_pending);
2128 vcpu->irq_summary = 0;
2129 for (i = 0; i < NR_IRQ_WORDS; ++i)
2130 if (vcpu->irq_pending[i])
2131 __set_bit(i, &vcpu->irq_summary);
2132
Avi Kivity024aa1c2007-03-21 13:44:58 +02002133 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2134 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2135 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2136 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2137 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2138 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2139
2140 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2141 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2142
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 vcpu_put(vcpu);
2144
2145 return 0;
2146}
2147
2148/*
2149 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2150 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002151 *
2152 * This list is modified at module load time to reflect the
2153 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 */
2155static u32 msrs_to_save[] = {
2156 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2157 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002158#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2160#endif
2161 MSR_IA32_TIME_STAMP_COUNTER,
2162};
2163
Michael Riepebf591b22006-12-22 01:05:36 -08002164static unsigned num_msrs_to_save;
2165
Avi Kivity6f00e682007-01-26 00:56:40 -08002166static u32 emulated_msrs[] = {
2167 MSR_IA32_MISC_ENABLE,
2168};
2169
Michael Riepebf591b22006-12-22 01:05:36 -08002170static __init void kvm_init_msr_list(void)
2171{
2172 u32 dummy[2];
2173 unsigned i, j;
2174
2175 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2176 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2177 continue;
2178 if (j < i)
2179 msrs_to_save[j] = msrs_to_save[i];
2180 j++;
2181 }
2182 num_msrs_to_save = j;
2183}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184
2185/*
2186 * Adapt set_msr() to msr_io()'s calling convention
2187 */
2188static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2189{
Avi Kivity35f3f282007-07-17 14:20:30 +03002190 return kvm_set_msr(vcpu, index, *data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191}
2192
2193/*
2194 * Read or write a bunch of msrs. All parameters are kernel addresses.
2195 *
2196 * @return number of msrs set successfully.
2197 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002198static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 struct kvm_msr_entry *entries,
2200 int (*do_msr)(struct kvm_vcpu *vcpu,
2201 unsigned index, u64 *data))
2202{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 int i;
2204
Avi Kivitybccf2152007-02-21 18:04:26 +02002205 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206
2207 for (i = 0; i < msrs->nmsrs; ++i)
2208 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2209 break;
2210
2211 vcpu_put(vcpu);
2212
2213 return i;
2214}
2215
2216/*
2217 * Read or write a bunch of msrs. Parameters are user addresses.
2218 *
2219 * @return number of msrs set successfully.
2220 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002221static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222 int (*do_msr)(struct kvm_vcpu *vcpu,
2223 unsigned index, u64 *data),
2224 int writeback)
2225{
2226 struct kvm_msrs msrs;
2227 struct kvm_msr_entry *entries;
2228 int r, n;
2229 unsigned size;
2230
2231 r = -EFAULT;
2232 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2233 goto out;
2234
2235 r = -E2BIG;
2236 if (msrs.nmsrs >= MAX_IO_MSRS)
2237 goto out;
2238
2239 r = -ENOMEM;
2240 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2241 entries = vmalloc(size);
2242 if (!entries)
2243 goto out;
2244
2245 r = -EFAULT;
2246 if (copy_from_user(entries, user_msrs->entries, size))
2247 goto out_free;
2248
Avi Kivitybccf2152007-02-21 18:04:26 +02002249 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 if (r < 0)
2251 goto out_free;
2252
2253 r = -EFAULT;
2254 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2255 goto out_free;
2256
2257 r = n;
2258
2259out_free:
2260 vfree(entries);
2261out:
2262 return r;
2263}
2264
2265/*
2266 * Translate a guest virtual address to a guest physical address.
2267 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002268static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2269 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270{
2271 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 gpa_t gpa;
2273
Avi Kivitybccf2152007-02-21 18:04:26 +02002274 vcpu_load(vcpu);
2275 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2277 tr->physical_address = gpa;
2278 tr->valid = gpa != UNMAPPED_GVA;
2279 tr->writeable = 1;
2280 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002281 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 vcpu_put(vcpu);
2283
2284 return 0;
2285}
2286
Avi Kivitybccf2152007-02-21 18:04:26 +02002287static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2288 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 if (irq->irq < 0 || irq->irq >= 256)
2291 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002292 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293
2294 set_bit(irq->irq, vcpu->irq_pending);
2295 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2296
2297 vcpu_put(vcpu);
2298
2299 return 0;
2300}
2301
Avi Kivitybccf2152007-02-21 18:04:26 +02002302static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2303 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 int r;
2306
Avi Kivitybccf2152007-02-21 18:04:26 +02002307 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
2309 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2310
2311 vcpu_put(vcpu);
2312
2313 return r;
2314}
2315
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002316static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2317 unsigned long address,
2318 int *type)
2319{
2320 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2321 unsigned long pgoff;
2322 struct page *page;
2323
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002324 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002325 if (pgoff == 0)
2326 page = virt_to_page(vcpu->run);
2327 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2328 page = virt_to_page(vcpu->pio_data);
2329 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002330 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002331 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002332 if (type != NULL)
2333 *type = VM_FAULT_MINOR;
2334
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002335 return page;
2336}
2337
2338static struct vm_operations_struct kvm_vcpu_vm_ops = {
2339 .nopage = kvm_vcpu_nopage,
2340};
2341
2342static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2343{
2344 vma->vm_ops = &kvm_vcpu_vm_ops;
2345 return 0;
2346}
2347
Avi Kivitybccf2152007-02-21 18:04:26 +02002348static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2349{
2350 struct kvm_vcpu *vcpu = filp->private_data;
2351
2352 fput(vcpu->kvm->filp);
2353 return 0;
2354}
2355
2356static struct file_operations kvm_vcpu_fops = {
2357 .release = kvm_vcpu_release,
2358 .unlocked_ioctl = kvm_vcpu_ioctl,
2359 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002360 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002361};
2362
2363/*
2364 * Allocates an inode for the vcpu.
2365 */
2366static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2367{
2368 int fd, r;
2369 struct inode *inode;
2370 struct file *file;
2371
Avi Kivityd6d28162007-06-28 08:38:16 -04002372 r = anon_inode_getfd(&fd, &inode, &file,
2373 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2374 if (r)
2375 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002376 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002377 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002378}
2379
Avi Kivityc5ea7662007-02-20 18:41:05 +02002380/*
2381 * Creates some virtual cpus. Good luck creating more than one.
2382 */
2383static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2384{
2385 int r;
2386 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002387 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002388
2389 r = -EINVAL;
2390 if (!valid_vcpu(n))
2391 goto out;
2392
2393 vcpu = &kvm->vcpus[n];
Qing Hedad37952007-07-12 12:33:56 +03002394 vcpu->vcpu_id = n;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002395
2396 mutex_lock(&vcpu->mutex);
2397
2398 if (vcpu->vmcs) {
2399 mutex_unlock(&vcpu->mutex);
2400 return -EEXIST;
2401 }
2402
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002403 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2404 r = -ENOMEM;
2405 if (!page)
2406 goto out_unlock;
2407 vcpu->run = page_address(page);
2408
Avi Kivity039576c2007-03-20 12:46:50 +02002409 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2410 r = -ENOMEM;
2411 if (!page)
2412 goto out_free_run;
2413 vcpu->pio_data = page_address(page);
2414
Avi Kivityc5ea7662007-02-20 18:41:05 +02002415 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2416 FX_IMAGE_ALIGN);
2417 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
Avi Kivityd917a6b2007-04-12 13:03:01 +03002418 vcpu->cr0 = 0x10;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002419
2420 r = kvm_arch_ops->vcpu_create(vcpu);
2421 if (r < 0)
2422 goto out_free_vcpus;
2423
2424 r = kvm_mmu_create(vcpu);
2425 if (r < 0)
2426 goto out_free_vcpus;
2427
2428 kvm_arch_ops->vcpu_load(vcpu);
2429 r = kvm_mmu_setup(vcpu);
2430 if (r >= 0)
2431 r = kvm_arch_ops->vcpu_setup(vcpu);
2432 vcpu_put(vcpu);
2433
2434 if (r < 0)
2435 goto out_free_vcpus;
2436
Avi Kivitybccf2152007-02-21 18:04:26 +02002437 r = create_vcpu_fd(vcpu);
2438 if (r < 0)
2439 goto out_free_vcpus;
2440
Avi Kivity39c3b862007-06-07 19:11:53 +03002441 spin_lock(&kvm_lock);
2442 if (n >= kvm->nvcpus)
2443 kvm->nvcpus = n + 1;
2444 spin_unlock(&kvm_lock);
2445
Avi Kivitybccf2152007-02-21 18:04:26 +02002446 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002447
2448out_free_vcpus:
2449 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002450out_free_run:
2451 free_page((unsigned long)vcpu->run);
2452 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002453out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002454 mutex_unlock(&vcpu->mutex);
2455out:
2456 return r;
2457}
2458
Eddie Dong2cc51562007-05-21 07:28:09 +03002459static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2460{
2461 u64 efer;
2462 int i;
2463 struct kvm_cpuid_entry *e, *entry;
2464
2465 rdmsrl(MSR_EFER, efer);
2466 entry = NULL;
2467 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2468 e = &vcpu->cpuid_entries[i];
2469 if (e->function == 0x80000001) {
2470 entry = e;
2471 break;
2472 }
2473 }
Avi Kivity4c981b42007-07-25 09:22:12 +03002474 if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
Eddie Dong2cc51562007-05-21 07:28:09 +03002475 entry->edx &= ~(1 << 20);
Avi Kivity4c981b42007-07-25 09:22:12 +03002476 printk(KERN_INFO "kvm: guest NX capability removed\n");
Eddie Dong2cc51562007-05-21 07:28:09 +03002477 }
2478}
2479
Avi Kivity06465c52007-02-28 20:46:53 +02002480static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2481 struct kvm_cpuid *cpuid,
2482 struct kvm_cpuid_entry __user *entries)
2483{
2484 int r;
2485
2486 r = -E2BIG;
2487 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2488 goto out;
2489 r = -EFAULT;
2490 if (copy_from_user(&vcpu->cpuid_entries, entries,
2491 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2492 goto out;
2493 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002494 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002495 return 0;
2496
2497out:
2498 return r;
2499}
2500
Avi Kivity1961d272007-03-05 19:46:05 +02002501static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2502{
2503 if (sigset) {
2504 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2505 vcpu->sigset_active = 1;
2506 vcpu->sigset = *sigset;
2507 } else
2508 vcpu->sigset_active = 0;
2509 return 0;
2510}
2511
Avi Kivityb8836732007-04-01 16:34:31 +03002512/*
2513 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2514 * we have asm/x86/processor.h
2515 */
2516struct fxsave {
2517 u16 cwd;
2518 u16 swd;
2519 u16 twd;
2520 u16 fop;
2521 u64 rip;
2522 u64 rdp;
2523 u32 mxcsr;
2524 u32 mxcsr_mask;
2525 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2526#ifdef CONFIG_X86_64
2527 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2528#else
2529 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2530#endif
2531};
2532
2533static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2534{
2535 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2536
2537 vcpu_load(vcpu);
2538
2539 memcpy(fpu->fpr, fxsave->st_space, 128);
2540 fpu->fcw = fxsave->cwd;
2541 fpu->fsw = fxsave->swd;
2542 fpu->ftwx = fxsave->twd;
2543 fpu->last_opcode = fxsave->fop;
2544 fpu->last_ip = fxsave->rip;
2545 fpu->last_dp = fxsave->rdp;
2546 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2547
2548 vcpu_put(vcpu);
2549
2550 return 0;
2551}
2552
2553static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2554{
2555 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2556
2557 vcpu_load(vcpu);
2558
2559 memcpy(fxsave->st_space, fpu->fpr, 128);
2560 fxsave->cwd = fpu->fcw;
2561 fxsave->swd = fpu->fsw;
2562 fxsave->twd = fpu->ftwx;
2563 fxsave->fop = fpu->last_opcode;
2564 fxsave->rip = fpu->last_ip;
2565 fxsave->rdp = fpu->last_dp;
2566 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2567
2568 vcpu_put(vcpu);
2569
2570 return 0;
2571}
2572
Avi Kivitybccf2152007-02-21 18:04:26 +02002573static long kvm_vcpu_ioctl(struct file *filp,
2574 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575{
Avi Kivitybccf2152007-02-21 18:04:26 +02002576 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002577 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 int r = -EINVAL;
2579
2580 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002581 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002582 r = -EINVAL;
2583 if (arg)
2584 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002585 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 case KVM_GET_REGS: {
2588 struct kvm_regs kvm_regs;
2589
Avi Kivitybccf2152007-02-21 18:04:26 +02002590 memset(&kvm_regs, 0, sizeof kvm_regs);
2591 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 if (r)
2593 goto out;
2594 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002595 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 goto out;
2597 r = 0;
2598 break;
2599 }
2600 case KVM_SET_REGS: {
2601 struct kvm_regs kvm_regs;
2602
2603 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002604 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002606 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 if (r)
2608 goto out;
2609 r = 0;
2610 break;
2611 }
2612 case KVM_GET_SREGS: {
2613 struct kvm_sregs kvm_sregs;
2614
Avi Kivitybccf2152007-02-21 18:04:26 +02002615 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2616 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617 if (r)
2618 goto out;
2619 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002620 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 goto out;
2622 r = 0;
2623 break;
2624 }
2625 case KVM_SET_SREGS: {
2626 struct kvm_sregs kvm_sregs;
2627
2628 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002629 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002631 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632 if (r)
2633 goto out;
2634 r = 0;
2635 break;
2636 }
2637 case KVM_TRANSLATE: {
2638 struct kvm_translation tr;
2639
2640 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002641 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002643 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 if (r)
2645 goto out;
2646 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002647 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 goto out;
2649 r = 0;
2650 break;
2651 }
2652 case KVM_INTERRUPT: {
2653 struct kvm_interrupt irq;
2654
2655 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002656 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002658 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 if (r)
2660 goto out;
2661 r = 0;
2662 break;
2663 }
2664 case KVM_DEBUG_GUEST: {
2665 struct kvm_debug_guest dbg;
2666
2667 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002668 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002670 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 if (r)
2672 goto out;
2673 r = 0;
2674 break;
2675 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002676 case KVM_GET_MSRS:
Avi Kivity35f3f282007-07-17 14:20:30 +03002677 r = msr_io(vcpu, argp, kvm_get_msr, 1);
Avi Kivitybccf2152007-02-21 18:04:26 +02002678 break;
2679 case KVM_SET_MSRS:
2680 r = msr_io(vcpu, argp, do_set_msr, 0);
2681 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002682 case KVM_SET_CPUID: {
2683 struct kvm_cpuid __user *cpuid_arg = argp;
2684 struct kvm_cpuid cpuid;
2685
2686 r = -EFAULT;
2687 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2688 goto out;
2689 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2690 if (r)
2691 goto out;
2692 break;
2693 }
Avi Kivity1961d272007-03-05 19:46:05 +02002694 case KVM_SET_SIGNAL_MASK: {
2695 struct kvm_signal_mask __user *sigmask_arg = argp;
2696 struct kvm_signal_mask kvm_sigmask;
2697 sigset_t sigset, *p;
2698
2699 p = NULL;
2700 if (argp) {
2701 r = -EFAULT;
2702 if (copy_from_user(&kvm_sigmask, argp,
2703 sizeof kvm_sigmask))
2704 goto out;
2705 r = -EINVAL;
2706 if (kvm_sigmask.len != sizeof sigset)
2707 goto out;
2708 r = -EFAULT;
2709 if (copy_from_user(&sigset, sigmask_arg->sigset,
2710 sizeof sigset))
2711 goto out;
2712 p = &sigset;
2713 }
2714 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2715 break;
2716 }
Avi Kivityb8836732007-04-01 16:34:31 +03002717 case KVM_GET_FPU: {
2718 struct kvm_fpu fpu;
2719
2720 memset(&fpu, 0, sizeof fpu);
2721 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2722 if (r)
2723 goto out;
2724 r = -EFAULT;
2725 if (copy_to_user(argp, &fpu, sizeof fpu))
2726 goto out;
2727 r = 0;
2728 break;
2729 }
2730 case KVM_SET_FPU: {
2731 struct kvm_fpu fpu;
2732
2733 r = -EFAULT;
2734 if (copy_from_user(&fpu, argp, sizeof fpu))
2735 goto out;
2736 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2737 if (r)
2738 goto out;
2739 r = 0;
2740 break;
2741 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002742 default:
2743 ;
2744 }
2745out:
2746 return r;
2747}
2748
2749static long kvm_vm_ioctl(struct file *filp,
2750 unsigned int ioctl, unsigned long arg)
2751{
2752 struct kvm *kvm = filp->private_data;
2753 void __user *argp = (void __user *)arg;
2754 int r = -EINVAL;
2755
2756 switch (ioctl) {
2757 case KVM_CREATE_VCPU:
2758 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2759 if (r < 0)
2760 goto out;
2761 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 case KVM_SET_MEMORY_REGION: {
2763 struct kvm_memory_region kvm_mem;
2764
2765 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002766 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002768 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 if (r)
2770 goto out;
2771 break;
2772 }
2773 case KVM_GET_DIRTY_LOG: {
2774 struct kvm_dirty_log log;
2775
2776 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002777 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002779 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 if (r)
2781 goto out;
2782 break;
2783 }
Avi Kivitye8207542007-03-30 16:54:30 +03002784 case KVM_SET_MEMORY_ALIAS: {
2785 struct kvm_memory_alias alias;
2786
2787 r = -EFAULT;
2788 if (copy_from_user(&alias, argp, sizeof alias))
2789 goto out;
2790 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2791 if (r)
2792 goto out;
2793 break;
2794 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002795 default:
2796 ;
2797 }
2798out:
2799 return r;
2800}
2801
2802static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2803 unsigned long address,
2804 int *type)
2805{
2806 struct kvm *kvm = vma->vm_file->private_data;
2807 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002808 struct page *page;
2809
Avi Kivityf17abe92007-02-21 19:28:04 +02002810 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002811 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002812 if (!page)
2813 return NOPAGE_SIGBUS;
2814 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002815 if (type != NULL)
2816 *type = VM_FAULT_MINOR;
2817
Avi Kivityf17abe92007-02-21 19:28:04 +02002818 return page;
2819}
2820
2821static struct vm_operations_struct kvm_vm_vm_ops = {
2822 .nopage = kvm_vm_nopage,
2823};
2824
2825static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2826{
2827 vma->vm_ops = &kvm_vm_vm_ops;
2828 return 0;
2829}
2830
2831static struct file_operations kvm_vm_fops = {
2832 .release = kvm_vm_release,
2833 .unlocked_ioctl = kvm_vm_ioctl,
2834 .compat_ioctl = kvm_vm_ioctl,
2835 .mmap = kvm_vm_mmap,
2836};
2837
2838static int kvm_dev_ioctl_create_vm(void)
2839{
2840 int fd, r;
2841 struct inode *inode;
2842 struct file *file;
2843 struct kvm *kvm;
2844
Avi Kivityf17abe92007-02-21 19:28:04 +02002845 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002846 if (IS_ERR(kvm))
2847 return PTR_ERR(kvm);
2848 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2849 if (r) {
2850 kvm_destroy_vm(kvm);
2851 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002852 }
2853
Avi Kivitybccf2152007-02-21 18:04:26 +02002854 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002855
Avi Kivityf17abe92007-02-21 19:28:04 +02002856 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002857}
2858
2859static long kvm_dev_ioctl(struct file *filp,
2860 unsigned int ioctl, unsigned long arg)
2861{
2862 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002863 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002864
2865 switch (ioctl) {
2866 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002867 r = -EINVAL;
2868 if (arg)
2869 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002870 r = KVM_API_VERSION;
2871 break;
2872 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002873 r = -EINVAL;
2874 if (arg)
2875 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002876 r = kvm_dev_ioctl_create_vm();
2877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002879 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 struct kvm_msr_list msr_list;
2881 unsigned n;
2882
2883 r = -EFAULT;
2884 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2885 goto out;
2886 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002887 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2889 goto out;
2890 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002891 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 goto out;
2893 r = -EFAULT;
2894 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002895 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002897 if (copy_to_user(user_msr_list->indices
2898 + num_msrs_to_save * sizeof(u32),
2899 &emulated_msrs,
2900 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2901 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002903 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002905 case KVM_CHECK_EXTENSION:
2906 /*
2907 * No extensions defined at present.
2908 */
2909 r = 0;
2910 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002911 case KVM_GET_VCPU_MMAP_SIZE:
2912 r = -EINVAL;
2913 if (arg)
2914 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002915 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002916 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 default:
2918 ;
2919 }
2920out:
2921 return r;
2922}
2923
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924static struct file_operations kvm_chardev_ops = {
2925 .open = kvm_dev_open,
2926 .release = kvm_dev_release,
2927 .unlocked_ioctl = kvm_dev_ioctl,
2928 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929};
2930
2931static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002932 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 "kvm",
2934 &kvm_chardev_ops,
2935};
2936
Avi Kivity774c47f2007-02-12 00:54:47 -08002937/*
2938 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2939 * cached on it.
2940 */
2941static void decache_vcpus_on_cpu(int cpu)
2942{
2943 struct kvm *vm;
2944 struct kvm_vcpu *vcpu;
2945 int i;
2946
2947 spin_lock(&kvm_lock);
2948 list_for_each_entry(vm, &vm_list, vm_list)
2949 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2950 vcpu = &vm->vcpus[i];
2951 /*
2952 * If the vcpu is locked, then it is running on some
2953 * other cpu and therefore it is not cached on the
2954 * cpu in question.
2955 *
2956 * If it's not locked, check the last cpu it executed
2957 * on.
2958 */
2959 if (mutex_trylock(&vcpu->mutex)) {
2960 if (vcpu->cpu == cpu) {
2961 kvm_arch_ops->vcpu_decache(vcpu);
2962 vcpu->cpu = -1;
2963 }
2964 mutex_unlock(&vcpu->mutex);
2965 }
2966 }
2967 spin_unlock(&kvm_lock);
2968}
2969
Avi Kivity1b6c0162007-05-24 13:03:52 +03002970static void hardware_enable(void *junk)
2971{
2972 int cpu = raw_smp_processor_id();
2973
2974 if (cpu_isset(cpu, cpus_hardware_enabled))
2975 return;
2976 cpu_set(cpu, cpus_hardware_enabled);
2977 kvm_arch_ops->hardware_enable(NULL);
2978}
2979
2980static void hardware_disable(void *junk)
2981{
2982 int cpu = raw_smp_processor_id();
2983
2984 if (!cpu_isset(cpu, cpus_hardware_enabled))
2985 return;
2986 cpu_clear(cpu, cpus_hardware_enabled);
2987 decache_vcpus_on_cpu(cpu);
2988 kvm_arch_ops->hardware_disable(NULL);
2989}
2990
Avi Kivity774c47f2007-02-12 00:54:47 -08002991static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2992 void *v)
2993{
2994 int cpu = (long)v;
2995
2996 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002997 case CPU_DYING:
2998 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002999 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3000 cpu);
3001 hardware_disable(NULL);
3002 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003003 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003004 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003005 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3006 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003007 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003008 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003009 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003010 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003011 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3012 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003013 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003014 break;
3015 }
3016 return NOTIFY_OK;
3017}
3018
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003019static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3020 void *v)
3021{
3022 if (val == SYS_RESTART) {
3023 /*
3024 * Some (well, at least mine) BIOSes hang on reboot if
3025 * in vmx root mode.
3026 */
3027 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3028 on_each_cpu(hardware_disable, NULL, 0, 1);
3029 }
3030 return NOTIFY_OK;
3031}
3032
3033static struct notifier_block kvm_reboot_notifier = {
3034 .notifier_call = kvm_reboot,
3035 .priority = 0,
3036};
3037
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003038void kvm_io_bus_init(struct kvm_io_bus *bus)
3039{
3040 memset(bus, 0, sizeof(*bus));
3041}
3042
3043void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3044{
3045 int i;
3046
3047 for (i = 0; i < bus->dev_count; i++) {
3048 struct kvm_io_device *pos = bus->devs[i];
3049
3050 kvm_iodevice_destructor(pos);
3051 }
3052}
3053
3054struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3055{
3056 int i;
3057
3058 for (i = 0; i < bus->dev_count; i++) {
3059 struct kvm_io_device *pos = bus->devs[i];
3060
3061 if (pos->in_range(pos, addr))
3062 return pos;
3063 }
3064
3065 return NULL;
3066}
3067
3068void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3069{
3070 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3071
3072 bus->devs[bus->dev_count++] = dev;
3073}
3074
Avi Kivity774c47f2007-02-12 00:54:47 -08003075static struct notifier_block kvm_cpu_notifier = {
3076 .notifier_call = kvm_cpu_hotplug,
3077 .priority = 20, /* must be > scheduler priority */
3078};
3079
Avi Kivity1165f5f2007-04-19 17:27:43 +03003080static u64 stat_get(void *_offset)
3081{
3082 unsigned offset = (long)_offset;
3083 u64 total = 0;
3084 struct kvm *kvm;
3085 struct kvm_vcpu *vcpu;
3086 int i;
3087
3088 spin_lock(&kvm_lock);
3089 list_for_each_entry(kvm, &vm_list, vm_list)
3090 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3091 vcpu = &kvm->vcpus[i];
3092 total += *(u32 *)((void *)vcpu + offset);
3093 }
3094 spin_unlock(&kvm_lock);
3095 return total;
3096}
3097
3098static void stat_set(void *offset, u64 val)
3099{
3100}
3101
3102DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3103
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104static __init void kvm_init_debug(void)
3105{
3106 struct kvm_stats_debugfs_item *p;
3107
Al Viro8b6d44c2007-02-09 16:38:40 +00003108 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003110 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3111 (void *)(long)p->offset,
3112 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113}
3114
3115static void kvm_exit_debug(void)
3116{
3117 struct kvm_stats_debugfs_item *p;
3118
3119 for (p = debugfs_entries; p->name; ++p)
3120 debugfs_remove(p->dentry);
3121 debugfs_remove(debugfs_dir);
3122}
3123
Avi Kivity59ae6c62007-02-12 00:54:48 -08003124static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3125{
Avi Kivity4267c412007-05-24 13:09:41 +03003126 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003127 return 0;
3128}
3129
3130static int kvm_resume(struct sys_device *dev)
3131{
Avi Kivity4267c412007-05-24 13:09:41 +03003132 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003133 return 0;
3134}
3135
3136static struct sysdev_class kvm_sysdev_class = {
3137 set_kset_name("kvm"),
3138 .suspend = kvm_suspend,
3139 .resume = kvm_resume,
3140};
3141
3142static struct sys_device kvm_sysdev = {
3143 .id = 0,
3144 .cls = &kvm_sysdev_class,
3145};
3146
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147hpa_t bad_page_address;
3148
3149int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3150{
3151 int r;
3152
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003153 if (kvm_arch_ops) {
3154 printk(KERN_ERR "kvm: already loaded the other module\n");
3155 return -EEXIST;
3156 }
3157
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003158 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 printk(KERN_ERR "kvm: no hardware support\n");
3160 return -EOPNOTSUPP;
3161 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003162 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 printk(KERN_ERR "kvm: disabled by bios\n");
3164 return -EOPNOTSUPP;
3165 }
3166
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003167 kvm_arch_ops = ops;
3168
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 r = kvm_arch_ops->hardware_setup();
3170 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003171 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172
Avi Kivity1b6c0162007-05-24 13:03:52 +03003173 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003174 r = register_cpu_notifier(&kvm_cpu_notifier);
3175 if (r)
3176 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 register_reboot_notifier(&kvm_reboot_notifier);
3178
Avi Kivity59ae6c62007-02-12 00:54:48 -08003179 r = sysdev_class_register(&kvm_sysdev_class);
3180 if (r)
3181 goto out_free_2;
3182
3183 r = sysdev_register(&kvm_sysdev);
3184 if (r)
3185 goto out_free_3;
3186
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187 kvm_chardev_ops.owner = module;
3188
3189 r = misc_register(&kvm_dev);
3190 if (r) {
3191 printk (KERN_ERR "kvm: misc device register failed\n");
3192 goto out_free;
3193 }
3194
3195 return r;
3196
3197out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003198 sysdev_unregister(&kvm_sysdev);
3199out_free_3:
3200 sysdev_class_unregister(&kvm_sysdev_class);
3201out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003203 unregister_cpu_notifier(&kvm_cpu_notifier);
3204out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003205 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003207out:
3208 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 return r;
3210}
3211
3212void kvm_exit_arch(void)
3213{
3214 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003215 sysdev_unregister(&kvm_sysdev);
3216 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003218 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003219 on_each_cpu(hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003221 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222}
3223
3224static __init int kvm_init(void)
3225{
3226 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003227 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228
Avi Kivityb5a33a72007-04-15 16:31:09 +03003229 r = kvm_mmu_module_init();
3230 if (r)
3231 goto out4;
3232
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233 kvm_init_debug();
3234
Michael Riepebf591b22006-12-22 01:05:36 -08003235 kvm_init_msr_list();
3236
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3238 r = -ENOMEM;
3239 goto out;
3240 }
3241
3242 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3243 memset(__va(bad_page_address), 0, PAGE_SIZE);
3244
Avi Kivity58e690e2007-02-26 16:29:43 +02003245 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246
3247out:
3248 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003249 kvm_mmu_module_exit();
3250out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 return r;
3252}
3253
3254static __exit void kvm_exit(void)
3255{
3256 kvm_exit_debug();
3257 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivityb5a33a72007-04-15 16:31:09 +03003258 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259}
3260
3261module_init(kvm_init)
3262module_exit(kvm_exit)
3263
3264EXPORT_SYMBOL_GPL(kvm_init_arch);
3265EXPORT_SYMBOL_GPL(kvm_exit_arch);