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