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