blob: af07cd539bbac481af799d8108352924c75b8647 [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"
19
20#include <linux/kvm.h>
21#include <linux/module.h>
22#include <linux/errno.h>
Andrew Mortone9cdb1e2007-03-01 11:28:13 +020023#include <linux/magic.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <asm/processor.h>
25#include <linux/percpu.h>
26#include <linux/gfp.h>
27#include <asm/msr.h>
28#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
31#include <asm/uaccess.h>
32#include <linux/reboot.h>
33#include <asm/io.h>
34#include <linux/debugfs.h>
35#include <linux/highmem.h>
36#include <linux/file.h>
37#include <asm/desc.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080038#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080039#include <linux/cpu.h>
Avi Kivityf17abe92007-02-21 19:28:04 +020040#include <linux/file.h>
Avi Kivity37e29d92007-02-20 14:07:37 +020041#include <linux/fs.h>
42#include <linux/mount.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040043#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
45#include "x86_emulate.h"
46#include "segment_descriptor.h"
47
48MODULE_AUTHOR("Qumranet");
49MODULE_LICENSE("GPL");
50
Avi Kivity133de902007-02-12 00:54:44 -080051static DEFINE_SPINLOCK(kvm_lock);
52static LIST_HEAD(vm_list);
53
Avi Kivity6aa8b732006-12-10 02:21:36 -080054struct kvm_arch_ops *kvm_arch_ops;
Avi Kivity1165f5f2007-04-19 17:27:43 +030055
56#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
58static struct kvm_stats_debugfs_item {
59 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030060 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080061 struct dentry *dentry;
62} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030063 { "pf_fixed", STAT_OFFSET(pf_fixed) },
64 { "pf_guest", STAT_OFFSET(pf_guest) },
65 { "tlb_flush", STAT_OFFSET(tlb_flush) },
66 { "invlpg", STAT_OFFSET(invlpg) },
67 { "exits", STAT_OFFSET(exits) },
68 { "io_exits", STAT_OFFSET(io_exits) },
69 { "mmio_exits", STAT_OFFSET(mmio_exits) },
70 { "signal_exits", STAT_OFFSET(signal_exits) },
71 { "irq_window", STAT_OFFSET(irq_window_exits) },
72 { "halt_exits", STAT_OFFSET(halt_exits) },
73 { "request_irq", STAT_OFFSET(request_irq_exits) },
74 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030075 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030076 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030077 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080078};
79
80static struct dentry *debugfs_dir;
81
Avi Kivity37e29d92007-02-20 14:07:37 +020082struct vfsmount *kvmfs_mnt;
83
Avi Kivity6aa8b732006-12-10 02:21:36 -080084#define MAX_IO_MSRS 256
85
86#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
87#define LMSW_GUEST_MASK 0x0eULL
88#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
89#define CR8_RESEVED_BITS (~0x0fULL)
90#define EFER_RESERVED_BITS 0xfffffffffffff2fe
91
Avi Kivity05b3e0c2006-12-13 00:33:45 -080092#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080093// LDT or TSS descriptor in the GDT. 16 bytes.
94struct segment_descriptor_64 {
95 struct segment_descriptor s;
96 u32 base_higher;
97 u32 pad_zero;
98};
99
100#endif
101
Avi Kivitybccf2152007-02-21 18:04:26 +0200102static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
103 unsigned long arg);
104
Avi Kivityf17abe92007-02-21 19:28:04 +0200105static struct inode *kvmfs_inode(struct file_operations *fops)
106{
107 int error = -ENOMEM;
108 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
109
110 if (!inode)
111 goto eexit_1;
112
113 inode->i_fop = fops;
114
115 /*
116 * Mark the inode dirty from the very beginning,
117 * that way it will never be moved to the dirty
118 * list because mark_inode_dirty() will think
119 * that it already _is_ on the dirty list.
120 */
121 inode->i_state = I_DIRTY;
122 inode->i_mode = S_IRUSR | S_IWUSR;
123 inode->i_uid = current->fsuid;
124 inode->i_gid = current->fsgid;
125 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
126 return inode;
127
128eexit_1:
129 return ERR_PTR(error);
130}
131
132static struct file *kvmfs_file(struct inode *inode, void *private_data)
133{
134 struct file *file = get_empty_filp();
135
136 if (!file)
137 return ERR_PTR(-ENFILE);
138
139 file->f_path.mnt = mntget(kvmfs_mnt);
140 file->f_path.dentry = d_alloc_anon(inode);
141 if (!file->f_path.dentry)
142 return ERR_PTR(-ENOMEM);
143 file->f_mapping = inode->i_mapping;
144
145 file->f_pos = 0;
146 file->f_flags = O_RDWR;
147 file->f_op = inode->i_fop;
148 file->f_mode = FMODE_READ | FMODE_WRITE;
149 file->f_version = 0;
150 file->private_data = private_data;
151 return file;
152}
153
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154unsigned long segment_base(u16 selector)
155{
156 struct descriptor_table gdt;
157 struct segment_descriptor *d;
158 unsigned long table_base;
159 typedef unsigned long ul;
160 unsigned long v;
161
162 if (selector == 0)
163 return 0;
164
165 asm ("sgdt %0" : "=m"(gdt));
166 table_base = gdt.base;
167
168 if (selector & 4) { /* from ldt */
169 u16 ldt_selector;
170
171 asm ("sldt %0" : "=g"(ldt_selector));
172 table_base = segment_base(ldt_selector);
173 }
174 d = (struct segment_descriptor *)(table_base + (selector & ~7));
175 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800176#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177 if (d->system == 0
178 && (d->type == 2 || d->type == 9 || d->type == 11))
179 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
180#endif
181 return v;
182}
183EXPORT_SYMBOL_GPL(segment_base);
184
James Morris5aacf0c2006-12-22 01:04:55 -0800185static inline int valid_vcpu(int n)
186{
187 return likely(n >= 0 && n < KVM_MAX_VCPUS);
188}
189
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200190int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
191 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192{
193 unsigned char *host_buf = dest;
194 unsigned long req_size = size;
195
196 while (size) {
197 hpa_t paddr;
198 unsigned now;
199 unsigned offset;
200 hva_t guest_buf;
201
202 paddr = gva_to_hpa(vcpu, addr);
203
204 if (is_error_hpa(paddr))
205 break;
206
207 guest_buf = (hva_t)kmap_atomic(
208 pfn_to_page(paddr >> PAGE_SHIFT),
209 KM_USER0);
210 offset = addr & ~PAGE_MASK;
211 guest_buf |= offset;
212 now = min(size, PAGE_SIZE - offset);
213 memcpy(host_buf, (void*)guest_buf, now);
214 host_buf += now;
215 addr += now;
216 size -= now;
217 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
218 }
219 return req_size - size;
220}
221EXPORT_SYMBOL_GPL(kvm_read_guest);
222
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200223int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
224 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225{
226 unsigned char *host_buf = data;
227 unsigned long req_size = size;
228
229 while (size) {
230 hpa_t paddr;
231 unsigned now;
232 unsigned offset;
233 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200234 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235
236 paddr = gva_to_hpa(vcpu, addr);
237
238 if (is_error_hpa(paddr))
239 break;
240
Uri Lublinab51a432007-02-21 18:25:21 +0200241 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
242 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 guest_buf = (hva_t)kmap_atomic(
244 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
245 offset = addr & ~PAGE_MASK;
246 guest_buf |= offset;
247 now = min(size, PAGE_SIZE - offset);
248 memcpy((void*)guest_buf, host_buf, now);
249 host_buf += now;
250 addr += now;
251 size -= now;
252 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
253 }
254 return req_size - size;
255}
256EXPORT_SYMBOL_GPL(kvm_write_guest);
257
Avi Kivity7702fd12007-06-14 16:27:40 +0300258void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
259{
260 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
261 return;
262
263 vcpu->guest_fpu_loaded = 1;
264 fx_save(vcpu->host_fx_image);
265 fx_restore(vcpu->guest_fx_image);
266}
267EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
268
269void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
270{
271 if (!vcpu->guest_fpu_loaded)
272 return;
273
274 vcpu->guest_fpu_loaded = 0;
275 fx_save(vcpu->guest_fx_image);
276 fx_restore(vcpu->host_fx_image);
277}
278EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
279
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280/*
281 * Switches to specified vcpu, until a matching vcpu_put()
282 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200283static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284{
Avi Kivitybccf2152007-02-21 18:04:26 +0200285 mutex_lock(&vcpu->mutex);
286 kvm_arch_ops->vcpu_load(vcpu);
287}
288
289/*
290 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
291 * if the slot is not populated.
292 */
293static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
294{
295 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296
297 mutex_lock(&vcpu->mutex);
Avi Kivitybccf2152007-02-21 18:04:26 +0200298 if (!vcpu->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299 mutex_unlock(&vcpu->mutex);
Al Viro8b6d44c2007-02-09 16:38:40 +0000300 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200302 kvm_arch_ops->vcpu_load(vcpu);
303 return vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304}
305
306static void vcpu_put(struct kvm_vcpu *vcpu)
307{
308 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309 mutex_unlock(&vcpu->mutex);
310}
311
Avi Kivityf17abe92007-02-21 19:28:04 +0200312static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313{
314 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
315 int i;
316
317 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200318 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319
320 spin_lock_init(&kvm->lock);
321 INIT_LIST_HEAD(&kvm->active_mmu_pages);
322 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
323 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
324
325 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800326 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800327 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800328 vcpu->mmu.root_hpa = INVALID_PAGE;
329 INIT_LIST_HEAD(&vcpu->free_pages);
Avi Kivity133de902007-02-12 00:54:44 -0800330 spin_lock(&kvm_lock);
331 list_add(&kvm->vm_list, &vm_list);
332 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200334 return kvm;
335}
336
337static int kvm_dev_open(struct inode *inode, struct file *filp)
338{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339 return 0;
340}
341
342/*
343 * Free any memory in @free but not in @dont.
344 */
345static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
346 struct kvm_memory_slot *dont)
347{
348 int i;
349
350 if (!dont || free->phys_mem != dont->phys_mem)
351 if (free->phys_mem) {
352 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800353 if (free->phys_mem[i])
354 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 vfree(free->phys_mem);
356 }
357
358 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
359 vfree(free->dirty_bitmap);
360
Al Viro8b6d44c2007-02-09 16:38:40 +0000361 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000363 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364}
365
366static void kvm_free_physmem(struct kvm *kvm)
367{
368 int i;
369
370 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000371 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372}
373
Avi Kivity039576c2007-03-20 12:46:50 +0200374static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
375{
376 int i;
377
378 for (i = 0; i < 2; ++i)
379 if (vcpu->pio.guest_pages[i]) {
380 __free_page(vcpu->pio.guest_pages[i]);
381 vcpu->pio.guest_pages[i] = NULL;
382 }
383}
384
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
386{
Avi Kivitybccf2152007-02-21 18:04:26 +0200387 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800388 return;
389
Avi Kivitybccf2152007-02-21 18:04:26 +0200390 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800392 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800393 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200394 free_page((unsigned long)vcpu->run);
395 vcpu->run = NULL;
Avi Kivity039576c2007-03-20 12:46:50 +0200396 free_page((unsigned long)vcpu->pio_data);
397 vcpu->pio_data = NULL;
398 free_pio_guest_pages(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399}
400
401static void kvm_free_vcpus(struct kvm *kvm)
402{
403 unsigned int i;
404
405 for (i = 0; i < KVM_MAX_VCPUS; ++i)
406 kvm_free_vcpu(&kvm->vcpus[i]);
407}
408
409static int kvm_dev_release(struct inode *inode, struct file *filp)
410{
Avi Kivityf17abe92007-02-21 19:28:04 +0200411 return 0;
412}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413
Avi Kivityf17abe92007-02-21 19:28:04 +0200414static void kvm_destroy_vm(struct kvm *kvm)
415{
Avi Kivity133de902007-02-12 00:54:44 -0800416 spin_lock(&kvm_lock);
417 list_del(&kvm->vm_list);
418 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 kvm_free_vcpus(kvm);
420 kvm_free_physmem(kvm);
421 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200422}
423
424static int kvm_vm_release(struct inode *inode, struct file *filp)
425{
426 struct kvm *kvm = filp->private_data;
427
428 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429 return 0;
430}
431
432static void inject_gp(struct kvm_vcpu *vcpu)
433{
434 kvm_arch_ops->inject_gp(vcpu, 0);
435}
436
Avi Kivity1342d352007-01-05 16:36:39 -0800437/*
438 * Load the pae pdptrs. Return true is they are all valid.
439 */
440static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441{
442 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800443 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 int i;
445 u64 pdpte;
446 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800447 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300448 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449
450 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300451 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
452 /* FIXME: !page - emulate? 0xff? */
453 pdpt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
Avi Kivity1342d352007-01-05 16:36:39 -0800455 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 for (i = 0; i < 4; ++i) {
457 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800458 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
459 ret = 0;
460 goto out;
461 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 }
463
Avi Kivity1342d352007-01-05 16:36:39 -0800464 for (i = 0; i < 4; ++i)
465 vcpu->pdptrs[i] = pdpt[offset + i];
466
467out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 kunmap_atomic(pdpt, KM_USER0);
469 spin_unlock(&vcpu->kvm->lock);
470
Avi Kivity1342d352007-01-05 16:36:39 -0800471 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472}
473
474void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
475{
476 if (cr0 & CR0_RESEVED_BITS) {
477 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
478 cr0, vcpu->cr0);
479 inject_gp(vcpu);
480 return;
481 }
482
483 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
484 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
485 inject_gp(vcpu);
486 return;
487 }
488
489 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
490 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
491 "and a clear PE flag\n");
492 inject_gp(vcpu);
493 return;
494 }
495
496 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 if ((vcpu->shadow_efer & EFER_LME)) {
499 int cs_db, cs_l;
500
501 if (!is_pae(vcpu)) {
502 printk(KERN_DEBUG "set_cr0: #GP, start paging "
503 "in long mode while PAE is disabled\n");
504 inject_gp(vcpu);
505 return;
506 }
507 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
508 if (cs_l) {
509 printk(KERN_DEBUG "set_cr0: #GP, start paging "
510 "in long mode while CS.L == 1\n");
511 inject_gp(vcpu);
512 return;
513
514 }
515 } else
516#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800517 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
519 "reserved bits\n");
520 inject_gp(vcpu);
521 return;
522 }
523
524 }
525
526 kvm_arch_ops->set_cr0(vcpu, cr0);
527 vcpu->cr0 = cr0;
528
529 spin_lock(&vcpu->kvm->lock);
530 kvm_mmu_reset_context(vcpu);
531 spin_unlock(&vcpu->kvm->lock);
532 return;
533}
534EXPORT_SYMBOL_GPL(set_cr0);
535
536void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
537{
538 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
539}
540EXPORT_SYMBOL_GPL(lmsw);
541
542void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
543{
544 if (cr4 & CR4_RESEVED_BITS) {
545 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
546 inject_gp(vcpu);
547 return;
548 }
549
Avi Kivitya9058ec2006-12-29 16:49:37 -0800550 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 if (!(cr4 & CR4_PAE_MASK)) {
552 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
553 "in long mode\n");
554 inject_gp(vcpu);
555 return;
556 }
557 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800558 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
560 inject_gp(vcpu);
561 }
562
563 if (cr4 & CR4_VMXE_MASK) {
564 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
565 inject_gp(vcpu);
566 return;
567 }
568 kvm_arch_ops->set_cr4(vcpu, cr4);
569 spin_lock(&vcpu->kvm->lock);
570 kvm_mmu_reset_context(vcpu);
571 spin_unlock(&vcpu->kvm->lock);
572}
573EXPORT_SYMBOL_GPL(set_cr4);
574
575void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
576{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800577 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200578 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
580 inject_gp(vcpu);
581 return;
582 }
583 } else {
584 if (cr3 & CR3_RESEVED_BITS) {
585 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
586 inject_gp(vcpu);
587 return;
588 }
589 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800590 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
592 "reserved bits\n");
593 inject_gp(vcpu);
594 return;
595 }
596 }
597
598 vcpu->cr3 = cr3;
599 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800600 /*
601 * Does the new cr3 value map to physical memory? (Note, we
602 * catch an invalid cr3 even in real-mode, because it would
603 * cause trouble later on when we turn on paging anyway.)
604 *
605 * A real CPU would silently accept an invalid cr3 and would
606 * attempt to use it - with largely undefined (and often hard
607 * to debug) behavior on the guest side.
608 */
609 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
610 inject_gp(vcpu);
611 else
612 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 spin_unlock(&vcpu->kvm->lock);
614}
615EXPORT_SYMBOL_GPL(set_cr3);
616
617void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
618{
619 if ( cr8 & CR8_RESEVED_BITS) {
620 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
621 inject_gp(vcpu);
622 return;
623 }
624 vcpu->cr8 = cr8;
625}
626EXPORT_SYMBOL_GPL(set_cr8);
627
628void fx_init(struct kvm_vcpu *vcpu)
629{
630 struct __attribute__ ((__packed__)) fx_image_s {
631 u16 control; //fcw
632 u16 status; //fsw
633 u16 tag; // ftw
634 u16 opcode; //fop
635 u64 ip; // fpu ip
636 u64 operand;// fpu dp
637 u32 mxcsr;
638 u32 mxcsr_mask;
639
640 } *fx_image;
641
642 fx_save(vcpu->host_fx_image);
643 fpu_init();
644 fx_save(vcpu->guest_fx_image);
645 fx_restore(vcpu->host_fx_image);
646
647 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
648 fx_image->mxcsr = 0x1f80;
649 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
650 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
651}
652EXPORT_SYMBOL_GPL(fx_init);
653
Uri Lublin02b27c12007-02-22 17:15:33 +0200654static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
655{
656 spin_lock(&vcpu->kvm->lock);
657 kvm_mmu_slot_remove_write_access(vcpu, slot);
658 spin_unlock(&vcpu->kvm->lock);
659}
660
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 * Allocate some memory and give it an address in the guest physical address
663 * space.
664 *
665 * Discontiguous memory is allowed, mostly for framebuffers.
666 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200667static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
668 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669{
670 int r;
671 gfn_t base_gfn;
672 unsigned long npages;
673 unsigned long i;
674 struct kvm_memory_slot *memslot;
675 struct kvm_memory_slot old, new;
676 int memory_config_version;
677
678 r = -EINVAL;
679 /* General sanity checks */
680 if (mem->memory_size & (PAGE_SIZE - 1))
681 goto out;
682 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
683 goto out;
684 if (mem->slot >= KVM_MEMORY_SLOTS)
685 goto out;
686 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
687 goto out;
688
689 memslot = &kvm->memslots[mem->slot];
690 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
691 npages = mem->memory_size >> PAGE_SHIFT;
692
693 if (!npages)
694 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
695
696raced:
697 spin_lock(&kvm->lock);
698
699 memory_config_version = kvm->memory_config_version;
700 new = old = *memslot;
701
702 new.base_gfn = base_gfn;
703 new.npages = npages;
704 new.flags = mem->flags;
705
706 /* Disallow changing a memory slot's size. */
707 r = -EINVAL;
708 if (npages && old.npages && npages != old.npages)
709 goto out_unlock;
710
711 /* Check for overlaps */
712 r = -EEXIST;
713 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
714 struct kvm_memory_slot *s = &kvm->memslots[i];
715
716 if (s == memslot)
717 continue;
718 if (!((base_gfn + npages <= s->base_gfn) ||
719 (base_gfn >= s->base_gfn + s->npages)))
720 goto out_unlock;
721 }
722 /*
723 * Do memory allocations outside lock. memory_config_version will
724 * detect any races.
725 */
726 spin_unlock(&kvm->lock);
727
728 /* Deallocate if slot is being removed */
729 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000730 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731
732 /* Free page dirty bitmap if unneeded */
733 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000734 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
736 r = -ENOMEM;
737
738 /* Allocate if a slot is being created */
739 if (npages && !new.phys_mem) {
740 new.phys_mem = vmalloc(npages * sizeof(struct page *));
741
742 if (!new.phys_mem)
743 goto out_free;
744
745 memset(new.phys_mem, 0, npages * sizeof(struct page *));
746 for (i = 0; i < npages; ++i) {
747 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
748 | __GFP_ZERO);
749 if (!new.phys_mem[i])
750 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200751 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 }
753 }
754
755 /* Allocate page dirty bitmap if needed */
756 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
757 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
758
759 new.dirty_bitmap = vmalloc(dirty_bytes);
760 if (!new.dirty_bitmap)
761 goto out_free;
762 memset(new.dirty_bitmap, 0, dirty_bytes);
763 }
764
765 spin_lock(&kvm->lock);
766
767 if (memory_config_version != kvm->memory_config_version) {
768 spin_unlock(&kvm->lock);
769 kvm_free_physmem_slot(&new, &old);
770 goto raced;
771 }
772
773 r = -EAGAIN;
774 if (kvm->busy)
775 goto out_unlock;
776
777 if (mem->slot >= kvm->nmemslots)
778 kvm->nmemslots = mem->slot + 1;
779
780 *memslot = new;
781 ++kvm->memory_config_version;
782
783 spin_unlock(&kvm->lock);
784
785 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
786 struct kvm_vcpu *vcpu;
787
Avi Kivitybccf2152007-02-21 18:04:26 +0200788 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 if (!vcpu)
790 continue;
Uri Lublinff990d52007-02-22 17:37:32 +0200791 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
792 do_remove_write_access(vcpu, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 kvm_mmu_reset_context(vcpu);
794 vcpu_put(vcpu);
795 }
796
797 kvm_free_physmem_slot(&old, &new);
798 return 0;
799
800out_unlock:
801 spin_unlock(&kvm->lock);
802out_free:
803 kvm_free_physmem_slot(&new, &old);
804out:
805 return r;
806}
807
808/*
809 * Get (and clear) the dirty memory log for a memory slot.
810 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200811static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
812 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813{
814 struct kvm_memory_slot *memslot;
815 int r, i;
816 int n;
Avi Kivity714b93d2007-01-05 16:36:53 -0800817 int cleared;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 unsigned long any = 0;
819
820 spin_lock(&kvm->lock);
821
822 /*
823 * Prevent changes to guest memory configuration even while the lock
824 * is not taken.
825 */
826 ++kvm->busy;
827 spin_unlock(&kvm->lock);
828 r = -EINVAL;
829 if (log->slot >= KVM_MEMORY_SLOTS)
830 goto out;
831
832 memslot = &kvm->memslots[log->slot];
833 r = -ENOENT;
834 if (!memslot->dirty_bitmap)
835 goto out;
836
Uri Lublincd1a4a92007-02-22 16:43:09 +0200837 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838
Uri Lublincd1a4a92007-02-22 16:43:09 +0200839 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840 any = memslot->dirty_bitmap[i];
841
842 r = -EFAULT;
843 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
844 goto out;
845
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 if (any) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800847 cleared = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Avi Kivitybccf2152007-02-21 18:04:26 +0200849 struct kvm_vcpu *vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
Avi Kivitybccf2152007-02-21 18:04:26 +0200851 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 if (!vcpu)
853 continue;
Avi Kivity714b93d2007-01-05 16:36:53 -0800854 if (!cleared) {
855 do_remove_write_access(vcpu, log->slot);
856 memset(memslot->dirty_bitmap, 0, n);
857 cleared = 1;
858 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 kvm_arch_ops->tlb_flush(vcpu);
860 vcpu_put(vcpu);
861 }
862 }
863
864 r = 0;
865
866out:
867 spin_lock(&kvm->lock);
868 --kvm->busy;
869 spin_unlock(&kvm->lock);
870 return r;
871}
872
Avi Kivitye8207542007-03-30 16:54:30 +0300873/*
874 * Set a new alias region. Aliases map a portion of physical memory into
875 * another portion. This is useful for memory windows, for example the PC
876 * VGA region.
877 */
878static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
879 struct kvm_memory_alias *alias)
880{
881 int r, n;
882 struct kvm_mem_alias *p;
883
884 r = -EINVAL;
885 /* General sanity checks */
886 if (alias->memory_size & (PAGE_SIZE - 1))
887 goto out;
888 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
889 goto out;
890 if (alias->slot >= KVM_ALIAS_SLOTS)
891 goto out;
892 if (alias->guest_phys_addr + alias->memory_size
893 < alias->guest_phys_addr)
894 goto out;
895 if (alias->target_phys_addr + alias->memory_size
896 < alias->target_phys_addr)
897 goto out;
898
899 spin_lock(&kvm->lock);
900
901 p = &kvm->aliases[alias->slot];
902 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
903 p->npages = alias->memory_size >> PAGE_SHIFT;
904 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
905
906 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
907 if (kvm->aliases[n - 1].npages)
908 break;
909 kvm->naliases = n;
910
911 spin_unlock(&kvm->lock);
912
913 vcpu_load(&kvm->vcpus[0]);
914 spin_lock(&kvm->lock);
915 kvm_mmu_zap_all(&kvm->vcpus[0]);
916 spin_unlock(&kvm->lock);
917 vcpu_put(&kvm->vcpus[0]);
918
919 return 0;
920
921out:
922 return r;
923}
924
925static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
926{
927 int i;
928 struct kvm_mem_alias *alias;
929
930 for (i = 0; i < kvm->naliases; ++i) {
931 alias = &kvm->aliases[i];
932 if (gfn >= alias->base_gfn
933 && gfn < alias->base_gfn + alias->npages)
934 return alias->target_gfn + gfn - alias->base_gfn;
935 }
936 return gfn;
937}
938
939static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940{
941 int i;
942
943 for (i = 0; i < kvm->nmemslots; ++i) {
944 struct kvm_memory_slot *memslot = &kvm->memslots[i];
945
946 if (gfn >= memslot->base_gfn
947 && gfn < memslot->base_gfn + memslot->npages)
948 return memslot;
949 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000950 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951}
Avi Kivitye8207542007-03-30 16:54:30 +0300952
953struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
954{
955 gfn = unalias_gfn(kvm, gfn);
956 return __gfn_to_memslot(kvm, gfn);
957}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
Avi Kivity954bbbc2007-03-30 14:02:32 +0300959struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
960{
961 struct kvm_memory_slot *slot;
962
Avi Kivitye8207542007-03-30 16:54:30 +0300963 gfn = unalias_gfn(kvm, gfn);
964 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300965 if (!slot)
966 return NULL;
967 return slot->phys_mem[gfn - slot->base_gfn];
968}
969EXPORT_SYMBOL_GPL(gfn_to_page);
970
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
972{
973 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000974 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 unsigned long rel_gfn;
976
977 for (i = 0; i < kvm->nmemslots; ++i) {
978 memslot = &kvm->memslots[i];
979
980 if (gfn >= memslot->base_gfn
981 && gfn < memslot->base_gfn + memslot->npages) {
982
983 if (!memslot || !memslot->dirty_bitmap)
984 return;
985
986 rel_gfn = gfn - memslot->base_gfn;
987
988 /* avoid RMW */
989 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
990 set_bit(rel_gfn, memslot->dirty_bitmap);
991 return;
992 }
993 }
994}
995
996static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300997 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 unsigned int bytes,
999 struct x86_emulate_ctxt *ctxt)
1000{
1001 struct kvm_vcpu *vcpu = ctxt->vcpu;
1002 void *data = val;
1003
1004 while (bytes) {
1005 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1006 unsigned offset = addr & (PAGE_SIZE-1);
1007 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1008 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001009 struct page *page;
1010 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011
1012 if (gpa == UNMAPPED_GVA)
1013 return X86EMUL_PROPAGATE_FAULT;
1014 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001015 page = gfn_to_page(vcpu->kvm, pfn);
1016 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001018 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Avi Kivity954bbbc2007-03-30 14:02:32 +03001020 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Avi Kivity954bbbc2007-03-30 14:02:32 +03001022 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023
1024 bytes -= tocopy;
1025 data += tocopy;
1026 addr += tocopy;
1027 }
1028
1029 return X86EMUL_CONTINUE;
1030}
1031
1032static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001033 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 unsigned int bytes,
1035 struct x86_emulate_ctxt *ctxt)
1036{
1037 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1038 addr, bytes);
1039 return X86EMUL_UNHANDLEABLE;
1040}
1041
1042static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001043 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 unsigned int bytes,
1045 struct x86_emulate_ctxt *ctxt)
1046{
1047 struct kvm_vcpu *vcpu = ctxt->vcpu;
1048
1049 if (vcpu->mmio_read_completed) {
1050 memcpy(val, vcpu->mmio_data, bytes);
1051 vcpu->mmio_read_completed = 0;
1052 return X86EMUL_CONTINUE;
1053 } else if (emulator_read_std(addr, val, bytes, ctxt)
1054 == X86EMUL_CONTINUE)
1055 return X86EMUL_CONTINUE;
1056 else {
1057 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001058
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001060 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 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;
1067 }
1068}
1069
Avi Kivityda4a00f2007-01-05 16:36:44 -08001070static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001071 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001072{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001073 struct page *page;
1074 void *virt;
Avi Kivity09072da2007-05-01 14:16:52 +03001075 unsigned offset = offset_in_page(gpa);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001076
1077 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1078 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001079 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1080 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001081 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001082 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001083 virt = kmap_atomic(page, KM_USER0);
Avi Kivity09072da2007-05-01 14:16:52 +03001084 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
Avi Kivity4c690a12007-04-22 15:28:19 +03001085 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001086 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001087 return 1;
1088}
1089
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090static int emulator_write_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001091 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 unsigned int bytes,
1093 struct x86_emulate_ctxt *ctxt)
1094{
1095 struct kvm_vcpu *vcpu = ctxt->vcpu;
1096 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1097
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
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 vcpu->mmio_needed = 1;
1107 vcpu->mmio_phys_addr = gpa;
1108 vcpu->mmio_size = bytes;
1109 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001110 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111
1112 return X86EMUL_CONTINUE;
1113}
1114
1115static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001116 const void *old,
1117 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 unsigned int bytes,
1119 struct x86_emulate_ctxt *ctxt)
1120{
1121 static int reported;
1122
1123 if (!reported) {
1124 reported = 1;
1125 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1126 }
1127 return emulator_write_emulated(addr, new, bytes, ctxt);
1128}
1129
1130static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1131{
1132 return kvm_arch_ops->get_segment_base(vcpu, seg);
1133}
1134
1135int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1136{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 return X86EMUL_CONTINUE;
1138}
1139
1140int emulate_clts(struct kvm_vcpu *vcpu)
1141{
Avi Kivity399badf2007-01-05 16:36:38 -08001142 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143
Avi Kivity399badf2007-01-05 16:36:38 -08001144 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 kvm_arch_ops->set_cr0(vcpu, cr0);
1146 return X86EMUL_CONTINUE;
1147}
1148
1149int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1150{
1151 struct kvm_vcpu *vcpu = ctxt->vcpu;
1152
1153 switch (dr) {
1154 case 0 ... 3:
1155 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1156 return X86EMUL_CONTINUE;
1157 default:
1158 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1159 __FUNCTION__, dr);
1160 return X86EMUL_UNHANDLEABLE;
1161 }
1162}
1163
1164int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1165{
1166 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1167 int exception;
1168
1169 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1170 if (exception) {
1171 /* FIXME: better handling */
1172 return X86EMUL_UNHANDLEABLE;
1173 }
1174 return X86EMUL_CONTINUE;
1175}
1176
1177static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1178{
1179 static int reported;
1180 u8 opcodes[4];
1181 unsigned long rip = ctxt->vcpu->rip;
1182 unsigned long rip_linear;
1183
1184 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1185
1186 if (reported)
1187 return;
1188
1189 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1190
1191 printk(KERN_ERR "emulation failed but !mmio_needed?"
1192 " rip %lx %02x %02x %02x %02x\n",
1193 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1194 reported = 1;
1195}
1196
1197struct x86_emulate_ops emulate_ops = {
1198 .read_std = emulator_read_std,
1199 .write_std = emulator_write_std,
1200 .read_emulated = emulator_read_emulated,
1201 .write_emulated = emulator_write_emulated,
1202 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1203};
1204
1205int emulate_instruction(struct kvm_vcpu *vcpu,
1206 struct kvm_run *run,
1207 unsigned long cr2,
1208 u16 error_code)
1209{
1210 struct x86_emulate_ctxt emulate_ctxt;
1211 int r;
1212 int cs_db, cs_l;
1213
Avi Kivitye7df56e2007-03-14 15:54:54 +02001214 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 kvm_arch_ops->cache_regs(vcpu);
1216
1217 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1218
1219 emulate_ctxt.vcpu = vcpu;
1220 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1221 emulate_ctxt.cr2 = cr2;
1222 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1223 ? X86EMUL_MODE_REAL : cs_l
1224 ? X86EMUL_MODE_PROT64 : cs_db
1225 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1226
1227 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1228 emulate_ctxt.cs_base = 0;
1229 emulate_ctxt.ds_base = 0;
1230 emulate_ctxt.es_base = 0;
1231 emulate_ctxt.ss_base = 0;
1232 } else {
1233 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1234 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1235 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1236 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1237 }
1238
1239 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1240 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1241
1242 vcpu->mmio_is_write = 0;
1243 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1244
1245 if ((r || vcpu->mmio_is_write) && run) {
1246 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1247 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1248 run->mmio.len = vcpu->mmio_size;
1249 run->mmio.is_write = vcpu->mmio_is_write;
1250 }
1251
1252 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001253 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1254 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 if (!vcpu->mmio_needed) {
1256 report_emulation_failure(&emulate_ctxt);
1257 return EMULATE_FAIL;
1258 }
1259 return EMULATE_DO_MMIO;
1260 }
1261
1262 kvm_arch_ops->decache_regs(vcpu);
1263 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1264
Avi Kivity02c83202007-04-29 15:02:17 +03001265 if (vcpu->mmio_is_write) {
1266 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269
1270 return EMULATE_DONE;
1271}
1272EXPORT_SYMBOL_GPL(emulate_instruction);
1273
Avi Kivity270fd9b2007-02-19 14:37:47 +02001274int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1275{
1276 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1277
Dor Laor9b22bf52007-02-19 16:44:49 +02001278 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001279 ret = -KVM_EINVAL;
1280#ifdef CONFIG_X86_64
1281 if (is_long_mode(vcpu)) {
1282 nr = vcpu->regs[VCPU_REGS_RAX];
1283 a0 = vcpu->regs[VCPU_REGS_RDI];
1284 a1 = vcpu->regs[VCPU_REGS_RSI];
1285 a2 = vcpu->regs[VCPU_REGS_RDX];
1286 a3 = vcpu->regs[VCPU_REGS_RCX];
1287 a4 = vcpu->regs[VCPU_REGS_R8];
1288 a5 = vcpu->regs[VCPU_REGS_R9];
1289 } else
1290#endif
1291 {
1292 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1293 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1294 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1295 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1296 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1297 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1298 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1299 }
1300 switch (nr) {
1301 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001302 run->hypercall.args[0] = a0;
1303 run->hypercall.args[1] = a1;
1304 run->hypercall.args[2] = a2;
1305 run->hypercall.args[3] = a3;
1306 run->hypercall.args[4] = a4;
1307 run->hypercall.args[5] = a5;
1308 run->hypercall.ret = ret;
1309 run->hypercall.longmode = is_long_mode(vcpu);
1310 kvm_arch_ops->decache_regs(vcpu);
1311 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001312 }
1313 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001314 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001315 return 1;
1316}
1317EXPORT_SYMBOL_GPL(kvm_hypercall);
1318
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1320{
1321 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1322}
1323
1324void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1325{
1326 struct descriptor_table dt = { limit, base };
1327
1328 kvm_arch_ops->set_gdt(vcpu, &dt);
1329}
1330
1331void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1332{
1333 struct descriptor_table dt = { limit, base };
1334
1335 kvm_arch_ops->set_idt(vcpu, &dt);
1336}
1337
1338void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1339 unsigned long *rflags)
1340{
1341 lmsw(vcpu, msw);
1342 *rflags = kvm_arch_ops->get_rflags(vcpu);
1343}
1344
1345unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1346{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001347 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 switch (cr) {
1349 case 0:
1350 return vcpu->cr0;
1351 case 2:
1352 return vcpu->cr2;
1353 case 3:
1354 return vcpu->cr3;
1355 case 4:
1356 return vcpu->cr4;
1357 default:
1358 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1359 return 0;
1360 }
1361}
1362
1363void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1364 unsigned long *rflags)
1365{
1366 switch (cr) {
1367 case 0:
1368 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1369 *rflags = kvm_arch_ops->get_rflags(vcpu);
1370 break;
1371 case 2:
1372 vcpu->cr2 = val;
1373 break;
1374 case 3:
1375 set_cr3(vcpu, val);
1376 break;
1377 case 4:
1378 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1379 break;
1380 default:
1381 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1382 }
1383}
1384
Ingo Molnar102d8322007-02-19 14:37:47 +02001385/*
1386 * Register the para guest with the host:
1387 */
1388static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1389{
1390 struct kvm_vcpu_para_state *para_state;
1391 hpa_t para_state_hpa, hypercall_hpa;
1392 struct page *para_state_page;
1393 unsigned char *hypercall;
1394 gpa_t hypercall_gpa;
1395
1396 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1397 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1398
1399 /*
1400 * Needs to be page aligned:
1401 */
1402 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1403 goto err_gp;
1404
1405 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1406 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1407 if (is_error_hpa(para_state_hpa))
1408 goto err_gp;
1409
Uri Lublinab51a432007-02-21 18:25:21 +02001410 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001411 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1412 para_state = kmap_atomic(para_state_page, KM_USER0);
1413
1414 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1415 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1416
1417 para_state->host_version = KVM_PARA_API_VERSION;
1418 /*
1419 * We cannot support guests that try to register themselves
1420 * with a newer API version than the host supports:
1421 */
1422 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1423 para_state->ret = -KVM_EINVAL;
1424 goto err_kunmap_skip;
1425 }
1426
1427 hypercall_gpa = para_state->hypercall_gpa;
1428 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1429 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1430 if (is_error_hpa(hypercall_hpa)) {
1431 para_state->ret = -KVM_EINVAL;
1432 goto err_kunmap_skip;
1433 }
1434
1435 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1436 vcpu->para_state_page = para_state_page;
1437 vcpu->para_state_gpa = para_state_gpa;
1438 vcpu->hypercall_gpa = hypercall_gpa;
1439
Uri Lublinab51a432007-02-21 18:25:21 +02001440 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001441 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1442 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1443 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1444 kunmap_atomic(hypercall, KM_USER1);
1445
1446 para_state->ret = 0;
1447err_kunmap_skip:
1448 kunmap_atomic(para_state, KM_USER0);
1449 return 0;
1450err_gp:
1451 return 1;
1452}
1453
Avi Kivity3bab1f52006-12-29 16:49:48 -08001454int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1455{
1456 u64 data;
1457
1458 switch (msr) {
1459 case 0xc0010010: /* SYSCFG */
1460 case 0xc0010015: /* HWCR */
1461 case MSR_IA32_PLATFORM_ID:
1462 case MSR_IA32_P5_MC_ADDR:
1463 case MSR_IA32_P5_MC_TYPE:
1464 case MSR_IA32_MC0_CTL:
1465 case MSR_IA32_MCG_STATUS:
1466 case MSR_IA32_MCG_CAP:
1467 case MSR_IA32_MC0_MISC:
1468 case MSR_IA32_MC0_MISC+4:
1469 case MSR_IA32_MC0_MISC+8:
1470 case MSR_IA32_MC0_MISC+12:
1471 case MSR_IA32_MC0_MISC+16:
1472 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001473 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001474 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001475 /* MTRR registers */
1476 case 0xfe:
1477 case 0x200 ... 0x2ff:
1478 data = 0;
1479 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001480 case 0xcd: /* fsb frequency */
1481 data = 3;
1482 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001483 case MSR_IA32_APICBASE:
1484 data = vcpu->apic_base;
1485 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001486 case MSR_IA32_MISC_ENABLE:
1487 data = vcpu->ia32_misc_enable_msr;
1488 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001489#ifdef CONFIG_X86_64
1490 case MSR_EFER:
1491 data = vcpu->shadow_efer;
1492 break;
1493#endif
1494 default:
1495 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1496 return 1;
1497 }
1498 *pdata = data;
1499 return 0;
1500}
1501EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1502
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503/*
1504 * Reads an msr value (of 'msr_index') into 'pdata'.
1505 * Returns 0 on success, non-0 otherwise.
1506 * Assumes vcpu_load() was already called.
1507 */
1508static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1509{
1510 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1511}
1512
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001513#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
Avi Kivity3bab1f52006-12-29 16:49:48 -08001515static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 if (efer & EFER_RESERVED_BITS) {
1518 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1519 efer);
1520 inject_gp(vcpu);
1521 return;
1522 }
1523
1524 if (is_paging(vcpu)
1525 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1526 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1527 inject_gp(vcpu);
1528 return;
1529 }
1530
Avi Kivity7725f0b2006-12-13 00:34:01 -08001531 kvm_arch_ops->set_efer(vcpu, efer);
1532
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533 efer &= ~EFER_LMA;
1534 efer |= vcpu->shadow_efer & EFER_LMA;
1535
1536 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538
1539#endif
1540
Avi Kivity3bab1f52006-12-29 16:49:48 -08001541int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1542{
1543 switch (msr) {
1544#ifdef CONFIG_X86_64
1545 case MSR_EFER:
1546 set_efer(vcpu, data);
1547 break;
1548#endif
1549 case MSR_IA32_MC0_STATUS:
1550 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1551 __FUNCTION__, data);
1552 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001553 case MSR_IA32_MCG_STATUS:
1554 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1555 __FUNCTION__, data);
1556 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001557 case MSR_IA32_UCODE_REV:
1558 case MSR_IA32_UCODE_WRITE:
1559 case 0x200 ... 0x2ff: /* MTRRs */
1560 break;
1561 case MSR_IA32_APICBASE:
1562 vcpu->apic_base = data;
1563 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001564 case MSR_IA32_MISC_ENABLE:
1565 vcpu->ia32_misc_enable_msr = data;
1566 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001567 /*
1568 * This is the 'probe whether the host is KVM' logic:
1569 */
1570 case MSR_KVM_API_MAGIC:
1571 return vcpu_register_para(vcpu, data);
1572
Avi Kivity3bab1f52006-12-29 16:49:48 -08001573 default:
1574 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1575 return 1;
1576 }
1577 return 0;
1578}
1579EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1580
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581/*
1582 * Writes msr value into into the appropriate "register".
1583 * Returns 0 on success, non-0 otherwise.
1584 * Assumes vcpu_load() was already called.
1585 */
1586static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1587{
1588 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1589}
1590
1591void kvm_resched(struct kvm_vcpu *vcpu)
1592{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001593 if (!need_resched())
1594 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595 vcpu_put(vcpu);
1596 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001597 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598}
1599EXPORT_SYMBOL_GPL(kvm_resched);
1600
1601void load_msrs(struct vmx_msr_entry *e, int n)
1602{
1603 int i;
1604
1605 for (i = 0; i < n; ++i)
1606 wrmsrl(e[i].index, e[i].data);
1607}
1608EXPORT_SYMBOL_GPL(load_msrs);
1609
1610void save_msrs(struct vmx_msr_entry *e, int n)
1611{
1612 int i;
1613
1614 for (i = 0; i < n; ++i)
1615 rdmsrl(e[i].index, e[i].data);
1616}
1617EXPORT_SYMBOL_GPL(save_msrs);
1618
Avi Kivity06465c52007-02-28 20:46:53 +02001619void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1620{
1621 int i;
1622 u32 function;
1623 struct kvm_cpuid_entry *e, *best;
1624
1625 kvm_arch_ops->cache_regs(vcpu);
1626 function = vcpu->regs[VCPU_REGS_RAX];
1627 vcpu->regs[VCPU_REGS_RAX] = 0;
1628 vcpu->regs[VCPU_REGS_RBX] = 0;
1629 vcpu->regs[VCPU_REGS_RCX] = 0;
1630 vcpu->regs[VCPU_REGS_RDX] = 0;
1631 best = NULL;
1632 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1633 e = &vcpu->cpuid_entries[i];
1634 if (e->function == function) {
1635 best = e;
1636 break;
1637 }
1638 /*
1639 * Both basic or both extended?
1640 */
1641 if (((e->function ^ function) & 0x80000000) == 0)
1642 if (!best || e->function > best->function)
1643 best = e;
1644 }
1645 if (best) {
1646 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1647 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1648 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1649 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1650 }
1651 kvm_arch_ops->decache_regs(vcpu);
1652 kvm_arch_ops->skip_emulated_instruction(vcpu);
1653}
1654EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1655
Avi Kivity039576c2007-03-20 12:46:50 +02001656static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001657{
Avi Kivity039576c2007-03-20 12:46:50 +02001658 void *p = vcpu->pio_data;
1659 void *q;
1660 unsigned bytes;
1661 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1662
1663 kvm_arch_ops->vcpu_put(vcpu);
1664 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1665 PAGE_KERNEL);
1666 if (!q) {
1667 kvm_arch_ops->vcpu_load(vcpu);
1668 free_pio_guest_pages(vcpu);
1669 return -ENOMEM;
1670 }
1671 q += vcpu->pio.guest_page_offset;
1672 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1673 if (vcpu->pio.in)
1674 memcpy(q, p, bytes);
1675 else
1676 memcpy(p, q, bytes);
1677 q -= vcpu->pio.guest_page_offset;
1678 vunmap(q);
1679 kvm_arch_ops->vcpu_load(vcpu);
1680 free_pio_guest_pages(vcpu);
1681 return 0;
1682}
1683
1684static int complete_pio(struct kvm_vcpu *vcpu)
1685{
1686 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001687 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001688 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001689
1690 kvm_arch_ops->cache_regs(vcpu);
1691
1692 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001693 if (io->in)
1694 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001695 io->size);
1696 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001697 if (io->in) {
1698 r = pio_copy_data(vcpu);
1699 if (r) {
1700 kvm_arch_ops->cache_regs(vcpu);
1701 return r;
1702 }
1703 }
1704
Avi Kivity46fc1472007-02-22 19:39:30 +02001705 delta = 1;
1706 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001707 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001708 /*
1709 * The size of the register should really depend on
1710 * current address size.
1711 */
1712 vcpu->regs[VCPU_REGS_RCX] -= delta;
1713 }
Avi Kivity039576c2007-03-20 12:46:50 +02001714 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001715 delta = -delta;
1716 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001717 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001718 vcpu->regs[VCPU_REGS_RDI] += delta;
1719 else
1720 vcpu->regs[VCPU_REGS_RSI] += delta;
1721 }
1722
Avi Kivity46fc1472007-02-22 19:39:30 +02001723 kvm_arch_ops->decache_regs(vcpu);
1724
Avi Kivity039576c2007-03-20 12:46:50 +02001725 io->count -= io->cur_count;
1726 io->cur_count = 0;
1727
1728 if (!io->count)
1729 kvm_arch_ops->skip_emulated_instruction(vcpu);
1730 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001731}
1732
Avi Kivity039576c2007-03-20 12:46:50 +02001733int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1734 int size, unsigned long count, int string, int down,
1735 gva_t address, int rep, unsigned port)
1736{
1737 unsigned now, in_page;
1738 int i;
1739 int nr_pages = 1;
1740 struct page *page;
1741
1742 vcpu->run->exit_reason = KVM_EXIT_IO;
1743 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1744 vcpu->run->io.size = size;
1745 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1746 vcpu->run->io.count = count;
1747 vcpu->run->io.port = port;
1748 vcpu->pio.count = count;
1749 vcpu->pio.cur_count = count;
1750 vcpu->pio.size = size;
1751 vcpu->pio.in = in;
1752 vcpu->pio.string = string;
1753 vcpu->pio.down = down;
1754 vcpu->pio.guest_page_offset = offset_in_page(address);
1755 vcpu->pio.rep = rep;
1756
1757 if (!string) {
1758 kvm_arch_ops->cache_regs(vcpu);
1759 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1760 kvm_arch_ops->decache_regs(vcpu);
1761 return 0;
1762 }
1763
1764 if (!count) {
1765 kvm_arch_ops->skip_emulated_instruction(vcpu);
1766 return 1;
1767 }
1768
1769 now = min(count, PAGE_SIZE / size);
1770
1771 if (!down)
1772 in_page = PAGE_SIZE - offset_in_page(address);
1773 else
1774 in_page = offset_in_page(address) + size;
1775 now = min(count, (unsigned long)in_page / size);
1776 if (!now) {
1777 /*
1778 * String I/O straddles page boundary. Pin two guest pages
1779 * so that we satisfy atomicity constraints. Do just one
1780 * transaction to avoid complexity.
1781 */
1782 nr_pages = 2;
1783 now = 1;
1784 }
1785 if (down) {
1786 /*
1787 * String I/O in reverse. Yuck. Kill the guest, fix later.
1788 */
1789 printk(KERN_ERR "kvm: guest string pio down\n");
1790 inject_gp(vcpu);
1791 return 1;
1792 }
1793 vcpu->run->io.count = now;
1794 vcpu->pio.cur_count = now;
1795
1796 for (i = 0; i < nr_pages; ++i) {
1797 spin_lock(&vcpu->kvm->lock);
1798 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1799 if (page)
1800 get_page(page);
1801 vcpu->pio.guest_pages[i] = page;
1802 spin_unlock(&vcpu->kvm->lock);
1803 if (!page) {
1804 inject_gp(vcpu);
1805 free_pio_guest_pages(vcpu);
1806 return 1;
1807 }
1808 }
1809
1810 if (!vcpu->pio.in)
1811 return pio_copy_data(vcpu);
1812 return 0;
1813}
1814EXPORT_SYMBOL_GPL(kvm_setup_pio);
1815
Avi Kivitybccf2152007-02-21 18:04:26 +02001816static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001819 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820
Avi Kivitybccf2152007-02-21 18:04:26 +02001821 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822
Avi Kivity1961d272007-03-05 19:46:05 +02001823 if (vcpu->sigset_active)
1824 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1825
Dor Laor54810342007-02-12 00:54:39 -08001826 /* re-sync apic's tpr */
1827 vcpu->cr8 = kvm_run->cr8;
1828
Avi Kivity02c83202007-04-29 15:02:17 +03001829 if (vcpu->pio.cur_count) {
1830 r = complete_pio(vcpu);
1831 if (r)
1832 goto out;
1833 }
1834
1835 if (vcpu->mmio_needed) {
1836 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1837 vcpu->mmio_read_completed = 1;
1838 vcpu->mmio_needed = 0;
1839 r = emulate_instruction(vcpu, kvm_run,
1840 vcpu->mmio_fault_cr2, 0);
1841 if (r == EMULATE_DO_MMIO) {
1842 /*
1843 * Read-modify-write. Back to userspace.
1844 */
1845 kvm_run->exit_reason = KVM_EXIT_MMIO;
1846 r = 0;
1847 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001848 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849 }
1850
Avi Kivity8eb7d332007-03-04 14:17:08 +02001851 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001852 kvm_arch_ops->cache_regs(vcpu);
1853 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1854 kvm_arch_ops->decache_regs(vcpu);
1855 }
1856
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 r = kvm_arch_ops->run(vcpu, kvm_run);
1858
Avi Kivity039576c2007-03-20 12:46:50 +02001859out:
Avi Kivity1961d272007-03-05 19:46:05 +02001860 if (vcpu->sigset_active)
1861 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1862
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 vcpu_put(vcpu);
1864 return r;
1865}
1866
Avi Kivitybccf2152007-02-21 18:04:26 +02001867static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1868 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869{
Avi Kivitybccf2152007-02-21 18:04:26 +02001870 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871
1872 kvm_arch_ops->cache_regs(vcpu);
1873
1874 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1875 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1876 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1877 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1878 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1879 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1880 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1881 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001882#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1884 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1885 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1886 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1887 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1888 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1889 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1890 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1891#endif
1892
1893 regs->rip = vcpu->rip;
1894 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1895
1896 /*
1897 * Don't leak debug flags in case they were set for guest debugging
1898 */
1899 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1900 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1901
1902 vcpu_put(vcpu);
1903
1904 return 0;
1905}
1906
Avi Kivitybccf2152007-02-21 18:04:26 +02001907static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1908 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909{
Avi Kivitybccf2152007-02-21 18:04:26 +02001910 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911
1912 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1913 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1914 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1915 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1916 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1917 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1918 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1919 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001920#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1922 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1923 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1924 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1925 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1926 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1927 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1928 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1929#endif
1930
1931 vcpu->rip = regs->rip;
1932 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1933
1934 kvm_arch_ops->decache_regs(vcpu);
1935
1936 vcpu_put(vcpu);
1937
1938 return 0;
1939}
1940
1941static void get_segment(struct kvm_vcpu *vcpu,
1942 struct kvm_segment *var, int seg)
1943{
1944 return kvm_arch_ops->get_segment(vcpu, var, seg);
1945}
1946
Avi Kivitybccf2152007-02-21 18:04:26 +02001947static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1948 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 struct descriptor_table dt;
1951
Avi Kivitybccf2152007-02-21 18:04:26 +02001952 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953
1954 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1955 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1956 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1957 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1958 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1959 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1960
1961 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1962 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1963
1964 kvm_arch_ops->get_idt(vcpu, &dt);
1965 sregs->idt.limit = dt.limit;
1966 sregs->idt.base = dt.base;
1967 kvm_arch_ops->get_gdt(vcpu, &dt);
1968 sregs->gdt.limit = dt.limit;
1969 sregs->gdt.base = dt.base;
1970
Anthony Liguori25c4c272007-04-27 09:29:21 +03001971 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 sregs->cr0 = vcpu->cr0;
1973 sregs->cr2 = vcpu->cr2;
1974 sregs->cr3 = vcpu->cr3;
1975 sregs->cr4 = vcpu->cr4;
1976 sregs->cr8 = vcpu->cr8;
1977 sregs->efer = vcpu->shadow_efer;
1978 sregs->apic_base = vcpu->apic_base;
1979
1980 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1981 sizeof sregs->interrupt_bitmap);
1982
1983 vcpu_put(vcpu);
1984
1985 return 0;
1986}
1987
1988static void set_segment(struct kvm_vcpu *vcpu,
1989 struct kvm_segment *var, int seg)
1990{
1991 return kvm_arch_ops->set_segment(vcpu, var, seg);
1992}
1993
Avi Kivitybccf2152007-02-21 18:04:26 +02001994static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1995 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 int mmu_reset_needed = 0;
1998 int i;
1999 struct descriptor_table dt;
2000
Avi Kivitybccf2152007-02-21 18:04:26 +02002001 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 dt.limit = sregs->idt.limit;
2004 dt.base = sregs->idt.base;
2005 kvm_arch_ops->set_idt(vcpu, &dt);
2006 dt.limit = sregs->gdt.limit;
2007 dt.base = sregs->gdt.base;
2008 kvm_arch_ops->set_gdt(vcpu, &dt);
2009
2010 vcpu->cr2 = sregs->cr2;
2011 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2012 vcpu->cr3 = sregs->cr3;
2013
2014 vcpu->cr8 = sregs->cr8;
2015
2016 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002017#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2019#endif
2020 vcpu->apic_base = sregs->apic_base;
2021
Anthony Liguori25c4c272007-04-27 09:29:21 +03002022 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002023
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002025 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026
2027 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2028 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002029 if (!is_long_mode(vcpu) && is_pae(vcpu))
2030 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031
2032 if (mmu_reset_needed)
2033 kvm_mmu_reset_context(vcpu);
2034
2035 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2036 sizeof vcpu->irq_pending);
2037 vcpu->irq_summary = 0;
2038 for (i = 0; i < NR_IRQ_WORDS; ++i)
2039 if (vcpu->irq_pending[i])
2040 __set_bit(i, &vcpu->irq_summary);
2041
Avi Kivity024aa1c2007-03-21 13:44:58 +02002042 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2043 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2044 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2045 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2046 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2047 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2048
2049 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2050 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2051
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052 vcpu_put(vcpu);
2053
2054 return 0;
2055}
2056
2057/*
2058 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2059 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002060 *
2061 * This list is modified at module load time to reflect the
2062 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 */
2064static u32 msrs_to_save[] = {
2065 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2066 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002067#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2069#endif
2070 MSR_IA32_TIME_STAMP_COUNTER,
2071};
2072
Michael Riepebf591b22006-12-22 01:05:36 -08002073static unsigned num_msrs_to_save;
2074
Avi Kivity6f00e682007-01-26 00:56:40 -08002075static u32 emulated_msrs[] = {
2076 MSR_IA32_MISC_ENABLE,
2077};
2078
Michael Riepebf591b22006-12-22 01:05:36 -08002079static __init void kvm_init_msr_list(void)
2080{
2081 u32 dummy[2];
2082 unsigned i, j;
2083
2084 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2085 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2086 continue;
2087 if (j < i)
2088 msrs_to_save[j] = msrs_to_save[i];
2089 j++;
2090 }
2091 num_msrs_to_save = j;
2092}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093
2094/*
2095 * Adapt set_msr() to msr_io()'s calling convention
2096 */
2097static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2098{
2099 return set_msr(vcpu, index, *data);
2100}
2101
2102/*
2103 * Read or write a bunch of msrs. All parameters are kernel addresses.
2104 *
2105 * @return number of msrs set successfully.
2106 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002107static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 struct kvm_msr_entry *entries,
2109 int (*do_msr)(struct kvm_vcpu *vcpu,
2110 unsigned index, u64 *data))
2111{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 int i;
2113
Avi Kivitybccf2152007-02-21 18:04:26 +02002114 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
2116 for (i = 0; i < msrs->nmsrs; ++i)
2117 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2118 break;
2119
2120 vcpu_put(vcpu);
2121
2122 return i;
2123}
2124
2125/*
2126 * Read or write a bunch of msrs. Parameters are user addresses.
2127 *
2128 * @return number of msrs set successfully.
2129 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002130static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 int (*do_msr)(struct kvm_vcpu *vcpu,
2132 unsigned index, u64 *data),
2133 int writeback)
2134{
2135 struct kvm_msrs msrs;
2136 struct kvm_msr_entry *entries;
2137 int r, n;
2138 unsigned size;
2139
2140 r = -EFAULT;
2141 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2142 goto out;
2143
2144 r = -E2BIG;
2145 if (msrs.nmsrs >= MAX_IO_MSRS)
2146 goto out;
2147
2148 r = -ENOMEM;
2149 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2150 entries = vmalloc(size);
2151 if (!entries)
2152 goto out;
2153
2154 r = -EFAULT;
2155 if (copy_from_user(entries, user_msrs->entries, size))
2156 goto out_free;
2157
Avi Kivitybccf2152007-02-21 18:04:26 +02002158 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 if (r < 0)
2160 goto out_free;
2161
2162 r = -EFAULT;
2163 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2164 goto out_free;
2165
2166 r = n;
2167
2168out_free:
2169 vfree(entries);
2170out:
2171 return r;
2172}
2173
2174/*
2175 * Translate a guest virtual address to a guest physical address.
2176 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002177static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2178 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179{
2180 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 gpa_t gpa;
2182
Avi Kivitybccf2152007-02-21 18:04:26 +02002183 vcpu_load(vcpu);
2184 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2186 tr->physical_address = gpa;
2187 tr->valid = gpa != UNMAPPED_GVA;
2188 tr->writeable = 1;
2189 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002190 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 vcpu_put(vcpu);
2192
2193 return 0;
2194}
2195
Avi Kivitybccf2152007-02-21 18:04:26 +02002196static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2197 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 if (irq->irq < 0 || irq->irq >= 256)
2200 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002201 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202
2203 set_bit(irq->irq, vcpu->irq_pending);
2204 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2205
2206 vcpu_put(vcpu);
2207
2208 return 0;
2209}
2210
Avi Kivitybccf2152007-02-21 18:04:26 +02002211static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2212 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214 int r;
2215
Avi Kivitybccf2152007-02-21 18:04:26 +02002216 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217
2218 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2219
2220 vcpu_put(vcpu);
2221
2222 return r;
2223}
2224
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002225static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2226 unsigned long address,
2227 int *type)
2228{
2229 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2230 unsigned long pgoff;
2231 struct page *page;
2232
2233 *type = VM_FAULT_MINOR;
2234 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002235 if (pgoff == 0)
2236 page = virt_to_page(vcpu->run);
2237 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2238 page = virt_to_page(vcpu->pio_data);
2239 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002240 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002241 get_page(page);
2242 return page;
2243}
2244
2245static struct vm_operations_struct kvm_vcpu_vm_ops = {
2246 .nopage = kvm_vcpu_nopage,
2247};
2248
2249static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2250{
2251 vma->vm_ops = &kvm_vcpu_vm_ops;
2252 return 0;
2253}
2254
Avi Kivitybccf2152007-02-21 18:04:26 +02002255static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2256{
2257 struct kvm_vcpu *vcpu = filp->private_data;
2258
2259 fput(vcpu->kvm->filp);
2260 return 0;
2261}
2262
2263static struct file_operations kvm_vcpu_fops = {
2264 .release = kvm_vcpu_release,
2265 .unlocked_ioctl = kvm_vcpu_ioctl,
2266 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002267 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002268};
2269
2270/*
2271 * Allocates an inode for the vcpu.
2272 */
2273static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2274{
2275 int fd, r;
2276 struct inode *inode;
2277 struct file *file;
2278
2279 atomic_inc(&vcpu->kvm->filp->f_count);
2280 inode = kvmfs_inode(&kvm_vcpu_fops);
2281 if (IS_ERR(inode)) {
2282 r = PTR_ERR(inode);
2283 goto out1;
2284 }
2285
2286 file = kvmfs_file(inode, vcpu);
2287 if (IS_ERR(file)) {
2288 r = PTR_ERR(file);
2289 goto out2;
2290 }
2291
2292 r = get_unused_fd();
2293 if (r < 0)
2294 goto out3;
2295 fd = r;
2296 fd_install(fd, file);
2297
2298 return fd;
2299
2300out3:
2301 fput(file);
2302out2:
2303 iput(inode);
2304out1:
2305 fput(vcpu->kvm->filp);
2306 return r;
2307}
2308
Avi Kivityc5ea7662007-02-20 18:41:05 +02002309/*
2310 * Creates some virtual cpus. Good luck creating more than one.
2311 */
2312static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2313{
2314 int r;
2315 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002316 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002317
2318 r = -EINVAL;
2319 if (!valid_vcpu(n))
2320 goto out;
2321
2322 vcpu = &kvm->vcpus[n];
2323
2324 mutex_lock(&vcpu->mutex);
2325
2326 if (vcpu->vmcs) {
2327 mutex_unlock(&vcpu->mutex);
2328 return -EEXIST;
2329 }
2330
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002331 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2332 r = -ENOMEM;
2333 if (!page)
2334 goto out_unlock;
2335 vcpu->run = page_address(page);
2336
Avi Kivity039576c2007-03-20 12:46:50 +02002337 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2338 r = -ENOMEM;
2339 if (!page)
2340 goto out_free_run;
2341 vcpu->pio_data = page_address(page);
2342
Avi Kivityc5ea7662007-02-20 18:41:05 +02002343 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2344 FX_IMAGE_ALIGN);
2345 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
Avi Kivityd917a6b2007-04-12 13:03:01 +03002346 vcpu->cr0 = 0x10;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002347
2348 r = kvm_arch_ops->vcpu_create(vcpu);
2349 if (r < 0)
2350 goto out_free_vcpus;
2351
2352 r = kvm_mmu_create(vcpu);
2353 if (r < 0)
2354 goto out_free_vcpus;
2355
2356 kvm_arch_ops->vcpu_load(vcpu);
2357 r = kvm_mmu_setup(vcpu);
2358 if (r >= 0)
2359 r = kvm_arch_ops->vcpu_setup(vcpu);
2360 vcpu_put(vcpu);
2361
2362 if (r < 0)
2363 goto out_free_vcpus;
2364
Avi Kivitybccf2152007-02-21 18:04:26 +02002365 r = create_vcpu_fd(vcpu);
2366 if (r < 0)
2367 goto out_free_vcpus;
2368
2369 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002370
2371out_free_vcpus:
2372 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002373out_free_run:
2374 free_page((unsigned long)vcpu->run);
2375 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002376out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002377 mutex_unlock(&vcpu->mutex);
2378out:
2379 return r;
2380}
2381
Eddie Dong2cc51562007-05-21 07:28:09 +03002382static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2383{
2384 u64 efer;
2385 int i;
2386 struct kvm_cpuid_entry *e, *entry;
2387
2388 rdmsrl(MSR_EFER, efer);
2389 entry = NULL;
2390 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2391 e = &vcpu->cpuid_entries[i];
2392 if (e->function == 0x80000001) {
2393 entry = e;
2394 break;
2395 }
2396 }
2397 if (entry && (entry->edx & EFER_NX) && !(efer & EFER_NX)) {
2398 entry->edx &= ~(1 << 20);
2399 printk(KERN_INFO ": guest NX capability removed\n");
2400 }
2401}
2402
Avi Kivity06465c52007-02-28 20:46:53 +02002403static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2404 struct kvm_cpuid *cpuid,
2405 struct kvm_cpuid_entry __user *entries)
2406{
2407 int r;
2408
2409 r = -E2BIG;
2410 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2411 goto out;
2412 r = -EFAULT;
2413 if (copy_from_user(&vcpu->cpuid_entries, entries,
2414 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2415 goto out;
2416 vcpu->cpuid_nent = cpuid->nent;
Eddie Dong2cc51562007-05-21 07:28:09 +03002417 cpuid_fix_nx_cap(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002418 return 0;
2419
2420out:
2421 return r;
2422}
2423
Avi Kivity1961d272007-03-05 19:46:05 +02002424static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2425{
2426 if (sigset) {
2427 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2428 vcpu->sigset_active = 1;
2429 vcpu->sigset = *sigset;
2430 } else
2431 vcpu->sigset_active = 0;
2432 return 0;
2433}
2434
Avi Kivityb8836732007-04-01 16:34:31 +03002435/*
2436 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2437 * we have asm/x86/processor.h
2438 */
2439struct fxsave {
2440 u16 cwd;
2441 u16 swd;
2442 u16 twd;
2443 u16 fop;
2444 u64 rip;
2445 u64 rdp;
2446 u32 mxcsr;
2447 u32 mxcsr_mask;
2448 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2449#ifdef CONFIG_X86_64
2450 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2451#else
2452 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2453#endif
2454};
2455
2456static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2457{
2458 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2459
2460 vcpu_load(vcpu);
2461
2462 memcpy(fpu->fpr, fxsave->st_space, 128);
2463 fpu->fcw = fxsave->cwd;
2464 fpu->fsw = fxsave->swd;
2465 fpu->ftwx = fxsave->twd;
2466 fpu->last_opcode = fxsave->fop;
2467 fpu->last_ip = fxsave->rip;
2468 fpu->last_dp = fxsave->rdp;
2469 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2470
2471 vcpu_put(vcpu);
2472
2473 return 0;
2474}
2475
2476static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2477{
2478 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2479
2480 vcpu_load(vcpu);
2481
2482 memcpy(fxsave->st_space, fpu->fpr, 128);
2483 fxsave->cwd = fpu->fcw;
2484 fxsave->swd = fpu->fsw;
2485 fxsave->twd = fpu->ftwx;
2486 fxsave->fop = fpu->last_opcode;
2487 fxsave->rip = fpu->last_ip;
2488 fxsave->rdp = fpu->last_dp;
2489 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2490
2491 vcpu_put(vcpu);
2492
2493 return 0;
2494}
2495
Avi Kivitybccf2152007-02-21 18:04:26 +02002496static long kvm_vcpu_ioctl(struct file *filp,
2497 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498{
Avi Kivitybccf2152007-02-21 18:04:26 +02002499 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002500 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 int r = -EINVAL;
2502
2503 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002504 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002505 r = -EINVAL;
2506 if (arg)
2507 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002508 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 case KVM_GET_REGS: {
2511 struct kvm_regs kvm_regs;
2512
Avi Kivitybccf2152007-02-21 18:04:26 +02002513 memset(&kvm_regs, 0, sizeof kvm_regs);
2514 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 if (r)
2516 goto out;
2517 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002518 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 goto out;
2520 r = 0;
2521 break;
2522 }
2523 case KVM_SET_REGS: {
2524 struct kvm_regs kvm_regs;
2525
2526 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002527 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002529 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 if (r)
2531 goto out;
2532 r = 0;
2533 break;
2534 }
2535 case KVM_GET_SREGS: {
2536 struct kvm_sregs kvm_sregs;
2537
Avi Kivitybccf2152007-02-21 18:04:26 +02002538 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2539 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540 if (r)
2541 goto out;
2542 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002543 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 goto out;
2545 r = 0;
2546 break;
2547 }
2548 case KVM_SET_SREGS: {
2549 struct kvm_sregs kvm_sregs;
2550
2551 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002552 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002553 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002554 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 if (r)
2556 goto out;
2557 r = 0;
2558 break;
2559 }
2560 case KVM_TRANSLATE: {
2561 struct kvm_translation tr;
2562
2563 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002564 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002566 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 if (r)
2568 goto out;
2569 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002570 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 goto out;
2572 r = 0;
2573 break;
2574 }
2575 case KVM_INTERRUPT: {
2576 struct kvm_interrupt irq;
2577
2578 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002579 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002581 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 if (r)
2583 goto out;
2584 r = 0;
2585 break;
2586 }
2587 case KVM_DEBUG_GUEST: {
2588 struct kvm_debug_guest dbg;
2589
2590 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002591 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002593 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 if (r)
2595 goto out;
2596 r = 0;
2597 break;
2598 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002599 case KVM_GET_MSRS:
2600 r = msr_io(vcpu, argp, get_msr, 1);
2601 break;
2602 case KVM_SET_MSRS:
2603 r = msr_io(vcpu, argp, do_set_msr, 0);
2604 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002605 case KVM_SET_CPUID: {
2606 struct kvm_cpuid __user *cpuid_arg = argp;
2607 struct kvm_cpuid cpuid;
2608
2609 r = -EFAULT;
2610 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2611 goto out;
2612 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2613 if (r)
2614 goto out;
2615 break;
2616 }
Avi Kivity1961d272007-03-05 19:46:05 +02002617 case KVM_SET_SIGNAL_MASK: {
2618 struct kvm_signal_mask __user *sigmask_arg = argp;
2619 struct kvm_signal_mask kvm_sigmask;
2620 sigset_t sigset, *p;
2621
2622 p = NULL;
2623 if (argp) {
2624 r = -EFAULT;
2625 if (copy_from_user(&kvm_sigmask, argp,
2626 sizeof kvm_sigmask))
2627 goto out;
2628 r = -EINVAL;
2629 if (kvm_sigmask.len != sizeof sigset)
2630 goto out;
2631 r = -EFAULT;
2632 if (copy_from_user(&sigset, sigmask_arg->sigset,
2633 sizeof sigset))
2634 goto out;
2635 p = &sigset;
2636 }
2637 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2638 break;
2639 }
Avi Kivityb8836732007-04-01 16:34:31 +03002640 case KVM_GET_FPU: {
2641 struct kvm_fpu fpu;
2642
2643 memset(&fpu, 0, sizeof fpu);
2644 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2645 if (r)
2646 goto out;
2647 r = -EFAULT;
2648 if (copy_to_user(argp, &fpu, sizeof fpu))
2649 goto out;
2650 r = 0;
2651 break;
2652 }
2653 case KVM_SET_FPU: {
2654 struct kvm_fpu fpu;
2655
2656 r = -EFAULT;
2657 if (copy_from_user(&fpu, argp, sizeof fpu))
2658 goto out;
2659 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2660 if (r)
2661 goto out;
2662 r = 0;
2663 break;
2664 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002665 default:
2666 ;
2667 }
2668out:
2669 return r;
2670}
2671
2672static long kvm_vm_ioctl(struct file *filp,
2673 unsigned int ioctl, unsigned long arg)
2674{
2675 struct kvm *kvm = filp->private_data;
2676 void __user *argp = (void __user *)arg;
2677 int r = -EINVAL;
2678
2679 switch (ioctl) {
2680 case KVM_CREATE_VCPU:
2681 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2682 if (r < 0)
2683 goto out;
2684 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685 case KVM_SET_MEMORY_REGION: {
2686 struct kvm_memory_region kvm_mem;
2687
2688 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002689 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002691 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692 if (r)
2693 goto out;
2694 break;
2695 }
2696 case KVM_GET_DIRTY_LOG: {
2697 struct kvm_dirty_log log;
2698
2699 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002700 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002702 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 if (r)
2704 goto out;
2705 break;
2706 }
Avi Kivitye8207542007-03-30 16:54:30 +03002707 case KVM_SET_MEMORY_ALIAS: {
2708 struct kvm_memory_alias alias;
2709
2710 r = -EFAULT;
2711 if (copy_from_user(&alias, argp, sizeof alias))
2712 goto out;
2713 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2714 if (r)
2715 goto out;
2716 break;
2717 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002718 default:
2719 ;
2720 }
2721out:
2722 return r;
2723}
2724
2725static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2726 unsigned long address,
2727 int *type)
2728{
2729 struct kvm *kvm = vma->vm_file->private_data;
2730 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002731 struct page *page;
2732
2733 *type = VM_FAULT_MINOR;
2734 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002735 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002736 if (!page)
2737 return NOPAGE_SIGBUS;
2738 get_page(page);
2739 return page;
2740}
2741
2742static struct vm_operations_struct kvm_vm_vm_ops = {
2743 .nopage = kvm_vm_nopage,
2744};
2745
2746static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2747{
2748 vma->vm_ops = &kvm_vm_vm_ops;
2749 return 0;
2750}
2751
2752static struct file_operations kvm_vm_fops = {
2753 .release = kvm_vm_release,
2754 .unlocked_ioctl = kvm_vm_ioctl,
2755 .compat_ioctl = kvm_vm_ioctl,
2756 .mmap = kvm_vm_mmap,
2757};
2758
2759static int kvm_dev_ioctl_create_vm(void)
2760{
2761 int fd, r;
2762 struct inode *inode;
2763 struct file *file;
2764 struct kvm *kvm;
2765
2766 inode = kvmfs_inode(&kvm_vm_fops);
2767 if (IS_ERR(inode)) {
2768 r = PTR_ERR(inode);
2769 goto out1;
2770 }
2771
2772 kvm = kvm_create_vm();
2773 if (IS_ERR(kvm)) {
2774 r = PTR_ERR(kvm);
2775 goto out2;
2776 }
2777
2778 file = kvmfs_file(inode, kvm);
2779 if (IS_ERR(file)) {
2780 r = PTR_ERR(file);
2781 goto out3;
2782 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002783 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002784
2785 r = get_unused_fd();
2786 if (r < 0)
2787 goto out4;
2788 fd = r;
2789 fd_install(fd, file);
2790
2791 return fd;
2792
2793out4:
2794 fput(file);
2795out3:
2796 kvm_destroy_vm(kvm);
2797out2:
2798 iput(inode);
2799out1:
2800 return r;
2801}
2802
2803static long kvm_dev_ioctl(struct file *filp,
2804 unsigned int ioctl, unsigned long arg)
2805{
2806 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002807 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002808
2809 switch (ioctl) {
2810 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002811 r = -EINVAL;
2812 if (arg)
2813 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002814 r = KVM_API_VERSION;
2815 break;
2816 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002817 r = -EINVAL;
2818 if (arg)
2819 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002820 r = kvm_dev_ioctl_create_vm();
2821 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002823 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 struct kvm_msr_list msr_list;
2825 unsigned n;
2826
2827 r = -EFAULT;
2828 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2829 goto out;
2830 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002831 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2833 goto out;
2834 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002835 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 goto out;
2837 r = -EFAULT;
2838 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002839 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002841 if (copy_to_user(user_msr_list->indices
2842 + num_msrs_to_save * sizeof(u32),
2843 &emulated_msrs,
2844 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2845 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002847 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002849 case KVM_CHECK_EXTENSION:
2850 /*
2851 * No extensions defined at present.
2852 */
2853 r = 0;
2854 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002855 case KVM_GET_VCPU_MMAP_SIZE:
2856 r = -EINVAL;
2857 if (arg)
2858 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002859 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002860 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 default:
2862 ;
2863 }
2864out:
2865 return r;
2866}
2867
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868static struct file_operations kvm_chardev_ops = {
2869 .open = kvm_dev_open,
2870 .release = kvm_dev_release,
2871 .unlocked_ioctl = kvm_dev_ioctl,
2872 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873};
2874
2875static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002876 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 "kvm",
2878 &kvm_chardev_ops,
2879};
2880
2881static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2882 void *v)
2883{
2884 if (val == SYS_RESTART) {
2885 /*
2886 * Some (well, at least mine) BIOSes hang on reboot if
2887 * in vmx root mode.
2888 */
2889 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002890 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 }
2892 return NOTIFY_OK;
2893}
2894
2895static struct notifier_block kvm_reboot_notifier = {
2896 .notifier_call = kvm_reboot,
2897 .priority = 0,
2898};
2899
Avi Kivity774c47f2007-02-12 00:54:47 -08002900/*
2901 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2902 * cached on it.
2903 */
2904static void decache_vcpus_on_cpu(int cpu)
2905{
2906 struct kvm *vm;
2907 struct kvm_vcpu *vcpu;
2908 int i;
2909
2910 spin_lock(&kvm_lock);
2911 list_for_each_entry(vm, &vm_list, vm_list)
2912 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2913 vcpu = &vm->vcpus[i];
2914 /*
2915 * If the vcpu is locked, then it is running on some
2916 * other cpu and therefore it is not cached on the
2917 * cpu in question.
2918 *
2919 * If it's not locked, check the last cpu it executed
2920 * on.
2921 */
2922 if (mutex_trylock(&vcpu->mutex)) {
2923 if (vcpu->cpu == cpu) {
2924 kvm_arch_ops->vcpu_decache(vcpu);
2925 vcpu->cpu = -1;
2926 }
2927 mutex_unlock(&vcpu->mutex);
2928 }
2929 }
2930 spin_unlock(&kvm_lock);
2931}
2932
2933static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2934 void *v)
2935{
2936 int cpu = (long)v;
2937
2938 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002939 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002940 case CPU_DOWN_PREPARE_FROZEN:
Avi Kivity774c47f2007-02-12 00:54:47 -08002941 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002942 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002943 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2944 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002945 decache_vcpus_on_cpu(cpu);
2946 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2947 NULL, 0, 1);
2948 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002949 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002950 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002951 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2952 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002953 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2954 NULL, 0, 1);
2955 break;
2956 }
2957 return NOTIFY_OK;
2958}
2959
2960static struct notifier_block kvm_cpu_notifier = {
2961 .notifier_call = kvm_cpu_hotplug,
2962 .priority = 20, /* must be > scheduler priority */
2963};
2964
Avi Kivity1165f5f2007-04-19 17:27:43 +03002965static u64 stat_get(void *_offset)
2966{
2967 unsigned offset = (long)_offset;
2968 u64 total = 0;
2969 struct kvm *kvm;
2970 struct kvm_vcpu *vcpu;
2971 int i;
2972
2973 spin_lock(&kvm_lock);
2974 list_for_each_entry(kvm, &vm_list, vm_list)
2975 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2976 vcpu = &kvm->vcpus[i];
2977 total += *(u32 *)((void *)vcpu + offset);
2978 }
2979 spin_unlock(&kvm_lock);
2980 return total;
2981}
2982
2983static void stat_set(void *offset, u64 val)
2984{
2985}
2986
2987DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
2988
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989static __init void kvm_init_debug(void)
2990{
2991 struct kvm_stats_debugfs_item *p;
2992
Al Viro8b6d44c2007-02-09 16:38:40 +00002993 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002995 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2996 (void *)(long)p->offset,
2997 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998}
2999
3000static void kvm_exit_debug(void)
3001{
3002 struct kvm_stats_debugfs_item *p;
3003
3004 for (p = debugfs_entries; p->name; ++p)
3005 debugfs_remove(p->dentry);
3006 debugfs_remove(debugfs_dir);
3007}
3008
Avi Kivity59ae6c62007-02-12 00:54:48 -08003009static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3010{
3011 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02003012 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003013 return 0;
3014}
3015
3016static int kvm_resume(struct sys_device *dev)
3017{
Avi Kivity19d14082007-02-19 14:37:48 +02003018 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003019 return 0;
3020}
3021
3022static struct sysdev_class kvm_sysdev_class = {
3023 set_kset_name("kvm"),
3024 .suspend = kvm_suspend,
3025 .resume = kvm_resume,
3026};
3027
3028static struct sys_device kvm_sysdev = {
3029 .id = 0,
3030 .cls = &kvm_sysdev_class,
3031};
3032
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033hpa_t bad_page_address;
3034
Avi Kivity37e29d92007-02-20 14:07:37 +02003035static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
3036 const char *dev_name, void *data, struct vfsmount *mnt)
3037{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02003038 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02003039}
3040
3041static struct file_system_type kvm_fs_type = {
3042 .name = "kvmfs",
3043 .get_sb = kvmfs_get_sb,
3044 .kill_sb = kill_anon_super,
3045};
3046
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3048{
3049 int r;
3050
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003051 if (kvm_arch_ops) {
3052 printk(KERN_ERR "kvm: already loaded the other module\n");
3053 return -EEXIST;
3054 }
3055
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003056 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 printk(KERN_ERR "kvm: no hardware support\n");
3058 return -EOPNOTSUPP;
3059 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003060 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 printk(KERN_ERR "kvm: disabled by bios\n");
3062 return -EOPNOTSUPP;
3063 }
3064
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003065 kvm_arch_ops = ops;
3066
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067 r = kvm_arch_ops->hardware_setup();
3068 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003069 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070
Al Viro8b6d44c2007-02-09 16:38:40 +00003071 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003072 r = register_cpu_notifier(&kvm_cpu_notifier);
3073 if (r)
3074 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 register_reboot_notifier(&kvm_reboot_notifier);
3076
Avi Kivity59ae6c62007-02-12 00:54:48 -08003077 r = sysdev_class_register(&kvm_sysdev_class);
3078 if (r)
3079 goto out_free_2;
3080
3081 r = sysdev_register(&kvm_sysdev);
3082 if (r)
3083 goto out_free_3;
3084
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085 kvm_chardev_ops.owner = module;
3086
3087 r = misc_register(&kvm_dev);
3088 if (r) {
3089 printk (KERN_ERR "kvm: misc device register failed\n");
3090 goto out_free;
3091 }
3092
3093 return r;
3094
3095out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003096 sysdev_unregister(&kvm_sysdev);
3097out_free_3:
3098 sysdev_class_unregister(&kvm_sysdev_class);
3099out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003101 unregister_cpu_notifier(&kvm_cpu_notifier);
3102out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00003103 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003105out:
3106 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 return r;
3108}
3109
3110void kvm_exit_arch(void)
3111{
3112 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003113 sysdev_unregister(&kvm_sysdev);
3114 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003116 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00003117 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003119 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120}
3121
3122static __init int kvm_init(void)
3123{
3124 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003125 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126
Avi Kivityb5a33a72007-04-15 16:31:09 +03003127 r = kvm_mmu_module_init();
3128 if (r)
3129 goto out4;
3130
Avi Kivity37e29d92007-02-20 14:07:37 +02003131 r = register_filesystem(&kvm_fs_type);
3132 if (r)
3133 goto out3;
3134
3135 kvmfs_mnt = kern_mount(&kvm_fs_type);
3136 r = PTR_ERR(kvmfs_mnt);
3137 if (IS_ERR(kvmfs_mnt))
3138 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139 kvm_init_debug();
3140
Michael Riepebf591b22006-12-22 01:05:36 -08003141 kvm_init_msr_list();
3142
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3144 r = -ENOMEM;
3145 goto out;
3146 }
3147
3148 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3149 memset(__va(bad_page_address), 0, PAGE_SIZE);
3150
Avi Kivity58e690e2007-02-26 16:29:43 +02003151 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152
3153out:
3154 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02003155 mntput(kvmfs_mnt);
3156out2:
3157 unregister_filesystem(&kvm_fs_type);
3158out3:
Avi Kivityb5a33a72007-04-15 16:31:09 +03003159 kvm_mmu_module_exit();
3160out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 return r;
3162}
3163
3164static __exit void kvm_exit(void)
3165{
3166 kvm_exit_debug();
3167 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02003168 mntput(kvmfs_mnt);
3169 unregister_filesystem(&kvm_fs_type);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003170 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171}
3172
3173module_init(kvm_init)
3174module_exit(kvm_exit)
3175
3176EXPORT_SYMBOL_GPL(kvm_init_arch);
3177EXPORT_SYMBOL_GPL(kvm_exit_arch);