]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
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" | |
e495606d AK |
19 | #include "x86_emulate.h" |
20 | #include "segment_descriptor.h" | |
85f455f7 | 21 | #include "irq.h" |
6aa8b732 AK |
22 | |
23 | #include <linux/kvm.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/errno.h> | |
6aa8b732 AK |
26 | #include <linux/percpu.h> |
27 | #include <linux/gfp.h> | |
6aa8b732 AK |
28 | #include <linux/mm.h> |
29 | #include <linux/miscdevice.h> | |
30 | #include <linux/vmalloc.h> | |
6aa8b732 | 31 | #include <linux/reboot.h> |
6aa8b732 AK |
32 | #include <linux/debugfs.h> |
33 | #include <linux/highmem.h> | |
34 | #include <linux/file.h> | |
59ae6c6b | 35 | #include <linux/sysdev.h> |
774c47f1 | 36 | #include <linux/cpu.h> |
e8edc6e0 | 37 | #include <linux/sched.h> |
d9e368d6 AK |
38 | #include <linux/cpumask.h> |
39 | #include <linux/smp.h> | |
d6d28168 | 40 | #include <linux/anon_inodes.h> |
04d2cc77 | 41 | #include <linux/profile.h> |
7aa81cc0 | 42 | #include <linux/kvm_para.h> |
6aa8b732 | 43 | |
e495606d AK |
44 | #include <asm/processor.h> |
45 | #include <asm/msr.h> | |
46 | #include <asm/io.h> | |
47 | #include <asm/uaccess.h> | |
48 | #include <asm/desc.h> | |
6aa8b732 AK |
49 | |
50 | MODULE_AUTHOR("Qumranet"); | |
51 | MODULE_LICENSE("GPL"); | |
52 | ||
133de902 AK |
53 | static DEFINE_SPINLOCK(kvm_lock); |
54 | static LIST_HEAD(vm_list); | |
55 | ||
1b6c0168 AK |
56 | static cpumask_t cpus_hardware_enabled; |
57 | ||
cbdd1bea | 58 | struct kvm_x86_ops *kvm_x86_ops; |
c16f862d RR |
59 | struct kmem_cache *kvm_vcpu_cache; |
60 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 61 | |
15ad7146 AK |
62 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
63 | ||
1165f5fe | 64 | #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) |
6aa8b732 AK |
65 | |
66 | static struct kvm_stats_debugfs_item { | |
67 | const char *name; | |
1165f5fe | 68 | int offset; |
6aa8b732 AK |
69 | struct dentry *dentry; |
70 | } debugfs_entries[] = { | |
1165f5fe AK |
71 | { "pf_fixed", STAT_OFFSET(pf_fixed) }, |
72 | { "pf_guest", STAT_OFFSET(pf_guest) }, | |
73 | { "tlb_flush", STAT_OFFSET(tlb_flush) }, | |
74 | { "invlpg", STAT_OFFSET(invlpg) }, | |
75 | { "exits", STAT_OFFSET(exits) }, | |
76 | { "io_exits", STAT_OFFSET(io_exits) }, | |
77 | { "mmio_exits", STAT_OFFSET(mmio_exits) }, | |
78 | { "signal_exits", STAT_OFFSET(signal_exits) }, | |
79 | { "irq_window", STAT_OFFSET(irq_window_exits) }, | |
80 | { "halt_exits", STAT_OFFSET(halt_exits) }, | |
b6958ce4 | 81 | { "halt_wakeup", STAT_OFFSET(halt_wakeup) }, |
1165f5fe AK |
82 | { "request_irq", STAT_OFFSET(request_irq_exits) }, |
83 | { "irq_exits", STAT_OFFSET(irq_exits) }, | |
e6adf283 | 84 | { "light_exits", STAT_OFFSET(light_exits) }, |
2cc51560 | 85 | { "efer_reload", STAT_OFFSET(efer_reload) }, |
1165f5fe | 86 | { NULL } |
6aa8b732 AK |
87 | }; |
88 | ||
89 | static struct dentry *debugfs_dir; | |
90 | ||
91 | #define MAX_IO_MSRS 256 | |
92 | ||
707d92fa RR |
93 | #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)) | |
66aee91a RR |
97 | #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 | ||
7075bc81 | 103 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
6aa8b732 AK |
104 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
105 | ||
05b3e0c2 | 106 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
107 | // LDT or TSS descriptor in the GDT. 16 bytes. |
108 | struct segment_descriptor_64 { | |
109 | struct segment_descriptor s; | |
110 | u32 base_higher; | |
111 | u32 pad_zero; | |
112 | }; | |
113 | ||
114 | #endif | |
115 | ||
bccf2150 AK |
116 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
117 | unsigned long arg); | |
118 | ||
6aa8b732 AK |
119 | unsigned long segment_base(u16 selector) |
120 | { | |
121 | struct descriptor_table gdt; | |
122 | struct segment_descriptor *d; | |
123 | unsigned long table_base; | |
124 | typedef unsigned long ul; | |
125 | unsigned long v; | |
126 | ||
127 | if (selector == 0) | |
128 | return 0; | |
129 | ||
130 | asm ("sgdt %0" : "=m"(gdt)); | |
131 | table_base = gdt.base; | |
132 | ||
133 | if (selector & 4) { /* from ldt */ | |
134 | u16 ldt_selector; | |
135 | ||
136 | asm ("sldt %0" : "=g"(ldt_selector)); | |
137 | table_base = segment_base(ldt_selector); | |
138 | } | |
139 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); | |
140 | v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24); | |
05b3e0c2 | 141 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
142 | if (d->system == 0 |
143 | && (d->type == 2 || d->type == 9 || d->type == 11)) | |
144 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; | |
145 | #endif | |
146 | return v; | |
147 | } | |
148 | EXPORT_SYMBOL_GPL(segment_base); | |
149 | ||
5aacf0ca JM |
150 | static inline int valid_vcpu(int n) |
151 | { | |
152 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
153 | } | |
154 | ||
7702fd1f AK |
155 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
156 | { | |
157 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
158 | return; | |
159 | ||
160 | vcpu->guest_fpu_loaded = 1; | |
b114b080 RR |
161 | fx_save(&vcpu->host_fx_image); |
162 | fx_restore(&vcpu->guest_fx_image); | |
7702fd1f AK |
163 | } |
164 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
165 | ||
166 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
167 | { | |
168 | if (!vcpu->guest_fpu_loaded) | |
169 | return; | |
170 | ||
171 | vcpu->guest_fpu_loaded = 0; | |
b114b080 RR |
172 | fx_save(&vcpu->guest_fx_image); |
173 | fx_restore(&vcpu->host_fx_image); | |
7702fd1f AK |
174 | } |
175 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
176 | ||
bccf2150 AK |
177 | /* |
178 | * Switches to specified vcpu, until a matching vcpu_put() | |
179 | */ | |
180 | static void vcpu_load(struct kvm_vcpu *vcpu) | |
6aa8b732 | 181 | { |
15ad7146 AK |
182 | int cpu; |
183 | ||
bccf2150 | 184 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
185 | cpu = get_cpu(); |
186 | preempt_notifier_register(&vcpu->preempt_notifier); | |
cbdd1bea | 187 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 | 188 | put_cpu(); |
6aa8b732 AK |
189 | } |
190 | ||
6aa8b732 AK |
191 | static void vcpu_put(struct kvm_vcpu *vcpu) |
192 | { | |
15ad7146 | 193 | preempt_disable(); |
cbdd1bea | 194 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
195 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
196 | preempt_enable(); | |
6aa8b732 AK |
197 | mutex_unlock(&vcpu->mutex); |
198 | } | |
199 | ||
d9e368d6 AK |
200 | static void ack_flush(void *_completed) |
201 | { | |
d9e368d6 AK |
202 | } |
203 | ||
204 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
205 | { | |
49d3bd7e | 206 | int i, cpu; |
d9e368d6 AK |
207 | cpumask_t cpus; |
208 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 209 | |
d9e368d6 | 210 | cpus_clear(cpus); |
fb3f0f51 RR |
211 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
212 | vcpu = kvm->vcpus[i]; | |
213 | if (!vcpu) | |
214 | continue; | |
d9e368d6 AK |
215 | if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests)) |
216 | continue; | |
217 | cpu = vcpu->cpu; | |
218 | if (cpu != -1 && cpu != raw_smp_processor_id()) | |
49d3bd7e | 219 | cpu_set(cpu, cpus); |
d9e368d6 | 220 | } |
49d3bd7e | 221 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
d9e368d6 AK |
222 | } |
223 | ||
fb3f0f51 RR |
224 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
225 | { | |
226 | struct page *page; | |
227 | int r; | |
228 | ||
229 | mutex_init(&vcpu->mutex); | |
230 | vcpu->cpu = -1; | |
231 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
232 | vcpu->kvm = kvm; | |
233 | vcpu->vcpu_id = id; | |
c5ec1534 HQ |
234 | if (!irqchip_in_kernel(kvm) || id == 0) |
235 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
236 | else | |
237 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; | |
b6958ce4 | 238 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
239 | |
240 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
241 | if (!page) { | |
242 | r = -ENOMEM; | |
243 | goto fail; | |
244 | } | |
245 | vcpu->run = page_address(page); | |
246 | ||
247 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
248 | if (!page) { | |
249 | r = -ENOMEM; | |
250 | goto fail_free_run; | |
251 | } | |
252 | vcpu->pio_data = page_address(page); | |
253 | ||
fb3f0f51 RR |
254 | r = kvm_mmu_create(vcpu); |
255 | if (r < 0) | |
256 | goto fail_free_pio_data; | |
257 | ||
258 | return 0; | |
259 | ||
260 | fail_free_pio_data: | |
261 | free_page((unsigned long)vcpu->pio_data); | |
262 | fail_free_run: | |
263 | free_page((unsigned long)vcpu->run); | |
264 | fail: | |
265 | return -ENOMEM; | |
266 | } | |
267 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
268 | ||
269 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
270 | { | |
271 | kvm_mmu_destroy(vcpu); | |
1b9778da ED |
272 | if (vcpu->apic) |
273 | hrtimer_cancel(&vcpu->apic->timer.dev); | |
97222cc8 | 274 | kvm_free_apic(vcpu->apic); |
fb3f0f51 RR |
275 | free_page((unsigned long)vcpu->pio_data); |
276 | free_page((unsigned long)vcpu->run); | |
277 | } | |
278 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
279 | ||
f17abe9a | 280 | static struct kvm *kvm_create_vm(void) |
6aa8b732 AK |
281 | { |
282 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
6aa8b732 AK |
283 | |
284 | if (!kvm) | |
f17abe9a | 285 | return ERR_PTR(-ENOMEM); |
6aa8b732 | 286 | |
74906345 | 287 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 288 | mutex_init(&kvm->lock); |
6aa8b732 | 289 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
2eeb2e94 | 290 | kvm_io_bus_init(&kvm->mmio_bus); |
5e58cfe4 RR |
291 | spin_lock(&kvm_lock); |
292 | list_add(&kvm->vm_list, &vm_list); | |
293 | spin_unlock(&kvm_lock); | |
f17abe9a AK |
294 | return kvm; |
295 | } | |
296 | ||
6aa8b732 AK |
297 | /* |
298 | * Free any memory in @free but not in @dont. | |
299 | */ | |
300 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
301 | struct kvm_memory_slot *dont) | |
302 | { | |
303 | int i; | |
304 | ||
305 | if (!dont || free->phys_mem != dont->phys_mem) | |
306 | if (free->phys_mem) { | |
307 | for (i = 0; i < free->npages; ++i) | |
55a54f79 AK |
308 | if (free->phys_mem[i]) |
309 | __free_page(free->phys_mem[i]); | |
6aa8b732 AK |
310 | vfree(free->phys_mem); |
311 | } | |
312 | ||
313 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
314 | vfree(free->dirty_bitmap); | |
315 | ||
8b6d44c7 | 316 | free->phys_mem = NULL; |
6aa8b732 | 317 | free->npages = 0; |
8b6d44c7 | 318 | free->dirty_bitmap = NULL; |
6aa8b732 AK |
319 | } |
320 | ||
321 | static void kvm_free_physmem(struct kvm *kvm) | |
322 | { | |
323 | int i; | |
324 | ||
325 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 326 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
327 | } |
328 | ||
039576c0 AK |
329 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
330 | { | |
331 | int i; | |
332 | ||
3077c451 | 333 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) |
039576c0 AK |
334 | if (vcpu->pio.guest_pages[i]) { |
335 | __free_page(vcpu->pio.guest_pages[i]); | |
336 | vcpu->pio.guest_pages[i] = NULL; | |
337 | } | |
338 | } | |
339 | ||
7b53aa56 AK |
340 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
341 | { | |
7b53aa56 AK |
342 | vcpu_load(vcpu); |
343 | kvm_mmu_unload(vcpu); | |
344 | vcpu_put(vcpu); | |
345 | } | |
346 | ||
6aa8b732 AK |
347 | static void kvm_free_vcpus(struct kvm *kvm) |
348 | { | |
349 | unsigned int i; | |
350 | ||
7b53aa56 AK |
351 | /* |
352 | * Unpin any mmu pages first. | |
353 | */ | |
354 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
fb3f0f51 RR |
355 | if (kvm->vcpus[i]) |
356 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
357 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
358 | if (kvm->vcpus[i]) { | |
cbdd1bea | 359 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
fb3f0f51 RR |
360 | kvm->vcpus[i] = NULL; |
361 | } | |
362 | } | |
363 | ||
6aa8b732 AK |
364 | } |
365 | ||
f17abe9a AK |
366 | static void kvm_destroy_vm(struct kvm *kvm) |
367 | { | |
133de902 AK |
368 | spin_lock(&kvm_lock); |
369 | list_del(&kvm->vm_list); | |
370 | spin_unlock(&kvm_lock); | |
74906345 | 371 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 372 | kvm_io_bus_destroy(&kvm->mmio_bus); |
85f455f7 | 373 | kfree(kvm->vpic); |
1fd4f2a5 | 374 | kfree(kvm->vioapic); |
6aa8b732 AK |
375 | kvm_free_vcpus(kvm); |
376 | kvm_free_physmem(kvm); | |
377 | kfree(kvm); | |
f17abe9a AK |
378 | } |
379 | ||
380 | static int kvm_vm_release(struct inode *inode, struct file *filp) | |
381 | { | |
382 | struct kvm *kvm = filp->private_data; | |
383 | ||
384 | kvm_destroy_vm(kvm); | |
6aa8b732 AK |
385 | return 0; |
386 | } | |
387 | ||
388 | static void inject_gp(struct kvm_vcpu *vcpu) | |
389 | { | |
cbdd1bea | 390 | kvm_x86_ops->inject_gp(vcpu, 0); |
6aa8b732 AK |
391 | } |
392 | ||
1342d353 AK |
393 | /* |
394 | * Load the pae pdptrs. Return true is they are all valid. | |
395 | */ | |
396 | static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
6aa8b732 AK |
397 | { |
398 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
1342d353 | 399 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
6aa8b732 | 400 | int i; |
6aa8b732 | 401 | u64 *pdpt; |
1342d353 | 402 | int ret; |
954bbbc2 | 403 | struct page *page; |
c820c2aa | 404 | u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)]; |
6aa8b732 | 405 | |
11ec2804 | 406 | mutex_lock(&vcpu->kvm->lock); |
954bbbc2 | 407 | page = gfn_to_page(vcpu->kvm, pdpt_gfn); |
c820c2aa RR |
408 | if (!page) { |
409 | ret = 0; | |
410 | goto out; | |
411 | } | |
412 | ||
954bbbc2 | 413 | pdpt = kmap_atomic(page, KM_USER0); |
c820c2aa RR |
414 | memcpy(pdpte, pdpt+offset, sizeof(pdpte)); |
415 | kunmap_atomic(pdpt, KM_USER0); | |
6aa8b732 | 416 | |
c820c2aa RR |
417 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
418 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { | |
1342d353 AK |
419 | ret = 0; |
420 | goto out; | |
421 | } | |
6aa8b732 | 422 | } |
c820c2aa | 423 | ret = 1; |
6aa8b732 | 424 | |
c820c2aa | 425 | memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs)); |
1342d353 | 426 | out: |
11ec2804 | 427 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 | 428 | |
1342d353 | 429 | return ret; |
6aa8b732 AK |
430 | } |
431 | ||
432 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
433 | { | |
707d92fa | 434 | if (cr0 & CR0_RESERVED_BITS) { |
6aa8b732 AK |
435 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
436 | cr0, vcpu->cr0); | |
437 | inject_gp(vcpu); | |
438 | return; | |
439 | } | |
440 | ||
707d92fa | 441 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
6aa8b732 AK |
442 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
443 | inject_gp(vcpu); | |
444 | return; | |
445 | } | |
446 | ||
707d92fa | 447 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
6aa8b732 AK |
448 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
449 | "and a clear PE flag\n"); | |
450 | inject_gp(vcpu); | |
451 | return; | |
452 | } | |
453 | ||
707d92fa | 454 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
05b3e0c2 | 455 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
456 | if ((vcpu->shadow_efer & EFER_LME)) { |
457 | int cs_db, cs_l; | |
458 | ||
459 | if (!is_pae(vcpu)) { | |
460 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
461 | "in long mode while PAE is disabled\n"); | |
462 | inject_gp(vcpu); | |
463 | return; | |
464 | } | |
cbdd1bea | 465 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
6aa8b732 AK |
466 | if (cs_l) { |
467 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
468 | "in long mode while CS.L == 1\n"); | |
469 | inject_gp(vcpu); | |
470 | return; | |
471 | ||
472 | } | |
473 | } else | |
474 | #endif | |
1342d353 | 475 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
476 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
477 | "reserved bits\n"); | |
478 | inject_gp(vcpu); | |
479 | return; | |
480 | } | |
481 | ||
482 | } | |
483 | ||
cbdd1bea | 484 | kvm_x86_ops->set_cr0(vcpu, cr0); |
6aa8b732 AK |
485 | vcpu->cr0 = cr0; |
486 | ||
11ec2804 | 487 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 | 488 | kvm_mmu_reset_context(vcpu); |
11ec2804 | 489 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
490 | return; |
491 | } | |
492 | EXPORT_SYMBOL_GPL(set_cr0); | |
493 | ||
494 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) | |
495 | { | |
496 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); | |
497 | } | |
498 | EXPORT_SYMBOL_GPL(lmsw); | |
499 | ||
500 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
501 | { | |
66aee91a | 502 | if (cr4 & CR4_RESERVED_BITS) { |
6aa8b732 AK |
503 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
504 | inject_gp(vcpu); | |
505 | return; | |
506 | } | |
507 | ||
a9058ecd | 508 | if (is_long_mode(vcpu)) { |
66aee91a | 509 | if (!(cr4 & X86_CR4_PAE)) { |
6aa8b732 AK |
510 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
511 | "in long mode\n"); | |
512 | inject_gp(vcpu); | |
513 | return; | |
514 | } | |
66aee91a | 515 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) |
1342d353 | 516 | && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
517 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
518 | inject_gp(vcpu); | |
310bc76c | 519 | return; |
6aa8b732 AK |
520 | } |
521 | ||
66aee91a | 522 | if (cr4 & X86_CR4_VMXE) { |
6aa8b732 AK |
523 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
524 | inject_gp(vcpu); | |
525 | return; | |
526 | } | |
cbdd1bea | 527 | kvm_x86_ops->set_cr4(vcpu, cr4); |
81f50e3b | 528 | vcpu->cr4 = cr4; |
11ec2804 | 529 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 | 530 | kvm_mmu_reset_context(vcpu); |
11ec2804 | 531 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
532 | } |
533 | EXPORT_SYMBOL_GPL(set_cr4); | |
534 | ||
535 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
536 | { | |
a9058ecd | 537 | if (is_long_mode(vcpu)) { |
f802a307 | 538 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
6aa8b732 AK |
539 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
540 | inject_gp(vcpu); | |
541 | return; | |
542 | } | |
543 | } else { | |
f802a307 RR |
544 | if (is_pae(vcpu)) { |
545 | if (cr3 & CR3_PAE_RESERVED_BITS) { | |
546 | printk(KERN_DEBUG | |
547 | "set_cr3: #GP, reserved bits\n"); | |
548 | inject_gp(vcpu); | |
549 | return; | |
550 | } | |
551 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { | |
552 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
553 | "reserved bits\n"); | |
554 | inject_gp(vcpu); | |
555 | return; | |
556 | } | |
557 | } else { | |
558 | if (cr3 & CR3_NONPAE_RESERVED_BITS) { | |
559 | printk(KERN_DEBUG | |
560 | "set_cr3: #GP, reserved bits\n"); | |
561 | inject_gp(vcpu); | |
562 | return; | |
563 | } | |
6aa8b732 AK |
564 | } |
565 | } | |
566 | ||
11ec2804 | 567 | mutex_lock(&vcpu->kvm->lock); |
d21225ee IM |
568 | /* |
569 | * Does the new cr3 value map to physical memory? (Note, we | |
570 | * catch an invalid cr3 even in real-mode, because it would | |
571 | * cause trouble later on when we turn on paging anyway.) | |
572 | * | |
573 | * A real CPU would silently accept an invalid cr3 and would | |
574 | * attempt to use it - with largely undefined (and often hard | |
575 | * to debug) behavior on the guest side. | |
576 | */ | |
577 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) | |
578 | inject_gp(vcpu); | |
fb764416 RR |
579 | else { |
580 | vcpu->cr3 = cr3; | |
d21225ee | 581 | vcpu->mmu.new_cr3(vcpu); |
fb764416 | 582 | } |
11ec2804 | 583 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
584 | } |
585 | EXPORT_SYMBOL_GPL(set_cr3); | |
586 | ||
587 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) | |
588 | { | |
7075bc81 | 589 | if (cr8 & CR8_RESERVED_BITS) { |
6aa8b732 AK |
590 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
591 | inject_gp(vcpu); | |
592 | return; | |
593 | } | |
97222cc8 ED |
594 | if (irqchip_in_kernel(vcpu->kvm)) |
595 | kvm_lapic_set_tpr(vcpu, cr8); | |
596 | else | |
597 | vcpu->cr8 = cr8; | |
6aa8b732 AK |
598 | } |
599 | EXPORT_SYMBOL_GPL(set_cr8); | |
600 | ||
7017fc3d ED |
601 | unsigned long get_cr8(struct kvm_vcpu *vcpu) |
602 | { | |
97222cc8 ED |
603 | if (irqchip_in_kernel(vcpu->kvm)) |
604 | return kvm_lapic_get_cr8(vcpu); | |
605 | else | |
606 | return vcpu->cr8; | |
7017fc3d ED |
607 | } |
608 | EXPORT_SYMBOL_GPL(get_cr8); | |
609 | ||
610 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) | |
611 | { | |
97222cc8 ED |
612 | if (irqchip_in_kernel(vcpu->kvm)) |
613 | return vcpu->apic_base; | |
614 | else | |
615 | return vcpu->apic_base; | |
7017fc3d ED |
616 | } |
617 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
618 | ||
619 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) | |
620 | { | |
97222cc8 ED |
621 | /* TODO: reserve bits check */ |
622 | if (irqchip_in_kernel(vcpu->kvm)) | |
623 | kvm_lapic_set_base(vcpu, data); | |
624 | else | |
625 | vcpu->apic_base = data; | |
7017fc3d ED |
626 | } |
627 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
628 | ||
6aa8b732 AK |
629 | void fx_init(struct kvm_vcpu *vcpu) |
630 | { | |
b114b080 | 631 | unsigned after_mxcsr_mask; |
6aa8b732 | 632 | |
9bd01506 RR |
633 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
634 | preempt_disable(); | |
b114b080 | 635 | fx_save(&vcpu->host_fx_image); |
6aa8b732 | 636 | fpu_init(); |
b114b080 RR |
637 | fx_save(&vcpu->guest_fx_image); |
638 | fx_restore(&vcpu->host_fx_image); | |
9bd01506 | 639 | preempt_enable(); |
6aa8b732 | 640 | |
380102c8 | 641 | vcpu->cr0 |= X86_CR0_ET; |
b114b080 RR |
642 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
643 | vcpu->guest_fx_image.mxcsr = 0x1f80; | |
644 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, | |
645 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); | |
6aa8b732 AK |
646 | } |
647 | EXPORT_SYMBOL_GPL(fx_init); | |
648 | ||
6aa8b732 AK |
649 | /* |
650 | * Allocate some memory and give it an address in the guest physical address | |
651 | * space. | |
652 | * | |
653 | * Discontiguous memory is allowed, mostly for framebuffers. | |
654 | */ | |
2c6f5df9 AK |
655 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
656 | struct kvm_memory_region *mem) | |
6aa8b732 AK |
657 | { |
658 | int r; | |
659 | gfn_t base_gfn; | |
660 | unsigned long npages; | |
661 | unsigned long i; | |
662 | struct kvm_memory_slot *memslot; | |
663 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
664 | |
665 | r = -EINVAL; | |
666 | /* General sanity checks */ | |
667 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
668 | goto out; | |
669 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
670 | goto out; | |
671 | if (mem->slot >= KVM_MEMORY_SLOTS) | |
672 | goto out; | |
673 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
674 | goto out; | |
675 | ||
676 | memslot = &kvm->memslots[mem->slot]; | |
677 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
678 | npages = mem->memory_size >> PAGE_SHIFT; | |
679 | ||
680 | if (!npages) | |
681 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
682 | ||
11ec2804 | 683 | mutex_lock(&kvm->lock); |
6aa8b732 | 684 | |
6aa8b732 AK |
685 | new = old = *memslot; |
686 | ||
687 | new.base_gfn = base_gfn; | |
688 | new.npages = npages; | |
689 | new.flags = mem->flags; | |
690 | ||
691 | /* Disallow changing a memory slot's size. */ | |
692 | r = -EINVAL; | |
693 | if (npages && old.npages && npages != old.npages) | |
694 | goto out_unlock; | |
695 | ||
696 | /* Check for overlaps */ | |
697 | r = -EEXIST; | |
698 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
699 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
700 | ||
701 | if (s == memslot) | |
702 | continue; | |
703 | if (!((base_gfn + npages <= s->base_gfn) || | |
704 | (base_gfn >= s->base_gfn + s->npages))) | |
705 | goto out_unlock; | |
706 | } | |
6aa8b732 AK |
707 | |
708 | /* Deallocate if slot is being removed */ | |
709 | if (!npages) | |
8b6d44c7 | 710 | new.phys_mem = NULL; |
6aa8b732 AK |
711 | |
712 | /* Free page dirty bitmap if unneeded */ | |
713 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 714 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
715 | |
716 | r = -ENOMEM; | |
717 | ||
718 | /* Allocate if a slot is being created */ | |
719 | if (npages && !new.phys_mem) { | |
720 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); | |
721 | ||
722 | if (!new.phys_mem) | |
0d8d2bd4 | 723 | goto out_unlock; |
6aa8b732 AK |
724 | |
725 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); | |
726 | for (i = 0; i < npages; ++i) { | |
727 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER | |
728 | | __GFP_ZERO); | |
729 | if (!new.phys_mem[i]) | |
0d8d2bd4 | 730 | goto out_unlock; |
5972e953 | 731 | set_page_private(new.phys_mem[i],0); |
6aa8b732 AK |
732 | } |
733 | } | |
734 | ||
735 | /* Allocate page dirty bitmap if needed */ | |
736 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
737 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
738 | ||
739 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
740 | if (!new.dirty_bitmap) | |
0d8d2bd4 | 741 | goto out_unlock; |
6aa8b732 AK |
742 | memset(new.dirty_bitmap, 0, dirty_bytes); |
743 | } | |
744 | ||
6aa8b732 AK |
745 | if (mem->slot >= kvm->nmemslots) |
746 | kvm->nmemslots = mem->slot + 1; | |
747 | ||
748 | *memslot = new; | |
6aa8b732 | 749 | |
90cb0529 AK |
750 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
751 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 752 | |
11ec2804 | 753 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
754 | |
755 | kvm_free_physmem_slot(&old, &new); | |
756 | return 0; | |
757 | ||
758 | out_unlock: | |
11ec2804 | 759 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
760 | kvm_free_physmem_slot(&new, &old); |
761 | out: | |
762 | return r; | |
763 | } | |
764 | ||
765 | /* | |
766 | * Get (and clear) the dirty memory log for a memory slot. | |
767 | */ | |
2c6f5df9 AK |
768 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
769 | struct kvm_dirty_log *log) | |
6aa8b732 AK |
770 | { |
771 | struct kvm_memory_slot *memslot; | |
772 | int r, i; | |
773 | int n; | |
774 | unsigned long any = 0; | |
775 | ||
11ec2804 | 776 | mutex_lock(&kvm->lock); |
6aa8b732 | 777 | |
6aa8b732 AK |
778 | r = -EINVAL; |
779 | if (log->slot >= KVM_MEMORY_SLOTS) | |
780 | goto out; | |
781 | ||
782 | memslot = &kvm->memslots[log->slot]; | |
783 | r = -ENOENT; | |
784 | if (!memslot->dirty_bitmap) | |
785 | goto out; | |
786 | ||
cd1a4a98 | 787 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 788 | |
cd1a4a98 | 789 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
790 | any = memslot->dirty_bitmap[i]; |
791 | ||
792 | r = -EFAULT; | |
793 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
794 | goto out; | |
795 | ||
39214915 RR |
796 | /* If nothing is dirty, don't bother messing with page tables. */ |
797 | if (any) { | |
39214915 RR |
798 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
799 | kvm_flush_remote_tlbs(kvm); | |
800 | memset(memslot->dirty_bitmap, 0, n); | |
39214915 | 801 | } |
6aa8b732 AK |
802 | |
803 | r = 0; | |
804 | ||
805 | out: | |
11ec2804 | 806 | mutex_unlock(&kvm->lock); |
6aa8b732 AK |
807 | return r; |
808 | } | |
809 | ||
e8207547 AK |
810 | /* |
811 | * Set a new alias region. Aliases map a portion of physical memory into | |
812 | * another portion. This is useful for memory windows, for example the PC | |
813 | * VGA region. | |
814 | */ | |
815 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, | |
816 | struct kvm_memory_alias *alias) | |
817 | { | |
818 | int r, n; | |
819 | struct kvm_mem_alias *p; | |
820 | ||
821 | r = -EINVAL; | |
822 | /* General sanity checks */ | |
823 | if (alias->memory_size & (PAGE_SIZE - 1)) | |
824 | goto out; | |
825 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) | |
826 | goto out; | |
827 | if (alias->slot >= KVM_ALIAS_SLOTS) | |
828 | goto out; | |
829 | if (alias->guest_phys_addr + alias->memory_size | |
830 | < alias->guest_phys_addr) | |
831 | goto out; | |
832 | if (alias->target_phys_addr + alias->memory_size | |
833 | < alias->target_phys_addr) | |
834 | goto out; | |
835 | ||
11ec2804 | 836 | mutex_lock(&kvm->lock); |
e8207547 AK |
837 | |
838 | p = &kvm->aliases[alias->slot]; | |
839 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; | |
840 | p->npages = alias->memory_size >> PAGE_SHIFT; | |
841 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; | |
842 | ||
843 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) | |
844 | if (kvm->aliases[n - 1].npages) | |
845 | break; | |
846 | kvm->naliases = n; | |
847 | ||
90cb0529 | 848 | kvm_mmu_zap_all(kvm); |
e8207547 | 849 | |
11ec2804 | 850 | mutex_unlock(&kvm->lock); |
e8207547 AK |
851 | |
852 | return 0; | |
853 | ||
854 | out: | |
855 | return r; | |
856 | } | |
857 | ||
6ceb9d79 HQ |
858 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
859 | { | |
860 | int r; | |
861 | ||
862 | r = 0; | |
863 | switch (chip->chip_id) { | |
864 | case KVM_IRQCHIP_PIC_MASTER: | |
865 | memcpy (&chip->chip.pic, | |
866 | &pic_irqchip(kvm)->pics[0], | |
867 | sizeof(struct kvm_pic_state)); | |
868 | break; | |
869 | case KVM_IRQCHIP_PIC_SLAVE: | |
870 | memcpy (&chip->chip.pic, | |
871 | &pic_irqchip(kvm)->pics[1], | |
872 | sizeof(struct kvm_pic_state)); | |
873 | break; | |
6bf9e962 HQ |
874 | case KVM_IRQCHIP_IOAPIC: |
875 | memcpy (&chip->chip.ioapic, | |
876 | ioapic_irqchip(kvm), | |
877 | sizeof(struct kvm_ioapic_state)); | |
878 | break; | |
6ceb9d79 HQ |
879 | default: |
880 | r = -EINVAL; | |
881 | break; | |
882 | } | |
883 | return r; | |
884 | } | |
885 | ||
886 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
887 | { | |
888 | int r; | |
889 | ||
890 | r = 0; | |
891 | switch (chip->chip_id) { | |
892 | case KVM_IRQCHIP_PIC_MASTER: | |
893 | memcpy (&pic_irqchip(kvm)->pics[0], | |
894 | &chip->chip.pic, | |
895 | sizeof(struct kvm_pic_state)); | |
896 | break; | |
897 | case KVM_IRQCHIP_PIC_SLAVE: | |
898 | memcpy (&pic_irqchip(kvm)->pics[1], | |
899 | &chip->chip.pic, | |
900 | sizeof(struct kvm_pic_state)); | |
901 | break; | |
6bf9e962 HQ |
902 | case KVM_IRQCHIP_IOAPIC: |
903 | memcpy (ioapic_irqchip(kvm), | |
904 | &chip->chip.ioapic, | |
905 | sizeof(struct kvm_ioapic_state)); | |
906 | break; | |
6ceb9d79 HQ |
907 | default: |
908 | r = -EINVAL; | |
909 | break; | |
910 | } | |
911 | kvm_pic_update_irq(pic_irqchip(kvm)); | |
912 | return r; | |
913 | } | |
914 | ||
e8207547 AK |
915 | static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
916 | { | |
917 | int i; | |
918 | struct kvm_mem_alias *alias; | |
919 | ||
920 | for (i = 0; i < kvm->naliases; ++i) { | |
921 | alias = &kvm->aliases[i]; | |
922 | if (gfn >= alias->base_gfn | |
923 | && gfn < alias->base_gfn + alias->npages) | |
924 | return alias->target_gfn + gfn - alias->base_gfn; | |
925 | } | |
926 | return gfn; | |
927 | } | |
928 | ||
929 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
6aa8b732 AK |
930 | { |
931 | int i; | |
932 | ||
933 | for (i = 0; i < kvm->nmemslots; ++i) { | |
934 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
935 | ||
936 | if (gfn >= memslot->base_gfn | |
937 | && gfn < memslot->base_gfn + memslot->npages) | |
938 | return memslot; | |
939 | } | |
8b6d44c7 | 940 | return NULL; |
6aa8b732 | 941 | } |
e8207547 AK |
942 | |
943 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
944 | { | |
945 | gfn = unalias_gfn(kvm, gfn); | |
946 | return __gfn_to_memslot(kvm, gfn); | |
947 | } | |
6aa8b732 | 948 | |
954bbbc2 AK |
949 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
950 | { | |
951 | struct kvm_memory_slot *slot; | |
952 | ||
e8207547 AK |
953 | gfn = unalias_gfn(kvm, gfn); |
954 | slot = __gfn_to_memslot(kvm, gfn); | |
954bbbc2 AK |
955 | if (!slot) |
956 | return NULL; | |
957 | return slot->phys_mem[gfn - slot->base_gfn]; | |
958 | } | |
959 | EXPORT_SYMBOL_GPL(gfn_to_page); | |
960 | ||
7e9d619d | 961 | /* WARNING: Does not work on aliased pages. */ |
6aa8b732 AK |
962 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
963 | { | |
31389947 | 964 | struct kvm_memory_slot *memslot; |
6aa8b732 | 965 | |
7e9d619d RR |
966 | memslot = __gfn_to_memslot(kvm, gfn); |
967 | if (memslot && memslot->dirty_bitmap) { | |
968 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 969 | |
7e9d619d RR |
970 | /* avoid RMW */ |
971 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
972 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
973 | } |
974 | } | |
975 | ||
e7d5d76c | 976 | int emulator_read_std(unsigned long addr, |
4c690a1e | 977 | void *val, |
6aa8b732 | 978 | unsigned int bytes, |
cebff02b | 979 | struct kvm_vcpu *vcpu) |
6aa8b732 | 980 | { |
6aa8b732 AK |
981 | void *data = val; |
982 | ||
983 | while (bytes) { | |
984 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
985 | unsigned offset = addr & (PAGE_SIZE-1); | |
986 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
987 | unsigned long pfn; | |
954bbbc2 AK |
988 | struct page *page; |
989 | void *page_virt; | |
6aa8b732 AK |
990 | |
991 | if (gpa == UNMAPPED_GVA) | |
992 | return X86EMUL_PROPAGATE_FAULT; | |
993 | pfn = gpa >> PAGE_SHIFT; | |
954bbbc2 AK |
994 | page = gfn_to_page(vcpu->kvm, pfn); |
995 | if (!page) | |
6aa8b732 | 996 | return X86EMUL_UNHANDLEABLE; |
954bbbc2 | 997 | page_virt = kmap_atomic(page, KM_USER0); |
6aa8b732 | 998 | |
954bbbc2 | 999 | memcpy(data, page_virt + offset, tocopy); |
6aa8b732 | 1000 | |
954bbbc2 | 1001 | kunmap_atomic(page_virt, KM_USER0); |
6aa8b732 AK |
1002 | |
1003 | bytes -= tocopy; | |
1004 | data += tocopy; | |
1005 | addr += tocopy; | |
1006 | } | |
1007 | ||
1008 | return X86EMUL_CONTINUE; | |
1009 | } | |
e7d5d76c | 1010 | EXPORT_SYMBOL_GPL(emulator_read_std); |
6aa8b732 AK |
1011 | |
1012 | static int emulator_write_std(unsigned long addr, | |
4c690a1e | 1013 | const void *val, |
6aa8b732 | 1014 | unsigned int bytes, |
cebff02b | 1015 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1016 | { |
f0242478 | 1017 | pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes); |
6aa8b732 AK |
1018 | return X86EMUL_UNHANDLEABLE; |
1019 | } | |
1020 | ||
97222cc8 ED |
1021 | /* |
1022 | * Only apic need an MMIO device hook, so shortcut now.. | |
1023 | */ | |
1024 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
1025 | gpa_t addr) | |
1026 | { | |
1027 | struct kvm_io_device *dev; | |
1028 | ||
1029 | if (vcpu->apic) { | |
1030 | dev = &vcpu->apic->dev; | |
1031 | if (dev->in_range(dev, addr)) | |
1032 | return dev; | |
1033 | } | |
1034 | return NULL; | |
1035 | } | |
1036 | ||
2eeb2e94 GH |
1037 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
1038 | gpa_t addr) | |
1039 | { | |
97222cc8 ED |
1040 | struct kvm_io_device *dev; |
1041 | ||
1042 | dev = vcpu_find_pervcpu_dev(vcpu, addr); | |
1043 | if (dev == NULL) | |
1044 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); | |
1045 | return dev; | |
2eeb2e94 GH |
1046 | } |
1047 | ||
74906345 ED |
1048 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
1049 | gpa_t addr) | |
1050 | { | |
1051 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); | |
1052 | } | |
1053 | ||
6aa8b732 | 1054 | static int emulator_read_emulated(unsigned long addr, |
4c690a1e | 1055 | void *val, |
6aa8b732 | 1056 | unsigned int bytes, |
cebff02b | 1057 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1058 | { |
2eeb2e94 GH |
1059 | struct kvm_io_device *mmio_dev; |
1060 | gpa_t gpa; | |
6aa8b732 AK |
1061 | |
1062 | if (vcpu->mmio_read_completed) { | |
1063 | memcpy(val, vcpu->mmio_data, bytes); | |
1064 | vcpu->mmio_read_completed = 0; | |
1065 | return X86EMUL_CONTINUE; | |
cebff02b | 1066 | } else if (emulator_read_std(addr, val, bytes, vcpu) |
6aa8b732 AK |
1067 | == X86EMUL_CONTINUE) |
1068 | return X86EMUL_CONTINUE; | |
d27d4aca | 1069 | |
2eeb2e94 GH |
1070 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); |
1071 | if (gpa == UNMAPPED_GVA) | |
1072 | return X86EMUL_PROPAGATE_FAULT; | |
6aa8b732 | 1073 | |
2eeb2e94 GH |
1074 | /* |
1075 | * Is this MMIO handled locally? | |
1076 | */ | |
1077 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1078 | if (mmio_dev) { | |
1079 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
1080 | return X86EMUL_CONTINUE; | |
6aa8b732 | 1081 | } |
2eeb2e94 GH |
1082 | |
1083 | vcpu->mmio_needed = 1; | |
1084 | vcpu->mmio_phys_addr = gpa; | |
1085 | vcpu->mmio_size = bytes; | |
1086 | vcpu->mmio_is_write = 0; | |
1087 | ||
1088 | return X86EMUL_UNHANDLEABLE; | |
6aa8b732 AK |
1089 | } |
1090 | ||
da4a00f0 | 1091 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
4c690a1e | 1092 | const void *val, int bytes) |
da4a00f0 | 1093 | { |
da4a00f0 AK |
1094 | struct page *page; |
1095 | void *virt; | |
1096 | ||
1097 | if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT)) | |
1098 | return 0; | |
954bbbc2 AK |
1099 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
1100 | if (!page) | |
da4a00f0 | 1101 | return 0; |
ab51a434 | 1102 | mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT); |
da4a00f0 | 1103 | virt = kmap_atomic(page, KM_USER0); |
fe551881 | 1104 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); |
7cfa4b0a | 1105 | memcpy(virt + offset_in_page(gpa), val, bytes); |
da4a00f0 | 1106 | kunmap_atomic(virt, KM_USER0); |
da4a00f0 AK |
1107 | return 1; |
1108 | } | |
1109 | ||
b0fcd903 AK |
1110 | static int emulator_write_emulated_onepage(unsigned long addr, |
1111 | const void *val, | |
1112 | unsigned int bytes, | |
cebff02b | 1113 | struct kvm_vcpu *vcpu) |
6aa8b732 | 1114 | { |
2eeb2e94 GH |
1115 | struct kvm_io_device *mmio_dev; |
1116 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
6aa8b732 | 1117 | |
c9047f53 | 1118 | if (gpa == UNMAPPED_GVA) { |
cbdd1bea | 1119 | kvm_x86_ops->inject_page_fault(vcpu, addr, 2); |
6aa8b732 | 1120 | return X86EMUL_PROPAGATE_FAULT; |
c9047f53 | 1121 | } |
6aa8b732 | 1122 | |
da4a00f0 AK |
1123 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
1124 | return X86EMUL_CONTINUE; | |
1125 | ||
2eeb2e94 GH |
1126 | /* |
1127 | * Is this MMIO handled locally? | |
1128 | */ | |
1129 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1130 | if (mmio_dev) { | |
1131 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
1132 | return X86EMUL_CONTINUE; | |
1133 | } | |
1134 | ||
6aa8b732 AK |
1135 | vcpu->mmio_needed = 1; |
1136 | vcpu->mmio_phys_addr = gpa; | |
1137 | vcpu->mmio_size = bytes; | |
1138 | vcpu->mmio_is_write = 1; | |
4c690a1e | 1139 | memcpy(vcpu->mmio_data, val, bytes); |
6aa8b732 AK |
1140 | |
1141 | return X86EMUL_CONTINUE; | |
1142 | } | |
1143 | ||
e7d5d76c | 1144 | int emulator_write_emulated(unsigned long addr, |
b0fcd903 AK |
1145 | const void *val, |
1146 | unsigned int bytes, | |
cebff02b | 1147 | struct kvm_vcpu *vcpu) |
b0fcd903 AK |
1148 | { |
1149 | /* Crossing a page boundary? */ | |
1150 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
1151 | int rc, now; | |
1152 | ||
1153 | now = -addr & ~PAGE_MASK; | |
cebff02b | 1154 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
b0fcd903 AK |
1155 | if (rc != X86EMUL_CONTINUE) |
1156 | return rc; | |
1157 | addr += now; | |
1158 | val += now; | |
1159 | bytes -= now; | |
1160 | } | |
cebff02b | 1161 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
b0fcd903 | 1162 | } |
e7d5d76c | 1163 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
b0fcd903 | 1164 | |
6aa8b732 | 1165 | static int emulator_cmpxchg_emulated(unsigned long addr, |
4c690a1e AK |
1166 | const void *old, |
1167 | const void *new, | |
6aa8b732 | 1168 | unsigned int bytes, |
cebff02b | 1169 | struct kvm_vcpu *vcpu) |
6aa8b732 AK |
1170 | { |
1171 | static int reported; | |
1172 | ||
1173 | if (!reported) { | |
1174 | reported = 1; | |
1175 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
1176 | } | |
cebff02b | 1177 | return emulator_write_emulated(addr, new, bytes, vcpu); |
6aa8b732 AK |
1178 | } |
1179 | ||
1180 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1181 | { | |
cbdd1bea | 1182 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
6aa8b732 AK |
1183 | } |
1184 | ||
1185 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
1186 | { | |
6aa8b732 AK |
1187 | return X86EMUL_CONTINUE; |
1188 | } | |
1189 | ||
1190 | int emulate_clts(struct kvm_vcpu *vcpu) | |
1191 | { | |
404fb881 | 1192 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS); |
6aa8b732 AK |
1193 | return X86EMUL_CONTINUE; |
1194 | } | |
1195 | ||
1196 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) | |
1197 | { | |
1198 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
1199 | ||
1200 | switch (dr) { | |
1201 | case 0 ... 3: | |
cbdd1bea | 1202 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
6aa8b732 AK |
1203 | return X86EMUL_CONTINUE; |
1204 | default: | |
f0242478 | 1205 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); |
6aa8b732 AK |
1206 | return X86EMUL_UNHANDLEABLE; |
1207 | } | |
1208 | } | |
1209 | ||
1210 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
1211 | { | |
1212 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
1213 | int exception; | |
1214 | ||
cbdd1bea | 1215 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
6aa8b732 AK |
1216 | if (exception) { |
1217 | /* FIXME: better handling */ | |
1218 | return X86EMUL_UNHANDLEABLE; | |
1219 | } | |
1220 | return X86EMUL_CONTINUE; | |
1221 | } | |
1222 | ||
054b1369 | 1223 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
6aa8b732 AK |
1224 | { |
1225 | static int reported; | |
1226 | u8 opcodes[4]; | |
054b1369 | 1227 | unsigned long rip = vcpu->rip; |
6aa8b732 AK |
1228 | unsigned long rip_linear; |
1229 | ||
054b1369 | 1230 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
6aa8b732 AK |
1231 | |
1232 | if (reported) | |
1233 | return; | |
1234 | ||
054b1369 | 1235 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
6aa8b732 | 1236 | |
054b1369 AK |
1237 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
1238 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
6aa8b732 AK |
1239 | reported = 1; |
1240 | } | |
054b1369 | 1241 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
6aa8b732 AK |
1242 | |
1243 | struct x86_emulate_ops emulate_ops = { | |
1244 | .read_std = emulator_read_std, | |
1245 | .write_std = emulator_write_std, | |
1246 | .read_emulated = emulator_read_emulated, | |
1247 | .write_emulated = emulator_write_emulated, | |
1248 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
1249 | }; | |
1250 | ||
1251 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
1252 | struct kvm_run *run, | |
1253 | unsigned long cr2, | |
3427318f LV |
1254 | u16 error_code, |
1255 | int no_decode) | |
6aa8b732 | 1256 | { |
3427318f | 1257 | int r = 0; |
6aa8b732 | 1258 | |
e7df56e4 | 1259 | vcpu->mmio_fault_cr2 = cr2; |
cbdd1bea | 1260 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 | 1261 | |
6aa8b732 | 1262 | vcpu->mmio_is_write = 0; |
e70669ab | 1263 | vcpu->pio.string = 0; |
3427318f LV |
1264 | |
1265 | if (!no_decode) { | |
1266 | int cs_db, cs_l; | |
1267 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
1268 | ||
1269 | vcpu->emulate_ctxt.vcpu = vcpu; | |
1270 | vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); | |
1271 | vcpu->emulate_ctxt.cr2 = cr2; | |
1272 | vcpu->emulate_ctxt.mode = | |
1273 | (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM) | |
1274 | ? X86EMUL_MODE_REAL : cs_l | |
1275 | ? X86EMUL_MODE_PROT64 : cs_db | |
1276 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
1277 | ||
1278 | if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
1279 | vcpu->emulate_ctxt.cs_base = 0; | |
1280 | vcpu->emulate_ctxt.ds_base = 0; | |
1281 | vcpu->emulate_ctxt.es_base = 0; | |
1282 | vcpu->emulate_ctxt.ss_base = 0; | |
1283 | } else { | |
1284 | vcpu->emulate_ctxt.cs_base = | |
1285 | get_segment_base(vcpu, VCPU_SREG_CS); | |
1286 | vcpu->emulate_ctxt.ds_base = | |
1287 | get_segment_base(vcpu, VCPU_SREG_DS); | |
1288 | vcpu->emulate_ctxt.es_base = | |
1289 | get_segment_base(vcpu, VCPU_SREG_ES); | |
1290 | vcpu->emulate_ctxt.ss_base = | |
1291 | get_segment_base(vcpu, VCPU_SREG_SS); | |
1292 | } | |
1293 | ||
1294 | vcpu->emulate_ctxt.gs_base = | |
1295 | get_segment_base(vcpu, VCPU_SREG_GS); | |
1296 | vcpu->emulate_ctxt.fs_base = | |
1297 | get_segment_base(vcpu, VCPU_SREG_FS); | |
1298 | ||
1299 | r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops); | |
1300 | } | |
1301 | ||
1be3aa47 | 1302 | if (r == 0) |
3427318f | 1303 | r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops); |
1be3aa47 | 1304 | |
e70669ab LV |
1305 | if (vcpu->pio.string) |
1306 | return EMULATE_DO_MMIO; | |
6aa8b732 AK |
1307 | |
1308 | if ((r || vcpu->mmio_is_write) && run) { | |
8fc0d085 | 1309 | run->exit_reason = KVM_EXIT_MMIO; |
6aa8b732 AK |
1310 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
1311 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1312 | run->mmio.len = vcpu->mmio_size; | |
1313 | run->mmio.is_write = vcpu->mmio_is_write; | |
1314 | } | |
1315 | ||
1316 | if (r) { | |
a436036b AK |
1317 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
1318 | return EMULATE_DONE; | |
6aa8b732 | 1319 | if (!vcpu->mmio_needed) { |
054b1369 | 1320 | kvm_report_emulation_failure(vcpu, "mmio"); |
6aa8b732 AK |
1321 | return EMULATE_FAIL; |
1322 | } | |
1323 | return EMULATE_DO_MMIO; | |
1324 | } | |
1325 | ||
cbdd1bea | 1326 | kvm_x86_ops->decache_regs(vcpu); |
3427318f | 1327 | kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags); |
6aa8b732 | 1328 | |
02c83209 AK |
1329 | if (vcpu->mmio_is_write) { |
1330 | vcpu->mmio_needed = 0; | |
6aa8b732 | 1331 | return EMULATE_DO_MMIO; |
02c83209 | 1332 | } |
6aa8b732 AK |
1333 | |
1334 | return EMULATE_DONE; | |
1335 | } | |
1336 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1337 | ||
b6958ce4 ED |
1338 | /* |
1339 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
1340 | */ | |
c5ec1534 | 1341 | static void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 1342 | { |
b6958ce4 ED |
1343 | DECLARE_WAITQUEUE(wait, current); |
1344 | ||
1345 | add_wait_queue(&vcpu->wq, &wait); | |
1346 | ||
1347 | /* | |
1348 | * We will block until either an interrupt or a signal wakes us up | |
1349 | */ | |
c5ec1534 HQ |
1350 | while (!kvm_cpu_has_interrupt(vcpu) |
1351 | && !signal_pending(current) | |
1352 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
1353 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
1354 | set_current_state(TASK_INTERRUPTIBLE); |
1355 | vcpu_put(vcpu); | |
1356 | schedule(); | |
1357 | vcpu_load(vcpu); | |
1358 | } | |
d3bef15f | 1359 | |
c5ec1534 | 1360 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 1361 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
1362 | } |
1363 | ||
1364 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
1365 | { | |
d3bef15f | 1366 | ++vcpu->stat.halt_exits; |
b6958ce4 | 1367 | if (irqchip_in_kernel(vcpu->kvm)) { |
c5ec1534 HQ |
1368 | vcpu->mp_state = VCPU_MP_STATE_HALTED; |
1369 | kvm_vcpu_block(vcpu); | |
1370 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) | |
1371 | return -EINTR; | |
b6958ce4 ED |
1372 | return 1; |
1373 | } else { | |
1374 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
1375 | return 0; | |
1376 | } | |
d3bef15f AK |
1377 | } |
1378 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
1379 | ||
7aa81cc0 | 1380 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
270fd9b9 | 1381 | { |
7aa81cc0 | 1382 | unsigned long nr, a0, a1, a2, a3, ret; |
270fd9b9 | 1383 | |
cbdd1bea | 1384 | kvm_x86_ops->cache_regs(vcpu); |
7aa81cc0 AL |
1385 | |
1386 | nr = vcpu->regs[VCPU_REGS_RAX]; | |
1387 | a0 = vcpu->regs[VCPU_REGS_RBX]; | |
1388 | a1 = vcpu->regs[VCPU_REGS_RCX]; | |
1389 | a2 = vcpu->regs[VCPU_REGS_RDX]; | |
1390 | a3 = vcpu->regs[VCPU_REGS_RSI]; | |
1391 | ||
1392 | if (!is_long_mode(vcpu)) { | |
1393 | nr &= 0xFFFFFFFF; | |
1394 | a0 &= 0xFFFFFFFF; | |
1395 | a1 &= 0xFFFFFFFF; | |
1396 | a2 &= 0xFFFFFFFF; | |
1397 | a3 &= 0xFFFFFFFF; | |
270fd9b9 | 1398 | } |
7aa81cc0 | 1399 | |
270fd9b9 AK |
1400 | switch (nr) { |
1401 | default: | |
7aa81cc0 AL |
1402 | ret = -KVM_ENOSYS; |
1403 | break; | |
270fd9b9 AK |
1404 | } |
1405 | vcpu->regs[VCPU_REGS_RAX] = ret; | |
cbdd1bea | 1406 | kvm_x86_ops->decache_regs(vcpu); |
7aa81cc0 AL |
1407 | return 0; |
1408 | } | |
1409 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
1410 | ||
1411 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) | |
1412 | { | |
1413 | char instruction[3]; | |
1414 | int ret = 0; | |
1415 | ||
1416 | mutex_lock(&vcpu->kvm->lock); | |
1417 | ||
1418 | /* | |
1419 | * Blow out the MMU to ensure that no other VCPU has an active mapping | |
1420 | * to ensure that the updated hypercall appears atomically across all | |
1421 | * VCPUs. | |
1422 | */ | |
1423 | kvm_mmu_zap_all(vcpu->kvm); | |
1424 | ||
1425 | kvm_x86_ops->cache_regs(vcpu); | |
1426 | kvm_x86_ops->patch_hypercall(vcpu, instruction); | |
1427 | if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu) | |
1428 | != X86EMUL_CONTINUE) | |
1429 | ret = -EFAULT; | |
1430 | ||
1431 | mutex_unlock(&vcpu->kvm->lock); | |
1432 | ||
1433 | return ret; | |
270fd9b9 | 1434 | } |
270fd9b9 | 1435 | |
6aa8b732 AK |
1436 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
1437 | { | |
1438 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
1439 | } | |
1440 | ||
1441 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1442 | { | |
1443 | struct descriptor_table dt = { limit, base }; | |
1444 | ||
cbdd1bea | 1445 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
1446 | } |
1447 | ||
1448 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1449 | { | |
1450 | struct descriptor_table dt = { limit, base }; | |
1451 | ||
cbdd1bea | 1452 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
1453 | } |
1454 | ||
1455 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
1456 | unsigned long *rflags) | |
1457 | { | |
1458 | lmsw(vcpu, msw); | |
cbdd1bea | 1459 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1460 | } |
1461 | ||
1462 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
1463 | { | |
cbdd1bea | 1464 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1465 | switch (cr) { |
1466 | case 0: | |
1467 | return vcpu->cr0; | |
1468 | case 2: | |
1469 | return vcpu->cr2; | |
1470 | case 3: | |
1471 | return vcpu->cr3; | |
1472 | case 4: | |
1473 | return vcpu->cr4; | |
1474 | default: | |
1475 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1476 | return 0; | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
1481 | unsigned long *rflags) | |
1482 | { | |
1483 | switch (cr) { | |
1484 | case 0: | |
1485 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
cbdd1bea | 1486 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
1487 | break; |
1488 | case 2: | |
1489 | vcpu->cr2 = val; | |
1490 | break; | |
1491 | case 3: | |
1492 | set_cr3(vcpu, val); | |
1493 | break; | |
1494 | case 4: | |
1495 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
1496 | break; | |
1497 | default: | |
1498 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1499 | } | |
1500 | } | |
1501 | ||
3bab1f5d AK |
1502 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
1503 | { | |
1504 | u64 data; | |
1505 | ||
1506 | switch (msr) { | |
1507 | case 0xc0010010: /* SYSCFG */ | |
1508 | case 0xc0010015: /* HWCR */ | |
1509 | case MSR_IA32_PLATFORM_ID: | |
1510 | case MSR_IA32_P5_MC_ADDR: | |
1511 | case MSR_IA32_P5_MC_TYPE: | |
1512 | case MSR_IA32_MC0_CTL: | |
1513 | case MSR_IA32_MCG_STATUS: | |
1514 | case MSR_IA32_MCG_CAP: | |
1515 | case MSR_IA32_MC0_MISC: | |
1516 | case MSR_IA32_MC0_MISC+4: | |
1517 | case MSR_IA32_MC0_MISC+8: | |
1518 | case MSR_IA32_MC0_MISC+12: | |
1519 | case MSR_IA32_MC0_MISC+16: | |
1520 | case MSR_IA32_UCODE_REV: | |
a8d13ea2 | 1521 | case MSR_IA32_PERF_STATUS: |
2dc7094b | 1522 | case MSR_IA32_EBL_CR_POWERON: |
3bab1f5d AK |
1523 | /* MTRR registers */ |
1524 | case 0xfe: | |
1525 | case 0x200 ... 0x2ff: | |
1526 | data = 0; | |
1527 | break; | |
a8d13ea2 AK |
1528 | case 0xcd: /* fsb frequency */ |
1529 | data = 3; | |
1530 | break; | |
3bab1f5d | 1531 | case MSR_IA32_APICBASE: |
7017fc3d | 1532 | data = kvm_get_apic_base(vcpu); |
3bab1f5d | 1533 | break; |
6f00e68f AK |
1534 | case MSR_IA32_MISC_ENABLE: |
1535 | data = vcpu->ia32_misc_enable_msr; | |
1536 | break; | |
3bab1f5d AK |
1537 | #ifdef CONFIG_X86_64 |
1538 | case MSR_EFER: | |
1539 | data = vcpu->shadow_efer; | |
1540 | break; | |
1541 | #endif | |
1542 | default: | |
f0242478 | 1543 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
3bab1f5d AK |
1544 | return 1; |
1545 | } | |
1546 | *pdata = data; | |
1547 | return 0; | |
1548 | } | |
1549 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
1550 | ||
6aa8b732 AK |
1551 | /* |
1552 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
1553 | * Returns 0 on success, non-0 otherwise. | |
1554 | * Assumes vcpu_load() was already called. | |
1555 | */ | |
35f3f286 | 1556 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
6aa8b732 | 1557 | { |
cbdd1bea | 1558 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
6aa8b732 AK |
1559 | } |
1560 | ||
05b3e0c2 | 1561 | #ifdef CONFIG_X86_64 |
6aa8b732 | 1562 | |
3bab1f5d | 1563 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
6aa8b732 | 1564 | { |
6aa8b732 AK |
1565 | if (efer & EFER_RESERVED_BITS) { |
1566 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
1567 | efer); | |
1568 | inject_gp(vcpu); | |
1569 | return; | |
1570 | } | |
1571 | ||
1572 | if (is_paging(vcpu) | |
1573 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
1574 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
1575 | inject_gp(vcpu); | |
1576 | return; | |
1577 | } | |
1578 | ||
cbdd1bea | 1579 | kvm_x86_ops->set_efer(vcpu, efer); |
7725f0ba | 1580 | |
6aa8b732 AK |
1581 | efer &= ~EFER_LMA; |
1582 | efer |= vcpu->shadow_efer & EFER_LMA; | |
1583 | ||
1584 | vcpu->shadow_efer = efer; | |
6aa8b732 | 1585 | } |
6aa8b732 AK |
1586 | |
1587 | #endif | |
1588 | ||
3bab1f5d AK |
1589 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
1590 | { | |
1591 | switch (msr) { | |
1592 | #ifdef CONFIG_X86_64 | |
1593 | case MSR_EFER: | |
1594 | set_efer(vcpu, data); | |
1595 | break; | |
1596 | #endif | |
1597 | case MSR_IA32_MC0_STATUS: | |
f0242478 | 1598 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
3bab1f5d AK |
1599 | __FUNCTION__, data); |
1600 | break; | |
0e5bf0d0 | 1601 | case MSR_IA32_MCG_STATUS: |
f0242478 | 1602 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
0e5bf0d0 SK |
1603 | __FUNCTION__, data); |
1604 | break; | |
3bab1f5d AK |
1605 | case MSR_IA32_UCODE_REV: |
1606 | case MSR_IA32_UCODE_WRITE: | |
1607 | case 0x200 ... 0x2ff: /* MTRRs */ | |
1608 | break; | |
1609 | case MSR_IA32_APICBASE: | |
7017fc3d | 1610 | kvm_set_apic_base(vcpu, data); |
3bab1f5d | 1611 | break; |
6f00e68f AK |
1612 | case MSR_IA32_MISC_ENABLE: |
1613 | vcpu->ia32_misc_enable_msr = data; | |
1614 | break; | |
3bab1f5d | 1615 | default: |
f0242478 | 1616 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); |
3bab1f5d AK |
1617 | return 1; |
1618 | } | |
1619 | return 0; | |
1620 | } | |
1621 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
1622 | ||
6aa8b732 AK |
1623 | /* |
1624 | * Writes msr value into into the appropriate "register". | |
1625 | * Returns 0 on success, non-0 otherwise. | |
1626 | * Assumes vcpu_load() was already called. | |
1627 | */ | |
35f3f286 | 1628 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
6aa8b732 | 1629 | { |
cbdd1bea | 1630 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
6aa8b732 AK |
1631 | } |
1632 | ||
1633 | void kvm_resched(struct kvm_vcpu *vcpu) | |
1634 | { | |
3fca0365 YD |
1635 | if (!need_resched()) |
1636 | return; | |
6aa8b732 | 1637 | cond_resched(); |
6aa8b732 AK |
1638 | } |
1639 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1640 | ||
06465c5a AK |
1641 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
1642 | { | |
1643 | int i; | |
1644 | u32 function; | |
1645 | struct kvm_cpuid_entry *e, *best; | |
1646 | ||
cbdd1bea | 1647 | kvm_x86_ops->cache_regs(vcpu); |
06465c5a AK |
1648 | function = vcpu->regs[VCPU_REGS_RAX]; |
1649 | vcpu->regs[VCPU_REGS_RAX] = 0; | |
1650 | vcpu->regs[VCPU_REGS_RBX] = 0; | |
1651 | vcpu->regs[VCPU_REGS_RCX] = 0; | |
1652 | vcpu->regs[VCPU_REGS_RDX] = 0; | |
1653 | best = NULL; | |
1654 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
1655 | e = &vcpu->cpuid_entries[i]; | |
1656 | if (e->function == function) { | |
1657 | best = e; | |
1658 | break; | |
1659 | } | |
1660 | /* | |
1661 | * Both basic or both extended? | |
1662 | */ | |
1663 | if (((e->function ^ function) & 0x80000000) == 0) | |
1664 | if (!best || e->function > best->function) | |
1665 | best = e; | |
1666 | } | |
1667 | if (best) { | |
1668 | vcpu->regs[VCPU_REGS_RAX] = best->eax; | |
1669 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; | |
1670 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; | |
1671 | vcpu->regs[VCPU_REGS_RDX] = best->edx; | |
1672 | } | |
cbdd1bea CE |
1673 | kvm_x86_ops->decache_regs(vcpu); |
1674 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
06465c5a AK |
1675 | } |
1676 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
1677 | ||
039576c0 | 1678 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
46fc1477 | 1679 | { |
039576c0 AK |
1680 | void *p = vcpu->pio_data; |
1681 | void *q; | |
1682 | unsigned bytes; | |
1683 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; | |
1684 | ||
039576c0 AK |
1685 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
1686 | PAGE_KERNEL); | |
1687 | if (!q) { | |
039576c0 AK |
1688 | free_pio_guest_pages(vcpu); |
1689 | return -ENOMEM; | |
1690 | } | |
1691 | q += vcpu->pio.guest_page_offset; | |
1692 | bytes = vcpu->pio.size * vcpu->pio.cur_count; | |
1693 | if (vcpu->pio.in) | |
1694 | memcpy(q, p, bytes); | |
1695 | else | |
1696 | memcpy(p, q, bytes); | |
1697 | q -= vcpu->pio.guest_page_offset; | |
1698 | vunmap(q); | |
039576c0 AK |
1699 | free_pio_guest_pages(vcpu); |
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | static int complete_pio(struct kvm_vcpu *vcpu) | |
1704 | { | |
1705 | struct kvm_pio_request *io = &vcpu->pio; | |
46fc1477 | 1706 | long delta; |
039576c0 | 1707 | int r; |
46fc1477 | 1708 | |
cbdd1bea | 1709 | kvm_x86_ops->cache_regs(vcpu); |
46fc1477 AK |
1710 | |
1711 | if (!io->string) { | |
039576c0 AK |
1712 | if (io->in) |
1713 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, | |
46fc1477 AK |
1714 | io->size); |
1715 | } else { | |
039576c0 AK |
1716 | if (io->in) { |
1717 | r = pio_copy_data(vcpu); | |
1718 | if (r) { | |
cbdd1bea | 1719 | kvm_x86_ops->cache_regs(vcpu); |
039576c0 AK |
1720 | return r; |
1721 | } | |
1722 | } | |
1723 | ||
46fc1477 AK |
1724 | delta = 1; |
1725 | if (io->rep) { | |
039576c0 | 1726 | delta *= io->cur_count; |
46fc1477 AK |
1727 | /* |
1728 | * The size of the register should really depend on | |
1729 | * current address size. | |
1730 | */ | |
1731 | vcpu->regs[VCPU_REGS_RCX] -= delta; | |
1732 | } | |
039576c0 | 1733 | if (io->down) |
46fc1477 AK |
1734 | delta = -delta; |
1735 | delta *= io->size; | |
039576c0 | 1736 | if (io->in) |
46fc1477 AK |
1737 | vcpu->regs[VCPU_REGS_RDI] += delta; |
1738 | else | |
1739 | vcpu->regs[VCPU_REGS_RSI] += delta; | |
1740 | } | |
1741 | ||
cbdd1bea | 1742 | kvm_x86_ops->decache_regs(vcpu); |
46fc1477 | 1743 | |
039576c0 AK |
1744 | io->count -= io->cur_count; |
1745 | io->cur_count = 0; | |
1746 | ||
039576c0 | 1747 | return 0; |
46fc1477 AK |
1748 | } |
1749 | ||
65619eb5 ED |
1750 | static void kernel_pio(struct kvm_io_device *pio_dev, |
1751 | struct kvm_vcpu *vcpu, | |
1752 | void *pd) | |
74906345 ED |
1753 | { |
1754 | /* TODO: String I/O for in kernel device */ | |
1755 | ||
9cf98828 | 1756 | mutex_lock(&vcpu->kvm->lock); |
74906345 ED |
1757 | if (vcpu->pio.in) |
1758 | kvm_iodevice_read(pio_dev, vcpu->pio.port, | |
1759 | vcpu->pio.size, | |
65619eb5 | 1760 | pd); |
74906345 ED |
1761 | else |
1762 | kvm_iodevice_write(pio_dev, vcpu->pio.port, | |
1763 | vcpu->pio.size, | |
65619eb5 | 1764 | pd); |
9cf98828 | 1765 | mutex_unlock(&vcpu->kvm->lock); |
65619eb5 ED |
1766 | } |
1767 | ||
1768 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
1769 | struct kvm_vcpu *vcpu) | |
1770 | { | |
1771 | struct kvm_pio_request *io = &vcpu->pio; | |
1772 | void *pd = vcpu->pio_data; | |
1773 | int i; | |
1774 | ||
9cf98828 | 1775 | mutex_lock(&vcpu->kvm->lock); |
65619eb5 ED |
1776 | for (i = 0; i < io->cur_count; i++) { |
1777 | kvm_iodevice_write(pio_dev, io->port, | |
1778 | io->size, | |
1779 | pd); | |
1780 | pd += io->size; | |
1781 | } | |
9cf98828 | 1782 | mutex_unlock(&vcpu->kvm->lock); |
74906345 ED |
1783 | } |
1784 | ||
3090dd73 LV |
1785 | int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
1786 | int size, unsigned port) | |
1787 | { | |
1788 | struct kvm_io_device *pio_dev; | |
1789 | ||
1790 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1791 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
1792 | vcpu->run->io.size = vcpu->pio.size = size; | |
1793 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
1794 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; | |
1795 | vcpu->run->io.port = vcpu->pio.port = port; | |
1796 | vcpu->pio.in = in; | |
1797 | vcpu->pio.string = 0; | |
1798 | vcpu->pio.down = 0; | |
1799 | vcpu->pio.guest_page_offset = 0; | |
1800 | vcpu->pio.rep = 0; | |
1801 | ||
cbdd1bea | 1802 | kvm_x86_ops->cache_regs(vcpu); |
3090dd73 | 1803 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); |
cbdd1bea | 1804 | kvm_x86_ops->decache_regs(vcpu); |
3090dd73 | 1805 | |
0967b7bf AK |
1806 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
1807 | ||
3090dd73 LV |
1808 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
1809 | if (pio_dev) { | |
1810 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); | |
1811 | complete_pio(vcpu); | |
1812 | return 1; | |
1813 | } | |
1814 | return 0; | |
1815 | } | |
1816 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
1817 | ||
1818 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
1819 | int size, unsigned long count, int down, | |
039576c0 AK |
1820 | gva_t address, int rep, unsigned port) |
1821 | { | |
1822 | unsigned now, in_page; | |
65619eb5 | 1823 | int i, ret = 0; |
039576c0 AK |
1824 | int nr_pages = 1; |
1825 | struct page *page; | |
74906345 | 1826 | struct kvm_io_device *pio_dev; |
039576c0 AK |
1827 | |
1828 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1829 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
3090dd73 | 1830 | vcpu->run->io.size = vcpu->pio.size = size; |
039576c0 | 1831 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
3090dd73 LV |
1832 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; |
1833 | vcpu->run->io.port = vcpu->pio.port = port; | |
039576c0 | 1834 | vcpu->pio.in = in; |
3090dd73 | 1835 | vcpu->pio.string = 1; |
039576c0 AK |
1836 | vcpu->pio.down = down; |
1837 | vcpu->pio.guest_page_offset = offset_in_page(address); | |
1838 | vcpu->pio.rep = rep; | |
1839 | ||
039576c0 | 1840 | if (!count) { |
cbdd1bea | 1841 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
039576c0 AK |
1842 | return 1; |
1843 | } | |
1844 | ||
039576c0 AK |
1845 | if (!down) |
1846 | in_page = PAGE_SIZE - offset_in_page(address); | |
1847 | else | |
1848 | in_page = offset_in_page(address) + size; | |
1849 | now = min(count, (unsigned long)in_page / size); | |
1850 | if (!now) { | |
1851 | /* | |
1852 | * String I/O straddles page boundary. Pin two guest pages | |
1853 | * so that we satisfy atomicity constraints. Do just one | |
1854 | * transaction to avoid complexity. | |
1855 | */ | |
1856 | nr_pages = 2; | |
1857 | now = 1; | |
1858 | } | |
1859 | if (down) { | |
1860 | /* | |
1861 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
1862 | */ | |
f0242478 | 1863 | pr_unimpl(vcpu, "guest string pio down\n"); |
039576c0 AK |
1864 | inject_gp(vcpu); |
1865 | return 1; | |
1866 | } | |
1867 | vcpu->run->io.count = now; | |
1868 | vcpu->pio.cur_count = now; | |
1869 | ||
0967b7bf AK |
1870 | if (vcpu->pio.cur_count == vcpu->pio.count) |
1871 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
1872 | ||
039576c0 | 1873 | for (i = 0; i < nr_pages; ++i) { |
11ec2804 | 1874 | mutex_lock(&vcpu->kvm->lock); |
039576c0 AK |
1875 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
1876 | if (page) | |
1877 | get_page(page); | |
1878 | vcpu->pio.guest_pages[i] = page; | |
11ec2804 | 1879 | mutex_unlock(&vcpu->kvm->lock); |
039576c0 AK |
1880 | if (!page) { |
1881 | inject_gp(vcpu); | |
1882 | free_pio_guest_pages(vcpu); | |
1883 | return 1; | |
1884 | } | |
1885 | } | |
1886 | ||
3090dd73 | 1887 | pio_dev = vcpu_find_pio_dev(vcpu, port); |
65619eb5 ED |
1888 | if (!vcpu->pio.in) { |
1889 | /* string PIO write */ | |
1890 | ret = pio_copy_data(vcpu); | |
1891 | if (ret >= 0 && pio_dev) { | |
1892 | pio_string_write(pio_dev, vcpu); | |
1893 | complete_pio(vcpu); | |
1894 | if (vcpu->pio.count == 0) | |
1895 | ret = 1; | |
1896 | } | |
1897 | } else if (pio_dev) | |
f0242478 | 1898 | pr_unimpl(vcpu, "no string pio read support yet, " |
65619eb5 ED |
1899 | "port %x size %d count %ld\n", |
1900 | port, size, count); | |
1901 | ||
1902 | return ret; | |
039576c0 | 1903 | } |
3090dd73 | 1904 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
039576c0 | 1905 | |
04d2cc77 AK |
1906 | /* |
1907 | * Check if userspace requested an interrupt window, and that the | |
1908 | * interrupt window is open. | |
1909 | * | |
1910 | * No need to exit to userspace if we already have an interrupt queued. | |
1911 | */ | |
1912 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
1913 | struct kvm_run *kvm_run) | |
1914 | { | |
1915 | return (!vcpu->irq_summary && | |
1916 | kvm_run->request_interrupt_window && | |
1917 | vcpu->interrupt_window_open && | |
1918 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); | |
1919 | } | |
1920 | ||
1921 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
1922 | struct kvm_run *kvm_run) | |
1923 | { | |
1924 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
1925 | kvm_run->cr8 = get_cr8(vcpu); | |
1926 | kvm_run->apic_base = kvm_get_apic_base(vcpu); | |
1927 | if (irqchip_in_kernel(vcpu->kvm)) | |
1928 | kvm_run->ready_for_interrupt_injection = 1; | |
1929 | else | |
1930 | kvm_run->ready_for_interrupt_injection = | |
1931 | (vcpu->interrupt_window_open && | |
1932 | vcpu->irq_summary == 0); | |
1933 | } | |
1934 | ||
1935 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1936 | { | |
1937 | int r; | |
1938 | ||
1939 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { | |
1940 | printk("vcpu %d received sipi with vector # %x\n", | |
1941 | vcpu->vcpu_id, vcpu->sipi_vector); | |
1942 | kvm_lapic_reset(vcpu); | |
1943 | kvm_x86_ops->vcpu_reset(vcpu); | |
1944 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
1945 | } | |
1946 | ||
1947 | preempted: | |
1948 | if (vcpu->guest_debug.enabled) | |
1949 | kvm_x86_ops->guest_debug_pre(vcpu); | |
1950 | ||
1951 | again: | |
1952 | r = kvm_mmu_reload(vcpu); | |
1953 | if (unlikely(r)) | |
1954 | goto out; | |
1955 | ||
1956 | preempt_disable(); | |
1957 | ||
1958 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
1959 | kvm_load_guest_fpu(vcpu); | |
1960 | ||
1961 | local_irq_disable(); | |
1962 | ||
1963 | if (signal_pending(current)) { | |
1964 | local_irq_enable(); | |
1965 | preempt_enable(); | |
1966 | r = -EINTR; | |
1967 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
1968 | ++vcpu->stat.signal_exits; | |
1969 | goto out; | |
1970 | } | |
1971 | ||
1972 | if (irqchip_in_kernel(vcpu->kvm)) | |
1973 | kvm_x86_ops->inject_pending_irq(vcpu); | |
1974 | else if (!vcpu->mmio_read_completed) | |
1975 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); | |
1976 | ||
1977 | vcpu->guest_mode = 1; | |
d172fcd3 | 1978 | kvm_guest_enter(); |
04d2cc77 AK |
1979 | |
1980 | if (vcpu->requests) | |
1981 | if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests)) | |
1982 | kvm_x86_ops->tlb_flush(vcpu); | |
1983 | ||
1984 | kvm_x86_ops->run(vcpu, kvm_run); | |
1985 | ||
1986 | vcpu->guest_mode = 0; | |
1987 | local_irq_enable(); | |
1988 | ||
1989 | ++vcpu->stat.exits; | |
1990 | ||
0552f73b LV |
1991 | /* |
1992 | * We must have an instruction between local_irq_enable() and | |
1993 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
1994 | * the interrupt shadow. The stat.exits increment will do nicely. | |
1995 | * But we need to prevent reordering, hence this barrier(): | |
1996 | */ | |
1997 | barrier(); | |
1998 | ||
1999 | kvm_guest_exit(); | |
2000 | ||
04d2cc77 AK |
2001 | preempt_enable(); |
2002 | ||
2003 | /* | |
2004 | * Profile KVM exit RIPs: | |
2005 | */ | |
2006 | if (unlikely(prof_on == KVM_PROFILING)) { | |
2007 | kvm_x86_ops->cache_regs(vcpu); | |
2008 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); | |
2009 | } | |
2010 | ||
2011 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); | |
2012 | ||
2013 | if (r > 0) { | |
2014 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
2015 | r = -EINTR; | |
2016 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2017 | ++vcpu->stat.request_irq_exits; | |
2018 | goto out; | |
2019 | } | |
2020 | if (!need_resched()) { | |
2021 | ++vcpu->stat.light_exits; | |
2022 | goto again; | |
2023 | } | |
2024 | } | |
2025 | ||
2026 | out: | |
2027 | if (r > 0) { | |
2028 | kvm_resched(vcpu); | |
2029 | goto preempted; | |
2030 | } | |
2031 | ||
2032 | post_kvm_run_save(vcpu, kvm_run); | |
2033 | ||
2034 | return r; | |
2035 | } | |
2036 | ||
2037 | ||
bccf2150 | 2038 | static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
6aa8b732 | 2039 | { |
6aa8b732 | 2040 | int r; |
1961d276 | 2041 | sigset_t sigsaved; |
6aa8b732 | 2042 | |
bccf2150 | 2043 | vcpu_load(vcpu); |
6aa8b732 | 2044 | |
c5ec1534 HQ |
2045 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { |
2046 | kvm_vcpu_block(vcpu); | |
2047 | vcpu_put(vcpu); | |
2048 | return -EAGAIN; | |
2049 | } | |
2050 | ||
1961d276 AK |
2051 | if (vcpu->sigset_active) |
2052 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
2053 | ||
54810342 | 2054 | /* re-sync apic's tpr */ |
5cd4f6fd HQ |
2055 | if (!irqchip_in_kernel(vcpu->kvm)) |
2056 | set_cr8(vcpu, kvm_run->cr8); | |
54810342 | 2057 | |
02c83209 AK |
2058 | if (vcpu->pio.cur_count) { |
2059 | r = complete_pio(vcpu); | |
2060 | if (r) | |
2061 | goto out; | |
2062 | } | |
2063 | ||
2064 | if (vcpu->mmio_needed) { | |
2065 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
2066 | vcpu->mmio_read_completed = 1; | |
2067 | vcpu->mmio_needed = 0; | |
2068 | r = emulate_instruction(vcpu, kvm_run, | |
3427318f | 2069 | vcpu->mmio_fault_cr2, 0, 1); |
02c83209 AK |
2070 | if (r == EMULATE_DO_MMIO) { |
2071 | /* | |
2072 | * Read-modify-write. Back to userspace. | |
2073 | */ | |
02c83209 AK |
2074 | r = 0; |
2075 | goto out; | |
46fc1477 | 2076 | } |
6aa8b732 AK |
2077 | } |
2078 | ||
8eb7d334 | 2079 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { |
cbdd1bea | 2080 | kvm_x86_ops->cache_regs(vcpu); |
b4e63f56 | 2081 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
cbdd1bea | 2082 | kvm_x86_ops->decache_regs(vcpu); |
b4e63f56 AK |
2083 | } |
2084 | ||
04d2cc77 | 2085 | r = __vcpu_run(vcpu, kvm_run); |
6aa8b732 | 2086 | |
039576c0 | 2087 | out: |
1961d276 AK |
2088 | if (vcpu->sigset_active) |
2089 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
2090 | ||
6aa8b732 AK |
2091 | vcpu_put(vcpu); |
2092 | return r; | |
2093 | } | |
2094 | ||
bccf2150 AK |
2095 | static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, |
2096 | struct kvm_regs *regs) | |
6aa8b732 | 2097 | { |
bccf2150 | 2098 | vcpu_load(vcpu); |
6aa8b732 | 2099 | |
cbdd1bea | 2100 | kvm_x86_ops->cache_regs(vcpu); |
6aa8b732 AK |
2101 | |
2102 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
2103 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
2104 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
2105 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
2106 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
2107 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
2108 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
2109 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 2110 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2111 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
2112 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
2113 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
2114 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
2115 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
2116 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
2117 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
2118 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
2119 | #endif | |
2120 | ||
2121 | regs->rip = vcpu->rip; | |
cbdd1bea | 2122 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
6aa8b732 AK |
2123 | |
2124 | /* | |
2125 | * Don't leak debug flags in case they were set for guest debugging | |
2126 | */ | |
2127 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
2128 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
2129 | ||
2130 | vcpu_put(vcpu); | |
2131 | ||
2132 | return 0; | |
2133 | } | |
2134 | ||
bccf2150 AK |
2135 | static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, |
2136 | struct kvm_regs *regs) | |
6aa8b732 | 2137 | { |
bccf2150 | 2138 | vcpu_load(vcpu); |
6aa8b732 AK |
2139 | |
2140 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
2141 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
2142 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
2143 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
2144 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
2145 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
2146 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
2147 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 2148 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2149 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
2150 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
2151 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
2152 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
2153 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
2154 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
2155 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
2156 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
2157 | #endif | |
2158 | ||
2159 | vcpu->rip = regs->rip; | |
cbdd1bea | 2160 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
6aa8b732 | 2161 | |
cbdd1bea | 2162 | kvm_x86_ops->decache_regs(vcpu); |
6aa8b732 AK |
2163 | |
2164 | vcpu_put(vcpu); | |
2165 | ||
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | static void get_segment(struct kvm_vcpu *vcpu, | |
2170 | struct kvm_segment *var, int seg) | |
2171 | { | |
cbdd1bea | 2172 | return kvm_x86_ops->get_segment(vcpu, var, seg); |
6aa8b732 AK |
2173 | } |
2174 | ||
bccf2150 AK |
2175 | static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
2176 | struct kvm_sregs *sregs) | |
6aa8b732 | 2177 | { |
6aa8b732 | 2178 | struct descriptor_table dt; |
2a8067f1 | 2179 | int pending_vec; |
6aa8b732 | 2180 | |
bccf2150 | 2181 | vcpu_load(vcpu); |
6aa8b732 AK |
2182 | |
2183 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
2184 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2185 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2186 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2187 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2188 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2189 | ||
2190 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2191 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2192 | ||
cbdd1bea | 2193 | kvm_x86_ops->get_idt(vcpu, &dt); |
6aa8b732 AK |
2194 | sregs->idt.limit = dt.limit; |
2195 | sregs->idt.base = dt.base; | |
cbdd1bea | 2196 | kvm_x86_ops->get_gdt(vcpu, &dt); |
6aa8b732 AK |
2197 | sregs->gdt.limit = dt.limit; |
2198 | sregs->gdt.base = dt.base; | |
2199 | ||
cbdd1bea | 2200 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
6aa8b732 AK |
2201 | sregs->cr0 = vcpu->cr0; |
2202 | sregs->cr2 = vcpu->cr2; | |
2203 | sregs->cr3 = vcpu->cr3; | |
2204 | sregs->cr4 = vcpu->cr4; | |
7017fc3d | 2205 | sregs->cr8 = get_cr8(vcpu); |
6aa8b732 | 2206 | sregs->efer = vcpu->shadow_efer; |
7017fc3d | 2207 | sregs->apic_base = kvm_get_apic_base(vcpu); |
6aa8b732 | 2208 | |
2a8067f1 | 2209 | if (irqchip_in_kernel(vcpu->kvm)) { |
c52fb35a HQ |
2210 | memset(sregs->interrupt_bitmap, 0, |
2211 | sizeof sregs->interrupt_bitmap); | |
cbdd1bea | 2212 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
2a8067f1 ED |
2213 | if (pending_vec >= 0) |
2214 | set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap); | |
2215 | } else | |
c52fb35a HQ |
2216 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, |
2217 | sizeof sregs->interrupt_bitmap); | |
6aa8b732 AK |
2218 | |
2219 | vcpu_put(vcpu); | |
2220 | ||
2221 | return 0; | |
2222 | } | |
2223 | ||
2224 | static void set_segment(struct kvm_vcpu *vcpu, | |
2225 | struct kvm_segment *var, int seg) | |
2226 | { | |
cbdd1bea | 2227 | return kvm_x86_ops->set_segment(vcpu, var, seg); |
6aa8b732 AK |
2228 | } |
2229 | ||
bccf2150 AK |
2230 | static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
2231 | struct kvm_sregs *sregs) | |
6aa8b732 | 2232 | { |
6aa8b732 | 2233 | int mmu_reset_needed = 0; |
2a8067f1 | 2234 | int i, pending_vec, max_bits; |
6aa8b732 AK |
2235 | struct descriptor_table dt; |
2236 | ||
bccf2150 | 2237 | vcpu_load(vcpu); |
6aa8b732 | 2238 | |
6aa8b732 AK |
2239 | dt.limit = sregs->idt.limit; |
2240 | dt.base = sregs->idt.base; | |
cbdd1bea | 2241 | kvm_x86_ops->set_idt(vcpu, &dt); |
6aa8b732 AK |
2242 | dt.limit = sregs->gdt.limit; |
2243 | dt.base = sregs->gdt.base; | |
cbdd1bea | 2244 | kvm_x86_ops->set_gdt(vcpu, &dt); |
6aa8b732 AK |
2245 | |
2246 | vcpu->cr2 = sregs->cr2; | |
2247 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
2248 | vcpu->cr3 = sregs->cr3; | |
2249 | ||
7017fc3d | 2250 | set_cr8(vcpu, sregs->cr8); |
6aa8b732 AK |
2251 | |
2252 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 2253 | #ifdef CONFIG_X86_64 |
cbdd1bea | 2254 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
6aa8b732 | 2255 | #endif |
7017fc3d | 2256 | kvm_set_apic_base(vcpu, sregs->apic_base); |
6aa8b732 | 2257 | |
cbdd1bea | 2258 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
399badf3 | 2259 | |
6aa8b732 | 2260 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
81f50e3b | 2261 | vcpu->cr0 = sregs->cr0; |
cbdd1bea | 2262 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
6aa8b732 AK |
2263 | |
2264 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
cbdd1bea | 2265 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
1b0973bd AK |
2266 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
2267 | load_pdptrs(vcpu, vcpu->cr3); | |
6aa8b732 AK |
2268 | |
2269 | if (mmu_reset_needed) | |
2270 | kvm_mmu_reset_context(vcpu); | |
2271 | ||
c52fb35a HQ |
2272 | if (!irqchip_in_kernel(vcpu->kvm)) { |
2273 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
2274 | sizeof vcpu->irq_pending); | |
2275 | vcpu->irq_summary = 0; | |
2276 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) | |
2277 | if (vcpu->irq_pending[i]) | |
2278 | __set_bit(i, &vcpu->irq_summary); | |
2a8067f1 ED |
2279 | } else { |
2280 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
2281 | pending_vec = find_first_bit( | |
2282 | (const unsigned long *)sregs->interrupt_bitmap, | |
2283 | max_bits); | |
2284 | /* Only pending external irq is handled here */ | |
2285 | if (pending_vec < max_bits) { | |
cbdd1bea | 2286 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
2a8067f1 ED |
2287 | printk("Set back pending irq %d\n", pending_vec); |
2288 | } | |
c52fb35a | 2289 | } |
6aa8b732 | 2290 | |
024aa1c0 AK |
2291 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
2292 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2293 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2294 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2295 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2296 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2297 | ||
2298 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2299 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2300 | ||
6aa8b732 AK |
2301 | vcpu_put(vcpu); |
2302 | ||
2303 | return 0; | |
2304 | } | |
2305 | ||
1747fb71 RR |
2306 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
2307 | { | |
2308 | struct kvm_segment cs; | |
2309 | ||
2310 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
2311 | *db = cs.db; | |
2312 | *l = cs.l; | |
2313 | } | |
2314 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
2315 | ||
6aa8b732 AK |
2316 | /* |
2317 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
2318 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
bf591b24 MR |
2319 | * |
2320 | * This list is modified at module load time to reflect the | |
2321 | * capabilities of the host cpu. | |
6aa8b732 AK |
2322 | */ |
2323 | static u32 msrs_to_save[] = { | |
2324 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
2325 | MSR_K6_STAR, | |
05b3e0c2 | 2326 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
2327 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
2328 | #endif | |
2329 | MSR_IA32_TIME_STAMP_COUNTER, | |
2330 | }; | |
2331 | ||
bf591b24 MR |
2332 | static unsigned num_msrs_to_save; |
2333 | ||
6f00e68f AK |
2334 | static u32 emulated_msrs[] = { |
2335 | MSR_IA32_MISC_ENABLE, | |
2336 | }; | |
2337 | ||
bf591b24 MR |
2338 | static __init void kvm_init_msr_list(void) |
2339 | { | |
2340 | u32 dummy[2]; | |
2341 | unsigned i, j; | |
2342 | ||
2343 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
2344 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
2345 | continue; | |
2346 | if (j < i) | |
2347 | msrs_to_save[j] = msrs_to_save[i]; | |
2348 | j++; | |
2349 | } | |
2350 | num_msrs_to_save = j; | |
2351 | } | |
6aa8b732 AK |
2352 | |
2353 | /* | |
2354 | * Adapt set_msr() to msr_io()'s calling convention | |
2355 | */ | |
2356 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
2357 | { | |
35f3f286 | 2358 | return kvm_set_msr(vcpu, index, *data); |
6aa8b732 AK |
2359 | } |
2360 | ||
2361 | /* | |
2362 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
2363 | * | |
2364 | * @return number of msrs set successfully. | |
2365 | */ | |
bccf2150 | 2366 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
6aa8b732 AK |
2367 | struct kvm_msr_entry *entries, |
2368 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
2369 | unsigned index, u64 *data)) | |
2370 | { | |
6aa8b732 AK |
2371 | int i; |
2372 | ||
bccf2150 | 2373 | vcpu_load(vcpu); |
6aa8b732 AK |
2374 | |
2375 | for (i = 0; i < msrs->nmsrs; ++i) | |
2376 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
2377 | break; | |
2378 | ||
2379 | vcpu_put(vcpu); | |
2380 | ||
2381 | return i; | |
2382 | } | |
2383 | ||
2384 | /* | |
2385 | * Read or write a bunch of msrs. Parameters are user addresses. | |
2386 | * | |
2387 | * @return number of msrs set successfully. | |
2388 | */ | |
bccf2150 | 2389 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
6aa8b732 AK |
2390 | int (*do_msr)(struct kvm_vcpu *vcpu, |
2391 | unsigned index, u64 *data), | |
2392 | int writeback) | |
2393 | { | |
2394 | struct kvm_msrs msrs; | |
2395 | struct kvm_msr_entry *entries; | |
2396 | int r, n; | |
2397 | unsigned size; | |
2398 | ||
2399 | r = -EFAULT; | |
2400 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
2401 | goto out; | |
2402 | ||
2403 | r = -E2BIG; | |
2404 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
2405 | goto out; | |
2406 | ||
2407 | r = -ENOMEM; | |
2408 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
2409 | entries = vmalloc(size); | |
2410 | if (!entries) | |
2411 | goto out; | |
2412 | ||
2413 | r = -EFAULT; | |
2414 | if (copy_from_user(entries, user_msrs->entries, size)) | |
2415 | goto out_free; | |
2416 | ||
bccf2150 | 2417 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
6aa8b732 AK |
2418 | if (r < 0) |
2419 | goto out_free; | |
2420 | ||
2421 | r = -EFAULT; | |
2422 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
2423 | goto out_free; | |
2424 | ||
2425 | r = n; | |
2426 | ||
2427 | out_free: | |
2428 | vfree(entries); | |
2429 | out: | |
2430 | return r; | |
2431 | } | |
2432 | ||
2433 | /* | |
2434 | * Translate a guest virtual address to a guest physical address. | |
2435 | */ | |
bccf2150 AK |
2436 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
2437 | struct kvm_translation *tr) | |
6aa8b732 AK |
2438 | { |
2439 | unsigned long vaddr = tr->linear_address; | |
6aa8b732 AK |
2440 | gpa_t gpa; |
2441 | ||
bccf2150 | 2442 | vcpu_load(vcpu); |
11ec2804 | 2443 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 AK |
2444 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
2445 | tr->physical_address = gpa; | |
2446 | tr->valid = gpa != UNMAPPED_GVA; | |
2447 | tr->writeable = 1; | |
2448 | tr->usermode = 0; | |
11ec2804 | 2449 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
2450 | vcpu_put(vcpu); |
2451 | ||
2452 | return 0; | |
2453 | } | |
2454 | ||
bccf2150 AK |
2455 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
2456 | struct kvm_interrupt *irq) | |
6aa8b732 | 2457 | { |
6aa8b732 AK |
2458 | if (irq->irq < 0 || irq->irq >= 256) |
2459 | return -EINVAL; | |
97222cc8 ED |
2460 | if (irqchip_in_kernel(vcpu->kvm)) |
2461 | return -ENXIO; | |
bccf2150 | 2462 | vcpu_load(vcpu); |
6aa8b732 AK |
2463 | |
2464 | set_bit(irq->irq, vcpu->irq_pending); | |
2465 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
2466 | ||
2467 | vcpu_put(vcpu); | |
2468 | ||
2469 | return 0; | |
2470 | } | |
2471 | ||
bccf2150 AK |
2472 | static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
2473 | struct kvm_debug_guest *dbg) | |
6aa8b732 | 2474 | { |
6aa8b732 AK |
2475 | int r; |
2476 | ||
bccf2150 | 2477 | vcpu_load(vcpu); |
6aa8b732 | 2478 | |
cbdd1bea | 2479 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
6aa8b732 AK |
2480 | |
2481 | vcpu_put(vcpu); | |
2482 | ||
2483 | return r; | |
2484 | } | |
2485 | ||
9a2bb7f4 AK |
2486 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
2487 | unsigned long address, | |
2488 | int *type) | |
2489 | { | |
2490 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
2491 | unsigned long pgoff; | |
2492 | struct page *page; | |
2493 | ||
9a2bb7f4 | 2494 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
2495 | if (pgoff == 0) |
2496 | page = virt_to_page(vcpu->run); | |
2497 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
2498 | page = virt_to_page(vcpu->pio_data); | |
2499 | else | |
9a2bb7f4 | 2500 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 2501 | get_page(page); |
cd0d9137 NAQ |
2502 | if (type != NULL) |
2503 | *type = VM_FAULT_MINOR; | |
2504 | ||
9a2bb7f4 AK |
2505 | return page; |
2506 | } | |
2507 | ||
2508 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
2509 | .nopage = kvm_vcpu_nopage, | |
2510 | }; | |
2511 | ||
2512 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
2513 | { | |
2514 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
2515 | return 0; | |
2516 | } | |
2517 | ||
bccf2150 AK |
2518 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
2519 | { | |
2520 | struct kvm_vcpu *vcpu = filp->private_data; | |
2521 | ||
2522 | fput(vcpu->kvm->filp); | |
2523 | return 0; | |
2524 | } | |
2525 | ||
2526 | static struct file_operations kvm_vcpu_fops = { | |
2527 | .release = kvm_vcpu_release, | |
2528 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
2529 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 2530 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
2531 | }; |
2532 | ||
2533 | /* | |
2534 | * Allocates an inode for the vcpu. | |
2535 | */ | |
2536 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
2537 | { | |
2538 | int fd, r; | |
2539 | struct inode *inode; | |
2540 | struct file *file; | |
2541 | ||
d6d28168 AK |
2542 | r = anon_inode_getfd(&fd, &inode, &file, |
2543 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
2544 | if (r) | |
2545 | return r; | |
bccf2150 | 2546 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 2547 | return fd; |
bccf2150 AK |
2548 | } |
2549 | ||
c5ea7660 AK |
2550 | /* |
2551 | * Creates some virtual cpus. Good luck creating more than one. | |
2552 | */ | |
2553 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
2554 | { | |
2555 | int r; | |
2556 | struct kvm_vcpu *vcpu; | |
2557 | ||
c5ea7660 | 2558 | if (!valid_vcpu(n)) |
fb3f0f51 | 2559 | return -EINVAL; |
c5ea7660 | 2560 | |
cbdd1bea | 2561 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
fb3f0f51 RR |
2562 | if (IS_ERR(vcpu)) |
2563 | return PTR_ERR(vcpu); | |
c5ea7660 | 2564 | |
15ad7146 AK |
2565 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
2566 | ||
b114b080 RR |
2567 | /* We do fxsave: this must be aligned. */ |
2568 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
2569 | ||
fb3f0f51 | 2570 | vcpu_load(vcpu); |
c5ea7660 | 2571 | r = kvm_mmu_setup(vcpu); |
c5ea7660 | 2572 | vcpu_put(vcpu); |
c5ea7660 | 2573 | if (r < 0) |
fb3f0f51 RR |
2574 | goto free_vcpu; |
2575 | ||
11ec2804 | 2576 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
2577 | if (kvm->vcpus[n]) { |
2578 | r = -EEXIST; | |
11ec2804 | 2579 | mutex_unlock(&kvm->lock); |
fb3f0f51 RR |
2580 | goto mmu_unload; |
2581 | } | |
2582 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 2583 | mutex_unlock(&kvm->lock); |
c5ea7660 | 2584 | |
fb3f0f51 | 2585 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
2586 | r = create_vcpu_fd(vcpu); |
2587 | if (r < 0) | |
fb3f0f51 RR |
2588 | goto unlink; |
2589 | return r; | |
39c3b86e | 2590 | |
fb3f0f51 | 2591 | unlink: |
11ec2804 | 2592 | mutex_lock(&kvm->lock); |
fb3f0f51 | 2593 | kvm->vcpus[n] = NULL; |
11ec2804 | 2594 | mutex_unlock(&kvm->lock); |
a2fa3e9f | 2595 | |
fb3f0f51 RR |
2596 | mmu_unload: |
2597 | vcpu_load(vcpu); | |
2598 | kvm_mmu_unload(vcpu); | |
2599 | vcpu_put(vcpu); | |
c5ea7660 | 2600 | |
fb3f0f51 | 2601 | free_vcpu: |
cbdd1bea | 2602 | kvm_x86_ops->vcpu_free(vcpu); |
c5ea7660 AK |
2603 | return r; |
2604 | } | |
2605 | ||
2cc51560 ED |
2606 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
2607 | { | |
2608 | u64 efer; | |
2609 | int i; | |
2610 | struct kvm_cpuid_entry *e, *entry; | |
2611 | ||
2612 | rdmsrl(MSR_EFER, efer); | |
2613 | entry = NULL; | |
2614 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
2615 | e = &vcpu->cpuid_entries[i]; | |
2616 | if (e->function == 0x80000001) { | |
2617 | entry = e; | |
2618 | break; | |
2619 | } | |
2620 | } | |
4c981b43 | 2621 | if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) { |
2cc51560 | 2622 | entry->edx &= ~(1 << 20); |
4c981b43 | 2623 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
2cc51560 ED |
2624 | } |
2625 | } | |
2626 | ||
06465c5a AK |
2627 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
2628 | struct kvm_cpuid *cpuid, | |
2629 | struct kvm_cpuid_entry __user *entries) | |
2630 | { | |
2631 | int r; | |
2632 | ||
2633 | r = -E2BIG; | |
2634 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
2635 | goto out; | |
2636 | r = -EFAULT; | |
2637 | if (copy_from_user(&vcpu->cpuid_entries, entries, | |
2638 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
2639 | goto out; | |
2640 | vcpu->cpuid_nent = cpuid->nent; | |
2cc51560 | 2641 | cpuid_fix_nx_cap(vcpu); |
06465c5a AK |
2642 | return 0; |
2643 | ||
2644 | out: | |
2645 | return r; | |
2646 | } | |
2647 | ||
1961d276 AK |
2648 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
2649 | { | |
2650 | if (sigset) { | |
2651 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
2652 | vcpu->sigset_active = 1; | |
2653 | vcpu->sigset = *sigset; | |
2654 | } else | |
2655 | vcpu->sigset_active = 0; | |
2656 | return 0; | |
2657 | } | |
2658 | ||
b8836737 AK |
2659 | /* |
2660 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
2661 | * we have asm/x86/processor.h | |
2662 | */ | |
2663 | struct fxsave { | |
2664 | u16 cwd; | |
2665 | u16 swd; | |
2666 | u16 twd; | |
2667 | u16 fop; | |
2668 | u64 rip; | |
2669 | u64 rdp; | |
2670 | u32 mxcsr; | |
2671 | u32 mxcsr_mask; | |
2672 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
2673 | #ifdef CONFIG_X86_64 | |
2674 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
2675 | #else | |
2676 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
2677 | #endif | |
2678 | }; | |
2679 | ||
2680 | static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2681 | { | |
b114b080 | 2682 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2683 | |
2684 | vcpu_load(vcpu); | |
2685 | ||
2686 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
2687 | fpu->fcw = fxsave->cwd; | |
2688 | fpu->fsw = fxsave->swd; | |
2689 | fpu->ftwx = fxsave->twd; | |
2690 | fpu->last_opcode = fxsave->fop; | |
2691 | fpu->last_ip = fxsave->rip; | |
2692 | fpu->last_dp = fxsave->rdp; | |
2693 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
2694 | ||
2695 | vcpu_put(vcpu); | |
2696 | ||
2697 | return 0; | |
2698 | } | |
2699 | ||
2700 | static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2701 | { | |
b114b080 | 2702 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; |
b8836737 AK |
2703 | |
2704 | vcpu_load(vcpu); | |
2705 | ||
2706 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
2707 | fxsave->cwd = fpu->fcw; | |
2708 | fxsave->swd = fpu->fsw; | |
2709 | fxsave->twd = fpu->ftwx; | |
2710 | fxsave->fop = fpu->last_opcode; | |
2711 | fxsave->rip = fpu->last_ip; | |
2712 | fxsave->rdp = fpu->last_dp; | |
2713 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
2714 | ||
2715 | vcpu_put(vcpu); | |
2716 | ||
2717 | return 0; | |
2718 | } | |
2719 | ||
96ad2cc6 ED |
2720 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
2721 | struct kvm_lapic_state *s) | |
2722 | { | |
2723 | vcpu_load(vcpu); | |
2724 | memcpy(s->regs, vcpu->apic->regs, sizeof *s); | |
2725 | vcpu_put(vcpu); | |
2726 | ||
2727 | return 0; | |
2728 | } | |
2729 | ||
2730 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
2731 | struct kvm_lapic_state *s) | |
2732 | { | |
2733 | vcpu_load(vcpu); | |
2734 | memcpy(vcpu->apic->regs, s->regs, sizeof *s); | |
2735 | kvm_apic_post_state_restore(vcpu); | |
2736 | vcpu_put(vcpu); | |
2737 | ||
2738 | return 0; | |
2739 | } | |
2740 | ||
bccf2150 AK |
2741 | static long kvm_vcpu_ioctl(struct file *filp, |
2742 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 2743 | { |
bccf2150 | 2744 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 2745 | void __user *argp = (void __user *)arg; |
6aa8b732 AK |
2746 | int r = -EINVAL; |
2747 | ||
2748 | switch (ioctl) { | |
9a2bb7f4 | 2749 | case KVM_RUN: |
f0fe5108 AK |
2750 | r = -EINVAL; |
2751 | if (arg) | |
2752 | goto out; | |
9a2bb7f4 | 2753 | r = kvm_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 2754 | break; |
6aa8b732 AK |
2755 | case KVM_GET_REGS: { |
2756 | struct kvm_regs kvm_regs; | |
2757 | ||
bccf2150 AK |
2758 | memset(&kvm_regs, 0, sizeof kvm_regs); |
2759 | r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs); | |
6aa8b732 AK |
2760 | if (r) |
2761 | goto out; | |
2762 | r = -EFAULT; | |
2f366987 | 2763 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
2764 | goto out; |
2765 | r = 0; | |
2766 | break; | |
2767 | } | |
2768 | case KVM_SET_REGS: { | |
2769 | struct kvm_regs kvm_regs; | |
2770 | ||
2771 | r = -EFAULT; | |
2f366987 | 2772 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 2773 | goto out; |
bccf2150 | 2774 | r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
2775 | if (r) |
2776 | goto out; | |
2777 | r = 0; | |
2778 | break; | |
2779 | } | |
2780 | case KVM_GET_SREGS: { | |
2781 | struct kvm_sregs kvm_sregs; | |
2782 | ||
bccf2150 AK |
2783 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
2784 | r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); | |
6aa8b732 AK |
2785 | if (r) |
2786 | goto out; | |
2787 | r = -EFAULT; | |
2f366987 | 2788 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
2789 | goto out; |
2790 | r = 0; | |
2791 | break; | |
2792 | } | |
2793 | case KVM_SET_SREGS: { | |
2794 | struct kvm_sregs kvm_sregs; | |
2795 | ||
2796 | r = -EFAULT; | |
2f366987 | 2797 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 2798 | goto out; |
bccf2150 | 2799 | r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
2800 | if (r) |
2801 | goto out; | |
2802 | r = 0; | |
2803 | break; | |
2804 | } | |
2805 | case KVM_TRANSLATE: { | |
2806 | struct kvm_translation tr; | |
2807 | ||
2808 | r = -EFAULT; | |
2f366987 | 2809 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 2810 | goto out; |
bccf2150 | 2811 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
2812 | if (r) |
2813 | goto out; | |
2814 | r = -EFAULT; | |
2f366987 | 2815 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
2816 | goto out; |
2817 | r = 0; | |
2818 | break; | |
2819 | } | |
2820 | case KVM_INTERRUPT: { | |
2821 | struct kvm_interrupt irq; | |
2822 | ||
2823 | r = -EFAULT; | |
2f366987 | 2824 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 2825 | goto out; |
bccf2150 | 2826 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
2827 | if (r) |
2828 | goto out; | |
2829 | r = 0; | |
2830 | break; | |
2831 | } | |
2832 | case KVM_DEBUG_GUEST: { | |
2833 | struct kvm_debug_guest dbg; | |
2834 | ||
2835 | r = -EFAULT; | |
2f366987 | 2836 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 2837 | goto out; |
bccf2150 | 2838 | r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
2839 | if (r) |
2840 | goto out; | |
2841 | r = 0; | |
2842 | break; | |
2843 | } | |
bccf2150 | 2844 | case KVM_GET_MSRS: |
35f3f286 | 2845 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
bccf2150 AK |
2846 | break; |
2847 | case KVM_SET_MSRS: | |
2848 | r = msr_io(vcpu, argp, do_set_msr, 0); | |
2849 | break; | |
06465c5a AK |
2850 | case KVM_SET_CPUID: { |
2851 | struct kvm_cpuid __user *cpuid_arg = argp; | |
2852 | struct kvm_cpuid cpuid; | |
2853 | ||
2854 | r = -EFAULT; | |
2855 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
2856 | goto out; | |
2857 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
2858 | if (r) | |
2859 | goto out; | |
2860 | break; | |
2861 | } | |
1961d276 AK |
2862 | case KVM_SET_SIGNAL_MASK: { |
2863 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2864 | struct kvm_signal_mask kvm_sigmask; | |
2865 | sigset_t sigset, *p; | |
2866 | ||
2867 | p = NULL; | |
2868 | if (argp) { | |
2869 | r = -EFAULT; | |
2870 | if (copy_from_user(&kvm_sigmask, argp, | |
2871 | sizeof kvm_sigmask)) | |
2872 | goto out; | |
2873 | r = -EINVAL; | |
2874 | if (kvm_sigmask.len != sizeof sigset) | |
2875 | goto out; | |
2876 | r = -EFAULT; | |
2877 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
2878 | sizeof sigset)) | |
2879 | goto out; | |
2880 | p = &sigset; | |
2881 | } | |
2882 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
2883 | break; | |
2884 | } | |
b8836737 AK |
2885 | case KVM_GET_FPU: { |
2886 | struct kvm_fpu fpu; | |
2887 | ||
2888 | memset(&fpu, 0, sizeof fpu); | |
2889 | r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu); | |
2890 | if (r) | |
2891 | goto out; | |
2892 | r = -EFAULT; | |
2893 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
2894 | goto out; | |
2895 | r = 0; | |
2896 | break; | |
2897 | } | |
2898 | case KVM_SET_FPU: { | |
2899 | struct kvm_fpu fpu; | |
2900 | ||
2901 | r = -EFAULT; | |
2902 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
2903 | goto out; | |
2904 | r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu); | |
2905 | if (r) | |
2906 | goto out; | |
2907 | r = 0; | |
2908 | break; | |
2909 | } | |
96ad2cc6 ED |
2910 | case KVM_GET_LAPIC: { |
2911 | struct kvm_lapic_state lapic; | |
2912 | ||
2913 | memset(&lapic, 0, sizeof lapic); | |
2914 | r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic); | |
2915 | if (r) | |
2916 | goto out; | |
2917 | r = -EFAULT; | |
2918 | if (copy_to_user(argp, &lapic, sizeof lapic)) | |
2919 | goto out; | |
2920 | r = 0; | |
2921 | break; | |
2922 | } | |
2923 | case KVM_SET_LAPIC: { | |
2924 | struct kvm_lapic_state lapic; | |
2925 | ||
2926 | r = -EFAULT; | |
2927 | if (copy_from_user(&lapic, argp, sizeof lapic)) | |
2928 | goto out; | |
2929 | r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);; | |
2930 | if (r) | |
2931 | goto out; | |
2932 | r = 0; | |
2933 | break; | |
2934 | } | |
bccf2150 AK |
2935 | default: |
2936 | ; | |
2937 | } | |
2938 | out: | |
2939 | return r; | |
2940 | } | |
2941 | ||
2942 | static long kvm_vm_ioctl(struct file *filp, | |
2943 | unsigned int ioctl, unsigned long arg) | |
2944 | { | |
2945 | struct kvm *kvm = filp->private_data; | |
2946 | void __user *argp = (void __user *)arg; | |
2947 | int r = -EINVAL; | |
2948 | ||
2949 | switch (ioctl) { | |
2950 | case KVM_CREATE_VCPU: | |
2951 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
2952 | if (r < 0) | |
2953 | goto out; | |
2954 | break; | |
6aa8b732 AK |
2955 | case KVM_SET_MEMORY_REGION: { |
2956 | struct kvm_memory_region kvm_mem; | |
2957 | ||
2958 | r = -EFAULT; | |
2f366987 | 2959 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
6aa8b732 | 2960 | goto out; |
2c6f5df9 | 2961 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem); |
6aa8b732 AK |
2962 | if (r) |
2963 | goto out; | |
2964 | break; | |
2965 | } | |
2966 | case KVM_GET_DIRTY_LOG: { | |
2967 | struct kvm_dirty_log log; | |
2968 | ||
2969 | r = -EFAULT; | |
2f366987 | 2970 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 2971 | goto out; |
2c6f5df9 | 2972 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
2973 | if (r) |
2974 | goto out; | |
2975 | break; | |
2976 | } | |
e8207547 AK |
2977 | case KVM_SET_MEMORY_ALIAS: { |
2978 | struct kvm_memory_alias alias; | |
2979 | ||
2980 | r = -EFAULT; | |
2981 | if (copy_from_user(&alias, argp, sizeof alias)) | |
2982 | goto out; | |
2983 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); | |
2984 | if (r) | |
2985 | goto out; | |
2986 | break; | |
2987 | } | |
85f455f7 ED |
2988 | case KVM_CREATE_IRQCHIP: |
2989 | r = -ENOMEM; | |
2990 | kvm->vpic = kvm_create_pic(kvm); | |
1fd4f2a5 ED |
2991 | if (kvm->vpic) { |
2992 | r = kvm_ioapic_init(kvm); | |
2993 | if (r) { | |
2994 | kfree(kvm->vpic); | |
2995 | kvm->vpic = NULL; | |
2996 | goto out; | |
2997 | } | |
2998 | } | |
85f455f7 ED |
2999 | else |
3000 | goto out; | |
3001 | break; | |
3002 | case KVM_IRQ_LINE: { | |
3003 | struct kvm_irq_level irq_event; | |
3004 | ||
3005 | r = -EFAULT; | |
3006 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | |
3007 | goto out; | |
3008 | if (irqchip_in_kernel(kvm)) { | |
9cf98828 | 3009 | mutex_lock(&kvm->lock); |
85f455f7 ED |
3010 | if (irq_event.irq < 16) |
3011 | kvm_pic_set_irq(pic_irqchip(kvm), | |
3012 | irq_event.irq, | |
3013 | irq_event.level); | |
1fd4f2a5 ED |
3014 | kvm_ioapic_set_irq(kvm->vioapic, |
3015 | irq_event.irq, | |
3016 | irq_event.level); | |
9cf98828 | 3017 | mutex_unlock(&kvm->lock); |
85f455f7 ED |
3018 | r = 0; |
3019 | } | |
3020 | break; | |
3021 | } | |
6ceb9d79 HQ |
3022 | case KVM_GET_IRQCHIP: { |
3023 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
3024 | struct kvm_irqchip chip; | |
3025 | ||
3026 | r = -EFAULT; | |
3027 | if (copy_from_user(&chip, argp, sizeof chip)) | |
3028 | goto out; | |
3029 | r = -ENXIO; | |
3030 | if (!irqchip_in_kernel(kvm)) | |
3031 | goto out; | |
3032 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); | |
3033 | if (r) | |
3034 | goto out; | |
3035 | r = -EFAULT; | |
3036 | if (copy_to_user(argp, &chip, sizeof chip)) | |
3037 | goto out; | |
3038 | r = 0; | |
3039 | break; | |
3040 | } | |
3041 | case KVM_SET_IRQCHIP: { | |
3042 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
3043 | struct kvm_irqchip chip; | |
3044 | ||
3045 | r = -EFAULT; | |
3046 | if (copy_from_user(&chip, argp, sizeof chip)) | |
3047 | goto out; | |
3048 | r = -ENXIO; | |
3049 | if (!irqchip_in_kernel(kvm)) | |
3050 | goto out; | |
3051 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); | |
3052 | if (r) | |
3053 | goto out; | |
3054 | r = 0; | |
3055 | break; | |
3056 | } | |
f17abe9a AK |
3057 | default: |
3058 | ; | |
3059 | } | |
3060 | out: | |
3061 | return r; | |
3062 | } | |
3063 | ||
3064 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
3065 | unsigned long address, | |
3066 | int *type) | |
3067 | { | |
3068 | struct kvm *kvm = vma->vm_file->private_data; | |
3069 | unsigned long pgoff; | |
f17abe9a AK |
3070 | struct page *page; |
3071 | ||
f17abe9a | 3072 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
954bbbc2 | 3073 | page = gfn_to_page(kvm, pgoff); |
f17abe9a AK |
3074 | if (!page) |
3075 | return NOPAGE_SIGBUS; | |
3076 | get_page(page); | |
cd0d9137 NAQ |
3077 | if (type != NULL) |
3078 | *type = VM_FAULT_MINOR; | |
3079 | ||
f17abe9a AK |
3080 | return page; |
3081 | } | |
3082 | ||
3083 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
3084 | .nopage = kvm_vm_nopage, | |
3085 | }; | |
3086 | ||
3087 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
3088 | { | |
3089 | vma->vm_ops = &kvm_vm_vm_ops; | |
3090 | return 0; | |
3091 | } | |
3092 | ||
3093 | static struct file_operations kvm_vm_fops = { | |
3094 | .release = kvm_vm_release, | |
3095 | .unlocked_ioctl = kvm_vm_ioctl, | |
3096 | .compat_ioctl = kvm_vm_ioctl, | |
3097 | .mmap = kvm_vm_mmap, | |
3098 | }; | |
3099 | ||
3100 | static int kvm_dev_ioctl_create_vm(void) | |
3101 | { | |
3102 | int fd, r; | |
3103 | struct inode *inode; | |
3104 | struct file *file; | |
3105 | struct kvm *kvm; | |
3106 | ||
f17abe9a | 3107 | kvm = kvm_create_vm(); |
d6d28168 AK |
3108 | if (IS_ERR(kvm)) |
3109 | return PTR_ERR(kvm); | |
3110 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
3111 | if (r) { | |
3112 | kvm_destroy_vm(kvm); | |
3113 | return r; | |
f17abe9a AK |
3114 | } |
3115 | ||
bccf2150 | 3116 | kvm->filp = file; |
f17abe9a | 3117 | |
f17abe9a | 3118 | return fd; |
f17abe9a AK |
3119 | } |
3120 | ||
3121 | static long kvm_dev_ioctl(struct file *filp, | |
3122 | unsigned int ioctl, unsigned long arg) | |
3123 | { | |
3124 | void __user *argp = (void __user *)arg; | |
07c45a36 | 3125 | long r = -EINVAL; |
f17abe9a AK |
3126 | |
3127 | switch (ioctl) { | |
3128 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
3129 | r = -EINVAL; |
3130 | if (arg) | |
3131 | goto out; | |
f17abe9a AK |
3132 | r = KVM_API_VERSION; |
3133 | break; | |
3134 | case KVM_CREATE_VM: | |
f0fe5108 AK |
3135 | r = -EINVAL; |
3136 | if (arg) | |
3137 | goto out; | |
f17abe9a AK |
3138 | r = kvm_dev_ioctl_create_vm(); |
3139 | break; | |
6aa8b732 | 3140 | case KVM_GET_MSR_INDEX_LIST: { |
2f366987 | 3141 | struct kvm_msr_list __user *user_msr_list = argp; |
6aa8b732 AK |
3142 | struct kvm_msr_list msr_list; |
3143 | unsigned n; | |
3144 | ||
3145 | r = -EFAULT; | |
3146 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
3147 | goto out; | |
3148 | n = msr_list.nmsrs; | |
6f00e68f | 3149 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
6aa8b732 AK |
3150 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
3151 | goto out; | |
3152 | r = -E2BIG; | |
bf591b24 | 3153 | if (n < num_msrs_to_save) |
6aa8b732 AK |
3154 | goto out; |
3155 | r = -EFAULT; | |
3156 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
bf591b24 | 3157 | num_msrs_to_save * sizeof(u32))) |
6aa8b732 | 3158 | goto out; |
6f00e68f AK |
3159 | if (copy_to_user(user_msr_list->indices |
3160 | + num_msrs_to_save * sizeof(u32), | |
3161 | &emulated_msrs, | |
3162 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) | |
3163 | goto out; | |
6aa8b732 | 3164 | r = 0; |
cc1d8955 | 3165 | break; |
6aa8b732 | 3166 | } |
85f455f7 ED |
3167 | case KVM_CHECK_EXTENSION: { |
3168 | int ext = (long)argp; | |
3169 | ||
3170 | switch (ext) { | |
3171 | case KVM_CAP_IRQCHIP: | |
b6958ce4 | 3172 | case KVM_CAP_HLT: |
85f455f7 ED |
3173 | r = 1; |
3174 | break; | |
3175 | default: | |
3176 | r = 0; | |
3177 | break; | |
3178 | } | |
5d308f45 | 3179 | break; |
85f455f7 | 3180 | } |
07c45a36 AK |
3181 | case KVM_GET_VCPU_MMAP_SIZE: |
3182 | r = -EINVAL; | |
3183 | if (arg) | |
3184 | goto out; | |
039576c0 | 3185 | r = 2 * PAGE_SIZE; |
07c45a36 | 3186 | break; |
6aa8b732 AK |
3187 | default: |
3188 | ; | |
3189 | } | |
3190 | out: | |
3191 | return r; | |
3192 | } | |
3193 | ||
6aa8b732 | 3194 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
3195 | .unlocked_ioctl = kvm_dev_ioctl, |
3196 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
3197 | }; |
3198 | ||
3199 | static struct miscdevice kvm_dev = { | |
bbe4432e | 3200 | KVM_MINOR, |
6aa8b732 AK |
3201 | "kvm", |
3202 | &kvm_chardev_ops, | |
3203 | }; | |
3204 | ||
774c47f1 AK |
3205 | /* |
3206 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
3207 | * cached on it. | |
3208 | */ | |
3209 | static void decache_vcpus_on_cpu(int cpu) | |
3210 | { | |
3211 | struct kvm *vm; | |
3212 | struct kvm_vcpu *vcpu; | |
3213 | int i; | |
3214 | ||
3215 | spin_lock(&kvm_lock); | |
11ec2804 | 3216 | list_for_each_entry(vm, &vm_list, vm_list) |
774c47f1 | 3217 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
fb3f0f51 RR |
3218 | vcpu = vm->vcpus[i]; |
3219 | if (!vcpu) | |
3220 | continue; | |
774c47f1 AK |
3221 | /* |
3222 | * If the vcpu is locked, then it is running on some | |
3223 | * other cpu and therefore it is not cached on the | |
3224 | * cpu in question. | |
3225 | * | |
3226 | * If it's not locked, check the last cpu it executed | |
3227 | * on. | |
3228 | */ | |
3229 | if (mutex_trylock(&vcpu->mutex)) { | |
3230 | if (vcpu->cpu == cpu) { | |
cbdd1bea | 3231 | kvm_x86_ops->vcpu_decache(vcpu); |
774c47f1 AK |
3232 | vcpu->cpu = -1; |
3233 | } | |
3234 | mutex_unlock(&vcpu->mutex); | |
3235 | } | |
3236 | } | |
3237 | spin_unlock(&kvm_lock); | |
3238 | } | |
3239 | ||
1b6c0168 AK |
3240 | static void hardware_enable(void *junk) |
3241 | { | |
3242 | int cpu = raw_smp_processor_id(); | |
3243 | ||
3244 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
3245 | return; | |
3246 | cpu_set(cpu, cpus_hardware_enabled); | |
cbdd1bea | 3247 | kvm_x86_ops->hardware_enable(NULL); |
1b6c0168 AK |
3248 | } |
3249 | ||
3250 | static void hardware_disable(void *junk) | |
3251 | { | |
3252 | int cpu = raw_smp_processor_id(); | |
3253 | ||
3254 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
3255 | return; | |
3256 | cpu_clear(cpu, cpus_hardware_enabled); | |
3257 | decache_vcpus_on_cpu(cpu); | |
cbdd1bea | 3258 | kvm_x86_ops->hardware_disable(NULL); |
1b6c0168 AK |
3259 | } |
3260 | ||
774c47f1 AK |
3261 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
3262 | void *v) | |
3263 | { | |
3264 | int cpu = (long)v; | |
3265 | ||
3266 | switch (val) { | |
cec9ad27 AK |
3267 | case CPU_DYING: |
3268 | case CPU_DYING_FROZEN: | |
6ec8a856 AK |
3269 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
3270 | cpu); | |
3271 | hardware_disable(NULL); | |
3272 | break; | |
774c47f1 | 3273 | case CPU_UP_CANCELED: |
8bb78442 | 3274 | case CPU_UP_CANCELED_FROZEN: |
43934a38 JK |
3275 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
3276 | cpu); | |
1b6c0168 | 3277 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 3278 | break; |
43934a38 | 3279 | case CPU_ONLINE: |
8bb78442 | 3280 | case CPU_ONLINE_FROZEN: |
43934a38 JK |
3281 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
3282 | cpu); | |
1b6c0168 | 3283 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
3284 | break; |
3285 | } | |
3286 | return NOTIFY_OK; | |
3287 | } | |
3288 | ||
9a2b85c6 RR |
3289 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
3290 | void *v) | |
3291 | { | |
3292 | if (val == SYS_RESTART) { | |
3293 | /* | |
3294 | * Some (well, at least mine) BIOSes hang on reboot if | |
3295 | * in vmx root mode. | |
3296 | */ | |
3297 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
3298 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
3299 | } | |
3300 | return NOTIFY_OK; | |
3301 | } | |
3302 | ||
3303 | static struct notifier_block kvm_reboot_notifier = { | |
3304 | .notifier_call = kvm_reboot, | |
3305 | .priority = 0, | |
3306 | }; | |
3307 | ||
2eeb2e94 GH |
3308 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
3309 | { | |
3310 | memset(bus, 0, sizeof(*bus)); | |
3311 | } | |
3312 | ||
3313 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
3314 | { | |
3315 | int i; | |
3316 | ||
3317 | for (i = 0; i < bus->dev_count; i++) { | |
3318 | struct kvm_io_device *pos = bus->devs[i]; | |
3319 | ||
3320 | kvm_iodevice_destructor(pos); | |
3321 | } | |
3322 | } | |
3323 | ||
3324 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
3325 | { | |
3326 | int i; | |
3327 | ||
3328 | for (i = 0; i < bus->dev_count; i++) { | |
3329 | struct kvm_io_device *pos = bus->devs[i]; | |
3330 | ||
3331 | if (pos->in_range(pos, addr)) | |
3332 | return pos; | |
3333 | } | |
3334 | ||
3335 | return NULL; | |
3336 | } | |
3337 | ||
3338 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
3339 | { | |
3340 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
3341 | ||
3342 | bus->devs[bus->dev_count++] = dev; | |
3343 | } | |
3344 | ||
774c47f1 AK |
3345 | static struct notifier_block kvm_cpu_notifier = { |
3346 | .notifier_call = kvm_cpu_hotplug, | |
3347 | .priority = 20, /* must be > scheduler priority */ | |
3348 | }; | |
3349 | ||
1165f5fe AK |
3350 | static u64 stat_get(void *_offset) |
3351 | { | |
3352 | unsigned offset = (long)_offset; | |
3353 | u64 total = 0; | |
3354 | struct kvm *kvm; | |
3355 | struct kvm_vcpu *vcpu; | |
3356 | int i; | |
3357 | ||
3358 | spin_lock(&kvm_lock); | |
3359 | list_for_each_entry(kvm, &vm_list, vm_list) | |
3360 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
3361 | vcpu = kvm->vcpus[i]; |
3362 | if (vcpu) | |
3363 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
3364 | } |
3365 | spin_unlock(&kvm_lock); | |
3366 | return total; | |
3367 | } | |
3368 | ||
3dea7ca7 | 3369 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
1165f5fe | 3370 | |
6aa8b732 AK |
3371 | static __init void kvm_init_debug(void) |
3372 | { | |
3373 | struct kvm_stats_debugfs_item *p; | |
3374 | ||
8b6d44c7 | 3375 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 3376 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
3377 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
3378 | (void *)(long)p->offset, | |
3379 | &stat_fops); | |
6aa8b732 AK |
3380 | } |
3381 | ||
3382 | static void kvm_exit_debug(void) | |
3383 | { | |
3384 | struct kvm_stats_debugfs_item *p; | |
3385 | ||
3386 | for (p = debugfs_entries; p->name; ++p) | |
3387 | debugfs_remove(p->dentry); | |
3388 | debugfs_remove(debugfs_dir); | |
3389 | } | |
3390 | ||
59ae6c6b AK |
3391 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
3392 | { | |
4267c41a | 3393 | hardware_disable(NULL); |
59ae6c6b AK |
3394 | return 0; |
3395 | } | |
3396 | ||
3397 | static int kvm_resume(struct sys_device *dev) | |
3398 | { | |
4267c41a | 3399 | hardware_enable(NULL); |
59ae6c6b AK |
3400 | return 0; |
3401 | } | |
3402 | ||
3403 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 3404 | .name = "kvm", |
59ae6c6b AK |
3405 | .suspend = kvm_suspend, |
3406 | .resume = kvm_resume, | |
3407 | }; | |
3408 | ||
3409 | static struct sys_device kvm_sysdev = { | |
3410 | .id = 0, | |
3411 | .cls = &kvm_sysdev_class, | |
3412 | }; | |
3413 | ||
6aa8b732 AK |
3414 | hpa_t bad_page_address; |
3415 | ||
15ad7146 AK |
3416 | static inline |
3417 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
3418 | { | |
3419 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
3420 | } | |
3421 | ||
3422 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
3423 | { | |
3424 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
3425 | ||
cbdd1bea | 3426 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 AK |
3427 | } |
3428 | ||
3429 | static void kvm_sched_out(struct preempt_notifier *pn, | |
3430 | struct task_struct *next) | |
3431 | { | |
3432 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
3433 | ||
cbdd1bea | 3434 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
3435 | } |
3436 | ||
cbdd1bea | 3437 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
c16f862d | 3438 | struct module *module) |
6aa8b732 AK |
3439 | { |
3440 | int r; | |
002c7f7c | 3441 | int cpu; |
6aa8b732 | 3442 | |
cbdd1bea | 3443 | if (kvm_x86_ops) { |
09db28b8 YI |
3444 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
3445 | return -EEXIST; | |
3446 | } | |
3447 | ||
e097f35c | 3448 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
3449 | printk(KERN_ERR "kvm: no hardware support\n"); |
3450 | return -EOPNOTSUPP; | |
3451 | } | |
e097f35c | 3452 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
3453 | printk(KERN_ERR "kvm: disabled by bios\n"); |
3454 | return -EOPNOTSUPP; | |
3455 | } | |
3456 | ||
cbdd1bea | 3457 | kvm_x86_ops = ops; |
e097f35c | 3458 | |
cbdd1bea | 3459 | r = kvm_x86_ops->hardware_setup(); |
6aa8b732 | 3460 | if (r < 0) |
ca45aaae | 3461 | goto out; |
6aa8b732 | 3462 | |
002c7f7c YS |
3463 | for_each_online_cpu(cpu) { |
3464 | smp_call_function_single(cpu, | |
cbdd1bea | 3465 | kvm_x86_ops->check_processor_compatibility, |
002c7f7c YS |
3466 | &r, 0, 1); |
3467 | if (r < 0) | |
3468 | goto out_free_0; | |
3469 | } | |
3470 | ||
1b6c0168 | 3471 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
3472 | r = register_cpu_notifier(&kvm_cpu_notifier); |
3473 | if (r) | |
3474 | goto out_free_1; | |
6aa8b732 AK |
3475 | register_reboot_notifier(&kvm_reboot_notifier); |
3476 | ||
59ae6c6b AK |
3477 | r = sysdev_class_register(&kvm_sysdev_class); |
3478 | if (r) | |
3479 | goto out_free_2; | |
3480 | ||
3481 | r = sysdev_register(&kvm_sysdev); | |
3482 | if (r) | |
3483 | goto out_free_3; | |
3484 | ||
c16f862d RR |
3485 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
3486 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
3487 | __alignof__(struct kvm_vcpu), 0, 0); | |
3488 | if (!kvm_vcpu_cache) { | |
3489 | r = -ENOMEM; | |
3490 | goto out_free_4; | |
3491 | } | |
3492 | ||
6aa8b732 AK |
3493 | kvm_chardev_ops.owner = module; |
3494 | ||
3495 | r = misc_register(&kvm_dev); | |
3496 | if (r) { | |
3497 | printk (KERN_ERR "kvm: misc device register failed\n"); | |
3498 | goto out_free; | |
3499 | } | |
3500 | ||
15ad7146 AK |
3501 | kvm_preempt_ops.sched_in = kvm_sched_in; |
3502 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
3503 | ||
c7addb90 AK |
3504 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
3505 | ||
3506 | return 0; | |
6aa8b732 AK |
3507 | |
3508 | out_free: | |
c16f862d RR |
3509 | kmem_cache_destroy(kvm_vcpu_cache); |
3510 | out_free_4: | |
59ae6c6b AK |
3511 | sysdev_unregister(&kvm_sysdev); |
3512 | out_free_3: | |
3513 | sysdev_class_unregister(&kvm_sysdev_class); | |
3514 | out_free_2: | |
6aa8b732 | 3515 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
3516 | unregister_cpu_notifier(&kvm_cpu_notifier); |
3517 | out_free_1: | |
1b6c0168 | 3518 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 3519 | out_free_0: |
cbdd1bea | 3520 | kvm_x86_ops->hardware_unsetup(); |
ca45aaae | 3521 | out: |
cbdd1bea | 3522 | kvm_x86_ops = NULL; |
6aa8b732 AK |
3523 | return r; |
3524 | } | |
3525 | ||
cbdd1bea | 3526 | void kvm_exit_x86(void) |
6aa8b732 AK |
3527 | { |
3528 | misc_deregister(&kvm_dev); | |
c16f862d | 3529 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
3530 | sysdev_unregister(&kvm_sysdev); |
3531 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 3532 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 3533 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 3534 | on_each_cpu(hardware_disable, NULL, 0, 1); |
cbdd1bea CE |
3535 | kvm_x86_ops->hardware_unsetup(); |
3536 | kvm_x86_ops = NULL; | |
6aa8b732 AK |
3537 | } |
3538 | ||
3539 | static __init int kvm_init(void) | |
3540 | { | |
3541 | static struct page *bad_page; | |
37e29d90 AK |
3542 | int r; |
3543 | ||
b5a33a75 AK |
3544 | r = kvm_mmu_module_init(); |
3545 | if (r) | |
3546 | goto out4; | |
3547 | ||
6aa8b732 AK |
3548 | kvm_init_debug(); |
3549 | ||
bf591b24 MR |
3550 | kvm_init_msr_list(); |
3551 | ||
6aa8b732 AK |
3552 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { |
3553 | r = -ENOMEM; | |
3554 | goto out; | |
3555 | } | |
3556 | ||
3557 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; | |
3558 | memset(__va(bad_page_address), 0, PAGE_SIZE); | |
3559 | ||
58e690e6 | 3560 | return 0; |
6aa8b732 AK |
3561 | |
3562 | out: | |
3563 | kvm_exit_debug(); | |
b5a33a75 AK |
3564 | kvm_mmu_module_exit(); |
3565 | out4: | |
6aa8b732 AK |
3566 | return r; |
3567 | } | |
3568 | ||
3569 | static __exit void kvm_exit(void) | |
3570 | { | |
3571 | kvm_exit_debug(); | |
3572 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); | |
b5a33a75 | 3573 | kvm_mmu_module_exit(); |
6aa8b732 AK |
3574 | } |
3575 | ||
3576 | module_init(kvm_init) | |
3577 | module_exit(kvm_exit) | |
3578 | ||
cbdd1bea CE |
3579 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
3580 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |