]>
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" | |
19 | ||
20 | #include <linux/kvm.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/errno.h> | |
23 | #include <asm/processor.h> | |
24 | #include <linux/percpu.h> | |
25 | #include <linux/gfp.h> | |
26 | #include <asm/msr.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/miscdevice.h> | |
29 | #include <linux/vmalloc.h> | |
30 | #include <asm/uaccess.h> | |
31 | #include <linux/reboot.h> | |
32 | #include <asm/io.h> | |
33 | #include <linux/debugfs.h> | |
34 | #include <linux/highmem.h> | |
35 | #include <linux/file.h> | |
36 | #include <asm/desc.h> | |
37 | ||
38 | #include "x86_emulate.h" | |
39 | #include "segment_descriptor.h" | |
40 | ||
41 | MODULE_AUTHOR("Qumranet"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
44 | struct kvm_arch_ops *kvm_arch_ops; | |
45 | struct kvm_stat kvm_stat; | |
46 | EXPORT_SYMBOL_GPL(kvm_stat); | |
47 | ||
48 | static struct kvm_stats_debugfs_item { | |
49 | const char *name; | |
50 | u32 *data; | |
51 | struct dentry *dentry; | |
52 | } debugfs_entries[] = { | |
53 | { "pf_fixed", &kvm_stat.pf_fixed }, | |
54 | { "pf_guest", &kvm_stat.pf_guest }, | |
55 | { "tlb_flush", &kvm_stat.tlb_flush }, | |
56 | { "invlpg", &kvm_stat.invlpg }, | |
57 | { "exits", &kvm_stat.exits }, | |
58 | { "io_exits", &kvm_stat.io_exits }, | |
59 | { "mmio_exits", &kvm_stat.mmio_exits }, | |
60 | { "signal_exits", &kvm_stat.signal_exits }, | |
c1150d8c DL |
61 | { "irq_window", &kvm_stat.irq_window_exits }, |
62 | { "halt_exits", &kvm_stat.halt_exits }, | |
63 | { "request_irq", &kvm_stat.request_irq_exits }, | |
6aa8b732 AK |
64 | { "irq_exits", &kvm_stat.irq_exits }, |
65 | { 0, 0 } | |
66 | }; | |
67 | ||
68 | static struct dentry *debugfs_dir; | |
69 | ||
70 | #define MAX_IO_MSRS 256 | |
71 | ||
72 | #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL | |
73 | #define LMSW_GUEST_MASK 0x0eULL | |
74 | #define CR4_RESEVED_BITS (~((1ULL << 11) - 1)) | |
75 | #define CR8_RESEVED_BITS (~0x0fULL) | |
76 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe | |
77 | ||
05b3e0c2 | 78 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
79 | // LDT or TSS descriptor in the GDT. 16 bytes. |
80 | struct segment_descriptor_64 { | |
81 | struct segment_descriptor s; | |
82 | u32 base_higher; | |
83 | u32 pad_zero; | |
84 | }; | |
85 | ||
86 | #endif | |
87 | ||
88 | unsigned long segment_base(u16 selector) | |
89 | { | |
90 | struct descriptor_table gdt; | |
91 | struct segment_descriptor *d; | |
92 | unsigned long table_base; | |
93 | typedef unsigned long ul; | |
94 | unsigned long v; | |
95 | ||
96 | if (selector == 0) | |
97 | return 0; | |
98 | ||
99 | asm ("sgdt %0" : "=m"(gdt)); | |
100 | table_base = gdt.base; | |
101 | ||
102 | if (selector & 4) { /* from ldt */ | |
103 | u16 ldt_selector; | |
104 | ||
105 | asm ("sldt %0" : "=g"(ldt_selector)); | |
106 | table_base = segment_base(ldt_selector); | |
107 | } | |
108 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); | |
109 | v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24); | |
05b3e0c2 | 110 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
111 | if (d->system == 0 |
112 | && (d->type == 2 || d->type == 9 || d->type == 11)) | |
113 | v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32; | |
114 | #endif | |
115 | return v; | |
116 | } | |
117 | EXPORT_SYMBOL_GPL(segment_base); | |
118 | ||
5aacf0ca JM |
119 | static inline int valid_vcpu(int n) |
120 | { | |
121 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
122 | } | |
123 | ||
6aa8b732 AK |
124 | int kvm_read_guest(struct kvm_vcpu *vcpu, |
125 | gva_t addr, | |
126 | unsigned long size, | |
127 | void *dest) | |
128 | { | |
129 | unsigned char *host_buf = dest; | |
130 | unsigned long req_size = size; | |
131 | ||
132 | while (size) { | |
133 | hpa_t paddr; | |
134 | unsigned now; | |
135 | unsigned offset; | |
136 | hva_t guest_buf; | |
137 | ||
138 | paddr = gva_to_hpa(vcpu, addr); | |
139 | ||
140 | if (is_error_hpa(paddr)) | |
141 | break; | |
142 | ||
143 | guest_buf = (hva_t)kmap_atomic( | |
144 | pfn_to_page(paddr >> PAGE_SHIFT), | |
145 | KM_USER0); | |
146 | offset = addr & ~PAGE_MASK; | |
147 | guest_buf |= offset; | |
148 | now = min(size, PAGE_SIZE - offset); | |
149 | memcpy(host_buf, (void*)guest_buf, now); | |
150 | host_buf += now; | |
151 | addr += now; | |
152 | size -= now; | |
153 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); | |
154 | } | |
155 | return req_size - size; | |
156 | } | |
157 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
158 | ||
159 | int kvm_write_guest(struct kvm_vcpu *vcpu, | |
160 | gva_t addr, | |
161 | unsigned long size, | |
162 | void *data) | |
163 | { | |
164 | unsigned char *host_buf = data; | |
165 | unsigned long req_size = size; | |
166 | ||
167 | while (size) { | |
168 | hpa_t paddr; | |
169 | unsigned now; | |
170 | unsigned offset; | |
171 | hva_t guest_buf; | |
172 | ||
173 | paddr = gva_to_hpa(vcpu, addr); | |
174 | ||
175 | if (is_error_hpa(paddr)) | |
176 | break; | |
177 | ||
178 | guest_buf = (hva_t)kmap_atomic( | |
179 | pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0); | |
180 | offset = addr & ~PAGE_MASK; | |
181 | guest_buf |= offset; | |
182 | now = min(size, PAGE_SIZE - offset); | |
183 | memcpy((void*)guest_buf, host_buf, now); | |
184 | host_buf += now; | |
185 | addr += now; | |
186 | size -= now; | |
187 | kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0); | |
188 | } | |
189 | return req_size - size; | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(kvm_write_guest); | |
192 | ||
193 | static int vcpu_slot(struct kvm_vcpu *vcpu) | |
194 | { | |
195 | return vcpu - vcpu->kvm->vcpus; | |
196 | } | |
197 | ||
198 | /* | |
199 | * Switches to specified vcpu, until a matching vcpu_put() | |
200 | */ | |
201 | static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot) | |
202 | { | |
203 | struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot]; | |
204 | ||
205 | mutex_lock(&vcpu->mutex); | |
206 | if (unlikely(!vcpu->vmcs)) { | |
207 | mutex_unlock(&vcpu->mutex); | |
208 | return 0; | |
209 | } | |
210 | return kvm_arch_ops->vcpu_load(vcpu); | |
211 | } | |
212 | ||
213 | static void vcpu_put(struct kvm_vcpu *vcpu) | |
214 | { | |
215 | kvm_arch_ops->vcpu_put(vcpu); | |
6aa8b732 AK |
216 | mutex_unlock(&vcpu->mutex); |
217 | } | |
218 | ||
219 | static int kvm_dev_open(struct inode *inode, struct file *filp) | |
220 | { | |
221 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
222 | int i; | |
223 | ||
224 | if (!kvm) | |
225 | return -ENOMEM; | |
226 | ||
227 | spin_lock_init(&kvm->lock); | |
228 | INIT_LIST_HEAD(&kvm->active_mmu_pages); | |
229 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
230 | struct kvm_vcpu *vcpu = &kvm->vcpus[i]; | |
231 | ||
232 | mutex_init(&vcpu->mutex); | |
233 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
234 | INIT_LIST_HEAD(&vcpu->free_pages); | |
235 | } | |
236 | filp->private_data = kvm; | |
237 | return 0; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Free any memory in @free but not in @dont. | |
242 | */ | |
243 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
244 | struct kvm_memory_slot *dont) | |
245 | { | |
246 | int i; | |
247 | ||
248 | if (!dont || free->phys_mem != dont->phys_mem) | |
249 | if (free->phys_mem) { | |
250 | for (i = 0; i < free->npages; ++i) | |
55a54f79 AK |
251 | if (free->phys_mem[i]) |
252 | __free_page(free->phys_mem[i]); | |
6aa8b732 AK |
253 | vfree(free->phys_mem); |
254 | } | |
255 | ||
256 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
257 | vfree(free->dirty_bitmap); | |
258 | ||
259 | free->phys_mem = 0; | |
260 | free->npages = 0; | |
261 | free->dirty_bitmap = 0; | |
262 | } | |
263 | ||
264 | static void kvm_free_physmem(struct kvm *kvm) | |
265 | { | |
266 | int i; | |
267 | ||
268 | for (i = 0; i < kvm->nmemslots; ++i) | |
269 | kvm_free_physmem_slot(&kvm->memslots[i], 0); | |
270 | } | |
271 | ||
272 | static void kvm_free_vcpu(struct kvm_vcpu *vcpu) | |
273 | { | |
274 | kvm_arch_ops->vcpu_free(vcpu); | |
275 | kvm_mmu_destroy(vcpu); | |
276 | } | |
277 | ||
278 | static void kvm_free_vcpus(struct kvm *kvm) | |
279 | { | |
280 | unsigned int i; | |
281 | ||
282 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
283 | kvm_free_vcpu(&kvm->vcpus[i]); | |
284 | } | |
285 | ||
286 | static int kvm_dev_release(struct inode *inode, struct file *filp) | |
287 | { | |
288 | struct kvm *kvm = filp->private_data; | |
289 | ||
290 | kvm_free_vcpus(kvm); | |
291 | kvm_free_physmem(kvm); | |
292 | kfree(kvm); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static void inject_gp(struct kvm_vcpu *vcpu) | |
297 | { | |
298 | kvm_arch_ops->inject_gp(vcpu, 0); | |
299 | } | |
300 | ||
1342d353 AK |
301 | /* |
302 | * Load the pae pdptrs. Return true is they are all valid. | |
303 | */ | |
304 | static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
6aa8b732 AK |
305 | { |
306 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
1342d353 | 307 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
6aa8b732 AK |
308 | int i; |
309 | u64 pdpte; | |
310 | u64 *pdpt; | |
1342d353 | 311 | int ret; |
6aa8b732 AK |
312 | struct kvm_memory_slot *memslot; |
313 | ||
314 | spin_lock(&vcpu->kvm->lock); | |
315 | memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn); | |
316 | /* FIXME: !memslot - emulate? 0xff? */ | |
317 | pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0); | |
318 | ||
1342d353 | 319 | ret = 1; |
6aa8b732 AK |
320 | for (i = 0; i < 4; ++i) { |
321 | pdpte = pdpt[offset + i]; | |
1342d353 AK |
322 | if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) { |
323 | ret = 0; | |
324 | goto out; | |
325 | } | |
6aa8b732 AK |
326 | } |
327 | ||
1342d353 AK |
328 | for (i = 0; i < 4; ++i) |
329 | vcpu->pdptrs[i] = pdpt[offset + i]; | |
330 | ||
331 | out: | |
6aa8b732 AK |
332 | kunmap_atomic(pdpt, KM_USER0); |
333 | spin_unlock(&vcpu->kvm->lock); | |
334 | ||
1342d353 | 335 | return ret; |
6aa8b732 AK |
336 | } |
337 | ||
338 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
339 | { | |
340 | if (cr0 & CR0_RESEVED_BITS) { | |
341 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", | |
342 | cr0, vcpu->cr0); | |
343 | inject_gp(vcpu); | |
344 | return; | |
345 | } | |
346 | ||
347 | if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) { | |
348 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); | |
349 | inject_gp(vcpu); | |
350 | return; | |
351 | } | |
352 | ||
353 | if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) { | |
354 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " | |
355 | "and a clear PE flag\n"); | |
356 | inject_gp(vcpu); | |
357 | return; | |
358 | } | |
359 | ||
360 | if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) { | |
05b3e0c2 | 361 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
362 | if ((vcpu->shadow_efer & EFER_LME)) { |
363 | int cs_db, cs_l; | |
364 | ||
365 | if (!is_pae(vcpu)) { | |
366 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
367 | "in long mode while PAE is disabled\n"); | |
368 | inject_gp(vcpu); | |
369 | return; | |
370 | } | |
371 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
372 | if (cs_l) { | |
373 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
374 | "in long mode while CS.L == 1\n"); | |
375 | inject_gp(vcpu); | |
376 | return; | |
377 | ||
378 | } | |
379 | } else | |
380 | #endif | |
1342d353 | 381 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
382 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
383 | "reserved bits\n"); | |
384 | inject_gp(vcpu); | |
385 | return; | |
386 | } | |
387 | ||
388 | } | |
389 | ||
390 | kvm_arch_ops->set_cr0(vcpu, cr0); | |
391 | vcpu->cr0 = cr0; | |
392 | ||
393 | spin_lock(&vcpu->kvm->lock); | |
394 | kvm_mmu_reset_context(vcpu); | |
395 | spin_unlock(&vcpu->kvm->lock); | |
396 | return; | |
397 | } | |
398 | EXPORT_SYMBOL_GPL(set_cr0); | |
399 | ||
400 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) | |
401 | { | |
399badf3 | 402 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
6aa8b732 AK |
403 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); |
404 | } | |
405 | EXPORT_SYMBOL_GPL(lmsw); | |
406 | ||
407 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
408 | { | |
409 | if (cr4 & CR4_RESEVED_BITS) { | |
410 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); | |
411 | inject_gp(vcpu); | |
412 | return; | |
413 | } | |
414 | ||
a9058ecd | 415 | if (is_long_mode(vcpu)) { |
6aa8b732 AK |
416 | if (!(cr4 & CR4_PAE_MASK)) { |
417 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " | |
418 | "in long mode\n"); | |
419 | inject_gp(vcpu); | |
420 | return; | |
421 | } | |
422 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK) | |
1342d353 | 423 | && !load_pdptrs(vcpu, vcpu->cr3)) { |
6aa8b732 AK |
424 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
425 | inject_gp(vcpu); | |
426 | } | |
427 | ||
428 | if (cr4 & CR4_VMXE_MASK) { | |
429 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); | |
430 | inject_gp(vcpu); | |
431 | return; | |
432 | } | |
433 | kvm_arch_ops->set_cr4(vcpu, cr4); | |
434 | spin_lock(&vcpu->kvm->lock); | |
435 | kvm_mmu_reset_context(vcpu); | |
436 | spin_unlock(&vcpu->kvm->lock); | |
437 | } | |
438 | EXPORT_SYMBOL_GPL(set_cr4); | |
439 | ||
440 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
441 | { | |
a9058ecd | 442 | if (is_long_mode(vcpu)) { |
6aa8b732 AK |
443 | if ( cr3 & CR3_L_MODE_RESEVED_BITS) { |
444 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
445 | inject_gp(vcpu); | |
446 | return; | |
447 | } | |
448 | } else { | |
449 | if (cr3 & CR3_RESEVED_BITS) { | |
450 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
451 | inject_gp(vcpu); | |
452 | return; | |
453 | } | |
454 | if (is_paging(vcpu) && is_pae(vcpu) && | |
1342d353 | 455 | !load_pdptrs(vcpu, cr3)) { |
6aa8b732 AK |
456 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
457 | "reserved bits\n"); | |
458 | inject_gp(vcpu); | |
459 | return; | |
460 | } | |
461 | } | |
462 | ||
463 | vcpu->cr3 = cr3; | |
464 | spin_lock(&vcpu->kvm->lock); | |
465 | vcpu->mmu.new_cr3(vcpu); | |
466 | spin_unlock(&vcpu->kvm->lock); | |
467 | } | |
468 | EXPORT_SYMBOL_GPL(set_cr3); | |
469 | ||
470 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) | |
471 | { | |
472 | if ( cr8 & CR8_RESEVED_BITS) { | |
473 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); | |
474 | inject_gp(vcpu); | |
475 | return; | |
476 | } | |
477 | vcpu->cr8 = cr8; | |
478 | } | |
479 | EXPORT_SYMBOL_GPL(set_cr8); | |
480 | ||
481 | void fx_init(struct kvm_vcpu *vcpu) | |
482 | { | |
483 | struct __attribute__ ((__packed__)) fx_image_s { | |
484 | u16 control; //fcw | |
485 | u16 status; //fsw | |
486 | u16 tag; // ftw | |
487 | u16 opcode; //fop | |
488 | u64 ip; // fpu ip | |
489 | u64 operand;// fpu dp | |
490 | u32 mxcsr; | |
491 | u32 mxcsr_mask; | |
492 | ||
493 | } *fx_image; | |
494 | ||
495 | fx_save(vcpu->host_fx_image); | |
496 | fpu_init(); | |
497 | fx_save(vcpu->guest_fx_image); | |
498 | fx_restore(vcpu->host_fx_image); | |
499 | ||
500 | fx_image = (struct fx_image_s *)vcpu->guest_fx_image; | |
501 | fx_image->mxcsr = 0x1f80; | |
502 | memset(vcpu->guest_fx_image + sizeof(struct fx_image_s), | |
503 | 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s)); | |
504 | } | |
505 | EXPORT_SYMBOL_GPL(fx_init); | |
506 | ||
507 | /* | |
508 | * Creates some virtual cpus. Good luck creating more than one. | |
509 | */ | |
510 | static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n) | |
511 | { | |
512 | int r; | |
513 | struct kvm_vcpu *vcpu; | |
514 | ||
515 | r = -EINVAL; | |
5aacf0ca | 516 | if (!valid_vcpu(n)) |
6aa8b732 AK |
517 | goto out; |
518 | ||
519 | vcpu = &kvm->vcpus[n]; | |
520 | ||
521 | mutex_lock(&vcpu->mutex); | |
522 | ||
523 | if (vcpu->vmcs) { | |
524 | mutex_unlock(&vcpu->mutex); | |
525 | return -EEXIST; | |
526 | } | |
527 | ||
528 | vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf, | |
529 | FX_IMAGE_ALIGN); | |
530 | vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE; | |
531 | ||
532 | vcpu->cpu = -1; /* First load will set up TR */ | |
533 | vcpu->kvm = kvm; | |
534 | r = kvm_arch_ops->vcpu_create(vcpu); | |
535 | if (r < 0) | |
536 | goto out_free_vcpus; | |
537 | ||
8018c27b IM |
538 | r = kvm_mmu_create(vcpu); |
539 | if (r < 0) | |
540 | goto out_free_vcpus; | |
6aa8b732 | 541 | |
8018c27b IM |
542 | kvm_arch_ops->vcpu_load(vcpu); |
543 | r = kvm_mmu_setup(vcpu); | |
6aa8b732 | 544 | if (r >= 0) |
8018c27b | 545 | r = kvm_arch_ops->vcpu_setup(vcpu); |
6aa8b732 AK |
546 | vcpu_put(vcpu); |
547 | ||
548 | if (r < 0) | |
549 | goto out_free_vcpus; | |
550 | ||
551 | return 0; | |
552 | ||
553 | out_free_vcpus: | |
554 | kvm_free_vcpu(vcpu); | |
555 | mutex_unlock(&vcpu->mutex); | |
556 | out: | |
557 | return r; | |
558 | } | |
559 | ||
560 | /* | |
561 | * Allocate some memory and give it an address in the guest physical address | |
562 | * space. | |
563 | * | |
564 | * Discontiguous memory is allowed, mostly for framebuffers. | |
565 | */ | |
566 | static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm, | |
567 | struct kvm_memory_region *mem) | |
568 | { | |
569 | int r; | |
570 | gfn_t base_gfn; | |
571 | unsigned long npages; | |
572 | unsigned long i; | |
573 | struct kvm_memory_slot *memslot; | |
574 | struct kvm_memory_slot old, new; | |
575 | int memory_config_version; | |
576 | ||
577 | r = -EINVAL; | |
578 | /* General sanity checks */ | |
579 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
580 | goto out; | |
581 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
582 | goto out; | |
583 | if (mem->slot >= KVM_MEMORY_SLOTS) | |
584 | goto out; | |
585 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
586 | goto out; | |
587 | ||
588 | memslot = &kvm->memslots[mem->slot]; | |
589 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
590 | npages = mem->memory_size >> PAGE_SHIFT; | |
591 | ||
592 | if (!npages) | |
593 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
594 | ||
595 | raced: | |
596 | spin_lock(&kvm->lock); | |
597 | ||
598 | memory_config_version = kvm->memory_config_version; | |
599 | new = old = *memslot; | |
600 | ||
601 | new.base_gfn = base_gfn; | |
602 | new.npages = npages; | |
603 | new.flags = mem->flags; | |
604 | ||
605 | /* Disallow changing a memory slot's size. */ | |
606 | r = -EINVAL; | |
607 | if (npages && old.npages && npages != old.npages) | |
608 | goto out_unlock; | |
609 | ||
610 | /* Check for overlaps */ | |
611 | r = -EEXIST; | |
612 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
613 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
614 | ||
615 | if (s == memslot) | |
616 | continue; | |
617 | if (!((base_gfn + npages <= s->base_gfn) || | |
618 | (base_gfn >= s->base_gfn + s->npages))) | |
619 | goto out_unlock; | |
620 | } | |
621 | /* | |
622 | * Do memory allocations outside lock. memory_config_version will | |
623 | * detect any races. | |
624 | */ | |
625 | spin_unlock(&kvm->lock); | |
626 | ||
627 | /* Deallocate if slot is being removed */ | |
628 | if (!npages) | |
629 | new.phys_mem = 0; | |
630 | ||
631 | /* Free page dirty bitmap if unneeded */ | |
632 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
633 | new.dirty_bitmap = 0; | |
634 | ||
635 | r = -ENOMEM; | |
636 | ||
637 | /* Allocate if a slot is being created */ | |
638 | if (npages && !new.phys_mem) { | |
639 | new.phys_mem = vmalloc(npages * sizeof(struct page *)); | |
640 | ||
641 | if (!new.phys_mem) | |
642 | goto out_free; | |
643 | ||
644 | memset(new.phys_mem, 0, npages * sizeof(struct page *)); | |
645 | for (i = 0; i < npages; ++i) { | |
646 | new.phys_mem[i] = alloc_page(GFP_HIGHUSER | |
647 | | __GFP_ZERO); | |
648 | if (!new.phys_mem[i]) | |
649 | goto out_free; | |
cd4a4e53 | 650 | new.phys_mem[i]->private = 0; |
6aa8b732 AK |
651 | } |
652 | } | |
653 | ||
654 | /* Allocate page dirty bitmap if needed */ | |
655 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
656 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
657 | ||
658 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
659 | if (!new.dirty_bitmap) | |
660 | goto out_free; | |
661 | memset(new.dirty_bitmap, 0, dirty_bytes); | |
662 | } | |
663 | ||
664 | spin_lock(&kvm->lock); | |
665 | ||
666 | if (memory_config_version != kvm->memory_config_version) { | |
667 | spin_unlock(&kvm->lock); | |
668 | kvm_free_physmem_slot(&new, &old); | |
669 | goto raced; | |
670 | } | |
671 | ||
672 | r = -EAGAIN; | |
673 | if (kvm->busy) | |
674 | goto out_unlock; | |
675 | ||
676 | if (mem->slot >= kvm->nmemslots) | |
677 | kvm->nmemslots = mem->slot + 1; | |
678 | ||
679 | *memslot = new; | |
680 | ++kvm->memory_config_version; | |
681 | ||
682 | spin_unlock(&kvm->lock); | |
683 | ||
684 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
685 | struct kvm_vcpu *vcpu; | |
686 | ||
687 | vcpu = vcpu_load(kvm, i); | |
688 | if (!vcpu) | |
689 | continue; | |
690 | kvm_mmu_reset_context(vcpu); | |
691 | vcpu_put(vcpu); | |
692 | } | |
693 | ||
694 | kvm_free_physmem_slot(&old, &new); | |
695 | return 0; | |
696 | ||
697 | out_unlock: | |
698 | spin_unlock(&kvm->lock); | |
699 | out_free: | |
700 | kvm_free_physmem_slot(&new, &old); | |
701 | out: | |
702 | return r; | |
703 | } | |
704 | ||
705 | /* | |
706 | * Get (and clear) the dirty memory log for a memory slot. | |
707 | */ | |
708 | static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm, | |
709 | struct kvm_dirty_log *log) | |
710 | { | |
711 | struct kvm_memory_slot *memslot; | |
712 | int r, i; | |
713 | int n; | |
714 | unsigned long any = 0; | |
715 | ||
716 | spin_lock(&kvm->lock); | |
717 | ||
718 | /* | |
719 | * Prevent changes to guest memory configuration even while the lock | |
720 | * is not taken. | |
721 | */ | |
722 | ++kvm->busy; | |
723 | spin_unlock(&kvm->lock); | |
724 | r = -EINVAL; | |
725 | if (log->slot >= KVM_MEMORY_SLOTS) | |
726 | goto out; | |
727 | ||
728 | memslot = &kvm->memslots[log->slot]; | |
729 | r = -ENOENT; | |
730 | if (!memslot->dirty_bitmap) | |
731 | goto out; | |
732 | ||
733 | n = ALIGN(memslot->npages, 8) / 8; | |
734 | ||
735 | for (i = 0; !any && i < n; ++i) | |
736 | any = memslot->dirty_bitmap[i]; | |
737 | ||
738 | r = -EFAULT; | |
739 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
740 | goto out; | |
741 | ||
742 | ||
743 | if (any) { | |
744 | spin_lock(&kvm->lock); | |
745 | kvm_mmu_slot_remove_write_access(kvm, log->slot); | |
746 | spin_unlock(&kvm->lock); | |
747 | memset(memslot->dirty_bitmap, 0, n); | |
748 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
749 | struct kvm_vcpu *vcpu = vcpu_load(kvm, i); | |
750 | ||
751 | if (!vcpu) | |
752 | continue; | |
753 | kvm_arch_ops->tlb_flush(vcpu); | |
754 | vcpu_put(vcpu); | |
755 | } | |
756 | } | |
757 | ||
758 | r = 0; | |
759 | ||
760 | out: | |
761 | spin_lock(&kvm->lock); | |
762 | --kvm->busy; | |
763 | spin_unlock(&kvm->lock); | |
764 | return r; | |
765 | } | |
766 | ||
767 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
768 | { | |
769 | int i; | |
770 | ||
771 | for (i = 0; i < kvm->nmemslots; ++i) { | |
772 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
773 | ||
774 | if (gfn >= memslot->base_gfn | |
775 | && gfn < memslot->base_gfn + memslot->npages) | |
776 | return memslot; | |
777 | } | |
778 | return 0; | |
779 | } | |
780 | EXPORT_SYMBOL_GPL(gfn_to_memslot); | |
781 | ||
782 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) | |
783 | { | |
784 | int i; | |
785 | struct kvm_memory_slot *memslot = 0; | |
786 | unsigned long rel_gfn; | |
787 | ||
788 | for (i = 0; i < kvm->nmemslots; ++i) { | |
789 | memslot = &kvm->memslots[i]; | |
790 | ||
791 | if (gfn >= memslot->base_gfn | |
792 | && gfn < memslot->base_gfn + memslot->npages) { | |
793 | ||
794 | if (!memslot || !memslot->dirty_bitmap) | |
795 | return; | |
796 | ||
797 | rel_gfn = gfn - memslot->base_gfn; | |
798 | ||
799 | /* avoid RMW */ | |
800 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
801 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
802 | return; | |
803 | } | |
804 | } | |
805 | } | |
806 | ||
807 | static int emulator_read_std(unsigned long addr, | |
808 | unsigned long *val, | |
809 | unsigned int bytes, | |
810 | struct x86_emulate_ctxt *ctxt) | |
811 | { | |
812 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
813 | void *data = val; | |
814 | ||
815 | while (bytes) { | |
816 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
817 | unsigned offset = addr & (PAGE_SIZE-1); | |
818 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
819 | unsigned long pfn; | |
820 | struct kvm_memory_slot *memslot; | |
821 | void *page; | |
822 | ||
823 | if (gpa == UNMAPPED_GVA) | |
824 | return X86EMUL_PROPAGATE_FAULT; | |
825 | pfn = gpa >> PAGE_SHIFT; | |
826 | memslot = gfn_to_memslot(vcpu->kvm, pfn); | |
827 | if (!memslot) | |
828 | return X86EMUL_UNHANDLEABLE; | |
829 | page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0); | |
830 | ||
831 | memcpy(data, page + offset, tocopy); | |
832 | ||
833 | kunmap_atomic(page, KM_USER0); | |
834 | ||
835 | bytes -= tocopy; | |
836 | data += tocopy; | |
837 | addr += tocopy; | |
838 | } | |
839 | ||
840 | return X86EMUL_CONTINUE; | |
841 | } | |
842 | ||
843 | static int emulator_write_std(unsigned long addr, | |
844 | unsigned long val, | |
845 | unsigned int bytes, | |
846 | struct x86_emulate_ctxt *ctxt) | |
847 | { | |
848 | printk(KERN_ERR "emulator_write_std: addr %lx n %d\n", | |
849 | addr, bytes); | |
850 | return X86EMUL_UNHANDLEABLE; | |
851 | } | |
852 | ||
853 | static int emulator_read_emulated(unsigned long addr, | |
854 | unsigned long *val, | |
855 | unsigned int bytes, | |
856 | struct x86_emulate_ctxt *ctxt) | |
857 | { | |
858 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
859 | ||
860 | if (vcpu->mmio_read_completed) { | |
861 | memcpy(val, vcpu->mmio_data, bytes); | |
862 | vcpu->mmio_read_completed = 0; | |
863 | return X86EMUL_CONTINUE; | |
864 | } else if (emulator_read_std(addr, val, bytes, ctxt) | |
865 | == X86EMUL_CONTINUE) | |
866 | return X86EMUL_CONTINUE; | |
867 | else { | |
868 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
869 | if (gpa == UNMAPPED_GVA) | |
870 | return vcpu_printf(vcpu, "not present\n"), X86EMUL_PROPAGATE_FAULT; | |
871 | vcpu->mmio_needed = 1; | |
872 | vcpu->mmio_phys_addr = gpa; | |
873 | vcpu->mmio_size = bytes; | |
874 | vcpu->mmio_is_write = 0; | |
875 | ||
876 | return X86EMUL_UNHANDLEABLE; | |
877 | } | |
878 | } | |
879 | ||
da4a00f0 AK |
880 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
881 | unsigned long val, int bytes) | |
882 | { | |
883 | struct kvm_memory_slot *m; | |
884 | struct page *page; | |
885 | void *virt; | |
886 | ||
887 | if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT)) | |
888 | return 0; | |
889 | m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT); | |
890 | if (!m) | |
891 | return 0; | |
892 | page = gfn_to_page(m, gpa >> PAGE_SHIFT); | |
893 | kvm_mmu_pre_write(vcpu, gpa, bytes); | |
894 | virt = kmap_atomic(page, KM_USER0); | |
895 | memcpy(virt + offset_in_page(gpa), &val, bytes); | |
896 | kunmap_atomic(virt, KM_USER0); | |
897 | kvm_mmu_post_write(vcpu, gpa, bytes); | |
898 | return 1; | |
899 | } | |
900 | ||
6aa8b732 AK |
901 | static int emulator_write_emulated(unsigned long addr, |
902 | unsigned long val, | |
903 | unsigned int bytes, | |
904 | struct x86_emulate_ctxt *ctxt) | |
905 | { | |
906 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
907 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
908 | ||
909 | if (gpa == UNMAPPED_GVA) | |
910 | return X86EMUL_PROPAGATE_FAULT; | |
911 | ||
da4a00f0 AK |
912 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
913 | return X86EMUL_CONTINUE; | |
914 | ||
6aa8b732 AK |
915 | vcpu->mmio_needed = 1; |
916 | vcpu->mmio_phys_addr = gpa; | |
917 | vcpu->mmio_size = bytes; | |
918 | vcpu->mmio_is_write = 1; | |
919 | memcpy(vcpu->mmio_data, &val, bytes); | |
920 | ||
921 | return X86EMUL_CONTINUE; | |
922 | } | |
923 | ||
924 | static int emulator_cmpxchg_emulated(unsigned long addr, | |
925 | unsigned long old, | |
926 | unsigned long new, | |
927 | unsigned int bytes, | |
928 | struct x86_emulate_ctxt *ctxt) | |
929 | { | |
930 | static int reported; | |
931 | ||
932 | if (!reported) { | |
933 | reported = 1; | |
934 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
935 | } | |
936 | return emulator_write_emulated(addr, new, bytes, ctxt); | |
937 | } | |
938 | ||
939 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
940 | { | |
941 | return kvm_arch_ops->get_segment_base(vcpu, seg); | |
942 | } | |
943 | ||
944 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
945 | { | |
946 | spin_lock(&vcpu->kvm->lock); | |
947 | vcpu->mmu.inval_page(vcpu, address); | |
948 | spin_unlock(&vcpu->kvm->lock); | |
949 | kvm_arch_ops->invlpg(vcpu, address); | |
950 | return X86EMUL_CONTINUE; | |
951 | } | |
952 | ||
953 | int emulate_clts(struct kvm_vcpu *vcpu) | |
954 | { | |
399badf3 | 955 | unsigned long cr0; |
6aa8b732 | 956 | |
399badf3 AK |
957 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
958 | cr0 = vcpu->cr0 & ~CR0_TS_MASK; | |
6aa8b732 AK |
959 | kvm_arch_ops->set_cr0(vcpu, cr0); |
960 | return X86EMUL_CONTINUE; | |
961 | } | |
962 | ||
963 | int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest) | |
964 | { | |
965 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
966 | ||
967 | switch (dr) { | |
968 | case 0 ... 3: | |
969 | *dest = kvm_arch_ops->get_dr(vcpu, dr); | |
970 | return X86EMUL_CONTINUE; | |
971 | default: | |
972 | printk(KERN_DEBUG "%s: unexpected dr %u\n", | |
973 | __FUNCTION__, dr); | |
974 | return X86EMUL_UNHANDLEABLE; | |
975 | } | |
976 | } | |
977 | ||
978 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
979 | { | |
980 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
981 | int exception; | |
982 | ||
983 | kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); | |
984 | if (exception) { | |
985 | /* FIXME: better handling */ | |
986 | return X86EMUL_UNHANDLEABLE; | |
987 | } | |
988 | return X86EMUL_CONTINUE; | |
989 | } | |
990 | ||
991 | static void report_emulation_failure(struct x86_emulate_ctxt *ctxt) | |
992 | { | |
993 | static int reported; | |
994 | u8 opcodes[4]; | |
995 | unsigned long rip = ctxt->vcpu->rip; | |
996 | unsigned long rip_linear; | |
997 | ||
998 | rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS); | |
999 | ||
1000 | if (reported) | |
1001 | return; | |
1002 | ||
1003 | emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt); | |
1004 | ||
1005 | printk(KERN_ERR "emulation failed but !mmio_needed?" | |
1006 | " rip %lx %02x %02x %02x %02x\n", | |
1007 | rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
1008 | reported = 1; | |
1009 | } | |
1010 | ||
1011 | struct x86_emulate_ops emulate_ops = { | |
1012 | .read_std = emulator_read_std, | |
1013 | .write_std = emulator_write_std, | |
1014 | .read_emulated = emulator_read_emulated, | |
1015 | .write_emulated = emulator_write_emulated, | |
1016 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
1017 | }; | |
1018 | ||
1019 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
1020 | struct kvm_run *run, | |
1021 | unsigned long cr2, | |
1022 | u16 error_code) | |
1023 | { | |
1024 | struct x86_emulate_ctxt emulate_ctxt; | |
1025 | int r; | |
1026 | int cs_db, cs_l; | |
1027 | ||
1028 | kvm_arch_ops->cache_regs(vcpu); | |
1029 | ||
1030 | kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
1031 | ||
1032 | emulate_ctxt.vcpu = vcpu; | |
1033 | emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu); | |
1034 | emulate_ctxt.cr2 = cr2; | |
1035 | emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM) | |
1036 | ? X86EMUL_MODE_REAL : cs_l | |
1037 | ? X86EMUL_MODE_PROT64 : cs_db | |
1038 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
1039 | ||
1040 | if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
1041 | emulate_ctxt.cs_base = 0; | |
1042 | emulate_ctxt.ds_base = 0; | |
1043 | emulate_ctxt.es_base = 0; | |
1044 | emulate_ctxt.ss_base = 0; | |
1045 | } else { | |
1046 | emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS); | |
1047 | emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS); | |
1048 | emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES); | |
1049 | emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS); | |
1050 | } | |
1051 | ||
1052 | emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS); | |
1053 | emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS); | |
1054 | ||
1055 | vcpu->mmio_is_write = 0; | |
1056 | r = x86_emulate_memop(&emulate_ctxt, &emulate_ops); | |
1057 | ||
1058 | if ((r || vcpu->mmio_is_write) && run) { | |
1059 | run->mmio.phys_addr = vcpu->mmio_phys_addr; | |
1060 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1061 | run->mmio.len = vcpu->mmio_size; | |
1062 | run->mmio.is_write = vcpu->mmio_is_write; | |
1063 | } | |
1064 | ||
1065 | if (r) { | |
1066 | if (!vcpu->mmio_needed) { | |
1067 | report_emulation_failure(&emulate_ctxt); | |
1068 | return EMULATE_FAIL; | |
1069 | } | |
1070 | return EMULATE_DO_MMIO; | |
1071 | } | |
1072 | ||
1073 | kvm_arch_ops->decache_regs(vcpu); | |
1074 | kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags); | |
1075 | ||
1076 | if (vcpu->mmio_is_write) | |
1077 | return EMULATE_DO_MMIO; | |
1078 | ||
1079 | return EMULATE_DONE; | |
1080 | } | |
1081 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1082 | ||
1083 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | |
1084 | { | |
1085 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
1086 | } | |
1087 | ||
1088 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1089 | { | |
1090 | struct descriptor_table dt = { limit, base }; | |
1091 | ||
1092 | kvm_arch_ops->set_gdt(vcpu, &dt); | |
1093 | } | |
1094 | ||
1095 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
1096 | { | |
1097 | struct descriptor_table dt = { limit, base }; | |
1098 | ||
1099 | kvm_arch_ops->set_idt(vcpu, &dt); | |
1100 | } | |
1101 | ||
1102 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
1103 | unsigned long *rflags) | |
1104 | { | |
1105 | lmsw(vcpu, msw); | |
1106 | *rflags = kvm_arch_ops->get_rflags(vcpu); | |
1107 | } | |
1108 | ||
1109 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
1110 | { | |
399badf3 | 1111 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1112 | switch (cr) { |
1113 | case 0: | |
1114 | return vcpu->cr0; | |
1115 | case 2: | |
1116 | return vcpu->cr2; | |
1117 | case 3: | |
1118 | return vcpu->cr3; | |
1119 | case 4: | |
1120 | return vcpu->cr4; | |
1121 | default: | |
1122 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1123 | return 0; | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
1128 | unsigned long *rflags) | |
1129 | { | |
1130 | switch (cr) { | |
1131 | case 0: | |
1132 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
1133 | *rflags = kvm_arch_ops->get_rflags(vcpu); | |
1134 | break; | |
1135 | case 2: | |
1136 | vcpu->cr2 = val; | |
1137 | break; | |
1138 | case 3: | |
1139 | set_cr3(vcpu, val); | |
1140 | break; | |
1141 | case 4: | |
1142 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
1143 | break; | |
1144 | default: | |
1145 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
1146 | } | |
1147 | } | |
1148 | ||
3bab1f5d AK |
1149 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
1150 | { | |
1151 | u64 data; | |
1152 | ||
1153 | switch (msr) { | |
1154 | case 0xc0010010: /* SYSCFG */ | |
1155 | case 0xc0010015: /* HWCR */ | |
1156 | case MSR_IA32_PLATFORM_ID: | |
1157 | case MSR_IA32_P5_MC_ADDR: | |
1158 | case MSR_IA32_P5_MC_TYPE: | |
1159 | case MSR_IA32_MC0_CTL: | |
1160 | case MSR_IA32_MCG_STATUS: | |
1161 | case MSR_IA32_MCG_CAP: | |
1162 | case MSR_IA32_MC0_MISC: | |
1163 | case MSR_IA32_MC0_MISC+4: | |
1164 | case MSR_IA32_MC0_MISC+8: | |
1165 | case MSR_IA32_MC0_MISC+12: | |
1166 | case MSR_IA32_MC0_MISC+16: | |
1167 | case MSR_IA32_UCODE_REV: | |
a8d13ea2 | 1168 | case MSR_IA32_PERF_STATUS: |
3bab1f5d AK |
1169 | /* MTRR registers */ |
1170 | case 0xfe: | |
1171 | case 0x200 ... 0x2ff: | |
1172 | data = 0; | |
1173 | break; | |
a8d13ea2 AK |
1174 | case 0xcd: /* fsb frequency */ |
1175 | data = 3; | |
1176 | break; | |
3bab1f5d AK |
1177 | case MSR_IA32_APICBASE: |
1178 | data = vcpu->apic_base; | |
1179 | break; | |
1180 | #ifdef CONFIG_X86_64 | |
1181 | case MSR_EFER: | |
1182 | data = vcpu->shadow_efer; | |
1183 | break; | |
1184 | #endif | |
1185 | default: | |
1186 | printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr); | |
1187 | return 1; | |
1188 | } | |
1189 | *pdata = data; | |
1190 | return 0; | |
1191 | } | |
1192 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
1193 | ||
6aa8b732 AK |
1194 | /* |
1195 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
1196 | * Returns 0 on success, non-0 otherwise. | |
1197 | * Assumes vcpu_load() was already called. | |
1198 | */ | |
1199 | static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
1200 | { | |
1201 | return kvm_arch_ops->get_msr(vcpu, msr_index, pdata); | |
1202 | } | |
1203 | ||
05b3e0c2 | 1204 | #ifdef CONFIG_X86_64 |
6aa8b732 | 1205 | |
3bab1f5d | 1206 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
6aa8b732 | 1207 | { |
6aa8b732 AK |
1208 | if (efer & EFER_RESERVED_BITS) { |
1209 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
1210 | efer); | |
1211 | inject_gp(vcpu); | |
1212 | return; | |
1213 | } | |
1214 | ||
1215 | if (is_paging(vcpu) | |
1216 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
1217 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
1218 | inject_gp(vcpu); | |
1219 | return; | |
1220 | } | |
1221 | ||
7725f0ba AK |
1222 | kvm_arch_ops->set_efer(vcpu, efer); |
1223 | ||
6aa8b732 AK |
1224 | efer &= ~EFER_LMA; |
1225 | efer |= vcpu->shadow_efer & EFER_LMA; | |
1226 | ||
1227 | vcpu->shadow_efer = efer; | |
6aa8b732 | 1228 | } |
6aa8b732 AK |
1229 | |
1230 | #endif | |
1231 | ||
3bab1f5d AK |
1232 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
1233 | { | |
1234 | switch (msr) { | |
1235 | #ifdef CONFIG_X86_64 | |
1236 | case MSR_EFER: | |
1237 | set_efer(vcpu, data); | |
1238 | break; | |
1239 | #endif | |
1240 | case MSR_IA32_MC0_STATUS: | |
1241 | printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", | |
1242 | __FUNCTION__, data); | |
1243 | break; | |
1244 | case MSR_IA32_UCODE_REV: | |
1245 | case MSR_IA32_UCODE_WRITE: | |
1246 | case 0x200 ... 0x2ff: /* MTRRs */ | |
1247 | break; | |
1248 | case MSR_IA32_APICBASE: | |
1249 | vcpu->apic_base = data; | |
1250 | break; | |
1251 | default: | |
1252 | printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr); | |
1253 | return 1; | |
1254 | } | |
1255 | return 0; | |
1256 | } | |
1257 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
1258 | ||
6aa8b732 AK |
1259 | /* |
1260 | * Writes msr value into into the appropriate "register". | |
1261 | * Returns 0 on success, non-0 otherwise. | |
1262 | * Assumes vcpu_load() was already called. | |
1263 | */ | |
1264 | static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
1265 | { | |
1266 | return kvm_arch_ops->set_msr(vcpu, msr_index, data); | |
1267 | } | |
1268 | ||
1269 | void kvm_resched(struct kvm_vcpu *vcpu) | |
1270 | { | |
1271 | vcpu_put(vcpu); | |
1272 | cond_resched(); | |
1273 | /* Cannot fail - no vcpu unplug yet. */ | |
1274 | vcpu_load(vcpu->kvm, vcpu_slot(vcpu)); | |
1275 | } | |
1276 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1277 | ||
1278 | void load_msrs(struct vmx_msr_entry *e, int n) | |
1279 | { | |
1280 | int i; | |
1281 | ||
1282 | for (i = 0; i < n; ++i) | |
1283 | wrmsrl(e[i].index, e[i].data); | |
1284 | } | |
1285 | EXPORT_SYMBOL_GPL(load_msrs); | |
1286 | ||
1287 | void save_msrs(struct vmx_msr_entry *e, int n) | |
1288 | { | |
1289 | int i; | |
1290 | ||
1291 | for (i = 0; i < n; ++i) | |
1292 | rdmsrl(e[i].index, e[i].data); | |
1293 | } | |
1294 | EXPORT_SYMBOL_GPL(save_msrs); | |
1295 | ||
1296 | static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run) | |
1297 | { | |
1298 | struct kvm_vcpu *vcpu; | |
1299 | int r; | |
1300 | ||
5aacf0ca | 1301 | if (!valid_vcpu(kvm_run->vcpu)) |
6aa8b732 AK |
1302 | return -EINVAL; |
1303 | ||
1304 | vcpu = vcpu_load(kvm, kvm_run->vcpu); | |
1305 | if (!vcpu) | |
1306 | return -ENOENT; | |
1307 | ||
1308 | if (kvm_run->emulated) { | |
1309 | kvm_arch_ops->skip_emulated_instruction(vcpu); | |
1310 | kvm_run->emulated = 0; | |
1311 | } | |
1312 | ||
1313 | if (kvm_run->mmio_completed) { | |
1314 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
1315 | vcpu->mmio_read_completed = 1; | |
1316 | } | |
1317 | ||
1318 | vcpu->mmio_needed = 0; | |
1319 | ||
1320 | r = kvm_arch_ops->run(vcpu, kvm_run); | |
1321 | ||
1322 | vcpu_put(vcpu); | |
1323 | return r; | |
1324 | } | |
1325 | ||
1326 | static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs) | |
1327 | { | |
1328 | struct kvm_vcpu *vcpu; | |
1329 | ||
5aacf0ca | 1330 | if (!valid_vcpu(regs->vcpu)) |
6aa8b732 AK |
1331 | return -EINVAL; |
1332 | ||
1333 | vcpu = vcpu_load(kvm, regs->vcpu); | |
1334 | if (!vcpu) | |
1335 | return -ENOENT; | |
1336 | ||
1337 | kvm_arch_ops->cache_regs(vcpu); | |
1338 | ||
1339 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
1340 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
1341 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
1342 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
1343 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
1344 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
1345 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
1346 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
05b3e0c2 | 1347 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1348 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; |
1349 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
1350 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
1351 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
1352 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
1353 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
1354 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
1355 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
1356 | #endif | |
1357 | ||
1358 | regs->rip = vcpu->rip; | |
1359 | regs->rflags = kvm_arch_ops->get_rflags(vcpu); | |
1360 | ||
1361 | /* | |
1362 | * Don't leak debug flags in case they were set for guest debugging | |
1363 | */ | |
1364 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
1365 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
1366 | ||
1367 | vcpu_put(vcpu); | |
1368 | ||
1369 | return 0; | |
1370 | } | |
1371 | ||
1372 | static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs) | |
1373 | { | |
1374 | struct kvm_vcpu *vcpu; | |
1375 | ||
5aacf0ca | 1376 | if (!valid_vcpu(regs->vcpu)) |
6aa8b732 AK |
1377 | return -EINVAL; |
1378 | ||
1379 | vcpu = vcpu_load(kvm, regs->vcpu); | |
1380 | if (!vcpu) | |
1381 | return -ENOENT; | |
1382 | ||
1383 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
1384 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
1385 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
1386 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
1387 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
1388 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
1389 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
1390 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
05b3e0c2 | 1391 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1392 | vcpu->regs[VCPU_REGS_R8] = regs->r8; |
1393 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
1394 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
1395 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
1396 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
1397 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
1398 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
1399 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
1400 | #endif | |
1401 | ||
1402 | vcpu->rip = regs->rip; | |
1403 | kvm_arch_ops->set_rflags(vcpu, regs->rflags); | |
1404 | ||
1405 | kvm_arch_ops->decache_regs(vcpu); | |
1406 | ||
1407 | vcpu_put(vcpu); | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | static void get_segment(struct kvm_vcpu *vcpu, | |
1413 | struct kvm_segment *var, int seg) | |
1414 | { | |
1415 | return kvm_arch_ops->get_segment(vcpu, var, seg); | |
1416 | } | |
1417 | ||
1418 | static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | |
1419 | { | |
1420 | struct kvm_vcpu *vcpu; | |
1421 | struct descriptor_table dt; | |
1422 | ||
5aacf0ca | 1423 | if (!valid_vcpu(sregs->vcpu)) |
6aa8b732 AK |
1424 | return -EINVAL; |
1425 | vcpu = vcpu_load(kvm, sregs->vcpu); | |
1426 | if (!vcpu) | |
1427 | return -ENOENT; | |
1428 | ||
1429 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
1430 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1431 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1432 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1433 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1434 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1435 | ||
1436 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1437 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1438 | ||
1439 | kvm_arch_ops->get_idt(vcpu, &dt); | |
1440 | sregs->idt.limit = dt.limit; | |
1441 | sregs->idt.base = dt.base; | |
1442 | kvm_arch_ops->get_gdt(vcpu, &dt); | |
1443 | sregs->gdt.limit = dt.limit; | |
1444 | sregs->gdt.base = dt.base; | |
1445 | ||
399badf3 | 1446 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
6aa8b732 AK |
1447 | sregs->cr0 = vcpu->cr0; |
1448 | sregs->cr2 = vcpu->cr2; | |
1449 | sregs->cr3 = vcpu->cr3; | |
1450 | sregs->cr4 = vcpu->cr4; | |
1451 | sregs->cr8 = vcpu->cr8; | |
1452 | sregs->efer = vcpu->shadow_efer; | |
1453 | sregs->apic_base = vcpu->apic_base; | |
1454 | ||
1455 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, | |
1456 | sizeof sregs->interrupt_bitmap); | |
1457 | ||
1458 | vcpu_put(vcpu); | |
1459 | ||
1460 | return 0; | |
1461 | } | |
1462 | ||
1463 | static void set_segment(struct kvm_vcpu *vcpu, | |
1464 | struct kvm_segment *var, int seg) | |
1465 | { | |
1466 | return kvm_arch_ops->set_segment(vcpu, var, seg); | |
1467 | } | |
1468 | ||
1469 | static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs) | |
1470 | { | |
1471 | struct kvm_vcpu *vcpu; | |
1472 | int mmu_reset_needed = 0; | |
1473 | int i; | |
1474 | struct descriptor_table dt; | |
1475 | ||
5aacf0ca | 1476 | if (!valid_vcpu(sregs->vcpu)) |
6aa8b732 AK |
1477 | return -EINVAL; |
1478 | vcpu = vcpu_load(kvm, sregs->vcpu); | |
1479 | if (!vcpu) | |
1480 | return -ENOENT; | |
1481 | ||
1482 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
1483 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
1484 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
1485 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
1486 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
1487 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
1488 | ||
1489 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
1490 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
1491 | ||
1492 | dt.limit = sregs->idt.limit; | |
1493 | dt.base = sregs->idt.base; | |
1494 | kvm_arch_ops->set_idt(vcpu, &dt); | |
1495 | dt.limit = sregs->gdt.limit; | |
1496 | dt.base = sregs->gdt.base; | |
1497 | kvm_arch_ops->set_gdt(vcpu, &dt); | |
1498 | ||
1499 | vcpu->cr2 = sregs->cr2; | |
1500 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
1501 | vcpu->cr3 = sregs->cr3; | |
1502 | ||
1503 | vcpu->cr8 = sregs->cr8; | |
1504 | ||
1505 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
05b3e0c2 | 1506 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1507 | kvm_arch_ops->set_efer(vcpu, sregs->efer); |
1508 | #endif | |
1509 | vcpu->apic_base = sregs->apic_base; | |
1510 | ||
399badf3 AK |
1511 | kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu); |
1512 | ||
6aa8b732 AK |
1513 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; |
1514 | kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0); | |
1515 | ||
1516 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
1517 | kvm_arch_ops->set_cr4(vcpu, sregs->cr4); | |
1b0973bd AK |
1518 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
1519 | load_pdptrs(vcpu, vcpu->cr3); | |
6aa8b732 AK |
1520 | |
1521 | if (mmu_reset_needed) | |
1522 | kvm_mmu_reset_context(vcpu); | |
1523 | ||
1524 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
1525 | sizeof vcpu->irq_pending); | |
1526 | vcpu->irq_summary = 0; | |
1527 | for (i = 0; i < NR_IRQ_WORDS; ++i) | |
1528 | if (vcpu->irq_pending[i]) | |
1529 | __set_bit(i, &vcpu->irq_summary); | |
1530 | ||
1531 | vcpu_put(vcpu); | |
1532 | ||
1533 | return 0; | |
1534 | } | |
1535 | ||
1536 | /* | |
1537 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1538 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
bf591b24 MR |
1539 | * |
1540 | * This list is modified at module load time to reflect the | |
1541 | * capabilities of the host cpu. | |
6aa8b732 AK |
1542 | */ |
1543 | static u32 msrs_to_save[] = { | |
1544 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
1545 | MSR_K6_STAR, | |
05b3e0c2 | 1546 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1547 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
1548 | #endif | |
1549 | MSR_IA32_TIME_STAMP_COUNTER, | |
1550 | }; | |
1551 | ||
bf591b24 MR |
1552 | static unsigned num_msrs_to_save; |
1553 | ||
1554 | static __init void kvm_init_msr_list(void) | |
1555 | { | |
1556 | u32 dummy[2]; | |
1557 | unsigned i, j; | |
1558 | ||
1559 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
1560 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
1561 | continue; | |
1562 | if (j < i) | |
1563 | msrs_to_save[j] = msrs_to_save[i]; | |
1564 | j++; | |
1565 | } | |
1566 | num_msrs_to_save = j; | |
1567 | } | |
6aa8b732 AK |
1568 | |
1569 | /* | |
1570 | * Adapt set_msr() to msr_io()'s calling convention | |
1571 | */ | |
1572 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1573 | { | |
1574 | return set_msr(vcpu, index, *data); | |
1575 | } | |
1576 | ||
1577 | /* | |
1578 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
1579 | * | |
1580 | * @return number of msrs set successfully. | |
1581 | */ | |
1582 | static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs, | |
1583 | struct kvm_msr_entry *entries, | |
1584 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1585 | unsigned index, u64 *data)) | |
1586 | { | |
1587 | struct kvm_vcpu *vcpu; | |
1588 | int i; | |
1589 | ||
5aacf0ca | 1590 | if (!valid_vcpu(msrs->vcpu)) |
6aa8b732 AK |
1591 | return -EINVAL; |
1592 | ||
1593 | vcpu = vcpu_load(kvm, msrs->vcpu); | |
1594 | if (!vcpu) | |
1595 | return -ENOENT; | |
1596 | ||
1597 | for (i = 0; i < msrs->nmsrs; ++i) | |
1598 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
1599 | break; | |
1600 | ||
1601 | vcpu_put(vcpu); | |
1602 | ||
1603 | return i; | |
1604 | } | |
1605 | ||
1606 | /* | |
1607 | * Read or write a bunch of msrs. Parameters are user addresses. | |
1608 | * | |
1609 | * @return number of msrs set successfully. | |
1610 | */ | |
1611 | static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs, | |
1612 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1613 | unsigned index, u64 *data), | |
1614 | int writeback) | |
1615 | { | |
1616 | struct kvm_msrs msrs; | |
1617 | struct kvm_msr_entry *entries; | |
1618 | int r, n; | |
1619 | unsigned size; | |
1620 | ||
1621 | r = -EFAULT; | |
1622 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
1623 | goto out; | |
1624 | ||
1625 | r = -E2BIG; | |
1626 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
1627 | goto out; | |
1628 | ||
1629 | r = -ENOMEM; | |
1630 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
1631 | entries = vmalloc(size); | |
1632 | if (!entries) | |
1633 | goto out; | |
1634 | ||
1635 | r = -EFAULT; | |
1636 | if (copy_from_user(entries, user_msrs->entries, size)) | |
1637 | goto out_free; | |
1638 | ||
1639 | r = n = __msr_io(kvm, &msrs, entries, do_msr); | |
1640 | if (r < 0) | |
1641 | goto out_free; | |
1642 | ||
1643 | r = -EFAULT; | |
1644 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
1645 | goto out_free; | |
1646 | ||
1647 | r = n; | |
1648 | ||
1649 | out_free: | |
1650 | vfree(entries); | |
1651 | out: | |
1652 | return r; | |
1653 | } | |
1654 | ||
1655 | /* | |
1656 | * Translate a guest virtual address to a guest physical address. | |
1657 | */ | |
1658 | static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr) | |
1659 | { | |
1660 | unsigned long vaddr = tr->linear_address; | |
1661 | struct kvm_vcpu *vcpu; | |
1662 | gpa_t gpa; | |
1663 | ||
1664 | vcpu = vcpu_load(kvm, tr->vcpu); | |
1665 | if (!vcpu) | |
1666 | return -ENOENT; | |
1667 | spin_lock(&kvm->lock); | |
1668 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); | |
1669 | tr->physical_address = gpa; | |
1670 | tr->valid = gpa != UNMAPPED_GVA; | |
1671 | tr->writeable = 1; | |
1672 | tr->usermode = 0; | |
1673 | spin_unlock(&kvm->lock); | |
1674 | vcpu_put(vcpu); | |
1675 | ||
1676 | return 0; | |
1677 | } | |
1678 | ||
1679 | static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq) | |
1680 | { | |
1681 | struct kvm_vcpu *vcpu; | |
1682 | ||
5aacf0ca | 1683 | if (!valid_vcpu(irq->vcpu)) |
6aa8b732 AK |
1684 | return -EINVAL; |
1685 | if (irq->irq < 0 || irq->irq >= 256) | |
1686 | return -EINVAL; | |
1687 | vcpu = vcpu_load(kvm, irq->vcpu); | |
1688 | if (!vcpu) | |
1689 | return -ENOENT; | |
1690 | ||
1691 | set_bit(irq->irq, vcpu->irq_pending); | |
1692 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
1693 | ||
1694 | vcpu_put(vcpu); | |
1695 | ||
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | static int kvm_dev_ioctl_debug_guest(struct kvm *kvm, | |
1700 | struct kvm_debug_guest *dbg) | |
1701 | { | |
1702 | struct kvm_vcpu *vcpu; | |
1703 | int r; | |
1704 | ||
5aacf0ca | 1705 | if (!valid_vcpu(dbg->vcpu)) |
6aa8b732 AK |
1706 | return -EINVAL; |
1707 | vcpu = vcpu_load(kvm, dbg->vcpu); | |
1708 | if (!vcpu) | |
1709 | return -ENOENT; | |
1710 | ||
1711 | r = kvm_arch_ops->set_guest_debug(vcpu, dbg); | |
1712 | ||
1713 | vcpu_put(vcpu); | |
1714 | ||
1715 | return r; | |
1716 | } | |
1717 | ||
1718 | static long kvm_dev_ioctl(struct file *filp, | |
1719 | unsigned int ioctl, unsigned long arg) | |
1720 | { | |
1721 | struct kvm *kvm = filp->private_data; | |
1722 | int r = -EINVAL; | |
1723 | ||
1724 | switch (ioctl) { | |
0b76e20b AK |
1725 | case KVM_GET_API_VERSION: |
1726 | r = KVM_API_VERSION; | |
1727 | break; | |
6aa8b732 AK |
1728 | case KVM_CREATE_VCPU: { |
1729 | r = kvm_dev_ioctl_create_vcpu(kvm, arg); | |
1730 | if (r) | |
1731 | goto out; | |
1732 | break; | |
1733 | } | |
1734 | case KVM_RUN: { | |
1735 | struct kvm_run kvm_run; | |
1736 | ||
1737 | r = -EFAULT; | |
1738 | if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run)) | |
1739 | goto out; | |
1740 | r = kvm_dev_ioctl_run(kvm, &kvm_run); | |
c1150d8c | 1741 | if (r < 0 && r != -EINTR) |
6aa8b732 | 1742 | goto out; |
c1150d8c DL |
1743 | if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) { |
1744 | r = -EFAULT; | |
6aa8b732 | 1745 | goto out; |
c1150d8c | 1746 | } |
6aa8b732 AK |
1747 | break; |
1748 | } | |
1749 | case KVM_GET_REGS: { | |
1750 | struct kvm_regs kvm_regs; | |
1751 | ||
1752 | r = -EFAULT; | |
1753 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) | |
1754 | goto out; | |
1755 | r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs); | |
1756 | if (r) | |
1757 | goto out; | |
1758 | r = -EFAULT; | |
1759 | if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs)) | |
1760 | goto out; | |
1761 | r = 0; | |
1762 | break; | |
1763 | } | |
1764 | case KVM_SET_REGS: { | |
1765 | struct kvm_regs kvm_regs; | |
1766 | ||
1767 | r = -EFAULT; | |
1768 | if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs)) | |
1769 | goto out; | |
1770 | r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs); | |
1771 | if (r) | |
1772 | goto out; | |
1773 | r = 0; | |
1774 | break; | |
1775 | } | |
1776 | case KVM_GET_SREGS: { | |
1777 | struct kvm_sregs kvm_sregs; | |
1778 | ||
1779 | r = -EFAULT; | |
1780 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) | |
1781 | goto out; | |
1782 | r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs); | |
1783 | if (r) | |
1784 | goto out; | |
1785 | r = -EFAULT; | |
1786 | if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs)) | |
1787 | goto out; | |
1788 | r = 0; | |
1789 | break; | |
1790 | } | |
1791 | case KVM_SET_SREGS: { | |
1792 | struct kvm_sregs kvm_sregs; | |
1793 | ||
1794 | r = -EFAULT; | |
1795 | if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs)) | |
1796 | goto out; | |
1797 | r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs); | |
1798 | if (r) | |
1799 | goto out; | |
1800 | r = 0; | |
1801 | break; | |
1802 | } | |
1803 | case KVM_TRANSLATE: { | |
1804 | struct kvm_translation tr; | |
1805 | ||
1806 | r = -EFAULT; | |
1807 | if (copy_from_user(&tr, (void *)arg, sizeof tr)) | |
1808 | goto out; | |
1809 | r = kvm_dev_ioctl_translate(kvm, &tr); | |
1810 | if (r) | |
1811 | goto out; | |
1812 | r = -EFAULT; | |
1813 | if (copy_to_user((void *)arg, &tr, sizeof tr)) | |
1814 | goto out; | |
1815 | r = 0; | |
1816 | break; | |
1817 | } | |
1818 | case KVM_INTERRUPT: { | |
1819 | struct kvm_interrupt irq; | |
1820 | ||
1821 | r = -EFAULT; | |
1822 | if (copy_from_user(&irq, (void *)arg, sizeof irq)) | |
1823 | goto out; | |
1824 | r = kvm_dev_ioctl_interrupt(kvm, &irq); | |
1825 | if (r) | |
1826 | goto out; | |
1827 | r = 0; | |
1828 | break; | |
1829 | } | |
1830 | case KVM_DEBUG_GUEST: { | |
1831 | struct kvm_debug_guest dbg; | |
1832 | ||
1833 | r = -EFAULT; | |
1834 | if (copy_from_user(&dbg, (void *)arg, sizeof dbg)) | |
1835 | goto out; | |
1836 | r = kvm_dev_ioctl_debug_guest(kvm, &dbg); | |
1837 | if (r) | |
1838 | goto out; | |
1839 | r = 0; | |
1840 | break; | |
1841 | } | |
1842 | case KVM_SET_MEMORY_REGION: { | |
1843 | struct kvm_memory_region kvm_mem; | |
1844 | ||
1845 | r = -EFAULT; | |
1846 | if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem)) | |
1847 | goto out; | |
1848 | r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem); | |
1849 | if (r) | |
1850 | goto out; | |
1851 | break; | |
1852 | } | |
1853 | case KVM_GET_DIRTY_LOG: { | |
1854 | struct kvm_dirty_log log; | |
1855 | ||
1856 | r = -EFAULT; | |
1857 | if (copy_from_user(&log, (void *)arg, sizeof log)) | |
1858 | goto out; | |
1859 | r = kvm_dev_ioctl_get_dirty_log(kvm, &log); | |
1860 | if (r) | |
1861 | goto out; | |
1862 | break; | |
1863 | } | |
1864 | case KVM_GET_MSRS: | |
1865 | r = msr_io(kvm, (void __user *)arg, get_msr, 1); | |
1866 | break; | |
1867 | case KVM_SET_MSRS: | |
1868 | r = msr_io(kvm, (void __user *)arg, do_set_msr, 0); | |
1869 | break; | |
1870 | case KVM_GET_MSR_INDEX_LIST: { | |
1871 | struct kvm_msr_list __user *user_msr_list = (void __user *)arg; | |
1872 | struct kvm_msr_list msr_list; | |
1873 | unsigned n; | |
1874 | ||
1875 | r = -EFAULT; | |
1876 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
1877 | goto out; | |
1878 | n = msr_list.nmsrs; | |
bf591b24 | 1879 | msr_list.nmsrs = num_msrs_to_save; |
6aa8b732 AK |
1880 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
1881 | goto out; | |
1882 | r = -E2BIG; | |
bf591b24 | 1883 | if (n < num_msrs_to_save) |
6aa8b732 AK |
1884 | goto out; |
1885 | r = -EFAULT; | |
1886 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
bf591b24 | 1887 | num_msrs_to_save * sizeof(u32))) |
6aa8b732 AK |
1888 | goto out; |
1889 | r = 0; | |
1890 | } | |
1891 | default: | |
1892 | ; | |
1893 | } | |
1894 | out: | |
1895 | return r; | |
1896 | } | |
1897 | ||
1898 | static struct page *kvm_dev_nopage(struct vm_area_struct *vma, | |
1899 | unsigned long address, | |
1900 | int *type) | |
1901 | { | |
1902 | struct kvm *kvm = vma->vm_file->private_data; | |
1903 | unsigned long pgoff; | |
1904 | struct kvm_memory_slot *slot; | |
1905 | struct page *page; | |
1906 | ||
1907 | *type = VM_FAULT_MINOR; | |
1908 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | |
1909 | slot = gfn_to_memslot(kvm, pgoff); | |
1910 | if (!slot) | |
1911 | return NOPAGE_SIGBUS; | |
1912 | page = gfn_to_page(slot, pgoff); | |
1913 | if (!page) | |
1914 | return NOPAGE_SIGBUS; | |
1915 | get_page(page); | |
1916 | return page; | |
1917 | } | |
1918 | ||
1919 | static struct vm_operations_struct kvm_dev_vm_ops = { | |
1920 | .nopage = kvm_dev_nopage, | |
1921 | }; | |
1922 | ||
1923 | static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma) | |
1924 | { | |
1925 | vma->vm_ops = &kvm_dev_vm_ops; | |
1926 | return 0; | |
1927 | } | |
1928 | ||
1929 | static struct file_operations kvm_chardev_ops = { | |
1930 | .open = kvm_dev_open, | |
1931 | .release = kvm_dev_release, | |
1932 | .unlocked_ioctl = kvm_dev_ioctl, | |
1933 | .compat_ioctl = kvm_dev_ioctl, | |
1934 | .mmap = kvm_dev_mmap, | |
1935 | }; | |
1936 | ||
1937 | static struct miscdevice kvm_dev = { | |
1938 | MISC_DYNAMIC_MINOR, | |
1939 | "kvm", | |
1940 | &kvm_chardev_ops, | |
1941 | }; | |
1942 | ||
1943 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, | |
1944 | void *v) | |
1945 | { | |
1946 | if (val == SYS_RESTART) { | |
1947 | /* | |
1948 | * Some (well, at least mine) BIOSes hang on reboot if | |
1949 | * in vmx root mode. | |
1950 | */ | |
1951 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
1952 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
1953 | } | |
1954 | return NOTIFY_OK; | |
1955 | } | |
1956 | ||
1957 | static struct notifier_block kvm_reboot_notifier = { | |
1958 | .notifier_call = kvm_reboot, | |
1959 | .priority = 0, | |
1960 | }; | |
1961 | ||
1962 | static __init void kvm_init_debug(void) | |
1963 | { | |
1964 | struct kvm_stats_debugfs_item *p; | |
1965 | ||
1966 | debugfs_dir = debugfs_create_dir("kvm", 0); | |
1967 | for (p = debugfs_entries; p->name; ++p) | |
1968 | p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir, | |
1969 | p->data); | |
1970 | } | |
1971 | ||
1972 | static void kvm_exit_debug(void) | |
1973 | { | |
1974 | struct kvm_stats_debugfs_item *p; | |
1975 | ||
1976 | for (p = debugfs_entries; p->name; ++p) | |
1977 | debugfs_remove(p->dentry); | |
1978 | debugfs_remove(debugfs_dir); | |
1979 | } | |
1980 | ||
1981 | hpa_t bad_page_address; | |
1982 | ||
1983 | int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module) | |
1984 | { | |
1985 | int r; | |
1986 | ||
09db28b8 YI |
1987 | if (kvm_arch_ops) { |
1988 | printk(KERN_ERR "kvm: already loaded the other module\n"); | |
1989 | return -EEXIST; | |
1990 | } | |
1991 | ||
e097f35c | 1992 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
1993 | printk(KERN_ERR "kvm: no hardware support\n"); |
1994 | return -EOPNOTSUPP; | |
1995 | } | |
e097f35c | 1996 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
1997 | printk(KERN_ERR "kvm: disabled by bios\n"); |
1998 | return -EOPNOTSUPP; | |
1999 | } | |
2000 | ||
e097f35c YI |
2001 | kvm_arch_ops = ops; |
2002 | ||
6aa8b732 AK |
2003 | r = kvm_arch_ops->hardware_setup(); |
2004 | if (r < 0) | |
2005 | return r; | |
2006 | ||
2007 | on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1); | |
2008 | register_reboot_notifier(&kvm_reboot_notifier); | |
2009 | ||
2010 | kvm_chardev_ops.owner = module; | |
2011 | ||
2012 | r = misc_register(&kvm_dev); | |
2013 | if (r) { | |
2014 | printk (KERN_ERR "kvm: misc device register failed\n"); | |
2015 | goto out_free; | |
2016 | } | |
2017 | ||
2018 | return r; | |
2019 | ||
2020 | out_free: | |
2021 | unregister_reboot_notifier(&kvm_reboot_notifier); | |
2022 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
2023 | kvm_arch_ops->hardware_unsetup(); | |
2024 | return r; | |
2025 | } | |
2026 | ||
2027 | void kvm_exit_arch(void) | |
2028 | { | |
2029 | misc_deregister(&kvm_dev); | |
2030 | ||
2031 | unregister_reboot_notifier(&kvm_reboot_notifier); | |
2032 | on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1); | |
2033 | kvm_arch_ops->hardware_unsetup(); | |
09db28b8 | 2034 | kvm_arch_ops = NULL; |
6aa8b732 AK |
2035 | } |
2036 | ||
2037 | static __init int kvm_init(void) | |
2038 | { | |
2039 | static struct page *bad_page; | |
2040 | int r = 0; | |
2041 | ||
2042 | kvm_init_debug(); | |
2043 | ||
bf591b24 MR |
2044 | kvm_init_msr_list(); |
2045 | ||
6aa8b732 AK |
2046 | if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) { |
2047 | r = -ENOMEM; | |
2048 | goto out; | |
2049 | } | |
2050 | ||
2051 | bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT; | |
2052 | memset(__va(bad_page_address), 0, PAGE_SIZE); | |
2053 | ||
2054 | return r; | |
2055 | ||
2056 | out: | |
2057 | kvm_exit_debug(); | |
2058 | return r; | |
2059 | } | |
2060 | ||
2061 | static __exit void kvm_exit(void) | |
2062 | { | |
2063 | kvm_exit_debug(); | |
2064 | __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT)); | |
2065 | } | |
2066 | ||
2067 | module_init(kvm_init) | |
2068 | module_exit(kvm_exit) | |
2069 | ||
2070 | EXPORT_SYMBOL_GPL(kvm_init_arch); | |
2071 | EXPORT_SYMBOL_GPL(kvm_exit_arch); |