blob: 8f7125710d02ff5141fd5477e6d8e687797c042b [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"
Carsten Otte043405e2007-10-10 17:16:19 +020019#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030021#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
23#include <linux/kvm.h>
24#include <linux/module.h>
25#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/percpu.h>
27#include <linux/gfp.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/debugfs.h>
33#include <linux/highmem.h>
34#include <linux/file.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080035#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080036#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040037#include <linux/sched.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030038#include <linux/cpumask.h>
39#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040040#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030041#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050042#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020043#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050044#include <linux/mman.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/processor.h>
47#include <asm/msr.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
52MODULE_AUTHOR("Qumranet");
53MODULE_LICENSE("GPL");
54
Avi Kivity133de902007-02-12 00:54:44 -080055static DEFINE_SPINLOCK(kvm_lock);
56static LIST_HEAD(vm_list);
57
Avi Kivity1b6c0162007-05-24 13:03:52 +030058static cpumask_t cpus_hardware_enabled;
59
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030060struct kvm_x86_ops *kvm_x86_ops;
Rusty Russellc16f8622007-07-30 21:12:19 +100061struct kmem_cache *kvm_vcpu_cache;
62EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
Avi Kivity1165f5f2007-04-19 17:27:43 +030063
Avi Kivity15ad7142007-07-11 18:17:21 +030064static __read_mostly struct preempt_ops kvm_preempt_ops;
65
Avi Kivity1165f5f2007-04-19 17:27:43 +030066#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080067
68static struct kvm_stats_debugfs_item {
69 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030070 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080071 struct dentry *dentry;
72} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030073 { "pf_fixed", STAT_OFFSET(pf_fixed) },
74 { "pf_guest", STAT_OFFSET(pf_guest) },
75 { "tlb_flush", STAT_OFFSET(tlb_flush) },
76 { "invlpg", STAT_OFFSET(invlpg) },
77 { "exits", STAT_OFFSET(exits) },
78 { "io_exits", STAT_OFFSET(io_exits) },
79 { "mmio_exits", STAT_OFFSET(mmio_exits) },
80 { "signal_exits", STAT_OFFSET(signal_exits) },
81 { "irq_window", STAT_OFFSET(irq_window_exits) },
82 { "halt_exits", STAT_OFFSET(halt_exits) },
Eddie Dongb6958ce2007-07-18 12:15:21 +030083 { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030084 { "request_irq", STAT_OFFSET(request_irq_exits) },
85 { "irq_exits", STAT_OFFSET(irq_exits) },
Avi Kivitye6adf282007-04-30 16:07:54 +030086 { "light_exits", STAT_OFFSET(light_exits) },
Eddie Dong2cc51562007-05-21 07:28:09 +030087 { "efer_reload", STAT_OFFSET(efer_reload) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030088 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080089};
90
91static struct dentry *debugfs_dir;
92
Avi Kivity6aa8b732006-12-10 02:21:36 -080093#define EFER_RESERVED_BITS 0xfffffffffffff2fe
94
Avi Kivitybccf2152007-02-21 18:04:26 +020095static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
96 unsigned long arg);
97
James Morris5aacf0c2006-12-22 01:04:55 -080098static inline int valid_vcpu(int n)
99{
100 return likely(n >= 0 && n < KVM_MAX_VCPUS);
101}
102
Avi Kivity7702fd12007-06-14 16:27:40 +0300103void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
104{
105 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
106 return;
107
108 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000109 fx_save(&vcpu->host_fx_image);
110 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300111}
112EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
113
114void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
115{
116 if (!vcpu->guest_fpu_loaded)
117 return;
118
119 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000120 fx_save(&vcpu->guest_fx_image);
121 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300122}
123EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
124
Avi Kivity6aa8b732006-12-10 02:21:36 -0800125/*
126 * Switches to specified vcpu, until a matching vcpu_put()
127 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200128void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129{
Avi Kivity15ad7142007-07-11 18:17:21 +0300130 int cpu;
131
Avi Kivitybccf2152007-02-21 18:04:26 +0200132 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300133 cpu = get_cpu();
134 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200135 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300136 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200137}
138
Carsten Otte313a3dc2007-10-11 19:16:52 +0200139void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140{
Avi Kivity15ad7142007-07-11 18:17:21 +0300141 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200142 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300143 preempt_notifier_unregister(&vcpu->preempt_notifier);
144 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145 mutex_unlock(&vcpu->mutex);
146}
147
Avi Kivityd9e368d2007-06-07 19:18:30 +0300148static void ack_flush(void *_completed)
149{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300150}
151
152void kvm_flush_remote_tlbs(struct kvm *kvm)
153{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200154 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300155 cpumask_t cpus;
156 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300157
Avi Kivityd9e368d2007-06-07 19:18:30 +0300158 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000159 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
160 vcpu = kvm->vcpus[i];
161 if (!vcpu)
162 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200163 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300164 continue;
165 cpu = vcpu->cpu;
166 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200167 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300168 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200169 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300170}
171
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000172int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
173{
174 struct page *page;
175 int r;
176
177 mutex_init(&vcpu->mutex);
178 vcpu->cpu = -1;
179 vcpu->mmu.root_hpa = INVALID_PAGE;
180 vcpu->kvm = kvm;
181 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300182 if (!irqchip_in_kernel(kvm) || id == 0)
183 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
184 else
185 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300186 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000187
188 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
189 if (!page) {
190 r = -ENOMEM;
191 goto fail;
192 }
193 vcpu->run = page_address(page);
194
195 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
196 if (!page) {
197 r = -ENOMEM;
198 goto fail_free_run;
199 }
200 vcpu->pio_data = page_address(page);
201
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000202 r = kvm_mmu_create(vcpu);
203 if (r < 0)
204 goto fail_free_pio_data;
205
Rusty Russell76fafa52007-10-08 10:50:48 +1000206 if (irqchip_in_kernel(kvm)) {
207 r = kvm_create_lapic(vcpu);
208 if (r < 0)
209 goto fail_mmu_destroy;
210 }
211
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000212 return 0;
213
Rusty Russell76fafa52007-10-08 10:50:48 +1000214fail_mmu_destroy:
215 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000216fail_free_pio_data:
217 free_page((unsigned long)vcpu->pio_data);
218fail_free_run:
219 free_page((unsigned long)vcpu->run);
220fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000221 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000222}
223EXPORT_SYMBOL_GPL(kvm_vcpu_init);
224
225void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
226{
Rusty Russelld5894442007-10-08 10:48:30 +1000227 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000228 kvm_mmu_destroy(vcpu);
229 free_page((unsigned long)vcpu->pio_data);
230 free_page((unsigned long)vcpu->run);
231}
232EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
233
Avi Kivityf17abe92007-02-21 19:28:04 +0200234static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235{
236 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237
238 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200239 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240
Eddie Dong74906342007-06-19 18:05:03 +0300241 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800242 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400244 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000245 spin_lock(&kvm_lock);
246 list_add(&kvm->vm_list, &vm_list);
247 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200248 return kvm;
249}
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251/*
252 * Free any memory in @free but not in @dont.
253 */
254static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
255 struct kvm_memory_slot *dont)
256{
Izik Eidus290fc382007-09-27 14:11:22 +0200257 if (!dont || free->rmap != dont->rmap)
258 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259
260 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
261 vfree(free->dirty_bitmap);
262
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000264 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500265 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266}
267
268static void kvm_free_physmem(struct kvm *kvm)
269{
270 int i;
271
272 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000273 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274}
275
Avi Kivity039576c2007-03-20 12:46:50 +0200276static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
277{
278 int i;
279
Rusty Russell3077c4512007-07-30 16:41:57 +1000280 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200281 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200282 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200283 vcpu->pio.guest_pages[i] = NULL;
284 }
285}
286
Avi Kivity7b53aa52007-06-05 12:17:03 +0300287static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
288{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300289 vcpu_load(vcpu);
290 kvm_mmu_unload(vcpu);
291 vcpu_put(vcpu);
292}
293
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294static void kvm_free_vcpus(struct kvm *kvm)
295{
296 unsigned int i;
297
Avi Kivity7b53aa52007-06-05 12:17:03 +0300298 /*
299 * Unpin any mmu pages first.
300 */
301 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000302 if (kvm->vcpus[i])
303 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
304 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
305 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300306 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000307 kvm->vcpus[i] = NULL;
308 }
309 }
310
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311}
312
Avi Kivityf17abe92007-02-21 19:28:04 +0200313static void kvm_destroy_vm(struct kvm *kvm)
314{
Avi Kivity133de902007-02-12 00:54:44 -0800315 spin_lock(&kvm_lock);
316 list_del(&kvm->vm_list);
317 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300318 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400319 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300320 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300321 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322 kvm_free_vcpus(kvm);
323 kvm_free_physmem(kvm);
324 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200325}
326
327static int kvm_vm_release(struct inode *inode, struct file *filp)
328{
329 struct kvm *kvm = filp->private_data;
330
331 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 return 0;
333}
334
335static void inject_gp(struct kvm_vcpu *vcpu)
336{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300337 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338}
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340void fx_init(struct kvm_vcpu *vcpu)
341{
Rusty Russellb114b082007-07-30 21:13:43 +1000342 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343
Rusty Russell9bd01502007-07-30 16:29:56 +1000344 /* Initialize guest FPU by resetting ours and saving into guest's */
345 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000346 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000348 fx_save(&vcpu->guest_fx_image);
349 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd01502007-07-30 16:29:56 +1000350 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351
Amit Shah380102c2007-08-25 11:35:52 +0300352 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000353 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
354 vcpu->guest_fx_image.mxcsr = 0x1f80;
355 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
356 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357}
358EXPORT_SYMBOL_GPL(fx_init);
359
360/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 * Allocate some memory and give it an address in the guest physical address
362 * space.
363 *
364 * Discontiguous memory is allowed, mostly for framebuffers.
365 */
Izik Eidus210c7c42007-10-24 23:52:57 +0200366int kvm_set_memory_region(struct kvm *kvm,
367 struct kvm_userspace_memory_region *mem,
368 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369{
370 int r;
371 gfn_t base_gfn;
372 unsigned long npages;
373 unsigned long i;
374 struct kvm_memory_slot *memslot;
375 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376
377 r = -EINVAL;
378 /* General sanity checks */
379 if (mem->memory_size & (PAGE_SIZE - 1))
380 goto out;
381 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
382 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200383 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 goto out;
385 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
386 goto out;
387
388 memslot = &kvm->memslots[mem->slot];
389 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
390 npages = mem->memory_size >> PAGE_SHIFT;
391
392 if (!npages)
393 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
394
Shaohua Li11ec2802007-07-23 14:51:37 +0800395 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 new = old = *memslot;
398
399 new.base_gfn = base_gfn;
400 new.npages = npages;
401 new.flags = mem->flags;
402
403 /* Disallow changing a memory slot's size. */
404 r = -EINVAL;
405 if (npages && old.npages && npages != old.npages)
406 goto out_unlock;
407
408 /* Check for overlaps */
409 r = -EEXIST;
410 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
411 struct kvm_memory_slot *s = &kvm->memslots[i];
412
413 if (s == memslot)
414 continue;
415 if (!((base_gfn + npages <= s->base_gfn) ||
416 (base_gfn >= s->base_gfn + s->npages)))
417 goto out_unlock;
418 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 /* Free page dirty bitmap if unneeded */
421 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000422 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423
424 r = -ENOMEM;
425
426 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500427 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400428 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200429
430 if (!new.rmap)
431 goto out_unlock;
432
Izik Eidus290fc382007-09-27 14:11:22 +0200433 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500434
Izik Eidus80b14b52007-10-25 11:54:04 +0200435 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500436 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200437 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500438 else {
439 down_write(&current->mm->mmap_sem);
440 new.userspace_addr = do_mmap(NULL, 0,
441 npages * PAGE_SIZE,
442 PROT_READ | PROT_WRITE,
443 MAP_SHARED | MAP_ANONYMOUS,
444 0);
445 up_write(&current->mm->mmap_sem);
446
447 if (IS_ERR((void *)new.userspace_addr))
448 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200450 } else {
451 if (!old.user_alloc && old.rmap) {
452 int ret;
453
454 down_write(&current->mm->mmap_sem);
455 ret = do_munmap(current->mm, old.userspace_addr,
456 old.npages * PAGE_SIZE);
457 up_write(&current->mm->mmap_sem);
458 if (ret < 0)
459 printk(KERN_WARNING
460 "kvm_vm_ioctl_set_memory_region: "
461 "failed to munmap memory\n");
462 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463 }
464
465 /* Allocate page dirty bitmap if needed */
466 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
467 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
468
469 new.dirty_bitmap = vmalloc(dirty_bytes);
470 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200471 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 memset(new.dirty_bitmap, 0, dirty_bytes);
473 }
474
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 if (mem->slot >= kvm->nmemslots)
476 kvm->nmemslots = mem->slot + 1;
477
Izik Eidus82ce2c92007-10-02 18:52:55 +0200478 if (!kvm->n_requested_mmu_pages) {
479 unsigned int n_pages;
480
481 if (npages) {
482 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
483 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
484 n_pages);
485 } else {
486 unsigned int nr_mmu_pages;
487
488 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
489 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
490 nr_mmu_pages = max(nr_mmu_pages,
491 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
492 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
493 }
494 }
495
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497
Avi Kivity90cb0522007-07-17 13:04:56 +0300498 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
499 kvm_flush_remote_tlbs(kvm);
500
Shaohua Li11ec2802007-07-23 14:51:37 +0800501 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 kvm_free_physmem_slot(&old, &new);
504 return 0;
505
506out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800507 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 kvm_free_physmem_slot(&new, &old);
509out:
510 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200511
512}
513EXPORT_SYMBOL_GPL(kvm_set_memory_region);
514
Carsten Otte1fe779f2007-10-29 16:08:35 +0100515int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
516 struct
517 kvm_userspace_memory_region *mem,
518 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200519{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200520 if (mem->slot >= KVM_MEMORY_SLOTS)
521 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200522 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523}
524
525/*
526 * Get (and clear) the dirty memory log for a memory slot.
527 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200528static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
529 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530{
531 struct kvm_memory_slot *memslot;
532 int r, i;
533 int n;
534 unsigned long any = 0;
535
Shaohua Li11ec2802007-07-23 14:51:37 +0800536 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538 r = -EINVAL;
539 if (log->slot >= KVM_MEMORY_SLOTS)
540 goto out;
541
542 memslot = &kvm->memslots[log->slot];
543 r = -ENOENT;
544 if (!memslot->dirty_bitmap)
545 goto out;
546
Uri Lublincd1a4a92007-02-22 16:43:09 +0200547 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548
Uri Lublincd1a4a92007-02-22 16:43:09 +0200549 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 any = memslot->dirty_bitmap[i];
551
552 r = -EFAULT;
553 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
554 goto out;
555
Rusty Russell39214912007-07-31 19:57:47 +1000556 /* If nothing is dirty, don't bother messing with page tables. */
557 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000558 kvm_mmu_slot_remove_write_access(kvm, log->slot);
559 kvm_flush_remote_tlbs(kvm);
560 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000561 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562
563 r = 0;
564
565out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800566 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 return r;
568}
569
Izik Eiduscea7bb22007-10-17 19:17:48 +0200570int is_error_page(struct page *page)
571{
572 return page == bad_page;
573}
574EXPORT_SYMBOL_GPL(is_error_page);
575
Izik Eidus290fc382007-09-27 14:11:22 +0200576gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300577{
578 int i;
579 struct kvm_mem_alias *alias;
580
581 for (i = 0; i < kvm->naliases; ++i) {
582 alias = &kvm->aliases[i];
583 if (gfn >= alias->base_gfn
584 && gfn < alias->base_gfn + alias->npages)
585 return alias->target_gfn + gfn - alias->base_gfn;
586 }
587 return gfn;
588}
589
590static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591{
592 int i;
593
594 for (i = 0; i < kvm->nmemslots; ++i) {
595 struct kvm_memory_slot *memslot = &kvm->memslots[i];
596
597 if (gfn >= memslot->base_gfn
598 && gfn < memslot->base_gfn + memslot->npages)
599 return memslot;
600 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000601 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800602}
Avi Kivitye8207542007-03-30 16:54:30 +0300603
604struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
605{
606 gfn = unalias_gfn(kvm, gfn);
607 return __gfn_to_memslot(kvm, gfn);
608}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609
Izik Eiduse0d62c72007-10-24 23:57:46 +0200610int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
611{
612 int i;
613
614 gfn = unalias_gfn(kvm, gfn);
615 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
616 struct kvm_memory_slot *memslot = &kvm->memslots[i];
617
618 if (gfn >= memslot->base_gfn
619 && gfn < memslot->base_gfn + memslot->npages)
620 return 1;
621 }
622 return 0;
623}
624EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
625
Avi Kivity954bbbc2007-03-30 14:02:32 +0300626struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
627{
628 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500629 struct page *page[1];
630 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300631
Avi Kivity60395222007-10-21 11:03:36 +0200632 might_sleep();
633
Avi Kivitye8207542007-03-30 16:54:30 +0300634 gfn = unalias_gfn(kvm, gfn);
635 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200636 if (!slot) {
637 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200638 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200639 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200640
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500641 down_read(&current->mm->mmap_sem);
642 npages = get_user_pages(current, current->mm,
643 slot->userspace_addr
644 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
645 1, 1, page, NULL);
646 up_read(&current->mm->mmap_sem);
647 if (npages != 1) {
648 get_page(bad_page);
649 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200650 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500651
652 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300653}
654EXPORT_SYMBOL_GPL(gfn_to_page);
655
Izik Eidus8a7ae052007-10-18 11:09:33 +0200656void kvm_release_page(struct page *page)
657{
658 if (!PageReserved(page))
659 SetPageDirty(page);
660 put_page(page);
661}
662EXPORT_SYMBOL_GPL(kvm_release_page);
663
Izik Eidus195aefd2007-10-01 22:14:18 +0200664static int next_segment(unsigned long len, int offset)
665{
666 if (len > PAGE_SIZE - offset)
667 return PAGE_SIZE - offset;
668 else
669 return len;
670}
671
672int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
673 int len)
674{
675 void *page_virt;
676 struct page *page;
677
678 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200679 if (is_error_page(page)) {
680 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200681 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200682 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200683 page_virt = kmap_atomic(page, KM_USER0);
684
685 memcpy(data, page_virt + offset, len);
686
687 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200688 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200689 return 0;
690}
691EXPORT_SYMBOL_GPL(kvm_read_guest_page);
692
693int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
694{
695 gfn_t gfn = gpa >> PAGE_SHIFT;
696 int seg;
697 int offset = offset_in_page(gpa);
698 int ret;
699
700 while ((seg = next_segment(len, offset)) != 0) {
701 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
702 if (ret < 0)
703 return ret;
704 offset = 0;
705 len -= seg;
706 data += seg;
707 ++gfn;
708 }
709 return 0;
710}
711EXPORT_SYMBOL_GPL(kvm_read_guest);
712
713int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
714 int offset, int len)
715{
716 void *page_virt;
717 struct page *page;
718
719 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200720 if (is_error_page(page)) {
721 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200722 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200723 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200724 page_virt = kmap_atomic(page, KM_USER0);
725
726 memcpy(page_virt + offset, data, len);
727
728 kunmap_atomic(page_virt, KM_USER0);
729 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200730 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200731 return 0;
732}
733EXPORT_SYMBOL_GPL(kvm_write_guest_page);
734
735int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
736 unsigned long len)
737{
738 gfn_t gfn = gpa >> PAGE_SHIFT;
739 int seg;
740 int offset = offset_in_page(gpa);
741 int ret;
742
743 while ((seg = next_segment(len, offset)) != 0) {
744 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
745 if (ret < 0)
746 return ret;
747 offset = 0;
748 len -= seg;
749 data += seg;
750 ++gfn;
751 }
752 return 0;
753}
754
755int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
756{
757 void *page_virt;
758 struct page *page;
759
760 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200761 if (is_error_page(page)) {
762 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200763 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200764 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200765 page_virt = kmap_atomic(page, KM_USER0);
766
767 memset(page_virt + offset, 0, len);
768
769 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200770 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200771 return 0;
772}
773EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
774
775int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
776{
777 gfn_t gfn = gpa >> PAGE_SHIFT;
778 int seg;
779 int offset = offset_in_page(gpa);
780 int ret;
781
782 while ((seg = next_segment(len, offset)) != 0) {
783 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
784 if (ret < 0)
785 return ret;
786 offset = 0;
787 len -= seg;
788 ++gfn;
789 }
790 return 0;
791}
792EXPORT_SYMBOL_GPL(kvm_clear_guest);
793
Rusty Russell7e9d6192007-07-31 20:41:14 +1000794/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
796{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300797 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798
Rusty Russell7e9d6192007-07-31 20:41:14 +1000799 memslot = __gfn_to_memslot(kvm, gfn);
800 if (memslot && memslot->dirty_bitmap) {
801 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802
Rusty Russell7e9d6192007-07-31 20:41:14 +1000803 /* avoid RMW */
804 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
805 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 }
807}
808
Laurent Viviere7d5d762007-07-30 13:41:19 +0300809int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300810 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300812 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 void *data = val;
815
816 while (bytes) {
817 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
818 unsigned offset = addr & (PAGE_SIZE-1);
819 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +0200820 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
822 if (gpa == UNMAPPED_GVA)
823 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200824 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
825 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
828 bytes -= tocopy;
829 data += tocopy;
830 addr += tocopy;
831 }
832
833 return X86EMUL_CONTINUE;
834}
Laurent Viviere7d5d762007-07-30 13:41:19 +0300835EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836
837static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300838 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300840 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841{
Rusty Russellf0242472007-08-01 10:48:02 +1000842 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843 return X86EMUL_UNHANDLEABLE;
844}
845
Eddie Dong97222cc2007-09-12 10:58:04 +0300846/*
847 * Only apic need an MMIO device hook, so shortcut now..
848 */
849static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
850 gpa_t addr)
851{
852 struct kvm_io_device *dev;
853
854 if (vcpu->apic) {
855 dev = &vcpu->apic->dev;
856 if (dev->in_range(dev, addr))
857 return dev;
858 }
859 return NULL;
860}
861
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400862static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
863 gpa_t addr)
864{
Eddie Dong97222cc2007-09-12 10:58:04 +0300865 struct kvm_io_device *dev;
866
867 dev = vcpu_find_pervcpu_dev(vcpu, addr);
868 if (dev == NULL)
869 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
870 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400871}
872
Eddie Dong74906342007-06-19 18:05:03 +0300873static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
874 gpa_t addr)
875{
876 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
877}
878
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300880 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300882 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400884 struct kvm_io_device *mmio_dev;
885 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
887 if (vcpu->mmio_read_completed) {
888 memcpy(val, vcpu->mmio_data, bytes);
889 vcpu->mmio_read_completed = 0;
890 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +0300891 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 == X86EMUL_CONTINUE)
893 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200894
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400895 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
896 if (gpa == UNMAPPED_GVA)
897 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400899 /*
900 * Is this MMIO handled locally?
901 */
902 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
903 if (mmio_dev) {
904 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
905 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400907
908 vcpu->mmio_needed = 1;
909 vcpu->mmio_phys_addr = gpa;
910 vcpu->mmio_size = bytes;
911 vcpu->mmio_is_write = 0;
912
913 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914}
915
Avi Kivityda4a00f2007-01-05 16:36:44 -0800916static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +0300917 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -0800918{
Izik Eidus195aefd2007-10-01 22:14:18 +0200919 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -0800920
Izik Eidus195aefd2007-10-01 22:14:18 +0200921 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
922 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -0800923 return 0;
Shaohua Life551882007-07-23 14:51:39 +0800924 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -0800925 return 1;
926}
927
Avi Kivityb0fcd902007-07-22 18:48:54 +0300928static int emulator_write_emulated_onepage(unsigned long addr,
929 const void *val,
930 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300931 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400933 struct kvm_io_device *mmio_dev;
934 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935
Avi Kivityc9047f52007-04-17 10:53:22 +0300936 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300937 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +0300939 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Avi Kivityda4a00f2007-01-05 16:36:44 -0800941 if (emulator_write_phys(vcpu, gpa, val, bytes))
942 return X86EMUL_CONTINUE;
943
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400944 /*
945 * Is this MMIO handled locally?
946 */
947 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
948 if (mmio_dev) {
949 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
950 return X86EMUL_CONTINUE;
951 }
952
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 vcpu->mmio_needed = 1;
954 vcpu->mmio_phys_addr = gpa;
955 vcpu->mmio_size = bytes;
956 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +0300957 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958
959 return X86EMUL_CONTINUE;
960}
961
Laurent Viviere7d5d762007-07-30 13:41:19 +0300962int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +0300963 const void *val,
964 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300965 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +0300966{
967 /* Crossing a page boundary? */
968 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
969 int rc, now;
970
971 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +0300972 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +0300973 if (rc != X86EMUL_CONTINUE)
974 return rc;
975 addr += now;
976 val += now;
977 bytes -= now;
978 }
Laurent Viviercebff022007-07-30 13:35:24 +0300979 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +0300980}
Laurent Viviere7d5d762007-07-30 13:41:19 +0300981EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +0300982
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300984 const void *old,
985 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300987 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988{
989 static int reported;
990
991 if (!reported) {
992 reported = 1;
993 printk(KERN_WARNING "kvm: emulating exchange as write\n");
994 }
Laurent Viviercebff022007-07-30 13:35:24 +0300995 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996}
997
998static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
999{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001000 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001}
1002
1003int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1004{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 return X86EMUL_CONTINUE;
1006}
1007
1008int emulate_clts(struct kvm_vcpu *vcpu)
1009{
Amit Shah404fb882007-11-19 17:57:35 +02001010 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011 return X86EMUL_CONTINUE;
1012}
1013
Mike Dayd77c26f2007-10-08 09:02:08 -04001014int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015{
1016 struct kvm_vcpu *vcpu = ctxt->vcpu;
1017
1018 switch (dr) {
1019 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001020 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 return X86EMUL_CONTINUE;
1022 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001023 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 return X86EMUL_UNHANDLEABLE;
1025 }
1026}
1027
1028int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1029{
1030 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1031 int exception;
1032
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001033 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 if (exception) {
1035 /* FIXME: better handling */
1036 return X86EMUL_UNHANDLEABLE;
1037 }
1038 return X86EMUL_CONTINUE;
1039}
1040
Avi Kivity054b1362007-09-12 13:21:09 +03001041void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042{
1043 static int reported;
1044 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001045 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 unsigned long rip_linear;
1047
Avi Kivity054b1362007-09-12 13:21:09 +03001048 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049
1050 if (reported)
1051 return;
1052
Avi Kivity054b1362007-09-12 13:21:09 +03001053 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054
Avi Kivity054b1362007-09-12 13:21:09 +03001055 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1056 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 reported = 1;
1058}
Avi Kivity054b1362007-09-12 13:21:09 +03001059EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060
1061struct x86_emulate_ops emulate_ops = {
1062 .read_std = emulator_read_std,
1063 .write_std = emulator_write_std,
1064 .read_emulated = emulator_read_emulated,
1065 .write_emulated = emulator_write_emulated,
1066 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1067};
1068
1069int emulate_instruction(struct kvm_vcpu *vcpu,
1070 struct kvm_run *run,
1071 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001072 u16 error_code,
1073 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074{
Laurent Viviera22436b2007-09-24 17:00:58 +02001075 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076
Avi Kivitye7df56e2007-03-14 15:54:54 +02001077 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001078 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001081 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001082
1083 if (!no_decode) {
1084 int cs_db, cs_l;
1085 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1086
1087 vcpu->emulate_ctxt.vcpu = vcpu;
1088 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1089 vcpu->emulate_ctxt.cr2 = cr2;
1090 vcpu->emulate_ctxt.mode =
1091 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1092 ? X86EMUL_MODE_REAL : cs_l
1093 ? X86EMUL_MODE_PROT64 : cs_db
1094 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1095
1096 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1097 vcpu->emulate_ctxt.cs_base = 0;
1098 vcpu->emulate_ctxt.ds_base = 0;
1099 vcpu->emulate_ctxt.es_base = 0;
1100 vcpu->emulate_ctxt.ss_base = 0;
1101 } else {
1102 vcpu->emulate_ctxt.cs_base =
1103 get_segment_base(vcpu, VCPU_SREG_CS);
1104 vcpu->emulate_ctxt.ds_base =
1105 get_segment_base(vcpu, VCPU_SREG_DS);
1106 vcpu->emulate_ctxt.es_base =
1107 get_segment_base(vcpu, VCPU_SREG_ES);
1108 vcpu->emulate_ctxt.ss_base =
1109 get_segment_base(vcpu, VCPU_SREG_SS);
1110 }
1111
1112 vcpu->emulate_ctxt.gs_base =
1113 get_segment_base(vcpu, VCPU_SREG_GS);
1114 vcpu->emulate_ctxt.fs_base =
1115 get_segment_base(vcpu, VCPU_SREG_FS);
1116
1117 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001118 if (r) {
1119 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1120 return EMULATE_DONE;
1121 return EMULATE_FAIL;
1122 }
Laurent Vivier34273182007-09-18 11:27:37 +02001123 }
1124
Laurent Viviera22436b2007-09-24 17:00:58 +02001125 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001126
Laurent Viviere70669a2007-08-05 10:36:40 +03001127 if (vcpu->pio.string)
1128 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129
1130 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001131 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1133 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1134 run->mmio.len = vcpu->mmio_size;
1135 run->mmio.is_write = vcpu->mmio_is_write;
1136 }
1137
1138 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001139 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1140 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001142 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 return EMULATE_FAIL;
1144 }
1145 return EMULATE_DO_MMIO;
1146 }
1147
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001148 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001149 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150
Avi Kivity02c83202007-04-29 15:02:17 +03001151 if (vcpu->mmio_is_write) {
1152 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001154 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155
1156 return EMULATE_DONE;
1157}
1158EXPORT_SYMBOL_GPL(emulate_instruction);
1159
Eddie Dongb6958ce2007-07-18 12:15:21 +03001160/*
1161 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1162 */
He, Qingc5ec1532007-09-03 17:07:41 +03001163static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001164{
1165 DECLARE_WAITQUEUE(wait, current);
1166
1167 add_wait_queue(&vcpu->wq, &wait);
1168
1169 /*
1170 * We will block until either an interrupt or a signal wakes us up
1171 */
He, Qingc5ec1532007-09-03 17:07:41 +03001172 while (!kvm_cpu_has_interrupt(vcpu)
1173 && !signal_pending(current)
1174 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1175 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001176 set_current_state(TASK_INTERRUPTIBLE);
1177 vcpu_put(vcpu);
1178 schedule();
1179 vcpu_load(vcpu);
1180 }
1181
He, Qingc5ec1532007-09-03 17:07:41 +03001182 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001183 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001184}
1185
Avi Kivityd3bef152007-06-05 15:53:05 +03001186int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1187{
Avi Kivityd3bef152007-06-05 15:53:05 +03001188 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001189 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001190 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1191 kvm_vcpu_block(vcpu);
1192 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1193 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001194 return 1;
1195 } else {
1196 vcpu->run->exit_reason = KVM_EXIT_HLT;
1197 return 0;
1198 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001199}
1200EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1201
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001202int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001203{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001204 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001205
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001206 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001207
1208 nr = vcpu->regs[VCPU_REGS_RAX];
1209 a0 = vcpu->regs[VCPU_REGS_RBX];
1210 a1 = vcpu->regs[VCPU_REGS_RCX];
1211 a2 = vcpu->regs[VCPU_REGS_RDX];
1212 a3 = vcpu->regs[VCPU_REGS_RSI];
1213
1214 if (!is_long_mode(vcpu)) {
1215 nr &= 0xFFFFFFFF;
1216 a0 &= 0xFFFFFFFF;
1217 a1 &= 0xFFFFFFFF;
1218 a2 &= 0xFFFFFFFF;
1219 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001220 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001221
Avi Kivity270fd9b2007-02-19 14:37:47 +02001222 switch (nr) {
1223 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001224 ret = -KVM_ENOSYS;
1225 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001226 }
1227 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001228 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001229 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001230}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001231EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1232
1233int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1234{
1235 char instruction[3];
1236 int ret = 0;
1237
1238 mutex_lock(&vcpu->kvm->lock);
1239
1240 /*
1241 * Blow out the MMU to ensure that no other VCPU has an active mapping
1242 * to ensure that the updated hypercall appears atomically across all
1243 * VCPUs.
1244 */
1245 kvm_mmu_zap_all(vcpu->kvm);
1246
1247 kvm_x86_ops->cache_regs(vcpu);
1248 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1249 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1250 != X86EMUL_CONTINUE)
1251 ret = -EFAULT;
1252
1253 mutex_unlock(&vcpu->kvm->lock);
1254
1255 return ret;
1256}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001257
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1259{
1260 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1261}
1262
1263void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1264{
1265 struct descriptor_table dt = { limit, base };
1266
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001267 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268}
1269
1270void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1271{
1272 struct descriptor_table dt = { limit, base };
1273
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001274 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275}
1276
1277void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1278 unsigned long *rflags)
1279{
1280 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001281 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
1283
1284unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1285{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001286 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 switch (cr) {
1288 case 0:
1289 return vcpu->cr0;
1290 case 2:
1291 return vcpu->cr2;
1292 case 3:
1293 return vcpu->cr3;
1294 case 4:
1295 return vcpu->cr4;
1296 default:
1297 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1298 return 0;
1299 }
1300}
1301
1302void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1303 unsigned long *rflags)
1304{
1305 switch (cr) {
1306 case 0:
1307 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001308 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 break;
1310 case 2:
1311 vcpu->cr2 = val;
1312 break;
1313 case 3:
1314 set_cr3(vcpu, val);
1315 break;
1316 case 4:
1317 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1318 break;
1319 default:
1320 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1321 }
1322}
1323
Avi Kivity3bab1f52006-12-29 16:49:48 -08001324int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1325{
1326 u64 data;
1327
1328 switch (msr) {
1329 case 0xc0010010: /* SYSCFG */
1330 case 0xc0010015: /* HWCR */
1331 case MSR_IA32_PLATFORM_ID:
1332 case MSR_IA32_P5_MC_ADDR:
1333 case MSR_IA32_P5_MC_TYPE:
1334 case MSR_IA32_MC0_CTL:
1335 case MSR_IA32_MCG_STATUS:
1336 case MSR_IA32_MCG_CAP:
1337 case MSR_IA32_MC0_MISC:
1338 case MSR_IA32_MC0_MISC+4:
1339 case MSR_IA32_MC0_MISC+8:
1340 case MSR_IA32_MC0_MISC+12:
1341 case MSR_IA32_MC0_MISC+16:
1342 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001343 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001344 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001345 /* MTRR registers */
1346 case 0xfe:
1347 case 0x200 ... 0x2ff:
1348 data = 0;
1349 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001350 case 0xcd: /* fsb frequency */
1351 data = 3;
1352 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001353 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001354 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001355 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001356 case MSR_IA32_MISC_ENABLE:
1357 data = vcpu->ia32_misc_enable_msr;
1358 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001359#ifdef CONFIG_X86_64
1360 case MSR_EFER:
1361 data = vcpu->shadow_efer;
1362 break;
1363#endif
1364 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001365 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001366 return 1;
1367 }
1368 *pdata = data;
1369 return 0;
1370}
1371EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1372
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373/*
1374 * Reads an msr value (of 'msr_index') into 'pdata'.
1375 * Returns 0 on success, non-0 otherwise.
1376 * Assumes vcpu_load() was already called.
1377 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001378int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001380 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001383#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
Avi Kivity3bab1f52006-12-29 16:49:48 -08001385static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 if (efer & EFER_RESERVED_BITS) {
1388 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1389 efer);
1390 inject_gp(vcpu);
1391 return;
1392 }
1393
1394 if (is_paging(vcpu)
1395 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1396 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1397 inject_gp(vcpu);
1398 return;
1399 }
1400
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001401 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001402
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 efer &= ~EFER_LMA;
1404 efer |= vcpu->shadow_efer & EFER_LMA;
1405
1406 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408
1409#endif
1410
Avi Kivity3bab1f52006-12-29 16:49:48 -08001411int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1412{
1413 switch (msr) {
1414#ifdef CONFIG_X86_64
1415 case MSR_EFER:
1416 set_efer(vcpu, data);
1417 break;
1418#endif
1419 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001420 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001421 __FUNCTION__, data);
1422 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001423 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001424 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001425 __FUNCTION__, data);
1426 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001427 case MSR_IA32_UCODE_REV:
1428 case MSR_IA32_UCODE_WRITE:
1429 case 0x200 ... 0x2ff: /* MTRRs */
1430 break;
1431 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001432 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001433 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001434 case MSR_IA32_MISC_ENABLE:
1435 vcpu->ia32_misc_enable_msr = data;
1436 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001437 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001438 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001439 return 1;
1440 }
1441 return 0;
1442}
1443EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1444
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445/*
1446 * Writes msr value into into the appropriate "register".
1447 * Returns 0 on success, non-0 otherwise.
1448 * Assumes vcpu_load() was already called.
1449 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001450int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001452 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
1454
1455void kvm_resched(struct kvm_vcpu *vcpu)
1456{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001457 if (!need_resched())
1458 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460}
1461EXPORT_SYMBOL_GPL(kvm_resched);
1462
Avi Kivity06465c52007-02-28 20:46:53 +02001463void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1464{
1465 int i;
1466 u32 function;
1467 struct kvm_cpuid_entry *e, *best;
1468
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001469 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001470 function = vcpu->regs[VCPU_REGS_RAX];
1471 vcpu->regs[VCPU_REGS_RAX] = 0;
1472 vcpu->regs[VCPU_REGS_RBX] = 0;
1473 vcpu->regs[VCPU_REGS_RCX] = 0;
1474 vcpu->regs[VCPU_REGS_RDX] = 0;
1475 best = NULL;
1476 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1477 e = &vcpu->cpuid_entries[i];
1478 if (e->function == function) {
1479 best = e;
1480 break;
1481 }
1482 /*
1483 * Both basic or both extended?
1484 */
1485 if (((e->function ^ function) & 0x80000000) == 0)
1486 if (!best || e->function > best->function)
1487 best = e;
1488 }
1489 if (best) {
1490 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1491 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1492 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1493 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1494 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001495 kvm_x86_ops->decache_regs(vcpu);
1496 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001497}
1498EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1499
Avi Kivity039576c2007-03-20 12:46:50 +02001500static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001501{
Avi Kivity039576c2007-03-20 12:46:50 +02001502 void *p = vcpu->pio_data;
1503 void *q;
1504 unsigned bytes;
1505 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1506
Avi Kivity039576c2007-03-20 12:46:50 +02001507 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1508 PAGE_KERNEL);
1509 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001510 free_pio_guest_pages(vcpu);
1511 return -ENOMEM;
1512 }
1513 q += vcpu->pio.guest_page_offset;
1514 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1515 if (vcpu->pio.in)
1516 memcpy(q, p, bytes);
1517 else
1518 memcpy(p, q, bytes);
1519 q -= vcpu->pio.guest_page_offset;
1520 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001521 free_pio_guest_pages(vcpu);
1522 return 0;
1523}
1524
1525static int complete_pio(struct kvm_vcpu *vcpu)
1526{
1527 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001528 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001529 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001530
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001531 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001532
1533 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001534 if (io->in)
1535 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001536 io->size);
1537 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001538 if (io->in) {
1539 r = pio_copy_data(vcpu);
1540 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001541 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001542 return r;
1543 }
1544 }
1545
Avi Kivity46fc1472007-02-22 19:39:30 +02001546 delta = 1;
1547 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001548 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001549 /*
1550 * The size of the register should really depend on
1551 * current address size.
1552 */
1553 vcpu->regs[VCPU_REGS_RCX] -= delta;
1554 }
Avi Kivity039576c2007-03-20 12:46:50 +02001555 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001556 delta = -delta;
1557 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001558 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001559 vcpu->regs[VCPU_REGS_RDI] += delta;
1560 else
1561 vcpu->regs[VCPU_REGS_RSI] += delta;
1562 }
1563
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001564 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001565
Avi Kivity039576c2007-03-20 12:46:50 +02001566 io->count -= io->cur_count;
1567 io->cur_count = 0;
1568
Avi Kivity039576c2007-03-20 12:46:50 +02001569 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001570}
1571
Eddie Dong65619eb2007-07-17 11:52:33 +03001572static void kernel_pio(struct kvm_io_device *pio_dev,
1573 struct kvm_vcpu *vcpu,
1574 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001575{
1576 /* TODO: String I/O for in kernel device */
1577
Eddie Dong9cf98822007-07-22 10:36:31 +03001578 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001579 if (vcpu->pio.in)
1580 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1581 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001582 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001583 else
1584 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1585 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001586 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001587 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001588}
1589
1590static void pio_string_write(struct kvm_io_device *pio_dev,
1591 struct kvm_vcpu *vcpu)
1592{
1593 struct kvm_pio_request *io = &vcpu->pio;
1594 void *pd = vcpu->pio_data;
1595 int i;
1596
Eddie Dong9cf98822007-07-22 10:36:31 +03001597 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001598 for (i = 0; i < io->cur_count; i++) {
1599 kvm_iodevice_write(pio_dev, io->port,
1600 io->size,
1601 pd);
1602 pd += io->size;
1603 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001604 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001605}
1606
Mike Dayd77c26f2007-10-08 09:02:08 -04001607int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001608 int size, unsigned port)
1609{
1610 struct kvm_io_device *pio_dev;
1611
1612 vcpu->run->exit_reason = KVM_EXIT_IO;
1613 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1614 vcpu->run->io.size = vcpu->pio.size = size;
1615 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1616 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1617 vcpu->run->io.port = vcpu->pio.port = port;
1618 vcpu->pio.in = in;
1619 vcpu->pio.string = 0;
1620 vcpu->pio.down = 0;
1621 vcpu->pio.guest_page_offset = 0;
1622 vcpu->pio.rep = 0;
1623
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001624 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001625 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001626 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001627
Avi Kivity0967b7b2007-09-15 17:34:36 +03001628 kvm_x86_ops->skip_emulated_instruction(vcpu);
1629
Laurent Vivier3090dd72007-08-05 10:43:32 +03001630 pio_dev = vcpu_find_pio_dev(vcpu, port);
1631 if (pio_dev) {
1632 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1633 complete_pio(vcpu);
1634 return 1;
1635 }
1636 return 0;
1637}
1638EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1639
1640int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1641 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001642 gva_t address, int rep, unsigned port)
1643{
1644 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001645 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001646 int nr_pages = 1;
1647 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001648 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001649
1650 vcpu->run->exit_reason = KVM_EXIT_IO;
1651 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001652 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001653 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001654 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1655 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001656 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001657 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001658 vcpu->pio.down = down;
1659 vcpu->pio.guest_page_offset = offset_in_page(address);
1660 vcpu->pio.rep = rep;
1661
Avi Kivity039576c2007-03-20 12:46:50 +02001662 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001663 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001664 return 1;
1665 }
1666
Avi Kivity039576c2007-03-20 12:46:50 +02001667 if (!down)
1668 in_page = PAGE_SIZE - offset_in_page(address);
1669 else
1670 in_page = offset_in_page(address) + size;
1671 now = min(count, (unsigned long)in_page / size);
1672 if (!now) {
1673 /*
1674 * String I/O straddles page boundary. Pin two guest pages
1675 * so that we satisfy atomicity constraints. Do just one
1676 * transaction to avoid complexity.
1677 */
1678 nr_pages = 2;
1679 now = 1;
1680 }
1681 if (down) {
1682 /*
1683 * String I/O in reverse. Yuck. Kill the guest, fix later.
1684 */
Rusty Russellf0242472007-08-01 10:48:02 +10001685 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001686 inject_gp(vcpu);
1687 return 1;
1688 }
1689 vcpu->run->io.count = now;
1690 vcpu->pio.cur_count = now;
1691
Avi Kivity0967b7b2007-09-15 17:34:36 +03001692 if (vcpu->pio.cur_count == vcpu->pio.count)
1693 kvm_x86_ops->skip_emulated_instruction(vcpu);
1694
Avi Kivity039576c2007-03-20 12:46:50 +02001695 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001696 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001697 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02001698 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001699 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001700 if (!page) {
1701 inject_gp(vcpu);
1702 free_pio_guest_pages(vcpu);
1703 return 1;
1704 }
1705 }
1706
Laurent Vivier3090dd72007-08-05 10:43:32 +03001707 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001708 if (!vcpu->pio.in) {
1709 /* string PIO write */
1710 ret = pio_copy_data(vcpu);
1711 if (ret >= 0 && pio_dev) {
1712 pio_string_write(pio_dev, vcpu);
1713 complete_pio(vcpu);
1714 if (vcpu->pio.count == 0)
1715 ret = 1;
1716 }
1717 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10001718 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03001719 "port %x size %d count %ld\n",
1720 port, size, count);
1721
1722 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001723}
Laurent Vivier3090dd72007-08-05 10:43:32 +03001724EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02001725
Avi Kivity04d2cc72007-09-10 18:10:54 +03001726/*
1727 * Check if userspace requested an interrupt window, and that the
1728 * interrupt window is open.
1729 *
1730 * No need to exit to userspace if we already have an interrupt queued.
1731 */
1732static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1733 struct kvm_run *kvm_run)
1734{
1735 return (!vcpu->irq_summary &&
1736 kvm_run->request_interrupt_window &&
1737 vcpu->interrupt_window_open &&
1738 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1739}
1740
1741static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1742 struct kvm_run *kvm_run)
1743{
1744 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1745 kvm_run->cr8 = get_cr8(vcpu);
1746 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1747 if (irqchip_in_kernel(vcpu->kvm))
1748 kvm_run->ready_for_interrupt_injection = 1;
1749 else
1750 kvm_run->ready_for_interrupt_injection =
1751 (vcpu->interrupt_window_open &&
1752 vcpu->irq_summary == 0);
1753}
1754
1755static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1756{
1757 int r;
1758
1759 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001760 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03001761 vcpu->vcpu_id, vcpu->sipi_vector);
1762 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001763 r = kvm_x86_ops->vcpu_reset(vcpu);
1764 if (r)
1765 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001766 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
1767 }
1768
1769preempted:
1770 if (vcpu->guest_debug.enabled)
1771 kvm_x86_ops->guest_debug_pre(vcpu);
1772
1773again:
1774 r = kvm_mmu_reload(vcpu);
1775 if (unlikely(r))
1776 goto out;
1777
Avi Kivityab6ef342007-10-16 16:23:22 +02001778 kvm_inject_pending_timer_irqs(vcpu);
1779
Avi Kivity04d2cc72007-09-10 18:10:54 +03001780 preempt_disable();
1781
1782 kvm_x86_ops->prepare_guest_switch(vcpu);
1783 kvm_load_guest_fpu(vcpu);
1784
1785 local_irq_disable();
1786
1787 if (signal_pending(current)) {
1788 local_irq_enable();
1789 preempt_enable();
1790 r = -EINTR;
1791 kvm_run->exit_reason = KVM_EXIT_INTR;
1792 ++vcpu->stat.signal_exits;
1793 goto out;
1794 }
1795
1796 if (irqchip_in_kernel(vcpu->kvm))
1797 kvm_x86_ops->inject_pending_irq(vcpu);
1798 else if (!vcpu->mmio_read_completed)
1799 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
1800
1801 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02001802 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03001803
1804 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02001805 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03001806 kvm_x86_ops->tlb_flush(vcpu);
1807
1808 kvm_x86_ops->run(vcpu, kvm_run);
1809
1810 vcpu->guest_mode = 0;
1811 local_irq_enable();
1812
1813 ++vcpu->stat.exits;
1814
Laurent Vivier0552f732007-10-18 15:19:01 +02001815 /*
1816 * We must have an instruction between local_irq_enable() and
1817 * kvm_guest_exit(), so the timer interrupt isn't delayed by
1818 * the interrupt shadow. The stat.exits increment will do nicely.
1819 * But we need to prevent reordering, hence this barrier():
1820 */
1821 barrier();
1822
1823 kvm_guest_exit();
1824
Avi Kivity04d2cc72007-09-10 18:10:54 +03001825 preempt_enable();
1826
1827 /*
1828 * Profile KVM exit RIPs:
1829 */
1830 if (unlikely(prof_on == KVM_PROFILING)) {
1831 kvm_x86_ops->cache_regs(vcpu);
1832 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
1833 }
1834
1835 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
1836
1837 if (r > 0) {
1838 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
1839 r = -EINTR;
1840 kvm_run->exit_reason = KVM_EXIT_INTR;
1841 ++vcpu->stat.request_irq_exits;
1842 goto out;
1843 }
1844 if (!need_resched()) {
1845 ++vcpu->stat.light_exits;
1846 goto again;
1847 }
1848 }
1849
1850out:
1851 if (r > 0) {
1852 kvm_resched(vcpu);
1853 goto preempted;
1854 }
1855
1856 post_kvm_run_save(vcpu, kvm_run);
1857
1858 return r;
1859}
1860
1861
Avi Kivitybccf2152007-02-21 18:04:26 +02001862static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001865 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866
Avi Kivitybccf2152007-02-21 18:04:26 +02001867 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868
He, Qingc5ec1532007-09-03 17:07:41 +03001869 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
1870 kvm_vcpu_block(vcpu);
1871 vcpu_put(vcpu);
1872 return -EAGAIN;
1873 }
1874
Avi Kivity1961d272007-03-05 19:46:05 +02001875 if (vcpu->sigset_active)
1876 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1877
Dor Laor54810342007-02-12 00:54:39 -08001878 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08001879 if (!irqchip_in_kernel(vcpu->kvm))
1880 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08001881
Avi Kivity02c83202007-04-29 15:02:17 +03001882 if (vcpu->pio.cur_count) {
1883 r = complete_pio(vcpu);
1884 if (r)
1885 goto out;
1886 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08001887#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03001888 if (vcpu->mmio_needed) {
1889 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1890 vcpu->mmio_read_completed = 1;
1891 vcpu->mmio_needed = 0;
1892 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02001893 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03001894 if (r == EMULATE_DO_MMIO) {
1895 /*
1896 * Read-modify-write. Back to userspace.
1897 */
Avi Kivity02c83202007-04-29 15:02:17 +03001898 r = 0;
1899 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001900 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08001902#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02001903 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001904 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02001905 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001906 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02001907 }
1908
Avi Kivity04d2cc72007-09-10 18:10:54 +03001909 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910
Avi Kivity039576c2007-03-20 12:46:50 +02001911out:
Avi Kivity1961d272007-03-05 19:46:05 +02001912 if (vcpu->sigset_active)
1913 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1914
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 vcpu_put(vcpu);
1916 return r;
1917}
1918
Avi Kivitybccf2152007-02-21 18:04:26 +02001919static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1920 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921{
Avi Kivitybccf2152007-02-21 18:04:26 +02001922 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001923
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001924 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925
1926 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1927 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1928 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1929 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1930 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1931 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1932 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1933 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001934#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1936 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1937 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1938 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1939 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1940 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1941 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1942 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1943#endif
1944
1945 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001946 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947
1948 /*
1949 * Don't leak debug flags in case they were set for guest debugging
1950 */
1951 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1952 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1953
1954 vcpu_put(vcpu);
1955
1956 return 0;
1957}
1958
Avi Kivitybccf2152007-02-21 18:04:26 +02001959static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1960 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961{
Avi Kivitybccf2152007-02-21 18:04:26 +02001962 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963
1964 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1965 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1966 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1967 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1968 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1969 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1970 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1971 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001972#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1974 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1975 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1976 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1977 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1978 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1979 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1980 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1981#endif
1982
1983 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001984 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001986 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001987
1988 vcpu_put(vcpu);
1989
1990 return 0;
1991}
1992
1993static void get_segment(struct kvm_vcpu *vcpu,
1994 struct kvm_segment *var, int seg)
1995{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001996 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997}
1998
Avi Kivitybccf2152007-02-21 18:04:26 +02001999static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2000 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002003 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004
Avi Kivitybccf2152007-02-21 18:04:26 +02002005 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006
2007 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2008 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2009 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2010 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2011 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2012 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2013
2014 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2015 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2016
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002017 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 sregs->idt.limit = dt.limit;
2019 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002020 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 sregs->gdt.limit = dt.limit;
2022 sregs->gdt.base = dt.base;
2023
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002024 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 sregs->cr0 = vcpu->cr0;
2026 sregs->cr2 = vcpu->cr2;
2027 sregs->cr3 = vcpu->cr3;
2028 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002029 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002031 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032
Eddie Dong2a8067f2007-08-06 16:29:07 +03002033 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002034 memset(sregs->interrupt_bitmap, 0,
2035 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002036 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002037 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002038 set_bit(pending_vec,
2039 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002040 } else
He, Qingc52fb352007-08-02 14:03:07 +03002041 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2042 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043
2044 vcpu_put(vcpu);
2045
2046 return 0;
2047}
2048
2049static void set_segment(struct kvm_vcpu *vcpu,
2050 struct kvm_segment *var, int seg)
2051{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002052 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053}
2054
Avi Kivitybccf2152007-02-21 18:04:26 +02002055static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2056 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002058 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002059 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 struct descriptor_table dt;
2061
Avi Kivitybccf2152007-02-21 18:04:26 +02002062 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 dt.limit = sregs->idt.limit;
2065 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002066 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 dt.limit = sregs->gdt.limit;
2068 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002069 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070
2071 vcpu->cr2 = sregs->cr2;
2072 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2073 vcpu->cr3 = sregs->cr3;
2074
Eddie Dong7017fc32007-07-18 11:34:57 +03002075 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076
2077 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002078#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002079 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002081 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002083 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002084
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002086 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002087 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088
2089 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002090 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002091 if (!is_long_mode(vcpu) && is_pae(vcpu))
2092 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093
2094 if (mmu_reset_needed)
2095 kvm_mmu_reset_context(vcpu);
2096
He, Qingc52fb352007-08-02 14:03:07 +03002097 if (!irqchip_in_kernel(vcpu->kvm)) {
2098 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2099 sizeof vcpu->irq_pending);
2100 vcpu->irq_summary = 0;
2101 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2102 if (vcpu->irq_pending[i])
2103 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002104 } else {
2105 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2106 pending_vec = find_first_bit(
2107 (const unsigned long *)sregs->interrupt_bitmap,
2108 max_bits);
2109 /* Only pending external irq is handled here */
2110 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002111 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002112 pr_debug("Set back pending irq %d\n",
2113 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002114 }
He, Qingc52fb352007-08-02 14:03:07 +03002115 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116
Avi Kivity024aa1c2007-03-21 13:44:58 +02002117 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2118 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2119 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2120 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2121 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2122 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2123
2124 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2125 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2126
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 vcpu_put(vcpu);
2128
2129 return 0;
2130}
2131
Rusty Russell1747fb72007-09-06 01:21:32 +10002132void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2133{
2134 struct kvm_segment cs;
2135
2136 get_segment(vcpu, &cs, VCPU_SREG_CS);
2137 *db = cs.db;
2138 *l = cs.l;
2139}
2140EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2141
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 * Translate a guest virtual address to a guest physical address.
2144 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002145static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2146 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147{
2148 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 gpa_t gpa;
2150
Avi Kivitybccf2152007-02-21 18:04:26 +02002151 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002152 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2154 tr->physical_address = gpa;
2155 tr->valid = gpa != UNMAPPED_GVA;
2156 tr->writeable = 1;
2157 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002158 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 vcpu_put(vcpu);
2160
2161 return 0;
2162}
2163
Avi Kivitybccf2152007-02-21 18:04:26 +02002164static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2165 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167 if (irq->irq < 0 || irq->irq >= 256)
2168 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002169 if (irqchip_in_kernel(vcpu->kvm))
2170 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002171 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172
2173 set_bit(irq->irq, vcpu->irq_pending);
2174 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2175
2176 vcpu_put(vcpu);
2177
2178 return 0;
2179}
2180
Avi Kivitybccf2152007-02-21 18:04:26 +02002181static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2182 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 int r;
2185
Avi Kivitybccf2152007-02-21 18:04:26 +02002186 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002188 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189
2190 vcpu_put(vcpu);
2191
2192 return r;
2193}
2194
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002195static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2196 unsigned long address,
2197 int *type)
2198{
2199 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2200 unsigned long pgoff;
2201 struct page *page;
2202
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002203 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002204 if (pgoff == 0)
2205 page = virt_to_page(vcpu->run);
2206 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2207 page = virt_to_page(vcpu->pio_data);
2208 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002209 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002210 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002211 if (type != NULL)
2212 *type = VM_FAULT_MINOR;
2213
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002214 return page;
2215}
2216
2217static struct vm_operations_struct kvm_vcpu_vm_ops = {
2218 .nopage = kvm_vcpu_nopage,
2219};
2220
2221static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2222{
2223 vma->vm_ops = &kvm_vcpu_vm_ops;
2224 return 0;
2225}
2226
Avi Kivitybccf2152007-02-21 18:04:26 +02002227static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2228{
2229 struct kvm_vcpu *vcpu = filp->private_data;
2230
2231 fput(vcpu->kvm->filp);
2232 return 0;
2233}
2234
2235static struct file_operations kvm_vcpu_fops = {
2236 .release = kvm_vcpu_release,
2237 .unlocked_ioctl = kvm_vcpu_ioctl,
2238 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002239 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002240};
2241
2242/*
2243 * Allocates an inode for the vcpu.
2244 */
2245static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2246{
2247 int fd, r;
2248 struct inode *inode;
2249 struct file *file;
2250
Avi Kivityd6d28162007-06-28 08:38:16 -04002251 r = anon_inode_getfd(&fd, &inode, &file,
2252 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2253 if (r)
2254 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002255 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002256 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002257}
2258
Avi Kivityc5ea7662007-02-20 18:41:05 +02002259/*
2260 * Creates some virtual cpus. Good luck creating more than one.
2261 */
2262static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2263{
2264 int r;
2265 struct kvm_vcpu *vcpu;
2266
Avi Kivityc5ea7662007-02-20 18:41:05 +02002267 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002268 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002269
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002270 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002271 if (IS_ERR(vcpu))
2272 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002273
Avi Kivity15ad7142007-07-11 18:17:21 +03002274 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2275
Rusty Russellb114b082007-07-30 21:13:43 +10002276 /* We do fxsave: this must be aligned. */
2277 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2278
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002279 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002280 r = kvm_x86_ops->vcpu_reset(vcpu);
2281 if (r == 0)
2282 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002283 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002284 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002285 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002286
Shaohua Li11ec2802007-07-23 14:51:37 +08002287 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002288 if (kvm->vcpus[n]) {
2289 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002290 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002291 goto mmu_unload;
2292 }
2293 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002294 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002295
2296 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002297 r = create_vcpu_fd(vcpu);
2298 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002299 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002300 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002301
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002302unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002303 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002304 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002305 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002306
2307mmu_unload:
2308 vcpu_load(vcpu);
2309 kvm_mmu_unload(vcpu);
2310 vcpu_put(vcpu);
2311
2312free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002313 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002314 return r;
2315}
2316
Avi Kivity1961d272007-03-05 19:46:05 +02002317static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2318{
2319 if (sigset) {
2320 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2321 vcpu->sigset_active = 1;
2322 vcpu->sigset = *sigset;
2323 } else
2324 vcpu->sigset_active = 0;
2325 return 0;
2326}
2327
Avi Kivityb8836732007-04-01 16:34:31 +03002328/*
2329 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2330 * we have asm/x86/processor.h
2331 */
2332struct fxsave {
2333 u16 cwd;
2334 u16 swd;
2335 u16 twd;
2336 u16 fop;
2337 u64 rip;
2338 u64 rdp;
2339 u32 mxcsr;
2340 u32 mxcsr_mask;
2341 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2342#ifdef CONFIG_X86_64
2343 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2344#else
2345 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2346#endif
2347};
2348
2349static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2350{
Rusty Russellb114b082007-07-30 21:13:43 +10002351 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002352
2353 vcpu_load(vcpu);
2354
2355 memcpy(fpu->fpr, fxsave->st_space, 128);
2356 fpu->fcw = fxsave->cwd;
2357 fpu->fsw = fxsave->swd;
2358 fpu->ftwx = fxsave->twd;
2359 fpu->last_opcode = fxsave->fop;
2360 fpu->last_ip = fxsave->rip;
2361 fpu->last_dp = fxsave->rdp;
2362 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2363
2364 vcpu_put(vcpu);
2365
2366 return 0;
2367}
2368
2369static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2370{
Rusty Russellb114b082007-07-30 21:13:43 +10002371 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002372
2373 vcpu_load(vcpu);
2374
2375 memcpy(fxsave->st_space, fpu->fpr, 128);
2376 fxsave->cwd = fpu->fcw;
2377 fxsave->swd = fpu->fsw;
2378 fxsave->twd = fpu->ftwx;
2379 fxsave->fop = fpu->last_opcode;
2380 fxsave->rip = fpu->last_ip;
2381 fxsave->rdp = fpu->last_dp;
2382 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2383
2384 vcpu_put(vcpu);
2385
2386 return 0;
2387}
2388
Avi Kivitybccf2152007-02-21 18:04:26 +02002389static long kvm_vcpu_ioctl(struct file *filp,
2390 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391{
Avi Kivitybccf2152007-02-21 18:04:26 +02002392 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002393 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002394 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395
2396 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002397 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002398 r = -EINVAL;
2399 if (arg)
2400 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002401 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 case KVM_GET_REGS: {
2404 struct kvm_regs kvm_regs;
2405
Avi Kivitybccf2152007-02-21 18:04:26 +02002406 memset(&kvm_regs, 0, sizeof kvm_regs);
2407 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 if (r)
2409 goto out;
2410 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002411 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412 goto out;
2413 r = 0;
2414 break;
2415 }
2416 case KVM_SET_REGS: {
2417 struct kvm_regs kvm_regs;
2418
2419 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002420 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002422 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423 if (r)
2424 goto out;
2425 r = 0;
2426 break;
2427 }
2428 case KVM_GET_SREGS: {
2429 struct kvm_sregs kvm_sregs;
2430
Avi Kivitybccf2152007-02-21 18:04:26 +02002431 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2432 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 if (r)
2434 goto out;
2435 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002436 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 goto out;
2438 r = 0;
2439 break;
2440 }
2441 case KVM_SET_SREGS: {
2442 struct kvm_sregs kvm_sregs;
2443
2444 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002445 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002447 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002448 if (r)
2449 goto out;
2450 r = 0;
2451 break;
2452 }
2453 case KVM_TRANSLATE: {
2454 struct kvm_translation tr;
2455
2456 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002457 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002459 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 if (r)
2461 goto out;
2462 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002463 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 goto out;
2465 r = 0;
2466 break;
2467 }
2468 case KVM_INTERRUPT: {
2469 struct kvm_interrupt irq;
2470
2471 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002472 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002474 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475 if (r)
2476 goto out;
2477 r = 0;
2478 break;
2479 }
2480 case KVM_DEBUG_GUEST: {
2481 struct kvm_debug_guest dbg;
2482
2483 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002484 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002486 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 if (r)
2488 goto out;
2489 r = 0;
2490 break;
2491 }
Avi Kivity1961d272007-03-05 19:46:05 +02002492 case KVM_SET_SIGNAL_MASK: {
2493 struct kvm_signal_mask __user *sigmask_arg = argp;
2494 struct kvm_signal_mask kvm_sigmask;
2495 sigset_t sigset, *p;
2496
2497 p = NULL;
2498 if (argp) {
2499 r = -EFAULT;
2500 if (copy_from_user(&kvm_sigmask, argp,
2501 sizeof kvm_sigmask))
2502 goto out;
2503 r = -EINVAL;
2504 if (kvm_sigmask.len != sizeof sigset)
2505 goto out;
2506 r = -EFAULT;
2507 if (copy_from_user(&sigset, sigmask_arg->sigset,
2508 sizeof sigset))
2509 goto out;
2510 p = &sigset;
2511 }
2512 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2513 break;
2514 }
Avi Kivityb8836732007-04-01 16:34:31 +03002515 case KVM_GET_FPU: {
2516 struct kvm_fpu fpu;
2517
2518 memset(&fpu, 0, sizeof fpu);
2519 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2520 if (r)
2521 goto out;
2522 r = -EFAULT;
2523 if (copy_to_user(argp, &fpu, sizeof fpu))
2524 goto out;
2525 r = 0;
2526 break;
2527 }
2528 case KVM_SET_FPU: {
2529 struct kvm_fpu fpu;
2530
2531 r = -EFAULT;
2532 if (copy_from_user(&fpu, argp, sizeof fpu))
2533 goto out;
2534 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2535 if (r)
2536 goto out;
2537 r = 0;
2538 break;
2539 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002540 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002541 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002542 }
2543out:
2544 return r;
2545}
2546
2547static long kvm_vm_ioctl(struct file *filp,
2548 unsigned int ioctl, unsigned long arg)
2549{
2550 struct kvm *kvm = filp->private_data;
2551 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002552 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002553
2554 switch (ioctl) {
2555 case KVM_CREATE_VCPU:
2556 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2557 if (r < 0)
2558 goto out;
2559 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002560 case KVM_SET_USER_MEMORY_REGION: {
2561 struct kvm_userspace_memory_region kvm_userspace_mem;
2562
2563 r = -EFAULT;
2564 if (copy_from_user(&kvm_userspace_mem, argp,
2565 sizeof kvm_userspace_mem))
2566 goto out;
2567
2568 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 if (r)
2570 goto out;
2571 break;
2572 }
2573 case KVM_GET_DIRTY_LOG: {
2574 struct kvm_dirty_log log;
2575
2576 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002577 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002579 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 if (r)
2581 goto out;
2582 break;
2583 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002584 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002585 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002586 }
2587out:
2588 return r;
2589}
2590
2591static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2592 unsigned long address,
2593 int *type)
2594{
2595 struct kvm *kvm = vma->vm_file->private_data;
2596 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002597 struct page *page;
2598
Avi Kivityf17abe92007-02-21 19:28:04 +02002599 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02002600 if (!kvm_is_visible_gfn(kvm, pgoff))
2601 return NOPAGE_SIGBUS;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002602 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002603 if (is_error_page(page)) {
2604 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02002605 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02002606 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002607 if (type != NULL)
2608 *type = VM_FAULT_MINOR;
2609
Avi Kivityf17abe92007-02-21 19:28:04 +02002610 return page;
2611}
2612
2613static struct vm_operations_struct kvm_vm_vm_ops = {
2614 .nopage = kvm_vm_nopage,
2615};
2616
2617static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2618{
2619 vma->vm_ops = &kvm_vm_vm_ops;
2620 return 0;
2621}
2622
2623static struct file_operations kvm_vm_fops = {
2624 .release = kvm_vm_release,
2625 .unlocked_ioctl = kvm_vm_ioctl,
2626 .compat_ioctl = kvm_vm_ioctl,
2627 .mmap = kvm_vm_mmap,
2628};
2629
2630static int kvm_dev_ioctl_create_vm(void)
2631{
2632 int fd, r;
2633 struct inode *inode;
2634 struct file *file;
2635 struct kvm *kvm;
2636
Avi Kivityf17abe92007-02-21 19:28:04 +02002637 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002638 if (IS_ERR(kvm))
2639 return PTR_ERR(kvm);
2640 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2641 if (r) {
2642 kvm_destroy_vm(kvm);
2643 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002644 }
2645
Avi Kivitybccf2152007-02-21 18:04:26 +02002646 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002647
Avi Kivityf17abe92007-02-21 19:28:04 +02002648 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002649}
2650
2651static long kvm_dev_ioctl(struct file *filp,
2652 unsigned int ioctl, unsigned long arg)
2653{
2654 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002655 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002656
2657 switch (ioctl) {
2658 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002659 r = -EINVAL;
2660 if (arg)
2661 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002662 r = KVM_API_VERSION;
2663 break;
2664 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002665 r = -EINVAL;
2666 if (arg)
2667 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002668 r = kvm_dev_ioctl_create_vm();
2669 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002670 case KVM_CHECK_EXTENSION: {
2671 int ext = (long)argp;
2672
2673 switch (ext) {
2674 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03002675 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02002676 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02002677 case KVM_CAP_USER_MEMORY:
Izik Eiduscbc94022007-10-25 00:29:55 +02002678 case KVM_CAP_SET_TSS_ADDR:
Eddie Dong85f455f2007-07-06 12:20:49 +03002679 r = 1;
2680 break;
2681 default:
2682 r = 0;
2683 break;
2684 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002685 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002686 }
Avi Kivity07c45a32007-03-07 13:05:38 +02002687 case KVM_GET_VCPU_MMAP_SIZE:
2688 r = -EINVAL;
2689 if (arg)
2690 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002691 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002692 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002694 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 }
2696out:
2697 return r;
2698}
2699
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 .unlocked_ioctl = kvm_dev_ioctl,
2702 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703};
2704
2705static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002706 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 "kvm",
2708 &kvm_chardev_ops,
2709};
2710
Avi Kivity774c47f2007-02-12 00:54:47 -08002711/*
2712 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2713 * cached on it.
2714 */
2715static void decache_vcpus_on_cpu(int cpu)
2716{
2717 struct kvm *vm;
2718 struct kvm_vcpu *vcpu;
2719 int i;
2720
2721 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08002722 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08002723 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002724 vcpu = vm->vcpus[i];
2725 if (!vcpu)
2726 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08002727 /*
2728 * If the vcpu is locked, then it is running on some
2729 * other cpu and therefore it is not cached on the
2730 * cpu in question.
2731 *
2732 * If it's not locked, check the last cpu it executed
2733 * on.
2734 */
2735 if (mutex_trylock(&vcpu->mutex)) {
2736 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002737 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002738 vcpu->cpu = -1;
2739 }
2740 mutex_unlock(&vcpu->mutex);
2741 }
2742 }
2743 spin_unlock(&kvm_lock);
2744}
2745
Avi Kivity1b6c0162007-05-24 13:03:52 +03002746static void hardware_enable(void *junk)
2747{
2748 int cpu = raw_smp_processor_id();
2749
2750 if (cpu_isset(cpu, cpus_hardware_enabled))
2751 return;
2752 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002753 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002754}
2755
2756static void hardware_disable(void *junk)
2757{
2758 int cpu = raw_smp_processor_id();
2759
2760 if (!cpu_isset(cpu, cpus_hardware_enabled))
2761 return;
2762 cpu_clear(cpu, cpus_hardware_enabled);
2763 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002764 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002765}
2766
Avi Kivity774c47f2007-02-12 00:54:47 -08002767static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2768 void *v)
2769{
2770 int cpu = (long)v;
2771
2772 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002773 case CPU_DYING:
2774 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002775 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2776 cpu);
2777 hardware_disable(NULL);
2778 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002779 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002780 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002781 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2782 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002783 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002784 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002785 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002786 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002787 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2788 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002789 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002790 break;
2791 }
2792 return NOTIFY_OK;
2793}
2794
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002795static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002796 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002797{
2798 if (val == SYS_RESTART) {
2799 /*
2800 * Some (well, at least mine) BIOSes hang on reboot if
2801 * in vmx root mode.
2802 */
2803 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2804 on_each_cpu(hardware_disable, NULL, 0, 1);
2805 }
2806 return NOTIFY_OK;
2807}
2808
2809static struct notifier_block kvm_reboot_notifier = {
2810 .notifier_call = kvm_reboot,
2811 .priority = 0,
2812};
2813
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002814void kvm_io_bus_init(struct kvm_io_bus *bus)
2815{
2816 memset(bus, 0, sizeof(*bus));
2817}
2818
2819void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2820{
2821 int i;
2822
2823 for (i = 0; i < bus->dev_count; i++) {
2824 struct kvm_io_device *pos = bus->devs[i];
2825
2826 kvm_iodevice_destructor(pos);
2827 }
2828}
2829
2830struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
2831{
2832 int i;
2833
2834 for (i = 0; i < bus->dev_count; i++) {
2835 struct kvm_io_device *pos = bus->devs[i];
2836
2837 if (pos->in_range(pos, addr))
2838 return pos;
2839 }
2840
2841 return NULL;
2842}
2843
2844void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2845{
2846 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2847
2848 bus->devs[bus->dev_count++] = dev;
2849}
2850
Avi Kivity774c47f2007-02-12 00:54:47 -08002851static struct notifier_block kvm_cpu_notifier = {
2852 .notifier_call = kvm_cpu_hotplug,
2853 .priority = 20, /* must be > scheduler priority */
2854};
2855
Avi Kivity1165f5f2007-04-19 17:27:43 +03002856static u64 stat_get(void *_offset)
2857{
2858 unsigned offset = (long)_offset;
2859 u64 total = 0;
2860 struct kvm *kvm;
2861 struct kvm_vcpu *vcpu;
2862 int i;
2863
2864 spin_lock(&kvm_lock);
2865 list_for_each_entry(kvm, &vm_list, vm_list)
2866 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002867 vcpu = kvm->vcpus[i];
2868 if (vcpu)
2869 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002870 }
2871 spin_unlock(&kvm_lock);
2872 return total;
2873}
2874
Rusty Russell3dea7ca2007-08-01 10:12:22 +10002875DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03002876
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877static __init void kvm_init_debug(void)
2878{
2879 struct kvm_stats_debugfs_item *p;
2880
Al Viro8b6d44c2007-02-09 16:38:40 +00002881 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002883 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2884 (void *)(long)p->offset,
2885 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886}
2887
2888static void kvm_exit_debug(void)
2889{
2890 struct kvm_stats_debugfs_item *p;
2891
2892 for (p = debugfs_entries; p->name; ++p)
2893 debugfs_remove(p->dentry);
2894 debugfs_remove(debugfs_dir);
2895}
2896
Avi Kivity59ae6c62007-02-12 00:54:48 -08002897static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2898{
Avi Kivity4267c412007-05-24 13:09:41 +03002899 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002900 return 0;
2901}
2902
2903static int kvm_resume(struct sys_device *dev)
2904{
Avi Kivity4267c412007-05-24 13:09:41 +03002905 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002906 return 0;
2907}
2908
2909static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002910 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002911 .suspend = kvm_suspend,
2912 .resume = kvm_resume,
2913};
2914
2915static struct sys_device kvm_sysdev = {
2916 .id = 0,
2917 .cls = &kvm_sysdev_class,
2918};
2919
Izik Eiduscea7bb22007-10-17 19:17:48 +02002920struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921
Avi Kivity15ad7142007-07-11 18:17:21 +03002922static inline
2923struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2924{
2925 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2926}
2927
2928static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2929{
2930 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2931
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002932 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002933}
2934
2935static void kvm_sched_out(struct preempt_notifier *pn,
2936 struct task_struct *next)
2937{
2938 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2939
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002940 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002941}
2942
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002943int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002944 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945{
2946 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002947 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002949 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002950 printk(KERN_ERR "kvm: already loaded the other module\n");
2951 return -EEXIST;
2952 }
2953
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002954 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955 printk(KERN_ERR "kvm: no hardware support\n");
2956 return -EOPNOTSUPP;
2957 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002958 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 printk(KERN_ERR "kvm: disabled by bios\n");
2960 return -EOPNOTSUPP;
2961 }
2962
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002963 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002964
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002965 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02002967 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968
Yang, Sheng002c7f72007-07-31 14:23:01 +03002969 for_each_online_cpu(cpu) {
2970 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002971 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002972 &r, 0, 1);
2973 if (r < 0)
2974 goto out_free_0;
2975 }
2976
Avi Kivity1b6c0162007-05-24 13:03:52 +03002977 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002978 r = register_cpu_notifier(&kvm_cpu_notifier);
2979 if (r)
2980 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 register_reboot_notifier(&kvm_reboot_notifier);
2982
Avi Kivity59ae6c62007-02-12 00:54:48 -08002983 r = sysdev_class_register(&kvm_sysdev_class);
2984 if (r)
2985 goto out_free_2;
2986
2987 r = sysdev_register(&kvm_sysdev);
2988 if (r)
2989 goto out_free_3;
2990
Rusty Russellc16f8622007-07-30 21:12:19 +10002991 /* A kmem cache lets us meet the alignment requirements of fx_save. */
2992 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
2993 __alignof__(struct kvm_vcpu), 0, 0);
2994 if (!kvm_vcpu_cache) {
2995 r = -ENOMEM;
2996 goto out_free_4;
2997 }
2998
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 kvm_chardev_ops.owner = module;
3000
3001 r = misc_register(&kvm_dev);
3002 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003003 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004 goto out_free;
3005 }
3006
Avi Kivity15ad7142007-07-11 18:17:21 +03003007 kvm_preempt_ops.sched_in = kvm_sched_in;
3008 kvm_preempt_ops.sched_out = kvm_sched_out;
3009
Avi Kivityc7addb92007-09-16 18:58:32 +02003010 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3011
3012 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013
3014out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003015 kmem_cache_destroy(kvm_vcpu_cache);
3016out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003017 sysdev_unregister(&kvm_sysdev);
3018out_free_3:
3019 sysdev_class_unregister(&kvm_sysdev_class);
3020out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003022 unregister_cpu_notifier(&kvm_cpu_notifier);
3023out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003024 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003025out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003026 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003027out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003028 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 return r;
3030}
Mike Dayd77c26f2007-10-08 09:02:08 -04003031EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003033void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034{
3035 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003036 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003037 sysdev_unregister(&kvm_sysdev);
3038 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003040 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003041 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003042 kvm_x86_ops->hardware_unsetup();
3043 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044}
Mike Dayd77c26f2007-10-08 09:02:08 -04003045EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046
3047static __init int kvm_init(void)
3048{
Avi Kivity37e29d92007-02-20 14:07:37 +02003049 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050
Avi Kivityb5a33a72007-04-15 16:31:09 +03003051 r = kvm_mmu_module_init();
3052 if (r)
3053 goto out4;
3054
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 kvm_init_debug();
3056
Carsten Otte043405e2007-10-10 17:16:19 +02003057 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003058
Izik Eiduscea7bb22007-10-17 19:17:48 +02003059 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003060
3061 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 r = -ENOMEM;
3063 goto out;
3064 }
3065
Avi Kivity58e690e2007-02-26 16:29:43 +02003066 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067
3068out:
3069 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003070 kvm_mmu_module_exit();
3071out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 return r;
3073}
3074
3075static __exit void kvm_exit(void)
3076{
3077 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003078 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003079 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080}
3081
3082module_init(kvm_init)
3083module_exit(kvm_exit)