]>
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 <[email protected]> | |
10 | * Yaniv Kamay <[email protected]> | |
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 | ||
edf88417 | 17 | #include <linux/kvm_host.h> |
313a3dc7 | 18 | #include "irq.h" |
1d737c8a | 19 | #include "mmu.h" |
7837699f | 20 | #include "i8254.h" |
37817f29 | 21 | #include "tss.h" |
313a3dc7 | 22 | |
18068523 | 23 | #include <linux/clocksource.h> |
313a3dc7 CO |
24 | #include <linux/kvm.h> |
25 | #include <linux/fs.h> | |
26 | #include <linux/vmalloc.h> | |
5fb76f9b | 27 | #include <linux/module.h> |
0de10343 | 28 | #include <linux/mman.h> |
2bacc55c | 29 | #include <linux/highmem.h> |
043405e1 CO |
30 | |
31 | #include <asm/uaccess.h> | |
d825ed0a | 32 | #include <asm/msr.h> |
a5f61300 | 33 | #include <asm/desc.h> |
043405e1 | 34 | |
313a3dc7 | 35 | #define MAX_IO_MSRS 256 |
a03490ed CO |
36 | #define CR0_RESERVED_BITS \ |
37 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ | |
38 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ | |
39 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) | |
40 | #define CR4_RESERVED_BITS \ | |
41 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ | |
42 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ | |
43 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
44 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) | |
45 | ||
46 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) | |
50a37eb4 JR |
47 | /* EFER defaults: |
48 | * - enable syscall per default because its emulated by KVM | |
49 | * - enable LME and LMA per default on 64 bit KVM | |
50 | */ | |
51 | #ifdef CONFIG_X86_64 | |
52 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; | |
53 | #else | |
54 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; | |
55 | #endif | |
313a3dc7 | 56 | |
ba1389b7 AK |
57 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
58 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU | |
417bc304 | 59 | |
674eea0f AK |
60 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
61 | struct kvm_cpuid_entry2 __user *entries); | |
62 | ||
97896d04 ZX |
63 | struct kvm_x86_ops *kvm_x86_ops; |
64 | ||
417bc304 | 65 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
ba1389b7 AK |
66 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
67 | { "pf_guest", VCPU_STAT(pf_guest) }, | |
68 | { "tlb_flush", VCPU_STAT(tlb_flush) }, | |
69 | { "invlpg", VCPU_STAT(invlpg) }, | |
70 | { "exits", VCPU_STAT(exits) }, | |
71 | { "io_exits", VCPU_STAT(io_exits) }, | |
72 | { "mmio_exits", VCPU_STAT(mmio_exits) }, | |
73 | { "signal_exits", VCPU_STAT(signal_exits) }, | |
74 | { "irq_window", VCPU_STAT(irq_window_exits) }, | |
f08864b4 | 75 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
ba1389b7 AK |
76 | { "halt_exits", VCPU_STAT(halt_exits) }, |
77 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, | |
f11c3a8d | 78 | { "hypercalls", VCPU_STAT(hypercalls) }, |
ba1389b7 AK |
79 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
80 | { "irq_exits", VCPU_STAT(irq_exits) }, | |
81 | { "host_state_reload", VCPU_STAT(host_state_reload) }, | |
82 | { "efer_reload", VCPU_STAT(efer_reload) }, | |
83 | { "fpu_reload", VCPU_STAT(fpu_reload) }, | |
84 | { "insn_emulation", VCPU_STAT(insn_emulation) }, | |
85 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, | |
4cee5764 AK |
86 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
87 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, | |
88 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, | |
89 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, | |
90 | { "mmu_flooded", VM_STAT(mmu_flooded) }, | |
91 | { "mmu_recycled", VM_STAT(mmu_recycled) }, | |
dfc5aa00 | 92 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
0f74a24c | 93 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
05da4558 | 94 | { "largepages", VM_STAT(lpages) }, |
417bc304 HB |
95 | { NULL } |
96 | }; | |
97 | ||
98 | ||
5fb76f9b CO |
99 | unsigned long segment_base(u16 selector) |
100 | { | |
101 | struct descriptor_table gdt; | |
a5f61300 | 102 | struct desc_struct *d; |
5fb76f9b CO |
103 | unsigned long table_base; |
104 | unsigned long v; | |
105 | ||
106 | if (selector == 0) | |
107 | return 0; | |
108 | ||
109 | asm("sgdt %0" : "=m"(gdt)); | |
110 | table_base = gdt.base; | |
111 | ||
112 | if (selector & 4) { /* from ldt */ | |
113 | u16 ldt_selector; | |
114 | ||
115 | asm("sldt %0" : "=g"(ldt_selector)); | |
116 | table_base = segment_base(ldt_selector); | |
117 | } | |
a5f61300 AK |
118 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
119 | v = d->base0 | ((unsigned long)d->base1 << 16) | | |
120 | ((unsigned long)d->base2 << 24); | |
5fb76f9b | 121 | #ifdef CONFIG_X86_64 |
a5f61300 AK |
122 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
123 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; | |
5fb76f9b CO |
124 | #endif |
125 | return v; | |
126 | } | |
127 | EXPORT_SYMBOL_GPL(segment_base); | |
128 | ||
6866b83e CO |
129 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
130 | { | |
131 | if (irqchip_in_kernel(vcpu->kvm)) | |
ad312c7c | 132 | return vcpu->arch.apic_base; |
6866b83e | 133 | else |
ad312c7c | 134 | return vcpu->arch.apic_base; |
6866b83e CO |
135 | } |
136 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
137 | ||
138 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) | |
139 | { | |
140 | /* TODO: reserve bits check */ | |
141 | if (irqchip_in_kernel(vcpu->kvm)) | |
142 | kvm_lapic_set_base(vcpu, data); | |
143 | else | |
ad312c7c | 144 | vcpu->arch.apic_base = data; |
6866b83e CO |
145 | } |
146 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
147 | ||
298101da AK |
148 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
149 | { | |
ad312c7c ZX |
150 | WARN_ON(vcpu->arch.exception.pending); |
151 | vcpu->arch.exception.pending = true; | |
152 | vcpu->arch.exception.has_error_code = false; | |
153 | vcpu->arch.exception.nr = nr; | |
298101da AK |
154 | } |
155 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
156 | ||
c3c91fee AK |
157 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
158 | u32 error_code) | |
159 | { | |
160 | ++vcpu->stat.pf_guest; | |
71c4dfaf JR |
161 | if (vcpu->arch.exception.pending) { |
162 | if (vcpu->arch.exception.nr == PF_VECTOR) { | |
163 | printk(KERN_DEBUG "kvm: inject_page_fault:" | |
164 | " double fault 0x%lx\n", addr); | |
165 | vcpu->arch.exception.nr = DF_VECTOR; | |
166 | vcpu->arch.exception.error_code = 0; | |
167 | } else if (vcpu->arch.exception.nr == DF_VECTOR) { | |
168 | /* triple fault -> shutdown */ | |
169 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); | |
170 | } | |
c3c91fee AK |
171 | return; |
172 | } | |
ad312c7c | 173 | vcpu->arch.cr2 = addr; |
c3c91fee AK |
174 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
175 | } | |
176 | ||
3419ffc8 SY |
177 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
178 | { | |
179 | vcpu->arch.nmi_pending = 1; | |
180 | } | |
181 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
182 | ||
298101da AK |
183 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
184 | { | |
ad312c7c ZX |
185 | WARN_ON(vcpu->arch.exception.pending); |
186 | vcpu->arch.exception.pending = true; | |
187 | vcpu->arch.exception.has_error_code = true; | |
188 | vcpu->arch.exception.nr = nr; | |
189 | vcpu->arch.exception.error_code = error_code; | |
298101da AK |
190 | } |
191 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
192 | ||
193 | static void __queue_exception(struct kvm_vcpu *vcpu) | |
194 | { | |
ad312c7c ZX |
195 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
196 | vcpu->arch.exception.has_error_code, | |
197 | vcpu->arch.exception.error_code); | |
298101da AK |
198 | } |
199 | ||
a03490ed CO |
200 | /* |
201 | * Load the pae pdptrs. Return true is they are all valid. | |
202 | */ | |
203 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
204 | { | |
205 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
206 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
207 | int i; | |
208 | int ret; | |
ad312c7c | 209 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
a03490ed | 210 | |
a03490ed CO |
211 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
212 | offset * sizeof(u64), sizeof(pdpte)); | |
213 | if (ret < 0) { | |
214 | ret = 0; | |
215 | goto out; | |
216 | } | |
217 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
218 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { | |
219 | ret = 0; | |
220 | goto out; | |
221 | } | |
222 | } | |
223 | ret = 1; | |
224 | ||
ad312c7c | 225 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
a03490ed | 226 | out: |
a03490ed CO |
227 | |
228 | return ret; | |
229 | } | |
cc4b6871 | 230 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 231 | |
d835dfec AK |
232 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
233 | { | |
ad312c7c | 234 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
d835dfec AK |
235 | bool changed = true; |
236 | int r; | |
237 | ||
238 | if (is_long_mode(vcpu) || !is_pae(vcpu)) | |
239 | return false; | |
240 | ||
ad312c7c | 241 | r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte)); |
d835dfec AK |
242 | if (r < 0) |
243 | goto out; | |
ad312c7c | 244 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
d835dfec | 245 | out: |
d835dfec AK |
246 | |
247 | return changed; | |
248 | } | |
249 | ||
2d3ad1f4 | 250 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed CO |
251 | { |
252 | if (cr0 & CR0_RESERVED_BITS) { | |
253 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", | |
ad312c7c | 254 | cr0, vcpu->arch.cr0); |
c1a5d4f9 | 255 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
256 | return; |
257 | } | |
258 | ||
259 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { | |
260 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); | |
c1a5d4f9 | 261 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
262 | return; |
263 | } | |
264 | ||
265 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { | |
266 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " | |
267 | "and a clear PE flag\n"); | |
c1a5d4f9 | 268 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
269 | return; |
270 | } | |
271 | ||
272 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
273 | #ifdef CONFIG_X86_64 | |
ad312c7c | 274 | if ((vcpu->arch.shadow_efer & EFER_LME)) { |
a03490ed CO |
275 | int cs_db, cs_l; |
276 | ||
277 | if (!is_pae(vcpu)) { | |
278 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
279 | "in long mode while PAE is disabled\n"); | |
c1a5d4f9 | 280 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
281 | return; |
282 | } | |
283 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
284 | if (cs_l) { | |
285 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
286 | "in long mode while CS.L == 1\n"); | |
c1a5d4f9 | 287 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
288 | return; |
289 | ||
290 | } | |
291 | } else | |
292 | #endif | |
ad312c7c | 293 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
a03490ed CO |
294 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
295 | "reserved bits\n"); | |
c1a5d4f9 | 296 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
297 | return; |
298 | } | |
299 | ||
300 | } | |
301 | ||
302 | kvm_x86_ops->set_cr0(vcpu, cr0); | |
ad312c7c | 303 | vcpu->arch.cr0 = cr0; |
a03490ed | 304 | |
a03490ed | 305 | kvm_mmu_reset_context(vcpu); |
a03490ed CO |
306 | return; |
307 | } | |
2d3ad1f4 | 308 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 309 | |
2d3ad1f4 | 310 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 311 | { |
2d3ad1f4 | 312 | kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)); |
2714d1d3 FEL |
313 | KVMTRACE_1D(LMSW, vcpu, |
314 | (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)), | |
315 | handler); | |
a03490ed | 316 | } |
2d3ad1f4 | 317 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 318 | |
2d3ad1f4 | 319 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed CO |
320 | { |
321 | if (cr4 & CR4_RESERVED_BITS) { | |
322 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); | |
c1a5d4f9 | 323 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
324 | return; |
325 | } | |
326 | ||
327 | if (is_long_mode(vcpu)) { | |
328 | if (!(cr4 & X86_CR4_PAE)) { | |
329 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " | |
330 | "in long mode\n"); | |
c1a5d4f9 | 331 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
332 | return; |
333 | } | |
334 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) | |
ad312c7c | 335 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
a03490ed | 336 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
c1a5d4f9 | 337 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
338 | return; |
339 | } | |
340 | ||
341 | if (cr4 & X86_CR4_VMXE) { | |
342 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); | |
c1a5d4f9 | 343 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
344 | return; |
345 | } | |
346 | kvm_x86_ops->set_cr4(vcpu, cr4); | |
ad312c7c | 347 | vcpu->arch.cr4 = cr4; |
a03490ed | 348 | kvm_mmu_reset_context(vcpu); |
a03490ed | 349 | } |
2d3ad1f4 | 350 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 351 | |
2d3ad1f4 | 352 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 353 | { |
ad312c7c | 354 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
d835dfec AK |
355 | kvm_mmu_flush_tlb(vcpu); |
356 | return; | |
357 | } | |
358 | ||
a03490ed CO |
359 | if (is_long_mode(vcpu)) { |
360 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { | |
361 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 362 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
363 | return; |
364 | } | |
365 | } else { | |
366 | if (is_pae(vcpu)) { | |
367 | if (cr3 & CR3_PAE_RESERVED_BITS) { | |
368 | printk(KERN_DEBUG | |
369 | "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 370 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
371 | return; |
372 | } | |
373 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { | |
374 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
375 | "reserved bits\n"); | |
c1a5d4f9 | 376 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
377 | return; |
378 | } | |
379 | } | |
380 | /* | |
381 | * We don't check reserved bits in nonpae mode, because | |
382 | * this isn't enforced, and VMware depends on this. | |
383 | */ | |
384 | } | |
385 | ||
a03490ed CO |
386 | /* |
387 | * Does the new cr3 value map to physical memory? (Note, we | |
388 | * catch an invalid cr3 even in real-mode, because it would | |
389 | * cause trouble later on when we turn on paging anyway.) | |
390 | * | |
391 | * A real CPU would silently accept an invalid cr3 and would | |
392 | * attempt to use it - with largely undefined (and often hard | |
393 | * to debug) behavior on the guest side. | |
394 | */ | |
395 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) | |
c1a5d4f9 | 396 | kvm_inject_gp(vcpu, 0); |
a03490ed | 397 | else { |
ad312c7c ZX |
398 | vcpu->arch.cr3 = cr3; |
399 | vcpu->arch.mmu.new_cr3(vcpu); | |
a03490ed | 400 | } |
a03490ed | 401 | } |
2d3ad1f4 | 402 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 403 | |
2d3ad1f4 | 404 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed CO |
405 | { |
406 | if (cr8 & CR8_RESERVED_BITS) { | |
407 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); | |
c1a5d4f9 | 408 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
409 | return; |
410 | } | |
411 | if (irqchip_in_kernel(vcpu->kvm)) | |
412 | kvm_lapic_set_tpr(vcpu, cr8); | |
413 | else | |
ad312c7c | 414 | vcpu->arch.cr8 = cr8; |
a03490ed | 415 | } |
2d3ad1f4 | 416 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 417 | |
2d3ad1f4 | 418 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed CO |
419 | { |
420 | if (irqchip_in_kernel(vcpu->kvm)) | |
421 | return kvm_lapic_get_cr8(vcpu); | |
422 | else | |
ad312c7c | 423 | return vcpu->arch.cr8; |
a03490ed | 424 | } |
2d3ad1f4 | 425 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 426 | |
043405e1 CO |
427 | /* |
428 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
429 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
430 | * | |
431 | * This list is modified at module load time to reflect the | |
432 | * capabilities of the host cpu. | |
433 | */ | |
434 | static u32 msrs_to_save[] = { | |
435 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
436 | MSR_K6_STAR, | |
437 | #ifdef CONFIG_X86_64 | |
438 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
439 | #endif | |
18068523 | 440 | MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
847f0ad8 | 441 | MSR_IA32_PERF_STATUS, |
043405e1 CO |
442 | }; |
443 | ||
444 | static unsigned num_msrs_to_save; | |
445 | ||
446 | static u32 emulated_msrs[] = { | |
447 | MSR_IA32_MISC_ENABLE, | |
448 | }; | |
449 | ||
15c4a640 CO |
450 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
451 | { | |
f2b4b7dd | 452 | if (efer & efer_reserved_bits) { |
15c4a640 CO |
453 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
454 | efer); | |
c1a5d4f9 | 455 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
456 | return; |
457 | } | |
458 | ||
459 | if (is_paging(vcpu) | |
ad312c7c | 460 | && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
15c4a640 | 461 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
c1a5d4f9 | 462 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
463 | return; |
464 | } | |
465 | ||
466 | kvm_x86_ops->set_efer(vcpu, efer); | |
467 | ||
468 | efer &= ~EFER_LMA; | |
ad312c7c | 469 | efer |= vcpu->arch.shadow_efer & EFER_LMA; |
15c4a640 | 470 | |
ad312c7c | 471 | vcpu->arch.shadow_efer = efer; |
15c4a640 CO |
472 | } |
473 | ||
f2b4b7dd JR |
474 | void kvm_enable_efer_bits(u64 mask) |
475 | { | |
476 | efer_reserved_bits &= ~mask; | |
477 | } | |
478 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
479 | ||
480 | ||
15c4a640 CO |
481 | /* |
482 | * Writes msr value into into the appropriate "register". | |
483 | * Returns 0 on success, non-0 otherwise. | |
484 | * Assumes vcpu_load() was already called. | |
485 | */ | |
486 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
487 | { | |
488 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); | |
489 | } | |
490 | ||
313a3dc7 CO |
491 | /* |
492 | * Adapt set_msr() to msr_io()'s calling convention | |
493 | */ | |
494 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
495 | { | |
496 | return kvm_set_msr(vcpu, index, *data); | |
497 | } | |
498 | ||
18068523 GOC |
499 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
500 | { | |
501 | static int version; | |
50d0a0f9 GH |
502 | struct pvclock_wall_clock wc; |
503 | struct timespec now, sys, boot; | |
18068523 GOC |
504 | |
505 | if (!wall_clock) | |
506 | return; | |
507 | ||
508 | version++; | |
509 | ||
18068523 GOC |
510 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
511 | ||
50d0a0f9 GH |
512 | /* |
513 | * The guest calculates current wall clock time by adding | |
514 | * system time (updated by kvm_write_guest_time below) to the | |
515 | * wall clock specified here. guest system time equals host | |
516 | * system time for us, thus we must fill in host boot time here. | |
517 | */ | |
518 | now = current_kernel_time(); | |
519 | ktime_get_ts(&sys); | |
520 | boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys)); | |
521 | ||
522 | wc.sec = boot.tv_sec; | |
523 | wc.nsec = boot.tv_nsec; | |
524 | wc.version = version; | |
18068523 GOC |
525 | |
526 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
527 | ||
528 | version++; | |
529 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
530 | } |
531 | ||
50d0a0f9 GH |
532 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
533 | { | |
534 | uint32_t quotient, remainder; | |
535 | ||
536 | /* Don't try to replace with do_div(), this one calculates | |
537 | * "(dividend << 32) / divisor" */ | |
538 | __asm__ ( "divl %4" | |
539 | : "=a" (quotient), "=d" (remainder) | |
540 | : "0" (0), "1" (dividend), "r" (divisor) ); | |
541 | return quotient; | |
542 | } | |
543 | ||
544 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) | |
545 | { | |
546 | uint64_t nsecs = 1000000000LL; | |
547 | int32_t shift = 0; | |
548 | uint64_t tps64; | |
549 | uint32_t tps32; | |
550 | ||
551 | tps64 = tsc_khz * 1000LL; | |
552 | while (tps64 > nsecs*2) { | |
553 | tps64 >>= 1; | |
554 | shift--; | |
555 | } | |
556 | ||
557 | tps32 = (uint32_t)tps64; | |
558 | while (tps32 <= (uint32_t)nsecs) { | |
559 | tps32 <<= 1; | |
560 | shift++; | |
561 | } | |
562 | ||
563 | hv_clock->tsc_shift = shift; | |
564 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); | |
565 | ||
566 | pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n", | |
567 | __FUNCTION__, tsc_khz, hv_clock->tsc_shift, | |
568 | hv_clock->tsc_to_system_mul); | |
569 | } | |
570 | ||
18068523 GOC |
571 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
572 | { | |
573 | struct timespec ts; | |
574 | unsigned long flags; | |
575 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
576 | void *shared_kaddr; | |
577 | ||
578 | if ((!vcpu->time_page)) | |
579 | return; | |
580 | ||
50d0a0f9 GH |
581 | if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) { |
582 | kvm_set_time_scale(tsc_khz, &vcpu->hv_clock); | |
583 | vcpu->hv_clock_tsc_khz = tsc_khz; | |
584 | } | |
585 | ||
18068523 GOC |
586 | /* Keep irq disabled to prevent changes to the clock */ |
587 | local_irq_save(flags); | |
588 | kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER, | |
589 | &vcpu->hv_clock.tsc_timestamp); | |
590 | ktime_get_ts(&ts); | |
591 | local_irq_restore(flags); | |
592 | ||
593 | /* With all the info we got, fill in the values */ | |
594 | ||
595 | vcpu->hv_clock.system_time = ts.tv_nsec + | |
596 | (NSEC_PER_SEC * (u64)ts.tv_sec); | |
597 | /* | |
598 | * The interface expects us to write an even number signaling that the | |
599 | * update is finished. Since the guest won't see the intermediate | |
50d0a0f9 | 600 | * state, we just increase by 2 at the end. |
18068523 | 601 | */ |
50d0a0f9 | 602 | vcpu->hv_clock.version += 2; |
18068523 GOC |
603 | |
604 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); | |
605 | ||
606 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, | |
50d0a0f9 | 607 | sizeof(vcpu->hv_clock)); |
18068523 GOC |
608 | |
609 | kunmap_atomic(shared_kaddr, KM_USER0); | |
610 | ||
611 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); | |
612 | } | |
613 | ||
9ba075a6 AK |
614 | static bool msr_mtrr_valid(unsigned msr) |
615 | { | |
616 | switch (msr) { | |
617 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: | |
618 | case MSR_MTRRfix64K_00000: | |
619 | case MSR_MTRRfix16K_80000: | |
620 | case MSR_MTRRfix16K_A0000: | |
621 | case MSR_MTRRfix4K_C0000: | |
622 | case MSR_MTRRfix4K_C8000: | |
623 | case MSR_MTRRfix4K_D0000: | |
624 | case MSR_MTRRfix4K_D8000: | |
625 | case MSR_MTRRfix4K_E0000: | |
626 | case MSR_MTRRfix4K_E8000: | |
627 | case MSR_MTRRfix4K_F0000: | |
628 | case MSR_MTRRfix4K_F8000: | |
629 | case MSR_MTRRdefType: | |
630 | case MSR_IA32_CR_PAT: | |
631 | return true; | |
632 | case 0x2f8: | |
633 | return true; | |
634 | } | |
635 | return false; | |
636 | } | |
637 | ||
638 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |
639 | { | |
640 | if (!msr_mtrr_valid(msr)) | |
641 | return 1; | |
642 | ||
643 | vcpu->arch.mtrr[msr - 0x200] = data; | |
644 | return 0; | |
645 | } | |
15c4a640 CO |
646 | |
647 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |
648 | { | |
649 | switch (msr) { | |
15c4a640 CO |
650 | case MSR_EFER: |
651 | set_efer(vcpu, data); | |
652 | break; | |
15c4a640 CO |
653 | case MSR_IA32_MC0_STATUS: |
654 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", | |
b8688d51 | 655 | __func__, data); |
15c4a640 CO |
656 | break; |
657 | case MSR_IA32_MCG_STATUS: | |
658 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", | |
b8688d51 | 659 | __func__, data); |
15c4a640 | 660 | break; |
c7ac679c JR |
661 | case MSR_IA32_MCG_CTL: |
662 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n", | |
b8688d51 | 663 | __func__, data); |
c7ac679c | 664 | break; |
15c4a640 CO |
665 | case MSR_IA32_UCODE_REV: |
666 | case MSR_IA32_UCODE_WRITE: | |
15c4a640 | 667 | break; |
9ba075a6 AK |
668 | case 0x200 ... 0x2ff: |
669 | return set_msr_mtrr(vcpu, msr, data); | |
15c4a640 CO |
670 | case MSR_IA32_APICBASE: |
671 | kvm_set_apic_base(vcpu, data); | |
672 | break; | |
673 | case MSR_IA32_MISC_ENABLE: | |
ad312c7c | 674 | vcpu->arch.ia32_misc_enable_msr = data; |
15c4a640 | 675 | break; |
18068523 GOC |
676 | case MSR_KVM_WALL_CLOCK: |
677 | vcpu->kvm->arch.wall_clock = data; | |
678 | kvm_write_wall_clock(vcpu->kvm, data); | |
679 | break; | |
680 | case MSR_KVM_SYSTEM_TIME: { | |
681 | if (vcpu->arch.time_page) { | |
682 | kvm_release_page_dirty(vcpu->arch.time_page); | |
683 | vcpu->arch.time_page = NULL; | |
684 | } | |
685 | ||
686 | vcpu->arch.time = data; | |
687 | ||
688 | /* we verify if the enable bit is set... */ | |
689 | if (!(data & 1)) | |
690 | break; | |
691 | ||
692 | /* ...but clean it before doing the actual write */ | |
693 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); | |
694 | ||
18068523 | 695 | down_read(¤t->mm->mmap_sem); |
18068523 GOC |
696 | vcpu->arch.time_page = |
697 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); | |
18068523 GOC |
698 | up_read(¤t->mm->mmap_sem); |
699 | ||
700 | if (is_error_page(vcpu->arch.time_page)) { | |
701 | kvm_release_page_clean(vcpu->arch.time_page); | |
702 | vcpu->arch.time_page = NULL; | |
703 | } | |
704 | ||
705 | kvm_write_guest_time(vcpu); | |
706 | break; | |
707 | } | |
15c4a640 | 708 | default: |
565f1fbd | 709 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data); |
15c4a640 CO |
710 | return 1; |
711 | } | |
712 | return 0; | |
713 | } | |
714 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
715 | ||
716 | ||
717 | /* | |
718 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
719 | * Returns 0 on success, non-0 otherwise. | |
720 | * Assumes vcpu_load() was already called. | |
721 | */ | |
722 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
723 | { | |
724 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); | |
725 | } | |
726 | ||
9ba075a6 AK |
727 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
728 | { | |
729 | if (!msr_mtrr_valid(msr)) | |
730 | return 1; | |
731 | ||
732 | *pdata = vcpu->arch.mtrr[msr - 0x200]; | |
733 | return 0; | |
734 | } | |
735 | ||
15c4a640 CO |
736 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
737 | { | |
738 | u64 data; | |
739 | ||
740 | switch (msr) { | |
741 | case 0xc0010010: /* SYSCFG */ | |
742 | case 0xc0010015: /* HWCR */ | |
743 | case MSR_IA32_PLATFORM_ID: | |
744 | case MSR_IA32_P5_MC_ADDR: | |
745 | case MSR_IA32_P5_MC_TYPE: | |
746 | case MSR_IA32_MC0_CTL: | |
747 | case MSR_IA32_MCG_STATUS: | |
748 | case MSR_IA32_MCG_CAP: | |
c7ac679c | 749 | case MSR_IA32_MCG_CTL: |
15c4a640 CO |
750 | case MSR_IA32_MC0_MISC: |
751 | case MSR_IA32_MC0_MISC+4: | |
752 | case MSR_IA32_MC0_MISC+8: | |
753 | case MSR_IA32_MC0_MISC+12: | |
754 | case MSR_IA32_MC0_MISC+16: | |
755 | case MSR_IA32_UCODE_REV: | |
15c4a640 | 756 | case MSR_IA32_EBL_CR_POWERON: |
15c4a640 CO |
757 | data = 0; |
758 | break; | |
9ba075a6 AK |
759 | case MSR_MTRRcap: |
760 | data = 0x500 | KVM_NR_VAR_MTRR; | |
761 | break; | |
762 | case 0x200 ... 0x2ff: | |
763 | return get_msr_mtrr(vcpu, msr, pdata); | |
15c4a640 CO |
764 | case 0xcd: /* fsb frequency */ |
765 | data = 3; | |
766 | break; | |
767 | case MSR_IA32_APICBASE: | |
768 | data = kvm_get_apic_base(vcpu); | |
769 | break; | |
770 | case MSR_IA32_MISC_ENABLE: | |
ad312c7c | 771 | data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 772 | break; |
847f0ad8 AG |
773 | case MSR_IA32_PERF_STATUS: |
774 | /* TSC increment by tick */ | |
775 | data = 1000ULL; | |
776 | /* CPU multiplier */ | |
777 | data |= (((uint64_t)4ULL) << 40); | |
778 | break; | |
15c4a640 | 779 | case MSR_EFER: |
ad312c7c | 780 | data = vcpu->arch.shadow_efer; |
15c4a640 | 781 | break; |
18068523 GOC |
782 | case MSR_KVM_WALL_CLOCK: |
783 | data = vcpu->kvm->arch.wall_clock; | |
784 | break; | |
785 | case MSR_KVM_SYSTEM_TIME: | |
786 | data = vcpu->arch.time; | |
787 | break; | |
15c4a640 CO |
788 | default: |
789 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); | |
790 | return 1; | |
791 | } | |
792 | *pdata = data; | |
793 | return 0; | |
794 | } | |
795 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
796 | ||
313a3dc7 CO |
797 | /* |
798 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
799 | * | |
800 | * @return number of msrs set successfully. | |
801 | */ | |
802 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
803 | struct kvm_msr_entry *entries, | |
804 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
805 | unsigned index, u64 *data)) | |
806 | { | |
807 | int i; | |
808 | ||
809 | vcpu_load(vcpu); | |
810 | ||
3200f405 | 811 | down_read(&vcpu->kvm->slots_lock); |
313a3dc7 CO |
812 | for (i = 0; i < msrs->nmsrs; ++i) |
813 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
814 | break; | |
3200f405 | 815 | up_read(&vcpu->kvm->slots_lock); |
313a3dc7 CO |
816 | |
817 | vcpu_put(vcpu); | |
818 | ||
819 | return i; | |
820 | } | |
821 | ||
822 | /* | |
823 | * Read or write a bunch of msrs. Parameters are user addresses. | |
824 | * | |
825 | * @return number of msrs set successfully. | |
826 | */ | |
827 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
828 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
829 | unsigned index, u64 *data), | |
830 | int writeback) | |
831 | { | |
832 | struct kvm_msrs msrs; | |
833 | struct kvm_msr_entry *entries; | |
834 | int r, n; | |
835 | unsigned size; | |
836 | ||
837 | r = -EFAULT; | |
838 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
839 | goto out; | |
840 | ||
841 | r = -E2BIG; | |
842 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
843 | goto out; | |
844 | ||
845 | r = -ENOMEM; | |
846 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
847 | entries = vmalloc(size); | |
848 | if (!entries) | |
849 | goto out; | |
850 | ||
851 | r = -EFAULT; | |
852 | if (copy_from_user(entries, user_msrs->entries, size)) | |
853 | goto out_free; | |
854 | ||
855 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
856 | if (r < 0) | |
857 | goto out_free; | |
858 | ||
859 | r = -EFAULT; | |
860 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
861 | goto out_free; | |
862 | ||
863 | r = n; | |
864 | ||
865 | out_free: | |
866 | vfree(entries); | |
867 | out: | |
868 | return r; | |
869 | } | |
870 | ||
018d00d2 ZX |
871 | int kvm_dev_ioctl_check_extension(long ext) |
872 | { | |
873 | int r; | |
874 | ||
875 | switch (ext) { | |
876 | case KVM_CAP_IRQCHIP: | |
877 | case KVM_CAP_HLT: | |
878 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
879 | case KVM_CAP_USER_MEMORY: | |
880 | case KVM_CAP_SET_TSS_ADDR: | |
07716717 | 881 | case KVM_CAP_EXT_CPUID: |
18068523 | 882 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 883 | case KVM_CAP_PIT: |
a28e4f5a | 884 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 885 | case KVM_CAP_MP_STATE: |
018d00d2 ZX |
886 | r = 1; |
887 | break; | |
774ead3a AK |
888 | case KVM_CAP_VAPIC: |
889 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); | |
890 | break; | |
f725230a AK |
891 | case KVM_CAP_NR_VCPUS: |
892 | r = KVM_MAX_VCPUS; | |
893 | break; | |
a988b910 AK |
894 | case KVM_CAP_NR_MEMSLOTS: |
895 | r = KVM_MEMORY_SLOTS; | |
896 | break; | |
2f333bcb MT |
897 | case KVM_CAP_PV_MMU: |
898 | r = !tdp_enabled; | |
899 | break; | |
018d00d2 ZX |
900 | default: |
901 | r = 0; | |
902 | break; | |
903 | } | |
904 | return r; | |
905 | ||
906 | } | |
907 | ||
043405e1 CO |
908 | long kvm_arch_dev_ioctl(struct file *filp, |
909 | unsigned int ioctl, unsigned long arg) | |
910 | { | |
911 | void __user *argp = (void __user *)arg; | |
912 | long r; | |
913 | ||
914 | switch (ioctl) { | |
915 | case KVM_GET_MSR_INDEX_LIST: { | |
916 | struct kvm_msr_list __user *user_msr_list = argp; | |
917 | struct kvm_msr_list msr_list; | |
918 | unsigned n; | |
919 | ||
920 | r = -EFAULT; | |
921 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
922 | goto out; | |
923 | n = msr_list.nmsrs; | |
924 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); | |
925 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) | |
926 | goto out; | |
927 | r = -E2BIG; | |
928 | if (n < num_msrs_to_save) | |
929 | goto out; | |
930 | r = -EFAULT; | |
931 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
932 | num_msrs_to_save * sizeof(u32))) | |
933 | goto out; | |
934 | if (copy_to_user(user_msr_list->indices | |
935 | + num_msrs_to_save * sizeof(u32), | |
936 | &emulated_msrs, | |
937 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) | |
938 | goto out; | |
939 | r = 0; | |
940 | break; | |
941 | } | |
674eea0f AK |
942 | case KVM_GET_SUPPORTED_CPUID: { |
943 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
944 | struct kvm_cpuid2 cpuid; | |
945 | ||
946 | r = -EFAULT; | |
947 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
948 | goto out; | |
949 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, | |
950 | cpuid_arg->entries); | |
951 | if (r) | |
952 | goto out; | |
953 | ||
954 | r = -EFAULT; | |
955 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
956 | goto out; | |
957 | r = 0; | |
958 | break; | |
959 | } | |
043405e1 CO |
960 | default: |
961 | r = -EINVAL; | |
962 | } | |
963 | out: | |
964 | return r; | |
965 | } | |
966 | ||
313a3dc7 CO |
967 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
968 | { | |
969 | kvm_x86_ops->vcpu_load(vcpu, cpu); | |
18068523 | 970 | kvm_write_guest_time(vcpu); |
313a3dc7 CO |
971 | } |
972 | ||
973 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
974 | { | |
975 | kvm_x86_ops->vcpu_put(vcpu); | |
9327fd11 | 976 | kvm_put_guest_fpu(vcpu); |
313a3dc7 CO |
977 | } |
978 | ||
07716717 | 979 | static int is_efer_nx(void) |
313a3dc7 CO |
980 | { |
981 | u64 efer; | |
313a3dc7 CO |
982 | |
983 | rdmsrl(MSR_EFER, efer); | |
07716717 DK |
984 | return efer & EFER_NX; |
985 | } | |
986 | ||
987 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) | |
988 | { | |
989 | int i; | |
990 | struct kvm_cpuid_entry2 *e, *entry; | |
991 | ||
313a3dc7 | 992 | entry = NULL; |
ad312c7c ZX |
993 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
994 | e = &vcpu->arch.cpuid_entries[i]; | |
313a3dc7 CO |
995 | if (e->function == 0x80000001) { |
996 | entry = e; | |
997 | break; | |
998 | } | |
999 | } | |
07716717 | 1000 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
313a3dc7 CO |
1001 | entry->edx &= ~(1 << 20); |
1002 | printk(KERN_INFO "kvm: guest NX capability removed\n"); | |
1003 | } | |
1004 | } | |
1005 | ||
07716717 | 1006 | /* when an old userspace process fills a new kernel module */ |
313a3dc7 CO |
1007 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
1008 | struct kvm_cpuid *cpuid, | |
1009 | struct kvm_cpuid_entry __user *entries) | |
07716717 DK |
1010 | { |
1011 | int r, i; | |
1012 | struct kvm_cpuid_entry *cpuid_entries; | |
1013 | ||
1014 | r = -E2BIG; | |
1015 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
1016 | goto out; | |
1017 | r = -ENOMEM; | |
1018 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); | |
1019 | if (!cpuid_entries) | |
1020 | goto out; | |
1021 | r = -EFAULT; | |
1022 | if (copy_from_user(cpuid_entries, entries, | |
1023 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
1024 | goto out_free; | |
1025 | for (i = 0; i < cpuid->nent; i++) { | |
ad312c7c ZX |
1026 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
1027 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; | |
1028 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; | |
1029 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; | |
1030 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; | |
1031 | vcpu->arch.cpuid_entries[i].index = 0; | |
1032 | vcpu->arch.cpuid_entries[i].flags = 0; | |
1033 | vcpu->arch.cpuid_entries[i].padding[0] = 0; | |
1034 | vcpu->arch.cpuid_entries[i].padding[1] = 0; | |
1035 | vcpu->arch.cpuid_entries[i].padding[2] = 0; | |
1036 | } | |
1037 | vcpu->arch.cpuid_nent = cpuid->nent; | |
07716717 DK |
1038 | cpuid_fix_nx_cap(vcpu); |
1039 | r = 0; | |
1040 | ||
1041 | out_free: | |
1042 | vfree(cpuid_entries); | |
1043 | out: | |
1044 | return r; | |
1045 | } | |
1046 | ||
1047 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | |
1048 | struct kvm_cpuid2 *cpuid, | |
1049 | struct kvm_cpuid_entry2 __user *entries) | |
313a3dc7 CO |
1050 | { |
1051 | int r; | |
1052 | ||
1053 | r = -E2BIG; | |
1054 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
1055 | goto out; | |
1056 | r = -EFAULT; | |
ad312c7c | 1057 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
07716717 | 1058 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
313a3dc7 | 1059 | goto out; |
ad312c7c | 1060 | vcpu->arch.cpuid_nent = cpuid->nent; |
313a3dc7 CO |
1061 | return 0; |
1062 | ||
1063 | out: | |
1064 | return r; | |
1065 | } | |
1066 | ||
07716717 DK |
1067 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
1068 | struct kvm_cpuid2 *cpuid, | |
1069 | struct kvm_cpuid_entry2 __user *entries) | |
1070 | { | |
1071 | int r; | |
1072 | ||
1073 | r = -E2BIG; | |
ad312c7c | 1074 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
07716717 DK |
1075 | goto out; |
1076 | r = -EFAULT; | |
ad312c7c ZX |
1077 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
1078 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) | |
07716717 DK |
1079 | goto out; |
1080 | return 0; | |
1081 | ||
1082 | out: | |
ad312c7c | 1083 | cpuid->nent = vcpu->arch.cpuid_nent; |
07716717 DK |
1084 | return r; |
1085 | } | |
1086 | ||
1087 | static inline u32 bit(int bitno) | |
1088 | { | |
1089 | return 1 << (bitno & 31); | |
1090 | } | |
1091 | ||
1092 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
1093 | u32 index) | |
1094 | { | |
1095 | entry->function = function; | |
1096 | entry->index = index; | |
1097 | cpuid_count(entry->function, entry->index, | |
1098 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); | |
1099 | entry->flags = 0; | |
1100 | } | |
1101 | ||
1102 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, | |
1103 | u32 index, int *nent, int maxnent) | |
1104 | { | |
1105 | const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) | | |
1106 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | | |
1107 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | | |
1108 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | | |
1109 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | | |
1110 | bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) | | |
1111 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | | |
1112 | bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) | | |
1113 | bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) | | |
1114 | bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP); | |
1115 | const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) | | |
1116 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | | |
1117 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | | |
1118 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | | |
1119 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | | |
1120 | bit(X86_FEATURE_PGE) | | |
1121 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | | |
1122 | bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) | | |
1123 | bit(X86_FEATURE_SYSCALL) | | |
1124 | (bit(X86_FEATURE_NX) && is_efer_nx()) | | |
1125 | #ifdef CONFIG_X86_64 | |
1126 | bit(X86_FEATURE_LM) | | |
1127 | #endif | |
1128 | bit(X86_FEATURE_MMXEXT) | | |
1129 | bit(X86_FEATURE_3DNOWEXT) | | |
1130 | bit(X86_FEATURE_3DNOW); | |
1131 | const u32 kvm_supported_word3_x86_features = | |
1132 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); | |
1133 | const u32 kvm_supported_word6_x86_features = | |
1134 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY); | |
1135 | ||
1136 | /* all func 2 cpuid_count() should be called on the same cpu */ | |
1137 | get_cpu(); | |
1138 | do_cpuid_1_ent(entry, function, index); | |
1139 | ++*nent; | |
1140 | ||
1141 | switch (function) { | |
1142 | case 0: | |
1143 | entry->eax = min(entry->eax, (u32)0xb); | |
1144 | break; | |
1145 | case 1: | |
1146 | entry->edx &= kvm_supported_word0_x86_features; | |
1147 | entry->ecx &= kvm_supported_word3_x86_features; | |
1148 | break; | |
1149 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands | |
1150 | * may return different values. This forces us to get_cpu() before | |
1151 | * issuing the first command, and also to emulate this annoying behavior | |
1152 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ | |
1153 | case 2: { | |
1154 | int t, times = entry->eax & 0xff; | |
1155 | ||
1156 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
1157 | for (t = 1; t < times && *nent < maxnent; ++t) { | |
1158 | do_cpuid_1_ent(&entry[t], function, 0); | |
1159 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
1160 | ++*nent; | |
1161 | } | |
1162 | break; | |
1163 | } | |
1164 | /* function 4 and 0xb have additional index. */ | |
1165 | case 4: { | |
14af3f3c | 1166 | int i, cache_type; |
07716717 DK |
1167 | |
1168 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
1169 | /* read more entries until cache_type is zero */ | |
14af3f3c HH |
1170 | for (i = 1; *nent < maxnent; ++i) { |
1171 | cache_type = entry[i - 1].eax & 0x1f; | |
07716717 DK |
1172 | if (!cache_type) |
1173 | break; | |
14af3f3c HH |
1174 | do_cpuid_1_ent(&entry[i], function, i); |
1175 | entry[i].flags |= | |
07716717 DK |
1176 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
1177 | ++*nent; | |
1178 | } | |
1179 | break; | |
1180 | } | |
1181 | case 0xb: { | |
14af3f3c | 1182 | int i, level_type; |
07716717 DK |
1183 | |
1184 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
1185 | /* read more entries until level_type is zero */ | |
14af3f3c HH |
1186 | for (i = 1; *nent < maxnent; ++i) { |
1187 | level_type = entry[i - 1].ecx & 0xff; | |
07716717 DK |
1188 | if (!level_type) |
1189 | break; | |
14af3f3c HH |
1190 | do_cpuid_1_ent(&entry[i], function, i); |
1191 | entry[i].flags |= | |
07716717 DK |
1192 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
1193 | ++*nent; | |
1194 | } | |
1195 | break; | |
1196 | } | |
1197 | case 0x80000000: | |
1198 | entry->eax = min(entry->eax, 0x8000001a); | |
1199 | break; | |
1200 | case 0x80000001: | |
1201 | entry->edx &= kvm_supported_word1_x86_features; | |
1202 | entry->ecx &= kvm_supported_word6_x86_features; | |
1203 | break; | |
1204 | } | |
1205 | put_cpu(); | |
1206 | } | |
1207 | ||
674eea0f | 1208 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
07716717 DK |
1209 | struct kvm_cpuid_entry2 __user *entries) |
1210 | { | |
1211 | struct kvm_cpuid_entry2 *cpuid_entries; | |
1212 | int limit, nent = 0, r = -E2BIG; | |
1213 | u32 func; | |
1214 | ||
1215 | if (cpuid->nent < 1) | |
1216 | goto out; | |
1217 | r = -ENOMEM; | |
1218 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); | |
1219 | if (!cpuid_entries) | |
1220 | goto out; | |
1221 | ||
1222 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); | |
1223 | limit = cpuid_entries[0].eax; | |
1224 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) | |
1225 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
1226 | &nent, cpuid->nent); | |
1227 | r = -E2BIG; | |
1228 | if (nent >= cpuid->nent) | |
1229 | goto out_free; | |
1230 | ||
1231 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); | |
1232 | limit = cpuid_entries[nent - 1].eax; | |
1233 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) | |
1234 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
1235 | &nent, cpuid->nent); | |
1236 | r = -EFAULT; | |
1237 | if (copy_to_user(entries, cpuid_entries, | |
1238 | nent * sizeof(struct kvm_cpuid_entry2))) | |
1239 | goto out_free; | |
1240 | cpuid->nent = nent; | |
1241 | r = 0; | |
1242 | ||
1243 | out_free: | |
1244 | vfree(cpuid_entries); | |
1245 | out: | |
1246 | return r; | |
1247 | } | |
1248 | ||
313a3dc7 CO |
1249 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
1250 | struct kvm_lapic_state *s) | |
1251 | { | |
1252 | vcpu_load(vcpu); | |
ad312c7c | 1253 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
313a3dc7 CO |
1254 | vcpu_put(vcpu); |
1255 | ||
1256 | return 0; | |
1257 | } | |
1258 | ||
1259 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
1260 | struct kvm_lapic_state *s) | |
1261 | { | |
1262 | vcpu_load(vcpu); | |
ad312c7c | 1263 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
313a3dc7 CO |
1264 | kvm_apic_post_state_restore(vcpu); |
1265 | vcpu_put(vcpu); | |
1266 | ||
1267 | return 0; | |
1268 | } | |
1269 | ||
f77bc6a4 ZX |
1270 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
1271 | struct kvm_interrupt *irq) | |
1272 | { | |
1273 | if (irq->irq < 0 || irq->irq >= 256) | |
1274 | return -EINVAL; | |
1275 | if (irqchip_in_kernel(vcpu->kvm)) | |
1276 | return -ENXIO; | |
1277 | vcpu_load(vcpu); | |
1278 | ||
ad312c7c ZX |
1279 | set_bit(irq->irq, vcpu->arch.irq_pending); |
1280 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary); | |
f77bc6a4 ZX |
1281 | |
1282 | vcpu_put(vcpu); | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
b209749f AK |
1287 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
1288 | struct kvm_tpr_access_ctl *tac) | |
1289 | { | |
1290 | if (tac->flags) | |
1291 | return -EINVAL; | |
1292 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
1293 | return 0; | |
1294 | } | |
1295 | ||
313a3dc7 CO |
1296 | long kvm_arch_vcpu_ioctl(struct file *filp, |
1297 | unsigned int ioctl, unsigned long arg) | |
1298 | { | |
1299 | struct kvm_vcpu *vcpu = filp->private_data; | |
1300 | void __user *argp = (void __user *)arg; | |
1301 | int r; | |
1302 | ||
1303 | switch (ioctl) { | |
1304 | case KVM_GET_LAPIC: { | |
1305 | struct kvm_lapic_state lapic; | |
1306 | ||
1307 | memset(&lapic, 0, sizeof lapic); | |
1308 | r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic); | |
1309 | if (r) | |
1310 | goto out; | |
1311 | r = -EFAULT; | |
1312 | if (copy_to_user(argp, &lapic, sizeof lapic)) | |
1313 | goto out; | |
1314 | r = 0; | |
1315 | break; | |
1316 | } | |
1317 | case KVM_SET_LAPIC: { | |
1318 | struct kvm_lapic_state lapic; | |
1319 | ||
1320 | r = -EFAULT; | |
1321 | if (copy_from_user(&lapic, argp, sizeof lapic)) | |
1322 | goto out; | |
1323 | r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);; | |
1324 | if (r) | |
1325 | goto out; | |
1326 | r = 0; | |
1327 | break; | |
1328 | } | |
f77bc6a4 ZX |
1329 | case KVM_INTERRUPT: { |
1330 | struct kvm_interrupt irq; | |
1331 | ||
1332 | r = -EFAULT; | |
1333 | if (copy_from_user(&irq, argp, sizeof irq)) | |
1334 | goto out; | |
1335 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
1336 | if (r) | |
1337 | goto out; | |
1338 | r = 0; | |
1339 | break; | |
1340 | } | |
313a3dc7 CO |
1341 | case KVM_SET_CPUID: { |
1342 | struct kvm_cpuid __user *cpuid_arg = argp; | |
1343 | struct kvm_cpuid cpuid; | |
1344 | ||
1345 | r = -EFAULT; | |
1346 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1347 | goto out; | |
1348 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
1349 | if (r) | |
1350 | goto out; | |
1351 | break; | |
1352 | } | |
07716717 DK |
1353 | case KVM_SET_CPUID2: { |
1354 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1355 | struct kvm_cpuid2 cpuid; | |
1356 | ||
1357 | r = -EFAULT; | |
1358 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1359 | goto out; | |
1360 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
1361 | cpuid_arg->entries); | |
1362 | if (r) | |
1363 | goto out; | |
1364 | break; | |
1365 | } | |
1366 | case KVM_GET_CPUID2: { | |
1367 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1368 | struct kvm_cpuid2 cpuid; | |
1369 | ||
1370 | r = -EFAULT; | |
1371 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1372 | goto out; | |
1373 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
1374 | cpuid_arg->entries); | |
1375 | if (r) | |
1376 | goto out; | |
1377 | r = -EFAULT; | |
1378 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
1379 | goto out; | |
1380 | r = 0; | |
1381 | break; | |
1382 | } | |
313a3dc7 CO |
1383 | case KVM_GET_MSRS: |
1384 | r = msr_io(vcpu, argp, kvm_get_msr, 1); | |
1385 | break; | |
1386 | case KVM_SET_MSRS: | |
1387 | r = msr_io(vcpu, argp, do_set_msr, 0); | |
1388 | break; | |
b209749f AK |
1389 | case KVM_TPR_ACCESS_REPORTING: { |
1390 | struct kvm_tpr_access_ctl tac; | |
1391 | ||
1392 | r = -EFAULT; | |
1393 | if (copy_from_user(&tac, argp, sizeof tac)) | |
1394 | goto out; | |
1395 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
1396 | if (r) | |
1397 | goto out; | |
1398 | r = -EFAULT; | |
1399 | if (copy_to_user(argp, &tac, sizeof tac)) | |
1400 | goto out; | |
1401 | r = 0; | |
1402 | break; | |
1403 | }; | |
b93463aa AK |
1404 | case KVM_SET_VAPIC_ADDR: { |
1405 | struct kvm_vapic_addr va; | |
1406 | ||
1407 | r = -EINVAL; | |
1408 | if (!irqchip_in_kernel(vcpu->kvm)) | |
1409 | goto out; | |
1410 | r = -EFAULT; | |
1411 | if (copy_from_user(&va, argp, sizeof va)) | |
1412 | goto out; | |
1413 | r = 0; | |
1414 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); | |
1415 | break; | |
1416 | } | |
313a3dc7 CO |
1417 | default: |
1418 | r = -EINVAL; | |
1419 | } | |
1420 | out: | |
1421 | return r; | |
1422 | } | |
1423 | ||
1fe779f8 CO |
1424 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
1425 | { | |
1426 | int ret; | |
1427 | ||
1428 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
1429 | return -1; | |
1430 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); | |
1431 | return ret; | |
1432 | } | |
1433 | ||
1434 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, | |
1435 | u32 kvm_nr_mmu_pages) | |
1436 | { | |
1437 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
1438 | return -EINVAL; | |
1439 | ||
72dc67a6 | 1440 | down_write(&kvm->slots_lock); |
1fe779f8 CO |
1441 | |
1442 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 1443 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 1444 | |
72dc67a6 | 1445 | up_write(&kvm->slots_lock); |
1fe779f8 CO |
1446 | return 0; |
1447 | } | |
1448 | ||
1449 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) | |
1450 | { | |
f05e70ac | 1451 | return kvm->arch.n_alloc_mmu_pages; |
1fe779f8 CO |
1452 | } |
1453 | ||
e9f85cde ZX |
1454 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
1455 | { | |
1456 | int i; | |
1457 | struct kvm_mem_alias *alias; | |
1458 | ||
d69fb81f ZX |
1459 | for (i = 0; i < kvm->arch.naliases; ++i) { |
1460 | alias = &kvm->arch.aliases[i]; | |
e9f85cde ZX |
1461 | if (gfn >= alias->base_gfn |
1462 | && gfn < alias->base_gfn + alias->npages) | |
1463 | return alias->target_gfn + gfn - alias->base_gfn; | |
1464 | } | |
1465 | return gfn; | |
1466 | } | |
1467 | ||
1fe779f8 CO |
1468 | /* |
1469 | * Set a new alias region. Aliases map a portion of physical memory into | |
1470 | * another portion. This is useful for memory windows, for example the PC | |
1471 | * VGA region. | |
1472 | */ | |
1473 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, | |
1474 | struct kvm_memory_alias *alias) | |
1475 | { | |
1476 | int r, n; | |
1477 | struct kvm_mem_alias *p; | |
1478 | ||
1479 | r = -EINVAL; | |
1480 | /* General sanity checks */ | |
1481 | if (alias->memory_size & (PAGE_SIZE - 1)) | |
1482 | goto out; | |
1483 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) | |
1484 | goto out; | |
1485 | if (alias->slot >= KVM_ALIAS_SLOTS) | |
1486 | goto out; | |
1487 | if (alias->guest_phys_addr + alias->memory_size | |
1488 | < alias->guest_phys_addr) | |
1489 | goto out; | |
1490 | if (alias->target_phys_addr + alias->memory_size | |
1491 | < alias->target_phys_addr) | |
1492 | goto out; | |
1493 | ||
72dc67a6 | 1494 | down_write(&kvm->slots_lock); |
1fe779f8 | 1495 | |
d69fb81f | 1496 | p = &kvm->arch.aliases[alias->slot]; |
1fe779f8 CO |
1497 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
1498 | p->npages = alias->memory_size >> PAGE_SHIFT; | |
1499 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; | |
1500 | ||
1501 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) | |
d69fb81f | 1502 | if (kvm->arch.aliases[n - 1].npages) |
1fe779f8 | 1503 | break; |
d69fb81f | 1504 | kvm->arch.naliases = n; |
1fe779f8 CO |
1505 | |
1506 | kvm_mmu_zap_all(kvm); | |
1507 | ||
72dc67a6 | 1508 | up_write(&kvm->slots_lock); |
1fe779f8 CO |
1509 | |
1510 | return 0; | |
1511 | ||
1512 | out: | |
1513 | return r; | |
1514 | } | |
1515 | ||
1516 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1517 | { | |
1518 | int r; | |
1519 | ||
1520 | r = 0; | |
1521 | switch (chip->chip_id) { | |
1522 | case KVM_IRQCHIP_PIC_MASTER: | |
1523 | memcpy(&chip->chip.pic, | |
1524 | &pic_irqchip(kvm)->pics[0], | |
1525 | sizeof(struct kvm_pic_state)); | |
1526 | break; | |
1527 | case KVM_IRQCHIP_PIC_SLAVE: | |
1528 | memcpy(&chip->chip.pic, | |
1529 | &pic_irqchip(kvm)->pics[1], | |
1530 | sizeof(struct kvm_pic_state)); | |
1531 | break; | |
1532 | case KVM_IRQCHIP_IOAPIC: | |
1533 | memcpy(&chip->chip.ioapic, | |
1534 | ioapic_irqchip(kvm), | |
1535 | sizeof(struct kvm_ioapic_state)); | |
1536 | break; | |
1537 | default: | |
1538 | r = -EINVAL; | |
1539 | break; | |
1540 | } | |
1541 | return r; | |
1542 | } | |
1543 | ||
1544 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1545 | { | |
1546 | int r; | |
1547 | ||
1548 | r = 0; | |
1549 | switch (chip->chip_id) { | |
1550 | case KVM_IRQCHIP_PIC_MASTER: | |
1551 | memcpy(&pic_irqchip(kvm)->pics[0], | |
1552 | &chip->chip.pic, | |
1553 | sizeof(struct kvm_pic_state)); | |
1554 | break; | |
1555 | case KVM_IRQCHIP_PIC_SLAVE: | |
1556 | memcpy(&pic_irqchip(kvm)->pics[1], | |
1557 | &chip->chip.pic, | |
1558 | sizeof(struct kvm_pic_state)); | |
1559 | break; | |
1560 | case KVM_IRQCHIP_IOAPIC: | |
1561 | memcpy(ioapic_irqchip(kvm), | |
1562 | &chip->chip.ioapic, | |
1563 | sizeof(struct kvm_ioapic_state)); | |
1564 | break; | |
1565 | default: | |
1566 | r = -EINVAL; | |
1567 | break; | |
1568 | } | |
1569 | kvm_pic_update_irq(pic_irqchip(kvm)); | |
1570 | return r; | |
1571 | } | |
1572 | ||
e0f63cb9 SY |
1573 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
1574 | { | |
1575 | int r = 0; | |
1576 | ||
1577 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); | |
1578 | return r; | |
1579 | } | |
1580 | ||
1581 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
1582 | { | |
1583 | int r = 0; | |
1584 | ||
1585 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); | |
1586 | kvm_pit_load_count(kvm, 0, ps->channels[0].count); | |
1587 | return r; | |
1588 | } | |
1589 | ||
5bb064dc ZX |
1590 | /* |
1591 | * Get (and clear) the dirty memory log for a memory slot. | |
1592 | */ | |
1593 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
1594 | struct kvm_dirty_log *log) | |
1595 | { | |
1596 | int r; | |
1597 | int n; | |
1598 | struct kvm_memory_slot *memslot; | |
1599 | int is_dirty = 0; | |
1600 | ||
72dc67a6 | 1601 | down_write(&kvm->slots_lock); |
5bb064dc ZX |
1602 | |
1603 | r = kvm_get_dirty_log(kvm, log, &is_dirty); | |
1604 | if (r) | |
1605 | goto out; | |
1606 | ||
1607 | /* If nothing is dirty, don't bother messing with page tables. */ | |
1608 | if (is_dirty) { | |
1609 | kvm_mmu_slot_remove_write_access(kvm, log->slot); | |
1610 | kvm_flush_remote_tlbs(kvm); | |
1611 | memslot = &kvm->memslots[log->slot]; | |
1612 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; | |
1613 | memset(memslot->dirty_bitmap, 0, n); | |
1614 | } | |
1615 | r = 0; | |
1616 | out: | |
72dc67a6 | 1617 | up_write(&kvm->slots_lock); |
5bb064dc ZX |
1618 | return r; |
1619 | } | |
1620 | ||
1fe779f8 CO |
1621 | long kvm_arch_vm_ioctl(struct file *filp, |
1622 | unsigned int ioctl, unsigned long arg) | |
1623 | { | |
1624 | struct kvm *kvm = filp->private_data; | |
1625 | void __user *argp = (void __user *)arg; | |
1626 | int r = -EINVAL; | |
1627 | ||
1628 | switch (ioctl) { | |
1629 | case KVM_SET_TSS_ADDR: | |
1630 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1631 | if (r < 0) | |
1632 | goto out; | |
1633 | break; | |
1634 | case KVM_SET_MEMORY_REGION: { | |
1635 | struct kvm_memory_region kvm_mem; | |
1636 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1637 | ||
1638 | r = -EFAULT; | |
1639 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) | |
1640 | goto out; | |
1641 | kvm_userspace_mem.slot = kvm_mem.slot; | |
1642 | kvm_userspace_mem.flags = kvm_mem.flags; | |
1643 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; | |
1644 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; | |
1645 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); | |
1646 | if (r) | |
1647 | goto out; | |
1648 | break; | |
1649 | } | |
1650 | case KVM_SET_NR_MMU_PAGES: | |
1651 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1652 | if (r) | |
1653 | goto out; | |
1654 | break; | |
1655 | case KVM_GET_NR_MMU_PAGES: | |
1656 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
1657 | break; | |
1658 | case KVM_SET_MEMORY_ALIAS: { | |
1659 | struct kvm_memory_alias alias; | |
1660 | ||
1661 | r = -EFAULT; | |
1662 | if (copy_from_user(&alias, argp, sizeof alias)) | |
1663 | goto out; | |
1664 | r = kvm_vm_ioctl_set_memory_alias(kvm, &alias); | |
1665 | if (r) | |
1666 | goto out; | |
1667 | break; | |
1668 | } | |
1669 | case KVM_CREATE_IRQCHIP: | |
1670 | r = -ENOMEM; | |
d7deeeb0 ZX |
1671 | kvm->arch.vpic = kvm_create_pic(kvm); |
1672 | if (kvm->arch.vpic) { | |
1fe779f8 CO |
1673 | r = kvm_ioapic_init(kvm); |
1674 | if (r) { | |
d7deeeb0 ZX |
1675 | kfree(kvm->arch.vpic); |
1676 | kvm->arch.vpic = NULL; | |
1fe779f8 CO |
1677 | goto out; |
1678 | } | |
1679 | } else | |
1680 | goto out; | |
1681 | break; | |
7837699f SY |
1682 | case KVM_CREATE_PIT: |
1683 | r = -ENOMEM; | |
1684 | kvm->arch.vpit = kvm_create_pit(kvm); | |
1685 | if (kvm->arch.vpit) | |
1686 | r = 0; | |
1687 | break; | |
1fe779f8 CO |
1688 | case KVM_IRQ_LINE: { |
1689 | struct kvm_irq_level irq_event; | |
1690 | ||
1691 | r = -EFAULT; | |
1692 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | |
1693 | goto out; | |
1694 | if (irqchip_in_kernel(kvm)) { | |
1695 | mutex_lock(&kvm->lock); | |
1696 | if (irq_event.irq < 16) | |
1697 | kvm_pic_set_irq(pic_irqchip(kvm), | |
1698 | irq_event.irq, | |
1699 | irq_event.level); | |
d7deeeb0 | 1700 | kvm_ioapic_set_irq(kvm->arch.vioapic, |
1fe779f8 CO |
1701 | irq_event.irq, |
1702 | irq_event.level); | |
1703 | mutex_unlock(&kvm->lock); | |
1704 | r = 0; | |
1705 | } | |
1706 | break; | |
1707 | } | |
1708 | case KVM_GET_IRQCHIP: { | |
1709 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
1710 | struct kvm_irqchip chip; | |
1711 | ||
1712 | r = -EFAULT; | |
1713 | if (copy_from_user(&chip, argp, sizeof chip)) | |
1714 | goto out; | |
1715 | r = -ENXIO; | |
1716 | if (!irqchip_in_kernel(kvm)) | |
1717 | goto out; | |
1718 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); | |
1719 | if (r) | |
1720 | goto out; | |
1721 | r = -EFAULT; | |
1722 | if (copy_to_user(argp, &chip, sizeof chip)) | |
1723 | goto out; | |
1724 | r = 0; | |
1725 | break; | |
1726 | } | |
1727 | case KVM_SET_IRQCHIP: { | |
1728 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
1729 | struct kvm_irqchip chip; | |
1730 | ||
1731 | r = -EFAULT; | |
1732 | if (copy_from_user(&chip, argp, sizeof chip)) | |
1733 | goto out; | |
1734 | r = -ENXIO; | |
1735 | if (!irqchip_in_kernel(kvm)) | |
1736 | goto out; | |
1737 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); | |
1738 | if (r) | |
1739 | goto out; | |
1740 | r = 0; | |
1741 | break; | |
1742 | } | |
e0f63cb9 SY |
1743 | case KVM_GET_PIT: { |
1744 | struct kvm_pit_state ps; | |
1745 | r = -EFAULT; | |
1746 | if (copy_from_user(&ps, argp, sizeof ps)) | |
1747 | goto out; | |
1748 | r = -ENXIO; | |
1749 | if (!kvm->arch.vpit) | |
1750 | goto out; | |
1751 | r = kvm_vm_ioctl_get_pit(kvm, &ps); | |
1752 | if (r) | |
1753 | goto out; | |
1754 | r = -EFAULT; | |
1755 | if (copy_to_user(argp, &ps, sizeof ps)) | |
1756 | goto out; | |
1757 | r = 0; | |
1758 | break; | |
1759 | } | |
1760 | case KVM_SET_PIT: { | |
1761 | struct kvm_pit_state ps; | |
1762 | r = -EFAULT; | |
1763 | if (copy_from_user(&ps, argp, sizeof ps)) | |
1764 | goto out; | |
1765 | r = -ENXIO; | |
1766 | if (!kvm->arch.vpit) | |
1767 | goto out; | |
1768 | r = kvm_vm_ioctl_set_pit(kvm, &ps); | |
1769 | if (r) | |
1770 | goto out; | |
1771 | r = 0; | |
1772 | break; | |
1773 | } | |
1fe779f8 CO |
1774 | default: |
1775 | ; | |
1776 | } | |
1777 | out: | |
1778 | return r; | |
1779 | } | |
1780 | ||
a16b043c | 1781 | static void kvm_init_msr_list(void) |
043405e1 CO |
1782 | { |
1783 | u32 dummy[2]; | |
1784 | unsigned i, j; | |
1785 | ||
1786 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
1787 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
1788 | continue; | |
1789 | if (j < i) | |
1790 | msrs_to_save[j] = msrs_to_save[i]; | |
1791 | j++; | |
1792 | } | |
1793 | num_msrs_to_save = j; | |
1794 | } | |
1795 | ||
bbd9b64e CO |
1796 | /* |
1797 | * Only apic need an MMIO device hook, so shortcut now.. | |
1798 | */ | |
1799 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
1800 | gpa_t addr) | |
1801 | { | |
1802 | struct kvm_io_device *dev; | |
1803 | ||
ad312c7c ZX |
1804 | if (vcpu->arch.apic) { |
1805 | dev = &vcpu->arch.apic->dev; | |
bbd9b64e CO |
1806 | if (dev->in_range(dev, addr)) |
1807 | return dev; | |
1808 | } | |
1809 | return NULL; | |
1810 | } | |
1811 | ||
1812 | ||
1813 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, | |
1814 | gpa_t addr) | |
1815 | { | |
1816 | struct kvm_io_device *dev; | |
1817 | ||
1818 | dev = vcpu_find_pervcpu_dev(vcpu, addr); | |
1819 | if (dev == NULL) | |
1820 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr); | |
1821 | return dev; | |
1822 | } | |
1823 | ||
1824 | int emulator_read_std(unsigned long addr, | |
1825 | void *val, | |
1826 | unsigned int bytes, | |
1827 | struct kvm_vcpu *vcpu) | |
1828 | { | |
1829 | void *data = val; | |
10589a46 | 1830 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
1831 | |
1832 | while (bytes) { | |
ad312c7c | 1833 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e CO |
1834 | unsigned offset = addr & (PAGE_SIZE-1); |
1835 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); | |
1836 | int ret; | |
1837 | ||
10589a46 MT |
1838 | if (gpa == UNMAPPED_GVA) { |
1839 | r = X86EMUL_PROPAGATE_FAULT; | |
1840 | goto out; | |
1841 | } | |
bbd9b64e | 1842 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
10589a46 MT |
1843 | if (ret < 0) { |
1844 | r = X86EMUL_UNHANDLEABLE; | |
1845 | goto out; | |
1846 | } | |
bbd9b64e CO |
1847 | |
1848 | bytes -= tocopy; | |
1849 | data += tocopy; | |
1850 | addr += tocopy; | |
1851 | } | |
10589a46 | 1852 | out: |
10589a46 | 1853 | return r; |
bbd9b64e CO |
1854 | } |
1855 | EXPORT_SYMBOL_GPL(emulator_read_std); | |
1856 | ||
bbd9b64e CO |
1857 | static int emulator_read_emulated(unsigned long addr, |
1858 | void *val, | |
1859 | unsigned int bytes, | |
1860 | struct kvm_vcpu *vcpu) | |
1861 | { | |
1862 | struct kvm_io_device *mmio_dev; | |
1863 | gpa_t gpa; | |
1864 | ||
1865 | if (vcpu->mmio_read_completed) { | |
1866 | memcpy(val, vcpu->mmio_data, bytes); | |
1867 | vcpu->mmio_read_completed = 0; | |
1868 | return X86EMUL_CONTINUE; | |
1869 | } | |
1870 | ||
ad312c7c | 1871 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e CO |
1872 | |
1873 | /* For APIC access vmexit */ | |
1874 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
1875 | goto mmio; | |
1876 | ||
1877 | if (emulator_read_std(addr, val, bytes, vcpu) | |
1878 | == X86EMUL_CONTINUE) | |
1879 | return X86EMUL_CONTINUE; | |
1880 | if (gpa == UNMAPPED_GVA) | |
1881 | return X86EMUL_PROPAGATE_FAULT; | |
1882 | ||
1883 | mmio: | |
1884 | /* | |
1885 | * Is this MMIO handled locally? | |
1886 | */ | |
10589a46 | 1887 | mutex_lock(&vcpu->kvm->lock); |
bbd9b64e CO |
1888 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
1889 | if (mmio_dev) { | |
1890 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
10589a46 | 1891 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
1892 | return X86EMUL_CONTINUE; |
1893 | } | |
10589a46 | 1894 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
1895 | |
1896 | vcpu->mmio_needed = 1; | |
1897 | vcpu->mmio_phys_addr = gpa; | |
1898 | vcpu->mmio_size = bytes; | |
1899 | vcpu->mmio_is_write = 0; | |
1900 | ||
1901 | return X86EMUL_UNHANDLEABLE; | |
1902 | } | |
1903 | ||
3200f405 | 1904 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
9f811285 | 1905 | const void *val, int bytes) |
bbd9b64e CO |
1906 | { |
1907 | int ret; | |
1908 | ||
1909 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); | |
9f811285 | 1910 | if (ret < 0) |
bbd9b64e CO |
1911 | return 0; |
1912 | kvm_mmu_pte_write(vcpu, gpa, val, bytes); | |
1913 | return 1; | |
1914 | } | |
1915 | ||
1916 | static int emulator_write_emulated_onepage(unsigned long addr, | |
1917 | const void *val, | |
1918 | unsigned int bytes, | |
1919 | struct kvm_vcpu *vcpu) | |
1920 | { | |
1921 | struct kvm_io_device *mmio_dev; | |
10589a46 MT |
1922 | gpa_t gpa; |
1923 | ||
10589a46 | 1924 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e CO |
1925 | |
1926 | if (gpa == UNMAPPED_GVA) { | |
c3c91fee | 1927 | kvm_inject_page_fault(vcpu, addr, 2); |
bbd9b64e CO |
1928 | return X86EMUL_PROPAGATE_FAULT; |
1929 | } | |
1930 | ||
1931 | /* For APIC access vmexit */ | |
1932 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
1933 | goto mmio; | |
1934 | ||
1935 | if (emulator_write_phys(vcpu, gpa, val, bytes)) | |
1936 | return X86EMUL_CONTINUE; | |
1937 | ||
1938 | mmio: | |
1939 | /* | |
1940 | * Is this MMIO handled locally? | |
1941 | */ | |
10589a46 | 1942 | mutex_lock(&vcpu->kvm->lock); |
bbd9b64e CO |
1943 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa); |
1944 | if (mmio_dev) { | |
1945 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
10589a46 | 1946 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
1947 | return X86EMUL_CONTINUE; |
1948 | } | |
10589a46 | 1949 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
1950 | |
1951 | vcpu->mmio_needed = 1; | |
1952 | vcpu->mmio_phys_addr = gpa; | |
1953 | vcpu->mmio_size = bytes; | |
1954 | vcpu->mmio_is_write = 1; | |
1955 | memcpy(vcpu->mmio_data, val, bytes); | |
1956 | ||
1957 | return X86EMUL_CONTINUE; | |
1958 | } | |
1959 | ||
1960 | int emulator_write_emulated(unsigned long addr, | |
1961 | const void *val, | |
1962 | unsigned int bytes, | |
1963 | struct kvm_vcpu *vcpu) | |
1964 | { | |
1965 | /* Crossing a page boundary? */ | |
1966 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
1967 | int rc, now; | |
1968 | ||
1969 | now = -addr & ~PAGE_MASK; | |
1970 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); | |
1971 | if (rc != X86EMUL_CONTINUE) | |
1972 | return rc; | |
1973 | addr += now; | |
1974 | val += now; | |
1975 | bytes -= now; | |
1976 | } | |
1977 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); | |
1978 | } | |
1979 | EXPORT_SYMBOL_GPL(emulator_write_emulated); | |
1980 | ||
1981 | static int emulator_cmpxchg_emulated(unsigned long addr, | |
1982 | const void *old, | |
1983 | const void *new, | |
1984 | unsigned int bytes, | |
1985 | struct kvm_vcpu *vcpu) | |
1986 | { | |
1987 | static int reported; | |
1988 | ||
1989 | if (!reported) { | |
1990 | reported = 1; | |
1991 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
1992 | } | |
2bacc55c MT |
1993 | #ifndef CONFIG_X86_64 |
1994 | /* guests cmpxchg8b have to be emulated atomically */ | |
1995 | if (bytes == 8) { | |
10589a46 | 1996 | gpa_t gpa; |
2bacc55c | 1997 | struct page *page; |
c0b49b0d | 1998 | char *kaddr; |
2bacc55c MT |
1999 | u64 val; |
2000 | ||
10589a46 MT |
2001 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
2002 | ||
2bacc55c MT |
2003 | if (gpa == UNMAPPED_GVA || |
2004 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
2005 | goto emul_write; | |
2006 | ||
2007 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) | |
2008 | goto emul_write; | |
2009 | ||
2010 | val = *(u64 *)new; | |
72dc67a6 IE |
2011 | |
2012 | down_read(¤t->mm->mmap_sem); | |
2bacc55c | 2013 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
72dc67a6 IE |
2014 | up_read(¤t->mm->mmap_sem); |
2015 | ||
c0b49b0d AM |
2016 | kaddr = kmap_atomic(page, KM_USER0); |
2017 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); | |
2018 | kunmap_atomic(kaddr, KM_USER0); | |
2bacc55c MT |
2019 | kvm_release_page_dirty(page); |
2020 | } | |
3200f405 | 2021 | emul_write: |
2bacc55c MT |
2022 | #endif |
2023 | ||
bbd9b64e CO |
2024 | return emulator_write_emulated(addr, new, bytes, vcpu); |
2025 | } | |
2026 | ||
2027 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
2028 | { | |
2029 | return kvm_x86_ops->get_segment_base(vcpu, seg); | |
2030 | } | |
2031 | ||
2032 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
2033 | { | |
2034 | return X86EMUL_CONTINUE; | |
2035 | } | |
2036 | ||
2037 | int emulate_clts(struct kvm_vcpu *vcpu) | |
2038 | { | |
54e445ca | 2039 | KVMTRACE_0D(CLTS, vcpu, handler); |
ad312c7c | 2040 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS); |
bbd9b64e CO |
2041 | return X86EMUL_CONTINUE; |
2042 | } | |
2043 | ||
2044 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) | |
2045 | { | |
2046 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
2047 | ||
2048 | switch (dr) { | |
2049 | case 0 ... 3: | |
2050 | *dest = kvm_x86_ops->get_dr(vcpu, dr); | |
2051 | return X86EMUL_CONTINUE; | |
2052 | default: | |
b8688d51 | 2053 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr); |
bbd9b64e CO |
2054 | return X86EMUL_UNHANDLEABLE; |
2055 | } | |
2056 | } | |
2057 | ||
2058 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
2059 | { | |
2060 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
2061 | int exception; | |
2062 | ||
2063 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); | |
2064 | if (exception) { | |
2065 | /* FIXME: better handling */ | |
2066 | return X86EMUL_UNHANDLEABLE; | |
2067 | } | |
2068 | return X86EMUL_CONTINUE; | |
2069 | } | |
2070 | ||
2071 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | |
2072 | { | |
2073 | static int reported; | |
2074 | u8 opcodes[4]; | |
ad312c7c | 2075 | unsigned long rip = vcpu->arch.rip; |
bbd9b64e CO |
2076 | unsigned long rip_linear; |
2077 | ||
2078 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); | |
2079 | ||
2080 | if (reported) | |
2081 | return; | |
2082 | ||
2083 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); | |
2084 | ||
2085 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", | |
2086 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
2087 | reported = 1; | |
2088 | } | |
2089 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); | |
2090 | ||
14af3f3c | 2091 | static struct x86_emulate_ops emulate_ops = { |
bbd9b64e | 2092 | .read_std = emulator_read_std, |
bbd9b64e CO |
2093 | .read_emulated = emulator_read_emulated, |
2094 | .write_emulated = emulator_write_emulated, | |
2095 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
2096 | }; | |
2097 | ||
2098 | int emulate_instruction(struct kvm_vcpu *vcpu, | |
2099 | struct kvm_run *run, | |
2100 | unsigned long cr2, | |
2101 | u16 error_code, | |
571008da | 2102 | int emulation_type) |
bbd9b64e CO |
2103 | { |
2104 | int r; | |
571008da | 2105 | struct decode_cache *c; |
bbd9b64e | 2106 | |
ad312c7c | 2107 | vcpu->arch.mmio_fault_cr2 = cr2; |
bbd9b64e CO |
2108 | kvm_x86_ops->cache_regs(vcpu); |
2109 | ||
2110 | vcpu->mmio_is_write = 0; | |
ad312c7c | 2111 | vcpu->arch.pio.string = 0; |
bbd9b64e | 2112 | |
571008da | 2113 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
bbd9b64e CO |
2114 | int cs_db, cs_l; |
2115 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
2116 | ||
ad312c7c ZX |
2117 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
2118 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); | |
2119 | vcpu->arch.emulate_ctxt.mode = | |
2120 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) | |
bbd9b64e CO |
2121 | ? X86EMUL_MODE_REAL : cs_l |
2122 | ? X86EMUL_MODE_PROT64 : cs_db | |
2123 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
2124 | ||
ad312c7c ZX |
2125 | if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) { |
2126 | vcpu->arch.emulate_ctxt.cs_base = 0; | |
2127 | vcpu->arch.emulate_ctxt.ds_base = 0; | |
2128 | vcpu->arch.emulate_ctxt.es_base = 0; | |
2129 | vcpu->arch.emulate_ctxt.ss_base = 0; | |
bbd9b64e | 2130 | } else { |
ad312c7c | 2131 | vcpu->arch.emulate_ctxt.cs_base = |
bbd9b64e | 2132 | get_segment_base(vcpu, VCPU_SREG_CS); |
ad312c7c | 2133 | vcpu->arch.emulate_ctxt.ds_base = |
bbd9b64e | 2134 | get_segment_base(vcpu, VCPU_SREG_DS); |
ad312c7c | 2135 | vcpu->arch.emulate_ctxt.es_base = |
bbd9b64e | 2136 | get_segment_base(vcpu, VCPU_SREG_ES); |
ad312c7c | 2137 | vcpu->arch.emulate_ctxt.ss_base = |
bbd9b64e CO |
2138 | get_segment_base(vcpu, VCPU_SREG_SS); |
2139 | } | |
2140 | ||
ad312c7c | 2141 | vcpu->arch.emulate_ctxt.gs_base = |
bbd9b64e | 2142 | get_segment_base(vcpu, VCPU_SREG_GS); |
ad312c7c | 2143 | vcpu->arch.emulate_ctxt.fs_base = |
bbd9b64e CO |
2144 | get_segment_base(vcpu, VCPU_SREG_FS); |
2145 | ||
ad312c7c | 2146 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
571008da SY |
2147 | |
2148 | /* Reject the instructions other than VMCALL/VMMCALL when | |
2149 | * try to emulate invalid opcode */ | |
2150 | c = &vcpu->arch.emulate_ctxt.decode; | |
2151 | if ((emulation_type & EMULTYPE_TRAP_UD) && | |
2152 | (!(c->twobyte && c->b == 0x01 && | |
2153 | (c->modrm_reg == 0 || c->modrm_reg == 3) && | |
2154 | c->modrm_mod == 3 && c->modrm_rm == 1))) | |
2155 | return EMULATE_FAIL; | |
2156 | ||
f2b5756b | 2157 | ++vcpu->stat.insn_emulation; |
bbd9b64e | 2158 | if (r) { |
f2b5756b | 2159 | ++vcpu->stat.insn_emulation_fail; |
bbd9b64e CO |
2160 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
2161 | return EMULATE_DONE; | |
2162 | return EMULATE_FAIL; | |
2163 | } | |
2164 | } | |
2165 | ||
ad312c7c | 2166 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
bbd9b64e | 2167 | |
ad312c7c | 2168 | if (vcpu->arch.pio.string) |
bbd9b64e CO |
2169 | return EMULATE_DO_MMIO; |
2170 | ||
2171 | if ((r || vcpu->mmio_is_write) && run) { | |
2172 | run->exit_reason = KVM_EXIT_MMIO; | |
2173 | run->mmio.phys_addr = vcpu->mmio_phys_addr; | |
2174 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
2175 | run->mmio.len = vcpu->mmio_size; | |
2176 | run->mmio.is_write = vcpu->mmio_is_write; | |
2177 | } | |
2178 | ||
2179 | if (r) { | |
2180 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | |
2181 | return EMULATE_DONE; | |
2182 | if (!vcpu->mmio_needed) { | |
2183 | kvm_report_emulation_failure(vcpu, "mmio"); | |
2184 | return EMULATE_FAIL; | |
2185 | } | |
2186 | return EMULATE_DO_MMIO; | |
2187 | } | |
2188 | ||
2189 | kvm_x86_ops->decache_regs(vcpu); | |
ad312c7c | 2190 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
bbd9b64e CO |
2191 | |
2192 | if (vcpu->mmio_is_write) { | |
2193 | vcpu->mmio_needed = 0; | |
2194 | return EMULATE_DO_MMIO; | |
2195 | } | |
2196 | ||
2197 | return EMULATE_DONE; | |
2198 | } | |
2199 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
2200 | ||
de7d789a CO |
2201 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
2202 | { | |
2203 | int i; | |
2204 | ||
ad312c7c ZX |
2205 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i) |
2206 | if (vcpu->arch.pio.guest_pages[i]) { | |
2207 | kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]); | |
2208 | vcpu->arch.pio.guest_pages[i] = NULL; | |
de7d789a CO |
2209 | } |
2210 | } | |
2211 | ||
2212 | static int pio_copy_data(struct kvm_vcpu *vcpu) | |
2213 | { | |
ad312c7c | 2214 | void *p = vcpu->arch.pio_data; |
de7d789a CO |
2215 | void *q; |
2216 | unsigned bytes; | |
ad312c7c | 2217 | int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1; |
de7d789a | 2218 | |
ad312c7c | 2219 | q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
de7d789a CO |
2220 | PAGE_KERNEL); |
2221 | if (!q) { | |
2222 | free_pio_guest_pages(vcpu); | |
2223 | return -ENOMEM; | |
2224 | } | |
ad312c7c ZX |
2225 | q += vcpu->arch.pio.guest_page_offset; |
2226 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; | |
2227 | if (vcpu->arch.pio.in) | |
de7d789a CO |
2228 | memcpy(q, p, bytes); |
2229 | else | |
2230 | memcpy(p, q, bytes); | |
ad312c7c | 2231 | q -= vcpu->arch.pio.guest_page_offset; |
de7d789a CO |
2232 | vunmap(q); |
2233 | free_pio_guest_pages(vcpu); | |
2234 | return 0; | |
2235 | } | |
2236 | ||
2237 | int complete_pio(struct kvm_vcpu *vcpu) | |
2238 | { | |
ad312c7c | 2239 | struct kvm_pio_request *io = &vcpu->arch.pio; |
de7d789a CO |
2240 | long delta; |
2241 | int r; | |
2242 | ||
2243 | kvm_x86_ops->cache_regs(vcpu); | |
2244 | ||
2245 | if (!io->string) { | |
2246 | if (io->in) | |
ad312c7c | 2247 | memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data, |
de7d789a CO |
2248 | io->size); |
2249 | } else { | |
2250 | if (io->in) { | |
2251 | r = pio_copy_data(vcpu); | |
2252 | if (r) { | |
2253 | kvm_x86_ops->cache_regs(vcpu); | |
2254 | return r; | |
2255 | } | |
2256 | } | |
2257 | ||
2258 | delta = 1; | |
2259 | if (io->rep) { | |
2260 | delta *= io->cur_count; | |
2261 | /* | |
2262 | * The size of the register should really depend on | |
2263 | * current address size. | |
2264 | */ | |
ad312c7c | 2265 | vcpu->arch.regs[VCPU_REGS_RCX] -= delta; |
de7d789a CO |
2266 | } |
2267 | if (io->down) | |
2268 | delta = -delta; | |
2269 | delta *= io->size; | |
2270 | if (io->in) | |
ad312c7c | 2271 | vcpu->arch.regs[VCPU_REGS_RDI] += delta; |
de7d789a | 2272 | else |
ad312c7c | 2273 | vcpu->arch.regs[VCPU_REGS_RSI] += delta; |
de7d789a CO |
2274 | } |
2275 | ||
2276 | kvm_x86_ops->decache_regs(vcpu); | |
2277 | ||
2278 | io->count -= io->cur_count; | |
2279 | io->cur_count = 0; | |
2280 | ||
2281 | return 0; | |
2282 | } | |
2283 | ||
2284 | static void kernel_pio(struct kvm_io_device *pio_dev, | |
2285 | struct kvm_vcpu *vcpu, | |
2286 | void *pd) | |
2287 | { | |
2288 | /* TODO: String I/O for in kernel device */ | |
2289 | ||
2290 | mutex_lock(&vcpu->kvm->lock); | |
ad312c7c ZX |
2291 | if (vcpu->arch.pio.in) |
2292 | kvm_iodevice_read(pio_dev, vcpu->arch.pio.port, | |
2293 | vcpu->arch.pio.size, | |
de7d789a CO |
2294 | pd); |
2295 | else | |
ad312c7c ZX |
2296 | kvm_iodevice_write(pio_dev, vcpu->arch.pio.port, |
2297 | vcpu->arch.pio.size, | |
de7d789a CO |
2298 | pd); |
2299 | mutex_unlock(&vcpu->kvm->lock); | |
2300 | } | |
2301 | ||
2302 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
2303 | struct kvm_vcpu *vcpu) | |
2304 | { | |
ad312c7c ZX |
2305 | struct kvm_pio_request *io = &vcpu->arch.pio; |
2306 | void *pd = vcpu->arch.pio_data; | |
de7d789a CO |
2307 | int i; |
2308 | ||
2309 | mutex_lock(&vcpu->kvm->lock); | |
2310 | for (i = 0; i < io->cur_count; i++) { | |
2311 | kvm_iodevice_write(pio_dev, io->port, | |
2312 | io->size, | |
2313 | pd); | |
2314 | pd += io->size; | |
2315 | } | |
2316 | mutex_unlock(&vcpu->kvm->lock); | |
2317 | } | |
2318 | ||
2319 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, | |
2320 | gpa_t addr) | |
2321 | { | |
2322 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr); | |
2323 | } | |
2324 | ||
2325 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
2326 | int size, unsigned port) | |
2327 | { | |
2328 | struct kvm_io_device *pio_dev; | |
2329 | ||
2330 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
2331 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
ad312c7c | 2332 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
de7d789a | 2333 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
ad312c7c ZX |
2334 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
2335 | vcpu->run->io.port = vcpu->arch.pio.port = port; | |
2336 | vcpu->arch.pio.in = in; | |
2337 | vcpu->arch.pio.string = 0; | |
2338 | vcpu->arch.pio.down = 0; | |
2339 | vcpu->arch.pio.guest_page_offset = 0; | |
2340 | vcpu->arch.pio.rep = 0; | |
de7d789a | 2341 | |
2714d1d3 FEL |
2342 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
2343 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, | |
2344 | handler); | |
2345 | else | |
2346 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, | |
2347 | handler); | |
2348 | ||
de7d789a | 2349 | kvm_x86_ops->cache_regs(vcpu); |
ad312c7c | 2350 | memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4); |
de7d789a CO |
2351 | |
2352 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2353 | ||
2354 | pio_dev = vcpu_find_pio_dev(vcpu, port); | |
2355 | if (pio_dev) { | |
ad312c7c | 2356 | kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data); |
de7d789a CO |
2357 | complete_pio(vcpu); |
2358 | return 1; | |
2359 | } | |
2360 | return 0; | |
2361 | } | |
2362 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
2363 | ||
2364 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
2365 | int size, unsigned long count, int down, | |
2366 | gva_t address, int rep, unsigned port) | |
2367 | { | |
2368 | unsigned now, in_page; | |
2369 | int i, ret = 0; | |
2370 | int nr_pages = 1; | |
2371 | struct page *page; | |
2372 | struct kvm_io_device *pio_dev; | |
2373 | ||
2374 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
2375 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
ad312c7c | 2376 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
de7d789a | 2377 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
ad312c7c ZX |
2378 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
2379 | vcpu->run->io.port = vcpu->arch.pio.port = port; | |
2380 | vcpu->arch.pio.in = in; | |
2381 | vcpu->arch.pio.string = 1; | |
2382 | vcpu->arch.pio.down = down; | |
2383 | vcpu->arch.pio.guest_page_offset = offset_in_page(address); | |
2384 | vcpu->arch.pio.rep = rep; | |
de7d789a | 2385 | |
2714d1d3 FEL |
2386 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
2387 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, | |
2388 | handler); | |
2389 | else | |
2390 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, | |
2391 | handler); | |
2392 | ||
de7d789a CO |
2393 | if (!count) { |
2394 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2395 | return 1; | |
2396 | } | |
2397 | ||
2398 | if (!down) | |
2399 | in_page = PAGE_SIZE - offset_in_page(address); | |
2400 | else | |
2401 | in_page = offset_in_page(address) + size; | |
2402 | now = min(count, (unsigned long)in_page / size); | |
2403 | if (!now) { | |
2404 | /* | |
2405 | * String I/O straddles page boundary. Pin two guest pages | |
2406 | * so that we satisfy atomicity constraints. Do just one | |
2407 | * transaction to avoid complexity. | |
2408 | */ | |
2409 | nr_pages = 2; | |
2410 | now = 1; | |
2411 | } | |
2412 | if (down) { | |
2413 | /* | |
2414 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
2415 | */ | |
2416 | pr_unimpl(vcpu, "guest string pio down\n"); | |
c1a5d4f9 | 2417 | kvm_inject_gp(vcpu, 0); |
de7d789a CO |
2418 | return 1; |
2419 | } | |
2420 | vcpu->run->io.count = now; | |
ad312c7c | 2421 | vcpu->arch.pio.cur_count = now; |
de7d789a | 2422 | |
ad312c7c | 2423 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
de7d789a CO |
2424 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
2425 | ||
2426 | for (i = 0; i < nr_pages; ++i) { | |
de7d789a | 2427 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
ad312c7c | 2428 | vcpu->arch.pio.guest_pages[i] = page; |
de7d789a | 2429 | if (!page) { |
c1a5d4f9 | 2430 | kvm_inject_gp(vcpu, 0); |
de7d789a CO |
2431 | free_pio_guest_pages(vcpu); |
2432 | return 1; | |
2433 | } | |
2434 | } | |
2435 | ||
2436 | pio_dev = vcpu_find_pio_dev(vcpu, port); | |
ad312c7c | 2437 | if (!vcpu->arch.pio.in) { |
de7d789a CO |
2438 | /* string PIO write */ |
2439 | ret = pio_copy_data(vcpu); | |
2440 | if (ret >= 0 && pio_dev) { | |
2441 | pio_string_write(pio_dev, vcpu); | |
2442 | complete_pio(vcpu); | |
ad312c7c | 2443 | if (vcpu->arch.pio.count == 0) |
de7d789a CO |
2444 | ret = 1; |
2445 | } | |
2446 | } else if (pio_dev) | |
2447 | pr_unimpl(vcpu, "no string pio read support yet, " | |
2448 | "port %x size %d count %ld\n", | |
2449 | port, size, count); | |
2450 | ||
2451 | return ret; | |
2452 | } | |
2453 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); | |
2454 | ||
f8c16bba | 2455 | int kvm_arch_init(void *opaque) |
043405e1 | 2456 | { |
56c6d28a | 2457 | int r; |
f8c16bba ZX |
2458 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
2459 | ||
f8c16bba ZX |
2460 | if (kvm_x86_ops) { |
2461 | printk(KERN_ERR "kvm: already loaded the other module\n"); | |
56c6d28a ZX |
2462 | r = -EEXIST; |
2463 | goto out; | |
f8c16bba ZX |
2464 | } |
2465 | ||
2466 | if (!ops->cpu_has_kvm_support()) { | |
2467 | printk(KERN_ERR "kvm: no hardware support\n"); | |
56c6d28a ZX |
2468 | r = -EOPNOTSUPP; |
2469 | goto out; | |
f8c16bba ZX |
2470 | } |
2471 | if (ops->disabled_by_bios()) { | |
2472 | printk(KERN_ERR "kvm: disabled by bios\n"); | |
56c6d28a ZX |
2473 | r = -EOPNOTSUPP; |
2474 | goto out; | |
f8c16bba ZX |
2475 | } |
2476 | ||
97db56ce AK |
2477 | r = kvm_mmu_module_init(); |
2478 | if (r) | |
2479 | goto out; | |
2480 | ||
2481 | kvm_init_msr_list(); | |
2482 | ||
f8c16bba | 2483 | kvm_x86_ops = ops; |
56c6d28a | 2484 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
7b52345e SY |
2485 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
2486 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, | |
2487 | PT_DIRTY_MASK, PT64_NX_MASK, 0); | |
f8c16bba | 2488 | return 0; |
56c6d28a ZX |
2489 | |
2490 | out: | |
56c6d28a | 2491 | return r; |
043405e1 | 2492 | } |
8776e519 | 2493 | |
f8c16bba ZX |
2494 | void kvm_arch_exit(void) |
2495 | { | |
2496 | kvm_x86_ops = NULL; | |
56c6d28a ZX |
2497 | kvm_mmu_module_exit(); |
2498 | } | |
f8c16bba | 2499 | |
8776e519 HB |
2500 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
2501 | { | |
2502 | ++vcpu->stat.halt_exits; | |
2714d1d3 | 2503 | KVMTRACE_0D(HLT, vcpu, handler); |
8776e519 | 2504 | if (irqchip_in_kernel(vcpu->kvm)) { |
a4535290 | 2505 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
3200f405 | 2506 | up_read(&vcpu->kvm->slots_lock); |
8776e519 | 2507 | kvm_vcpu_block(vcpu); |
3200f405 | 2508 | down_read(&vcpu->kvm->slots_lock); |
a4535290 | 2509 | if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) |
8776e519 HB |
2510 | return -EINTR; |
2511 | return 1; | |
2512 | } else { | |
2513 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
2514 | return 0; | |
2515 | } | |
2516 | } | |
2517 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
2518 | ||
2f333bcb MT |
2519 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
2520 | unsigned long a1) | |
2521 | { | |
2522 | if (is_long_mode(vcpu)) | |
2523 | return a0; | |
2524 | else | |
2525 | return a0 | ((gpa_t)a1 << 32); | |
2526 | } | |
2527 | ||
8776e519 HB |
2528 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
2529 | { | |
2530 | unsigned long nr, a0, a1, a2, a3, ret; | |
2f333bcb | 2531 | int r = 1; |
8776e519 HB |
2532 | |
2533 | kvm_x86_ops->cache_regs(vcpu); | |
2534 | ||
ad312c7c ZX |
2535 | nr = vcpu->arch.regs[VCPU_REGS_RAX]; |
2536 | a0 = vcpu->arch.regs[VCPU_REGS_RBX]; | |
2537 | a1 = vcpu->arch.regs[VCPU_REGS_RCX]; | |
2538 | a2 = vcpu->arch.regs[VCPU_REGS_RDX]; | |
2539 | a3 = vcpu->arch.regs[VCPU_REGS_RSI]; | |
8776e519 | 2540 | |
2714d1d3 FEL |
2541 | KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler); |
2542 | ||
8776e519 HB |
2543 | if (!is_long_mode(vcpu)) { |
2544 | nr &= 0xFFFFFFFF; | |
2545 | a0 &= 0xFFFFFFFF; | |
2546 | a1 &= 0xFFFFFFFF; | |
2547 | a2 &= 0xFFFFFFFF; | |
2548 | a3 &= 0xFFFFFFFF; | |
2549 | } | |
2550 | ||
2551 | switch (nr) { | |
b93463aa AK |
2552 | case KVM_HC_VAPIC_POLL_IRQ: |
2553 | ret = 0; | |
2554 | break; | |
2f333bcb MT |
2555 | case KVM_HC_MMU_OP: |
2556 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); | |
2557 | break; | |
8776e519 HB |
2558 | default: |
2559 | ret = -KVM_ENOSYS; | |
2560 | break; | |
2561 | } | |
ad312c7c | 2562 | vcpu->arch.regs[VCPU_REGS_RAX] = ret; |
8776e519 | 2563 | kvm_x86_ops->decache_regs(vcpu); |
f11c3a8d | 2564 | ++vcpu->stat.hypercalls; |
2f333bcb | 2565 | return r; |
8776e519 HB |
2566 | } |
2567 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
2568 | ||
2569 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) | |
2570 | { | |
2571 | char instruction[3]; | |
2572 | int ret = 0; | |
2573 | ||
8776e519 HB |
2574 | |
2575 | /* | |
2576 | * Blow out the MMU to ensure that no other VCPU has an active mapping | |
2577 | * to ensure that the updated hypercall appears atomically across all | |
2578 | * VCPUs. | |
2579 | */ | |
2580 | kvm_mmu_zap_all(vcpu->kvm); | |
2581 | ||
2582 | kvm_x86_ops->cache_regs(vcpu); | |
2583 | kvm_x86_ops->patch_hypercall(vcpu, instruction); | |
ad312c7c | 2584 | if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu) |
8776e519 HB |
2585 | != X86EMUL_CONTINUE) |
2586 | ret = -EFAULT; | |
2587 | ||
8776e519 HB |
2588 | return ret; |
2589 | } | |
2590 | ||
2591 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | |
2592 | { | |
2593 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
2594 | } | |
2595 | ||
2596 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
2597 | { | |
2598 | struct descriptor_table dt = { limit, base }; | |
2599 | ||
2600 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
2601 | } | |
2602 | ||
2603 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
2604 | { | |
2605 | struct descriptor_table dt = { limit, base }; | |
2606 | ||
2607 | kvm_x86_ops->set_idt(vcpu, &dt); | |
2608 | } | |
2609 | ||
2610 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
2611 | unsigned long *rflags) | |
2612 | { | |
2d3ad1f4 | 2613 | kvm_lmsw(vcpu, msw); |
8776e519 HB |
2614 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
2615 | } | |
2616 | ||
2617 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
2618 | { | |
54e445ca JR |
2619 | unsigned long value; |
2620 | ||
8776e519 HB |
2621 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
2622 | switch (cr) { | |
2623 | case 0: | |
54e445ca JR |
2624 | value = vcpu->arch.cr0; |
2625 | break; | |
8776e519 | 2626 | case 2: |
54e445ca JR |
2627 | value = vcpu->arch.cr2; |
2628 | break; | |
8776e519 | 2629 | case 3: |
54e445ca JR |
2630 | value = vcpu->arch.cr3; |
2631 | break; | |
8776e519 | 2632 | case 4: |
54e445ca JR |
2633 | value = vcpu->arch.cr4; |
2634 | break; | |
152ff9be | 2635 | case 8: |
54e445ca JR |
2636 | value = kvm_get_cr8(vcpu); |
2637 | break; | |
8776e519 | 2638 | default: |
b8688d51 | 2639 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
8776e519 HB |
2640 | return 0; |
2641 | } | |
54e445ca JR |
2642 | KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value, |
2643 | (u32)((u64)value >> 32), handler); | |
2644 | ||
2645 | return value; | |
8776e519 HB |
2646 | } |
2647 | ||
2648 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
2649 | unsigned long *rflags) | |
2650 | { | |
54e445ca JR |
2651 | KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val, |
2652 | (u32)((u64)val >> 32), handler); | |
2653 | ||
8776e519 HB |
2654 | switch (cr) { |
2655 | case 0: | |
2d3ad1f4 | 2656 | kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val)); |
8776e519 HB |
2657 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
2658 | break; | |
2659 | case 2: | |
ad312c7c | 2660 | vcpu->arch.cr2 = val; |
8776e519 HB |
2661 | break; |
2662 | case 3: | |
2d3ad1f4 | 2663 | kvm_set_cr3(vcpu, val); |
8776e519 HB |
2664 | break; |
2665 | case 4: | |
2d3ad1f4 | 2666 | kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val)); |
8776e519 | 2667 | break; |
152ff9be | 2668 | case 8: |
2d3ad1f4 | 2669 | kvm_set_cr8(vcpu, val & 0xfUL); |
152ff9be | 2670 | break; |
8776e519 | 2671 | default: |
b8688d51 | 2672 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
8776e519 HB |
2673 | } |
2674 | } | |
2675 | ||
07716717 DK |
2676 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
2677 | { | |
ad312c7c ZX |
2678 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
2679 | int j, nent = vcpu->arch.cpuid_nent; | |
07716717 DK |
2680 | |
2681 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; | |
2682 | /* when no next entry is found, the current entry[i] is reselected */ | |
2683 | for (j = i + 1; j == i; j = (j + 1) % nent) { | |
ad312c7c | 2684 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
07716717 DK |
2685 | if (ej->function == e->function) { |
2686 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
2687 | return j; | |
2688 | } | |
2689 | } | |
2690 | return 0; /* silence gcc, even though control never reaches here */ | |
2691 | } | |
2692 | ||
2693 | /* find an entry with matching function, matching index (if needed), and that | |
2694 | * should be read next (if it's stateful) */ | |
2695 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |
2696 | u32 function, u32 index) | |
2697 | { | |
2698 | if (e->function != function) | |
2699 | return 0; | |
2700 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | |
2701 | return 0; | |
2702 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | |
2703 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) | |
2704 | return 0; | |
2705 | return 1; | |
2706 | } | |
2707 | ||
8776e519 HB |
2708 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
2709 | { | |
2710 | int i; | |
07716717 DK |
2711 | u32 function, index; |
2712 | struct kvm_cpuid_entry2 *e, *best; | |
8776e519 HB |
2713 | |
2714 | kvm_x86_ops->cache_regs(vcpu); | |
ad312c7c ZX |
2715 | function = vcpu->arch.regs[VCPU_REGS_RAX]; |
2716 | index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
2717 | vcpu->arch.regs[VCPU_REGS_RAX] = 0; | |
2718 | vcpu->arch.regs[VCPU_REGS_RBX] = 0; | |
2719 | vcpu->arch.regs[VCPU_REGS_RCX] = 0; | |
2720 | vcpu->arch.regs[VCPU_REGS_RDX] = 0; | |
8776e519 | 2721 | best = NULL; |
ad312c7c ZX |
2722 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
2723 | e = &vcpu->arch.cpuid_entries[i]; | |
07716717 DK |
2724 | if (is_matching_cpuid_entry(e, function, index)) { |
2725 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | |
2726 | move_to_next_stateful_cpuid_entry(vcpu, i); | |
8776e519 HB |
2727 | best = e; |
2728 | break; | |
2729 | } | |
2730 | /* | |
2731 | * Both basic or both extended? | |
2732 | */ | |
2733 | if (((e->function ^ function) & 0x80000000) == 0) | |
2734 | if (!best || e->function > best->function) | |
2735 | best = e; | |
2736 | } | |
2737 | if (best) { | |
ad312c7c ZX |
2738 | vcpu->arch.regs[VCPU_REGS_RAX] = best->eax; |
2739 | vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx; | |
2740 | vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx; | |
2741 | vcpu->arch.regs[VCPU_REGS_RDX] = best->edx; | |
8776e519 HB |
2742 | } |
2743 | kvm_x86_ops->decache_regs(vcpu); | |
2744 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2714d1d3 FEL |
2745 | KVMTRACE_5D(CPUID, vcpu, function, |
2746 | (u32)vcpu->arch.regs[VCPU_REGS_RAX], | |
2747 | (u32)vcpu->arch.regs[VCPU_REGS_RBX], | |
2748 | (u32)vcpu->arch.regs[VCPU_REGS_RCX], | |
2749 | (u32)vcpu->arch.regs[VCPU_REGS_RDX], handler); | |
8776e519 HB |
2750 | } |
2751 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
d0752060 | 2752 | |
b6c7a5dc HB |
2753 | /* |
2754 | * Check if userspace requested an interrupt window, and that the | |
2755 | * interrupt window is open. | |
2756 | * | |
2757 | * No need to exit to userspace if we already have an interrupt queued. | |
2758 | */ | |
2759 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
2760 | struct kvm_run *kvm_run) | |
2761 | { | |
ad312c7c | 2762 | return (!vcpu->arch.irq_summary && |
b6c7a5dc | 2763 | kvm_run->request_interrupt_window && |
ad312c7c | 2764 | vcpu->arch.interrupt_window_open && |
b6c7a5dc HB |
2765 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); |
2766 | } | |
2767 | ||
2768 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
2769 | struct kvm_run *kvm_run) | |
2770 | { | |
2771 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
2d3ad1f4 | 2772 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc HB |
2773 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
2774 | if (irqchip_in_kernel(vcpu->kvm)) | |
2775 | kvm_run->ready_for_interrupt_injection = 1; | |
2776 | else | |
2777 | kvm_run->ready_for_interrupt_injection = | |
ad312c7c ZX |
2778 | (vcpu->arch.interrupt_window_open && |
2779 | vcpu->arch.irq_summary == 0); | |
b6c7a5dc HB |
2780 | } |
2781 | ||
b93463aa AK |
2782 | static void vapic_enter(struct kvm_vcpu *vcpu) |
2783 | { | |
2784 | struct kvm_lapic *apic = vcpu->arch.apic; | |
2785 | struct page *page; | |
2786 | ||
2787 | if (!apic || !apic->vapic_addr) | |
2788 | return; | |
2789 | ||
10589a46 | 2790 | down_read(¤t->mm->mmap_sem); |
b93463aa | 2791 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
10589a46 | 2792 | up_read(¤t->mm->mmap_sem); |
72dc67a6 IE |
2793 | |
2794 | vcpu->arch.apic->vapic_page = page; | |
b93463aa AK |
2795 | } |
2796 | ||
2797 | static void vapic_exit(struct kvm_vcpu *vcpu) | |
2798 | { | |
2799 | struct kvm_lapic *apic = vcpu->arch.apic; | |
2800 | ||
2801 | if (!apic || !apic->vapic_addr) | |
2802 | return; | |
2803 | ||
2804 | kvm_release_page_dirty(apic->vapic_page); | |
2805 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); | |
2806 | } | |
2807 | ||
b6c7a5dc HB |
2808 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
2809 | { | |
2810 | int r; | |
2811 | ||
a4535290 | 2812 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
b6c7a5dc | 2813 | pr_debug("vcpu %d received sipi with vector # %x\n", |
ad312c7c | 2814 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
b6c7a5dc HB |
2815 | kvm_lapic_reset(vcpu); |
2816 | r = kvm_x86_ops->vcpu_reset(vcpu); | |
2817 | if (r) | |
2818 | return r; | |
a4535290 | 2819 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
b6c7a5dc HB |
2820 | } |
2821 | ||
3200f405 | 2822 | down_read(&vcpu->kvm->slots_lock); |
b93463aa AK |
2823 | vapic_enter(vcpu); |
2824 | ||
b6c7a5dc HB |
2825 | preempted: |
2826 | if (vcpu->guest_debug.enabled) | |
2827 | kvm_x86_ops->guest_debug_pre(vcpu); | |
2828 | ||
2829 | again: | |
2e53d63a MT |
2830 | if (vcpu->requests) |
2831 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) | |
2832 | kvm_mmu_unload(vcpu); | |
2833 | ||
b6c7a5dc HB |
2834 | r = kvm_mmu_reload(vcpu); |
2835 | if (unlikely(r)) | |
2836 | goto out; | |
2837 | ||
2f52d58c AK |
2838 | if (vcpu->requests) { |
2839 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) | |
2f599714 | 2840 | __kvm_migrate_timers(vcpu); |
d4acf7e7 MT |
2841 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
2842 | kvm_x86_ops->tlb_flush(vcpu); | |
b93463aa AK |
2843 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
2844 | &vcpu->requests)) { | |
2845 | kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS; | |
2846 | r = 0; | |
2847 | goto out; | |
2848 | } | |
71c4dfaf JR |
2849 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
2850 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; | |
2851 | r = 0; | |
2852 | goto out; | |
2853 | } | |
2f52d58c | 2854 | } |
b93463aa | 2855 | |
06e05645 | 2856 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
b6c7a5dc HB |
2857 | kvm_inject_pending_timer_irqs(vcpu); |
2858 | ||
2859 | preempt_disable(); | |
2860 | ||
2861 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
2862 | kvm_load_guest_fpu(vcpu); | |
2863 | ||
2864 | local_irq_disable(); | |
2865 | ||
d4acf7e7 | 2866 | if (vcpu->requests || need_resched()) { |
6c142801 AK |
2867 | local_irq_enable(); |
2868 | preempt_enable(); | |
2869 | r = 1; | |
2870 | goto out; | |
2871 | } | |
2872 | ||
b6c7a5dc HB |
2873 | if (signal_pending(current)) { |
2874 | local_irq_enable(); | |
2875 | preempt_enable(); | |
2876 | r = -EINTR; | |
2877 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2878 | ++vcpu->stat.signal_exits; | |
2879 | goto out; | |
2880 | } | |
2881 | ||
e9571ed5 MT |
2882 | vcpu->guest_mode = 1; |
2883 | /* | |
2884 | * Make sure that guest_mode assignment won't happen after | |
2885 | * testing the pending IRQ vector bitmap. | |
2886 | */ | |
2887 | smp_wmb(); | |
2888 | ||
ad312c7c | 2889 | if (vcpu->arch.exception.pending) |
298101da AK |
2890 | __queue_exception(vcpu); |
2891 | else if (irqchip_in_kernel(vcpu->kvm)) | |
b6c7a5dc | 2892 | kvm_x86_ops->inject_pending_irq(vcpu); |
eb9774f0 | 2893 | else |
b6c7a5dc HB |
2894 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
2895 | ||
b93463aa AK |
2896 | kvm_lapic_sync_to_vapic(vcpu); |
2897 | ||
3200f405 MT |
2898 | up_read(&vcpu->kvm->slots_lock); |
2899 | ||
b6c7a5dc HB |
2900 | kvm_guest_enter(); |
2901 | ||
b6c7a5dc | 2902 | |
2714d1d3 | 2903 | KVMTRACE_0D(VMENTRY, vcpu, entryexit); |
b6c7a5dc HB |
2904 | kvm_x86_ops->run(vcpu, kvm_run); |
2905 | ||
2906 | vcpu->guest_mode = 0; | |
2907 | local_irq_enable(); | |
2908 | ||
2909 | ++vcpu->stat.exits; | |
2910 | ||
2911 | /* | |
2912 | * We must have an instruction between local_irq_enable() and | |
2913 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
2914 | * the interrupt shadow. The stat.exits increment will do nicely. | |
2915 | * But we need to prevent reordering, hence this barrier(): | |
2916 | */ | |
2917 | barrier(); | |
2918 | ||
2919 | kvm_guest_exit(); | |
2920 | ||
2921 | preempt_enable(); | |
2922 | ||
3200f405 MT |
2923 | down_read(&vcpu->kvm->slots_lock); |
2924 | ||
b6c7a5dc HB |
2925 | /* |
2926 | * Profile KVM exit RIPs: | |
2927 | */ | |
2928 | if (unlikely(prof_on == KVM_PROFILING)) { | |
2929 | kvm_x86_ops->cache_regs(vcpu); | |
ad312c7c | 2930 | profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip); |
b6c7a5dc HB |
2931 | } |
2932 | ||
ad312c7c ZX |
2933 | if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu)) |
2934 | vcpu->arch.exception.pending = false; | |
298101da | 2935 | |
b93463aa AK |
2936 | kvm_lapic_sync_from_vapic(vcpu); |
2937 | ||
b6c7a5dc HB |
2938 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
2939 | ||
2940 | if (r > 0) { | |
2941 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
2942 | r = -EINTR; | |
2943 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2944 | ++vcpu->stat.request_irq_exits; | |
2945 | goto out; | |
2946 | } | |
e1beb1d3 | 2947 | if (!need_resched()) |
b6c7a5dc | 2948 | goto again; |
b6c7a5dc HB |
2949 | } |
2950 | ||
2951 | out: | |
3200f405 | 2952 | up_read(&vcpu->kvm->slots_lock); |
b6c7a5dc HB |
2953 | if (r > 0) { |
2954 | kvm_resched(vcpu); | |
3200f405 | 2955 | down_read(&vcpu->kvm->slots_lock); |
b6c7a5dc HB |
2956 | goto preempted; |
2957 | } | |
2958 | ||
2959 | post_kvm_run_save(vcpu, kvm_run); | |
2960 | ||
3200f405 | 2961 | down_read(&vcpu->kvm->slots_lock); |
b93463aa | 2962 | vapic_exit(vcpu); |
3200f405 | 2963 | up_read(&vcpu->kvm->slots_lock); |
b93463aa | 2964 | |
b6c7a5dc HB |
2965 | return r; |
2966 | } | |
2967 | ||
2968 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2969 | { | |
2970 | int r; | |
2971 | sigset_t sigsaved; | |
2972 | ||
2973 | vcpu_load(vcpu); | |
2974 | ||
a4535290 | 2975 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
b6c7a5dc HB |
2976 | kvm_vcpu_block(vcpu); |
2977 | vcpu_put(vcpu); | |
2978 | return -EAGAIN; | |
2979 | } | |
2980 | ||
2981 | if (vcpu->sigset_active) | |
2982 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
2983 | ||
2984 | /* re-sync apic's tpr */ | |
2985 | if (!irqchip_in_kernel(vcpu->kvm)) | |
2d3ad1f4 | 2986 | kvm_set_cr8(vcpu, kvm_run->cr8); |
b6c7a5dc | 2987 | |
ad312c7c | 2988 | if (vcpu->arch.pio.cur_count) { |
b6c7a5dc HB |
2989 | r = complete_pio(vcpu); |
2990 | if (r) | |
2991 | goto out; | |
2992 | } | |
2993 | #if CONFIG_HAS_IOMEM | |
2994 | if (vcpu->mmio_needed) { | |
2995 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
2996 | vcpu->mmio_read_completed = 1; | |
2997 | vcpu->mmio_needed = 0; | |
3200f405 MT |
2998 | |
2999 | down_read(&vcpu->kvm->slots_lock); | |
b6c7a5dc | 3000 | r = emulate_instruction(vcpu, kvm_run, |
571008da SY |
3001 | vcpu->arch.mmio_fault_cr2, 0, |
3002 | EMULTYPE_NO_DECODE); | |
3200f405 | 3003 | up_read(&vcpu->kvm->slots_lock); |
b6c7a5dc HB |
3004 | if (r == EMULATE_DO_MMIO) { |
3005 | /* | |
3006 | * Read-modify-write. Back to userspace. | |
3007 | */ | |
3008 | r = 0; | |
3009 | goto out; | |
3010 | } | |
3011 | } | |
3012 | #endif | |
3013 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) { | |
3014 | kvm_x86_ops->cache_regs(vcpu); | |
ad312c7c | 3015 | vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret; |
b6c7a5dc HB |
3016 | kvm_x86_ops->decache_regs(vcpu); |
3017 | } | |
3018 | ||
3019 | r = __vcpu_run(vcpu, kvm_run); | |
3020 | ||
3021 | out: | |
3022 | if (vcpu->sigset_active) | |
3023 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
3024 | ||
3025 | vcpu_put(vcpu); | |
3026 | return r; | |
3027 | } | |
3028 | ||
3029 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
3030 | { | |
3031 | vcpu_load(vcpu); | |
3032 | ||
3033 | kvm_x86_ops->cache_regs(vcpu); | |
3034 | ||
ad312c7c ZX |
3035 | regs->rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
3036 | regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX]; | |
3037 | regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX]; | |
3038 | regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX]; | |
3039 | regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI]; | |
3040 | regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI]; | |
3041 | regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
3042 | regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP]; | |
b6c7a5dc | 3043 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
3044 | regs->r8 = vcpu->arch.regs[VCPU_REGS_R8]; |
3045 | regs->r9 = vcpu->arch.regs[VCPU_REGS_R9]; | |
3046 | regs->r10 = vcpu->arch.regs[VCPU_REGS_R10]; | |
3047 | regs->r11 = vcpu->arch.regs[VCPU_REGS_R11]; | |
3048 | regs->r12 = vcpu->arch.regs[VCPU_REGS_R12]; | |
3049 | regs->r13 = vcpu->arch.regs[VCPU_REGS_R13]; | |
3050 | regs->r14 = vcpu->arch.regs[VCPU_REGS_R14]; | |
3051 | regs->r15 = vcpu->arch.regs[VCPU_REGS_R15]; | |
b6c7a5dc HB |
3052 | #endif |
3053 | ||
ad312c7c | 3054 | regs->rip = vcpu->arch.rip; |
b6c7a5dc HB |
3055 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
3056 | ||
3057 | /* | |
3058 | * Don't leak debug flags in case they were set for guest debugging | |
3059 | */ | |
3060 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) | |
3061 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
3062 | ||
3063 | vcpu_put(vcpu); | |
3064 | ||
3065 | return 0; | |
3066 | } | |
3067 | ||
3068 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
3069 | { | |
3070 | vcpu_load(vcpu); | |
3071 | ||
ad312c7c ZX |
3072 | vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax; |
3073 | vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx; | |
3074 | vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx; | |
3075 | vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx; | |
3076 | vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi; | |
3077 | vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi; | |
3078 | vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp; | |
3079 | vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp; | |
b6c7a5dc | 3080 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
3081 | vcpu->arch.regs[VCPU_REGS_R8] = regs->r8; |
3082 | vcpu->arch.regs[VCPU_REGS_R9] = regs->r9; | |
3083 | vcpu->arch.regs[VCPU_REGS_R10] = regs->r10; | |
3084 | vcpu->arch.regs[VCPU_REGS_R11] = regs->r11; | |
3085 | vcpu->arch.regs[VCPU_REGS_R12] = regs->r12; | |
3086 | vcpu->arch.regs[VCPU_REGS_R13] = regs->r13; | |
3087 | vcpu->arch.regs[VCPU_REGS_R14] = regs->r14; | |
3088 | vcpu->arch.regs[VCPU_REGS_R15] = regs->r15; | |
b6c7a5dc HB |
3089 | #endif |
3090 | ||
ad312c7c | 3091 | vcpu->arch.rip = regs->rip; |
b6c7a5dc HB |
3092 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
3093 | ||
3094 | kvm_x86_ops->decache_regs(vcpu); | |
3095 | ||
b4f14abd JK |
3096 | vcpu->arch.exception.pending = false; |
3097 | ||
b6c7a5dc HB |
3098 | vcpu_put(vcpu); |
3099 | ||
3100 | return 0; | |
3101 | } | |
3102 | ||
3103 | static void get_segment(struct kvm_vcpu *vcpu, | |
3104 | struct kvm_segment *var, int seg) | |
3105 | { | |
14af3f3c | 3106 | kvm_x86_ops->get_segment(vcpu, var, seg); |
b6c7a5dc HB |
3107 | } |
3108 | ||
3109 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) | |
3110 | { | |
3111 | struct kvm_segment cs; | |
3112 | ||
3113 | get_segment(vcpu, &cs, VCPU_SREG_CS); | |
3114 | *db = cs.db; | |
3115 | *l = cs.l; | |
3116 | } | |
3117 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
3118 | ||
3119 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
3120 | struct kvm_sregs *sregs) | |
3121 | { | |
3122 | struct descriptor_table dt; | |
3123 | int pending_vec; | |
3124 | ||
3125 | vcpu_load(vcpu); | |
3126 | ||
3127 | get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
3128 | get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
3129 | get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
3130 | get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
3131 | get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
3132 | get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
3133 | ||
3134 | get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
3135 | get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
3136 | ||
3137 | kvm_x86_ops->get_idt(vcpu, &dt); | |
3138 | sregs->idt.limit = dt.limit; | |
3139 | sregs->idt.base = dt.base; | |
3140 | kvm_x86_ops->get_gdt(vcpu, &dt); | |
3141 | sregs->gdt.limit = dt.limit; | |
3142 | sregs->gdt.base = dt.base; | |
3143 | ||
3144 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
ad312c7c ZX |
3145 | sregs->cr0 = vcpu->arch.cr0; |
3146 | sregs->cr2 = vcpu->arch.cr2; | |
3147 | sregs->cr3 = vcpu->arch.cr3; | |
3148 | sregs->cr4 = vcpu->arch.cr4; | |
2d3ad1f4 | 3149 | sregs->cr8 = kvm_get_cr8(vcpu); |
ad312c7c | 3150 | sregs->efer = vcpu->arch.shadow_efer; |
b6c7a5dc HB |
3151 | sregs->apic_base = kvm_get_apic_base(vcpu); |
3152 | ||
3153 | if (irqchip_in_kernel(vcpu->kvm)) { | |
3154 | memset(sregs->interrupt_bitmap, 0, | |
3155 | sizeof sregs->interrupt_bitmap); | |
3156 | pending_vec = kvm_x86_ops->get_irq(vcpu); | |
3157 | if (pending_vec >= 0) | |
3158 | set_bit(pending_vec, | |
3159 | (unsigned long *)sregs->interrupt_bitmap); | |
3160 | } else | |
ad312c7c | 3161 | memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending, |
b6c7a5dc HB |
3162 | sizeof sregs->interrupt_bitmap); |
3163 | ||
3164 | vcpu_put(vcpu); | |
3165 | ||
3166 | return 0; | |
3167 | } | |
3168 | ||
62d9f0db MT |
3169 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
3170 | struct kvm_mp_state *mp_state) | |
3171 | { | |
3172 | vcpu_load(vcpu); | |
3173 | mp_state->mp_state = vcpu->arch.mp_state; | |
3174 | vcpu_put(vcpu); | |
3175 | return 0; | |
3176 | } | |
3177 | ||
3178 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
3179 | struct kvm_mp_state *mp_state) | |
3180 | { | |
3181 | vcpu_load(vcpu); | |
3182 | vcpu->arch.mp_state = mp_state->mp_state; | |
3183 | vcpu_put(vcpu); | |
3184 | return 0; | |
3185 | } | |
3186 | ||
b6c7a5dc HB |
3187 | static void set_segment(struct kvm_vcpu *vcpu, |
3188 | struct kvm_segment *var, int seg) | |
3189 | { | |
14af3f3c | 3190 | kvm_x86_ops->set_segment(vcpu, var, seg); |
b6c7a5dc HB |
3191 | } |
3192 | ||
37817f29 IE |
3193 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
3194 | struct kvm_segment *kvm_desct) | |
3195 | { | |
3196 | kvm_desct->base = seg_desc->base0; | |
3197 | kvm_desct->base |= seg_desc->base1 << 16; | |
3198 | kvm_desct->base |= seg_desc->base2 << 24; | |
3199 | kvm_desct->limit = seg_desc->limit0; | |
3200 | kvm_desct->limit |= seg_desc->limit << 16; | |
3201 | kvm_desct->selector = selector; | |
3202 | kvm_desct->type = seg_desc->type; | |
3203 | kvm_desct->present = seg_desc->p; | |
3204 | kvm_desct->dpl = seg_desc->dpl; | |
3205 | kvm_desct->db = seg_desc->d; | |
3206 | kvm_desct->s = seg_desc->s; | |
3207 | kvm_desct->l = seg_desc->l; | |
3208 | kvm_desct->g = seg_desc->g; | |
3209 | kvm_desct->avl = seg_desc->avl; | |
3210 | if (!selector) | |
3211 | kvm_desct->unusable = 1; | |
3212 | else | |
3213 | kvm_desct->unusable = 0; | |
3214 | kvm_desct->padding = 0; | |
3215 | } | |
3216 | ||
3217 | static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu, | |
3218 | u16 selector, | |
3219 | struct descriptor_table *dtable) | |
3220 | { | |
3221 | if (selector & 1 << 2) { | |
3222 | struct kvm_segment kvm_seg; | |
3223 | ||
3224 | get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); | |
3225 | ||
3226 | if (kvm_seg.unusable) | |
3227 | dtable->limit = 0; | |
3228 | else | |
3229 | dtable->limit = kvm_seg.limit; | |
3230 | dtable->base = kvm_seg.base; | |
3231 | } | |
3232 | else | |
3233 | kvm_x86_ops->get_gdt(vcpu, dtable); | |
3234 | } | |
3235 | ||
3236 | /* allowed just for 8 bytes segments */ | |
3237 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, | |
3238 | struct desc_struct *seg_desc) | |
3239 | { | |
3240 | struct descriptor_table dtable; | |
3241 | u16 index = selector >> 3; | |
3242 | ||
3243 | get_segment_descritptor_dtable(vcpu, selector, &dtable); | |
3244 | ||
3245 | if (dtable.limit < index * 8 + 7) { | |
3246 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); | |
3247 | return 1; | |
3248 | } | |
3249 | return kvm_read_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8); | |
3250 | } | |
3251 | ||
3252 | /* allowed just for 8 bytes segments */ | |
3253 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, | |
3254 | struct desc_struct *seg_desc) | |
3255 | { | |
3256 | struct descriptor_table dtable; | |
3257 | u16 index = selector >> 3; | |
3258 | ||
3259 | get_segment_descritptor_dtable(vcpu, selector, &dtable); | |
3260 | ||
3261 | if (dtable.limit < index * 8 + 7) | |
3262 | return 1; | |
3263 | return kvm_write_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8); | |
3264 | } | |
3265 | ||
3266 | static u32 get_tss_base_addr(struct kvm_vcpu *vcpu, | |
3267 | struct desc_struct *seg_desc) | |
3268 | { | |
3269 | u32 base_addr; | |
3270 | ||
3271 | base_addr = seg_desc->base0; | |
3272 | base_addr |= (seg_desc->base1 << 16); | |
3273 | base_addr |= (seg_desc->base2 << 24); | |
3274 | ||
3275 | return base_addr; | |
3276 | } | |
3277 | ||
3278 | static int load_tss_segment32(struct kvm_vcpu *vcpu, | |
3279 | struct desc_struct *seg_desc, | |
3280 | struct tss_segment_32 *tss) | |
3281 | { | |
3282 | u32 base_addr; | |
3283 | ||
3284 | base_addr = get_tss_base_addr(vcpu, seg_desc); | |
3285 | ||
3286 | return kvm_read_guest(vcpu->kvm, base_addr, tss, | |
3287 | sizeof(struct tss_segment_32)); | |
3288 | } | |
3289 | ||
3290 | static int save_tss_segment32(struct kvm_vcpu *vcpu, | |
3291 | struct desc_struct *seg_desc, | |
3292 | struct tss_segment_32 *tss) | |
3293 | { | |
3294 | u32 base_addr; | |
3295 | ||
3296 | base_addr = get_tss_base_addr(vcpu, seg_desc); | |
3297 | ||
3298 | return kvm_write_guest(vcpu->kvm, base_addr, tss, | |
3299 | sizeof(struct tss_segment_32)); | |
3300 | } | |
3301 | ||
3302 | static int load_tss_segment16(struct kvm_vcpu *vcpu, | |
3303 | struct desc_struct *seg_desc, | |
3304 | struct tss_segment_16 *tss) | |
3305 | { | |
3306 | u32 base_addr; | |
3307 | ||
3308 | base_addr = get_tss_base_addr(vcpu, seg_desc); | |
3309 | ||
3310 | return kvm_read_guest(vcpu->kvm, base_addr, tss, | |
3311 | sizeof(struct tss_segment_16)); | |
3312 | } | |
3313 | ||
3314 | static int save_tss_segment16(struct kvm_vcpu *vcpu, | |
3315 | struct desc_struct *seg_desc, | |
3316 | struct tss_segment_16 *tss) | |
3317 | { | |
3318 | u32 base_addr; | |
3319 | ||
3320 | base_addr = get_tss_base_addr(vcpu, seg_desc); | |
3321 | ||
3322 | return kvm_write_guest(vcpu->kvm, base_addr, tss, | |
3323 | sizeof(struct tss_segment_16)); | |
3324 | } | |
3325 | ||
3326 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) | |
3327 | { | |
3328 | struct kvm_segment kvm_seg; | |
3329 | ||
3330 | get_segment(vcpu, &kvm_seg, seg); | |
3331 | return kvm_seg.selector; | |
3332 | } | |
3333 | ||
3334 | static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu, | |
3335 | u16 selector, | |
3336 | struct kvm_segment *kvm_seg) | |
3337 | { | |
3338 | struct desc_struct seg_desc; | |
3339 | ||
3340 | if (load_guest_segment_descriptor(vcpu, selector, &seg_desc)) | |
3341 | return 1; | |
3342 | seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg); | |
3343 | return 0; | |
3344 | } | |
3345 | ||
3346 | static int load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, | |
3347 | int type_bits, int seg) | |
3348 | { | |
3349 | struct kvm_segment kvm_seg; | |
3350 | ||
3351 | if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg)) | |
3352 | return 1; | |
3353 | kvm_seg.type |= type_bits; | |
3354 | ||
3355 | if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS && | |
3356 | seg != VCPU_SREG_LDTR) | |
3357 | if (!kvm_seg.s) | |
3358 | kvm_seg.unusable = 1; | |
3359 | ||
3360 | set_segment(vcpu, &kvm_seg, seg); | |
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, | |
3365 | struct tss_segment_32 *tss) | |
3366 | { | |
3367 | tss->cr3 = vcpu->arch.cr3; | |
3368 | tss->eip = vcpu->arch.rip; | |
3369 | tss->eflags = kvm_x86_ops->get_rflags(vcpu); | |
3370 | tss->eax = vcpu->arch.regs[VCPU_REGS_RAX]; | |
3371 | tss->ecx = vcpu->arch.regs[VCPU_REGS_RCX]; | |
3372 | tss->edx = vcpu->arch.regs[VCPU_REGS_RDX]; | |
3373 | tss->ebx = vcpu->arch.regs[VCPU_REGS_RBX]; | |
3374 | tss->esp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
3375 | tss->ebp = vcpu->arch.regs[VCPU_REGS_RBP]; | |
3376 | tss->esi = vcpu->arch.regs[VCPU_REGS_RSI]; | |
3377 | tss->edi = vcpu->arch.regs[VCPU_REGS_RDI]; | |
3378 | ||
3379 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); | |
3380 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); | |
3381 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); | |
3382 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); | |
3383 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); | |
3384 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); | |
3385 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); | |
3386 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); | |
3387 | } | |
3388 | ||
3389 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, | |
3390 | struct tss_segment_32 *tss) | |
3391 | { | |
3392 | kvm_set_cr3(vcpu, tss->cr3); | |
3393 | ||
3394 | vcpu->arch.rip = tss->eip; | |
3395 | kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2); | |
3396 | ||
3397 | vcpu->arch.regs[VCPU_REGS_RAX] = tss->eax; | |
3398 | vcpu->arch.regs[VCPU_REGS_RCX] = tss->ecx; | |
3399 | vcpu->arch.regs[VCPU_REGS_RDX] = tss->edx; | |
3400 | vcpu->arch.regs[VCPU_REGS_RBX] = tss->ebx; | |
3401 | vcpu->arch.regs[VCPU_REGS_RSP] = tss->esp; | |
3402 | vcpu->arch.regs[VCPU_REGS_RBP] = tss->ebp; | |
3403 | vcpu->arch.regs[VCPU_REGS_RSI] = tss->esi; | |
3404 | vcpu->arch.regs[VCPU_REGS_RDI] = tss->edi; | |
3405 | ||
3406 | if (load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR)) | |
3407 | return 1; | |
3408 | ||
3409 | if (load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) | |
3410 | return 1; | |
3411 | ||
3412 | if (load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) | |
3413 | return 1; | |
3414 | ||
3415 | if (load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) | |
3416 | return 1; | |
3417 | ||
3418 | if (load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) | |
3419 | return 1; | |
3420 | ||
3421 | if (load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS)) | |
3422 | return 1; | |
3423 | ||
3424 | if (load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS)) | |
3425 | return 1; | |
3426 | return 0; | |
3427 | } | |
3428 | ||
3429 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, | |
3430 | struct tss_segment_16 *tss) | |
3431 | { | |
3432 | tss->ip = vcpu->arch.rip; | |
3433 | tss->flag = kvm_x86_ops->get_rflags(vcpu); | |
3434 | tss->ax = vcpu->arch.regs[VCPU_REGS_RAX]; | |
3435 | tss->cx = vcpu->arch.regs[VCPU_REGS_RCX]; | |
3436 | tss->dx = vcpu->arch.regs[VCPU_REGS_RDX]; | |
3437 | tss->bx = vcpu->arch.regs[VCPU_REGS_RBX]; | |
3438 | tss->sp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
3439 | tss->bp = vcpu->arch.regs[VCPU_REGS_RBP]; | |
3440 | tss->si = vcpu->arch.regs[VCPU_REGS_RSI]; | |
3441 | tss->di = vcpu->arch.regs[VCPU_REGS_RDI]; | |
3442 | ||
3443 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); | |
3444 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); | |
3445 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); | |
3446 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); | |
3447 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); | |
3448 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); | |
3449 | } | |
3450 | ||
3451 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, | |
3452 | struct tss_segment_16 *tss) | |
3453 | { | |
3454 | vcpu->arch.rip = tss->ip; | |
3455 | kvm_x86_ops->set_rflags(vcpu, tss->flag | 2); | |
3456 | vcpu->arch.regs[VCPU_REGS_RAX] = tss->ax; | |
3457 | vcpu->arch.regs[VCPU_REGS_RCX] = tss->cx; | |
3458 | vcpu->arch.regs[VCPU_REGS_RDX] = tss->dx; | |
3459 | vcpu->arch.regs[VCPU_REGS_RBX] = tss->bx; | |
3460 | vcpu->arch.regs[VCPU_REGS_RSP] = tss->sp; | |
3461 | vcpu->arch.regs[VCPU_REGS_RBP] = tss->bp; | |
3462 | vcpu->arch.regs[VCPU_REGS_RSI] = tss->si; | |
3463 | vcpu->arch.regs[VCPU_REGS_RDI] = tss->di; | |
3464 | ||
3465 | if (load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR)) | |
3466 | return 1; | |
3467 | ||
3468 | if (load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) | |
3469 | return 1; | |
3470 | ||
3471 | if (load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) | |
3472 | return 1; | |
3473 | ||
3474 | if (load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) | |
3475 | return 1; | |
3476 | ||
3477 | if (load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) | |
3478 | return 1; | |
3479 | return 0; | |
3480 | } | |
3481 | ||
8b2cf73c | 3482 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
37817f29 IE |
3483 | struct desc_struct *cseg_desc, |
3484 | struct desc_struct *nseg_desc) | |
3485 | { | |
3486 | struct tss_segment_16 tss_segment_16; | |
3487 | int ret = 0; | |
3488 | ||
3489 | if (load_tss_segment16(vcpu, cseg_desc, &tss_segment_16)) | |
3490 | goto out; | |
3491 | ||
3492 | save_state_to_tss16(vcpu, &tss_segment_16); | |
3493 | save_tss_segment16(vcpu, cseg_desc, &tss_segment_16); | |
3494 | ||
3495 | if (load_tss_segment16(vcpu, nseg_desc, &tss_segment_16)) | |
3496 | goto out; | |
3497 | if (load_state_from_tss16(vcpu, &tss_segment_16)) | |
3498 | goto out; | |
3499 | ||
3500 | ret = 1; | |
3501 | out: | |
3502 | return ret; | |
3503 | } | |
3504 | ||
8b2cf73c | 3505 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
37817f29 IE |
3506 | struct desc_struct *cseg_desc, |
3507 | struct desc_struct *nseg_desc) | |
3508 | { | |
3509 | struct tss_segment_32 tss_segment_32; | |
3510 | int ret = 0; | |
3511 | ||
3512 | if (load_tss_segment32(vcpu, cseg_desc, &tss_segment_32)) | |
3513 | goto out; | |
3514 | ||
3515 | save_state_to_tss32(vcpu, &tss_segment_32); | |
3516 | save_tss_segment32(vcpu, cseg_desc, &tss_segment_32); | |
3517 | ||
3518 | if (load_tss_segment32(vcpu, nseg_desc, &tss_segment_32)) | |
3519 | goto out; | |
3520 | if (load_state_from_tss32(vcpu, &tss_segment_32)) | |
3521 | goto out; | |
3522 | ||
3523 | ret = 1; | |
3524 | out: | |
3525 | return ret; | |
3526 | } | |
3527 | ||
3528 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) | |
3529 | { | |
3530 | struct kvm_segment tr_seg; | |
3531 | struct desc_struct cseg_desc; | |
3532 | struct desc_struct nseg_desc; | |
3533 | int ret = 0; | |
3534 | ||
3535 | get_segment(vcpu, &tr_seg, VCPU_SREG_TR); | |
3536 | ||
3537 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) | |
3538 | goto out; | |
3539 | ||
3540 | if (load_guest_segment_descriptor(vcpu, tr_seg.selector, &cseg_desc)) | |
3541 | goto out; | |
3542 | ||
3543 | ||
3544 | if (reason != TASK_SWITCH_IRET) { | |
3545 | int cpl; | |
3546 | ||
3547 | cpl = kvm_x86_ops->get_cpl(vcpu); | |
3548 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { | |
3549 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
3550 | return 1; | |
3551 | } | |
3552 | } | |
3553 | ||
3554 | if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) { | |
3555 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); | |
3556 | return 1; | |
3557 | } | |
3558 | ||
3559 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { | |
3fe913e7 | 3560 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
37817f29 IE |
3561 | save_guest_segment_descriptor(vcpu, tr_seg.selector, |
3562 | &cseg_desc); | |
3563 | } | |
3564 | ||
3565 | if (reason == TASK_SWITCH_IRET) { | |
3566 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); | |
3567 | kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); | |
3568 | } | |
3569 | ||
3570 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
3571 | kvm_x86_ops->cache_regs(vcpu); | |
3572 | ||
3573 | if (nseg_desc.type & 8) | |
3574 | ret = kvm_task_switch_32(vcpu, tss_selector, &cseg_desc, | |
3575 | &nseg_desc); | |
3576 | else | |
3577 | ret = kvm_task_switch_16(vcpu, tss_selector, &cseg_desc, | |
3578 | &nseg_desc); | |
3579 | ||
3580 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { | |
3581 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); | |
3582 | kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT); | |
3583 | } | |
3584 | ||
3585 | if (reason != TASK_SWITCH_IRET) { | |
3fe913e7 | 3586 | nseg_desc.type |= (1 << 1); |
37817f29 IE |
3587 | save_guest_segment_descriptor(vcpu, tss_selector, |
3588 | &nseg_desc); | |
3589 | } | |
3590 | ||
3591 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS); | |
3592 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); | |
3593 | tr_seg.type = 11; | |
3594 | set_segment(vcpu, &tr_seg, VCPU_SREG_TR); | |
3595 | out: | |
3596 | kvm_x86_ops->decache_regs(vcpu); | |
3597 | return ret; | |
3598 | } | |
3599 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
3600 | ||
b6c7a5dc HB |
3601 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
3602 | struct kvm_sregs *sregs) | |
3603 | { | |
3604 | int mmu_reset_needed = 0; | |
3605 | int i, pending_vec, max_bits; | |
3606 | struct descriptor_table dt; | |
3607 | ||
3608 | vcpu_load(vcpu); | |
3609 | ||
3610 | dt.limit = sregs->idt.limit; | |
3611 | dt.base = sregs->idt.base; | |
3612 | kvm_x86_ops->set_idt(vcpu, &dt); | |
3613 | dt.limit = sregs->gdt.limit; | |
3614 | dt.base = sregs->gdt.base; | |
3615 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
3616 | ||
ad312c7c ZX |
3617 | vcpu->arch.cr2 = sregs->cr2; |
3618 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; | |
3619 | vcpu->arch.cr3 = sregs->cr3; | |
b6c7a5dc | 3620 | |
2d3ad1f4 | 3621 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 3622 | |
ad312c7c | 3623 | mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer; |
b6c7a5dc | 3624 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
b6c7a5dc HB |
3625 | kvm_set_apic_base(vcpu, sregs->apic_base); |
3626 | ||
3627 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
3628 | ||
ad312c7c | 3629 | mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0; |
b6c7a5dc | 3630 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
d7306163 | 3631 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 3632 | |
ad312c7c | 3633 | mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4; |
b6c7a5dc HB |
3634 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
3635 | if (!is_long_mode(vcpu) && is_pae(vcpu)) | |
ad312c7c | 3636 | load_pdptrs(vcpu, vcpu->arch.cr3); |
b6c7a5dc HB |
3637 | |
3638 | if (mmu_reset_needed) | |
3639 | kvm_mmu_reset_context(vcpu); | |
3640 | ||
3641 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
ad312c7c ZX |
3642 | memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap, |
3643 | sizeof vcpu->arch.irq_pending); | |
3644 | vcpu->arch.irq_summary = 0; | |
3645 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i) | |
3646 | if (vcpu->arch.irq_pending[i]) | |
3647 | __set_bit(i, &vcpu->arch.irq_summary); | |
b6c7a5dc HB |
3648 | } else { |
3649 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; | |
3650 | pending_vec = find_first_bit( | |
3651 | (const unsigned long *)sregs->interrupt_bitmap, | |
3652 | max_bits); | |
3653 | /* Only pending external irq is handled here */ | |
3654 | if (pending_vec < max_bits) { | |
3655 | kvm_x86_ops->set_irq(vcpu, pending_vec); | |
3656 | pr_debug("Set back pending irq %d\n", | |
3657 | pending_vec); | |
3658 | } | |
3659 | } | |
3660 | ||
3661 | set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); | |
3662 | set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
3663 | set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
3664 | set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
3665 | set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
3666 | set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
3667 | ||
3668 | set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); | |
3669 | set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
3670 | ||
3671 | vcpu_put(vcpu); | |
3672 | ||
3673 | return 0; | |
3674 | } | |
3675 | ||
3676 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, | |
3677 | struct kvm_debug_guest *dbg) | |
3678 | { | |
3679 | int r; | |
3680 | ||
3681 | vcpu_load(vcpu); | |
3682 | ||
3683 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); | |
3684 | ||
3685 | vcpu_put(vcpu); | |
3686 | ||
3687 | return r; | |
3688 | } | |
3689 | ||
d0752060 HB |
3690 | /* |
3691 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
3692 | * we have asm/x86/processor.h | |
3693 | */ | |
3694 | struct fxsave { | |
3695 | u16 cwd; | |
3696 | u16 swd; | |
3697 | u16 twd; | |
3698 | u16 fop; | |
3699 | u64 rip; | |
3700 | u64 rdp; | |
3701 | u32 mxcsr; | |
3702 | u32 mxcsr_mask; | |
3703 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
3704 | #ifdef CONFIG_X86_64 | |
3705 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
3706 | #else | |
3707 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
3708 | #endif | |
3709 | }; | |
3710 | ||
8b006791 ZX |
3711 | /* |
3712 | * Translate a guest virtual address to a guest physical address. | |
3713 | */ | |
3714 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
3715 | struct kvm_translation *tr) | |
3716 | { | |
3717 | unsigned long vaddr = tr->linear_address; | |
3718 | gpa_t gpa; | |
3719 | ||
3720 | vcpu_load(vcpu); | |
72dc67a6 | 3721 | down_read(&vcpu->kvm->slots_lock); |
ad312c7c | 3722 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr); |
72dc67a6 | 3723 | up_read(&vcpu->kvm->slots_lock); |
8b006791 ZX |
3724 | tr->physical_address = gpa; |
3725 | tr->valid = gpa != UNMAPPED_GVA; | |
3726 | tr->writeable = 1; | |
3727 | tr->usermode = 0; | |
8b006791 ZX |
3728 | vcpu_put(vcpu); |
3729 | ||
3730 | return 0; | |
3731 | } | |
3732 | ||
d0752060 HB |
3733 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
3734 | { | |
ad312c7c | 3735 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
d0752060 HB |
3736 | |
3737 | vcpu_load(vcpu); | |
3738 | ||
3739 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
3740 | fpu->fcw = fxsave->cwd; | |
3741 | fpu->fsw = fxsave->swd; | |
3742 | fpu->ftwx = fxsave->twd; | |
3743 | fpu->last_opcode = fxsave->fop; | |
3744 | fpu->last_ip = fxsave->rip; | |
3745 | fpu->last_dp = fxsave->rdp; | |
3746 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
3747 | ||
3748 | vcpu_put(vcpu); | |
3749 | ||
3750 | return 0; | |
3751 | } | |
3752 | ||
3753 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
3754 | { | |
ad312c7c | 3755 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
d0752060 HB |
3756 | |
3757 | vcpu_load(vcpu); | |
3758 | ||
3759 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
3760 | fxsave->cwd = fpu->fcw; | |
3761 | fxsave->swd = fpu->fsw; | |
3762 | fxsave->twd = fpu->ftwx; | |
3763 | fxsave->fop = fpu->last_opcode; | |
3764 | fxsave->rip = fpu->last_ip; | |
3765 | fxsave->rdp = fpu->last_dp; | |
3766 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
3767 | ||
3768 | vcpu_put(vcpu); | |
3769 | ||
3770 | return 0; | |
3771 | } | |
3772 | ||
3773 | void fx_init(struct kvm_vcpu *vcpu) | |
3774 | { | |
3775 | unsigned after_mxcsr_mask; | |
3776 | ||
bc1a34f1 AA |
3777 | /* |
3778 | * Touch the fpu the first time in non atomic context as if | |
3779 | * this is the first fpu instruction the exception handler | |
3780 | * will fire before the instruction returns and it'll have to | |
3781 | * allocate ram with GFP_KERNEL. | |
3782 | */ | |
3783 | if (!used_math()) | |
3784 | fx_save(&vcpu->arch.host_fx_image); | |
3785 | ||
d0752060 HB |
3786 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
3787 | preempt_disable(); | |
ad312c7c | 3788 | fx_save(&vcpu->arch.host_fx_image); |
bc1a34f1 | 3789 | fx_finit(); |
ad312c7c ZX |
3790 | fx_save(&vcpu->arch.guest_fx_image); |
3791 | fx_restore(&vcpu->arch.host_fx_image); | |
d0752060 HB |
3792 | preempt_enable(); |
3793 | ||
ad312c7c | 3794 | vcpu->arch.cr0 |= X86_CR0_ET; |
d0752060 | 3795 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
ad312c7c ZX |
3796 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
3797 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, | |
d0752060 HB |
3798 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
3799 | } | |
3800 | EXPORT_SYMBOL_GPL(fx_init); | |
3801 | ||
3802 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
3803 | { | |
3804 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
3805 | return; | |
3806 | ||
3807 | vcpu->guest_fpu_loaded = 1; | |
ad312c7c ZX |
3808 | fx_save(&vcpu->arch.host_fx_image); |
3809 | fx_restore(&vcpu->arch.guest_fx_image); | |
d0752060 HB |
3810 | } |
3811 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
3812 | ||
3813 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
3814 | { | |
3815 | if (!vcpu->guest_fpu_loaded) | |
3816 | return; | |
3817 | ||
3818 | vcpu->guest_fpu_loaded = 0; | |
ad312c7c ZX |
3819 | fx_save(&vcpu->arch.guest_fx_image); |
3820 | fx_restore(&vcpu->arch.host_fx_image); | |
f096ed85 | 3821 | ++vcpu->stat.fpu_reload; |
d0752060 HB |
3822 | } |
3823 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
e9b11c17 ZX |
3824 | |
3825 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
3826 | { | |
3827 | kvm_x86_ops->vcpu_free(vcpu); | |
3828 | } | |
3829 | ||
3830 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, | |
3831 | unsigned int id) | |
3832 | { | |
26e5215f AK |
3833 | return kvm_x86_ops->vcpu_create(kvm, id); |
3834 | } | |
e9b11c17 | 3835 | |
26e5215f AK |
3836 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
3837 | { | |
3838 | int r; | |
e9b11c17 ZX |
3839 | |
3840 | /* We do fxsave: this must be aligned. */ | |
ad312c7c | 3841 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
e9b11c17 ZX |
3842 | |
3843 | vcpu_load(vcpu); | |
3844 | r = kvm_arch_vcpu_reset(vcpu); | |
3845 | if (r == 0) | |
3846 | r = kvm_mmu_setup(vcpu); | |
3847 | vcpu_put(vcpu); | |
3848 | if (r < 0) | |
3849 | goto free_vcpu; | |
3850 | ||
26e5215f | 3851 | return 0; |
e9b11c17 ZX |
3852 | free_vcpu: |
3853 | kvm_x86_ops->vcpu_free(vcpu); | |
26e5215f | 3854 | return r; |
e9b11c17 ZX |
3855 | } |
3856 | ||
d40ccc62 | 3857 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 ZX |
3858 | { |
3859 | vcpu_load(vcpu); | |
3860 | kvm_mmu_unload(vcpu); | |
3861 | vcpu_put(vcpu); | |
3862 | ||
3863 | kvm_x86_ops->vcpu_free(vcpu); | |
3864 | } | |
3865 | ||
3866 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) | |
3867 | { | |
3868 | return kvm_x86_ops->vcpu_reset(vcpu); | |
3869 | } | |
3870 | ||
3871 | void kvm_arch_hardware_enable(void *garbage) | |
3872 | { | |
3873 | kvm_x86_ops->hardware_enable(garbage); | |
3874 | } | |
3875 | ||
3876 | void kvm_arch_hardware_disable(void *garbage) | |
3877 | { | |
3878 | kvm_x86_ops->hardware_disable(garbage); | |
3879 | } | |
3880 | ||
3881 | int kvm_arch_hardware_setup(void) | |
3882 | { | |
3883 | return kvm_x86_ops->hardware_setup(); | |
3884 | } | |
3885 | ||
3886 | void kvm_arch_hardware_unsetup(void) | |
3887 | { | |
3888 | kvm_x86_ops->hardware_unsetup(); | |
3889 | } | |
3890 | ||
3891 | void kvm_arch_check_processor_compat(void *rtn) | |
3892 | { | |
3893 | kvm_x86_ops->check_processor_compatibility(rtn); | |
3894 | } | |
3895 | ||
3896 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) | |
3897 | { | |
3898 | struct page *page; | |
3899 | struct kvm *kvm; | |
3900 | int r; | |
3901 | ||
3902 | BUG_ON(vcpu->kvm == NULL); | |
3903 | kvm = vcpu->kvm; | |
3904 | ||
ad312c7c | 3905 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
e9b11c17 | 3906 | if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) |
a4535290 | 3907 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
e9b11c17 | 3908 | else |
a4535290 | 3909 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
e9b11c17 ZX |
3910 | |
3911 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
3912 | if (!page) { | |
3913 | r = -ENOMEM; | |
3914 | goto fail; | |
3915 | } | |
ad312c7c | 3916 | vcpu->arch.pio_data = page_address(page); |
e9b11c17 ZX |
3917 | |
3918 | r = kvm_mmu_create(vcpu); | |
3919 | if (r < 0) | |
3920 | goto fail_free_pio_data; | |
3921 | ||
3922 | if (irqchip_in_kernel(kvm)) { | |
3923 | r = kvm_create_lapic(vcpu); | |
3924 | if (r < 0) | |
3925 | goto fail_mmu_destroy; | |
3926 | } | |
3927 | ||
3928 | return 0; | |
3929 | ||
3930 | fail_mmu_destroy: | |
3931 | kvm_mmu_destroy(vcpu); | |
3932 | fail_free_pio_data: | |
ad312c7c | 3933 | free_page((unsigned long)vcpu->arch.pio_data); |
e9b11c17 ZX |
3934 | fail: |
3935 | return r; | |
3936 | } | |
3937 | ||
3938 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
3939 | { | |
3940 | kvm_free_lapic(vcpu); | |
3200f405 | 3941 | down_read(&vcpu->kvm->slots_lock); |
e9b11c17 | 3942 | kvm_mmu_destroy(vcpu); |
3200f405 | 3943 | up_read(&vcpu->kvm->slots_lock); |
ad312c7c | 3944 | free_page((unsigned long)vcpu->arch.pio_data); |
e9b11c17 | 3945 | } |
d19a9cd2 ZX |
3946 | |
3947 | struct kvm *kvm_arch_create_vm(void) | |
3948 | { | |
3949 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
3950 | ||
3951 | if (!kvm) | |
3952 | return ERR_PTR(-ENOMEM); | |
3953 | ||
f05e70ac | 3954 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
d19a9cd2 ZX |
3955 | |
3956 | return kvm; | |
3957 | } | |
3958 | ||
3959 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) | |
3960 | { | |
3961 | vcpu_load(vcpu); | |
3962 | kvm_mmu_unload(vcpu); | |
3963 | vcpu_put(vcpu); | |
3964 | } | |
3965 | ||
3966 | static void kvm_free_vcpus(struct kvm *kvm) | |
3967 | { | |
3968 | unsigned int i; | |
3969 | ||
3970 | /* | |
3971 | * Unpin any mmu pages first. | |
3972 | */ | |
3973 | for (i = 0; i < KVM_MAX_VCPUS; ++i) | |
3974 | if (kvm->vcpus[i]) | |
3975 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); | |
3976 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
3977 | if (kvm->vcpus[i]) { | |
3978 | kvm_arch_vcpu_free(kvm->vcpus[i]); | |
3979 | kvm->vcpus[i] = NULL; | |
3980 | } | |
3981 | } | |
3982 | ||
3983 | } | |
3984 | ||
3985 | void kvm_arch_destroy_vm(struct kvm *kvm) | |
3986 | { | |
7837699f | 3987 | kvm_free_pit(kvm); |
d7deeeb0 ZX |
3988 | kfree(kvm->arch.vpic); |
3989 | kfree(kvm->arch.vioapic); | |
d19a9cd2 ZX |
3990 | kvm_free_vcpus(kvm); |
3991 | kvm_free_physmem(kvm); | |
3d45830c AK |
3992 | if (kvm->arch.apic_access_page) |
3993 | put_page(kvm->arch.apic_access_page); | |
b7ebfb05 SY |
3994 | if (kvm->arch.ept_identity_pagetable) |
3995 | put_page(kvm->arch.ept_identity_pagetable); | |
d19a9cd2 ZX |
3996 | kfree(kvm); |
3997 | } | |
0de10343 ZX |
3998 | |
3999 | int kvm_arch_set_memory_region(struct kvm *kvm, | |
4000 | struct kvm_userspace_memory_region *mem, | |
4001 | struct kvm_memory_slot old, | |
4002 | int user_alloc) | |
4003 | { | |
4004 | int npages = mem->memory_size >> PAGE_SHIFT; | |
4005 | struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot]; | |
4006 | ||
4007 | /*To keep backward compatibility with older userspace, | |
4008 | *x86 needs to hanlde !user_alloc case. | |
4009 | */ | |
4010 | if (!user_alloc) { | |
4011 | if (npages && !old.rmap) { | |
72dc67a6 | 4012 | down_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4013 | memslot->userspace_addr = do_mmap(NULL, 0, |
4014 | npages * PAGE_SIZE, | |
4015 | PROT_READ | PROT_WRITE, | |
4016 | MAP_SHARED | MAP_ANONYMOUS, | |
4017 | 0); | |
72dc67a6 | 4018 | up_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4019 | |
4020 | if (IS_ERR((void *)memslot->userspace_addr)) | |
4021 | return PTR_ERR((void *)memslot->userspace_addr); | |
4022 | } else { | |
4023 | if (!old.user_alloc && old.rmap) { | |
4024 | int ret; | |
4025 | ||
72dc67a6 | 4026 | down_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4027 | ret = do_munmap(current->mm, old.userspace_addr, |
4028 | old.npages * PAGE_SIZE); | |
72dc67a6 | 4029 | up_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4030 | if (ret < 0) |
4031 | printk(KERN_WARNING | |
4032 | "kvm_vm_ioctl_set_memory_region: " | |
4033 | "failed to munmap memory\n"); | |
4034 | } | |
4035 | } | |
4036 | } | |
4037 | ||
f05e70ac | 4038 | if (!kvm->arch.n_requested_mmu_pages) { |
0de10343 ZX |
4039 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
4040 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
4041 | } | |
4042 | ||
4043 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); | |
4044 | kvm_flush_remote_tlbs(kvm); | |
4045 | ||
4046 | return 0; | |
4047 | } | |
1d737c8a ZX |
4048 | |
4049 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) | |
4050 | { | |
a4535290 AK |
4051 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
4052 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED; | |
1d737c8a | 4053 | } |
5736199a ZX |
4054 | |
4055 | static void vcpu_kick_intr(void *info) | |
4056 | { | |
4057 | #ifdef DEBUG | |
4058 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info; | |
4059 | printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu); | |
4060 | #endif | |
4061 | } | |
4062 | ||
4063 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) | |
4064 | { | |
4065 | int ipi_pcpu = vcpu->cpu; | |
e9571ed5 | 4066 | int cpu = get_cpu(); |
5736199a ZX |
4067 | |
4068 | if (waitqueue_active(&vcpu->wq)) { | |
4069 | wake_up_interruptible(&vcpu->wq); | |
4070 | ++vcpu->stat.halt_wakeup; | |
4071 | } | |
e9571ed5 MT |
4072 | /* |
4073 | * We may be called synchronously with irqs disabled in guest mode, | |
4074 | * So need not to call smp_call_function_single() in that case. | |
4075 | */ | |
4076 | if (vcpu->guest_mode && vcpu->cpu != cpu) | |
8691e5a8 | 4077 | smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0); |
e9571ed5 | 4078 | put_cpu(); |
5736199a | 4079 | } |