]>
Commit | Line | Data |
---|---|---|
043405e1 CO |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * derived from drivers/kvm/kvm_main.c | |
5 | * | |
6 | * Copyright (C) 2006 Qumranet, Inc. | |
7 | * | |
8 | * Authors: | |
9 | * Avi Kivity <avi@qumranet.com> | |
10 | * Yaniv Kamay <yaniv@qumranet.com> | |
11 | * | |
12 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
13 | * the COPYING file in the top-level directory. | |
14 | * | |
15 | */ | |
16 | ||
313a3dc7 | 17 | #include "kvm.h" |
043405e1 | 18 | #include "x86.h" |
d825ed0a | 19 | #include "x86_emulate.h" |
5fb76f9b | 20 | #include "segment_descriptor.h" |
313a3dc7 CO |
21 | #include "irq.h" |
22 | ||
23 | #include <linux/kvm.h> | |
24 | #include <linux/fs.h> | |
25 | #include <linux/vmalloc.h> | |
5fb76f9b | 26 | #include <linux/module.h> |
0de10343 | 27 | #include <linux/mman.h> |
043405e1 CO |
28 | |
29 | #include <asm/uaccess.h> | |
d825ed0a | 30 | #include <asm/msr.h> |
043405e1 | 31 | |
313a3dc7 | 32 | #define MAX_IO_MSRS 256 |
a03490ed CO |
33 | #define CR0_RESERVED_BITS \ |
34 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ | |
35 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ | |
36 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) | |
37 | #define CR4_RESERVED_BITS \ | |
38 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ | |
39 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ | |
40 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
41 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) | |
42 | ||
43 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) | |
15c4a640 | 44 | #define EFER_RESERVED_BITS 0xfffffffffffff2fe |
313a3dc7 | 45 | |
ba1389b7 AK |
46 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
47 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU | |
417bc304 | 48 | |
97896d04 ZX |
49 | struct kvm_x86_ops *kvm_x86_ops; |
50 | ||
417bc304 | 51 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
ba1389b7 AK |
52 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
53 | { "pf_guest", VCPU_STAT(pf_guest) }, | |
54 | { "tlb_flush", VCPU_STAT(tlb_flush) }, | |
55 | { "invlpg", VCPU_STAT(invlpg) }, | |
56 | { "exits", VCPU_STAT(exits) }, | |
57 | { "io_exits", VCPU_STAT(io_exits) }, | |
58 | { "mmio_exits", VCPU_STAT(mmio_exits) }, | |
59 | { "signal_exits", VCPU_STAT(signal_exits) }, | |
60 | { "irq_window", VCPU_STAT(irq_window_exits) }, | |
61 | { "halt_exits", VCPU_STAT(halt_exits) }, | |
62 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, | |
63 | { "request_irq", VCPU_STAT(request_irq_exits) }, | |
64 | { "irq_exits", VCPU_STAT(irq_exits) }, | |
65 | { "host_state_reload", VCPU_STAT(host_state_reload) }, | |
66 | { "efer_reload", VCPU_STAT(efer_reload) }, | |
67 | { "fpu_reload", VCPU_STAT(fpu_reload) }, | |
68 | { "insn_emulation", VCPU_STAT(insn_emulation) }, | |
69 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, | |
4cee5764 AK |
70 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
71 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, | |
72 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, | |
73 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, | |
74 | { "mmu_flooded", VM_STAT(mmu_flooded) }, | |
75 | { "mmu_recycled", VM_STAT(mmu_recycled) }, | |
0f74a24c | 76 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
417bc304 HB |
77 | { NULL } |
78 | }; | |
79 | ||
80 | ||
5fb76f9b CO |
81 | unsigned long segment_base(u16 selector) |
82 | { | |
83 | struct descriptor_table gdt; | |
84 | struct segment_descriptor *d; | |
85 | unsigned long table_base; | |
86 | unsigned long v; | |
87 | ||
88 | if (selector == 0) | |
89 | return 0; | |
90 | ||
91 | asm("sgdt %0" : "=m"(gdt)); | |
92 | table_base = gdt.base; | |
93 | ||
94 | if (selector & 4) { /* from ldt */ | |
95 | u16 ldt_selector; | |
96 | ||
97 | asm("sldt %0" : "=g"(ldt_selector)); | |
98 | table_base = segment_base(ldt_selector); | |
99 | } | |
100 | d = (struct segment_descriptor *)(table_base + (selector & ~7)); | |
101 | v = d->base_low | ((unsigned long)d->base_mid << 16) | | |
102 | ((unsigned long)d->base_high << 24); | |
103 | #ifdef CONFIG_X86_64 | |
104 | if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) | |
105 | v |= ((unsigned long) \ | |
106 | ((struct segment_descriptor_64 *)d)->base_higher) << 32; | |
107 | #endif | |
108 | return v; | |
109 | } | |
110 | EXPORT_SYMBOL_GPL(segment_base); | |
111 | ||
6866b83e CO |
112 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
113 | { | |
114 | if (irqchip_in_kernel(vcpu->kvm)) | |
115 | return vcpu->apic_base; | |
116 | else | |
117 | return vcpu->apic_base; | |
118 | } | |
119 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
120 | ||
121 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) | |
122 | { | |
123 | /* TODO: reserve bits check */ | |
124 | if (irqchip_in_kernel(vcpu->kvm)) | |
125 | kvm_lapic_set_base(vcpu, data); | |
126 | else | |
127 | vcpu->apic_base = data; | |
128 | } | |
129 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
130 | ||
298101da AK |
131 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
132 | { | |
133 | WARN_ON(vcpu->exception.pending); | |
134 | vcpu->exception.pending = true; | |
135 | vcpu->exception.has_error_code = false; | |
136 | vcpu->exception.nr = nr; | |
137 | } | |
138 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
139 | ||
c3c91fee AK |
140 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
141 | u32 error_code) | |
142 | { | |
143 | ++vcpu->stat.pf_guest; | |
144 | if (vcpu->exception.pending && vcpu->exception.nr == PF_VECTOR) { | |
145 | printk(KERN_DEBUG "kvm: inject_page_fault:" | |
146 | " double fault 0x%lx\n", addr); | |
147 | vcpu->exception.nr = DF_VECTOR; | |
148 | vcpu->exception.error_code = 0; | |
149 | return; | |
150 | } | |
151 | vcpu->cr2 = addr; | |
152 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); | |
153 | } | |
154 | ||
298101da AK |
155 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
156 | { | |
157 | WARN_ON(vcpu->exception.pending); | |
158 | vcpu->exception.pending = true; | |
159 | vcpu->exception.has_error_code = true; | |
160 | vcpu->exception.nr = nr; | |
161 | vcpu->exception.error_code = error_code; | |
162 | } | |
163 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
164 | ||
165 | static void __queue_exception(struct kvm_vcpu *vcpu) | |
166 | { | |
167 | kvm_x86_ops->queue_exception(vcpu, vcpu->exception.nr, | |
168 | vcpu->exception.has_error_code, | |
169 | vcpu->exception.error_code); | |
170 | } | |
171 | ||
a03490ed CO |
172 | /* |
173 | * Load the pae pdptrs. Return true is they are all valid. | |
174 | */ | |
175 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
176 | { | |
177 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
178 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
179 | int i; | |
180 | int ret; | |
181 | u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)]; | |
182 | ||
183 | mutex_lock(&vcpu->kvm->lock); | |
184 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, | |
185 | offset * sizeof(u64), sizeof(pdpte)); | |
186 | if (ret < 0) { | |
187 | ret = 0; | |
188 | goto out; | |
189 | } | |
190 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
191 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { | |
192 | ret = 0; | |
193 | goto out; | |
194 | } | |
195 | } | |
196 | ret = 1; | |
197 | ||
198 | memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs)); | |
199 | out: | |
200 | mutex_unlock(&vcpu->kvm->lock); | |
201 | ||
202 | return ret; | |
203 | } | |
204 | ||
d835dfec AK |
205 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
206 | { | |
207 | u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)]; | |
208 | bool changed = true; | |
209 | int r; | |
210 | ||
211 | if (is_long_mode(vcpu) || !is_pae(vcpu)) | |
212 | return false; | |
213 | ||
214 | mutex_lock(&vcpu->kvm->lock); | |
215 | r = kvm_read_guest(vcpu->kvm, vcpu->cr3 & ~31u, pdpte, sizeof(pdpte)); | |
216 | if (r < 0) | |
217 | goto out; | |
218 | changed = memcmp(pdpte, vcpu->pdptrs, sizeof(pdpte)) != 0; | |
219 | out: | |
220 | mutex_unlock(&vcpu->kvm->lock); | |
221 | ||
222 | return changed; | |
223 | } | |
224 | ||
a03490ed CO |
225 | void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
226 | { | |
227 | if (cr0 & CR0_RESERVED_BITS) { | |
228 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", | |
229 | cr0, vcpu->cr0); | |
c1a5d4f9 | 230 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
231 | return; |
232 | } | |
233 | ||
234 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { | |
235 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); | |
c1a5d4f9 | 236 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
237 | return; |
238 | } | |
239 | ||
240 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { | |
241 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " | |
242 | "and a clear PE flag\n"); | |
c1a5d4f9 | 243 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
244 | return; |
245 | } | |
246 | ||
247 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
248 | #ifdef CONFIG_X86_64 | |
249 | if ((vcpu->shadow_efer & EFER_LME)) { | |
250 | int cs_db, cs_l; | |
251 | ||
252 | if (!is_pae(vcpu)) { | |
253 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
254 | "in long mode while PAE is disabled\n"); | |
c1a5d4f9 | 255 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
256 | return; |
257 | } | |
258 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
259 | if (cs_l) { | |
260 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
261 | "in long mode while CS.L == 1\n"); | |
c1a5d4f9 | 262 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
263 | return; |
264 | ||
265 | } | |
266 | } else | |
267 | #endif | |
268 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) { | |
269 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " | |
270 | "reserved bits\n"); | |
c1a5d4f9 | 271 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
272 | return; |
273 | } | |
274 | ||
275 | } | |
276 | ||
277 | kvm_x86_ops->set_cr0(vcpu, cr0); | |
278 | vcpu->cr0 = cr0; | |
279 | ||
280 | mutex_lock(&vcpu->kvm->lock); | |
281 | kvm_mmu_reset_context(vcpu); | |
282 | mutex_unlock(&vcpu->kvm->lock); | |
283 | return; | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(set_cr0); | |
286 | ||
287 | void lmsw(struct kvm_vcpu *vcpu, unsigned long msw) | |
288 | { | |
289 | set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f)); | |
290 | } | |
291 | EXPORT_SYMBOL_GPL(lmsw); | |
292 | ||
293 | void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
294 | { | |
295 | if (cr4 & CR4_RESERVED_BITS) { | |
296 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); | |
c1a5d4f9 | 297 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
298 | return; |
299 | } | |
300 | ||
301 | if (is_long_mode(vcpu)) { | |
302 | if (!(cr4 & X86_CR4_PAE)) { | |
303 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " | |
304 | "in long mode\n"); | |
c1a5d4f9 | 305 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
306 | return; |
307 | } | |
308 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) | |
309 | && !load_pdptrs(vcpu, vcpu->cr3)) { | |
310 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); | |
c1a5d4f9 | 311 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
312 | return; |
313 | } | |
314 | ||
315 | if (cr4 & X86_CR4_VMXE) { | |
316 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); | |
c1a5d4f9 | 317 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
318 | return; |
319 | } | |
320 | kvm_x86_ops->set_cr4(vcpu, cr4); | |
321 | vcpu->cr4 = cr4; | |
322 | mutex_lock(&vcpu->kvm->lock); | |
323 | kvm_mmu_reset_context(vcpu); | |
324 | mutex_unlock(&vcpu->kvm->lock); | |
325 | } | |
326 | EXPORT_SYMBOL_GPL(set_cr4); | |
327 | ||
328 | void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
329 | { | |
d835dfec AK |
330 | if (cr3 == vcpu->cr3 && !pdptrs_changed(vcpu)) { |
331 | kvm_mmu_flush_tlb(vcpu); | |
332 | return; | |
333 | } | |
334 | ||
a03490ed CO |
335 | if (is_long_mode(vcpu)) { |
336 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { | |
337 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 338 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
339 | return; |
340 | } | |
341 | } else { | |
342 | if (is_pae(vcpu)) { | |
343 | if (cr3 & CR3_PAE_RESERVED_BITS) { | |
344 | printk(KERN_DEBUG | |
345 | "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 346 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
347 | return; |
348 | } | |
349 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { | |
350 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
351 | "reserved bits\n"); | |
c1a5d4f9 | 352 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
353 | return; |
354 | } | |
355 | } | |
356 | /* | |
357 | * We don't check reserved bits in nonpae mode, because | |
358 | * this isn't enforced, and VMware depends on this. | |
359 | */ | |
360 | } | |
361 | ||
362 | mutex_lock(&vcpu->kvm->lock); | |
363 | /* | |
364 | * Does the new cr3 value map to physical memory? (Note, we | |
365 | * catch an invalid cr3 even in real-mode, because it would | |
366 | * cause trouble later on when we turn on paging anyway.) | |
367 | * | |
368 | * A real CPU would silently accept an invalid cr3 and would | |
369 | * attempt to use it - with largely undefined (and often hard | |
370 | * to debug) behavior on the guest side. | |
371 | */ | |
372 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) | |
c1a5d4f9 | 373 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
374 | else { |
375 | vcpu->cr3 = cr3; | |
376 | vcpu->mmu.new_cr3(vcpu); | |
377 | } | |
378 | mutex_unlock(&vcpu->kvm->lock); | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(set_cr3); | |
381 | ||
382 | void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) | |
383 | { | |
384 | if (cr8 & CR8_RESERVED_BITS) { | |
385 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); | |
c1a5d4f9 | 386 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
387 | return; |
388 | } | |
389 | if (irqchip_in_kernel(vcpu->kvm)) | |
390 | kvm_lapic_set_tpr(vcpu, cr8); | |
391 | else | |
392 | vcpu->cr8 = cr8; | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(set_cr8); | |
395 | ||
396 | unsigned long get_cr8(struct kvm_vcpu *vcpu) | |
397 | { | |
398 | if (irqchip_in_kernel(vcpu->kvm)) | |
399 | return kvm_lapic_get_cr8(vcpu); | |
400 | else | |
401 | return vcpu->cr8; | |
402 | } | |
403 | EXPORT_SYMBOL_GPL(get_cr8); | |
404 | ||
043405e1 CO |
405 | /* |
406 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
407 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
408 | * | |
409 | * This list is modified at module load time to reflect the | |
410 | * capabilities of the host cpu. | |
411 | */ | |
412 | static u32 msrs_to_save[] = { | |
413 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
414 | MSR_K6_STAR, | |
415 | #ifdef CONFIG_X86_64 | |
416 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
417 | #endif | |
418 | MSR_IA32_TIME_STAMP_COUNTER, | |
419 | }; | |
420 | ||
421 | static unsigned num_msrs_to_save; | |
422 | ||
423 | static u32 emulated_msrs[] = { | |
424 | MSR_IA32_MISC_ENABLE, | |
425 | }; | |
426 | ||
15c4a640 CO |
427 | #ifdef CONFIG_X86_64 |
428 | ||
429 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) | |
430 | { | |
431 | if (efer & EFER_RESERVED_BITS) { | |
432 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", | |
433 | efer); | |
c1a5d4f9 | 434 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
435 | return; |
436 | } | |
437 | ||
438 | if (is_paging(vcpu) | |
439 | && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) { | |
440 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); | |
c1a5d4f9 | 441 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
442 | return; |
443 | } | |
444 | ||
445 | kvm_x86_ops->set_efer(vcpu, efer); | |
446 | ||
447 | efer &= ~EFER_LMA; | |
448 | efer |= vcpu->shadow_efer & EFER_LMA; | |
449 | ||
450 | vcpu->shadow_efer = efer; | |
451 | } | |
452 | ||
453 | #endif | |
454 | ||
455 | /* | |
456 | * Writes msr value into into the appropriate "register". | |
457 | * Returns 0 on success, non-0 otherwise. | |
458 | * Assumes vcpu_load() was already called. | |
459 | */ | |
460 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
461 | { | |
462 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); | |
463 | } | |
464 | ||
313a3dc7 CO |
465 | /* |
466 | * Adapt set_msr() to msr_io()'s calling convention | |
467 | */ | |
468 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
469 | { | |
470 | return kvm_set_msr(vcpu, index, *data); | |
471 | } | |
472 | ||
15c4a640 CO |
473 | |
474 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |
475 | { | |
476 | switch (msr) { | |
477 | #ifdef CONFIG_X86_64 | |
478 | case MSR_EFER: | |
479 | set_efer(vcpu, data); | |
480 | break; | |
481 | #endif | |
482 | case MSR_IA32_MC0_STATUS: | |
483 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", | |
484 | __FUNCTION__, data); | |
485 | break; | |
486 | case MSR_IA32_MCG_STATUS: | |
487 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", | |
488 | __FUNCTION__, data); | |
489 | break; | |
490 | case MSR_IA32_UCODE_REV: | |
491 | case MSR_IA32_UCODE_WRITE: | |
492 | case 0x200 ... 0x2ff: /* MTRRs */ | |
493 | break; | |
494 | case MSR_IA32_APICBASE: | |
495 | kvm_set_apic_base(vcpu, data); | |
496 | break; | |
497 | case MSR_IA32_MISC_ENABLE: | |
498 | vcpu->ia32_misc_enable_msr = data; | |
499 | break; | |
500 | default: | |
501 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr); | |
502 | return 1; | |
503 | } | |
504 | return 0; | |
505 | } | |
506 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
507 | ||
508 | ||
509 | /* | |
510 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
511 | * Returns 0 on success, non-0 otherwise. | |
512 | * Assumes vcpu_load() was already called. | |
513 | */ | |
514 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
515 | { | |
516 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); | |
517 | } | |
518 | ||
519 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) | |
520 | { | |
521 | u64 data; | |
522 | ||
523 | switch (msr) { | |
524 | case 0xc0010010: /* SYSCFG */ | |
525 | case 0xc0010015: /* HWCR */ | |
526 | case MSR_IA32_PLATFORM_ID: | |
527 | case MSR_IA32_P5_MC_ADDR: | |
528 | case MSR_IA32_P5_MC_TYPE: | |
529 | case MSR_IA32_MC0_CTL: | |
530 | case MSR_IA32_MCG_STATUS: | |
531 | case MSR_IA32_MCG_CAP: | |
532 | case MSR_IA32_MC0_MISC: | |
533 | case MSR_IA32_MC0_MISC+4: | |
534 | case MSR_IA32_MC0_MISC+8: | |
535 | case MSR_IA32_MC0_MISC+12: | |
536 | case MSR_IA32_MC0_MISC+16: | |
537 | case MSR_IA32_UCODE_REV: | |
538 | case MSR_IA32_PERF_STATUS: | |
539 | case MSR_IA32_EBL_CR_POWERON: | |
540 | /* MTRR registers */ | |
541 | case 0xfe: | |
542 | case 0x200 ... 0x2ff: | |
543 | data = 0; | |
544 | break; | |
545 | case 0xcd: /* fsb frequency */ | |
546 | data = 3; | |
547 | break; | |
548 | case MSR_IA32_APICBASE: | |
549 | data = kvm_get_apic_base(vcpu); | |
550 | break; | |
551 | case MSR_IA32_MISC_ENABLE: | |
552 | data = vcpu->ia32_misc_enable_msr; | |
553 | break; | |
554 | #ifdef CONFIG_X86_64 | |
555 | case MSR_EFER: | |
556 | data = vcpu->shadow_efer; | |
557 | break; | |
558 | #endif | |
559 | default: | |
560 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); | |
561 | return 1; | |
562 | } | |
563 | *pdata = data; | |
564 | return 0; | |
565 | } | |
566 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
567 | ||
313a3dc7 CO |
568 | /* |
569 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
570 | * | |
571 | * @return number of msrs set successfully. | |
572 | */ | |
573 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
574 | struct kvm_msr_entry *entries, | |
575 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
576 | unsigned index, u64 *data)) | |
577 | { | |
578 | int i; | |
579 | ||
580 | vcpu_load(vcpu); | |
581 | ||
582 | for (i = 0; i < msrs->nmsrs; ++i) | |
583 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
584 | break; | |
585 | ||
586 | vcpu_put(vcpu); | |
587 | ||
588 | return i; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Read or write a bunch of msrs. Parameters are user addresses. | |
593 | * | |
594 | * @return number of msrs set successfully. | |
595 | */ | |
596 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
597 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
598 | unsigned index, u64 *data), | |
599 | int writeback) | |
600 | { | |
601 | struct kvm_msrs msrs; | |
602 | struct kvm_msr_entry *entries; | |
603 | int r, n; | |
604 | unsigned size; | |
605 | ||
606 | r = -EFAULT; | |
607 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
608 | goto out; | |
609 | ||
610 | r = -E2BIG; | |
611 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
612 | goto out; | |
613 | ||
614 | r = -ENOMEM; | |
615 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
616 | entries = vmalloc(size); | |
617 | if (!entries) | |
618 | goto out; | |
619 | ||
620 | r = -EFAULT; | |
621 | if (copy_from_user(entries, user_msrs->entries, size)) | |
622 | goto out_free; | |
623 | ||
624 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
625 | if (r < 0) | |
626 | goto out_free; | |
627 | ||
628 | r = -EFAULT; | |
629 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
630 | goto out_free; | |
631 | ||
632 | r = n; | |
633 | ||
634 | out_free: | |
635 | vfree(entries); | |
636 | out: | |
637 | return r; | |
638 | } | |
639 | ||
e9b11c17 ZX |
640 | /* |
641 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
642 | * cached on it. | |
643 | */ | |
644 | void decache_vcpus_on_cpu(int cpu) | |
645 | { | |
646 | struct kvm *vm; | |
647 | struct kvm_vcpu *vcpu; | |
648 | int i; | |
649 | ||
650 | spin_lock(&kvm_lock); | |
651 | list_for_each_entry(vm, &vm_list, vm_list) | |
652 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
653 | vcpu = vm->vcpus[i]; | |
654 | if (!vcpu) | |
655 | continue; | |
656 | /* | |
657 | * If the vcpu is locked, then it is running on some | |
658 | * other cpu and therefore it is not cached on the | |
659 | * cpu in question. | |
660 | * | |
661 | * If it's not locked, check the last cpu it executed | |
662 | * on. | |
663 | */ | |
664 | if (mutex_trylock(&vcpu->mutex)) { | |
665 | if (vcpu->cpu == cpu) { | |
666 | kvm_x86_ops->vcpu_decache(vcpu); | |
667 | vcpu->cpu = -1; | |
668 | } | |
669 | mutex_unlock(&vcpu->mutex); | |
670 | } | |
671 | } | |
672 | spin_unlock(&kvm_lock); | |
673 | } | |
674 | ||
018d00d2 ZX |
675 | int kvm_dev_ioctl_check_extension(long ext) |
676 | { | |
677 | int r; | |
678 | ||
679 | switch (ext) { | |
680 | case KVM_CAP_IRQCHIP: | |
681 | case KVM_CAP_HLT: | |
682 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
683 | case KVM_CAP_USER_MEMORY: | |
684 | case KVM_CAP_SET_TSS_ADDR: | |
07716717 | 685 | case KVM_CAP_EXT_CPUID: |
018d00d2 ZX |
686 | r = 1; |
687 | break; | |
688 | default: | |
689 | r = 0; | |
690 | break; | |
691 | } | |
692 | return r; | |
693 | ||
694 | } | |
695 | ||
043405e1 CO |
696 | long kvm_arch_dev_ioctl(struct file *filp, |
697 | unsigned int ioctl, unsigned long arg) | |
698 | { | |
699 | void __user *argp = (void __user *)arg; | |
700 | long r; | |
701 | ||
702 | switch (ioctl) { | |
703 | case KVM_GET_MSR_INDEX_LIST: { | |
704 | struct kvm_msr_list __user *user_msr_list = argp; | |
705 | struct kvm_msr_list msr_list; | |
706 | unsigned n; | |
707 | ||
708 | r = -EFAULT; | |
709 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
710 | goto out; | |
711 | n = msr_list.nmsrs; | |
712 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); | |
713 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) | |
714 | goto out; | |
715 | r = -E2BIG; | |
716 | if (n < num_msrs_to_save) | |
717 | goto out; | |
718 | r = -EFAULT; | |
719 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
720 | num_msrs_to_save * sizeof(u32))) | |
721 | goto out; | |
722 | if (copy_to_user(user_msr_list->indices | |
723 | + num_msrs_to_save * sizeof(u32), | |
724 | &emulated_msrs, | |
725 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) | |
726 | goto out; | |
727 | r = 0; | |
728 | break; | |
729 | } | |
730 | default: | |
731 | r = -EINVAL; | |
732 | } | |
733 | out: | |
734 | return r; | |
735 | } | |
736 | ||
313a3dc7 CO |
737 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
738 | { | |
739 | kvm_x86_ops->vcpu_load(vcpu, cpu); | |
740 | } | |
741 | ||
742 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
743 | { | |
744 | kvm_x86_ops->vcpu_put(vcpu); | |
9327fd11 | 745 | kvm_put_guest_fpu(vcpu); |
313a3dc7 CO |
746 | } |
747 | ||
07716717 | 748 | static int is_efer_nx(void) |
313a3dc7 CO |
749 | { |
750 | u64 efer; | |
313a3dc7 CO |
751 | |
752 | rdmsrl(MSR_EFER, efer); | |
07716717 DK |
753 | return efer & EFER_NX; |
754 | } | |
755 | ||
756 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) | |
757 | { | |
758 | int i; | |
759 | struct kvm_cpuid_entry2 *e, *entry; | |
760 | ||
313a3dc7 CO |
761 | entry = NULL; |
762 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
763 | e = &vcpu->cpuid_entries[i]; | |
764 | if (e->function == 0x80000001) { | |
765 | entry = e; | |
766 | break; | |
767 | } | |
768 | } | |
07716717 | 769 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
313a3dc7 CO |
770 | entry->edx &= ~(1 << 20); |
771 | printk(KERN_INFO "kvm: guest NX capability removed\n"); | |
772 | } | |
773 | } | |
774 | ||
07716717 | 775 | /* when an old userspace process fills a new kernel module */ |
313a3dc7 CO |
776 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
777 | struct kvm_cpuid *cpuid, | |
778 | struct kvm_cpuid_entry __user *entries) | |
07716717 DK |
779 | { |
780 | int r, i; | |
781 | struct kvm_cpuid_entry *cpuid_entries; | |
782 | ||
783 | r = -E2BIG; | |
784 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
785 | goto out; | |
786 | r = -ENOMEM; | |
787 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); | |
788 | if (!cpuid_entries) | |
789 | goto out; | |
790 | r = -EFAULT; | |
791 | if (copy_from_user(cpuid_entries, entries, | |
792 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
793 | goto out_free; | |
794 | for (i = 0; i < cpuid->nent; i++) { | |
795 | vcpu->cpuid_entries[i].function = cpuid_entries[i].function; | |
796 | vcpu->cpuid_entries[i].eax = cpuid_entries[i].eax; | |
797 | vcpu->cpuid_entries[i].ebx = cpuid_entries[i].ebx; | |
798 | vcpu->cpuid_entries[i].ecx = cpuid_entries[i].ecx; | |
799 | vcpu->cpuid_entries[i].edx = cpuid_entries[i].edx; | |
800 | vcpu->cpuid_entries[i].index = 0; | |
801 | vcpu->cpuid_entries[i].flags = 0; | |
802 | vcpu->cpuid_entries[i].padding[0] = 0; | |
803 | vcpu->cpuid_entries[i].padding[1] = 0; | |
804 | vcpu->cpuid_entries[i].padding[2] = 0; | |
805 | } | |
806 | vcpu->cpuid_nent = cpuid->nent; | |
807 | cpuid_fix_nx_cap(vcpu); | |
808 | r = 0; | |
809 | ||
810 | out_free: | |
811 | vfree(cpuid_entries); | |
812 | out: | |
813 | return r; | |
814 | } | |
815 | ||
816 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | |
817 | struct kvm_cpuid2 *cpuid, | |
818 | struct kvm_cpuid_entry2 __user *entries) | |
313a3dc7 CO |
819 | { |
820 | int r; | |
821 | ||
822 | r = -E2BIG; | |
823 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
824 | goto out; | |
825 | r = -EFAULT; | |
826 | if (copy_from_user(&vcpu->cpuid_entries, entries, | |
07716717 | 827 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
313a3dc7 CO |
828 | goto out; |
829 | vcpu->cpuid_nent = cpuid->nent; | |
313a3dc7 CO |
830 | return 0; |
831 | ||
832 | out: | |
833 | return r; | |
834 | } | |
835 | ||
07716717 DK |
836 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
837 | struct kvm_cpuid2 *cpuid, | |
838 | struct kvm_cpuid_entry2 __user *entries) | |
839 | { | |
840 | int r; | |
841 | ||
842 | r = -E2BIG; | |
843 | if (cpuid->nent < vcpu->cpuid_nent) | |
844 | goto out; | |
845 | r = -EFAULT; | |
846 | if (copy_to_user(entries, &vcpu->cpuid_entries, | |
847 | vcpu->cpuid_nent * sizeof(struct kvm_cpuid_entry2))) | |
848 | goto out; | |
849 | return 0; | |
850 | ||
851 | out: | |
852 | cpuid->nent = vcpu->cpuid_nent; | |
853 | return r; | |
854 | } | |
855 | ||
856 | static inline u32 bit(int bitno) | |
857 | { | |
858 | return 1 << (bitno & 31); | |
859 | } | |
860 | ||
861 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
862 | u32 index) | |
863 | { | |
864 | entry->function = function; | |
865 | entry->index = index; | |
866 | cpuid_count(entry->function, entry->index, | |
867 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); | |
868 | entry->flags = 0; | |
869 | } | |
870 | ||
871 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
872 | u32 index, int *nent, int maxnent) | |
873 | { | |
874 | const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) | | |
875 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | | |
876 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | | |
877 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | | |
878 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | | |
879 | bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) | | |
880 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | | |
881 | bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) | | |
882 | bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) | | |
883 | bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP); | |
884 | const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) | | |
885 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | | |
886 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | | |
887 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | | |
888 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | | |
889 | bit(X86_FEATURE_PGE) | | |
890 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | | |
891 | bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) | | |
892 | bit(X86_FEATURE_SYSCALL) | | |
893 | (bit(X86_FEATURE_NX) && is_efer_nx()) | | |
894 | #ifdef CONFIG_X86_64 | |
895 | bit(X86_FEATURE_LM) | | |
896 | #endif | |
897 | bit(X86_FEATURE_MMXEXT) | | |
898 | bit(X86_FEATURE_3DNOWEXT) | | |
899 | bit(X86_FEATURE_3DNOW); | |
900 | const u32 kvm_supported_word3_x86_features = | |
901 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); | |
902 | const u32 kvm_supported_word6_x86_features = | |
903 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY); | |
904 | ||
905 | /* all func 2 cpuid_count() should be called on the same cpu */ | |
906 | get_cpu(); | |
907 | do_cpuid_1_ent(entry, function, index); | |
908 | ++*nent; | |
909 | ||
910 | switch (function) { | |
911 | case 0: | |
912 | entry->eax = min(entry->eax, (u32)0xb); | |
913 | break; | |
914 | case 1: | |
915 | entry->edx &= kvm_supported_word0_x86_features; | |
916 | entry->ecx &= kvm_supported_word3_x86_features; | |
917 | break; | |
918 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands | |
919 | * may return different values. This forces us to get_cpu() before | |
920 | * issuing the first command, and also to emulate this annoying behavior | |
921 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ | |
922 | case 2: { | |
923 | int t, times = entry->eax & 0xff; | |
924 | ||
925 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
926 | for (t = 1; t < times && *nent < maxnent; ++t) { | |
927 | do_cpuid_1_ent(&entry[t], function, 0); | |
928 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
929 | ++*nent; | |
930 | } | |
931 | break; | |
932 | } | |
933 | /* function 4 and 0xb have additional index. */ | |
934 | case 4: { | |
935 | int index, cache_type; | |
936 | ||
937 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
938 | /* read more entries until cache_type is zero */ | |
939 | for (index = 1; *nent < maxnent; ++index) { | |
940 | cache_type = entry[index - 1].eax & 0x1f; | |
941 | if (!cache_type) | |
942 | break; | |
943 | do_cpuid_1_ent(&entry[index], function, index); | |
944 | entry[index].flags |= | |
945 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
946 | ++*nent; | |
947 | } | |
948 | break; | |
949 | } | |
950 | case 0xb: { | |
951 | int index, level_type; | |
952 | ||
953 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
954 | /* read more entries until level_type is zero */ | |
955 | for (index = 1; *nent < maxnent; ++index) { | |
956 | level_type = entry[index - 1].ecx & 0xff; | |
957 | if (!level_type) | |
958 | break; | |
959 | do_cpuid_1_ent(&entry[index], function, index); | |
960 | entry[index].flags |= | |
961 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
962 | ++*nent; | |
963 | } | |
964 | break; | |
965 | } | |
966 | case 0x80000000: | |
967 | entry->eax = min(entry->eax, 0x8000001a); | |
968 | break; | |
969 | case 0x80000001: | |
970 | entry->edx &= kvm_supported_word1_x86_features; | |
971 | entry->ecx &= kvm_supported_word6_x86_features; | |
972 | break; | |
973 | } | |
974 | put_cpu(); | |
975 | } | |
976 | ||
977 | static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm, | |
978 | struct kvm_cpuid2 *cpuid, | |
979 | struct kvm_cpuid_entry2 __user *entries) | |
980 | { | |
981 | struct kvm_cpuid_entry2 *cpuid_entries; | |
982 | int limit, nent = 0, r = -E2BIG; | |
983 | u32 func; | |
984 | ||
985 | if (cpuid->nent < 1) | |
986 | goto out; | |
987 | r = -ENOMEM; | |
988 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); | |
989 | if (!cpuid_entries) | |
990 | goto out; | |
991 | ||
992 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); | |
993 | limit = cpuid_entries[0].eax; | |
994 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) | |
995 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
996 | &nent, cpuid->nent); | |
997 | r = -E2BIG; | |
998 | if (nent >= cpuid->nent) | |
999 | goto out_free; | |
1000 | ||
1001 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); | |
1002 | limit = cpuid_entries[nent - 1].eax; | |
1003 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) | |
1004 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
1005 | &nent, cpuid->nent); | |
1006 | r = -EFAULT; | |
1007 | if (copy_to_user(entries, cpuid_entries, | |
1008 | nent * sizeof(struct kvm_cpuid_entry2))) | |
1009 | goto out_free; | |
1010 | cpuid->nent = nent; | |
1011 | r = 0; | |
1012 | ||
1013 | out_free: | |
1014 | vfree(cpuid_entries); | |
1015 | out: | |
1016 | return r; | |
1017 | } | |
1018 | ||
313a3dc7 CO |
1019 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
1020 | struct kvm_lapic_state *s) | |
1021 | { | |
1022 | vcpu_load(vcpu); | |
1023 | memcpy(s->regs, vcpu->apic->regs, sizeof *s); | |
1024 | vcpu_put(vcpu); | |
1025 | ||
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
1030 | struct kvm_lapic_state *s) | |
1031 | { | |
1032 | vcpu_load(vcpu); | |
1033 | memcpy(vcpu->apic->regs, s->regs, sizeof *s); | |
1034 | kvm_apic_post_state_restore(vcpu); | |
1035 | vcpu_put(vcpu); | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
f77bc6a4 ZX |
1040 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
1041 | struct kvm_interrupt *irq) | |
1042 | { | |
1043 | if (irq->irq < 0 || irq->irq >= 256) | |
1044 | return -EINVAL; | |
1045 | if (irqchip_in_kernel(vcpu->kvm)) | |
1046 | return -ENXIO; | |
1047 | vcpu_load(vcpu); | |
1048 | ||
1049 | set_bit(irq->irq, vcpu->irq_pending); | |
1050 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
1051 | ||
1052 | vcpu_put(vcpu); | |
1053 | ||
1054 | return 0; | |
1055 | } | |
1056 | ||
313a3dc7 CO |
1057 | long kvm_arch_vcpu_ioctl(struct file *filp, |
1058 | unsigned int ioctl, unsigned long arg) | |
1059 | { | |
1060 | struct kvm_vcpu *vcpu = filp->private_data; | |
1061 | void __user *argp = (void __user *)arg; | |
1062 | int r; | |
1063 | ||
1064 | switch (ioctl) { | |
1065 | case KVM_GET_LAPIC: { | |
1066 | struct kvm_lapic_state lapic; | |
1067 | ||
1068 | memset(&lapic, 0, sizeof lapic); | |
1069 | r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic); | |
1070 | if (r) | |
1071 | goto out; | |
1072 | r = -EFAULT; | |
1073 | if (copy_to_user(argp, &lapic, sizeof lapic)) | |
1074 | goto out; | |
1075 | r = 0; | |
1076 | break; | |
1077 | } | |
1078 | case KVM_SET_LAPIC: { | |
1079 | struct kvm_lapic_state lapic; | |
1080 | ||
1081 | r = -EFAULT; | |
1082 | if (copy_from_user(&lapic, argp, sizeof lapic)) | |
1083 | goto out; | |
1084 | r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);; | |
1085 | if (r) | |
1086 | goto out; | |
1087 | r = 0; | |
1088 | break; | |
1089 | } | |
f77bc6a4 ZX |
1090 | case KVM_INTERRUPT: { |
1091 | struct kvm_interrupt irq; | |
1092 | ||
1093 | r = -EFAULT; | |
1094 | if (copy_from_user(&irq, argp, sizeof irq)) | |
1095 | goto out; | |
1096 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
1097 | if (r) | |
1098 | goto out; | |
1099 | r = 0; | |
1100 | break; | |
1101 | } | |
313a3dc7 CO |
1102 | case KVM_SET_CPUID: { |
1103 | struct kvm_cpuid __user *cpuid_arg = argp; | |
1104 | struct kvm_cpuid cpuid; | |
1105 | ||
1106 | r = -EFAULT; | |
1107 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1108 | goto out; | |
1109 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
1110 | if (r) | |
1111 | goto out; | |
1112 | break; | |
1113 | } | |
07716717 DK |
1114 | case KVM_SET_CPUID2: { |
1115 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1116 | struct kvm_cpuid2 cpuid; | |
1117 | ||
1118 | r = -EFAULT; | |
1119 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1120 | goto out; | |
1121 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
1122 | cpuid_arg->entries); | |
1123 | if (r) | |
1124 | goto out; | |
1125 | break; | |
1126 | } | |
1127 | case KVM_GET_CPUID2: { | |
1128 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1129 | struct kvm_cpuid2 cpuid; | |
1130 | ||
1131 | r = -EFAULT; | |
1132 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1133 | goto out; | |
1134 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
1135 | cpuid_arg->entries); | |
1136 | if (r) | |
1137 | goto out; | |
1138 | r = -EFAULT; | |
1139 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
1140 | goto out; | |
1141 | r = 0; | |
1142 | break; | |
1143 | } | |
313a3dc7 CO |
1144 | case KVM_GET_MSRS: |
1145 | r = msr_io(vcpu, argp, kvm_get_msr, 1); | |
1146 | break; | |
1147 | case KVM_SET_MSRS: | |
1148 | r = msr_io(vcpu, argp, do_set_msr, 0); | |
1149 | break; | |
1150 | default: | |
1151 | r = -EINVAL; | |
1152 | } | |
1153 | out: | |
1154 | return r; | |
1155 | } | |
1156 | ||
1fe779f8 CO |
1157 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
1158 | { | |
1159 | int ret; | |
1160 | ||
1161 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
1162 | return -1; | |
1163 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); | |
1164 | return ret; | |
1165 | } | |
1166 | ||
1167 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, | |
1168 | u32 kvm_nr_mmu_pages) | |
1169 | { | |
1170 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
1171 | return -EINVAL; | |
1172 | ||
1173 | mutex_lock(&kvm->lock); | |
1174 | ||
1175 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
1176 | kvm->n_requested_mmu_pages = kvm_nr_mmu_pages; | |
1177 | ||
1178 | mutex_unlock(&kvm->lock); | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) | |
1183 | { | |
1184 | return kvm->n_alloc_mmu_pages; | |
1185 | } | |
1186 | ||
e9f85cde ZX |
1187 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
1188 | { | |
1189 | int i; | |
1190 | struct kvm_mem_alias *alias; | |
1191 | ||
1192 | for (i = 0; i < kvm->naliases; ++i) { | |
1193 | alias = &kvm->aliases[i]; | |
1194 | if (gfn >= alias->base_gfn | |
1195 | && gfn < alias->base_gfn + alias->npages) | |
1196 | return alias->target_gfn + gfn - alias->base_gfn; | |
1197 | } | |
1198 | return gfn; | |
1199 | } | |
1200 | ||
1fe779f8 CO |
1201 | /* |
1202 | * Set a new alias region. Aliases map a portion of physical memory into | |
1203 | * another portion. This is useful for memory windows, for example the PC | |
1204 | * VGA region. | |
1205 | */ | |
1206 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, | |
1207 | struct kvm_memory_alias *alias) | |
1208 | { | |
1209 | int r, n; | |
1210 | struct kvm_mem_alias *p; | |
1211 | ||
1212 | r = -EINVAL; | |
1213 | /* General sanity checks */ | |
1214 | if (alias->memory_size & (PAGE_SIZE - 1)) | |
1215 | goto out; | |
1216 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) | |
1217 | goto out; | |
1218 | if (alias->slot >= KVM_ALIAS_SLOTS) | |
1219 | goto out; | |
1220 | if (alias->guest_phys_addr + alias->memory_size | |
1221 | < alias->guest_phys_addr) | |
1222 | goto out; | |
1223 | if (alias->target_phys_addr + alias->memory_size | |
1224 | < alias->target_phys_addr) | |
1225 | goto out; | |
1226 | ||
1227 | mutex_lock(&kvm->lock); | |
1228 | ||
1229 | p = &kvm->aliases[alias->slot]; | |
1230 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; | |
1231 | p->npages = alias->memory_size >> PAGE_SHIFT; | |
1232 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; | |
1233 | ||
1234 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) | |
1235 | if (kvm->aliases[n - 1].npages) | |
1236 | break; | |
1237 | kvm->naliases = n; | |
1238 | ||
1239 | kvm_mmu_zap_all(kvm); | |
1240 | ||
1241 | mutex_unlock(&kvm->lock); | |
1242 | ||
1243 | return 0; | |
1244 | ||
1245 | out: | |
1246 | return r; | |
1247 | } | |
1248 | ||
1249 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1250 | { | |
1251 | int r; | |
1252 | ||
1253 | r = 0; | |
1254 | switch (chip->chip_id) { | |
1255 | case KVM_IRQCHIP_PIC_MASTER: | |
1256 | memcpy(&chip->chip.pic, | |
1257 | &pic_irqchip(kvm)->pics[0], | |
1258 | sizeof(struct kvm_pic_state)); | |
1259 | break; | |
1260 | case KVM_IRQCHIP_PIC_SLAVE: | |
1261 | memcpy(&chip->chip.pic, | |
1262 | &pic_irqchip(kvm)->pics[1], | |
1263 | sizeof(struct kvm_pic_state)); | |
1264 | break; | |
1265 | case KVM_IRQCHIP_IOAPIC: | |
1266 | memcpy(&chip->chip.ioapic, | |
1267 | ioapic_irqchip(kvm), | |
1268 | sizeof(struct kvm_ioapic_state)); | |
1269 | break; | |
1270 | default: | |
1271 | r = -EINVAL; | |
1272 | break; | |
1273 | } | |
1274 | return r; | |
1275 | } | |
1276 | ||
1277 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1278 | { | |
1279 | int r; | |
1280 | ||
1281 | r = 0; | |
1282 | switch (chip->chip_id) { | |
1283 | case KVM_IRQCHIP_PIC_MASTER: | |
1284 | memcpy(&pic_irqchip(kvm)->pics[0], | |
1285 | &chip->chip.pic, | |
1286 | sizeof(struct kvm_pic_state)); | |
1287 | break; | |
1288 | case KVM_IRQCHIP_PIC_SLAVE: | |
1289 | memcpy(&pic_irqchip(kvm)->pics[1], | |
1290 | &chip->chip.pic, | |
1291 | sizeof(struct kvm_pic_state)); | |
1292 | break; | |
1293 | case KVM_IRQCHIP_IOAPIC: | |
1294 | memcpy(ioapic_irqchip(kvm), | |
1295 | &chip->chip.ioapic, | |
1296 | sizeof(struct kvm_ioapic_state)); | |
1297 | break; | |
1298 | default: | |
1299 | r = -EINVAL; | |
1300 | break; | |
1301 | } | |
1302 | kvm_pic_update_irq(pic_irqchip(kvm)); | |
1303 | return r; | |
1304 | } | |
1305 | ||
5bb064dc ZX |
1306 | /* |
1307 | * Get (and clear) the dirty memory log for a memory slot. | |
1308 | */ | |
1309 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
1310 | struct kvm_dirty_log *log) | |
1311 | { | |
1312 | int r; | |
1313 | int n; | |
1314 | struct kvm_memory_slot *memslot; | |
1315 | int is_dirty = 0; | |
1316 | ||
1317 | mutex_lock(&kvm->lock); | |
1318 | ||
1319 | r = kvm_get_dirty_log(kvm, log, &is_dirty); | |
1320 | if (r) | |
1321 | goto out; | |
1322 | ||
1323 | /* If nothing is dirty, don't bother messing with page tables. */ | |
1324 | if (is_dirty) { | |
1325 | kvm_mmu_slot_remove_write_access(kvm, log->slot); | |
1326 | kvm_flush_remote_tlbs(kvm); | |
1327 | memslot = &kvm->memslots[log->slot]; | |
1328 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; | |
1329 | memset(memslot->dirty_bitmap, 0, n); | |
1330 | } | |
1331 | r = 0; | |
1332 | out: | |
1333 | mutex_unlock(&kvm->lock); | |
1334 | return r; | |
1335 | } | |
1336 | ||
1fe779f8 CO |
1337 | long kvm_arch_vm_ioctl(struct file *filp, |
1338 | unsigned int ioctl, unsigned long arg) | |
1339 | { | |
1340 | struct kvm *kvm = filp->private_data; | |
1341 | void __user *argp = (void __user *)arg; | |
1342 | int r = -EINVAL; | |
1343 | ||
1344 | switch (ioctl) { | |
1345 | case KVM_SET_TSS_ADDR: | |
1346 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1347 | if (r < 0) | |
1348 | goto out; | |
1349 | break; | |
1350 | case KVM_SET_MEMORY_REGION: { | |
1351 | struct kvm_memory_region kvm_mem; | |
1352 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1353 | ||
1354 | r = -EFAULT; | |
1355 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) | |
1356 | goto out; | |
1357 | kvm_userspace_mem.slot = kvm_mem.slot; | |
1358 | kvm_userspace_mem.flags = kvm_mem.flags; | |
1359 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; | |
1360 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; | |
1361 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); | |
1362 | if (r) | |
1363 | goto out; | |
1364 | break; | |
1365 | } | |
1366 | case KVM_SET_NR_MMU_PAGES: | |
1367 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1368 | if (r) | |
1369 | goto out; | |
1370 | break; | |
1371 | case KVM_GET_NR_MMU_PAGES: | |
1372 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
1373 | break; | |
1374 | case KVM_SET_MEMORY_ALIAS: { | |
1375 | struct kvm_memory_alias alias; | |
1376 | ||
1377 | r = -EFAULT; | |
1378 | if (copy_from_user(&alias, argp, sizeof alias)) | |
1379 | goto out; | |
1380 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); | |
1381 | if (r) | |
1382 | goto out; | |
1383 | break; | |
1384 | } | |
1385 | case KVM_CREATE_IRQCHIP: | |
1386 | r = -ENOMEM; | |
1387 | kvm->vpic = kvm_create_pic(kvm); | |
1388 | if (kvm->vpic) { | |
1389 | r = kvm_ioapic_init(kvm); | |
1390 | if (r) { | |
1391 | kfree(kvm->vpic); | |
1392 | kvm->vpic = NULL; | |
1393 | goto out; | |
1394 | } | |
1395 | } else | |
1396 | goto out; | |
1397 | break; | |
1398 | case KVM_IRQ_LINE: { | |
1399 | struct kvm_irq_level irq_event; | |
1400 | ||
1401 | r = -EFAULT; | |
1402 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | |
1403 | goto out; | |
1404 | if (irqchip_in_kernel(kvm)) { | |
1405 | mutex_lock(&kvm->lock); | |
1406 | if (irq_event.irq < 16) | |
1407 | kvm_pic_set_irq(pic_irqchip(kvm), | |
1408 | irq_event.irq, | |
1409 | irq_event.level); | |
1410 | kvm_ioapic_set_irq(kvm->vioapic, | |
1411 | irq_event.irq, | |
1412 | irq_event.level); | |
1413 | mutex_unlock(&kvm->lock); | |
1414 | r = 0; | |
1415 | } | |
1416 | break; | |
1417 | } | |
1418 | case KVM_GET_IRQCHIP: { | |
1419 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
1420 | struct kvm_irqchip chip; | |
1421 | ||
1422 | r = -EFAULT; | |
1423 | if (copy_from_user(&chip, argp, sizeof chip)) | |
1424 | goto out; | |
1425 | r = -ENXIO; | |
1426 | if (!irqchip_in_kernel(kvm)) | |
1427 | goto out; | |
1428 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); | |
1429 | if (r) | |
1430 | goto out; | |
1431 | r = -EFAULT; | |
1432 | if (copy_to_user(argp, &chip, sizeof chip)) | |
1433 | goto out; | |
1434 | r = 0; | |
1435 | break; | |
1436 | } | |
1437 | case KVM_SET_IRQCHIP: { | |
1438 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
1439 | struct kvm_irqchip chip; | |
1440 | ||
1441 | r = -EFAULT; | |
1442 | if (copy_from_user(&chip, argp, sizeof chip)) | |
1443 | goto out; | |
1444 | r = -ENXIO; | |
1445 | if (!irqchip_in_kernel(kvm)) | |
1446 | goto out; | |
1447 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); | |
1448 | if (r) | |
1449 | goto out; | |
1450 | r = 0; | |
1451 | break; | |
1452 | } | |
07716717 DK |
1453 | case KVM_GET_SUPPORTED_CPUID: { |
1454 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1455 | struct kvm_cpuid2 cpuid; | |
1456 | ||
1457 | r = -EFAULT; | |
1458 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1459 | goto out; | |
1460 | r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid, | |
1461 | cpuid_arg->entries); | |
1462 | if (r) | |
1463 | goto out; | |
1464 | ||
1465 | r = -EFAULT; | |
1466 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
1467 | goto out; | |
1468 | r = 0; | |
1469 | break; | |
1470 | } | |
1fe779f8 CO |
1471 | default: |
1472 | ; | |
1473 | } | |
1474 | out: | |
1475 | return r; | |
1476 | } | |
1477 | ||
a16b043c | 1478 | static void kvm_init_msr_list(void) |
043405e1 CO |
1479 | { |
1480 | u32 dummy[2]; | |
1481 | unsigned i, j; | |
1482 | ||
1483 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
1484 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
1485 | continue; | |
1486 | if (j < i) | |
1487 | msrs_to_save[j] = msrs_to_save[i]; | |
1488 | j++; | |
1489 | } | |
1490 | num_msrs_to_save = j; | |
1491 | } | |
1492 | ||
bbd9b64e CO |
1493 | /* |
1494 | * Only apic need an MMIO device hook, so shortcut now.. | |
1495 | */ | |
1496 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
1497 | gpa_t addr) | |
1498 | { | |
1499 | struct kvm_io_device *dev; | |
1500 | ||
1501 | if (vcpu->apic) { | |
1502 | dev = &vcpu->apic->dev; | |
1503 | if (dev->in_range(dev, addr)) | |
1504 | return dev; | |
1505 | } | |
1506 | return NULL; | |
1507 | } | |
1508 | ||
1509 | ||
1510 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, | |
1511 | gpa_t addr) | |
1512 | { | |
1513 | struct kvm_io_device *dev; | |
1514 | ||
1515 | dev = vcpu_find_pervcpu_dev(vcpu, addr); | |
1516 | if (dev == NULL) | |
1517 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); | |
1518 | return dev; | |
1519 | } | |
1520 | ||
1521 | int emulator_read_std(unsigned long addr, | |
1522 | void *val, | |
1523 | unsigned int bytes, | |
1524 | struct kvm_vcpu *vcpu) | |
1525 | { | |
1526 | void *data = val; | |
1527 | ||
1528 | while (bytes) { | |
1529 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
1530 | unsigned offset = addr & (PAGE_SIZE-1); | |
1531 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
1532 | int ret; | |
1533 | ||
1534 | if (gpa == UNMAPPED_GVA) | |
1535 | return X86EMUL_PROPAGATE_FAULT; | |
1536 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); | |
1537 | if (ret < 0) | |
1538 | return X86EMUL_UNHANDLEABLE; | |
1539 | ||
1540 | bytes -= tocopy; | |
1541 | data += tocopy; | |
1542 | addr += tocopy; | |
1543 | } | |
1544 | ||
1545 | return X86EMUL_CONTINUE; | |
1546 | } | |
1547 | EXPORT_SYMBOL_GPL(emulator_read_std); | |
1548 | ||
bbd9b64e CO |
1549 | static int emulator_read_emulated(unsigned long addr, |
1550 | void *val, | |
1551 | unsigned int bytes, | |
1552 | struct kvm_vcpu *vcpu) | |
1553 | { | |
1554 | struct kvm_io_device *mmio_dev; | |
1555 | gpa_t gpa; | |
1556 | ||
1557 | if (vcpu->mmio_read_completed) { | |
1558 | memcpy(val, vcpu->mmio_data, bytes); | |
1559 | vcpu->mmio_read_completed = 0; | |
1560 | return X86EMUL_CONTINUE; | |
1561 | } | |
1562 | ||
1563 | gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
1564 | ||
1565 | /* For APIC access vmexit */ | |
1566 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
1567 | goto mmio; | |
1568 | ||
1569 | if (emulator_read_std(addr, val, bytes, vcpu) | |
1570 | == X86EMUL_CONTINUE) | |
1571 | return X86EMUL_CONTINUE; | |
1572 | if (gpa == UNMAPPED_GVA) | |
1573 | return X86EMUL_PROPAGATE_FAULT; | |
1574 | ||
1575 | mmio: | |
1576 | /* | |
1577 | * Is this MMIO handled locally? | |
1578 | */ | |
1579 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1580 | if (mmio_dev) { | |
1581 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
1582 | return X86EMUL_CONTINUE; | |
1583 | } | |
1584 | ||
1585 | vcpu->mmio_needed = 1; | |
1586 | vcpu->mmio_phys_addr = gpa; | |
1587 | vcpu->mmio_size = bytes; | |
1588 | vcpu->mmio_is_write = 0; | |
1589 | ||
1590 | return X86EMUL_UNHANDLEABLE; | |
1591 | } | |
1592 | ||
1593 | static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, | |
1594 | const void *val, int bytes) | |
1595 | { | |
1596 | int ret; | |
1597 | ||
1598 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); | |
1599 | if (ret < 0) | |
1600 | return 0; | |
1601 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); | |
1602 | return 1; | |
1603 | } | |
1604 | ||
1605 | static int emulator_write_emulated_onepage(unsigned long addr, | |
1606 | const void *val, | |
1607 | unsigned int bytes, | |
1608 | struct kvm_vcpu *vcpu) | |
1609 | { | |
1610 | struct kvm_io_device *mmio_dev; | |
1611 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr); | |
1612 | ||
1613 | if (gpa == UNMAPPED_GVA) { | |
c3c91fee | 1614 | kvm_inject_page_fault(vcpu, addr, 2); |
bbd9b64e CO |
1615 | return X86EMUL_PROPAGATE_FAULT; |
1616 | } | |
1617 | ||
1618 | /* For APIC access vmexit */ | |
1619 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
1620 | goto mmio; | |
1621 | ||
1622 | if (emulator_write_phys(vcpu, gpa, val, bytes)) | |
1623 | return X86EMUL_CONTINUE; | |
1624 | ||
1625 | mmio: | |
1626 | /* | |
1627 | * Is this MMIO handled locally? | |
1628 | */ | |
1629 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); | |
1630 | if (mmio_dev) { | |
1631 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
1632 | return X86EMUL_CONTINUE; | |
1633 | } | |
1634 | ||
1635 | vcpu->mmio_needed = 1; | |
1636 | vcpu->mmio_phys_addr = gpa; | |
1637 | vcpu->mmio_size = bytes; | |
1638 | vcpu->mmio_is_write = 1; | |
1639 | memcpy(vcpu->mmio_data, val, bytes); | |
1640 | ||
1641 | return X86EMUL_CONTINUE; | |
1642 | } | |
1643 | ||
1644 | int emulator_write_emulated(unsigned long addr, | |
1645 | const void *val, | |
1646 | unsigned int bytes, | |
1647 | struct kvm_vcpu *vcpu) | |
1648 | { | |
1649 | /* Crossing a page boundary? */ | |
1650 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
1651 | int rc, now; | |
1652 | ||
1653 | now = -addr & ~PAGE_MASK; | |
1654 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); | |
1655 | if (rc != X86EMUL_CONTINUE) | |
1656 | return rc; | |
1657 | addr += now; | |
1658 | val += now; | |
1659 | bytes -= now; | |
1660 | } | |
1661 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); | |
1662 | } | |
1663 | EXPORT_SYMBOL_GPL(emulator_write_emulated); | |
1664 | ||
1665 | static int emulator_cmpxchg_emulated(unsigned long addr, | |
1666 | const void *old, | |
1667 | const void *new, | |
1668 | unsigned int bytes, | |
1669 | struct kvm_vcpu *vcpu) | |
1670 | { | |
1671 | static int reported; | |
1672 | ||
1673 | if (!reported) { | |
1674 | reported = 1; | |
1675 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
1676 | } | |
1677 | return emulator_write_emulated(addr, new, bytes, vcpu); | |
1678 | } | |
1679 | ||
1680 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1681 | { | |
1682 | return kvm_x86_ops->get_segment_base(vcpu, seg); | |
1683 | } | |
1684 | ||
1685 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
1686 | { | |
1687 | return X86EMUL_CONTINUE; | |
1688 | } | |
1689 | ||
1690 | int emulate_clts(struct kvm_vcpu *vcpu) | |
1691 | { | |
1692 | kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS); | |
1693 | return X86EMUL_CONTINUE; | |
1694 | } | |
1695 | ||
1696 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) | |
1697 | { | |
1698 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
1699 | ||
1700 | switch (dr) { | |
1701 | case 0 ... 3: | |
1702 | *dest = kvm_x86_ops->get_dr(vcpu, dr); | |
1703 | return X86EMUL_CONTINUE; | |
1704 | default: | |
1705 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr); | |
1706 | return X86EMUL_UNHANDLEABLE; | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
1711 | { | |
1712 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
1713 | int exception; | |
1714 | ||
1715 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); | |
1716 | if (exception) { | |
1717 | /* FIXME: better handling */ | |
1718 | return X86EMUL_UNHANDLEABLE; | |
1719 | } | |
1720 | return X86EMUL_CONTINUE; | |
1721 | } | |
1722 | ||
1723 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | |
1724 | { | |
1725 | static int reported; | |
1726 | u8 opcodes[4]; | |
1727 | unsigned long rip = vcpu->rip; | |
1728 | unsigned long rip_linear; | |
1729 | ||
1730 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); | |
1731 | ||
1732 | if (reported) | |
1733 | return; | |
1734 | ||
1735 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); | |
1736 | ||
1737 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", | |
1738 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
1739 | reported = 1; | |
1740 | } | |
1741 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); | |
1742 | ||
1743 | struct x86_emulate_ops emulate_ops = { | |
1744 | .read_std = emulator_read_std, | |
bbd9b64e CO |
1745 | .read_emulated = emulator_read_emulated, |
1746 | .write_emulated = emulator_write_emulated, | |
1747 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
1748 | }; | |
1749 | ||
1750 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
1751 | struct kvm_run *run, | |
1752 | unsigned long cr2, | |
1753 | u16 error_code, | |
1754 | int no_decode) | |
1755 | { | |
1756 | int r; | |
1757 | ||
1758 | vcpu->mmio_fault_cr2 = cr2; | |
1759 | kvm_x86_ops->cache_regs(vcpu); | |
1760 | ||
1761 | vcpu->mmio_is_write = 0; | |
1762 | vcpu->pio.string = 0; | |
1763 | ||
1764 | if (!no_decode) { | |
1765 | int cs_db, cs_l; | |
1766 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
1767 | ||
1768 | vcpu->emulate_ctxt.vcpu = vcpu; | |
1769 | vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); | |
bbd9b64e CO |
1770 | vcpu->emulate_ctxt.mode = |
1771 | (vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM) | |
1772 | ? X86EMUL_MODE_REAL : cs_l | |
1773 | ? X86EMUL_MODE_PROT64 : cs_db | |
1774 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
1775 | ||
1776 | if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) { | |
1777 | vcpu->emulate_ctxt.cs_base = 0; | |
1778 | vcpu->emulate_ctxt.ds_base = 0; | |
1779 | vcpu->emulate_ctxt.es_base = 0; | |
1780 | vcpu->emulate_ctxt.ss_base = 0; | |
1781 | } else { | |
1782 | vcpu->emulate_ctxt.cs_base = | |
1783 | get_segment_base(vcpu, VCPU_SREG_CS); | |
1784 | vcpu->emulate_ctxt.ds_base = | |
1785 | get_segment_base(vcpu, VCPU_SREG_DS); | |
1786 | vcpu->emulate_ctxt.es_base = | |
1787 | get_segment_base(vcpu, VCPU_SREG_ES); | |
1788 | vcpu->emulate_ctxt.ss_base = | |
1789 | get_segment_base(vcpu, VCPU_SREG_SS); | |
1790 | } | |
1791 | ||
1792 | vcpu->emulate_ctxt.gs_base = | |
1793 | get_segment_base(vcpu, VCPU_SREG_GS); | |
1794 | vcpu->emulate_ctxt.fs_base = | |
1795 | get_segment_base(vcpu, VCPU_SREG_FS); | |
1796 | ||
1797 | r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops); | |
f2b5756b | 1798 | ++vcpu->stat.insn_emulation; |
bbd9b64e | 1799 | if (r) { |
f2b5756b | 1800 | ++vcpu->stat.insn_emulation_fail; |
bbd9b64e CO |
1801 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
1802 | return EMULATE_DONE; | |
1803 | return EMULATE_FAIL; | |
1804 | } | |
1805 | } | |
1806 | ||
1807 | r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops); | |
1808 | ||
1809 | if (vcpu->pio.string) | |
1810 | return EMULATE_DO_MMIO; | |
1811 | ||
1812 | if ((r || vcpu->mmio_is_write) && run) { | |
1813 | run->exit_reason = KVM_EXIT_MMIO; | |
1814 | run->mmio.phys_addr = vcpu->mmio_phys_addr; | |
1815 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
1816 | run->mmio.len = vcpu->mmio_size; | |
1817 | run->mmio.is_write = vcpu->mmio_is_write; | |
1818 | } | |
1819 | ||
1820 | if (r) { | |
1821 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | |
1822 | return EMULATE_DONE; | |
1823 | if (!vcpu->mmio_needed) { | |
1824 | kvm_report_emulation_failure(vcpu, "mmio"); | |
1825 | return EMULATE_FAIL; | |
1826 | } | |
1827 | return EMULATE_DO_MMIO; | |
1828 | } | |
1829 | ||
1830 | kvm_x86_ops->decache_regs(vcpu); | |
1831 | kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags); | |
1832 | ||
1833 | if (vcpu->mmio_is_write) { | |
1834 | vcpu->mmio_needed = 0; | |
1835 | return EMULATE_DO_MMIO; | |
1836 | } | |
1837 | ||
1838 | return EMULATE_DONE; | |
1839 | } | |
1840 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
1841 | ||
de7d789a CO |
1842 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
1843 | { | |
1844 | int i; | |
1845 | ||
1846 | for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i) | |
1847 | if (vcpu->pio.guest_pages[i]) { | |
b4231d61 | 1848 | kvm_release_page_dirty(vcpu->pio.guest_pages[i]); |
de7d789a CO |
1849 | vcpu->pio.guest_pages[i] = NULL; |
1850 | } | |
1851 | } | |
1852 | ||
1853 | static int pio_copy_data(struct kvm_vcpu *vcpu) | |
1854 | { | |
1855 | void *p = vcpu->pio_data; | |
1856 | void *q; | |
1857 | unsigned bytes; | |
1858 | int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1; | |
1859 | ||
1860 | q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE, | |
1861 | PAGE_KERNEL); | |
1862 | if (!q) { | |
1863 | free_pio_guest_pages(vcpu); | |
1864 | return -ENOMEM; | |
1865 | } | |
1866 | q += vcpu->pio.guest_page_offset; | |
1867 | bytes = vcpu->pio.size * vcpu->pio.cur_count; | |
1868 | if (vcpu->pio.in) | |
1869 | memcpy(q, p, bytes); | |
1870 | else | |
1871 | memcpy(p, q, bytes); | |
1872 | q -= vcpu->pio.guest_page_offset; | |
1873 | vunmap(q); | |
1874 | free_pio_guest_pages(vcpu); | |
1875 | return 0; | |
1876 | } | |
1877 | ||
1878 | int complete_pio(struct kvm_vcpu *vcpu) | |
1879 | { | |
1880 | struct kvm_pio_request *io = &vcpu->pio; | |
1881 | long delta; | |
1882 | int r; | |
1883 | ||
1884 | kvm_x86_ops->cache_regs(vcpu); | |
1885 | ||
1886 | if (!io->string) { | |
1887 | if (io->in) | |
1888 | memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data, | |
1889 | io->size); | |
1890 | } else { | |
1891 | if (io->in) { | |
1892 | r = pio_copy_data(vcpu); | |
1893 | if (r) { | |
1894 | kvm_x86_ops->cache_regs(vcpu); | |
1895 | return r; | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | delta = 1; | |
1900 | if (io->rep) { | |
1901 | delta *= io->cur_count; | |
1902 | /* | |
1903 | * The size of the register should really depend on | |
1904 | * current address size. | |
1905 | */ | |
1906 | vcpu->regs[VCPU_REGS_RCX] -= delta; | |
1907 | } | |
1908 | if (io->down) | |
1909 | delta = -delta; | |
1910 | delta *= io->size; | |
1911 | if (io->in) | |
1912 | vcpu->regs[VCPU_REGS_RDI] += delta; | |
1913 | else | |
1914 | vcpu->regs[VCPU_REGS_RSI] += delta; | |
1915 | } | |
1916 | ||
1917 | kvm_x86_ops->decache_regs(vcpu); | |
1918 | ||
1919 | io->count -= io->cur_count; | |
1920 | io->cur_count = 0; | |
1921 | ||
1922 | return 0; | |
1923 | } | |
1924 | ||
1925 | static void kernel_pio(struct kvm_io_device *pio_dev, | |
1926 | struct kvm_vcpu *vcpu, | |
1927 | void *pd) | |
1928 | { | |
1929 | /* TODO: String I/O for in kernel device */ | |
1930 | ||
1931 | mutex_lock(&vcpu->kvm->lock); | |
1932 | if (vcpu->pio.in) | |
1933 | kvm_iodevice_read(pio_dev, vcpu->pio.port, | |
1934 | vcpu->pio.size, | |
1935 | pd); | |
1936 | else | |
1937 | kvm_iodevice_write(pio_dev, vcpu->pio.port, | |
1938 | vcpu->pio.size, | |
1939 | pd); | |
1940 | mutex_unlock(&vcpu->kvm->lock); | |
1941 | } | |
1942 | ||
1943 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
1944 | struct kvm_vcpu *vcpu) | |
1945 | { | |
1946 | struct kvm_pio_request *io = &vcpu->pio; | |
1947 | void *pd = vcpu->pio_data; | |
1948 | int i; | |
1949 | ||
1950 | mutex_lock(&vcpu->kvm->lock); | |
1951 | for (i = 0; i < io->cur_count; i++) { | |
1952 | kvm_iodevice_write(pio_dev, io->port, | |
1953 | io->size, | |
1954 | pd); | |
1955 | pd += io->size; | |
1956 | } | |
1957 | mutex_unlock(&vcpu->kvm->lock); | |
1958 | } | |
1959 | ||
1960 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, | |
1961 | gpa_t addr) | |
1962 | { | |
1963 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); | |
1964 | } | |
1965 | ||
1966 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
1967 | int size, unsigned port) | |
1968 | { | |
1969 | struct kvm_io_device *pio_dev; | |
1970 | ||
1971 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
1972 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
1973 | vcpu->run->io.size = vcpu->pio.size = size; | |
1974 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
1975 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1; | |
1976 | vcpu->run->io.port = vcpu->pio.port = port; | |
1977 | vcpu->pio.in = in; | |
1978 | vcpu->pio.string = 0; | |
1979 | vcpu->pio.down = 0; | |
1980 | vcpu->pio.guest_page_offset = 0; | |
1981 | vcpu->pio.rep = 0; | |
1982 | ||
1983 | kvm_x86_ops->cache_regs(vcpu); | |
1984 | memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4); | |
1985 | kvm_x86_ops->decache_regs(vcpu); | |
1986 | ||
1987 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
1988 | ||
1989 | pio_dev = vcpu_find_pio_dev(vcpu, port); | |
1990 | if (pio_dev) { | |
1991 | kernel_pio(pio_dev, vcpu, vcpu->pio_data); | |
1992 | complete_pio(vcpu); | |
1993 | return 1; | |
1994 | } | |
1995 | return 0; | |
1996 | } | |
1997 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
1998 | ||
1999 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
2000 | int size, unsigned long count, int down, | |
2001 | gva_t address, int rep, unsigned port) | |
2002 | { | |
2003 | unsigned now, in_page; | |
2004 | int i, ret = 0; | |
2005 | int nr_pages = 1; | |
2006 | struct page *page; | |
2007 | struct kvm_io_device *pio_dev; | |
2008 | ||
2009 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
2010 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
2011 | vcpu->run->io.size = vcpu->pio.size = size; | |
2012 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
2013 | vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count; | |
2014 | vcpu->run->io.port = vcpu->pio.port = port; | |
2015 | vcpu->pio.in = in; | |
2016 | vcpu->pio.string = 1; | |
2017 | vcpu->pio.down = down; | |
2018 | vcpu->pio.guest_page_offset = offset_in_page(address); | |
2019 | vcpu->pio.rep = rep; | |
2020 | ||
2021 | if (!count) { | |
2022 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2023 | return 1; | |
2024 | } | |
2025 | ||
2026 | if (!down) | |
2027 | in_page = PAGE_SIZE - offset_in_page(address); | |
2028 | else | |
2029 | in_page = offset_in_page(address) + size; | |
2030 | now = min(count, (unsigned long)in_page / size); | |
2031 | if (!now) { | |
2032 | /* | |
2033 | * String I/O straddles page boundary. Pin two guest pages | |
2034 | * so that we satisfy atomicity constraints. Do just one | |
2035 | * transaction to avoid complexity. | |
2036 | */ | |
2037 | nr_pages = 2; | |
2038 | now = 1; | |
2039 | } | |
2040 | if (down) { | |
2041 | /* | |
2042 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
2043 | */ | |
2044 | pr_unimpl(vcpu, "guest string pio down\n"); | |
c1a5d4f9 | 2045 | kvm_inject_gp(vcpu, 0); |
de7d789a CO |
2046 | return 1; |
2047 | } | |
2048 | vcpu->run->io.count = now; | |
2049 | vcpu->pio.cur_count = now; | |
2050 | ||
2051 | if (vcpu->pio.cur_count == vcpu->pio.count) | |
2052 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2053 | ||
2054 | for (i = 0; i < nr_pages; ++i) { | |
2055 | mutex_lock(&vcpu->kvm->lock); | |
2056 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); | |
2057 | vcpu->pio.guest_pages[i] = page; | |
2058 | mutex_unlock(&vcpu->kvm->lock); | |
2059 | if (!page) { | |
c1a5d4f9 | 2060 | kvm_inject_gp(vcpu, 0); |
de7d789a CO |
2061 | free_pio_guest_pages(vcpu); |
2062 | return 1; | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | pio_dev = vcpu_find_pio_dev(vcpu, port); | |
2067 | if (!vcpu->pio.in) { | |
2068 | /* string PIO write */ | |
2069 | ret = pio_copy_data(vcpu); | |
2070 | if (ret >= 0 && pio_dev) { | |
2071 | pio_string_write(pio_dev, vcpu); | |
2072 | complete_pio(vcpu); | |
2073 | if (vcpu->pio.count == 0) | |
2074 | ret = 1; | |
2075 | } | |
2076 | } else if (pio_dev) | |
2077 | pr_unimpl(vcpu, "no string pio read support yet, " | |
2078 | "port %x size %d count %ld\n", | |
2079 | port, size, count); | |
2080 | ||
2081 | return ret; | |
2082 | } | |
2083 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); | |
2084 | ||
f8c16bba | 2085 | int kvm_arch_init(void *opaque) |
043405e1 | 2086 | { |
56c6d28a | 2087 | int r; |
f8c16bba ZX |
2088 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
2089 | ||
56c6d28a ZX |
2090 | r = kvm_mmu_module_init(); |
2091 | if (r) | |
2092 | goto out_fail; | |
2093 | ||
043405e1 | 2094 | kvm_init_msr_list(); |
f8c16bba ZX |
2095 | |
2096 | if (kvm_x86_ops) { | |
2097 | printk(KERN_ERR "kvm: already loaded the other module\n"); | |
56c6d28a ZX |
2098 | r = -EEXIST; |
2099 | goto out; | |
f8c16bba ZX |
2100 | } |
2101 | ||
2102 | if (!ops->cpu_has_kvm_support()) { | |
2103 | printk(KERN_ERR "kvm: no hardware support\n"); | |
56c6d28a ZX |
2104 | r = -EOPNOTSUPP; |
2105 | goto out; | |
f8c16bba ZX |
2106 | } |
2107 | if (ops->disabled_by_bios()) { | |
2108 | printk(KERN_ERR "kvm: disabled by bios\n"); | |
56c6d28a ZX |
2109 | r = -EOPNOTSUPP; |
2110 | goto out; | |
f8c16bba ZX |
2111 | } |
2112 | ||
2113 | kvm_x86_ops = ops; | |
56c6d28a | 2114 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
f8c16bba | 2115 | return 0; |
56c6d28a ZX |
2116 | |
2117 | out: | |
2118 | kvm_mmu_module_exit(); | |
2119 | out_fail: | |
2120 | return r; | |
043405e1 | 2121 | } |
8776e519 | 2122 | |
f8c16bba ZX |
2123 | void kvm_arch_exit(void) |
2124 | { | |
2125 | kvm_x86_ops = NULL; | |
56c6d28a ZX |
2126 | kvm_mmu_module_exit(); |
2127 | } | |
f8c16bba | 2128 | |
8776e519 HB |
2129 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
2130 | { | |
2131 | ++vcpu->stat.halt_exits; | |
2132 | if (irqchip_in_kernel(vcpu->kvm)) { | |
2133 | vcpu->mp_state = VCPU_MP_STATE_HALTED; | |
2134 | kvm_vcpu_block(vcpu); | |
2135 | if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE) | |
2136 | return -EINTR; | |
2137 | return 1; | |
2138 | } else { | |
2139 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
2140 | return 0; | |
2141 | } | |
2142 | } | |
2143 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
2144 | ||
2145 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) | |
2146 | { | |
2147 | unsigned long nr, a0, a1, a2, a3, ret; | |
2148 | ||
2149 | kvm_x86_ops->cache_regs(vcpu); | |
2150 | ||
2151 | nr = vcpu->regs[VCPU_REGS_RAX]; | |
2152 | a0 = vcpu->regs[VCPU_REGS_RBX]; | |
2153 | a1 = vcpu->regs[VCPU_REGS_RCX]; | |
2154 | a2 = vcpu->regs[VCPU_REGS_RDX]; | |
2155 | a3 = vcpu->regs[VCPU_REGS_RSI]; | |
2156 | ||
2157 | if (!is_long_mode(vcpu)) { | |
2158 | nr &= 0xFFFFFFFF; | |
2159 | a0 &= 0xFFFFFFFF; | |
2160 | a1 &= 0xFFFFFFFF; | |
2161 | a2 &= 0xFFFFFFFF; | |
2162 | a3 &= 0xFFFFFFFF; | |
2163 | } | |
2164 | ||
2165 | switch (nr) { | |
2166 | default: | |
2167 | ret = -KVM_ENOSYS; | |
2168 | break; | |
2169 | } | |
2170 | vcpu->regs[VCPU_REGS_RAX] = ret; | |
2171 | kvm_x86_ops->decache_regs(vcpu); | |
2172 | return 0; | |
2173 | } | |
2174 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
2175 | ||
2176 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) | |
2177 | { | |
2178 | char instruction[3]; | |
2179 | int ret = 0; | |
2180 | ||
2181 | mutex_lock(&vcpu->kvm->lock); | |
2182 | ||
2183 | /* | |
2184 | * Blow out the MMU to ensure that no other VCPU has an active mapping | |
2185 | * to ensure that the updated hypercall appears atomically across all | |
2186 | * VCPUs. | |
2187 | */ | |
2188 | kvm_mmu_zap_all(vcpu->kvm); | |
2189 | ||
2190 | kvm_x86_ops->cache_regs(vcpu); | |
2191 | kvm_x86_ops->patch_hypercall(vcpu, instruction); | |
2192 | if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu) | |
2193 | != X86EMUL_CONTINUE) | |
2194 | ret = -EFAULT; | |
2195 | ||
2196 | mutex_unlock(&vcpu->kvm->lock); | |
2197 | ||
2198 | return ret; | |
2199 | } | |
2200 | ||
2201 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | |
2202 | { | |
2203 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
2204 | } | |
2205 | ||
2206 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
2207 | { | |
2208 | struct descriptor_table dt = { limit, base }; | |
2209 | ||
2210 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
2211 | } | |
2212 | ||
2213 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
2214 | { | |
2215 | struct descriptor_table dt = { limit, base }; | |
2216 | ||
2217 | kvm_x86_ops->set_idt(vcpu, &dt); | |
2218 | } | |
2219 | ||
2220 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
2221 | unsigned long *rflags) | |
2222 | { | |
2223 | lmsw(vcpu, msw); | |
2224 | *rflags = kvm_x86_ops->get_rflags(vcpu); | |
2225 | } | |
2226 | ||
2227 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
2228 | { | |
2229 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
2230 | switch (cr) { | |
2231 | case 0: | |
2232 | return vcpu->cr0; | |
2233 | case 2: | |
2234 | return vcpu->cr2; | |
2235 | case 3: | |
2236 | return vcpu->cr3; | |
2237 | case 4: | |
2238 | return vcpu->cr4; | |
152ff9be JR |
2239 | case 8: |
2240 | return get_cr8(vcpu); | |
8776e519 HB |
2241 | default: |
2242 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
2243 | return 0; | |
2244 | } | |
2245 | } | |
2246 | ||
2247 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
2248 | unsigned long *rflags) | |
2249 | { | |
2250 | switch (cr) { | |
2251 | case 0: | |
2252 | set_cr0(vcpu, mk_cr_64(vcpu->cr0, val)); | |
2253 | *rflags = kvm_x86_ops->get_rflags(vcpu); | |
2254 | break; | |
2255 | case 2: | |
2256 | vcpu->cr2 = val; | |
2257 | break; | |
2258 | case 3: | |
2259 | set_cr3(vcpu, val); | |
2260 | break; | |
2261 | case 4: | |
2262 | set_cr4(vcpu, mk_cr_64(vcpu->cr4, val)); | |
2263 | break; | |
152ff9be JR |
2264 | case 8: |
2265 | set_cr8(vcpu, val & 0xfUL); | |
2266 | break; | |
8776e519 HB |
2267 | default: |
2268 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr); | |
2269 | } | |
2270 | } | |
2271 | ||
07716717 DK |
2272 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
2273 | { | |
2274 | struct kvm_cpuid_entry2 *e = &vcpu->cpuid_entries[i]; | |
2275 | int j, nent = vcpu->cpuid_nent; | |
2276 | ||
2277 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; | |
2278 | /* when no next entry is found, the current entry[i] is reselected */ | |
2279 | for (j = i + 1; j == i; j = (j + 1) % nent) { | |
2280 | struct kvm_cpuid_entry2 *ej = &vcpu->cpuid_entries[j]; | |
2281 | if (ej->function == e->function) { | |
2282 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
2283 | return j; | |
2284 | } | |
2285 | } | |
2286 | return 0; /* silence gcc, even though control never reaches here */ | |
2287 | } | |
2288 | ||
2289 | /* find an entry with matching function, matching index (if needed), and that | |
2290 | * should be read next (if it's stateful) */ | |
2291 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |
2292 | u32 function, u32 index) | |
2293 | { | |
2294 | if (e->function != function) | |
2295 | return 0; | |
2296 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | |
2297 | return 0; | |
2298 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | |
2299 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) | |
2300 | return 0; | |
2301 | return 1; | |
2302 | } | |
2303 | ||
8776e519 HB |
2304 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
2305 | { | |
2306 | int i; | |
07716717 DK |
2307 | u32 function, index; |
2308 | struct kvm_cpuid_entry2 *e, *best; | |
8776e519 HB |
2309 | |
2310 | kvm_x86_ops->cache_regs(vcpu); | |
2311 | function = vcpu->regs[VCPU_REGS_RAX]; | |
07716717 | 2312 | index = vcpu->regs[VCPU_REGS_RCX]; |
8776e519 HB |
2313 | vcpu->regs[VCPU_REGS_RAX] = 0; |
2314 | vcpu->regs[VCPU_REGS_RBX] = 0; | |
2315 | vcpu->regs[VCPU_REGS_RCX] = 0; | |
2316 | vcpu->regs[VCPU_REGS_RDX] = 0; | |
2317 | best = NULL; | |
2318 | for (i = 0; i < vcpu->cpuid_nent; ++i) { | |
2319 | e = &vcpu->cpuid_entries[i]; | |
07716717 DK |
2320 | if (is_matching_cpuid_entry(e, function, index)) { |
2321 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | |
2322 | move_to_next_stateful_cpuid_entry(vcpu, i); | |
8776e519 HB |
2323 | best = e; |
2324 | break; | |
2325 | } | |
2326 | /* | |
2327 | * Both basic or both extended? | |
2328 | */ | |
2329 | if (((e->function ^ function) & 0x80000000) == 0) | |
2330 | if (!best || e->function > best->function) | |
2331 | best = e; | |
2332 | } | |
2333 | if (best) { | |
2334 | vcpu->regs[VCPU_REGS_RAX] = best->eax; | |
2335 | vcpu->regs[VCPU_REGS_RBX] = best->ebx; | |
2336 | vcpu->regs[VCPU_REGS_RCX] = best->ecx; | |
2337 | vcpu->regs[VCPU_REGS_RDX] = best->edx; | |
2338 | } | |
2339 | kvm_x86_ops->decache_regs(vcpu); | |
2340 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2341 | } | |
2342 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
d0752060 | 2343 | |
b6c7a5dc HB |
2344 | /* |
2345 | * Check if userspace requested an interrupt window, and that the | |
2346 | * interrupt window is open. | |
2347 | * | |
2348 | * No need to exit to userspace if we already have an interrupt queued. | |
2349 | */ | |
2350 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
2351 | struct kvm_run *kvm_run) | |
2352 | { | |
2353 | return (!vcpu->irq_summary && | |
2354 | kvm_run->request_interrupt_window && | |
2355 | vcpu->interrupt_window_open && | |
2356 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); | |
2357 | } | |
2358 | ||
2359 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
2360 | struct kvm_run *kvm_run) | |
2361 | { | |
2362 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
2363 | kvm_run->cr8 = get_cr8(vcpu); | |
2364 | kvm_run->apic_base = kvm_get_apic_base(vcpu); | |
2365 | if (irqchip_in_kernel(vcpu->kvm)) | |
2366 | kvm_run->ready_for_interrupt_injection = 1; | |
2367 | else | |
2368 | kvm_run->ready_for_interrupt_injection = | |
2369 | (vcpu->interrupt_window_open && | |
2370 | vcpu->irq_summary == 0); | |
2371 | } | |
2372 | ||
2373 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2374 | { | |
2375 | int r; | |
2376 | ||
2377 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) { | |
2378 | pr_debug("vcpu %d received sipi with vector # %x\n", | |
2379 | vcpu->vcpu_id, vcpu->sipi_vector); | |
2380 | kvm_lapic_reset(vcpu); | |
2381 | r = kvm_x86_ops->vcpu_reset(vcpu); | |
2382 | if (r) | |
2383 | return r; | |
2384 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
2385 | } | |
2386 | ||
2387 | preempted: | |
2388 | if (vcpu->guest_debug.enabled) | |
2389 | kvm_x86_ops->guest_debug_pre(vcpu); | |
2390 | ||
2391 | again: | |
2392 | r = kvm_mmu_reload(vcpu); | |
2393 | if (unlikely(r)) | |
2394 | goto out; | |
2395 | ||
2396 | kvm_inject_pending_timer_irqs(vcpu); | |
2397 | ||
2398 | preempt_disable(); | |
2399 | ||
2400 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
2401 | kvm_load_guest_fpu(vcpu); | |
2402 | ||
2403 | local_irq_disable(); | |
2404 | ||
2405 | if (signal_pending(current)) { | |
2406 | local_irq_enable(); | |
2407 | preempt_enable(); | |
2408 | r = -EINTR; | |
2409 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2410 | ++vcpu->stat.signal_exits; | |
2411 | goto out; | |
2412 | } | |
2413 | ||
298101da AK |
2414 | if (vcpu->exception.pending) |
2415 | __queue_exception(vcpu); | |
2416 | else if (irqchip_in_kernel(vcpu->kvm)) | |
b6c7a5dc | 2417 | kvm_x86_ops->inject_pending_irq(vcpu); |
eb9774f0 | 2418 | else |
b6c7a5dc HB |
2419 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
2420 | ||
2421 | vcpu->guest_mode = 1; | |
2422 | kvm_guest_enter(); | |
2423 | ||
2424 | if (vcpu->requests) | |
2425 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) | |
2426 | kvm_x86_ops->tlb_flush(vcpu); | |
2427 | ||
2428 | kvm_x86_ops->run(vcpu, kvm_run); | |
2429 | ||
2430 | vcpu->guest_mode = 0; | |
2431 | local_irq_enable(); | |
2432 | ||
2433 | ++vcpu->stat.exits; | |
2434 | ||
2435 | /* | |
2436 | * We must have an instruction between local_irq_enable() and | |
2437 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
2438 | * the interrupt shadow. The stat.exits increment will do nicely. | |
2439 | * But we need to prevent reordering, hence this barrier(): | |
2440 | */ | |
2441 | barrier(); | |
2442 | ||
2443 | kvm_guest_exit(); | |
2444 | ||
2445 | preempt_enable(); | |
2446 | ||
2447 | /* | |
2448 | * Profile KVM exit RIPs: | |
2449 | */ | |
2450 | if (unlikely(prof_on == KVM_PROFILING)) { | |
2451 | kvm_x86_ops->cache_regs(vcpu); | |
2452 | profile_hit(KVM_PROFILING, (void *)vcpu->rip); | |
2453 | } | |
2454 | ||
298101da AK |
2455 | if (vcpu->exception.pending && kvm_x86_ops->exception_injected(vcpu)) |
2456 | vcpu->exception.pending = false; | |
2457 | ||
b6c7a5dc HB |
2458 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
2459 | ||
2460 | if (r > 0) { | |
2461 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
2462 | r = -EINTR; | |
2463 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2464 | ++vcpu->stat.request_irq_exits; | |
2465 | goto out; | |
2466 | } | |
e1beb1d3 | 2467 | if (!need_resched()) |
b6c7a5dc | 2468 | goto again; |
b6c7a5dc HB |
2469 | } |
2470 | ||
2471 | out: | |
2472 | if (r > 0) { | |
2473 | kvm_resched(vcpu); | |
2474 | goto preempted; | |
2475 | } | |
2476 | ||
2477 | post_kvm_run_save(vcpu, kvm_run); | |
2478 | ||
2479 | return r; | |
2480 | } | |
2481 | ||
2482 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2483 | { | |
2484 | int r; | |
2485 | sigset_t sigsaved; | |
2486 | ||
2487 | vcpu_load(vcpu); | |
2488 | ||
2489 | if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) { | |
2490 | kvm_vcpu_block(vcpu); | |
2491 | vcpu_put(vcpu); | |
2492 | return -EAGAIN; | |
2493 | } | |
2494 | ||
2495 | if (vcpu->sigset_active) | |
2496 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
2497 | ||
2498 | /* re-sync apic's tpr */ | |
2499 | if (!irqchip_in_kernel(vcpu->kvm)) | |
2500 | set_cr8(vcpu, kvm_run->cr8); | |
2501 | ||
2502 | if (vcpu->pio.cur_count) { | |
2503 | r = complete_pio(vcpu); | |
2504 | if (r) | |
2505 | goto out; | |
2506 | } | |
2507 | #if CONFIG_HAS_IOMEM | |
2508 | if (vcpu->mmio_needed) { | |
2509 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
2510 | vcpu->mmio_read_completed = 1; | |
2511 | vcpu->mmio_needed = 0; | |
2512 | r = emulate_instruction(vcpu, kvm_run, | |
2513 | vcpu->mmio_fault_cr2, 0, 1); | |
2514 | if (r == EMULATE_DO_MMIO) { | |
2515 | /* | |
2516 | * Read-modify-write. Back to userspace. | |
2517 | */ | |
2518 | r = 0; | |
2519 | goto out; | |
2520 | } | |
2521 | } | |
2522 | #endif | |
2523 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { | |
2524 | kvm_x86_ops->cache_regs(vcpu); | |
2525 | vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; | |
2526 | kvm_x86_ops->decache_regs(vcpu); | |
2527 | } | |
2528 | ||
2529 | r = __vcpu_run(vcpu, kvm_run); | |
2530 | ||
2531 | out: | |
2532 | if (vcpu->sigset_active) | |
2533 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
2534 | ||
2535 | vcpu_put(vcpu); | |
2536 | return r; | |
2537 | } | |
2538 | ||
2539 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
2540 | { | |
2541 | vcpu_load(vcpu); | |
2542 | ||
2543 | kvm_x86_ops->cache_regs(vcpu); | |
2544 | ||
2545 | regs->rax = vcpu->regs[VCPU_REGS_RAX]; | |
2546 | regs->rbx = vcpu->regs[VCPU_REGS_RBX]; | |
2547 | regs->rcx = vcpu->regs[VCPU_REGS_RCX]; | |
2548 | regs->rdx = vcpu->regs[VCPU_REGS_RDX]; | |
2549 | regs->rsi = vcpu->regs[VCPU_REGS_RSI]; | |
2550 | regs->rdi = vcpu->regs[VCPU_REGS_RDI]; | |
2551 | regs->rsp = vcpu->regs[VCPU_REGS_RSP]; | |
2552 | regs->rbp = vcpu->regs[VCPU_REGS_RBP]; | |
2553 | #ifdef CONFIG_X86_64 | |
2554 | regs->r8 = vcpu->regs[VCPU_REGS_R8]; | |
2555 | regs->r9 = vcpu->regs[VCPU_REGS_R9]; | |
2556 | regs->r10 = vcpu->regs[VCPU_REGS_R10]; | |
2557 | regs->r11 = vcpu->regs[VCPU_REGS_R11]; | |
2558 | regs->r12 = vcpu->regs[VCPU_REGS_R12]; | |
2559 | regs->r13 = vcpu->regs[VCPU_REGS_R13]; | |
2560 | regs->r14 = vcpu->regs[VCPU_REGS_R14]; | |
2561 | regs->r15 = vcpu->regs[VCPU_REGS_R15]; | |
2562 | #endif | |
2563 | ||
2564 | regs->rip = vcpu->rip; | |
2565 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); | |
2566 | ||
2567 | /* | |
2568 | * Don't leak debug flags in case they were set for guest debugging | |
2569 | */ | |
2570 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
2571 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
2572 | ||
2573 | vcpu_put(vcpu); | |
2574 | ||
2575 | return 0; | |
2576 | } | |
2577 | ||
2578 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
2579 | { | |
2580 | vcpu_load(vcpu); | |
2581 | ||
2582 | vcpu->regs[VCPU_REGS_RAX] = regs->rax; | |
2583 | vcpu->regs[VCPU_REGS_RBX] = regs->rbx; | |
2584 | vcpu->regs[VCPU_REGS_RCX] = regs->rcx; | |
2585 | vcpu->regs[VCPU_REGS_RDX] = regs->rdx; | |
2586 | vcpu->regs[VCPU_REGS_RSI] = regs->rsi; | |
2587 | vcpu->regs[VCPU_REGS_RDI] = regs->rdi; | |
2588 | vcpu->regs[VCPU_REGS_RSP] = regs->rsp; | |
2589 | vcpu->regs[VCPU_REGS_RBP] = regs->rbp; | |
2590 | #ifdef CONFIG_X86_64 | |
2591 | vcpu->regs[VCPU_REGS_R8] = regs->r8; | |
2592 | vcpu->regs[VCPU_REGS_R9] = regs->r9; | |
2593 | vcpu->regs[VCPU_REGS_R10] = regs->r10; | |
2594 | vcpu->regs[VCPU_REGS_R11] = regs->r11; | |
2595 | vcpu->regs[VCPU_REGS_R12] = regs->r12; | |
2596 | vcpu->regs[VCPU_REGS_R13] = regs->r13; | |
2597 | vcpu->regs[VCPU_REGS_R14] = regs->r14; | |
2598 | vcpu->regs[VCPU_REGS_R15] = regs->r15; | |
2599 | #endif | |
2600 | ||
2601 | vcpu->rip = regs->rip; | |
2602 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); | |
2603 | ||
2604 | kvm_x86_ops->decache_regs(vcpu); | |
2605 | ||
2606 | vcpu_put(vcpu); | |
2607 | ||
2608 | return 0; | |
2609 | } | |
2610 | ||
2611 | static void get_segment(struct kvm_vcpu *vcpu, | |
2612 | struct kvm_segment *var, int seg) | |
2613 | { | |
2614 | return kvm_x86_ops->get_segment(vcpu, var, seg); | |
2615 | } | |
2616 | ||
2617 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) | |
2618 | { | |
2619 | struct kvm_segment cs; | |
2620 | ||
2621 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
2622 | *db = cs.db; | |
2623 | *l = cs.l; | |
2624 | } | |
2625 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
2626 | ||
2627 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
2628 | struct kvm_sregs *sregs) | |
2629 | { | |
2630 | struct descriptor_table dt; | |
2631 | int pending_vec; | |
2632 | ||
2633 | vcpu_load(vcpu); | |
2634 | ||
2635 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
2636 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2637 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2638 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2639 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2640 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2641 | ||
2642 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2643 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2644 | ||
2645 | kvm_x86_ops->get_idt(vcpu, &dt); | |
2646 | sregs->idt.limit = dt.limit; | |
2647 | sregs->idt.base = dt.base; | |
2648 | kvm_x86_ops->get_gdt(vcpu, &dt); | |
2649 | sregs->gdt.limit = dt.limit; | |
2650 | sregs->gdt.base = dt.base; | |
2651 | ||
2652 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
2653 | sregs->cr0 = vcpu->cr0; | |
2654 | sregs->cr2 = vcpu->cr2; | |
2655 | sregs->cr3 = vcpu->cr3; | |
2656 | sregs->cr4 = vcpu->cr4; | |
2657 | sregs->cr8 = get_cr8(vcpu); | |
2658 | sregs->efer = vcpu->shadow_efer; | |
2659 | sregs->apic_base = kvm_get_apic_base(vcpu); | |
2660 | ||
2661 | if (irqchip_in_kernel(vcpu->kvm)) { | |
2662 | memset(sregs->interrupt_bitmap, 0, | |
2663 | sizeof sregs->interrupt_bitmap); | |
2664 | pending_vec = kvm_x86_ops->get_irq(vcpu); | |
2665 | if (pending_vec >= 0) | |
2666 | set_bit(pending_vec, | |
2667 | (unsigned long *)sregs->interrupt_bitmap); | |
2668 | } else | |
2669 | memcpy(sregs->interrupt_bitmap, vcpu->irq_pending, | |
2670 | sizeof sregs->interrupt_bitmap); | |
2671 | ||
2672 | vcpu_put(vcpu); | |
2673 | ||
2674 | return 0; | |
2675 | } | |
2676 | ||
2677 | static void set_segment(struct kvm_vcpu *vcpu, | |
2678 | struct kvm_segment *var, int seg) | |
2679 | { | |
2680 | return kvm_x86_ops->set_segment(vcpu, var, seg); | |
2681 | } | |
2682 | ||
2683 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
2684 | struct kvm_sregs *sregs) | |
2685 | { | |
2686 | int mmu_reset_needed = 0; | |
2687 | int i, pending_vec, max_bits; | |
2688 | struct descriptor_table dt; | |
2689 | ||
2690 | vcpu_load(vcpu); | |
2691 | ||
2692 | dt.limit = sregs->idt.limit; | |
2693 | dt.base = sregs->idt.base; | |
2694 | kvm_x86_ops->set_idt(vcpu, &dt); | |
2695 | dt.limit = sregs->gdt.limit; | |
2696 | dt.base = sregs->gdt.base; | |
2697 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
2698 | ||
2699 | vcpu->cr2 = sregs->cr2; | |
2700 | mmu_reset_needed |= vcpu->cr3 != sregs->cr3; | |
2701 | vcpu->cr3 = sregs->cr3; | |
2702 | ||
2703 | set_cr8(vcpu, sregs->cr8); | |
2704 | ||
2705 | mmu_reset_needed |= vcpu->shadow_efer != sregs->efer; | |
2706 | #ifdef CONFIG_X86_64 | |
2707 | kvm_x86_ops->set_efer(vcpu, sregs->efer); | |
2708 | #endif | |
2709 | kvm_set_apic_base(vcpu, sregs->apic_base); | |
2710 | ||
2711 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
2712 | ||
2713 | mmu_reset_needed |= vcpu->cr0 != sregs->cr0; | |
2714 | vcpu->cr0 = sregs->cr0; | |
2715 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); | |
2716 | ||
2717 | mmu_reset_needed |= vcpu->cr4 != sregs->cr4; | |
2718 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); | |
2719 | if (!is_long_mode(vcpu) && is_pae(vcpu)) | |
2720 | load_pdptrs(vcpu, vcpu->cr3); | |
2721 | ||
2722 | if (mmu_reset_needed) | |
2723 | kvm_mmu_reset_context(vcpu); | |
2724 | ||
2725 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
2726 | memcpy(vcpu->irq_pending, sregs->interrupt_bitmap, | |
2727 | sizeof vcpu->irq_pending); | |
2728 | vcpu->irq_summary = 0; | |
2729 | for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i) | |
2730 | if (vcpu->irq_pending[i]) | |
2731 | __set_bit(i, &vcpu->irq_summary); | |
2732 | } else { | |
2733 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
2734 | pending_vec = find_first_bit( | |
2735 | (const unsigned long *)sregs->interrupt_bitmap, | |
2736 | max_bits); | |
2737 | /* Only pending external irq is handled here */ | |
2738 | if (pending_vec < max_bits) { | |
2739 | kvm_x86_ops->set_irq(vcpu, pending_vec); | |
2740 | pr_debug("Set back pending irq %d\n", | |
2741 | pending_vec); | |
2742 | } | |
2743 | } | |
2744 | ||
2745 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
2746 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
2747 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
2748 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
2749 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
2750 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
2751 | ||
2752 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
2753 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
2754 | ||
2755 | vcpu_put(vcpu); | |
2756 | ||
2757 | return 0; | |
2758 | } | |
2759 | ||
2760 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, | |
2761 | struct kvm_debug_guest *dbg) | |
2762 | { | |
2763 | int r; | |
2764 | ||
2765 | vcpu_load(vcpu); | |
2766 | ||
2767 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); | |
2768 | ||
2769 | vcpu_put(vcpu); | |
2770 | ||
2771 | return r; | |
2772 | } | |
2773 | ||
d0752060 HB |
2774 | /* |
2775 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
2776 | * we have asm/x86/processor.h | |
2777 | */ | |
2778 | struct fxsave { | |
2779 | u16 cwd; | |
2780 | u16 swd; | |
2781 | u16 twd; | |
2782 | u16 fop; | |
2783 | u64 rip; | |
2784 | u64 rdp; | |
2785 | u32 mxcsr; | |
2786 | u32 mxcsr_mask; | |
2787 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
2788 | #ifdef CONFIG_X86_64 | |
2789 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
2790 | #else | |
2791 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
2792 | #endif | |
2793 | }; | |
2794 | ||
8b006791 ZX |
2795 | /* |
2796 | * Translate a guest virtual address to a guest physical address. | |
2797 | */ | |
2798 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
2799 | struct kvm_translation *tr) | |
2800 | { | |
2801 | unsigned long vaddr = tr->linear_address; | |
2802 | gpa_t gpa; | |
2803 | ||
2804 | vcpu_load(vcpu); | |
2805 | mutex_lock(&vcpu->kvm->lock); | |
2806 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); | |
2807 | tr->physical_address = gpa; | |
2808 | tr->valid = gpa != UNMAPPED_GVA; | |
2809 | tr->writeable = 1; | |
2810 | tr->usermode = 0; | |
2811 | mutex_unlock(&vcpu->kvm->lock); | |
2812 | vcpu_put(vcpu); | |
2813 | ||
2814 | return 0; | |
2815 | } | |
2816 | ||
d0752060 HB |
2817 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
2818 | { | |
2819 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; | |
2820 | ||
2821 | vcpu_load(vcpu); | |
2822 | ||
2823 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
2824 | fpu->fcw = fxsave->cwd; | |
2825 | fpu->fsw = fxsave->swd; | |
2826 | fpu->ftwx = fxsave->twd; | |
2827 | fpu->last_opcode = fxsave->fop; | |
2828 | fpu->last_ip = fxsave->rip; | |
2829 | fpu->last_dp = fxsave->rdp; | |
2830 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
2831 | ||
2832 | vcpu_put(vcpu); | |
2833 | ||
2834 | return 0; | |
2835 | } | |
2836 | ||
2837 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
2838 | { | |
2839 | struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image; | |
2840 | ||
2841 | vcpu_load(vcpu); | |
2842 | ||
2843 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
2844 | fxsave->cwd = fpu->fcw; | |
2845 | fxsave->swd = fpu->fsw; | |
2846 | fxsave->twd = fpu->ftwx; | |
2847 | fxsave->fop = fpu->last_opcode; | |
2848 | fxsave->rip = fpu->last_ip; | |
2849 | fxsave->rdp = fpu->last_dp; | |
2850 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
2851 | ||
2852 | vcpu_put(vcpu); | |
2853 | ||
2854 | return 0; | |
2855 | } | |
2856 | ||
2857 | void fx_init(struct kvm_vcpu *vcpu) | |
2858 | { | |
2859 | unsigned after_mxcsr_mask; | |
2860 | ||
2861 | /* Initialize guest FPU by resetting ours and saving into guest's */ | |
2862 | preempt_disable(); | |
2863 | fx_save(&vcpu->host_fx_image); | |
2864 | fpu_init(); | |
2865 | fx_save(&vcpu->guest_fx_image); | |
2866 | fx_restore(&vcpu->host_fx_image); | |
2867 | preempt_enable(); | |
2868 | ||
2869 | vcpu->cr0 |= X86_CR0_ET; | |
2870 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); | |
2871 | vcpu->guest_fx_image.mxcsr = 0x1f80; | |
2872 | memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask, | |
2873 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); | |
2874 | } | |
2875 | EXPORT_SYMBOL_GPL(fx_init); | |
2876 | ||
2877 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
2878 | { | |
2879 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
2880 | return; | |
2881 | ||
2882 | vcpu->guest_fpu_loaded = 1; | |
2883 | fx_save(&vcpu->host_fx_image); | |
2884 | fx_restore(&vcpu->guest_fx_image); | |
2885 | } | |
2886 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
2887 | ||
2888 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
2889 | { | |
2890 | if (!vcpu->guest_fpu_loaded) | |
2891 | return; | |
2892 | ||
2893 | vcpu->guest_fpu_loaded = 0; | |
2894 | fx_save(&vcpu->guest_fx_image); | |
2895 | fx_restore(&vcpu->host_fx_image); | |
f096ed85 | 2896 | ++vcpu->stat.fpu_reload; |
d0752060 HB |
2897 | } |
2898 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
e9b11c17 ZX |
2899 | |
2900 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
2901 | { | |
2902 | kvm_x86_ops->vcpu_free(vcpu); | |
2903 | } | |
2904 | ||
2905 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, | |
2906 | unsigned int id) | |
2907 | { | |
26e5215f AK |
2908 | return kvm_x86_ops->vcpu_create(kvm, id); |
2909 | } | |
e9b11c17 | 2910 | |
26e5215f AK |
2911 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
2912 | { | |
2913 | int r; | |
e9b11c17 ZX |
2914 | |
2915 | /* We do fxsave: this must be aligned. */ | |
2916 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
2917 | ||
2918 | vcpu_load(vcpu); | |
2919 | r = kvm_arch_vcpu_reset(vcpu); | |
2920 | if (r == 0) | |
2921 | r = kvm_mmu_setup(vcpu); | |
2922 | vcpu_put(vcpu); | |
2923 | if (r < 0) | |
2924 | goto free_vcpu; | |
2925 | ||
26e5215f | 2926 | return 0; |
e9b11c17 ZX |
2927 | free_vcpu: |
2928 | kvm_x86_ops->vcpu_free(vcpu); | |
26e5215f | 2929 | return r; |
e9b11c17 ZX |
2930 | } |
2931 | ||
d40ccc62 | 2932 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 ZX |
2933 | { |
2934 | vcpu_load(vcpu); | |
2935 | kvm_mmu_unload(vcpu); | |
2936 | vcpu_put(vcpu); | |
2937 | ||
2938 | kvm_x86_ops->vcpu_free(vcpu); | |
2939 | } | |
2940 | ||
2941 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) | |
2942 | { | |
2943 | return kvm_x86_ops->vcpu_reset(vcpu); | |
2944 | } | |
2945 | ||
2946 | void kvm_arch_hardware_enable(void *garbage) | |
2947 | { | |
2948 | kvm_x86_ops->hardware_enable(garbage); | |
2949 | } | |
2950 | ||
2951 | void kvm_arch_hardware_disable(void *garbage) | |
2952 | { | |
2953 | kvm_x86_ops->hardware_disable(garbage); | |
2954 | } | |
2955 | ||
2956 | int kvm_arch_hardware_setup(void) | |
2957 | { | |
2958 | return kvm_x86_ops->hardware_setup(); | |
2959 | } | |
2960 | ||
2961 | void kvm_arch_hardware_unsetup(void) | |
2962 | { | |
2963 | kvm_x86_ops->hardware_unsetup(); | |
2964 | } | |
2965 | ||
2966 | void kvm_arch_check_processor_compat(void *rtn) | |
2967 | { | |
2968 | kvm_x86_ops->check_processor_compatibility(rtn); | |
2969 | } | |
2970 | ||
2971 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) | |
2972 | { | |
2973 | struct page *page; | |
2974 | struct kvm *kvm; | |
2975 | int r; | |
2976 | ||
2977 | BUG_ON(vcpu->kvm == NULL); | |
2978 | kvm = vcpu->kvm; | |
2979 | ||
2980 | vcpu->mmu.root_hpa = INVALID_PAGE; | |
2981 | if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) | |
2982 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; | |
2983 | else | |
2984 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; | |
2985 | ||
2986 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
2987 | if (!page) { | |
2988 | r = -ENOMEM; | |
2989 | goto fail; | |
2990 | } | |
2991 | vcpu->pio_data = page_address(page); | |
2992 | ||
2993 | r = kvm_mmu_create(vcpu); | |
2994 | if (r < 0) | |
2995 | goto fail_free_pio_data; | |
2996 | ||
2997 | if (irqchip_in_kernel(kvm)) { | |
2998 | r = kvm_create_lapic(vcpu); | |
2999 | if (r < 0) | |
3000 | goto fail_mmu_destroy; | |
3001 | } | |
3002 | ||
3003 | return 0; | |
3004 | ||
3005 | fail_mmu_destroy: | |
3006 | kvm_mmu_destroy(vcpu); | |
3007 | fail_free_pio_data: | |
3008 | free_page((unsigned long)vcpu->pio_data); | |
3009 | fail: | |
3010 | return r; | |
3011 | } | |
3012 | ||
3013 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
3014 | { | |
3015 | kvm_free_lapic(vcpu); | |
3016 | kvm_mmu_destroy(vcpu); | |
3017 | free_page((unsigned long)vcpu->pio_data); | |
3018 | } | |
d19a9cd2 ZX |
3019 | |
3020 | struct kvm *kvm_arch_create_vm(void) | |
3021 | { | |
3022 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
3023 | ||
3024 | if (!kvm) | |
3025 | return ERR_PTR(-ENOMEM); | |
3026 | ||
3027 | INIT_LIST_HEAD(&kvm->active_mmu_pages); | |
3028 | ||
3029 | return kvm; | |
3030 | } | |
3031 | ||
3032 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) | |
3033 | { | |
3034 | vcpu_load(vcpu); | |
3035 | kvm_mmu_unload(vcpu); | |
3036 | vcpu_put(vcpu); | |
3037 | } | |
3038 | ||
3039 | static void kvm_free_vcpus(struct kvm *kvm) | |
3040 | { | |
3041 | unsigned int i; | |
3042 | ||
3043 | /* | |
3044 | * Unpin any mmu pages first. | |
3045 | */ | |
3046 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
3047 | if (kvm->vcpus[i]) | |
3048 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
3049 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
3050 | if (kvm->vcpus[i]) { | |
3051 | kvm_arch_vcpu_free(kvm->vcpus[i]); | |
3052 | kvm->vcpus[i] = NULL; | |
3053 | } | |
3054 | } | |
3055 | ||
3056 | } | |
3057 | ||
3058 | void kvm_arch_destroy_vm(struct kvm *kvm) | |
3059 | { | |
3060 | kfree(kvm->vpic); | |
3061 | kfree(kvm->vioapic); | |
3062 | kvm_free_vcpus(kvm); | |
3063 | kvm_free_physmem(kvm); | |
3064 | kfree(kvm); | |
3065 | } | |
0de10343 ZX |
3066 | |
3067 | int kvm_arch_set_memory_region(struct kvm *kvm, | |
3068 | struct kvm_userspace_memory_region *mem, | |
3069 | struct kvm_memory_slot old, | |
3070 | int user_alloc) | |
3071 | { | |
3072 | int npages = mem->memory_size >> PAGE_SHIFT; | |
3073 | struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot]; | |
3074 | ||
3075 | /*To keep backward compatibility with older userspace, | |
3076 | *x86 needs to hanlde !user_alloc case. | |
3077 | */ | |
3078 | if (!user_alloc) { | |
3079 | if (npages && !old.rmap) { | |
3080 | down_write(¤t->mm->mmap_sem); | |
3081 | memslot->userspace_addr = do_mmap(NULL, 0, | |
3082 | npages * PAGE_SIZE, | |
3083 | PROT_READ | PROT_WRITE, | |
3084 | MAP_SHARED | MAP_ANONYMOUS, | |
3085 | 0); | |
3086 | up_write(¤t->mm->mmap_sem); | |
3087 | ||
3088 | if (IS_ERR((void *)memslot->userspace_addr)) | |
3089 | return PTR_ERR((void *)memslot->userspace_addr); | |
3090 | } else { | |
3091 | if (!old.user_alloc && old.rmap) { | |
3092 | int ret; | |
3093 | ||
3094 | down_write(¤t->mm->mmap_sem); | |
3095 | ret = do_munmap(current->mm, old.userspace_addr, | |
3096 | old.npages * PAGE_SIZE); | |
3097 | up_write(¤t->mm->mmap_sem); | |
3098 | if (ret < 0) | |
3099 | printk(KERN_WARNING | |
3100 | "kvm_vm_ioctl_set_memory_region: " | |
3101 | "failed to munmap memory\n"); | |
3102 | } | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | if (!kvm->n_requested_mmu_pages) { | |
3107 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); | |
3108 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
3109 | } | |
3110 | ||
3111 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); | |
3112 | kvm_flush_remote_tlbs(kvm); | |
3113 | ||
3114 | return 0; | |
3115 | } |