blob: f439e45233f2eefe75f4a9008076c3787fa452e6 [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.
Sheng Yangf78e0e22007-10-29 09:40:42 +0800365 *
366 * Must be called holding kvm->lock.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 */
Sheng Yangf78e0e22007-10-29 09:40:42 +0800368int __kvm_set_memory_region(struct kvm *kvm,
369 struct kvm_userspace_memory_region *mem,
370 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371{
372 int r;
373 gfn_t base_gfn;
374 unsigned long npages;
375 unsigned long i;
376 struct kvm_memory_slot *memslot;
377 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378
379 r = -EINVAL;
380 /* General sanity checks */
381 if (mem->memory_size & (PAGE_SIZE - 1))
382 goto out;
383 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
384 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200385 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386 goto out;
387 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
388 goto out;
389
390 memslot = &kvm->memslots[mem->slot];
391 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
392 npages = mem->memory_size >> PAGE_SHIFT;
393
394 if (!npages)
395 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
396
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)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800406 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407
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)))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800417 goto out_free;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 }
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)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800431 goto out_free;
Izik Eidus290fc382007-09-27 14:11:22 +0200432
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))
Sheng Yangf78e0e22007-10-29 09:40:42 +0800448 goto out_free;
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)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800471 goto out_free;
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
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 kvm_free_physmem_slot(&old, &new);
502 return 0;
503
Sheng Yangf78e0e22007-10-29 09:40:42 +0800504out_free:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 kvm_free_physmem_slot(&new, &old);
506out:
507 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200508
509}
Sheng Yangf78e0e22007-10-29 09:40:42 +0800510EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
511
512int kvm_set_memory_region(struct kvm *kvm,
513 struct kvm_userspace_memory_region *mem,
514 int user_alloc)
515{
516 int r;
517
518 mutex_lock(&kvm->lock);
519 r = __kvm_set_memory_region(kvm, mem, user_alloc);
520 mutex_unlock(&kvm->lock);
521 return r;
522}
Izik Eidus210c7c42007-10-24 23:52:57 +0200523EXPORT_SYMBOL_GPL(kvm_set_memory_region);
524
Carsten Otte1fe779f2007-10-29 16:08:35 +0100525int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
526 struct
527 kvm_userspace_memory_region *mem,
528 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200529{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200530 if (mem->slot >= KVM_MEMORY_SLOTS)
531 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200532 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533}
534
535/*
536 * Get (and clear) the dirty memory log for a memory slot.
537 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200538static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
539 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540{
541 struct kvm_memory_slot *memslot;
542 int r, i;
543 int n;
544 unsigned long any = 0;
545
Shaohua Li11ec2802007-07-23 14:51:37 +0800546 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548 r = -EINVAL;
549 if (log->slot >= KVM_MEMORY_SLOTS)
550 goto out;
551
552 memslot = &kvm->memslots[log->slot];
553 r = -ENOENT;
554 if (!memslot->dirty_bitmap)
555 goto out;
556
Uri Lublincd1a4a92007-02-22 16:43:09 +0200557 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Uri Lublincd1a4a92007-02-22 16:43:09 +0200559 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 any = memslot->dirty_bitmap[i];
561
562 r = -EFAULT;
563 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
564 goto out;
565
Rusty Russell39214912007-07-31 19:57:47 +1000566 /* If nothing is dirty, don't bother messing with page tables. */
567 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000568 kvm_mmu_slot_remove_write_access(kvm, log->slot);
569 kvm_flush_remote_tlbs(kvm);
570 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000571 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572
573 r = 0;
574
575out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800576 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 return r;
578}
579
Izik Eiduscea7bb22007-10-17 19:17:48 +0200580int is_error_page(struct page *page)
581{
582 return page == bad_page;
583}
584EXPORT_SYMBOL_GPL(is_error_page);
585
Izik Eidus290fc382007-09-27 14:11:22 +0200586gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300587{
588 int i;
589 struct kvm_mem_alias *alias;
590
591 for (i = 0; i < kvm->naliases; ++i) {
592 alias = &kvm->aliases[i];
593 if (gfn >= alias->base_gfn
594 && gfn < alias->base_gfn + alias->npages)
595 return alias->target_gfn + gfn - alias->base_gfn;
596 }
597 return gfn;
598}
599
600static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601{
602 int i;
603
604 for (i = 0; i < kvm->nmemslots; ++i) {
605 struct kvm_memory_slot *memslot = &kvm->memslots[i];
606
607 if (gfn >= memslot->base_gfn
608 && gfn < memslot->base_gfn + memslot->npages)
609 return memslot;
610 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000611 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612}
Avi Kivitye8207542007-03-30 16:54:30 +0300613
614struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
615{
616 gfn = unalias_gfn(kvm, gfn);
617 return __gfn_to_memslot(kvm, gfn);
618}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619
Izik Eiduse0d62c72007-10-24 23:57:46 +0200620int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
621{
622 int i;
623
624 gfn = unalias_gfn(kvm, gfn);
625 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
626 struct kvm_memory_slot *memslot = &kvm->memslots[i];
627
628 if (gfn >= memslot->base_gfn
629 && gfn < memslot->base_gfn + memslot->npages)
630 return 1;
631 }
632 return 0;
633}
634EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
635
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500636/*
637 * Requires current->mm->mmap_sem to be held
638 */
639static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +0300640{
641 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500642 struct page *page[1];
643 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300644
Avi Kivity60395222007-10-21 11:03:36 +0200645 might_sleep();
646
Avi Kivitye8207542007-03-30 16:54:30 +0300647 gfn = unalias_gfn(kvm, gfn);
648 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200649 if (!slot) {
650 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200651 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200652 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200653
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500654 npages = get_user_pages(current, current->mm,
655 slot->userspace_addr
656 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
657 1, 1, page, NULL);
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500658 if (npages != 1) {
659 get_page(bad_page);
660 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200661 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500662
663 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300664}
Anthony Liguoriaab61cc2007-10-29 15:15:20 -0500665
666struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
667{
668 struct page *page;
669
670 down_read(&current->mm->mmap_sem);
671 page = __gfn_to_page(kvm, gfn);
672 up_read(&current->mm->mmap_sem);
673
674 return page;
675}
676
Avi Kivity954bbbc2007-03-30 14:02:32 +0300677EXPORT_SYMBOL_GPL(gfn_to_page);
678
Izik Eidus8a7ae052007-10-18 11:09:33 +0200679void kvm_release_page(struct page *page)
680{
681 if (!PageReserved(page))
682 SetPageDirty(page);
683 put_page(page);
684}
685EXPORT_SYMBOL_GPL(kvm_release_page);
686
Izik Eidus195aefd2007-10-01 22:14:18 +0200687static int next_segment(unsigned long len, int offset)
688{
689 if (len > PAGE_SIZE - offset)
690 return PAGE_SIZE - offset;
691 else
692 return len;
693}
694
695int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
696 int len)
697{
698 void *page_virt;
699 struct page *page;
700
701 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200702 if (is_error_page(page)) {
703 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200704 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200705 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200706 page_virt = kmap_atomic(page, KM_USER0);
707
708 memcpy(data, page_virt + offset, len);
709
710 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200711 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200712 return 0;
713}
714EXPORT_SYMBOL_GPL(kvm_read_guest_page);
715
716int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
717{
718 gfn_t gfn = gpa >> PAGE_SHIFT;
719 int seg;
720 int offset = offset_in_page(gpa);
721 int ret;
722
723 while ((seg = next_segment(len, offset)) != 0) {
724 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
725 if (ret < 0)
726 return ret;
727 offset = 0;
728 len -= seg;
729 data += seg;
730 ++gfn;
731 }
732 return 0;
733}
734EXPORT_SYMBOL_GPL(kvm_read_guest);
735
736int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
737 int offset, int len)
738{
739 void *page_virt;
740 struct page *page;
741
742 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200743 if (is_error_page(page)) {
744 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200745 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200746 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200747 page_virt = kmap_atomic(page, KM_USER0);
748
749 memcpy(page_virt + offset, data, len);
750
751 kunmap_atomic(page_virt, KM_USER0);
752 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200753 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200754 return 0;
755}
756EXPORT_SYMBOL_GPL(kvm_write_guest_page);
757
758int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
759 unsigned long len)
760{
761 gfn_t gfn = gpa >> PAGE_SHIFT;
762 int seg;
763 int offset = offset_in_page(gpa);
764 int ret;
765
766 while ((seg = next_segment(len, offset)) != 0) {
767 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
768 if (ret < 0)
769 return ret;
770 offset = 0;
771 len -= seg;
772 data += seg;
773 ++gfn;
774 }
775 return 0;
776}
777
778int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
779{
780 void *page_virt;
781 struct page *page;
782
783 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200784 if (is_error_page(page)) {
785 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200786 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200787 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200788 page_virt = kmap_atomic(page, KM_USER0);
789
790 memset(page_virt + offset, 0, len);
791
792 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200793 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200794 return 0;
795}
796EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
797
798int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
799{
800 gfn_t gfn = gpa >> PAGE_SHIFT;
801 int seg;
802 int offset = offset_in_page(gpa);
803 int ret;
804
805 while ((seg = next_segment(len, offset)) != 0) {
806 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
807 if (ret < 0)
808 return ret;
809 offset = 0;
810 len -= seg;
811 ++gfn;
812 }
813 return 0;
814}
815EXPORT_SYMBOL_GPL(kvm_clear_guest);
816
Rusty Russell7e9d6192007-07-31 20:41:14 +1000817/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
819{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +0300820 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Rusty Russell7e9d6192007-07-31 20:41:14 +1000822 memslot = __gfn_to_memslot(kvm, gfn);
823 if (memslot && memslot->dirty_bitmap) {
824 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
Rusty Russell7e9d6192007-07-31 20:41:14 +1000826 /* avoid RMW */
827 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
828 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 }
830}
831
Laurent Viviere7d5d762007-07-30 13:41:19 +0300832int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300833 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300835 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 void *data = val;
838
839 while (bytes) {
840 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
841 unsigned offset = addr & (PAGE_SIZE-1);
842 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +0200843 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
845 if (gpa == UNMAPPED_GVA)
846 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +0200847 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
848 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
851 bytes -= tocopy;
852 data += tocopy;
853 addr += tocopy;
854 }
855
856 return X86EMUL_CONTINUE;
857}
Laurent Viviere7d5d762007-07-30 13:41:19 +0300858EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859
860static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300861 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300863 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864{
Rusty Russellf0242472007-08-01 10:48:02 +1000865 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866 return X86EMUL_UNHANDLEABLE;
867}
868
Eddie Dong97222cc2007-09-12 10:58:04 +0300869/*
870 * Only apic need an MMIO device hook, so shortcut now..
871 */
872static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
873 gpa_t addr)
874{
875 struct kvm_io_device *dev;
876
877 if (vcpu->apic) {
878 dev = &vcpu->apic->dev;
879 if (dev->in_range(dev, addr))
880 return dev;
881 }
882 return NULL;
883}
884
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400885static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
886 gpa_t addr)
887{
Eddie Dong97222cc2007-09-12 10:58:04 +0300888 struct kvm_io_device *dev;
889
890 dev = vcpu_find_pervcpu_dev(vcpu, addr);
891 if (dev == NULL)
892 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
893 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400894}
895
Eddie Dong74906342007-06-19 18:05:03 +0300896static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
897 gpa_t addr)
898{
899 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
900}
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300903 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300905 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400907 struct kvm_io_device *mmio_dev;
908 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
910 if (vcpu->mmio_read_completed) {
911 memcpy(val, vcpu->mmio_data, bytes);
912 vcpu->mmio_read_completed = 0;
913 return X86EMUL_CONTINUE;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800914 }
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200915
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400916 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800917
918 /* For APIC access vmexit */
919 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
920 goto mmio;
921
922 if (emulator_read_std(addr, val, bytes, vcpu)
923 == X86EMUL_CONTINUE)
924 return X86EMUL_CONTINUE;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400925 if (gpa == UNMAPPED_GVA)
926 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
Sheng Yangf78e0e22007-10-29 09:40:42 +0800928mmio:
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400929 /*
930 * Is this MMIO handled locally?
931 */
932 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
933 if (mmio_dev) {
934 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
935 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400937
938 vcpu->mmio_needed = 1;
939 vcpu->mmio_phys_addr = gpa;
940 vcpu->mmio_size = bytes;
941 vcpu->mmio_is_write = 0;
942
943 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944}
945
Avi Kivityda4a00f2007-01-05 16:36:44 -0800946static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +0300947 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -0800948{
Izik Eidus195aefd2007-10-01 22:14:18 +0200949 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -0800950
Izik Eidus195aefd2007-10-01 22:14:18 +0200951 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
952 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -0800953 return 0;
Shaohua Life5518812007-07-23 14:51:39 +0800954 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -0800955 return 1;
956}
957
Avi Kivityb0fcd902007-07-22 18:48:54 +0300958static int emulator_write_emulated_onepage(unsigned long addr,
959 const void *val,
960 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +0300961 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400963 struct kvm_io_device *mmio_dev;
964 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Avi Kivityc9047f52007-04-17 10:53:22 +0300966 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300967 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +0300969 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970
Sheng Yangf78e0e22007-10-29 09:40:42 +0800971 /* For APIC access vmexit */
972 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
973 goto mmio;
974
Avi Kivityda4a00f2007-01-05 16:36:44 -0800975 if (emulator_write_phys(vcpu, gpa, val, bytes))
976 return X86EMUL_CONTINUE;
977
Sheng Yangf78e0e22007-10-29 09:40:42 +0800978mmio:
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400979 /*
980 * Is this MMIO handled locally?
981 */
982 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
983 if (mmio_dev) {
984 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
985 return X86EMUL_CONTINUE;
986 }
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 vcpu->mmio_needed = 1;
989 vcpu->mmio_phys_addr = gpa;
990 vcpu->mmio_size = bytes;
991 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +0300992 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
994 return X86EMUL_CONTINUE;
995}
996
Laurent Viviere7d5d762007-07-30 13:41:19 +0300997int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +0300998 const void *val,
999 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001000 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001001{
1002 /* Crossing a page boundary? */
1003 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1004 int rc, now;
1005
1006 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001007 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001008 if (rc != X86EMUL_CONTINUE)
1009 return rc;
1010 addr += now;
1011 val += now;
1012 bytes -= now;
1013 }
Laurent Viviercebff022007-07-30 13:35:24 +03001014 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001015}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001016EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001017
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001019 const void *old,
1020 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001022 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023{
1024 static int reported;
1025
1026 if (!reported) {
1027 reported = 1;
1028 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1029 }
Laurent Viviercebff022007-07-30 13:35:24 +03001030 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
1033static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1034{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001035 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036}
1037
1038int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1039{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 return X86EMUL_CONTINUE;
1041}
1042
1043int emulate_clts(struct kvm_vcpu *vcpu)
1044{
Amit Shah404fb882007-11-19 17:57:35 +02001045 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 return X86EMUL_CONTINUE;
1047}
1048
Mike Dayd77c26f2007-10-08 09:02:08 -04001049int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050{
1051 struct kvm_vcpu *vcpu = ctxt->vcpu;
1052
1053 switch (dr) {
1054 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001055 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 return X86EMUL_CONTINUE;
1057 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001058 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 return X86EMUL_UNHANDLEABLE;
1060 }
1061}
1062
1063int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1064{
1065 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1066 int exception;
1067
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001068 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 if (exception) {
1070 /* FIXME: better handling */
1071 return X86EMUL_UNHANDLEABLE;
1072 }
1073 return X86EMUL_CONTINUE;
1074}
1075
Avi Kivity054b1362007-09-12 13:21:09 +03001076void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077{
1078 static int reported;
1079 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001080 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 unsigned long rip_linear;
1082
Avi Kivity054b1362007-09-12 13:21:09 +03001083 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084
1085 if (reported)
1086 return;
1087
Avi Kivity054b1362007-09-12 13:21:09 +03001088 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089
Avi Kivity054b1362007-09-12 13:21:09 +03001090 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1091 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 reported = 1;
1093}
Avi Kivity054b1362007-09-12 13:21:09 +03001094EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095
1096struct x86_emulate_ops emulate_ops = {
1097 .read_std = emulator_read_std,
1098 .write_std = emulator_write_std,
1099 .read_emulated = emulator_read_emulated,
1100 .write_emulated = emulator_write_emulated,
1101 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1102};
1103
1104int emulate_instruction(struct kvm_vcpu *vcpu,
1105 struct kvm_run *run,
1106 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001107 u16 error_code,
1108 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109{
Laurent Viviera22436b2007-09-24 17:00:58 +02001110 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111
Avi Kivitye7df56e2007-03-14 15:54:54 +02001112 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001113 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001116 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001117
1118 if (!no_decode) {
1119 int cs_db, cs_l;
1120 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1121
1122 vcpu->emulate_ctxt.vcpu = vcpu;
1123 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1124 vcpu->emulate_ctxt.cr2 = cr2;
1125 vcpu->emulate_ctxt.mode =
1126 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1127 ? X86EMUL_MODE_REAL : cs_l
1128 ? X86EMUL_MODE_PROT64 : cs_db
1129 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1130
1131 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1132 vcpu->emulate_ctxt.cs_base = 0;
1133 vcpu->emulate_ctxt.ds_base = 0;
1134 vcpu->emulate_ctxt.es_base = 0;
1135 vcpu->emulate_ctxt.ss_base = 0;
1136 } else {
1137 vcpu->emulate_ctxt.cs_base =
1138 get_segment_base(vcpu, VCPU_SREG_CS);
1139 vcpu->emulate_ctxt.ds_base =
1140 get_segment_base(vcpu, VCPU_SREG_DS);
1141 vcpu->emulate_ctxt.es_base =
1142 get_segment_base(vcpu, VCPU_SREG_ES);
1143 vcpu->emulate_ctxt.ss_base =
1144 get_segment_base(vcpu, VCPU_SREG_SS);
1145 }
1146
1147 vcpu->emulate_ctxt.gs_base =
1148 get_segment_base(vcpu, VCPU_SREG_GS);
1149 vcpu->emulate_ctxt.fs_base =
1150 get_segment_base(vcpu, VCPU_SREG_FS);
1151
1152 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001153 if (r) {
1154 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1155 return EMULATE_DONE;
1156 return EMULATE_FAIL;
1157 }
Laurent Vivier34273182007-09-18 11:27:37 +02001158 }
1159
Laurent Viviera22436b2007-09-24 17:00:58 +02001160 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001161
Laurent Viviere70669a2007-08-05 10:36:40 +03001162 if (vcpu->pio.string)
1163 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
1165 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001166 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1168 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1169 run->mmio.len = vcpu->mmio_size;
1170 run->mmio.is_write = vcpu->mmio_is_write;
1171 }
1172
1173 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001174 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1175 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001177 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 return EMULATE_FAIL;
1179 }
1180 return EMULATE_DO_MMIO;
1181 }
1182
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001183 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001184 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185
Avi Kivity02c83202007-04-29 15:02:17 +03001186 if (vcpu->mmio_is_write) {
1187 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001189 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190
1191 return EMULATE_DONE;
1192}
1193EXPORT_SYMBOL_GPL(emulate_instruction);
1194
Eddie Dongb6958ce2007-07-18 12:15:21 +03001195/*
1196 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1197 */
He, Qingc5ec1532007-09-03 17:07:41 +03001198static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001199{
1200 DECLARE_WAITQUEUE(wait, current);
1201
1202 add_wait_queue(&vcpu->wq, &wait);
1203
1204 /*
1205 * We will block until either an interrupt or a signal wakes us up
1206 */
He, Qingc5ec1532007-09-03 17:07:41 +03001207 while (!kvm_cpu_has_interrupt(vcpu)
1208 && !signal_pending(current)
1209 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1210 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001211 set_current_state(TASK_INTERRUPTIBLE);
1212 vcpu_put(vcpu);
1213 schedule();
1214 vcpu_load(vcpu);
1215 }
1216
He, Qingc5ec1532007-09-03 17:07:41 +03001217 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001218 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001219}
1220
Avi Kivityd3bef152007-06-05 15:53:05 +03001221int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1222{
Avi Kivityd3bef152007-06-05 15:53:05 +03001223 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001224 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001225 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1226 kvm_vcpu_block(vcpu);
1227 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1228 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001229 return 1;
1230 } else {
1231 vcpu->run->exit_reason = KVM_EXIT_HLT;
1232 return 0;
1233 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001234}
1235EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1236
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001237int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001238{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001239 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001240
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001241 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001242
1243 nr = vcpu->regs[VCPU_REGS_RAX];
1244 a0 = vcpu->regs[VCPU_REGS_RBX];
1245 a1 = vcpu->regs[VCPU_REGS_RCX];
1246 a2 = vcpu->regs[VCPU_REGS_RDX];
1247 a3 = vcpu->regs[VCPU_REGS_RSI];
1248
1249 if (!is_long_mode(vcpu)) {
1250 nr &= 0xFFFFFFFF;
1251 a0 &= 0xFFFFFFFF;
1252 a1 &= 0xFFFFFFFF;
1253 a2 &= 0xFFFFFFFF;
1254 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001255 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001256
Avi Kivity270fd9b2007-02-19 14:37:47 +02001257 switch (nr) {
1258 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001259 ret = -KVM_ENOSYS;
1260 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001261 }
1262 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001263 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001264 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001265}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001266EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1267
1268int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1269{
1270 char instruction[3];
1271 int ret = 0;
1272
1273 mutex_lock(&vcpu->kvm->lock);
1274
1275 /*
1276 * Blow out the MMU to ensure that no other VCPU has an active mapping
1277 * to ensure that the updated hypercall appears atomically across all
1278 * VCPUs.
1279 */
1280 kvm_mmu_zap_all(vcpu->kvm);
1281
1282 kvm_x86_ops->cache_regs(vcpu);
1283 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1284 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1285 != X86EMUL_CONTINUE)
1286 ret = -EFAULT;
1287
1288 mutex_unlock(&vcpu->kvm->lock);
1289
1290 return ret;
1291}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001292
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1294{
1295 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1296}
1297
1298void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1299{
1300 struct descriptor_table dt = { limit, base };
1301
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001302 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303}
1304
1305void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1306{
1307 struct descriptor_table dt = { limit, base };
1308
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001309 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310}
1311
1312void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1313 unsigned long *rflags)
1314{
1315 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001316 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317}
1318
1319unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1320{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001321 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 switch (cr) {
1323 case 0:
1324 return vcpu->cr0;
1325 case 2:
1326 return vcpu->cr2;
1327 case 3:
1328 return vcpu->cr3;
1329 case 4:
1330 return vcpu->cr4;
1331 default:
1332 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1333 return 0;
1334 }
1335}
1336
1337void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1338 unsigned long *rflags)
1339{
1340 switch (cr) {
1341 case 0:
1342 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001343 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 break;
1345 case 2:
1346 vcpu->cr2 = val;
1347 break;
1348 case 3:
1349 set_cr3(vcpu, val);
1350 break;
1351 case 4:
1352 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1353 break;
1354 default:
1355 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1356 }
1357}
1358
Avi Kivity3bab1f52006-12-29 16:49:48 -08001359int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1360{
1361 u64 data;
1362
1363 switch (msr) {
1364 case 0xc0010010: /* SYSCFG */
1365 case 0xc0010015: /* HWCR */
1366 case MSR_IA32_PLATFORM_ID:
1367 case MSR_IA32_P5_MC_ADDR:
1368 case MSR_IA32_P5_MC_TYPE:
1369 case MSR_IA32_MC0_CTL:
1370 case MSR_IA32_MCG_STATUS:
1371 case MSR_IA32_MCG_CAP:
1372 case MSR_IA32_MC0_MISC:
1373 case MSR_IA32_MC0_MISC+4:
1374 case MSR_IA32_MC0_MISC+8:
1375 case MSR_IA32_MC0_MISC+12:
1376 case MSR_IA32_MC0_MISC+16:
1377 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001378 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001379 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001380 /* MTRR registers */
1381 case 0xfe:
1382 case 0x200 ... 0x2ff:
1383 data = 0;
1384 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001385 case 0xcd: /* fsb frequency */
1386 data = 3;
1387 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001388 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001389 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001390 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001391 case MSR_IA32_MISC_ENABLE:
1392 data = vcpu->ia32_misc_enable_msr;
1393 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001394#ifdef CONFIG_X86_64
1395 case MSR_EFER:
1396 data = vcpu->shadow_efer;
1397 break;
1398#endif
1399 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001400 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001401 return 1;
1402 }
1403 *pdata = data;
1404 return 0;
1405}
1406EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1407
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408/*
1409 * Reads an msr value (of 'msr_index') into 'pdata'.
1410 * Returns 0 on success, non-0 otherwise.
1411 * Assumes vcpu_load() was already called.
1412 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001413int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001415 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001418#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419
Avi Kivity3bab1f52006-12-29 16:49:48 -08001420static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 if (efer & EFER_RESERVED_BITS) {
1423 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1424 efer);
1425 inject_gp(vcpu);
1426 return;
1427 }
1428
1429 if (is_paging(vcpu)
1430 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1431 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1432 inject_gp(vcpu);
1433 return;
1434 }
1435
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001436 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001437
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438 efer &= ~EFER_LMA;
1439 efer |= vcpu->shadow_efer & EFER_LMA;
1440
1441 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443
1444#endif
1445
Avi Kivity3bab1f52006-12-29 16:49:48 -08001446int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1447{
1448 switch (msr) {
1449#ifdef CONFIG_X86_64
1450 case MSR_EFER:
1451 set_efer(vcpu, data);
1452 break;
1453#endif
1454 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001455 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001456 __FUNCTION__, data);
1457 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001458 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001459 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001460 __FUNCTION__, data);
1461 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001462 case MSR_IA32_UCODE_REV:
1463 case MSR_IA32_UCODE_WRITE:
1464 case 0x200 ... 0x2ff: /* MTRRs */
1465 break;
1466 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001467 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001468 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001469 case MSR_IA32_MISC_ENABLE:
1470 vcpu->ia32_misc_enable_msr = data;
1471 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001472 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001473 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001474 return 1;
1475 }
1476 return 0;
1477}
1478EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1479
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480/*
1481 * Writes msr value into into the appropriate "register".
1482 * Returns 0 on success, non-0 otherwise.
1483 * Assumes vcpu_load() was already called.
1484 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001485int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001487 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
1490void kvm_resched(struct kvm_vcpu *vcpu)
1491{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001492 if (!need_resched())
1493 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496EXPORT_SYMBOL_GPL(kvm_resched);
1497
Avi Kivity06465c52007-02-28 20:46:53 +02001498void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1499{
1500 int i;
1501 u32 function;
1502 struct kvm_cpuid_entry *e, *best;
1503
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001504 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001505 function = vcpu->regs[VCPU_REGS_RAX];
1506 vcpu->regs[VCPU_REGS_RAX] = 0;
1507 vcpu->regs[VCPU_REGS_RBX] = 0;
1508 vcpu->regs[VCPU_REGS_RCX] = 0;
1509 vcpu->regs[VCPU_REGS_RDX] = 0;
1510 best = NULL;
1511 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1512 e = &vcpu->cpuid_entries[i];
1513 if (e->function == function) {
1514 best = e;
1515 break;
1516 }
1517 /*
1518 * Both basic or both extended?
1519 */
1520 if (((e->function ^ function) & 0x80000000) == 0)
1521 if (!best || e->function > best->function)
1522 best = e;
1523 }
1524 if (best) {
1525 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1526 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1527 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1528 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1529 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001530 kvm_x86_ops->decache_regs(vcpu);
1531 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001532}
1533EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1534
Avi Kivity039576c2007-03-20 12:46:50 +02001535static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001536{
Avi Kivity039576c2007-03-20 12:46:50 +02001537 void *p = vcpu->pio_data;
1538 void *q;
1539 unsigned bytes;
1540 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1541
Avi Kivity039576c2007-03-20 12:46:50 +02001542 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1543 PAGE_KERNEL);
1544 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001545 free_pio_guest_pages(vcpu);
1546 return -ENOMEM;
1547 }
1548 q += vcpu->pio.guest_page_offset;
1549 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1550 if (vcpu->pio.in)
1551 memcpy(q, p, bytes);
1552 else
1553 memcpy(p, q, bytes);
1554 q -= vcpu->pio.guest_page_offset;
1555 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001556 free_pio_guest_pages(vcpu);
1557 return 0;
1558}
1559
1560static int complete_pio(struct kvm_vcpu *vcpu)
1561{
1562 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001563 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001564 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001565
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001566 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001567
1568 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001569 if (io->in)
1570 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001571 io->size);
1572 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001573 if (io->in) {
1574 r = pio_copy_data(vcpu);
1575 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001576 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001577 return r;
1578 }
1579 }
1580
Avi Kivity46fc1472007-02-22 19:39:30 +02001581 delta = 1;
1582 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001583 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001584 /*
1585 * The size of the register should really depend on
1586 * current address size.
1587 */
1588 vcpu->regs[VCPU_REGS_RCX] -= delta;
1589 }
Avi Kivity039576c2007-03-20 12:46:50 +02001590 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001591 delta = -delta;
1592 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001593 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001594 vcpu->regs[VCPU_REGS_RDI] += delta;
1595 else
1596 vcpu->regs[VCPU_REGS_RSI] += delta;
1597 }
1598
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001599 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001600
Avi Kivity039576c2007-03-20 12:46:50 +02001601 io->count -= io->cur_count;
1602 io->cur_count = 0;
1603
Avi Kivity039576c2007-03-20 12:46:50 +02001604 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001605}
1606
Eddie Dong65619eb2007-07-17 11:52:33 +03001607static void kernel_pio(struct kvm_io_device *pio_dev,
1608 struct kvm_vcpu *vcpu,
1609 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001610{
1611 /* TODO: String I/O for in kernel device */
1612
Eddie Dong9cf98822007-07-22 10:36:31 +03001613 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001614 if (vcpu->pio.in)
1615 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1616 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001617 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001618 else
1619 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1620 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001621 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001622 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001623}
1624
1625static void pio_string_write(struct kvm_io_device *pio_dev,
1626 struct kvm_vcpu *vcpu)
1627{
1628 struct kvm_pio_request *io = &vcpu->pio;
1629 void *pd = vcpu->pio_data;
1630 int i;
1631
Eddie Dong9cf98822007-07-22 10:36:31 +03001632 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001633 for (i = 0; i < io->cur_count; i++) {
1634 kvm_iodevice_write(pio_dev, io->port,
1635 io->size,
1636 pd);
1637 pd += io->size;
1638 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001639 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001640}
1641
Mike Dayd77c26f2007-10-08 09:02:08 -04001642int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001643 int size, unsigned port)
1644{
1645 struct kvm_io_device *pio_dev;
1646
1647 vcpu->run->exit_reason = KVM_EXIT_IO;
1648 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1649 vcpu->run->io.size = vcpu->pio.size = size;
1650 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1651 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1652 vcpu->run->io.port = vcpu->pio.port = port;
1653 vcpu->pio.in = in;
1654 vcpu->pio.string = 0;
1655 vcpu->pio.down = 0;
1656 vcpu->pio.guest_page_offset = 0;
1657 vcpu->pio.rep = 0;
1658
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001659 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001660 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001661 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001662
Avi Kivity0967b7b2007-09-15 17:34:36 +03001663 kvm_x86_ops->skip_emulated_instruction(vcpu);
1664
Laurent Vivier3090dd72007-08-05 10:43:32 +03001665 pio_dev = vcpu_find_pio_dev(vcpu, port);
1666 if (pio_dev) {
1667 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1668 complete_pio(vcpu);
1669 return 1;
1670 }
1671 return 0;
1672}
1673EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1674
1675int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1676 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001677 gva_t address, int rep, unsigned port)
1678{
1679 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001680 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001681 int nr_pages = 1;
1682 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001683 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001684
1685 vcpu->run->exit_reason = KVM_EXIT_IO;
1686 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001687 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001688 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001689 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1690 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001691 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001692 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001693 vcpu->pio.down = down;
1694 vcpu->pio.guest_page_offset = offset_in_page(address);
1695 vcpu->pio.rep = rep;
1696
Avi Kivity039576c2007-03-20 12:46:50 +02001697 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001698 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001699 return 1;
1700 }
1701
Avi Kivity039576c2007-03-20 12:46:50 +02001702 if (!down)
1703 in_page = PAGE_SIZE - offset_in_page(address);
1704 else
1705 in_page = offset_in_page(address) + size;
1706 now = min(count, (unsigned long)in_page / size);
1707 if (!now) {
1708 /*
1709 * String I/O straddles page boundary. Pin two guest pages
1710 * so that we satisfy atomicity constraints. Do just one
1711 * transaction to avoid complexity.
1712 */
1713 nr_pages = 2;
1714 now = 1;
1715 }
1716 if (down) {
1717 /*
1718 * String I/O in reverse. Yuck. Kill the guest, fix later.
1719 */
Rusty Russellf0242472007-08-01 10:48:02 +10001720 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001721 inject_gp(vcpu);
1722 return 1;
1723 }
1724 vcpu->run->io.count = now;
1725 vcpu->pio.cur_count = now;
1726
Avi Kivity0967b7b2007-09-15 17:34:36 +03001727 if (vcpu->pio.cur_count == vcpu->pio.count)
1728 kvm_x86_ops->skip_emulated_instruction(vcpu);
1729
Avi Kivity039576c2007-03-20 12:46:50 +02001730 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001731 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001732 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02001733 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001734 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001735 if (!page) {
1736 inject_gp(vcpu);
1737 free_pio_guest_pages(vcpu);
1738 return 1;
1739 }
1740 }
1741
Laurent Vivier3090dd72007-08-05 10:43:32 +03001742 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001743 if (!vcpu->pio.in) {
1744 /* string PIO write */
1745 ret = pio_copy_data(vcpu);
1746 if (ret >= 0 && pio_dev) {
1747 pio_string_write(pio_dev, vcpu);
1748 complete_pio(vcpu);
1749 if (vcpu->pio.count == 0)
1750 ret = 1;
1751 }
1752 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10001753 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03001754 "port %x size %d count %ld\n",
1755 port, size, count);
1756
1757 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001758}
Laurent Vivier3090dd72007-08-05 10:43:32 +03001759EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02001760
Avi Kivity04d2cc72007-09-10 18:10:54 +03001761/*
1762 * Check if userspace requested an interrupt window, and that the
1763 * interrupt window is open.
1764 *
1765 * No need to exit to userspace if we already have an interrupt queued.
1766 */
1767static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1768 struct kvm_run *kvm_run)
1769{
1770 return (!vcpu->irq_summary &&
1771 kvm_run->request_interrupt_window &&
1772 vcpu->interrupt_window_open &&
1773 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1774}
1775
1776static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1777 struct kvm_run *kvm_run)
1778{
1779 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1780 kvm_run->cr8 = get_cr8(vcpu);
1781 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1782 if (irqchip_in_kernel(vcpu->kvm))
1783 kvm_run->ready_for_interrupt_injection = 1;
1784 else
1785 kvm_run->ready_for_interrupt_injection =
1786 (vcpu->interrupt_window_open &&
1787 vcpu->irq_summary == 0);
1788}
1789
1790static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1791{
1792 int r;
1793
1794 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001795 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03001796 vcpu->vcpu_id, vcpu->sipi_vector);
1797 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001798 r = kvm_x86_ops->vcpu_reset(vcpu);
1799 if (r)
1800 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001801 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
1802 }
1803
1804preempted:
1805 if (vcpu->guest_debug.enabled)
1806 kvm_x86_ops->guest_debug_pre(vcpu);
1807
1808again:
1809 r = kvm_mmu_reload(vcpu);
1810 if (unlikely(r))
1811 goto out;
1812
Avi Kivityab6ef342007-10-16 16:23:22 +02001813 kvm_inject_pending_timer_irqs(vcpu);
1814
Avi Kivity04d2cc72007-09-10 18:10:54 +03001815 preempt_disable();
1816
1817 kvm_x86_ops->prepare_guest_switch(vcpu);
1818 kvm_load_guest_fpu(vcpu);
1819
1820 local_irq_disable();
1821
1822 if (signal_pending(current)) {
1823 local_irq_enable();
1824 preempt_enable();
1825 r = -EINTR;
1826 kvm_run->exit_reason = KVM_EXIT_INTR;
1827 ++vcpu->stat.signal_exits;
1828 goto out;
1829 }
1830
1831 if (irqchip_in_kernel(vcpu->kvm))
1832 kvm_x86_ops->inject_pending_irq(vcpu);
1833 else if (!vcpu->mmio_read_completed)
1834 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
1835
1836 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02001837 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03001838
1839 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02001840 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03001841 kvm_x86_ops->tlb_flush(vcpu);
1842
1843 kvm_x86_ops->run(vcpu, kvm_run);
1844
1845 vcpu->guest_mode = 0;
1846 local_irq_enable();
1847
1848 ++vcpu->stat.exits;
1849
Laurent Vivier0552f732007-10-18 15:19:01 +02001850 /*
1851 * We must have an instruction between local_irq_enable() and
1852 * kvm_guest_exit(), so the timer interrupt isn't delayed by
1853 * the interrupt shadow. The stat.exits increment will do nicely.
1854 * But we need to prevent reordering, hence this barrier():
1855 */
1856 barrier();
1857
1858 kvm_guest_exit();
1859
Avi Kivity04d2cc72007-09-10 18:10:54 +03001860 preempt_enable();
1861
1862 /*
1863 * Profile KVM exit RIPs:
1864 */
1865 if (unlikely(prof_on == KVM_PROFILING)) {
1866 kvm_x86_ops->cache_regs(vcpu);
1867 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
1868 }
1869
1870 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
1871
1872 if (r > 0) {
1873 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
1874 r = -EINTR;
1875 kvm_run->exit_reason = KVM_EXIT_INTR;
1876 ++vcpu->stat.request_irq_exits;
1877 goto out;
1878 }
1879 if (!need_resched()) {
1880 ++vcpu->stat.light_exits;
1881 goto again;
1882 }
1883 }
1884
1885out:
1886 if (r > 0) {
1887 kvm_resched(vcpu);
1888 goto preempted;
1889 }
1890
1891 post_kvm_run_save(vcpu, kvm_run);
1892
1893 return r;
1894}
1895
1896
Avi Kivitybccf2152007-02-21 18:04:26 +02001897static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001900 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901
Avi Kivitybccf2152007-02-21 18:04:26 +02001902 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903
He, Qingc5ec1532007-09-03 17:07:41 +03001904 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
1905 kvm_vcpu_block(vcpu);
1906 vcpu_put(vcpu);
1907 return -EAGAIN;
1908 }
1909
Avi Kivity1961d272007-03-05 19:46:05 +02001910 if (vcpu->sigset_active)
1911 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1912
Dor Laor54810342007-02-12 00:54:39 -08001913 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08001914 if (!irqchip_in_kernel(vcpu->kvm))
1915 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08001916
Avi Kivity02c83202007-04-29 15:02:17 +03001917 if (vcpu->pio.cur_count) {
1918 r = complete_pio(vcpu);
1919 if (r)
1920 goto out;
1921 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08001922#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03001923 if (vcpu->mmio_needed) {
1924 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1925 vcpu->mmio_read_completed = 1;
1926 vcpu->mmio_needed = 0;
1927 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02001928 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03001929 if (r == EMULATE_DO_MMIO) {
1930 /*
1931 * Read-modify-write. Back to userspace.
1932 */
Avi Kivity02c83202007-04-29 15:02:17 +03001933 r = 0;
1934 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001935 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08001937#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02001938 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001939 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02001940 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001941 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02001942 }
1943
Avi Kivity04d2cc72007-09-10 18:10:54 +03001944 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945
Avi Kivity039576c2007-03-20 12:46:50 +02001946out:
Avi Kivity1961d272007-03-05 19:46:05 +02001947 if (vcpu->sigset_active)
1948 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1949
Avi Kivity6aa8b732006-12-10 02:21:36 -08001950 vcpu_put(vcpu);
1951 return r;
1952}
1953
Avi Kivitybccf2152007-02-21 18:04:26 +02001954static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1955 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956{
Avi Kivitybccf2152007-02-21 18:04:26 +02001957 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001959 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960
1961 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1962 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1963 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1964 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1965 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1966 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1967 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1968 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001969#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1971 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1972 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1973 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1974 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1975 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1976 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1977 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1978#endif
1979
1980 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001981 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982
1983 /*
1984 * Don't leak debug flags in case they were set for guest debugging
1985 */
1986 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1987 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1988
1989 vcpu_put(vcpu);
1990
1991 return 0;
1992}
1993
Avi Kivitybccf2152007-02-21 18:04:26 +02001994static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1995 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996{
Avi Kivitybccf2152007-02-21 18:04:26 +02001997 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001998
1999 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2000 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2001 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2002 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2003 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2004 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2005 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2006 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002007#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2009 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2010 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2011 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2012 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2013 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2014 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2015 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2016#endif
2017
2018 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002019 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002021 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022
2023 vcpu_put(vcpu);
2024
2025 return 0;
2026}
2027
2028static void get_segment(struct kvm_vcpu *vcpu,
2029 struct kvm_segment *var, int seg)
2030{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002031 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032}
2033
Avi Kivitybccf2152007-02-21 18:04:26 +02002034static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2035 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002038 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039
Avi Kivitybccf2152007-02-21 18:04:26 +02002040 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041
2042 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2043 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2044 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2045 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2046 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2047 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2048
2049 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2050 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2051
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002052 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053 sregs->idt.limit = dt.limit;
2054 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002055 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002056 sregs->gdt.limit = dt.limit;
2057 sregs->gdt.base = dt.base;
2058
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002059 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 sregs->cr0 = vcpu->cr0;
2061 sregs->cr2 = vcpu->cr2;
2062 sregs->cr3 = vcpu->cr3;
2063 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002064 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002066 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067
Eddie Dong2a8067f2007-08-06 16:29:07 +03002068 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002069 memset(sregs->interrupt_bitmap, 0,
2070 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002071 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002072 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002073 set_bit(pending_vec,
2074 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002075 } else
He, Qingc52fb352007-08-02 14:03:07 +03002076 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2077 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
2079 vcpu_put(vcpu);
2080
2081 return 0;
2082}
2083
2084static void set_segment(struct kvm_vcpu *vcpu,
2085 struct kvm_segment *var, int seg)
2086{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002087 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088}
2089
Avi Kivitybccf2152007-02-21 18:04:26 +02002090static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2091 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002094 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095 struct descriptor_table dt;
2096
Avi Kivitybccf2152007-02-21 18:04:26 +02002097 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 dt.limit = sregs->idt.limit;
2100 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002101 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 dt.limit = sregs->gdt.limit;
2103 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002104 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105
2106 vcpu->cr2 = sregs->cr2;
2107 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2108 vcpu->cr3 = sregs->cr3;
2109
Eddie Dong7017fc32007-07-18 11:34:57 +03002110 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111
2112 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002113#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002114 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002116 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002118 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002119
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002121 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002122 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123
2124 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002125 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002126 if (!is_long_mode(vcpu) && is_pae(vcpu))
2127 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128
2129 if (mmu_reset_needed)
2130 kvm_mmu_reset_context(vcpu);
2131
He, Qingc52fb352007-08-02 14:03:07 +03002132 if (!irqchip_in_kernel(vcpu->kvm)) {
2133 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2134 sizeof vcpu->irq_pending);
2135 vcpu->irq_summary = 0;
2136 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2137 if (vcpu->irq_pending[i])
2138 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002139 } else {
2140 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2141 pending_vec = find_first_bit(
2142 (const unsigned long *)sregs->interrupt_bitmap,
2143 max_bits);
2144 /* Only pending external irq is handled here */
2145 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002146 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002147 pr_debug("Set back pending irq %d\n",
2148 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002149 }
He, Qingc52fb352007-08-02 14:03:07 +03002150 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151
Avi Kivity024aa1c2007-03-21 13:44:58 +02002152 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2153 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2154 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2155 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2156 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2157 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2158
2159 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2160 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2161
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162 vcpu_put(vcpu);
2163
2164 return 0;
2165}
2166
Rusty Russell1747fb72007-09-06 01:21:32 +10002167void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2168{
2169 struct kvm_segment cs;
2170
2171 get_segment(vcpu, &cs, VCPU_SREG_CS);
2172 *db = cs.db;
2173 *l = cs.l;
2174}
2175EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2176
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 * Translate a guest virtual address to a guest physical address.
2179 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002180static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2181 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182{
2183 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 gpa_t gpa;
2185
Avi Kivitybccf2152007-02-21 18:04:26 +02002186 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002187 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2189 tr->physical_address = gpa;
2190 tr->valid = gpa != UNMAPPED_GVA;
2191 tr->writeable = 1;
2192 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002193 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 vcpu_put(vcpu);
2195
2196 return 0;
2197}
2198
Avi Kivitybccf2152007-02-21 18:04:26 +02002199static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2200 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202 if (irq->irq < 0 || irq->irq >= 256)
2203 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002204 if (irqchip_in_kernel(vcpu->kvm))
2205 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002206 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207
2208 set_bit(irq->irq, vcpu->irq_pending);
2209 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2210
2211 vcpu_put(vcpu);
2212
2213 return 0;
2214}
2215
Avi Kivitybccf2152007-02-21 18:04:26 +02002216static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2217 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 int r;
2220
Avi Kivitybccf2152007-02-21 18:04:26 +02002221 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002223 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224
2225 vcpu_put(vcpu);
2226
2227 return r;
2228}
2229
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002230static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2231 unsigned long address,
2232 int *type)
2233{
2234 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2235 unsigned long pgoff;
2236 struct page *page;
2237
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002238 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002239 if (pgoff == 0)
2240 page = virt_to_page(vcpu->run);
2241 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2242 page = virt_to_page(vcpu->pio_data);
2243 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002244 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002245 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002246 if (type != NULL)
2247 *type = VM_FAULT_MINOR;
2248
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002249 return page;
2250}
2251
2252static struct vm_operations_struct kvm_vcpu_vm_ops = {
2253 .nopage = kvm_vcpu_nopage,
2254};
2255
2256static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2257{
2258 vma->vm_ops = &kvm_vcpu_vm_ops;
2259 return 0;
2260}
2261
Avi Kivitybccf2152007-02-21 18:04:26 +02002262static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2263{
2264 struct kvm_vcpu *vcpu = filp->private_data;
2265
2266 fput(vcpu->kvm->filp);
2267 return 0;
2268}
2269
2270static struct file_operations kvm_vcpu_fops = {
2271 .release = kvm_vcpu_release,
2272 .unlocked_ioctl = kvm_vcpu_ioctl,
2273 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002274 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002275};
2276
2277/*
2278 * Allocates an inode for the vcpu.
2279 */
2280static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2281{
2282 int fd, r;
2283 struct inode *inode;
2284 struct file *file;
2285
Avi Kivityd6d28162007-06-28 08:38:16 -04002286 r = anon_inode_getfd(&fd, &inode, &file,
2287 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2288 if (r)
2289 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002290 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002291 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002292}
2293
Avi Kivityc5ea7662007-02-20 18:41:05 +02002294/*
2295 * Creates some virtual cpus. Good luck creating more than one.
2296 */
2297static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2298{
2299 int r;
2300 struct kvm_vcpu *vcpu;
2301
Avi Kivityc5ea7662007-02-20 18:41:05 +02002302 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002303 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002304
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002305 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002306 if (IS_ERR(vcpu))
2307 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002308
Avi Kivity15ad7142007-07-11 18:17:21 +03002309 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2310
Rusty Russellb114b082007-07-30 21:13:43 +10002311 /* We do fxsave: this must be aligned. */
2312 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2313
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002314 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002315 r = kvm_x86_ops->vcpu_reset(vcpu);
2316 if (r == 0)
2317 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002318 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002319 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002320 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002321
Shaohua Li11ec2802007-07-23 14:51:37 +08002322 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002323 if (kvm->vcpus[n]) {
2324 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002325 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002326 goto mmu_unload;
2327 }
2328 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002329 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002330
2331 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002332 r = create_vcpu_fd(vcpu);
2333 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002334 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002335 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002336
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002337unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002338 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002339 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002340 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002341
2342mmu_unload:
2343 vcpu_load(vcpu);
2344 kvm_mmu_unload(vcpu);
2345 vcpu_put(vcpu);
2346
2347free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002348 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002349 return r;
2350}
2351
Avi Kivity1961d272007-03-05 19:46:05 +02002352static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2353{
2354 if (sigset) {
2355 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2356 vcpu->sigset_active = 1;
2357 vcpu->sigset = *sigset;
2358 } else
2359 vcpu->sigset_active = 0;
2360 return 0;
2361}
2362
Avi Kivityb8836732007-04-01 16:34:31 +03002363/*
2364 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2365 * we have asm/x86/processor.h
2366 */
2367struct fxsave {
2368 u16 cwd;
2369 u16 swd;
2370 u16 twd;
2371 u16 fop;
2372 u64 rip;
2373 u64 rdp;
2374 u32 mxcsr;
2375 u32 mxcsr_mask;
2376 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2377#ifdef CONFIG_X86_64
2378 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2379#else
2380 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2381#endif
2382};
2383
2384static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2385{
Rusty Russellb114b082007-07-30 21:13:43 +10002386 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002387
2388 vcpu_load(vcpu);
2389
2390 memcpy(fpu->fpr, fxsave->st_space, 128);
2391 fpu->fcw = fxsave->cwd;
2392 fpu->fsw = fxsave->swd;
2393 fpu->ftwx = fxsave->twd;
2394 fpu->last_opcode = fxsave->fop;
2395 fpu->last_ip = fxsave->rip;
2396 fpu->last_dp = fxsave->rdp;
2397 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2398
2399 vcpu_put(vcpu);
2400
2401 return 0;
2402}
2403
2404static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2405{
Rusty Russellb114b082007-07-30 21:13:43 +10002406 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002407
2408 vcpu_load(vcpu);
2409
2410 memcpy(fxsave->st_space, fpu->fpr, 128);
2411 fxsave->cwd = fpu->fcw;
2412 fxsave->swd = fpu->fsw;
2413 fxsave->twd = fpu->ftwx;
2414 fxsave->fop = fpu->last_opcode;
2415 fxsave->rip = fpu->last_ip;
2416 fxsave->rdp = fpu->last_dp;
2417 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2418
2419 vcpu_put(vcpu);
2420
2421 return 0;
2422}
2423
Avi Kivitybccf2152007-02-21 18:04:26 +02002424static long kvm_vcpu_ioctl(struct file *filp,
2425 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Avi Kivitybccf2152007-02-21 18:04:26 +02002427 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002428 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002429 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430
2431 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002432 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002433 r = -EINVAL;
2434 if (arg)
2435 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002436 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 case KVM_GET_REGS: {
2439 struct kvm_regs kvm_regs;
2440
Avi Kivitybccf2152007-02-21 18:04:26 +02002441 memset(&kvm_regs, 0, sizeof kvm_regs);
2442 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443 if (r)
2444 goto out;
2445 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002446 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 goto out;
2448 r = 0;
2449 break;
2450 }
2451 case KVM_SET_REGS: {
2452 struct kvm_regs kvm_regs;
2453
2454 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002455 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002457 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 if (r)
2459 goto out;
2460 r = 0;
2461 break;
2462 }
2463 case KVM_GET_SREGS: {
2464 struct kvm_sregs kvm_sregs;
2465
Avi Kivitybccf2152007-02-21 18:04:26 +02002466 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2467 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 if (r)
2469 goto out;
2470 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002471 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 goto out;
2473 r = 0;
2474 break;
2475 }
2476 case KVM_SET_SREGS: {
2477 struct kvm_sregs kvm_sregs;
2478
2479 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002480 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002482 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 if (r)
2484 goto out;
2485 r = 0;
2486 break;
2487 }
2488 case KVM_TRANSLATE: {
2489 struct kvm_translation tr;
2490
2491 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002492 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002494 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495 if (r)
2496 goto out;
2497 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002498 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 goto out;
2500 r = 0;
2501 break;
2502 }
2503 case KVM_INTERRUPT: {
2504 struct kvm_interrupt irq;
2505
2506 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002507 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002508 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002509 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 if (r)
2511 goto out;
2512 r = 0;
2513 break;
2514 }
2515 case KVM_DEBUG_GUEST: {
2516 struct kvm_debug_guest dbg;
2517
2518 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002519 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002521 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522 if (r)
2523 goto out;
2524 r = 0;
2525 break;
2526 }
Avi Kivity1961d272007-03-05 19:46:05 +02002527 case KVM_SET_SIGNAL_MASK: {
2528 struct kvm_signal_mask __user *sigmask_arg = argp;
2529 struct kvm_signal_mask kvm_sigmask;
2530 sigset_t sigset, *p;
2531
2532 p = NULL;
2533 if (argp) {
2534 r = -EFAULT;
2535 if (copy_from_user(&kvm_sigmask, argp,
2536 sizeof kvm_sigmask))
2537 goto out;
2538 r = -EINVAL;
2539 if (kvm_sigmask.len != sizeof sigset)
2540 goto out;
2541 r = -EFAULT;
2542 if (copy_from_user(&sigset, sigmask_arg->sigset,
2543 sizeof sigset))
2544 goto out;
2545 p = &sigset;
2546 }
2547 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2548 break;
2549 }
Avi Kivityb8836732007-04-01 16:34:31 +03002550 case KVM_GET_FPU: {
2551 struct kvm_fpu fpu;
2552
2553 memset(&fpu, 0, sizeof fpu);
2554 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2555 if (r)
2556 goto out;
2557 r = -EFAULT;
2558 if (copy_to_user(argp, &fpu, sizeof fpu))
2559 goto out;
2560 r = 0;
2561 break;
2562 }
2563 case KVM_SET_FPU: {
2564 struct kvm_fpu fpu;
2565
2566 r = -EFAULT;
2567 if (copy_from_user(&fpu, argp, sizeof fpu))
2568 goto out;
2569 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2570 if (r)
2571 goto out;
2572 r = 0;
2573 break;
2574 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002575 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002576 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002577 }
2578out:
2579 return r;
2580}
2581
2582static long kvm_vm_ioctl(struct file *filp,
2583 unsigned int ioctl, unsigned long arg)
2584{
2585 struct kvm *kvm = filp->private_data;
2586 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002587 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002588
2589 switch (ioctl) {
2590 case KVM_CREATE_VCPU:
2591 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2592 if (r < 0)
2593 goto out;
2594 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002595 case KVM_SET_USER_MEMORY_REGION: {
2596 struct kvm_userspace_memory_region kvm_userspace_mem;
2597
2598 r = -EFAULT;
2599 if (copy_from_user(&kvm_userspace_mem, argp,
2600 sizeof kvm_userspace_mem))
2601 goto out;
2602
2603 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 if (r)
2605 goto out;
2606 break;
2607 }
2608 case KVM_GET_DIRTY_LOG: {
2609 struct kvm_dirty_log log;
2610
2611 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002612 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002614 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 if (r)
2616 goto out;
2617 break;
2618 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002619 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002620 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002621 }
2622out:
2623 return r;
2624}
2625
2626static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2627 unsigned long address,
2628 int *type)
2629{
2630 struct kvm *kvm = vma->vm_file->private_data;
2631 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002632 struct page *page;
2633
Avi Kivityf17abe92007-02-21 19:28:04 +02002634 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02002635 if (!kvm_is_visible_gfn(kvm, pgoff))
2636 return NOPAGE_SIGBUS;
Anthony Liguoriaab61cc2007-10-29 15:15:20 -05002637 /* current->mm->mmap_sem is already held so call lockless version */
2638 page = __gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002639 if (is_error_page(page)) {
2640 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02002641 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02002642 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002643 if (type != NULL)
2644 *type = VM_FAULT_MINOR;
2645
Avi Kivityf17abe92007-02-21 19:28:04 +02002646 return page;
2647}
2648
2649static struct vm_operations_struct kvm_vm_vm_ops = {
2650 .nopage = kvm_vm_nopage,
2651};
2652
2653static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2654{
2655 vma->vm_ops = &kvm_vm_vm_ops;
2656 return 0;
2657}
2658
2659static struct file_operations kvm_vm_fops = {
2660 .release = kvm_vm_release,
2661 .unlocked_ioctl = kvm_vm_ioctl,
2662 .compat_ioctl = kvm_vm_ioctl,
2663 .mmap = kvm_vm_mmap,
2664};
2665
2666static int kvm_dev_ioctl_create_vm(void)
2667{
2668 int fd, r;
2669 struct inode *inode;
2670 struct file *file;
2671 struct kvm *kvm;
2672
Avi Kivityf17abe92007-02-21 19:28:04 +02002673 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002674 if (IS_ERR(kvm))
2675 return PTR_ERR(kvm);
2676 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2677 if (r) {
2678 kvm_destroy_vm(kvm);
2679 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002680 }
2681
Avi Kivitybccf2152007-02-21 18:04:26 +02002682 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002683
Avi Kivityf17abe92007-02-21 19:28:04 +02002684 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002685}
2686
2687static long kvm_dev_ioctl(struct file *filp,
2688 unsigned int ioctl, unsigned long arg)
2689{
2690 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002691 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002692
2693 switch (ioctl) {
2694 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002695 r = -EINVAL;
2696 if (arg)
2697 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002698 r = KVM_API_VERSION;
2699 break;
2700 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002701 r = -EINVAL;
2702 if (arg)
2703 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002704 r = kvm_dev_ioctl_create_vm();
2705 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002706 case KVM_CHECK_EXTENSION: {
2707 int ext = (long)argp;
2708
2709 switch (ext) {
2710 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03002711 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02002712 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02002713 case KVM_CAP_USER_MEMORY:
Izik Eiduscbc94022007-10-25 00:29:55 +02002714 case KVM_CAP_SET_TSS_ADDR:
Eddie Dong85f455f2007-07-06 12:20:49 +03002715 r = 1;
2716 break;
2717 default:
2718 r = 0;
2719 break;
2720 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002721 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002722 }
Avi Kivity07c45a32007-03-07 13:05:38 +02002723 case KVM_GET_VCPU_MMAP_SIZE:
2724 r = -EINVAL;
2725 if (arg)
2726 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002727 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002728 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002730 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 }
2732out:
2733 return r;
2734}
2735
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 .unlocked_ioctl = kvm_dev_ioctl,
2738 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739};
2740
2741static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002742 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 "kvm",
2744 &kvm_chardev_ops,
2745};
2746
Avi Kivity774c47f2007-02-12 00:54:47 -08002747/*
2748 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2749 * cached on it.
2750 */
2751static void decache_vcpus_on_cpu(int cpu)
2752{
2753 struct kvm *vm;
2754 struct kvm_vcpu *vcpu;
2755 int i;
2756
2757 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08002758 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08002759 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002760 vcpu = vm->vcpus[i];
2761 if (!vcpu)
2762 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08002763 /*
2764 * If the vcpu is locked, then it is running on some
2765 * other cpu and therefore it is not cached on the
2766 * cpu in question.
2767 *
2768 * If it's not locked, check the last cpu it executed
2769 * on.
2770 */
2771 if (mutex_trylock(&vcpu->mutex)) {
2772 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002773 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002774 vcpu->cpu = -1;
2775 }
2776 mutex_unlock(&vcpu->mutex);
2777 }
2778 }
2779 spin_unlock(&kvm_lock);
2780}
2781
Avi Kivity1b6c0162007-05-24 13:03:52 +03002782static void hardware_enable(void *junk)
2783{
2784 int cpu = raw_smp_processor_id();
2785
2786 if (cpu_isset(cpu, cpus_hardware_enabled))
2787 return;
2788 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002789 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002790}
2791
2792static void hardware_disable(void *junk)
2793{
2794 int cpu = raw_smp_processor_id();
2795
2796 if (!cpu_isset(cpu, cpus_hardware_enabled))
2797 return;
2798 cpu_clear(cpu, cpus_hardware_enabled);
2799 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002800 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002801}
2802
Avi Kivity774c47f2007-02-12 00:54:47 -08002803static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2804 void *v)
2805{
2806 int cpu = (long)v;
2807
2808 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03002809 case CPU_DYING:
2810 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03002811 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2812 cpu);
2813 hardware_disable(NULL);
2814 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08002815 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002816 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002817 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2818 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002819 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002820 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002821 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002822 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002823 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2824 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002825 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002826 break;
2827 }
2828 return NOTIFY_OK;
2829}
2830
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002831static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04002832 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10002833{
2834 if (val == SYS_RESTART) {
2835 /*
2836 * Some (well, at least mine) BIOSes hang on reboot if
2837 * in vmx root mode.
2838 */
2839 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2840 on_each_cpu(hardware_disable, NULL, 0, 1);
2841 }
2842 return NOTIFY_OK;
2843}
2844
2845static struct notifier_block kvm_reboot_notifier = {
2846 .notifier_call = kvm_reboot,
2847 .priority = 0,
2848};
2849
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04002850void kvm_io_bus_init(struct kvm_io_bus *bus)
2851{
2852 memset(bus, 0, sizeof(*bus));
2853}
2854
2855void kvm_io_bus_destroy(struct kvm_io_bus *bus)
2856{
2857 int i;
2858
2859 for (i = 0; i < bus->dev_count; i++) {
2860 struct kvm_io_device *pos = bus->devs[i];
2861
2862 kvm_iodevice_destructor(pos);
2863 }
2864}
2865
2866struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
2867{
2868 int i;
2869
2870 for (i = 0; i < bus->dev_count; i++) {
2871 struct kvm_io_device *pos = bus->devs[i];
2872
2873 if (pos->in_range(pos, addr))
2874 return pos;
2875 }
2876
2877 return NULL;
2878}
2879
2880void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
2881{
2882 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
2883
2884 bus->devs[bus->dev_count++] = dev;
2885}
2886
Avi Kivity774c47f2007-02-12 00:54:47 -08002887static struct notifier_block kvm_cpu_notifier = {
2888 .notifier_call = kvm_cpu_hotplug,
2889 .priority = 20, /* must be > scheduler priority */
2890};
2891
Avi Kivity1165f5f2007-04-19 17:27:43 +03002892static u64 stat_get(void *_offset)
2893{
2894 unsigned offset = (long)_offset;
2895 u64 total = 0;
2896 struct kvm *kvm;
2897 struct kvm_vcpu *vcpu;
2898 int i;
2899
2900 spin_lock(&kvm_lock);
2901 list_for_each_entry(kvm, &vm_list, vm_list)
2902 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002903 vcpu = kvm->vcpus[i];
2904 if (vcpu)
2905 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03002906 }
2907 spin_unlock(&kvm_lock);
2908 return total;
2909}
2910
Rusty Russell3dea7ca2007-08-01 10:12:22 +10002911DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03002912
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913static __init void kvm_init_debug(void)
2914{
2915 struct kvm_stats_debugfs_item *p;
2916
Al Viro8b6d44c2007-02-09 16:38:40 +00002917 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002919 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2920 (void *)(long)p->offset,
2921 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922}
2923
2924static void kvm_exit_debug(void)
2925{
2926 struct kvm_stats_debugfs_item *p;
2927
2928 for (p = debugfs_entries; p->name; ++p)
2929 debugfs_remove(p->dentry);
2930 debugfs_remove(debugfs_dir);
2931}
2932
Avi Kivity59ae6c62007-02-12 00:54:48 -08002933static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2934{
Avi Kivity4267c412007-05-24 13:09:41 +03002935 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002936 return 0;
2937}
2938
2939static int kvm_resume(struct sys_device *dev)
2940{
Avi Kivity4267c412007-05-24 13:09:41 +03002941 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002942 return 0;
2943}
2944
2945static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002946 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08002947 .suspend = kvm_suspend,
2948 .resume = kvm_resume,
2949};
2950
2951static struct sys_device kvm_sysdev = {
2952 .id = 0,
2953 .cls = &kvm_sysdev_class,
2954};
2955
Izik Eiduscea7bb22007-10-17 19:17:48 +02002956struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957
Avi Kivity15ad7142007-07-11 18:17:21 +03002958static inline
2959struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
2960{
2961 return container_of(pn, struct kvm_vcpu, preempt_notifier);
2962}
2963
2964static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
2965{
2966 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2967
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002968 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002969}
2970
2971static void kvm_sched_out(struct preempt_notifier *pn,
2972 struct task_struct *next)
2973{
2974 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
2975
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002976 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002977}
2978
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002979int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10002980 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981{
2982 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002983 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002985 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002986 printk(KERN_ERR "kvm: already loaded the other module\n");
2987 return -EEXIST;
2988 }
2989
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002990 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 printk(KERN_ERR "kvm: no hardware support\n");
2992 return -EOPNOTSUPP;
2993 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002994 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995 printk(KERN_ERR "kvm: disabled by bios\n");
2996 return -EOPNOTSUPP;
2997 }
2998
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002999 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003000
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003001 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003003 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004
Yang, Sheng002c7f72007-07-31 14:23:01 +03003005 for_each_online_cpu(cpu) {
3006 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003007 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003008 &r, 0, 1);
3009 if (r < 0)
3010 goto out_free_0;
3011 }
3012
Avi Kivity1b6c0162007-05-24 13:03:52 +03003013 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003014 r = register_cpu_notifier(&kvm_cpu_notifier);
3015 if (r)
3016 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 register_reboot_notifier(&kvm_reboot_notifier);
3018
Avi Kivity59ae6c62007-02-12 00:54:48 -08003019 r = sysdev_class_register(&kvm_sysdev_class);
3020 if (r)
3021 goto out_free_2;
3022
3023 r = sysdev_register(&kvm_sysdev);
3024 if (r)
3025 goto out_free_3;
3026
Rusty Russellc16f8622007-07-30 21:12:19 +10003027 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3028 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3029 __alignof__(struct kvm_vcpu), 0, 0);
3030 if (!kvm_vcpu_cache) {
3031 r = -ENOMEM;
3032 goto out_free_4;
3033 }
3034
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 kvm_chardev_ops.owner = module;
3036
3037 r = misc_register(&kvm_dev);
3038 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003039 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 goto out_free;
3041 }
3042
Avi Kivity15ad7142007-07-11 18:17:21 +03003043 kvm_preempt_ops.sched_in = kvm_sched_in;
3044 kvm_preempt_ops.sched_out = kvm_sched_out;
3045
Avi Kivityc7addb92007-09-16 18:58:32 +02003046 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3047
3048 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049
3050out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003051 kmem_cache_destroy(kvm_vcpu_cache);
3052out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003053 sysdev_unregister(&kvm_sysdev);
3054out_free_3:
3055 sysdev_class_unregister(&kvm_sysdev_class);
3056out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003058 unregister_cpu_notifier(&kvm_cpu_notifier);
3059out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003060 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003061out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003062 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003063out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003064 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 return r;
3066}
Mike Dayd77c26f2007-10-08 09:02:08 -04003067EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003069void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070{
3071 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003072 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003073 sysdev_unregister(&kvm_sysdev);
3074 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003076 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003077 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003078 kvm_x86_ops->hardware_unsetup();
3079 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080}
Mike Dayd77c26f2007-10-08 09:02:08 -04003081EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082
3083static __init int kvm_init(void)
3084{
Avi Kivity37e29d92007-02-20 14:07:37 +02003085 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086
Avi Kivityb5a33a72007-04-15 16:31:09 +03003087 r = kvm_mmu_module_init();
3088 if (r)
3089 goto out4;
3090
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 kvm_init_debug();
3092
Carsten Otte043405e2007-10-10 17:16:19 +02003093 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003094
Izik Eiduscea7bb22007-10-17 19:17:48 +02003095 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003096
3097 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 r = -ENOMEM;
3099 goto out;
3100 }
3101
Avi Kivity58e690e2007-02-26 16:29:43 +02003102 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103
3104out:
3105 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003106 kvm_mmu_module_exit();
3107out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 return r;
3109}
3110
3111static __exit void kvm_exit(void)
3112{
3113 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003114 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003115 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116}
3117
3118module_init(kvm_init)
3119module_exit(kvm_exit)