blob: cf163bddfbd3d6d50212e969e9c843bd76a8a85f [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
Rusty Russell707d92f2007-07-17 23:19:08 +100093#define CR0_RESERVED_BITS \
94 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
95 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
96 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
Rusty Russell66aee912007-07-17 23:34:16 +100097#define CR4_RESERVED_BITS \
98 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
99 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
100 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
101 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
102
Rusty Russell7075bc82007-07-17 23:37:17 +1000103#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define EFER_RESERVED_BITS 0xfffffffffffff2fe
105
Avi Kivitybccf2152007-02-21 18:04:26 +0200106static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
107 unsigned long arg);
108
James Morris5aacf0c2006-12-22 01:04:55 -0800109static inline int valid_vcpu(int n)
110{
111 return likely(n >= 0 && n < KVM_MAX_VCPUS);
112}
113
Avi Kivity7702fd12007-06-14 16:27:40 +0300114void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
115{
116 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
117 return;
118
119 vcpu->guest_fpu_loaded = 1;
Rusty Russellb114b082007-07-30 21:13:43 +1000120 fx_save(&vcpu->host_fx_image);
121 fx_restore(&vcpu->guest_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300122}
123EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
124
125void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
126{
127 if (!vcpu->guest_fpu_loaded)
128 return;
129
130 vcpu->guest_fpu_loaded = 0;
Rusty Russellb114b082007-07-30 21:13:43 +1000131 fx_save(&vcpu->guest_fx_image);
132 fx_restore(&vcpu->host_fx_image);
Avi Kivity7702fd12007-06-14 16:27:40 +0300133}
134EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
135
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136/*
137 * Switches to specified vcpu, until a matching vcpu_put()
138 */
Carsten Otte313a3dc2007-10-11 19:16:52 +0200139void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140{
Avi Kivity15ad7142007-07-11 18:17:21 +0300141 int cpu;
142
Avi Kivitybccf2152007-02-21 18:04:26 +0200143 mutex_lock(&vcpu->mutex);
Avi Kivity15ad7142007-07-11 18:17:21 +0300144 cpu = get_cpu();
145 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200146 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300147 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200148}
149
Carsten Otte313a3dc2007-10-11 19:16:52 +0200150void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151{
Avi Kivity15ad7142007-07-11 18:17:21 +0300152 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200153 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300154 preempt_notifier_unregister(&vcpu->preempt_notifier);
155 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156 mutex_unlock(&vcpu->mutex);
157}
158
Avi Kivityd9e368d2007-06-07 19:18:30 +0300159static void ack_flush(void *_completed)
160{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300161}
162
163void kvm_flush_remote_tlbs(struct kvm *kvm)
164{
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200165 int i, cpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300166 cpumask_t cpus;
167 struct kvm_vcpu *vcpu;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300168
Avi Kivityd9e368d2007-06-07 19:18:30 +0300169 cpus_clear(cpus);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000170 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
171 vcpu = kvm->vcpus[i];
172 if (!vcpu)
173 continue;
Avi Kivity3176bc32007-10-16 17:22:08 +0200174 if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivityd9e368d2007-06-07 19:18:30 +0300175 continue;
176 cpu = vcpu->cpu;
177 if (cpu != -1 && cpu != raw_smp_processor_id())
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200178 cpu_set(cpu, cpus);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300179 }
Laurent Vivier49d3bd72007-10-22 16:33:07 +0200180 smp_call_function_mask(cpus, ack_flush, NULL, 1);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300181}
182
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000183int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
184{
185 struct page *page;
186 int r;
187
188 mutex_init(&vcpu->mutex);
189 vcpu->cpu = -1;
190 vcpu->mmu.root_hpa = INVALID_PAGE;
191 vcpu->kvm = kvm;
192 vcpu->vcpu_id = id;
He, Qingc5ec1532007-09-03 17:07:41 +0300193 if (!irqchip_in_kernel(kvm) || id == 0)
194 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
195 else
196 vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
Eddie Dongb6958ce2007-07-18 12:15:21 +0300197 init_waitqueue_head(&vcpu->wq);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000198
199 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
200 if (!page) {
201 r = -ENOMEM;
202 goto fail;
203 }
204 vcpu->run = page_address(page);
205
206 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
207 if (!page) {
208 r = -ENOMEM;
209 goto fail_free_run;
210 }
211 vcpu->pio_data = page_address(page);
212
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000213 r = kvm_mmu_create(vcpu);
214 if (r < 0)
215 goto fail_free_pio_data;
216
Rusty Russell76fafa52007-10-08 10:50:48 +1000217 if (irqchip_in_kernel(kvm)) {
218 r = kvm_create_lapic(vcpu);
219 if (r < 0)
220 goto fail_mmu_destroy;
221 }
222
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000223 return 0;
224
Rusty Russell76fafa52007-10-08 10:50:48 +1000225fail_mmu_destroy:
226 kvm_mmu_destroy(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000227fail_free_pio_data:
228 free_page((unsigned long)vcpu->pio_data);
229fail_free_run:
230 free_page((unsigned long)vcpu->run);
231fail:
Rusty Russell76fafa52007-10-08 10:50:48 +1000232 return r;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000233}
234EXPORT_SYMBOL_GPL(kvm_vcpu_init);
235
236void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
237{
Rusty Russelld5894442007-10-08 10:48:30 +1000238 kvm_free_lapic(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000239 kvm_mmu_destroy(vcpu);
240 free_page((unsigned long)vcpu->pio_data);
241 free_page((unsigned long)vcpu->run);
242}
243EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
244
Avi Kivityf17abe92007-02-21 19:28:04 +0200245static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246{
247 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248
249 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200250 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251
Eddie Dong74906342007-06-19 18:05:03 +0300252 kvm_io_bus_init(&kvm->pio_bus);
Shaohua Li11ec2802007-07-23 14:51:37 +0800253 mutex_init(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254 INIT_LIST_HEAD(&kvm->active_mmu_pages);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400255 kvm_io_bus_init(&kvm->mmio_bus);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000256 spin_lock(&kvm_lock);
257 list_add(&kvm->vm_list, &vm_list);
258 spin_unlock(&kvm_lock);
Avi Kivityf17abe92007-02-21 19:28:04 +0200259 return kvm;
260}
261
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262/*
263 * Free any memory in @free but not in @dont.
264 */
265static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
266 struct kvm_memory_slot *dont)
267{
Izik Eidus290fc382007-09-27 14:11:22 +0200268 if (!dont || free->rmap != dont->rmap)
269 vfree(free->rmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270
271 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
272 vfree(free->dirty_bitmap);
273
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000275 free->dirty_bitmap = NULL;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500276 free->rmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277}
278
279static void kvm_free_physmem(struct kvm *kvm)
280{
281 int i;
282
283 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000284 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285}
286
Avi Kivity039576c2007-03-20 12:46:50 +0200287static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
288{
289 int i;
290
Rusty Russell3077c4512007-07-30 16:41:57 +1000291 for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
Avi Kivity039576c2007-03-20 12:46:50 +0200292 if (vcpu->pio.guest_pages[i]) {
Izik Eidus8a7ae052007-10-18 11:09:33 +0200293 kvm_release_page(vcpu->pio.guest_pages[i]);
Avi Kivity039576c2007-03-20 12:46:50 +0200294 vcpu->pio.guest_pages[i] = NULL;
295 }
296}
297
Avi Kivity7b53aa52007-06-05 12:17:03 +0300298static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
299{
Avi Kivity7b53aa52007-06-05 12:17:03 +0300300 vcpu_load(vcpu);
301 kvm_mmu_unload(vcpu);
302 vcpu_put(vcpu);
303}
304
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305static void kvm_free_vcpus(struct kvm *kvm)
306{
307 unsigned int i;
308
Avi Kivity7b53aa52007-06-05 12:17:03 +0300309 /*
310 * Unpin any mmu pages first.
311 */
312 for (i = 0; i < KVM_MAX_VCPUS; ++i)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000313 if (kvm->vcpus[i])
314 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
315 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
316 if (kvm->vcpus[i]) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300317 kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000318 kvm->vcpus[i] = NULL;
319 }
320 }
321
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322}
323
Avi Kivityf17abe92007-02-21 19:28:04 +0200324static void kvm_destroy_vm(struct kvm *kvm)
325{
Avi Kivity133de902007-02-12 00:54:44 -0800326 spin_lock(&kvm_lock);
327 list_del(&kvm->vm_list);
328 spin_unlock(&kvm_lock);
Eddie Dong74906342007-06-19 18:05:03 +0300329 kvm_io_bus_destroy(&kvm->pio_bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -0400330 kvm_io_bus_destroy(&kvm->mmio_bus);
Eddie Dong85f455f2007-07-06 12:20:49 +0300331 kfree(kvm->vpic);
Eddie Dong1fd4f2a2007-07-18 12:03:39 +0300332 kfree(kvm->vioapic);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 kvm_free_vcpus(kvm);
334 kvm_free_physmem(kvm);
335 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200336}
337
338static int kvm_vm_release(struct inode *inode, struct file *filp)
339{
340 struct kvm *kvm = filp->private_data;
341
342 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343 return 0;
344}
345
346static void inject_gp(struct kvm_vcpu *vcpu)
347{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300348 kvm_x86_ops->inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349}
350
Avi Kivity1342d352007-01-05 16:36:39 -0800351/*
352 * Load the pae pdptrs. Return true is they are all valid.
353 */
354static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355{
356 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800357 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 int i;
Avi Kivity1342d352007-01-05 16:36:39 -0800359 int ret;
Rusty Russellc820c2a2007-07-25 13:29:51 +1000360 u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361
Shaohua Li11ec2802007-07-23 14:51:37 +0800362 mutex_lock(&vcpu->kvm->lock);
Izik Eidus195aefd2007-10-01 22:14:18 +0200363 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
364 offset * sizeof(u64), sizeof(pdpte));
365 if (ret < 0) {
Rusty Russellc820c2a2007-07-25 13:29:51 +1000366 ret = 0;
367 goto out;
368 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000369 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
370 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
Avi Kivity1342d352007-01-05 16:36:39 -0800371 ret = 0;
372 goto out;
373 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 }
Rusty Russellc820c2a2007-07-25 13:29:51 +1000375 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376
Rusty Russellc820c2a2007-07-25 13:29:51 +1000377 memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
Avi Kivity1342d352007-01-05 16:36:39 -0800378out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800379 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380
Avi Kivity1342d352007-01-05 16:36:39 -0800381 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382}
383
384void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
385{
Rusty Russell707d92f2007-07-17 23:19:08 +1000386 if (cr0 & CR0_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800387 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
388 cr0, vcpu->cr0);
389 inject_gp(vcpu);
390 return;
391 }
392
Rusty Russell707d92f2007-07-17 23:19:08 +1000393 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
395 inject_gp(vcpu);
396 return;
397 }
398
Rusty Russell707d92f2007-07-17 23:19:08 +1000399 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
401 "and a clear PE flag\n");
402 inject_gp(vcpu);
403 return;
404 }
405
Rusty Russell707d92f2007-07-17 23:19:08 +1000406 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800407#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408 if ((vcpu->shadow_efer & EFER_LME)) {
409 int cs_db, cs_l;
410
411 if (!is_pae(vcpu)) {
412 printk(KERN_DEBUG "set_cr0: #GP, start paging "
413 "in long mode while PAE is disabled\n");
414 inject_gp(vcpu);
415 return;
416 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300417 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 if (cs_l) {
419 printk(KERN_DEBUG "set_cr0: #GP, start paging "
420 "in long mode while CS.L == 1\n");
421 inject_gp(vcpu);
422 return;
423
424 }
425 } else
426#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800427 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
429 "reserved bits\n");
430 inject_gp(vcpu);
431 return;
432 }
433
434 }
435
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300436 kvm_x86_ops->set_cr0(vcpu, cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 vcpu->cr0 = cr0;
438
Shaohua Li11ec2802007-07-23 14:51:37 +0800439 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800441 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 return;
443}
444EXPORT_SYMBOL_GPL(set_cr0);
445
446void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
447{
448 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
449}
450EXPORT_SYMBOL_GPL(lmsw);
451
452void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
453{
Rusty Russell66aee912007-07-17 23:34:16 +1000454 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
456 inject_gp(vcpu);
457 return;
458 }
459
Avi Kivitya9058ec2006-12-29 16:49:37 -0800460 if (is_long_mode(vcpu)) {
Rusty Russell66aee912007-07-17 23:34:16 +1000461 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
463 "in long mode\n");
464 inject_gp(vcpu);
465 return;
466 }
Rusty Russell66aee912007-07-17 23:34:16 +1000467 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Avi Kivity1342d352007-01-05 16:36:39 -0800468 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
470 inject_gp(vcpu);
Rusty Russell310bc762007-07-23 17:11:02 +1000471 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 }
473
Rusty Russell66aee912007-07-17 23:34:16 +1000474 if (cr4 & X86_CR4_VMXE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
476 inject_gp(vcpu);
477 return;
478 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300479 kvm_x86_ops->set_cr4(vcpu, cr4);
Rusty Russell81f50e32007-09-06 01:20:38 +1000480 vcpu->cr4 = cr4;
Shaohua Li11ec2802007-07-23 14:51:37 +0800481 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 kvm_mmu_reset_context(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +0800483 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484}
485EXPORT_SYMBOL_GPL(set_cr4);
486
487void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
488{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800489 if (is_long_mode(vcpu)) {
Rusty Russellf802a302007-07-17 23:32:55 +1000490 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
492 inject_gp(vcpu);
493 return;
494 }
495 } else {
Rusty Russellf802a302007-07-17 23:32:55 +1000496 if (is_pae(vcpu)) {
497 if (cr3 & CR3_PAE_RESERVED_BITS) {
498 printk(KERN_DEBUG
499 "set_cr3: #GP, reserved bits\n");
500 inject_gp(vcpu);
501 return;
502 }
503 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
504 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
505 "reserved bits\n");
506 inject_gp(vcpu);
507 return;
508 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 }
Ryan Harper21764862007-09-18 14:05:16 -0500510 /*
511 * We don't check reserved bits in nonpae mode, because
512 * this isn't enforced, and VMware depends on this.
513 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 }
515
Shaohua Li11ec2802007-07-23 14:51:37 +0800516 mutex_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800517 /*
518 * Does the new cr3 value map to physical memory? (Note, we
519 * catch an invalid cr3 even in real-mode, because it would
520 * cause trouble later on when we turn on paging anyway.)
521 *
522 * A real CPU would silently accept an invalid cr3 and would
523 * attempt to use it - with largely undefined (and often hard
524 * to debug) behavior on the guest side.
525 */
526 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
527 inject_gp(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000528 else {
529 vcpu->cr3 = cr3;
Ingo Molnard21225e2007-01-05 16:36:59 -0800530 vcpu->mmu.new_cr3(vcpu);
Rusty Russellfb764412007-07-31 20:45:03 +1000531 }
Shaohua Li11ec2802007-07-23 14:51:37 +0800532 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533}
534EXPORT_SYMBOL_GPL(set_cr3);
535
536void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
537{
Rusty Russell7075bc82007-07-17 23:37:17 +1000538 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
540 inject_gp(vcpu);
541 return;
542 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300543 if (irqchip_in_kernel(vcpu->kvm))
544 kvm_lapic_set_tpr(vcpu, cr8);
545 else
546 vcpu->cr8 = cr8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547}
548EXPORT_SYMBOL_GPL(set_cr8);
549
Eddie Dong7017fc32007-07-18 11:34:57 +0300550unsigned long get_cr8(struct kvm_vcpu *vcpu)
551{
Eddie Dong97222cc2007-09-12 10:58:04 +0300552 if (irqchip_in_kernel(vcpu->kvm))
553 return kvm_lapic_get_cr8(vcpu);
554 else
555 return vcpu->cr8;
Eddie Dong7017fc32007-07-18 11:34:57 +0300556}
557EXPORT_SYMBOL_GPL(get_cr8);
558
559u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
560{
Eddie Dong97222cc2007-09-12 10:58:04 +0300561 if (irqchip_in_kernel(vcpu->kvm))
562 return vcpu->apic_base;
563 else
564 return vcpu->apic_base;
Eddie Dong7017fc32007-07-18 11:34:57 +0300565}
566EXPORT_SYMBOL_GPL(kvm_get_apic_base);
567
568void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
569{
Eddie Dong97222cc2007-09-12 10:58:04 +0300570 /* TODO: reserve bits check */
571 if (irqchip_in_kernel(vcpu->kvm))
572 kvm_lapic_set_base(vcpu, data);
573 else
574 vcpu->apic_base = data;
Eddie Dong7017fc32007-07-18 11:34:57 +0300575}
576EXPORT_SYMBOL_GPL(kvm_set_apic_base);
577
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578void fx_init(struct kvm_vcpu *vcpu)
579{
Rusty Russellb114b082007-07-30 21:13:43 +1000580 unsigned after_mxcsr_mask;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581
Rusty Russell9bd015062007-07-30 16:29:56 +1000582 /* Initialize guest FPU by resetting ours and saving into guest's */
583 preempt_disable();
Rusty Russellb114b082007-07-30 21:13:43 +1000584 fx_save(&vcpu->host_fx_image);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 fpu_init();
Rusty Russellb114b082007-07-30 21:13:43 +1000586 fx_save(&vcpu->guest_fx_image);
587 fx_restore(&vcpu->host_fx_image);
Rusty Russell9bd015062007-07-30 16:29:56 +1000588 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589
Amit Shah380102c2007-08-25 11:35:52 +0300590 vcpu->cr0 |= X86_CR0_ET;
Rusty Russellb114b082007-07-30 21:13:43 +1000591 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
592 vcpu->guest_fx_image.mxcsr = 0x1f80;
593 memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
594 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595}
596EXPORT_SYMBOL_GPL(fx_init);
597
598/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599 * Allocate some memory and give it an address in the guest physical address
600 * space.
601 *
602 * Discontiguous memory is allowed, mostly for framebuffers.
603 */
Izik Eidus210c7c42007-10-24 23:52:57 +0200604int kvm_set_memory_region(struct kvm *kvm,
605 struct kvm_userspace_memory_region *mem,
606 int user_alloc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607{
608 int r;
609 gfn_t base_gfn;
610 unsigned long npages;
611 unsigned long i;
612 struct kvm_memory_slot *memslot;
613 struct kvm_memory_slot old, new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614
615 r = -EINVAL;
616 /* General sanity checks */
617 if (mem->memory_size & (PAGE_SIZE - 1))
618 goto out;
619 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
620 goto out;
Izik Eiduse0d62c72007-10-24 23:57:46 +0200621 if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 goto out;
623 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
624 goto out;
625
626 memslot = &kvm->memslots[mem->slot];
627 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
628 npages = mem->memory_size >> PAGE_SHIFT;
629
630 if (!npages)
631 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
632
Shaohua Li11ec2802007-07-23 14:51:37 +0800633 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 new = old = *memslot;
636
637 new.base_gfn = base_gfn;
638 new.npages = npages;
639 new.flags = mem->flags;
640
641 /* Disallow changing a memory slot's size. */
642 r = -EINVAL;
643 if (npages && old.npages && npages != old.npages)
644 goto out_unlock;
645
646 /* Check for overlaps */
647 r = -EEXIST;
648 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
649 struct kvm_memory_slot *s = &kvm->memslots[i];
650
651 if (s == memslot)
652 continue;
653 if (!((base_gfn + npages <= s->base_gfn) ||
654 (base_gfn >= s->base_gfn + s->npages)))
655 goto out_unlock;
656 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 /* Free page dirty bitmap if unneeded */
659 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000660 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661
662 r = -ENOMEM;
663
664 /* Allocate if a slot is being created */
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500665 if (npages && !new.rmap) {
Mike Dayd77c26f2007-10-08 09:02:08 -0400666 new.rmap = vmalloc(npages * sizeof(struct page *));
Izik Eidus290fc382007-09-27 14:11:22 +0200667
668 if (!new.rmap)
669 goto out_unlock;
670
Izik Eidus290fc382007-09-27 14:11:22 +0200671 memset(new.rmap, 0, npages * sizeof(*new.rmap));
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500672
Izik Eidus80b14b52007-10-25 11:54:04 +0200673 new.user_alloc = user_alloc;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500674 if (user_alloc)
Izik Eidus8a7ae052007-10-18 11:09:33 +0200675 new.userspace_addr = mem->userspace_addr;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500676 else {
677 down_write(&current->mm->mmap_sem);
678 new.userspace_addr = do_mmap(NULL, 0,
679 npages * PAGE_SIZE,
680 PROT_READ | PROT_WRITE,
681 MAP_SHARED | MAP_ANONYMOUS,
682 0);
683 up_write(&current->mm->mmap_sem);
684
685 if (IS_ERR((void *)new.userspace_addr))
686 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 }
Izik Eidus80b14b52007-10-25 11:54:04 +0200688 } else {
689 if (!old.user_alloc && old.rmap) {
690 int ret;
691
692 down_write(&current->mm->mmap_sem);
693 ret = do_munmap(current->mm, old.userspace_addr,
694 old.npages * PAGE_SIZE);
695 up_write(&current->mm->mmap_sem);
696 if (ret < 0)
697 printk(KERN_WARNING
698 "kvm_vm_ioctl_set_memory_region: "
699 "failed to munmap memory\n");
700 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 }
702
703 /* Allocate page dirty bitmap if needed */
704 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
705 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
706
707 new.dirty_bitmap = vmalloc(dirty_bytes);
708 if (!new.dirty_bitmap)
Laurent Vivier0d8d2bd2007-08-30 14:56:21 +0200709 goto out_unlock;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710 memset(new.dirty_bitmap, 0, dirty_bytes);
711 }
712
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 if (mem->slot >= kvm->nmemslots)
714 kvm->nmemslots = mem->slot + 1;
715
Izik Eidus82ce2c92007-10-02 18:52:55 +0200716 if (!kvm->n_requested_mmu_pages) {
717 unsigned int n_pages;
718
719 if (npages) {
720 n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000;
721 kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages +
722 n_pages);
723 } else {
724 unsigned int nr_mmu_pages;
725
726 n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000;
727 nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages;
728 nr_mmu_pages = max(nr_mmu_pages,
729 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
730 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
731 }
732 }
733
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 *memslot = new;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Avi Kivity90cb0522007-07-17 13:04:56 +0300736 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
737 kvm_flush_remote_tlbs(kvm);
738
Shaohua Li11ec2802007-07-23 14:51:37 +0800739 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 kvm_free_physmem_slot(&old, &new);
742 return 0;
743
744out_unlock:
Shaohua Li11ec2802007-07-23 14:51:37 +0800745 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 kvm_free_physmem_slot(&new, &old);
747out:
748 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +0200749
750}
751EXPORT_SYMBOL_GPL(kvm_set_memory_region);
752
Carsten Otte1fe779f2007-10-29 16:08:35 +0100753int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
754 struct
755 kvm_userspace_memory_region *mem,
756 int user_alloc)
Izik Eidus210c7c42007-10-24 23:52:57 +0200757{
Izik Eiduse0d62c72007-10-24 23:57:46 +0200758 if (mem->slot >= KVM_MEMORY_SLOTS)
759 return -EINVAL;
Izik Eidus210c7c42007-10-24 23:52:57 +0200760 return kvm_set_memory_region(kvm, mem, user_alloc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762
763/*
764 * Get (and clear) the dirty memory log for a memory slot.
765 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200766static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
767 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768{
769 struct kvm_memory_slot *memslot;
770 int r, i;
771 int n;
772 unsigned long any = 0;
773
Shaohua Li11ec2802007-07-23 14:51:37 +0800774 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 r = -EINVAL;
777 if (log->slot >= KVM_MEMORY_SLOTS)
778 goto out;
779
780 memslot = &kvm->memslots[log->slot];
781 r = -ENOENT;
782 if (!memslot->dirty_bitmap)
783 goto out;
784
Uri Lublincd1a4a92007-02-22 16:43:09 +0200785 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786
Uri Lublincd1a4a92007-02-22 16:43:09 +0200787 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 any = memslot->dirty_bitmap[i];
789
790 r = -EFAULT;
791 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
792 goto out;
793
Rusty Russell39214912007-07-31 19:57:47 +1000794 /* If nothing is dirty, don't bother messing with page tables. */
795 if (any) {
Rusty Russell39214912007-07-31 19:57:47 +1000796 kvm_mmu_slot_remove_write_access(kvm, log->slot);
797 kvm_flush_remote_tlbs(kvm);
798 memset(memslot->dirty_bitmap, 0, n);
Rusty Russell39214912007-07-31 19:57:47 +1000799 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
801 r = 0;
802
803out:
Shaohua Li11ec2802007-07-23 14:51:37 +0800804 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 return r;
806}
807
Izik Eiduscea7bb22007-10-17 19:17:48 +0200808int is_error_page(struct page *page)
809{
810 return page == bad_page;
811}
812EXPORT_SYMBOL_GPL(is_error_page);
813
Izik Eidus290fc382007-09-27 14:11:22 +0200814gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
Avi Kivitye8207542007-03-30 16:54:30 +0300815{
816 int i;
817 struct kvm_mem_alias *alias;
818
819 for (i = 0; i < kvm->naliases; ++i) {
820 alias = &kvm->aliases[i];
821 if (gfn >= alias->base_gfn
822 && gfn < alias->base_gfn + alias->npages)
823 return alias->target_gfn + gfn - alias->base_gfn;
824 }
825 return gfn;
826}
827
828static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829{
830 int i;
831
832 for (i = 0; i < kvm->nmemslots; ++i) {
833 struct kvm_memory_slot *memslot = &kvm->memslots[i];
834
835 if (gfn >= memslot->base_gfn
836 && gfn < memslot->base_gfn + memslot->npages)
837 return memslot;
838 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000839 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840}
Avi Kivitye8207542007-03-30 16:54:30 +0300841
842struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
843{
844 gfn = unalias_gfn(kvm, gfn);
845 return __gfn_to_memslot(kvm, gfn);
846}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
Izik Eiduse0d62c72007-10-24 23:57:46 +0200848int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
849{
850 int i;
851
852 gfn = unalias_gfn(kvm, gfn);
853 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
854 struct kvm_memory_slot *memslot = &kvm->memslots[i];
855
856 if (gfn >= memslot->base_gfn
857 && gfn < memslot->base_gfn + memslot->npages)
858 return 1;
859 }
860 return 0;
861}
862EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
863
Avi Kivity954bbbc2007-03-30 14:02:32 +0300864struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
865{
866 struct kvm_memory_slot *slot;
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500867 struct page *page[1];
868 int npages;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300869
Avi Kivity60395222007-10-21 11:03:36 +0200870 might_sleep();
871
Avi Kivitye8207542007-03-30 16:54:30 +0300872 gfn = unalias_gfn(kvm, gfn);
873 slot = __gfn_to_memslot(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200874 if (!slot) {
875 get_page(bad_page);
Izik Eiduscea7bb22007-10-17 19:17:48 +0200876 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200877 }
Izik Eidus8a7ae052007-10-18 11:09:33 +0200878
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500879 down_read(&current->mm->mmap_sem);
880 npages = get_user_pages(current, current->mm,
881 slot->userspace_addr
882 + (gfn - slot->base_gfn) * PAGE_SIZE, 1,
883 1, 1, page, NULL);
884 up_read(&current->mm->mmap_sem);
885 if (npages != 1) {
886 get_page(bad_page);
887 return bad_page;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200888 }
Anthony Liguori8d4e1282007-10-18 09:59:34 -0500889
890 return page[0];
Avi Kivity954bbbc2007-03-30 14:02:32 +0300891}
892EXPORT_SYMBOL_GPL(gfn_to_page);
893
Izik Eidus8a7ae052007-10-18 11:09:33 +0200894void kvm_release_page(struct page *page)
895{
896 if (!PageReserved(page))
897 SetPageDirty(page);
898 put_page(page);
899}
900EXPORT_SYMBOL_GPL(kvm_release_page);
901
Izik Eidus195aefd2007-10-01 22:14:18 +0200902static int next_segment(unsigned long len, int offset)
903{
904 if (len > PAGE_SIZE - offset)
905 return PAGE_SIZE - offset;
906 else
907 return len;
908}
909
910int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
911 int len)
912{
913 void *page_virt;
914 struct page *page;
915
916 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200917 if (is_error_page(page)) {
918 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200919 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200920 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200921 page_virt = kmap_atomic(page, KM_USER0);
922
923 memcpy(data, page_virt + offset, len);
924
925 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200926 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200927 return 0;
928}
929EXPORT_SYMBOL_GPL(kvm_read_guest_page);
930
931int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
932{
933 gfn_t gfn = gpa >> PAGE_SHIFT;
934 int seg;
935 int offset = offset_in_page(gpa);
936 int ret;
937
938 while ((seg = next_segment(len, offset)) != 0) {
939 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
940 if (ret < 0)
941 return ret;
942 offset = 0;
943 len -= seg;
944 data += seg;
945 ++gfn;
946 }
947 return 0;
948}
949EXPORT_SYMBOL_GPL(kvm_read_guest);
950
951int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
952 int offset, int len)
953{
954 void *page_virt;
955 struct page *page;
956
957 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200958 if (is_error_page(page)) {
959 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200960 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200961 }
Izik Eidus195aefd2007-10-01 22:14:18 +0200962 page_virt = kmap_atomic(page, KM_USER0);
963
964 memcpy(page_virt + offset, data, len);
965
966 kunmap_atomic(page_virt, KM_USER0);
967 mark_page_dirty(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200968 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +0200969 return 0;
970}
971EXPORT_SYMBOL_GPL(kvm_write_guest_page);
972
973int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
974 unsigned long len)
975{
976 gfn_t gfn = gpa >> PAGE_SHIFT;
977 int seg;
978 int offset = offset_in_page(gpa);
979 int ret;
980
981 while ((seg = next_segment(len, offset)) != 0) {
982 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
983 if (ret < 0)
984 return ret;
985 offset = 0;
986 len -= seg;
987 data += seg;
988 ++gfn;
989 }
990 return 0;
991}
992
993int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
994{
995 void *page_virt;
996 struct page *page;
997
998 page = gfn_to_page(kvm, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200999 if (is_error_page(page)) {
1000 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001001 return -EFAULT;
Izik Eidus8a7ae052007-10-18 11:09:33 +02001002 }
Izik Eidus195aefd2007-10-01 22:14:18 +02001003 page_virt = kmap_atomic(page, KM_USER0);
1004
1005 memset(page_virt + offset, 0, len);
1006
1007 kunmap_atomic(page_virt, KM_USER0);
Izik Eidus8a7ae052007-10-18 11:09:33 +02001008 kvm_release_page(page);
Izik Eidus195aefd2007-10-01 22:14:18 +02001009 return 0;
1010}
1011EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
1012
1013int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
1014{
1015 gfn_t gfn = gpa >> PAGE_SHIFT;
1016 int seg;
1017 int offset = offset_in_page(gpa);
1018 int ret;
1019
1020 while ((seg = next_segment(len, offset)) != 0) {
1021 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
1022 if (ret < 0)
1023 return ret;
1024 offset = 0;
1025 len -= seg;
1026 ++gfn;
1027 }
1028 return 0;
1029}
1030EXPORT_SYMBOL_GPL(kvm_clear_guest);
1031
Rusty Russell7e9d6192007-07-31 20:41:14 +10001032/* WARNING: Does not work on aliased pages. */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1034{
Nguyen Anh Quynh31389942007-06-05 10:35:19 +03001035 struct kvm_memory_slot *memslot;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036
Rusty Russell7e9d6192007-07-31 20:41:14 +10001037 memslot = __gfn_to_memslot(kvm, gfn);
1038 if (memslot && memslot->dirty_bitmap) {
1039 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040
Rusty Russell7e9d6192007-07-31 20:41:14 +10001041 /* avoid RMW */
1042 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1043 set_bit(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 }
1045}
1046
Laurent Viviere7d5d762007-07-30 13:41:19 +03001047int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001048 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001050 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 void *data = val;
1053
1054 while (bytes) {
1055 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1056 unsigned offset = addr & (PAGE_SIZE-1);
1057 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
Izik Eidus195aefd2007-10-01 22:14:18 +02001058 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059
1060 if (gpa == UNMAPPED_GVA)
1061 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001062 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1063 if (ret < 0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065
1066 bytes -= tocopy;
1067 data += tocopy;
1068 addr += tocopy;
1069 }
1070
1071 return X86EMUL_CONTINUE;
1072}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001073EXPORT_SYMBOL_GPL(emulator_read_std);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074
1075static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001076 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001078 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079{
Rusty Russellf0242472007-08-01 10:48:02 +10001080 pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081 return X86EMUL_UNHANDLEABLE;
1082}
1083
Eddie Dong97222cc2007-09-12 10:58:04 +03001084/*
1085 * Only apic need an MMIO device hook, so shortcut now..
1086 */
1087static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1088 gpa_t addr)
1089{
1090 struct kvm_io_device *dev;
1091
1092 if (vcpu->apic) {
1093 dev = &vcpu->apic->dev;
1094 if (dev->in_range(dev, addr))
1095 return dev;
1096 }
1097 return NULL;
1098}
1099
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001100static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1101 gpa_t addr)
1102{
Eddie Dong97222cc2007-09-12 10:58:04 +03001103 struct kvm_io_device *dev;
1104
1105 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1106 if (dev == NULL)
1107 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1108 return dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001109}
1110
Eddie Dong74906342007-06-19 18:05:03 +03001111static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1112 gpa_t addr)
1113{
1114 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1115}
1116
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001118 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001120 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001122 struct kvm_io_device *mmio_dev;
1123 gpa_t gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124
1125 if (vcpu->mmio_read_completed) {
1126 memcpy(val, vcpu->mmio_data, bytes);
1127 vcpu->mmio_read_completed = 0;
1128 return X86EMUL_CONTINUE;
Laurent Viviercebff022007-07-30 13:35:24 +03001129 } else if (emulator_read_std(addr, val, bytes, vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 == X86EMUL_CONTINUE)
1131 return X86EMUL_CONTINUE;
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001132
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001133 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1134 if (gpa == UNMAPPED_GVA)
1135 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001137 /*
1138 * Is this MMIO handled locally?
1139 */
1140 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1141 if (mmio_dev) {
1142 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1143 return X86EMUL_CONTINUE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 }
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001145
1146 vcpu->mmio_needed = 1;
1147 vcpu->mmio_phys_addr = gpa;
1148 vcpu->mmio_size = bytes;
1149 vcpu->mmio_is_write = 0;
1150
1151 return X86EMUL_UNHANDLEABLE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152}
1153
Avi Kivityda4a00f2007-01-05 16:36:44 -08001154static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001155 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001156{
Izik Eidus195aefd2007-10-01 22:14:18 +02001157 int ret;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001158
Izik Eidus195aefd2007-10-01 22:14:18 +02001159 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1160 if (ret < 0)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001161 return 0;
Shaohua Life551882007-07-23 14:51:39 +08001162 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001163 return 1;
1164}
1165
Avi Kivityb0fcd902007-07-22 18:48:54 +03001166static int emulator_write_emulated_onepage(unsigned long addr,
1167 const void *val,
1168 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001169 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170{
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001171 struct kvm_io_device *mmio_dev;
1172 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173
Avi Kivityc9047f52007-04-17 10:53:22 +03001174 if (gpa == UNMAPPED_GVA) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001175 kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001177 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178
Avi Kivityda4a00f2007-01-05 16:36:44 -08001179 if (emulator_write_phys(vcpu, gpa, val, bytes))
1180 return X86EMUL_CONTINUE;
1181
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04001182 /*
1183 * Is this MMIO handled locally?
1184 */
1185 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1186 if (mmio_dev) {
1187 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1188 return X86EMUL_CONTINUE;
1189 }
1190
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 vcpu->mmio_needed = 1;
1192 vcpu->mmio_phys_addr = gpa;
1193 vcpu->mmio_size = bytes;
1194 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001195 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196
1197 return X86EMUL_CONTINUE;
1198}
1199
Laurent Viviere7d5d762007-07-30 13:41:19 +03001200int emulator_write_emulated(unsigned long addr,
Avi Kivityb0fcd902007-07-22 18:48:54 +03001201 const void *val,
1202 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001203 struct kvm_vcpu *vcpu)
Avi Kivityb0fcd902007-07-22 18:48:54 +03001204{
1205 /* Crossing a page boundary? */
1206 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1207 int rc, now;
1208
1209 now = -addr & ~PAGE_MASK;
Laurent Viviercebff022007-07-30 13:35:24 +03001210 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001211 if (rc != X86EMUL_CONTINUE)
1212 return rc;
1213 addr += now;
1214 val += now;
1215 bytes -= now;
1216 }
Laurent Viviercebff022007-07-30 13:35:24 +03001217 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001218}
Laurent Viviere7d5d762007-07-30 13:41:19 +03001219EXPORT_SYMBOL_GPL(emulator_write_emulated);
Avi Kivityb0fcd902007-07-22 18:48:54 +03001220
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001222 const void *old,
1223 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 unsigned int bytes,
Laurent Viviercebff022007-07-30 13:35:24 +03001225 struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226{
1227 static int reported;
1228
1229 if (!reported) {
1230 reported = 1;
1231 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1232 }
Laurent Viviercebff022007-07-30 13:35:24 +03001233 return emulator_write_emulated(addr, new, bytes, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
1236static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1237{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001238 return kvm_x86_ops->get_segment_base(vcpu, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239}
1240
1241int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1242{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 return X86EMUL_CONTINUE;
1244}
1245
1246int emulate_clts(struct kvm_vcpu *vcpu)
1247{
Amit Shah404fb882007-11-19 17:57:35 +02001248 kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 return X86EMUL_CONTINUE;
1250}
1251
Mike Dayd77c26f2007-10-08 09:02:08 -04001252int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253{
1254 struct kvm_vcpu *vcpu = ctxt->vcpu;
1255
1256 switch (dr) {
1257 case 0 ... 3:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001258 *dest = kvm_x86_ops->get_dr(vcpu, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 return X86EMUL_CONTINUE;
1260 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001261 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 return X86EMUL_UNHANDLEABLE;
1263 }
1264}
1265
1266int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1267{
1268 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1269 int exception;
1270
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001271 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 if (exception) {
1273 /* FIXME: better handling */
1274 return X86EMUL_UNHANDLEABLE;
1275 }
1276 return X86EMUL_CONTINUE;
1277}
1278
Avi Kivity054b1362007-09-12 13:21:09 +03001279void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
1281 static int reported;
1282 u8 opcodes[4];
Avi Kivity054b1362007-09-12 13:21:09 +03001283 unsigned long rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 unsigned long rip_linear;
1285
Avi Kivity054b1362007-09-12 13:21:09 +03001286 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287
1288 if (reported)
1289 return;
1290
Avi Kivity054b1362007-09-12 13:21:09 +03001291 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292
Avi Kivity054b1362007-09-12 13:21:09 +03001293 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1294 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 reported = 1;
1296}
Avi Kivity054b1362007-09-12 13:21:09 +03001297EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298
1299struct x86_emulate_ops emulate_ops = {
1300 .read_std = emulator_read_std,
1301 .write_std = emulator_write_std,
1302 .read_emulated = emulator_read_emulated,
1303 .write_emulated = emulator_write_emulated,
1304 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1305};
1306
1307int emulate_instruction(struct kvm_vcpu *vcpu,
1308 struct kvm_run *run,
1309 unsigned long cr2,
Laurent Vivier34273182007-09-18 11:27:37 +02001310 u16 error_code,
1311 int no_decode)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312{
Laurent Viviera22436b2007-09-24 17:00:58 +02001313 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314
Avi Kivitye7df56e2007-03-14 15:54:54 +02001315 vcpu->mmio_fault_cr2 = cr2;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001316 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 vcpu->mmio_is_write = 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03001319 vcpu->pio.string = 0;
Laurent Vivier34273182007-09-18 11:27:37 +02001320
1321 if (!no_decode) {
1322 int cs_db, cs_l;
1323 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1324
1325 vcpu->emulate_ctxt.vcpu = vcpu;
1326 vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1327 vcpu->emulate_ctxt.cr2 = cr2;
1328 vcpu->emulate_ctxt.mode =
1329 (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
1330 ? X86EMUL_MODE_REAL : cs_l
1331 ? X86EMUL_MODE_PROT64 : cs_db
1332 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1333
1334 if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1335 vcpu->emulate_ctxt.cs_base = 0;
1336 vcpu->emulate_ctxt.ds_base = 0;
1337 vcpu->emulate_ctxt.es_base = 0;
1338 vcpu->emulate_ctxt.ss_base = 0;
1339 } else {
1340 vcpu->emulate_ctxt.cs_base =
1341 get_segment_base(vcpu, VCPU_SREG_CS);
1342 vcpu->emulate_ctxt.ds_base =
1343 get_segment_base(vcpu, VCPU_SREG_DS);
1344 vcpu->emulate_ctxt.es_base =
1345 get_segment_base(vcpu, VCPU_SREG_ES);
1346 vcpu->emulate_ctxt.ss_base =
1347 get_segment_base(vcpu, VCPU_SREG_SS);
1348 }
1349
1350 vcpu->emulate_ctxt.gs_base =
1351 get_segment_base(vcpu, VCPU_SREG_GS);
1352 vcpu->emulate_ctxt.fs_base =
1353 get_segment_base(vcpu, VCPU_SREG_FS);
1354
1355 r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Viviera22436b2007-09-24 17:00:58 +02001356 if (r) {
1357 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1358 return EMULATE_DONE;
1359 return EMULATE_FAIL;
1360 }
Laurent Vivier34273182007-09-18 11:27:37 +02001361 }
1362
Laurent Viviera22436b2007-09-24 17:00:58 +02001363 r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
Laurent Vivier1be3aa42007-09-18 11:27:27 +02001364
Laurent Viviere70669a2007-08-05 10:36:40 +03001365 if (vcpu->pio.string)
1366 return EMULATE_DO_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
1368 if ((r || vcpu->mmio_is_write) && run) {
Jeff Dike8fc0d082007-07-17 12:26:59 -04001369 run->exit_reason = KVM_EXIT_MMIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1371 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1372 run->mmio.len = vcpu->mmio_size;
1373 run->mmio.is_write = vcpu->mmio_is_write;
1374 }
1375
1376 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001377 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1378 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 if (!vcpu->mmio_needed) {
Avi Kivity054b1362007-09-12 13:21:09 +03001380 kvm_report_emulation_failure(vcpu, "mmio");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 return EMULATE_FAIL;
1382 }
1383 return EMULATE_DO_MMIO;
1384 }
1385
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001386 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier34273182007-09-18 11:27:37 +02001387 kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Avi Kivity02c83202007-04-29 15:02:17 +03001389 if (vcpu->mmio_is_write) {
1390 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001392 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
1394 return EMULATE_DONE;
1395}
1396EXPORT_SYMBOL_GPL(emulate_instruction);
1397
Eddie Dongb6958ce2007-07-18 12:15:21 +03001398/*
1399 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
1400 */
He, Qingc5ec1532007-09-03 17:07:41 +03001401static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03001402{
1403 DECLARE_WAITQUEUE(wait, current);
1404
1405 add_wait_queue(&vcpu->wq, &wait);
1406
1407 /*
1408 * We will block until either an interrupt or a signal wakes us up
1409 */
He, Qingc5ec1532007-09-03 17:07:41 +03001410 while (!kvm_cpu_has_interrupt(vcpu)
1411 && !signal_pending(current)
1412 && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
1413 && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
Eddie Dongb6958ce2007-07-18 12:15:21 +03001414 set_current_state(TASK_INTERRUPTIBLE);
1415 vcpu_put(vcpu);
1416 schedule();
1417 vcpu_load(vcpu);
1418 }
1419
He, Qingc5ec1532007-09-03 17:07:41 +03001420 __set_current_state(TASK_RUNNING);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001421 remove_wait_queue(&vcpu->wq, &wait);
Eddie Dongb6958ce2007-07-18 12:15:21 +03001422}
1423
Avi Kivityd3bef152007-06-05 15:53:05 +03001424int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1425{
Avi Kivityd3bef152007-06-05 15:53:05 +03001426 ++vcpu->stat.halt_exits;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001427 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc5ec1532007-09-03 17:07:41 +03001428 vcpu->mp_state = VCPU_MP_STATE_HALTED;
1429 kvm_vcpu_block(vcpu);
1430 if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
1431 return -EINTR;
Eddie Dongb6958ce2007-07-18 12:15:21 +03001432 return 1;
1433 } else {
1434 vcpu->run->exit_reason = KVM_EXIT_HLT;
1435 return 0;
1436 }
Avi Kivityd3bef152007-06-05 15:53:05 +03001437}
1438EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1439
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001440int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
Avi Kivity270fd9b2007-02-19 14:37:47 +02001441{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001442 unsigned long nr, a0, a1, a2, a3, ret;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001443
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001444 kvm_x86_ops->cache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001445
1446 nr = vcpu->regs[VCPU_REGS_RAX];
1447 a0 = vcpu->regs[VCPU_REGS_RBX];
1448 a1 = vcpu->regs[VCPU_REGS_RCX];
1449 a2 = vcpu->regs[VCPU_REGS_RDX];
1450 a3 = vcpu->regs[VCPU_REGS_RSI];
1451
1452 if (!is_long_mode(vcpu)) {
1453 nr &= 0xFFFFFFFF;
1454 a0 &= 0xFFFFFFFF;
1455 a1 &= 0xFFFFFFFF;
1456 a2 &= 0xFFFFFFFF;
1457 a3 &= 0xFFFFFFFF;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001458 }
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001459
Avi Kivity270fd9b2007-02-19 14:37:47 +02001460 switch (nr) {
1461 default:
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001462 ret = -KVM_ENOSYS;
1463 break;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001464 }
1465 vcpu->regs[VCPU_REGS_RAX] = ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001466 kvm_x86_ops->decache_regs(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001467 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001468}
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001469EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
1470
1471int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
1472{
1473 char instruction[3];
1474 int ret = 0;
1475
1476 mutex_lock(&vcpu->kvm->lock);
1477
1478 /*
1479 * Blow out the MMU to ensure that no other VCPU has an active mapping
1480 * to ensure that the updated hypercall appears atomically across all
1481 * VCPUs.
1482 */
1483 kvm_mmu_zap_all(vcpu->kvm);
1484
1485 kvm_x86_ops->cache_regs(vcpu);
1486 kvm_x86_ops->patch_hypercall(vcpu, instruction);
1487 if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
1488 != X86EMUL_CONTINUE)
1489 ret = -EFAULT;
1490
1491 mutex_unlock(&vcpu->kvm->lock);
1492
1493 return ret;
1494}
Avi Kivity270fd9b2007-02-19 14:37:47 +02001495
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1497{
1498 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1499}
1500
1501void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1502{
1503 struct descriptor_table dt = { limit, base };
1504
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001505 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506}
1507
1508void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1509{
1510 struct descriptor_table dt = { limit, base };
1511
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001512 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513}
1514
1515void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1516 unsigned long *rflags)
1517{
1518 lmsw(vcpu, msw);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001519 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520}
1521
1522unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1523{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001524 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 switch (cr) {
1526 case 0:
1527 return vcpu->cr0;
1528 case 2:
1529 return vcpu->cr2;
1530 case 3:
1531 return vcpu->cr3;
1532 case 4:
1533 return vcpu->cr4;
1534 default:
1535 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1536 return 0;
1537 }
1538}
1539
1540void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1541 unsigned long *rflags)
1542{
1543 switch (cr) {
1544 case 0:
1545 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001546 *rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547 break;
1548 case 2:
1549 vcpu->cr2 = val;
1550 break;
1551 case 3:
1552 set_cr3(vcpu, val);
1553 break;
1554 case 4:
1555 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1556 break;
1557 default:
1558 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1559 }
1560}
1561
Avi Kivity3bab1f52006-12-29 16:49:48 -08001562int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1563{
1564 u64 data;
1565
1566 switch (msr) {
1567 case 0xc0010010: /* SYSCFG */
1568 case 0xc0010015: /* HWCR */
1569 case MSR_IA32_PLATFORM_ID:
1570 case MSR_IA32_P5_MC_ADDR:
1571 case MSR_IA32_P5_MC_TYPE:
1572 case MSR_IA32_MC0_CTL:
1573 case MSR_IA32_MCG_STATUS:
1574 case MSR_IA32_MCG_CAP:
1575 case MSR_IA32_MC0_MISC:
1576 case MSR_IA32_MC0_MISC+4:
1577 case MSR_IA32_MC0_MISC+8:
1578 case MSR_IA32_MC0_MISC+12:
1579 case MSR_IA32_MC0_MISC+16:
1580 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001581 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001582 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001583 /* MTRR registers */
1584 case 0xfe:
1585 case 0x200 ... 0x2ff:
1586 data = 0;
1587 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001588 case 0xcd: /* fsb frequency */
1589 data = 3;
1590 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001591 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001592 data = kvm_get_apic_base(vcpu);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001593 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001594 case MSR_IA32_MISC_ENABLE:
1595 data = vcpu->ia32_misc_enable_msr;
1596 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001597#ifdef CONFIG_X86_64
1598 case MSR_EFER:
1599 data = vcpu->shadow_efer;
1600 break;
1601#endif
1602 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001603 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001604 return 1;
1605 }
1606 *pdata = data;
1607 return 0;
1608}
1609EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1610
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611/*
1612 * Reads an msr value (of 'msr_index') into 'pdata'.
1613 * Returns 0 on success, non-0 otherwise.
1614 * Assumes vcpu_load() was already called.
1615 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001616int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001618 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619}
1620
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001621#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622
Avi Kivity3bab1f52006-12-29 16:49:48 -08001623static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 if (efer & EFER_RESERVED_BITS) {
1626 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1627 efer);
1628 inject_gp(vcpu);
1629 return;
1630 }
1631
1632 if (is_paging(vcpu)
1633 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1634 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1635 inject_gp(vcpu);
1636 return;
1637 }
1638
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001639 kvm_x86_ops->set_efer(vcpu, efer);
Avi Kivity7725f0b2006-12-13 00:34:01 -08001640
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641 efer &= ~EFER_LMA;
1642 efer |= vcpu->shadow_efer & EFER_LMA;
1643
1644 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646
1647#endif
1648
Avi Kivity3bab1f52006-12-29 16:49:48 -08001649int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1650{
1651 switch (msr) {
1652#ifdef CONFIG_X86_64
1653 case MSR_EFER:
1654 set_efer(vcpu, data);
1655 break;
1656#endif
1657 case MSR_IA32_MC0_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001658 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Avi Kivity3bab1f52006-12-29 16:49:48 -08001659 __FUNCTION__, data);
1660 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001661 case MSR_IA32_MCG_STATUS:
Rusty Russellf0242472007-08-01 10:48:02 +10001662 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001663 __FUNCTION__, data);
1664 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001665 case MSR_IA32_UCODE_REV:
1666 case MSR_IA32_UCODE_WRITE:
1667 case 0x200 ... 0x2ff: /* MTRRs */
1668 break;
1669 case MSR_IA32_APICBASE:
Eddie Dong7017fc32007-07-18 11:34:57 +03001670 kvm_set_apic_base(vcpu, data);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001671 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001672 case MSR_IA32_MISC_ENABLE:
1673 vcpu->ia32_misc_enable_msr = data;
1674 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001675 default:
Rusty Russellf0242472007-08-01 10:48:02 +10001676 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001677 return 1;
1678 }
1679 return 0;
1680}
1681EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1682
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683/*
1684 * Writes msr value into into the appropriate "register".
1685 * Returns 0 on success, non-0 otherwise.
1686 * Assumes vcpu_load() was already called.
1687 */
Avi Kivity35f3f282007-07-17 14:20:30 +03001688int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001690 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691}
1692
1693void kvm_resched(struct kvm_vcpu *vcpu)
1694{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001695 if (!need_resched())
1696 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697 cond_resched();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698}
1699EXPORT_SYMBOL_GPL(kvm_resched);
1700
Avi Kivity06465c52007-02-28 20:46:53 +02001701void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1702{
1703 int i;
1704 u32 function;
1705 struct kvm_cpuid_entry *e, *best;
1706
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001707 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001708 function = vcpu->regs[VCPU_REGS_RAX];
1709 vcpu->regs[VCPU_REGS_RAX] = 0;
1710 vcpu->regs[VCPU_REGS_RBX] = 0;
1711 vcpu->regs[VCPU_REGS_RCX] = 0;
1712 vcpu->regs[VCPU_REGS_RDX] = 0;
1713 best = NULL;
1714 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1715 e = &vcpu->cpuid_entries[i];
1716 if (e->function == function) {
1717 best = e;
1718 break;
1719 }
1720 /*
1721 * Both basic or both extended?
1722 */
1723 if (((e->function ^ function) & 0x80000000) == 0)
1724 if (!best || e->function > best->function)
1725 best = e;
1726 }
1727 if (best) {
1728 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1729 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1730 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1731 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1732 }
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001733 kvm_x86_ops->decache_regs(vcpu);
1734 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001735}
1736EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1737
Avi Kivity039576c2007-03-20 12:46:50 +02001738static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001739{
Avi Kivity039576c2007-03-20 12:46:50 +02001740 void *p = vcpu->pio_data;
1741 void *q;
1742 unsigned bytes;
1743 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1744
Avi Kivity039576c2007-03-20 12:46:50 +02001745 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1746 PAGE_KERNEL);
1747 if (!q) {
Avi Kivity039576c2007-03-20 12:46:50 +02001748 free_pio_guest_pages(vcpu);
1749 return -ENOMEM;
1750 }
1751 q += vcpu->pio.guest_page_offset;
1752 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1753 if (vcpu->pio.in)
1754 memcpy(q, p, bytes);
1755 else
1756 memcpy(p, q, bytes);
1757 q -= vcpu->pio.guest_page_offset;
1758 vunmap(q);
Avi Kivity039576c2007-03-20 12:46:50 +02001759 free_pio_guest_pages(vcpu);
1760 return 0;
1761}
1762
1763static int complete_pio(struct kvm_vcpu *vcpu)
1764{
1765 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001766 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001767 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001768
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001769 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001770
1771 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001772 if (io->in)
1773 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001774 io->size);
1775 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001776 if (io->in) {
1777 r = pio_copy_data(vcpu);
1778 if (r) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001779 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001780 return r;
1781 }
1782 }
1783
Avi Kivity46fc1472007-02-22 19:39:30 +02001784 delta = 1;
1785 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001786 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001787 /*
1788 * The size of the register should really depend on
1789 * current address size.
1790 */
1791 vcpu->regs[VCPU_REGS_RCX] -= delta;
1792 }
Avi Kivity039576c2007-03-20 12:46:50 +02001793 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001794 delta = -delta;
1795 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001796 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001797 vcpu->regs[VCPU_REGS_RDI] += delta;
1798 else
1799 vcpu->regs[VCPU_REGS_RSI] += delta;
1800 }
1801
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001802 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity46fc1472007-02-22 19:39:30 +02001803
Avi Kivity039576c2007-03-20 12:46:50 +02001804 io->count -= io->cur_count;
1805 io->cur_count = 0;
1806
Avi Kivity039576c2007-03-20 12:46:50 +02001807 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001808}
1809
Eddie Dong65619eb2007-07-17 11:52:33 +03001810static void kernel_pio(struct kvm_io_device *pio_dev,
1811 struct kvm_vcpu *vcpu,
1812 void *pd)
Eddie Dong74906342007-06-19 18:05:03 +03001813{
1814 /* TODO: String I/O for in kernel device */
1815
Eddie Dong9cf98822007-07-22 10:36:31 +03001816 mutex_lock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001817 if (vcpu->pio.in)
1818 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1819 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001820 pd);
Eddie Dong74906342007-06-19 18:05:03 +03001821 else
1822 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1823 vcpu->pio.size,
Eddie Dong65619eb2007-07-17 11:52:33 +03001824 pd);
Eddie Dong9cf98822007-07-22 10:36:31 +03001825 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001826}
1827
1828static void pio_string_write(struct kvm_io_device *pio_dev,
1829 struct kvm_vcpu *vcpu)
1830{
1831 struct kvm_pio_request *io = &vcpu->pio;
1832 void *pd = vcpu->pio_data;
1833 int i;
1834
Eddie Dong9cf98822007-07-22 10:36:31 +03001835 mutex_lock(&vcpu->kvm->lock);
Eddie Dong65619eb2007-07-17 11:52:33 +03001836 for (i = 0; i < io->cur_count; i++) {
1837 kvm_iodevice_write(pio_dev, io->port,
1838 io->size,
1839 pd);
1840 pd += io->size;
1841 }
Eddie Dong9cf98822007-07-22 10:36:31 +03001842 mutex_unlock(&vcpu->kvm->lock);
Eddie Dong74906342007-06-19 18:05:03 +03001843}
1844
Mike Dayd77c26f2007-10-08 09:02:08 -04001845int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
Laurent Vivier3090dd72007-08-05 10:43:32 +03001846 int size, unsigned port)
1847{
1848 struct kvm_io_device *pio_dev;
1849
1850 vcpu->run->exit_reason = KVM_EXIT_IO;
1851 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1852 vcpu->run->io.size = vcpu->pio.size = size;
1853 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1854 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
1855 vcpu->run->io.port = vcpu->pio.port = port;
1856 vcpu->pio.in = in;
1857 vcpu->pio.string = 0;
1858 vcpu->pio.down = 0;
1859 vcpu->pio.guest_page_offset = 0;
1860 vcpu->pio.rep = 0;
1861
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001862 kvm_x86_ops->cache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001863 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001864 kvm_x86_ops->decache_regs(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001865
Avi Kivity0967b7b2007-09-15 17:34:36 +03001866 kvm_x86_ops->skip_emulated_instruction(vcpu);
1867
Laurent Vivier3090dd72007-08-05 10:43:32 +03001868 pio_dev = vcpu_find_pio_dev(vcpu, port);
1869 if (pio_dev) {
1870 kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1871 complete_pio(vcpu);
1872 return 1;
1873 }
1874 return 0;
1875}
1876EXPORT_SYMBOL_GPL(kvm_emulate_pio);
1877
1878int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1879 int size, unsigned long count, int down,
Avi Kivity039576c2007-03-20 12:46:50 +02001880 gva_t address, int rep, unsigned port)
1881{
1882 unsigned now, in_page;
Eddie Dong65619eb2007-07-17 11:52:33 +03001883 int i, ret = 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001884 int nr_pages = 1;
1885 struct page *page;
Eddie Dong74906342007-06-19 18:05:03 +03001886 struct kvm_io_device *pio_dev;
Avi Kivity039576c2007-03-20 12:46:50 +02001887
1888 vcpu->run->exit_reason = KVM_EXIT_IO;
1889 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001890 vcpu->run->io.size = vcpu->pio.size = size;
Avi Kivity039576c2007-03-20 12:46:50 +02001891 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001892 vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
1893 vcpu->run->io.port = vcpu->pio.port = port;
Avi Kivity039576c2007-03-20 12:46:50 +02001894 vcpu->pio.in = in;
Laurent Vivier3090dd72007-08-05 10:43:32 +03001895 vcpu->pio.string = 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001896 vcpu->pio.down = down;
1897 vcpu->pio.guest_page_offset = offset_in_page(address);
1898 vcpu->pio.rep = rep;
1899
Avi Kivity039576c2007-03-20 12:46:50 +02001900 if (!count) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001901 kvm_x86_ops->skip_emulated_instruction(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02001902 return 1;
1903 }
1904
Avi Kivity039576c2007-03-20 12:46:50 +02001905 if (!down)
1906 in_page = PAGE_SIZE - offset_in_page(address);
1907 else
1908 in_page = offset_in_page(address) + size;
1909 now = min(count, (unsigned long)in_page / size);
1910 if (!now) {
1911 /*
1912 * String I/O straddles page boundary. Pin two guest pages
1913 * so that we satisfy atomicity constraints. Do just one
1914 * transaction to avoid complexity.
1915 */
1916 nr_pages = 2;
1917 now = 1;
1918 }
1919 if (down) {
1920 /*
1921 * String I/O in reverse. Yuck. Kill the guest, fix later.
1922 */
Rusty Russellf0242472007-08-01 10:48:02 +10001923 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivity039576c2007-03-20 12:46:50 +02001924 inject_gp(vcpu);
1925 return 1;
1926 }
1927 vcpu->run->io.count = now;
1928 vcpu->pio.cur_count = now;
1929
Avi Kivity0967b7b2007-09-15 17:34:36 +03001930 if (vcpu->pio.cur_count == vcpu->pio.count)
1931 kvm_x86_ops->skip_emulated_instruction(vcpu);
1932
Avi Kivity039576c2007-03-20 12:46:50 +02001933 for (i = 0; i < nr_pages; ++i) {
Shaohua Li11ec2802007-07-23 14:51:37 +08001934 mutex_lock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001935 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Avi Kivity039576c2007-03-20 12:46:50 +02001936 vcpu->pio.guest_pages[i] = page;
Shaohua Li11ec2802007-07-23 14:51:37 +08001937 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity039576c2007-03-20 12:46:50 +02001938 if (!page) {
1939 inject_gp(vcpu);
1940 free_pio_guest_pages(vcpu);
1941 return 1;
1942 }
1943 }
1944
Laurent Vivier3090dd72007-08-05 10:43:32 +03001945 pio_dev = vcpu_find_pio_dev(vcpu, port);
Eddie Dong65619eb2007-07-17 11:52:33 +03001946 if (!vcpu->pio.in) {
1947 /* string PIO write */
1948 ret = pio_copy_data(vcpu);
1949 if (ret >= 0 && pio_dev) {
1950 pio_string_write(pio_dev, vcpu);
1951 complete_pio(vcpu);
1952 if (vcpu->pio.count == 0)
1953 ret = 1;
1954 }
1955 } else if (pio_dev)
Rusty Russellf0242472007-08-01 10:48:02 +10001956 pr_unimpl(vcpu, "no string pio read support yet, "
Eddie Dong65619eb2007-07-17 11:52:33 +03001957 "port %x size %d count %ld\n",
1958 port, size, count);
1959
1960 return ret;
Avi Kivity039576c2007-03-20 12:46:50 +02001961}
Laurent Vivier3090dd72007-08-05 10:43:32 +03001962EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
Avi Kivity039576c2007-03-20 12:46:50 +02001963
Avi Kivity04d2cc72007-09-10 18:10:54 +03001964/*
1965 * Check if userspace requested an interrupt window, and that the
1966 * interrupt window is open.
1967 *
1968 * No need to exit to userspace if we already have an interrupt queued.
1969 */
1970static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1971 struct kvm_run *kvm_run)
1972{
1973 return (!vcpu->irq_summary &&
1974 kvm_run->request_interrupt_window &&
1975 vcpu->interrupt_window_open &&
1976 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
1977}
1978
1979static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1980 struct kvm_run *kvm_run)
1981{
1982 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
1983 kvm_run->cr8 = get_cr8(vcpu);
1984 kvm_run->apic_base = kvm_get_apic_base(vcpu);
1985 if (irqchip_in_kernel(vcpu->kvm))
1986 kvm_run->ready_for_interrupt_injection = 1;
1987 else
1988 kvm_run->ready_for_interrupt_injection =
1989 (vcpu->interrupt_window_open &&
1990 vcpu->irq_summary == 0);
1991}
1992
1993static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1994{
1995 int r;
1996
1997 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
Mike Dayd77c26f2007-10-08 09:02:08 -04001998 pr_debug("vcpu %d received sipi with vector # %x\n",
Avi Kivity04d2cc72007-09-10 18:10:54 +03001999 vcpu->vcpu_id, vcpu->sipi_vector);
2000 kvm_lapic_reset(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002001 r = kvm_x86_ops->vcpu_reset(vcpu);
2002 if (r)
2003 return r;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002004 vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
2005 }
2006
2007preempted:
2008 if (vcpu->guest_debug.enabled)
2009 kvm_x86_ops->guest_debug_pre(vcpu);
2010
2011again:
2012 r = kvm_mmu_reload(vcpu);
2013 if (unlikely(r))
2014 goto out;
2015
Avi Kivityab6ef342007-10-16 16:23:22 +02002016 kvm_inject_pending_timer_irqs(vcpu);
2017
Avi Kivity04d2cc72007-09-10 18:10:54 +03002018 preempt_disable();
2019
2020 kvm_x86_ops->prepare_guest_switch(vcpu);
2021 kvm_load_guest_fpu(vcpu);
2022
2023 local_irq_disable();
2024
2025 if (signal_pending(current)) {
2026 local_irq_enable();
2027 preempt_enable();
2028 r = -EINTR;
2029 kvm_run->exit_reason = KVM_EXIT_INTR;
2030 ++vcpu->stat.signal_exits;
2031 goto out;
2032 }
2033
2034 if (irqchip_in_kernel(vcpu->kvm))
2035 kvm_x86_ops->inject_pending_irq(vcpu);
2036 else if (!vcpu->mmio_read_completed)
2037 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2038
2039 vcpu->guest_mode = 1;
Laurent Vivierd172fcd2007-10-15 17:00:19 +02002040 kvm_guest_enter();
Avi Kivity04d2cc72007-09-10 18:10:54 +03002041
2042 if (vcpu->requests)
Avi Kivity3176bc32007-10-16 17:22:08 +02002043 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
Avi Kivity04d2cc72007-09-10 18:10:54 +03002044 kvm_x86_ops->tlb_flush(vcpu);
2045
2046 kvm_x86_ops->run(vcpu, kvm_run);
2047
2048 vcpu->guest_mode = 0;
2049 local_irq_enable();
2050
2051 ++vcpu->stat.exits;
2052
Laurent Vivier0552f732007-10-18 15:19:01 +02002053 /*
2054 * We must have an instruction between local_irq_enable() and
2055 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2056 * the interrupt shadow. The stat.exits increment will do nicely.
2057 * But we need to prevent reordering, hence this barrier():
2058 */
2059 barrier();
2060
2061 kvm_guest_exit();
2062
Avi Kivity04d2cc72007-09-10 18:10:54 +03002063 preempt_enable();
2064
2065 /*
2066 * Profile KVM exit RIPs:
2067 */
2068 if (unlikely(prof_on == KVM_PROFILING)) {
2069 kvm_x86_ops->cache_regs(vcpu);
2070 profile_hit(KVM_PROFILING, (void *)vcpu->rip);
2071 }
2072
2073 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2074
2075 if (r > 0) {
2076 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2077 r = -EINTR;
2078 kvm_run->exit_reason = KVM_EXIT_INTR;
2079 ++vcpu->stat.request_irq_exits;
2080 goto out;
2081 }
2082 if (!need_resched()) {
2083 ++vcpu->stat.light_exits;
2084 goto again;
2085 }
2086 }
2087
2088out:
2089 if (r > 0) {
2090 kvm_resched(vcpu);
2091 goto preempted;
2092 }
2093
2094 post_kvm_run_save(vcpu, kvm_run);
2095
2096 return r;
2097}
2098
2099
Avi Kivitybccf2152007-02-21 18:04:26 +02002100static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02002103 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104
Avi Kivitybccf2152007-02-21 18:04:26 +02002105 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106
He, Qingc5ec1532007-09-03 17:07:41 +03002107 if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2108 kvm_vcpu_block(vcpu);
2109 vcpu_put(vcpu);
2110 return -EAGAIN;
2111 }
2112
Avi Kivity1961d272007-03-05 19:46:05 +02002113 if (vcpu->sigset_active)
2114 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2115
Dor Laor54810342007-02-12 00:54:39 -08002116 /* re-sync apic's tpr */
He, Qing5cd4f6f2007-08-30 17:04:26 +08002117 if (!irqchip_in_kernel(vcpu->kvm))
2118 set_cr8(vcpu, kvm_run->cr8);
Dor Laor54810342007-02-12 00:54:39 -08002119
Avi Kivity02c83202007-04-29 15:02:17 +03002120 if (vcpu->pio.cur_count) {
2121 r = complete_pio(vcpu);
2122 if (r)
2123 goto out;
2124 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002125#if CONFIG_HAS_IOMEM
Avi Kivity02c83202007-04-29 15:02:17 +03002126 if (vcpu->mmio_needed) {
2127 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2128 vcpu->mmio_read_completed = 1;
2129 vcpu->mmio_needed = 0;
2130 r = emulate_instruction(vcpu, kvm_run,
Laurent Vivier34273182007-09-18 11:27:37 +02002131 vcpu->mmio_fault_cr2, 0, 1);
Avi Kivity02c83202007-04-29 15:02:17 +03002132 if (r == EMULATE_DO_MMIO) {
2133 /*
2134 * Read-modify-write. Back to userspace.
2135 */
Avi Kivity02c83202007-04-29 15:02:17 +03002136 r = 0;
2137 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02002138 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 }
Zhang Xiantao34c16ee2007-10-20 15:34:38 +08002140#endif
Avi Kivity8eb7d332007-03-04 14:17:08 +02002141 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002142 kvm_x86_ops->cache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002143 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002144 kvm_x86_ops->decache_regs(vcpu);
Avi Kivityb4e63f52007-03-04 13:59:30 +02002145 }
2146
Avi Kivity04d2cc72007-09-10 18:10:54 +03002147 r = __vcpu_run(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148
Avi Kivity039576c2007-03-20 12:46:50 +02002149out:
Avi Kivity1961d272007-03-05 19:46:05 +02002150 if (vcpu->sigset_active)
2151 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2152
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 vcpu_put(vcpu);
2154 return r;
2155}
2156
Avi Kivitybccf2152007-02-21 18:04:26 +02002157static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
2158 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159{
Avi Kivitybccf2152007-02-21 18:04:26 +02002160 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002162 kvm_x86_ops->cache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163
2164 regs->rax = vcpu->regs[VCPU_REGS_RAX];
2165 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
2166 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
2167 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
2168 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
2169 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
2170 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
2171 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002172#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173 regs->r8 = vcpu->regs[VCPU_REGS_R8];
2174 regs->r9 = vcpu->regs[VCPU_REGS_R9];
2175 regs->r10 = vcpu->regs[VCPU_REGS_R10];
2176 regs->r11 = vcpu->regs[VCPU_REGS_R11];
2177 regs->r12 = vcpu->regs[VCPU_REGS_R12];
2178 regs->r13 = vcpu->regs[VCPU_REGS_R13];
2179 regs->r14 = vcpu->regs[VCPU_REGS_R14];
2180 regs->r15 = vcpu->regs[VCPU_REGS_R15];
2181#endif
2182
2183 regs->rip = vcpu->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002184 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185
2186 /*
2187 * Don't leak debug flags in case they were set for guest debugging
2188 */
2189 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2190 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2191
2192 vcpu_put(vcpu);
2193
2194 return 0;
2195}
2196
Avi Kivitybccf2152007-02-21 18:04:26 +02002197static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2198 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199{
Avi Kivitybccf2152007-02-21 18:04:26 +02002200 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201
2202 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2203 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2204 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2205 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2206 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2207 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2208 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2209 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002210#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2212 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2213 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2214 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2215 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2216 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2217 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2218 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2219#endif
2220
2221 vcpu->rip = regs->rip;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002222 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002224 kvm_x86_ops->decache_regs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225
2226 vcpu_put(vcpu);
2227
2228 return 0;
2229}
2230
2231static void get_segment(struct kvm_vcpu *vcpu,
2232 struct kvm_segment *var, int seg)
2233{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002234 return kvm_x86_ops->get_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235}
2236
Avi Kivitybccf2152007-02-21 18:04:26 +02002237static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2238 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002239{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 struct descriptor_table dt;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002241 int pending_vec;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242
Avi Kivitybccf2152007-02-21 18:04:26 +02002243 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244
2245 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2246 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2247 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2248 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2249 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2250 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2251
2252 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2253 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2254
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002255 kvm_x86_ops->get_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 sregs->idt.limit = dt.limit;
2257 sregs->idt.base = dt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002258 kvm_x86_ops->get_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 sregs->gdt.limit = dt.limit;
2260 sregs->gdt.base = dt.base;
2261
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002262 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 sregs->cr0 = vcpu->cr0;
2264 sregs->cr2 = vcpu->cr2;
2265 sregs->cr3 = vcpu->cr3;
2266 sregs->cr4 = vcpu->cr4;
Eddie Dong7017fc32007-07-18 11:34:57 +03002267 sregs->cr8 = get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 sregs->efer = vcpu->shadow_efer;
Eddie Dong7017fc32007-07-18 11:34:57 +03002269 sregs->apic_base = kvm_get_apic_base(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270
Eddie Dong2a8067f2007-08-06 16:29:07 +03002271 if (irqchip_in_kernel(vcpu->kvm)) {
He, Qingc52fb352007-08-02 14:03:07 +03002272 memset(sregs->interrupt_bitmap, 0,
2273 sizeof sregs->interrupt_bitmap);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002274 pending_vec = kvm_x86_ops->get_irq(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002275 if (pending_vec >= 0)
Mike Dayd77c26f2007-10-08 09:02:08 -04002276 set_bit(pending_vec,
2277 (unsigned long *)sregs->interrupt_bitmap);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002278 } else
He, Qingc52fb352007-08-02 14:03:07 +03002279 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2280 sizeof sregs->interrupt_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002281
2282 vcpu_put(vcpu);
2283
2284 return 0;
2285}
2286
2287static void set_segment(struct kvm_vcpu *vcpu,
2288 struct kvm_segment *var, int seg)
2289{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002290 return kvm_x86_ops->set_segment(vcpu, var, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002291}
2292
Avi Kivitybccf2152007-02-21 18:04:26 +02002293static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2294 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 int mmu_reset_needed = 0;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002297 int i, pending_vec, max_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298 struct descriptor_table dt;
2299
Avi Kivitybccf2152007-02-21 18:04:26 +02002300 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302 dt.limit = sregs->idt.limit;
2303 dt.base = sregs->idt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002304 kvm_x86_ops->set_idt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 dt.limit = sregs->gdt.limit;
2306 dt.base = sregs->gdt.base;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002307 kvm_x86_ops->set_gdt(vcpu, &dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308
2309 vcpu->cr2 = sregs->cr2;
2310 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2311 vcpu->cr3 = sregs->cr3;
2312
Eddie Dong7017fc32007-07-18 11:34:57 +03002313 set_cr8(vcpu, sregs->cr8);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314
2315 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002316#ifdef CONFIG_X86_64
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002317 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318#endif
Eddie Dong7017fc32007-07-18 11:34:57 +03002319 kvm_set_apic_base(vcpu, sregs->apic_base);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002321 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002322
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Rusty Russell81f50e32007-09-06 01:20:38 +10002324 vcpu->cr0 = sregs->cr0;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002325 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326
2327 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002328 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002329 if (!is_long_mode(vcpu) && is_pae(vcpu))
2330 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331
2332 if (mmu_reset_needed)
2333 kvm_mmu_reset_context(vcpu);
2334
He, Qingc52fb352007-08-02 14:03:07 +03002335 if (!irqchip_in_kernel(vcpu->kvm)) {
2336 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2337 sizeof vcpu->irq_pending);
2338 vcpu->irq_summary = 0;
2339 for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2340 if (vcpu->irq_pending[i])
2341 __set_bit(i, &vcpu->irq_summary);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002342 } else {
2343 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2344 pending_vec = find_first_bit(
2345 (const unsigned long *)sregs->interrupt_bitmap,
2346 max_bits);
2347 /* Only pending external irq is handled here */
2348 if (pending_vec < max_bits) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002349 kvm_x86_ops->set_irq(vcpu, pending_vec);
Mike Dayd77c26f2007-10-08 09:02:08 -04002350 pr_debug("Set back pending irq %d\n",
2351 pending_vec);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002352 }
He, Qingc52fb352007-08-02 14:03:07 +03002353 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354
Avi Kivity024aa1c2007-03-21 13:44:58 +02002355 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2356 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2357 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2358 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2359 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2360 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2361
2362 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2363 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2364
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365 vcpu_put(vcpu);
2366
2367 return 0;
2368}
2369
Rusty Russell1747fb72007-09-06 01:21:32 +10002370void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2371{
2372 struct kvm_segment cs;
2373
2374 get_segment(vcpu, &cs, VCPU_SREG_CS);
2375 *db = cs.db;
2376 *l = cs.l;
2377}
2378EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2379
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381 * Translate a guest virtual address to a guest physical address.
2382 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002383static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2384 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385{
2386 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 gpa_t gpa;
2388
Avi Kivitybccf2152007-02-21 18:04:26 +02002389 vcpu_load(vcpu);
Shaohua Li11ec2802007-07-23 14:51:37 +08002390 mutex_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2392 tr->physical_address = gpa;
2393 tr->valid = gpa != UNMAPPED_GVA;
2394 tr->writeable = 1;
2395 tr->usermode = 0;
Shaohua Li11ec2802007-07-23 14:51:37 +08002396 mutex_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 vcpu_put(vcpu);
2398
2399 return 0;
2400}
2401
Avi Kivitybccf2152007-02-21 18:04:26 +02002402static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2403 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 if (irq->irq < 0 || irq->irq >= 256)
2406 return -EINVAL;
Eddie Dong97222cc2007-09-12 10:58:04 +03002407 if (irqchip_in_kernel(vcpu->kvm))
2408 return -ENXIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02002409 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410
2411 set_bit(irq->irq, vcpu->irq_pending);
2412 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2413
2414 vcpu_put(vcpu);
2415
2416 return 0;
2417}
2418
Avi Kivitybccf2152007-02-21 18:04:26 +02002419static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2420 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422 int r;
2423
Avi Kivitybccf2152007-02-21 18:04:26 +02002424 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002426 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
2428 vcpu_put(vcpu);
2429
2430 return r;
2431}
2432
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002433static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2434 unsigned long address,
2435 int *type)
2436{
2437 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2438 unsigned long pgoff;
2439 struct page *page;
2440
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002441 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002442 if (pgoff == 0)
2443 page = virt_to_page(vcpu->run);
2444 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2445 page = virt_to_page(vcpu->pio_data);
2446 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002447 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002448 get_page(page);
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002449 if (type != NULL)
2450 *type = VM_FAULT_MINOR;
2451
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002452 return page;
2453}
2454
2455static struct vm_operations_struct kvm_vcpu_vm_ops = {
2456 .nopage = kvm_vcpu_nopage,
2457};
2458
2459static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2460{
2461 vma->vm_ops = &kvm_vcpu_vm_ops;
2462 return 0;
2463}
2464
Avi Kivitybccf2152007-02-21 18:04:26 +02002465static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2466{
2467 struct kvm_vcpu *vcpu = filp->private_data;
2468
2469 fput(vcpu->kvm->filp);
2470 return 0;
2471}
2472
2473static struct file_operations kvm_vcpu_fops = {
2474 .release = kvm_vcpu_release,
2475 .unlocked_ioctl = kvm_vcpu_ioctl,
2476 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002477 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002478};
2479
2480/*
2481 * Allocates an inode for the vcpu.
2482 */
2483static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2484{
2485 int fd, r;
2486 struct inode *inode;
2487 struct file *file;
2488
Avi Kivityd6d28162007-06-28 08:38:16 -04002489 r = anon_inode_getfd(&fd, &inode, &file,
2490 "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2491 if (r)
2492 return r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002493 atomic_inc(&vcpu->kvm->filp->f_count);
Avi Kivitybccf2152007-02-21 18:04:26 +02002494 return fd;
Avi Kivitybccf2152007-02-21 18:04:26 +02002495}
2496
Avi Kivityc5ea7662007-02-20 18:41:05 +02002497/*
2498 * Creates some virtual cpus. Good luck creating more than one.
2499 */
2500static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2501{
2502 int r;
2503 struct kvm_vcpu *vcpu;
2504
Avi Kivityc5ea7662007-02-20 18:41:05 +02002505 if (!valid_vcpu(n))
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002506 return -EINVAL;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002507
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002508 vcpu = kvm_x86_ops->vcpu_create(kvm, n);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002509 if (IS_ERR(vcpu))
2510 return PTR_ERR(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002511
Avi Kivity15ad7142007-07-11 18:17:21 +03002512 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
2513
Rusty Russellb114b082007-07-30 21:13:43 +10002514 /* We do fxsave: this must be aligned. */
2515 BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
2516
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002517 vcpu_load(vcpu);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002518 r = kvm_x86_ops->vcpu_reset(vcpu);
2519 if (r == 0)
2520 r = kvm_mmu_setup(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002521 vcpu_put(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002522 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002523 goto free_vcpu;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002524
Shaohua Li11ec2802007-07-23 14:51:37 +08002525 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002526 if (kvm->vcpus[n]) {
2527 r = -EEXIST;
Shaohua Li11ec2802007-07-23 14:51:37 +08002528 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002529 goto mmu_unload;
2530 }
2531 kvm->vcpus[n] = vcpu;
Shaohua Li11ec2802007-07-23 14:51:37 +08002532 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002533
2534 /* Now it's all set up, let userspace reach it */
Avi Kivitybccf2152007-02-21 18:04:26 +02002535 r = create_vcpu_fd(vcpu);
2536 if (r < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002537 goto unlink;
Avi Kivitybccf2152007-02-21 18:04:26 +02002538 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002539
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002540unlink:
Shaohua Li11ec2802007-07-23 14:51:37 +08002541 mutex_lock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002542 kvm->vcpus[n] = NULL;
Shaohua Li11ec2802007-07-23 14:51:37 +08002543 mutex_unlock(&kvm->lock);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002544
2545mmu_unload:
2546 vcpu_load(vcpu);
2547 kvm_mmu_unload(vcpu);
2548 vcpu_put(vcpu);
2549
2550free_vcpu:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002551 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivityc5ea7662007-02-20 18:41:05 +02002552 return r;
2553}
2554
Avi Kivity1961d272007-03-05 19:46:05 +02002555static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2556{
2557 if (sigset) {
2558 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2559 vcpu->sigset_active = 1;
2560 vcpu->sigset = *sigset;
2561 } else
2562 vcpu->sigset_active = 0;
2563 return 0;
2564}
2565
Avi Kivityb8836732007-04-01 16:34:31 +03002566/*
2567 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2568 * we have asm/x86/processor.h
2569 */
2570struct fxsave {
2571 u16 cwd;
2572 u16 swd;
2573 u16 twd;
2574 u16 fop;
2575 u64 rip;
2576 u64 rdp;
2577 u32 mxcsr;
2578 u32 mxcsr_mask;
2579 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2580#ifdef CONFIG_X86_64
2581 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2582#else
2583 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2584#endif
2585};
2586
2587static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2588{
Rusty Russellb114b082007-07-30 21:13:43 +10002589 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002590
2591 vcpu_load(vcpu);
2592
2593 memcpy(fpu->fpr, fxsave->st_space, 128);
2594 fpu->fcw = fxsave->cwd;
2595 fpu->fsw = fxsave->swd;
2596 fpu->ftwx = fxsave->twd;
2597 fpu->last_opcode = fxsave->fop;
2598 fpu->last_ip = fxsave->rip;
2599 fpu->last_dp = fxsave->rdp;
2600 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2601
2602 vcpu_put(vcpu);
2603
2604 return 0;
2605}
2606
2607static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2608{
Rusty Russellb114b082007-07-30 21:13:43 +10002609 struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
Avi Kivityb8836732007-04-01 16:34:31 +03002610
2611 vcpu_load(vcpu);
2612
2613 memcpy(fxsave->st_space, fpu->fpr, 128);
2614 fxsave->cwd = fpu->fcw;
2615 fxsave->swd = fpu->fsw;
2616 fxsave->twd = fpu->ftwx;
2617 fxsave->fop = fpu->last_opcode;
2618 fxsave->rip = fpu->last_ip;
2619 fxsave->rdp = fpu->last_dp;
2620 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2621
2622 vcpu_put(vcpu);
2623
2624 return 0;
2625}
2626
Avi Kivitybccf2152007-02-21 18:04:26 +02002627static long kvm_vcpu_ioctl(struct file *filp,
2628 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629{
Avi Kivitybccf2152007-02-21 18:04:26 +02002630 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002631 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002632 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633
2634 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002635 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002636 r = -EINVAL;
2637 if (arg)
2638 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002639 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 case KVM_GET_REGS: {
2642 struct kvm_regs kvm_regs;
2643
Avi Kivitybccf2152007-02-21 18:04:26 +02002644 memset(&kvm_regs, 0, sizeof kvm_regs);
2645 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 if (r)
2647 goto out;
2648 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002649 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 goto out;
2651 r = 0;
2652 break;
2653 }
2654 case KVM_SET_REGS: {
2655 struct kvm_regs kvm_regs;
2656
2657 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002658 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002660 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 if (r)
2662 goto out;
2663 r = 0;
2664 break;
2665 }
2666 case KVM_GET_SREGS: {
2667 struct kvm_sregs kvm_sregs;
2668
Avi Kivitybccf2152007-02-21 18:04:26 +02002669 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2670 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 if (r)
2672 goto out;
2673 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002674 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 goto out;
2676 r = 0;
2677 break;
2678 }
2679 case KVM_SET_SREGS: {
2680 struct kvm_sregs kvm_sregs;
2681
2682 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002683 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002685 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 if (r)
2687 goto out;
2688 r = 0;
2689 break;
2690 }
2691 case KVM_TRANSLATE: {
2692 struct kvm_translation tr;
2693
2694 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002695 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002697 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 if (r)
2699 goto out;
2700 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002701 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 goto out;
2703 r = 0;
2704 break;
2705 }
2706 case KVM_INTERRUPT: {
2707 struct kvm_interrupt irq;
2708
2709 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002710 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002712 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 if (r)
2714 goto out;
2715 r = 0;
2716 break;
2717 }
2718 case KVM_DEBUG_GUEST: {
2719 struct kvm_debug_guest dbg;
2720
2721 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002722 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002724 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 if (r)
2726 goto out;
2727 r = 0;
2728 break;
2729 }
Avi Kivity1961d272007-03-05 19:46:05 +02002730 case KVM_SET_SIGNAL_MASK: {
2731 struct kvm_signal_mask __user *sigmask_arg = argp;
2732 struct kvm_signal_mask kvm_sigmask;
2733 sigset_t sigset, *p;
2734
2735 p = NULL;
2736 if (argp) {
2737 r = -EFAULT;
2738 if (copy_from_user(&kvm_sigmask, argp,
2739 sizeof kvm_sigmask))
2740 goto out;
2741 r = -EINVAL;
2742 if (kvm_sigmask.len != sizeof sigset)
2743 goto out;
2744 r = -EFAULT;
2745 if (copy_from_user(&sigset, sigmask_arg->sigset,
2746 sizeof sigset))
2747 goto out;
2748 p = &sigset;
2749 }
2750 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2751 break;
2752 }
Avi Kivityb8836732007-04-01 16:34:31 +03002753 case KVM_GET_FPU: {
2754 struct kvm_fpu fpu;
2755
2756 memset(&fpu, 0, sizeof fpu);
2757 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2758 if (r)
2759 goto out;
2760 r = -EFAULT;
2761 if (copy_to_user(argp, &fpu, sizeof fpu))
2762 goto out;
2763 r = 0;
2764 break;
2765 }
2766 case KVM_SET_FPU: {
2767 struct kvm_fpu fpu;
2768
2769 r = -EFAULT;
2770 if (copy_from_user(&fpu, argp, sizeof fpu))
2771 goto out;
2772 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2773 if (r)
2774 goto out;
2775 r = 0;
2776 break;
2777 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002778 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02002779 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02002780 }
2781out:
2782 return r;
2783}
2784
2785static long kvm_vm_ioctl(struct file *filp,
2786 unsigned int ioctl, unsigned long arg)
2787{
2788 struct kvm *kvm = filp->private_data;
2789 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002790 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02002791
2792 switch (ioctl) {
2793 case KVM_CREATE_VCPU:
2794 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2795 if (r < 0)
2796 goto out;
2797 break;
Izik Eidus6fc138d2007-10-09 19:20:39 +02002798 case KVM_SET_USER_MEMORY_REGION: {
2799 struct kvm_userspace_memory_region kvm_userspace_mem;
2800
2801 r = -EFAULT;
2802 if (copy_from_user(&kvm_userspace_mem, argp,
2803 sizeof kvm_userspace_mem))
2804 goto out;
2805
2806 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 if (r)
2808 goto out;
2809 break;
2810 }
2811 case KVM_GET_DIRTY_LOG: {
2812 struct kvm_dirty_log log;
2813
2814 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002815 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002817 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 if (r)
2819 goto out;
2820 break;
2821 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002822 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002823 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02002824 }
2825out:
2826 return r;
2827}
2828
2829static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2830 unsigned long address,
2831 int *type)
2832{
2833 struct kvm *kvm = vma->vm_file->private_data;
2834 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002835 struct page *page;
2836
Avi Kivityf17abe92007-02-21 19:28:04 +02002837 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Izik Eiduse0d62c72007-10-24 23:57:46 +02002838 if (!kvm_is_visible_gfn(kvm, pgoff))
2839 return NOPAGE_SIGBUS;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002840 page = gfn_to_page(kvm, pgoff);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002841 if (is_error_page(page)) {
2842 kvm_release_page(page);
Avi Kivityf17abe92007-02-21 19:28:04 +02002843 return NOPAGE_SIGBUS;
Izik Eidus8a7ae052007-10-18 11:09:33 +02002844 }
Nguyen Anh Quynhcd0d9132007-07-11 14:30:54 +03002845 if (type != NULL)
2846 *type = VM_FAULT_MINOR;
2847
Avi Kivityf17abe92007-02-21 19:28:04 +02002848 return page;
2849}
2850
2851static struct vm_operations_struct kvm_vm_vm_ops = {
2852 .nopage = kvm_vm_nopage,
2853};
2854
2855static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2856{
2857 vma->vm_ops = &kvm_vm_vm_ops;
2858 return 0;
2859}
2860
2861static struct file_operations kvm_vm_fops = {
2862 .release = kvm_vm_release,
2863 .unlocked_ioctl = kvm_vm_ioctl,
2864 .compat_ioctl = kvm_vm_ioctl,
2865 .mmap = kvm_vm_mmap,
2866};
2867
2868static int kvm_dev_ioctl_create_vm(void)
2869{
2870 int fd, r;
2871 struct inode *inode;
2872 struct file *file;
2873 struct kvm *kvm;
2874
Avi Kivityf17abe92007-02-21 19:28:04 +02002875 kvm = kvm_create_vm();
Avi Kivityd6d28162007-06-28 08:38:16 -04002876 if (IS_ERR(kvm))
2877 return PTR_ERR(kvm);
2878 r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2879 if (r) {
2880 kvm_destroy_vm(kvm);
2881 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02002882 }
2883
Avi Kivitybccf2152007-02-21 18:04:26 +02002884 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002885
Avi Kivityf17abe92007-02-21 19:28:04 +02002886 return fd;
Avi Kivityf17abe92007-02-21 19:28:04 +02002887}
2888
2889static long kvm_dev_ioctl(struct file *filp,
2890 unsigned int ioctl, unsigned long arg)
2891{
2892 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002893 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002894
2895 switch (ioctl) {
2896 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002897 r = -EINVAL;
2898 if (arg)
2899 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002900 r = KVM_API_VERSION;
2901 break;
2902 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002903 r = -EINVAL;
2904 if (arg)
2905 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002906 r = kvm_dev_ioctl_create_vm();
2907 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002908 case KVM_CHECK_EXTENSION: {
2909 int ext = (long)argp;
2910
2911 switch (ext) {
2912 case KVM_CAP_IRQCHIP:
Eddie Dongb6958ce2007-07-18 12:15:21 +03002913 case KVM_CAP_HLT:
Izik Eidus82ce2c92007-10-02 18:52:55 +02002914 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Izik Eidus6fc138d2007-10-09 19:20:39 +02002915 case KVM_CAP_USER_MEMORY:
Izik Eiduscbc94022007-10-25 00:29:55 +02002916 case KVM_CAP_SET_TSS_ADDR:
Eddie Dong85f455f2007-07-06 12:20:49 +03002917 r = 1;
2918 break;
2919 default:
2920 r = 0;
2921 break;
2922 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002923 break;
Eddie Dong85f455f2007-07-06 12:20:49 +03002924 }
Avi Kivity07c45a32007-03-07 13:05:38 +02002925 case KVM_GET_VCPU_MMAP_SIZE:
2926 r = -EINVAL;
2927 if (arg)
2928 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002929 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002930 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931 default:
Carsten Otte043405e2007-10-10 17:16:19 +02002932 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 }
2934out:
2935 return r;
2936}
2937
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 .unlocked_ioctl = kvm_dev_ioctl,
2940 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941};
2942
2943static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002944 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945 "kvm",
2946 &kvm_chardev_ops,
2947};
2948
Avi Kivity774c47f2007-02-12 00:54:47 -08002949/*
2950 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2951 * cached on it.
2952 */
2953static void decache_vcpus_on_cpu(int cpu)
2954{
2955 struct kvm *vm;
2956 struct kvm_vcpu *vcpu;
2957 int i;
2958
2959 spin_lock(&kvm_lock);
Shaohua Li11ec2802007-07-23 14:51:37 +08002960 list_for_each_entry(vm, &vm_list, vm_list)
Avi Kivity774c47f2007-02-12 00:54:47 -08002961 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002962 vcpu = vm->vcpus[i];
2963 if (!vcpu)
2964 continue;
Avi Kivity774c47f2007-02-12 00:54:47 -08002965 /*
2966 * If the vcpu is locked, then it is running on some
2967 * other cpu and therefore it is not cached on the
2968 * cpu in question.
2969 *
2970 * If it's not locked, check the last cpu it executed
2971 * on.
2972 */
2973 if (mutex_trylock(&vcpu->mutex)) {
2974 if (vcpu->cpu == cpu) {
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002975 kvm_x86_ops->vcpu_decache(vcpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002976 vcpu->cpu = -1;
2977 }
2978 mutex_unlock(&vcpu->mutex);
2979 }
2980 }
2981 spin_unlock(&kvm_lock);
2982}
2983
Avi Kivity1b6c0162007-05-24 13:03:52 +03002984static void hardware_enable(void *junk)
2985{
2986 int cpu = raw_smp_processor_id();
2987
2988 if (cpu_isset(cpu, cpus_hardware_enabled))
2989 return;
2990 cpu_set(cpu, cpus_hardware_enabled);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002991 kvm_x86_ops->hardware_enable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03002992}
2993
2994static void hardware_disable(void *junk)
2995{
2996 int cpu = raw_smp_processor_id();
2997
2998 if (!cpu_isset(cpu, cpus_hardware_enabled))
2999 return;
3000 cpu_clear(cpu, cpus_hardware_enabled);
3001 decache_vcpus_on_cpu(cpu);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003002 kvm_x86_ops->hardware_disable(NULL);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003003}
3004
Avi Kivity774c47f2007-02-12 00:54:47 -08003005static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3006 void *v)
3007{
3008 int cpu = (long)v;
3009
3010 switch (val) {
Avi Kivitycec9ad22007-05-24 13:11:41 +03003011 case CPU_DYING:
3012 case CPU_DYING_FROZEN:
Avi Kivity6ec8a852007-08-19 15:57:26 +03003013 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3014 cpu);
3015 hardware_disable(NULL);
3016 break;
Avi Kivity774c47f2007-02-12 00:54:47 -08003017 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003018 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003019 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3020 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003021 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003022 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02003023 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003024 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02003025 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3026 cpu);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003027 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003028 break;
3029 }
3030 return NOTIFY_OK;
3031}
3032
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003033static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04003034 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10003035{
3036 if (val == SYS_RESTART) {
3037 /*
3038 * Some (well, at least mine) BIOSes hang on reboot if
3039 * in vmx root mode.
3040 */
3041 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3042 on_each_cpu(hardware_disable, NULL, 0, 1);
3043 }
3044 return NOTIFY_OK;
3045}
3046
3047static struct notifier_block kvm_reboot_notifier = {
3048 .notifier_call = kvm_reboot,
3049 .priority = 0,
3050};
3051
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04003052void kvm_io_bus_init(struct kvm_io_bus *bus)
3053{
3054 memset(bus, 0, sizeof(*bus));
3055}
3056
3057void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3058{
3059 int i;
3060
3061 for (i = 0; i < bus->dev_count; i++) {
3062 struct kvm_io_device *pos = bus->devs[i];
3063
3064 kvm_iodevice_destructor(pos);
3065 }
3066}
3067
3068struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3069{
3070 int i;
3071
3072 for (i = 0; i < bus->dev_count; i++) {
3073 struct kvm_io_device *pos = bus->devs[i];
3074
3075 if (pos->in_range(pos, addr))
3076 return pos;
3077 }
3078
3079 return NULL;
3080}
3081
3082void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3083{
3084 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3085
3086 bus->devs[bus->dev_count++] = dev;
3087}
3088
Avi Kivity774c47f2007-02-12 00:54:47 -08003089static struct notifier_block kvm_cpu_notifier = {
3090 .notifier_call = kvm_cpu_hotplug,
3091 .priority = 20, /* must be > scheduler priority */
3092};
3093
Avi Kivity1165f5f2007-04-19 17:27:43 +03003094static u64 stat_get(void *_offset)
3095{
3096 unsigned offset = (long)_offset;
3097 u64 total = 0;
3098 struct kvm *kvm;
3099 struct kvm_vcpu *vcpu;
3100 int i;
3101
3102 spin_lock(&kvm_lock);
3103 list_for_each_entry(kvm, &vm_list, vm_list)
3104 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003105 vcpu = kvm->vcpus[i];
3106 if (vcpu)
3107 total += *(u32 *)((void *)vcpu + offset);
Avi Kivity1165f5f2007-04-19 17:27:43 +03003108 }
3109 spin_unlock(&kvm_lock);
3110 return total;
3111}
3112
Rusty Russell3dea7ca2007-08-01 10:12:22 +10003113DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
Avi Kivity1165f5f2007-04-19 17:27:43 +03003114
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115static __init void kvm_init_debug(void)
3116{
3117 struct kvm_stats_debugfs_item *p;
3118
Al Viro8b6d44c2007-02-09 16:38:40 +00003119 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03003121 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3122 (void *)(long)p->offset,
3123 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124}
3125
3126static void kvm_exit_debug(void)
3127{
3128 struct kvm_stats_debugfs_item *p;
3129
3130 for (p = debugfs_entries; p->name; ++p)
3131 debugfs_remove(p->dentry);
3132 debugfs_remove(debugfs_dir);
3133}
3134
Avi Kivity59ae6c62007-02-12 00:54:48 -08003135static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3136{
Avi Kivity4267c412007-05-24 13:09:41 +03003137 hardware_disable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003138 return 0;
3139}
3140
3141static int kvm_resume(struct sys_device *dev)
3142{
Avi Kivity4267c412007-05-24 13:09:41 +03003143 hardware_enable(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003144 return 0;
3145}
3146
3147static struct sysdev_class kvm_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003148 .name = "kvm",
Avi Kivity59ae6c62007-02-12 00:54:48 -08003149 .suspend = kvm_suspend,
3150 .resume = kvm_resume,
3151};
3152
3153static struct sys_device kvm_sysdev = {
3154 .id = 0,
3155 .cls = &kvm_sysdev_class,
3156};
3157
Izik Eiduscea7bb22007-10-17 19:17:48 +02003158struct page *bad_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159
Avi Kivity15ad7142007-07-11 18:17:21 +03003160static inline
3161struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
3162{
3163 return container_of(pn, struct kvm_vcpu, preempt_notifier);
3164}
3165
3166static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
3167{
3168 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3169
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003170 kvm_x86_ops->vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003171}
3172
3173static void kvm_sched_out(struct preempt_notifier *pn,
3174 struct task_struct *next)
3175{
3176 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
3177
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003178 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003179}
3180
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003181int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
Rusty Russellc16f8622007-07-30 21:12:19 +10003182 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183{
3184 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003185 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003187 if (kvm_x86_ops) {
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003188 printk(KERN_ERR "kvm: already loaded the other module\n");
3189 return -EEXIST;
3190 }
3191
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003192 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 printk(KERN_ERR "kvm: no hardware support\n");
3194 return -EOPNOTSUPP;
3195 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003196 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197 printk(KERN_ERR "kvm: disabled by bios\n");
3198 return -EOPNOTSUPP;
3199 }
3200
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003201 kvm_x86_ops = ops;
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003202
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003203 r = kvm_x86_ops->hardware_setup();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003205 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206
Yang, Sheng002c7f72007-07-31 14:23:01 +03003207 for_each_online_cpu(cpu) {
3208 smp_call_function_single(cpu,
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003209 kvm_x86_ops->check_processor_compatibility,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003210 &r, 0, 1);
3211 if (r < 0)
3212 goto out_free_0;
3213 }
3214
Avi Kivity1b6c0162007-05-24 13:03:52 +03003215 on_each_cpu(hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003216 r = register_cpu_notifier(&kvm_cpu_notifier);
3217 if (r)
3218 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 register_reboot_notifier(&kvm_reboot_notifier);
3220
Avi Kivity59ae6c62007-02-12 00:54:48 -08003221 r = sysdev_class_register(&kvm_sysdev_class);
3222 if (r)
3223 goto out_free_2;
3224
3225 r = sysdev_register(&kvm_sysdev);
3226 if (r)
3227 goto out_free_3;
3228
Rusty Russellc16f8622007-07-30 21:12:19 +10003229 /* A kmem cache lets us meet the alignment requirements of fx_save. */
3230 kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
3231 __alignof__(struct kvm_vcpu), 0, 0);
3232 if (!kvm_vcpu_cache) {
3233 r = -ENOMEM;
3234 goto out_free_4;
3235 }
3236
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 kvm_chardev_ops.owner = module;
3238
3239 r = misc_register(&kvm_dev);
3240 if (r) {
Mike Dayd77c26f2007-10-08 09:02:08 -04003241 printk(KERN_ERR "kvm: misc device register failed\n");
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 goto out_free;
3243 }
3244
Avi Kivity15ad7142007-07-11 18:17:21 +03003245 kvm_preempt_ops.sched_in = kvm_sched_in;
3246 kvm_preempt_ops.sched_out = kvm_sched_out;
3247
Avi Kivityc7addb92007-09-16 18:58:32 +02003248 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3249
3250 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251
3252out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10003253 kmem_cache_destroy(kvm_vcpu_cache);
3254out_free_4:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003255 sysdev_unregister(&kvm_sysdev);
3256out_free_3:
3257 sysdev_class_unregister(&kvm_sysdev_class);
3258out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003260 unregister_cpu_notifier(&kvm_cpu_notifier);
3261out_free_1:
Avi Kivity1b6c0162007-05-24 13:03:52 +03003262 on_each_cpu(hardware_disable, NULL, 0, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03003263out_free_0:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003264 kvm_x86_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003265out:
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003266 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 return r;
3268}
Mike Dayd77c26f2007-10-08 09:02:08 -04003269EXPORT_SYMBOL_GPL(kvm_init_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003271void kvm_exit_x86(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272{
3273 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10003274 kmem_cache_destroy(kvm_vcpu_cache);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003275 sysdev_unregister(&kvm_sysdev);
3276 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003278 unregister_cpu_notifier(&kvm_cpu_notifier);
Avi Kivity1b6c0162007-05-24 13:03:52 +03003279 on_each_cpu(hardware_disable, NULL, 0, 1);
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003280 kvm_x86_ops->hardware_unsetup();
3281 kvm_x86_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282}
Mike Dayd77c26f2007-10-08 09:02:08 -04003283EXPORT_SYMBOL_GPL(kvm_exit_x86);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284
3285static __init int kvm_init(void)
3286{
Avi Kivity37e29d92007-02-20 14:07:37 +02003287 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288
Avi Kivityb5a33a72007-04-15 16:31:09 +03003289 r = kvm_mmu_module_init();
3290 if (r)
3291 goto out4;
3292
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 kvm_init_debug();
3294
Carsten Otte043405e2007-10-10 17:16:19 +02003295 kvm_arch_init();
Michael Riepebf591b22006-12-22 01:05:36 -08003296
Izik Eiduscea7bb22007-10-17 19:17:48 +02003297 bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
Mike Dayd77c26f2007-10-08 09:02:08 -04003298
3299 if (bad_page == NULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300 r = -ENOMEM;
3301 goto out;
3302 }
3303
Avi Kivity58e690e2007-02-26 16:29:43 +02003304 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305
3306out:
3307 kvm_exit_debug();
Avi Kivityb5a33a72007-04-15 16:31:09 +03003308 kvm_mmu_module_exit();
3309out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 return r;
3311}
3312
3313static __exit void kvm_exit(void)
3314{
3315 kvm_exit_debug();
Izik Eiduscea7bb22007-10-17 19:17:48 +02003316 __free_page(bad_page);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003317 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318}
3319
3320module_init(kvm_init)
3321module_exit(kvm_exit)