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. | |
4d5c5d0f BAY |
7 | * Copyright (C) 2008 Qumranet, Inc. |
8 | * Copyright IBM Corporation, 2008 | |
043405e1 CO |
9 | * |
10 | * Authors: | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * Yaniv Kamay <yaniv@qumranet.com> | |
4d5c5d0f BAY |
13 | * Amit Shah <amit.shah@qumranet.com> |
14 | * Ben-Ami Yassour <benami@il.ibm.com> | |
043405e1 CO |
15 | * |
16 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
17 | * the COPYING file in the top-level directory. | |
18 | * | |
19 | */ | |
20 | ||
edf88417 | 21 | #include <linux/kvm_host.h> |
313a3dc7 | 22 | #include "irq.h" |
1d737c8a | 23 | #include "mmu.h" |
7837699f | 24 | #include "i8254.h" |
37817f29 | 25 | #include "tss.h" |
5fdbf976 | 26 | #include "kvm_cache_regs.h" |
26eef70c | 27 | #include "x86.h" |
313a3dc7 | 28 | |
18068523 | 29 | #include <linux/clocksource.h> |
4d5c5d0f | 30 | #include <linux/interrupt.h> |
313a3dc7 CO |
31 | #include <linux/kvm.h> |
32 | #include <linux/fs.h> | |
33 | #include <linux/vmalloc.h> | |
5fb76f9b | 34 | #include <linux/module.h> |
0de10343 | 35 | #include <linux/mman.h> |
2bacc55c | 36 | #include <linux/highmem.h> |
19de40a8 | 37 | #include <linux/iommu.h> |
62c476c7 | 38 | #include <linux/intel-iommu.h> |
c8076604 | 39 | #include <linux/cpufreq.h> |
229456fc MT |
40 | #define CREATE_TRACE_POINTS |
41 | #include "trace.h" | |
043405e1 CO |
42 | |
43 | #include <asm/uaccess.h> | |
d825ed0a | 44 | #include <asm/msr.h> |
a5f61300 | 45 | #include <asm/desc.h> |
0bed3b56 | 46 | #include <asm/mtrr.h> |
890ca9ae | 47 | #include <asm/mce.h> |
043405e1 | 48 | |
313a3dc7 | 49 | #define MAX_IO_MSRS 256 |
a03490ed CO |
50 | #define CR0_RESERVED_BITS \ |
51 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ | |
52 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ | |
53 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) | |
54 | #define CR4_RESERVED_BITS \ | |
55 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ | |
56 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ | |
57 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
58 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) | |
59 | ||
60 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) | |
890ca9ae HY |
61 | |
62 | #define KVM_MAX_MCE_BANKS 32 | |
63 | #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P | |
64 | ||
50a37eb4 JR |
65 | /* EFER defaults: |
66 | * - enable syscall per default because its emulated by KVM | |
67 | * - enable LME and LMA per default on 64 bit KVM | |
68 | */ | |
69 | #ifdef CONFIG_X86_64 | |
70 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; | |
71 | #else | |
72 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; | |
73 | #endif | |
313a3dc7 | 74 | |
ba1389b7 AK |
75 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
76 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU | |
417bc304 | 77 | |
674eea0f AK |
78 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
79 | struct kvm_cpuid_entry2 __user *entries); | |
d8017474 AG |
80 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
81 | u32 function, u32 index); | |
674eea0f | 82 | |
97896d04 | 83 | struct kvm_x86_ops *kvm_x86_ops; |
5fdbf976 | 84 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
97896d04 | 85 | |
417bc304 | 86 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
ba1389b7 AK |
87 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
88 | { "pf_guest", VCPU_STAT(pf_guest) }, | |
89 | { "tlb_flush", VCPU_STAT(tlb_flush) }, | |
90 | { "invlpg", VCPU_STAT(invlpg) }, | |
91 | { "exits", VCPU_STAT(exits) }, | |
92 | { "io_exits", VCPU_STAT(io_exits) }, | |
93 | { "mmio_exits", VCPU_STAT(mmio_exits) }, | |
94 | { "signal_exits", VCPU_STAT(signal_exits) }, | |
95 | { "irq_window", VCPU_STAT(irq_window_exits) }, | |
f08864b4 | 96 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
ba1389b7 AK |
97 | { "halt_exits", VCPU_STAT(halt_exits) }, |
98 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, | |
f11c3a8d | 99 | { "hypercalls", VCPU_STAT(hypercalls) }, |
ba1389b7 AK |
100 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
101 | { "irq_exits", VCPU_STAT(irq_exits) }, | |
102 | { "host_state_reload", VCPU_STAT(host_state_reload) }, | |
103 | { "efer_reload", VCPU_STAT(efer_reload) }, | |
104 | { "fpu_reload", VCPU_STAT(fpu_reload) }, | |
105 | { "insn_emulation", VCPU_STAT(insn_emulation) }, | |
106 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, | |
fa89a817 | 107 | { "irq_injections", VCPU_STAT(irq_injections) }, |
c4abb7c9 | 108 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
4cee5764 AK |
109 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
110 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, | |
111 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, | |
112 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, | |
113 | { "mmu_flooded", VM_STAT(mmu_flooded) }, | |
114 | { "mmu_recycled", VM_STAT(mmu_recycled) }, | |
dfc5aa00 | 115 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
4731d4c7 | 116 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
0f74a24c | 117 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
05da4558 | 118 | { "largepages", VM_STAT(lpages) }, |
417bc304 HB |
119 | { NULL } |
120 | }; | |
121 | ||
5fb76f9b CO |
122 | unsigned long segment_base(u16 selector) |
123 | { | |
124 | struct descriptor_table gdt; | |
a5f61300 | 125 | struct desc_struct *d; |
5fb76f9b CO |
126 | unsigned long table_base; |
127 | unsigned long v; | |
128 | ||
129 | if (selector == 0) | |
130 | return 0; | |
131 | ||
132 | asm("sgdt %0" : "=m"(gdt)); | |
133 | table_base = gdt.base; | |
134 | ||
135 | if (selector & 4) { /* from ldt */ | |
136 | u16 ldt_selector; | |
137 | ||
138 | asm("sldt %0" : "=g"(ldt_selector)); | |
139 | table_base = segment_base(ldt_selector); | |
140 | } | |
a5f61300 AK |
141 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
142 | v = d->base0 | ((unsigned long)d->base1 << 16) | | |
143 | ((unsigned long)d->base2 << 24); | |
5fb76f9b | 144 | #ifdef CONFIG_X86_64 |
a5f61300 AK |
145 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
146 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; | |
5fb76f9b CO |
147 | #endif |
148 | return v; | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(segment_base); | |
151 | ||
6866b83e CO |
152 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
153 | { | |
154 | if (irqchip_in_kernel(vcpu->kvm)) | |
ad312c7c | 155 | return vcpu->arch.apic_base; |
6866b83e | 156 | else |
ad312c7c | 157 | return vcpu->arch.apic_base; |
6866b83e CO |
158 | } |
159 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
160 | ||
161 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) | |
162 | { | |
163 | /* TODO: reserve bits check */ | |
164 | if (irqchip_in_kernel(vcpu->kvm)) | |
165 | kvm_lapic_set_base(vcpu, data); | |
166 | else | |
ad312c7c | 167 | vcpu->arch.apic_base = data; |
6866b83e CO |
168 | } |
169 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
170 | ||
298101da AK |
171 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
172 | { | |
ad312c7c ZX |
173 | WARN_ON(vcpu->arch.exception.pending); |
174 | vcpu->arch.exception.pending = true; | |
175 | vcpu->arch.exception.has_error_code = false; | |
176 | vcpu->arch.exception.nr = nr; | |
298101da AK |
177 | } |
178 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
179 | ||
c3c91fee AK |
180 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
181 | u32 error_code) | |
182 | { | |
183 | ++vcpu->stat.pf_guest; | |
d8017474 | 184 | |
71c4dfaf | 185 | if (vcpu->arch.exception.pending) { |
6edf14d8 GN |
186 | switch(vcpu->arch.exception.nr) { |
187 | case DF_VECTOR: | |
71c4dfaf JR |
188 | /* triple fault -> shutdown */ |
189 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); | |
6edf14d8 GN |
190 | return; |
191 | case PF_VECTOR: | |
192 | vcpu->arch.exception.nr = DF_VECTOR; | |
193 | vcpu->arch.exception.error_code = 0; | |
194 | return; | |
195 | default: | |
196 | /* replace previous exception with a new one in a hope | |
197 | that instruction re-execution will regenerate lost | |
198 | exception */ | |
199 | vcpu->arch.exception.pending = false; | |
200 | break; | |
71c4dfaf | 201 | } |
c3c91fee | 202 | } |
ad312c7c | 203 | vcpu->arch.cr2 = addr; |
c3c91fee AK |
204 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
205 | } | |
206 | ||
3419ffc8 SY |
207 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
208 | { | |
209 | vcpu->arch.nmi_pending = 1; | |
210 | } | |
211 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
212 | ||
298101da AK |
213 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
214 | { | |
ad312c7c ZX |
215 | WARN_ON(vcpu->arch.exception.pending); |
216 | vcpu->arch.exception.pending = true; | |
217 | vcpu->arch.exception.has_error_code = true; | |
218 | vcpu->arch.exception.nr = nr; | |
219 | vcpu->arch.exception.error_code = error_code; | |
298101da AK |
220 | } |
221 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
222 | ||
223 | static void __queue_exception(struct kvm_vcpu *vcpu) | |
224 | { | |
ad312c7c ZX |
225 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
226 | vcpu->arch.exception.has_error_code, | |
227 | vcpu->arch.exception.error_code); | |
298101da AK |
228 | } |
229 | ||
a03490ed CO |
230 | /* |
231 | * Load the pae pdptrs. Return true is they are all valid. | |
232 | */ | |
233 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) | |
234 | { | |
235 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
236 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
237 | int i; | |
238 | int ret; | |
ad312c7c | 239 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
a03490ed | 240 | |
a03490ed CO |
241 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
242 | offset * sizeof(u64), sizeof(pdpte)); | |
243 | if (ret < 0) { | |
244 | ret = 0; | |
245 | goto out; | |
246 | } | |
247 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
43a3795a | 248 | if (is_present_gpte(pdpte[i]) && |
20c466b5 | 249 | (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) { |
a03490ed CO |
250 | ret = 0; |
251 | goto out; | |
252 | } | |
253 | } | |
254 | ret = 1; | |
255 | ||
ad312c7c | 256 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
6de4f3ad AK |
257 | __set_bit(VCPU_EXREG_PDPTR, |
258 | (unsigned long *)&vcpu->arch.regs_avail); | |
259 | __set_bit(VCPU_EXREG_PDPTR, | |
260 | (unsigned long *)&vcpu->arch.regs_dirty); | |
a03490ed | 261 | out: |
a03490ed CO |
262 | |
263 | return ret; | |
264 | } | |
cc4b6871 | 265 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 266 | |
d835dfec AK |
267 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
268 | { | |
ad312c7c | 269 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
d835dfec AK |
270 | bool changed = true; |
271 | int r; | |
272 | ||
273 | if (is_long_mode(vcpu) || !is_pae(vcpu)) | |
274 | return false; | |
275 | ||
6de4f3ad AK |
276 | if (!test_bit(VCPU_EXREG_PDPTR, |
277 | (unsigned long *)&vcpu->arch.regs_avail)) | |
278 | return true; | |
279 | ||
ad312c7c | 280 | r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte)); |
d835dfec AK |
281 | if (r < 0) |
282 | goto out; | |
ad312c7c | 283 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
d835dfec | 284 | out: |
d835dfec AK |
285 | |
286 | return changed; | |
287 | } | |
288 | ||
2d3ad1f4 | 289 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed CO |
290 | { |
291 | if (cr0 & CR0_RESERVED_BITS) { | |
292 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", | |
ad312c7c | 293 | cr0, vcpu->arch.cr0); |
c1a5d4f9 | 294 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
295 | return; |
296 | } | |
297 | ||
298 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { | |
299 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); | |
c1a5d4f9 | 300 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
301 | return; |
302 | } | |
303 | ||
304 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { | |
305 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " | |
306 | "and a clear PE flag\n"); | |
c1a5d4f9 | 307 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
308 | return; |
309 | } | |
310 | ||
311 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
312 | #ifdef CONFIG_X86_64 | |
ad312c7c | 313 | if ((vcpu->arch.shadow_efer & EFER_LME)) { |
a03490ed CO |
314 | int cs_db, cs_l; |
315 | ||
316 | if (!is_pae(vcpu)) { | |
317 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
318 | "in long mode while PAE is disabled\n"); | |
c1a5d4f9 | 319 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
320 | return; |
321 | } | |
322 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
323 | if (cs_l) { | |
324 | printk(KERN_DEBUG "set_cr0: #GP, start paging " | |
325 | "in long mode while CS.L == 1\n"); | |
c1a5d4f9 | 326 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
327 | return; |
328 | ||
329 | } | |
330 | } else | |
331 | #endif | |
ad312c7c | 332 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
a03490ed CO |
333 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
334 | "reserved bits\n"); | |
c1a5d4f9 | 335 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
336 | return; |
337 | } | |
338 | ||
339 | } | |
340 | ||
341 | kvm_x86_ops->set_cr0(vcpu, cr0); | |
ad312c7c | 342 | vcpu->arch.cr0 = cr0; |
a03490ed | 343 | |
a03490ed | 344 | kvm_mmu_reset_context(vcpu); |
a03490ed CO |
345 | return; |
346 | } | |
2d3ad1f4 | 347 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 348 | |
2d3ad1f4 | 349 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 350 | { |
2d3ad1f4 | 351 | kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)); |
a03490ed | 352 | } |
2d3ad1f4 | 353 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 354 | |
2d3ad1f4 | 355 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed | 356 | { |
a2edf57f AK |
357 | unsigned long old_cr4 = vcpu->arch.cr4; |
358 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE; | |
359 | ||
a03490ed CO |
360 | if (cr4 & CR4_RESERVED_BITS) { |
361 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); | |
c1a5d4f9 | 362 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
363 | return; |
364 | } | |
365 | ||
366 | if (is_long_mode(vcpu)) { | |
367 | if (!(cr4 & X86_CR4_PAE)) { | |
368 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " | |
369 | "in long mode\n"); | |
c1a5d4f9 | 370 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
371 | return; |
372 | } | |
a2edf57f AK |
373 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
374 | && ((cr4 ^ old_cr4) & pdptr_bits) | |
ad312c7c | 375 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
a03490ed | 376 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
c1a5d4f9 | 377 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
378 | return; |
379 | } | |
380 | ||
381 | if (cr4 & X86_CR4_VMXE) { | |
382 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); | |
c1a5d4f9 | 383 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
384 | return; |
385 | } | |
386 | kvm_x86_ops->set_cr4(vcpu, cr4); | |
ad312c7c | 387 | vcpu->arch.cr4 = cr4; |
5a41accd | 388 | vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled; |
a03490ed | 389 | kvm_mmu_reset_context(vcpu); |
a03490ed | 390 | } |
2d3ad1f4 | 391 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 392 | |
2d3ad1f4 | 393 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 394 | { |
ad312c7c | 395 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
0ba73cda | 396 | kvm_mmu_sync_roots(vcpu); |
d835dfec AK |
397 | kvm_mmu_flush_tlb(vcpu); |
398 | return; | |
399 | } | |
400 | ||
a03490ed CO |
401 | if (is_long_mode(vcpu)) { |
402 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { | |
403 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 404 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
405 | return; |
406 | } | |
407 | } else { | |
408 | if (is_pae(vcpu)) { | |
409 | if (cr3 & CR3_PAE_RESERVED_BITS) { | |
410 | printk(KERN_DEBUG | |
411 | "set_cr3: #GP, reserved bits\n"); | |
c1a5d4f9 | 412 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
413 | return; |
414 | } | |
415 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { | |
416 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " | |
417 | "reserved bits\n"); | |
c1a5d4f9 | 418 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
419 | return; |
420 | } | |
421 | } | |
422 | /* | |
423 | * We don't check reserved bits in nonpae mode, because | |
424 | * this isn't enforced, and VMware depends on this. | |
425 | */ | |
426 | } | |
427 | ||
a03490ed CO |
428 | /* |
429 | * Does the new cr3 value map to physical memory? (Note, we | |
430 | * catch an invalid cr3 even in real-mode, because it would | |
431 | * cause trouble later on when we turn on paging anyway.) | |
432 | * | |
433 | * A real CPU would silently accept an invalid cr3 and would | |
434 | * attempt to use it - with largely undefined (and often hard | |
435 | * to debug) behavior on the guest side. | |
436 | */ | |
437 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) | |
c1a5d4f9 | 438 | kvm_inject_gp(vcpu, 0); |
a03490ed | 439 | else { |
ad312c7c ZX |
440 | vcpu->arch.cr3 = cr3; |
441 | vcpu->arch.mmu.new_cr3(vcpu); | |
a03490ed | 442 | } |
a03490ed | 443 | } |
2d3ad1f4 | 444 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 445 | |
2d3ad1f4 | 446 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed CO |
447 | { |
448 | if (cr8 & CR8_RESERVED_BITS) { | |
449 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); | |
c1a5d4f9 | 450 | kvm_inject_gp(vcpu, 0); |
a03490ed CO |
451 | return; |
452 | } | |
453 | if (irqchip_in_kernel(vcpu->kvm)) | |
454 | kvm_lapic_set_tpr(vcpu, cr8); | |
455 | else | |
ad312c7c | 456 | vcpu->arch.cr8 = cr8; |
a03490ed | 457 | } |
2d3ad1f4 | 458 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 459 | |
2d3ad1f4 | 460 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed CO |
461 | { |
462 | if (irqchip_in_kernel(vcpu->kvm)) | |
463 | return kvm_lapic_get_cr8(vcpu); | |
464 | else | |
ad312c7c | 465 | return vcpu->arch.cr8; |
a03490ed | 466 | } |
2d3ad1f4 | 467 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 468 | |
d8017474 AG |
469 | static inline u32 bit(int bitno) |
470 | { | |
471 | return 1 << (bitno & 31); | |
472 | } | |
473 | ||
043405e1 CO |
474 | /* |
475 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
476 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
477 | * | |
478 | * This list is modified at module load time to reflect the | |
479 | * capabilities of the host cpu. | |
480 | */ | |
481 | static u32 msrs_to_save[] = { | |
482 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
483 | MSR_K6_STAR, | |
484 | #ifdef CONFIG_X86_64 | |
485 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
486 | #endif | |
af24a4e4 | 487 | MSR_IA32_TSC, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
b286d5d8 | 488 | MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA |
043405e1 CO |
489 | }; |
490 | ||
491 | static unsigned num_msrs_to_save; | |
492 | ||
493 | static u32 emulated_msrs[] = { | |
494 | MSR_IA32_MISC_ENABLE, | |
495 | }; | |
496 | ||
15c4a640 CO |
497 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
498 | { | |
f2b4b7dd | 499 | if (efer & efer_reserved_bits) { |
15c4a640 CO |
500 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
501 | efer); | |
c1a5d4f9 | 502 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
503 | return; |
504 | } | |
505 | ||
506 | if (is_paging(vcpu) | |
ad312c7c | 507 | && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
15c4a640 | 508 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
c1a5d4f9 | 509 | kvm_inject_gp(vcpu, 0); |
15c4a640 CO |
510 | return; |
511 | } | |
512 | ||
1b2fd70c AG |
513 | if (efer & EFER_FFXSR) { |
514 | struct kvm_cpuid_entry2 *feat; | |
515 | ||
516 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); | |
517 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) { | |
518 | printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n"); | |
519 | kvm_inject_gp(vcpu, 0); | |
520 | return; | |
521 | } | |
522 | } | |
523 | ||
d8017474 AG |
524 | if (efer & EFER_SVME) { |
525 | struct kvm_cpuid_entry2 *feat; | |
526 | ||
527 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); | |
528 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) { | |
529 | printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n"); | |
530 | kvm_inject_gp(vcpu, 0); | |
531 | return; | |
532 | } | |
533 | } | |
534 | ||
15c4a640 CO |
535 | kvm_x86_ops->set_efer(vcpu, efer); |
536 | ||
537 | efer &= ~EFER_LMA; | |
ad312c7c | 538 | efer |= vcpu->arch.shadow_efer & EFER_LMA; |
15c4a640 | 539 | |
ad312c7c | 540 | vcpu->arch.shadow_efer = efer; |
9645bb56 AK |
541 | |
542 | vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled; | |
543 | kvm_mmu_reset_context(vcpu); | |
15c4a640 CO |
544 | } |
545 | ||
f2b4b7dd JR |
546 | void kvm_enable_efer_bits(u64 mask) |
547 | { | |
548 | efer_reserved_bits &= ~mask; | |
549 | } | |
550 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
551 | ||
552 | ||
15c4a640 CO |
553 | /* |
554 | * Writes msr value into into the appropriate "register". | |
555 | * Returns 0 on success, non-0 otherwise. | |
556 | * Assumes vcpu_load() was already called. | |
557 | */ | |
558 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
559 | { | |
560 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); | |
561 | } | |
562 | ||
313a3dc7 CO |
563 | /* |
564 | * Adapt set_msr() to msr_io()'s calling convention | |
565 | */ | |
566 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
567 | { | |
568 | return kvm_set_msr(vcpu, index, *data); | |
569 | } | |
570 | ||
18068523 GOC |
571 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
572 | { | |
573 | static int version; | |
50d0a0f9 GH |
574 | struct pvclock_wall_clock wc; |
575 | struct timespec now, sys, boot; | |
18068523 GOC |
576 | |
577 | if (!wall_clock) | |
578 | return; | |
579 | ||
580 | version++; | |
581 | ||
18068523 GOC |
582 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
583 | ||
50d0a0f9 GH |
584 | /* |
585 | * The guest calculates current wall clock time by adding | |
586 | * system time (updated by kvm_write_guest_time below) to the | |
587 | * wall clock specified here. guest system time equals host | |
588 | * system time for us, thus we must fill in host boot time here. | |
589 | */ | |
590 | now = current_kernel_time(); | |
591 | ktime_get_ts(&sys); | |
592 | boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys)); | |
593 | ||
594 | wc.sec = boot.tv_sec; | |
595 | wc.nsec = boot.tv_nsec; | |
596 | wc.version = version; | |
18068523 GOC |
597 | |
598 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
599 | ||
600 | version++; | |
601 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
602 | } |
603 | ||
50d0a0f9 GH |
604 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
605 | { | |
606 | uint32_t quotient, remainder; | |
607 | ||
608 | /* Don't try to replace with do_div(), this one calculates | |
609 | * "(dividend << 32) / divisor" */ | |
610 | __asm__ ( "divl %4" | |
611 | : "=a" (quotient), "=d" (remainder) | |
612 | : "0" (0), "1" (dividend), "r" (divisor) ); | |
613 | return quotient; | |
614 | } | |
615 | ||
616 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) | |
617 | { | |
618 | uint64_t nsecs = 1000000000LL; | |
619 | int32_t shift = 0; | |
620 | uint64_t tps64; | |
621 | uint32_t tps32; | |
622 | ||
623 | tps64 = tsc_khz * 1000LL; | |
624 | while (tps64 > nsecs*2) { | |
625 | tps64 >>= 1; | |
626 | shift--; | |
627 | } | |
628 | ||
629 | tps32 = (uint32_t)tps64; | |
630 | while (tps32 <= (uint32_t)nsecs) { | |
631 | tps32 <<= 1; | |
632 | shift++; | |
633 | } | |
634 | ||
635 | hv_clock->tsc_shift = shift; | |
636 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); | |
637 | ||
638 | pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n", | |
80a914dc | 639 | __func__, tsc_khz, hv_clock->tsc_shift, |
50d0a0f9 GH |
640 | hv_clock->tsc_to_system_mul); |
641 | } | |
642 | ||
c8076604 GH |
643 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
644 | ||
18068523 GOC |
645 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
646 | { | |
647 | struct timespec ts; | |
648 | unsigned long flags; | |
649 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
650 | void *shared_kaddr; | |
463656c0 | 651 | unsigned long this_tsc_khz; |
18068523 GOC |
652 | |
653 | if ((!vcpu->time_page)) | |
654 | return; | |
655 | ||
463656c0 AK |
656 | this_tsc_khz = get_cpu_var(cpu_tsc_khz); |
657 | if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) { | |
658 | kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock); | |
659 | vcpu->hv_clock_tsc_khz = this_tsc_khz; | |
50d0a0f9 | 660 | } |
463656c0 | 661 | put_cpu_var(cpu_tsc_khz); |
50d0a0f9 | 662 | |
18068523 GOC |
663 | /* Keep irq disabled to prevent changes to the clock */ |
664 | local_irq_save(flags); | |
af24a4e4 | 665 | kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp); |
18068523 GOC |
666 | ktime_get_ts(&ts); |
667 | local_irq_restore(flags); | |
668 | ||
669 | /* With all the info we got, fill in the values */ | |
670 | ||
671 | vcpu->hv_clock.system_time = ts.tv_nsec + | |
672 | (NSEC_PER_SEC * (u64)ts.tv_sec); | |
673 | /* | |
674 | * The interface expects us to write an even number signaling that the | |
675 | * update is finished. Since the guest won't see the intermediate | |
50d0a0f9 | 676 | * state, we just increase by 2 at the end. |
18068523 | 677 | */ |
50d0a0f9 | 678 | vcpu->hv_clock.version += 2; |
18068523 GOC |
679 | |
680 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); | |
681 | ||
682 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, | |
50d0a0f9 | 683 | sizeof(vcpu->hv_clock)); |
18068523 GOC |
684 | |
685 | kunmap_atomic(shared_kaddr, KM_USER0); | |
686 | ||
687 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); | |
688 | } | |
689 | ||
c8076604 GH |
690 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) |
691 | { | |
692 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
693 | ||
694 | if (!vcpu->time_page) | |
695 | return 0; | |
696 | set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests); | |
697 | return 1; | |
698 | } | |
699 | ||
9ba075a6 AK |
700 | static bool msr_mtrr_valid(unsigned msr) |
701 | { | |
702 | switch (msr) { | |
703 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: | |
704 | case MSR_MTRRfix64K_00000: | |
705 | case MSR_MTRRfix16K_80000: | |
706 | case MSR_MTRRfix16K_A0000: | |
707 | case MSR_MTRRfix4K_C0000: | |
708 | case MSR_MTRRfix4K_C8000: | |
709 | case MSR_MTRRfix4K_D0000: | |
710 | case MSR_MTRRfix4K_D8000: | |
711 | case MSR_MTRRfix4K_E0000: | |
712 | case MSR_MTRRfix4K_E8000: | |
713 | case MSR_MTRRfix4K_F0000: | |
714 | case MSR_MTRRfix4K_F8000: | |
715 | case MSR_MTRRdefType: | |
716 | case MSR_IA32_CR_PAT: | |
717 | return true; | |
718 | case 0x2f8: | |
719 | return true; | |
720 | } | |
721 | return false; | |
722 | } | |
723 | ||
d6289b93 MT |
724 | static bool valid_pat_type(unsigned t) |
725 | { | |
726 | return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */ | |
727 | } | |
728 | ||
729 | static bool valid_mtrr_type(unsigned t) | |
730 | { | |
731 | return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */ | |
732 | } | |
733 | ||
734 | static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |
735 | { | |
736 | int i; | |
737 | ||
738 | if (!msr_mtrr_valid(msr)) | |
739 | return false; | |
740 | ||
741 | if (msr == MSR_IA32_CR_PAT) { | |
742 | for (i = 0; i < 8; i++) | |
743 | if (!valid_pat_type((data >> (i * 8)) & 0xff)) | |
744 | return false; | |
745 | return true; | |
746 | } else if (msr == MSR_MTRRdefType) { | |
747 | if (data & ~0xcff) | |
748 | return false; | |
749 | return valid_mtrr_type(data & 0xff); | |
750 | } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { | |
751 | for (i = 0; i < 8 ; i++) | |
752 | if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) | |
753 | return false; | |
754 | return true; | |
755 | } | |
756 | ||
757 | /* variable MTRRs */ | |
758 | return valid_mtrr_type(data & 0xff); | |
759 | } | |
760 | ||
9ba075a6 AK |
761 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
762 | { | |
0bed3b56 SY |
763 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
764 | ||
d6289b93 | 765 | if (!mtrr_valid(vcpu, msr, data)) |
9ba075a6 AK |
766 | return 1; |
767 | ||
0bed3b56 SY |
768 | if (msr == MSR_MTRRdefType) { |
769 | vcpu->arch.mtrr_state.def_type = data; | |
770 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; | |
771 | } else if (msr == MSR_MTRRfix64K_00000) | |
772 | p[0] = data; | |
773 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) | |
774 | p[1 + msr - MSR_MTRRfix16K_80000] = data; | |
775 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) | |
776 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; | |
777 | else if (msr == MSR_IA32_CR_PAT) | |
778 | vcpu->arch.pat = data; | |
779 | else { /* Variable MTRRs */ | |
780 | int idx, is_mtrr_mask; | |
781 | u64 *pt; | |
782 | ||
783 | idx = (msr - 0x200) / 2; | |
784 | is_mtrr_mask = msr - 0x200 - 2 * idx; | |
785 | if (!is_mtrr_mask) | |
786 | pt = | |
787 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; | |
788 | else | |
789 | pt = | |
790 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; | |
791 | *pt = data; | |
792 | } | |
793 | ||
794 | kvm_mmu_reset_context(vcpu); | |
9ba075a6 AK |
795 | return 0; |
796 | } | |
15c4a640 | 797 | |
890ca9ae | 798 | static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
15c4a640 | 799 | { |
890ca9ae HY |
800 | u64 mcg_cap = vcpu->arch.mcg_cap; |
801 | unsigned bank_num = mcg_cap & 0xff; | |
802 | ||
15c4a640 | 803 | switch (msr) { |
15c4a640 | 804 | case MSR_IA32_MCG_STATUS: |
890ca9ae | 805 | vcpu->arch.mcg_status = data; |
15c4a640 | 806 | break; |
c7ac679c | 807 | case MSR_IA32_MCG_CTL: |
890ca9ae HY |
808 | if (!(mcg_cap & MCG_CTL_P)) |
809 | return 1; | |
810 | if (data != 0 && data != ~(u64)0) | |
811 | return -1; | |
812 | vcpu->arch.mcg_ctl = data; | |
813 | break; | |
814 | default: | |
815 | if (msr >= MSR_IA32_MC0_CTL && | |
816 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { | |
817 | u32 offset = msr - MSR_IA32_MC0_CTL; | |
818 | /* only 0 or all 1s can be written to IA32_MCi_CTL */ | |
819 | if ((offset & 0x3) == 0 && | |
820 | data != 0 && data != ~(u64)0) | |
821 | return -1; | |
822 | vcpu->arch.mce_banks[offset] = data; | |
823 | break; | |
824 | } | |
825 | return 1; | |
826 | } | |
827 | return 0; | |
828 | } | |
829 | ||
830 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) | |
831 | { | |
832 | switch (msr) { | |
833 | case MSR_EFER: | |
834 | set_efer(vcpu, data); | |
c7ac679c | 835 | break; |
b5e2fec0 AG |
836 | case MSR_IA32_DEBUGCTLMSR: |
837 | if (!data) { | |
838 | /* We support the non-activated case already */ | |
839 | break; | |
840 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { | |
841 | /* Values other than LBR and BTF are vendor-specific, | |
842 | thus reserved and should throw a #GP */ | |
843 | return 1; | |
844 | } | |
845 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", | |
846 | __func__, data); | |
847 | break; | |
15c4a640 CO |
848 | case MSR_IA32_UCODE_REV: |
849 | case MSR_IA32_UCODE_WRITE: | |
61a6bd67 | 850 | case MSR_VM_HSAVE_PA: |
15c4a640 | 851 | break; |
9ba075a6 AK |
852 | case 0x200 ... 0x2ff: |
853 | return set_msr_mtrr(vcpu, msr, data); | |
15c4a640 CO |
854 | case MSR_IA32_APICBASE: |
855 | kvm_set_apic_base(vcpu, data); | |
856 | break; | |
857 | case MSR_IA32_MISC_ENABLE: | |
ad312c7c | 858 | vcpu->arch.ia32_misc_enable_msr = data; |
15c4a640 | 859 | break; |
18068523 GOC |
860 | case MSR_KVM_WALL_CLOCK: |
861 | vcpu->kvm->arch.wall_clock = data; | |
862 | kvm_write_wall_clock(vcpu->kvm, data); | |
863 | break; | |
864 | case MSR_KVM_SYSTEM_TIME: { | |
865 | if (vcpu->arch.time_page) { | |
866 | kvm_release_page_dirty(vcpu->arch.time_page); | |
867 | vcpu->arch.time_page = NULL; | |
868 | } | |
869 | ||
870 | vcpu->arch.time = data; | |
871 | ||
872 | /* we verify if the enable bit is set... */ | |
873 | if (!(data & 1)) | |
874 | break; | |
875 | ||
876 | /* ...but clean it before doing the actual write */ | |
877 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); | |
878 | ||
18068523 GOC |
879 | vcpu->arch.time_page = |
880 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); | |
18068523 GOC |
881 | |
882 | if (is_error_page(vcpu->arch.time_page)) { | |
883 | kvm_release_page_clean(vcpu->arch.time_page); | |
884 | vcpu->arch.time_page = NULL; | |
885 | } | |
886 | ||
c8076604 | 887 | kvm_request_guest_time_update(vcpu); |
18068523 GOC |
888 | break; |
889 | } | |
890ca9ae HY |
890 | case MSR_IA32_MCG_CTL: |
891 | case MSR_IA32_MCG_STATUS: | |
892 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: | |
893 | return set_msr_mce(vcpu, msr, data); | |
71db6023 AP |
894 | |
895 | /* Performance counters are not protected by a CPUID bit, | |
896 | * so we should check all of them in the generic path for the sake of | |
897 | * cross vendor migration. | |
898 | * Writing a zero into the event select MSRs disables them, | |
899 | * which we perfectly emulate ;-). Any other value should be at least | |
900 | * reported, some guests depend on them. | |
901 | */ | |
902 | case MSR_P6_EVNTSEL0: | |
903 | case MSR_P6_EVNTSEL1: | |
904 | case MSR_K7_EVNTSEL0: | |
905 | case MSR_K7_EVNTSEL1: | |
906 | case MSR_K7_EVNTSEL2: | |
907 | case MSR_K7_EVNTSEL3: | |
908 | if (data != 0) | |
909 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " | |
910 | "0x%x data 0x%llx\n", msr, data); | |
911 | break; | |
912 | /* at least RHEL 4 unconditionally writes to the perfctr registers, | |
913 | * so we ignore writes to make it happy. | |
914 | */ | |
915 | case MSR_P6_PERFCTR0: | |
916 | case MSR_P6_PERFCTR1: | |
917 | case MSR_K7_PERFCTR0: | |
918 | case MSR_K7_PERFCTR1: | |
919 | case MSR_K7_PERFCTR2: | |
920 | case MSR_K7_PERFCTR3: | |
921 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " | |
922 | "0x%x data 0x%llx\n", msr, data); | |
923 | break; | |
15c4a640 | 924 | default: |
565f1fbd | 925 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data); |
15c4a640 CO |
926 | return 1; |
927 | } | |
928 | return 0; | |
929 | } | |
930 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
931 | ||
932 | ||
933 | /* | |
934 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
935 | * Returns 0 on success, non-0 otherwise. | |
936 | * Assumes vcpu_load() was already called. | |
937 | */ | |
938 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
939 | { | |
940 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); | |
941 | } | |
942 | ||
9ba075a6 AK |
943 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
944 | { | |
0bed3b56 SY |
945 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
946 | ||
9ba075a6 AK |
947 | if (!msr_mtrr_valid(msr)) |
948 | return 1; | |
949 | ||
0bed3b56 SY |
950 | if (msr == MSR_MTRRdefType) |
951 | *pdata = vcpu->arch.mtrr_state.def_type + | |
952 | (vcpu->arch.mtrr_state.enabled << 10); | |
953 | else if (msr == MSR_MTRRfix64K_00000) | |
954 | *pdata = p[0]; | |
955 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) | |
956 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; | |
957 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) | |
958 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; | |
959 | else if (msr == MSR_IA32_CR_PAT) | |
960 | *pdata = vcpu->arch.pat; | |
961 | else { /* Variable MTRRs */ | |
962 | int idx, is_mtrr_mask; | |
963 | u64 *pt; | |
964 | ||
965 | idx = (msr - 0x200) / 2; | |
966 | is_mtrr_mask = msr - 0x200 - 2 * idx; | |
967 | if (!is_mtrr_mask) | |
968 | pt = | |
969 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; | |
970 | else | |
971 | pt = | |
972 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; | |
973 | *pdata = *pt; | |
974 | } | |
975 | ||
9ba075a6 AK |
976 | return 0; |
977 | } | |
978 | ||
890ca9ae | 979 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
15c4a640 CO |
980 | { |
981 | u64 data; | |
890ca9ae HY |
982 | u64 mcg_cap = vcpu->arch.mcg_cap; |
983 | unsigned bank_num = mcg_cap & 0xff; | |
15c4a640 CO |
984 | |
985 | switch (msr) { | |
15c4a640 CO |
986 | case MSR_IA32_P5_MC_ADDR: |
987 | case MSR_IA32_P5_MC_TYPE: | |
890ca9ae HY |
988 | data = 0; |
989 | break; | |
15c4a640 | 990 | case MSR_IA32_MCG_CAP: |
890ca9ae HY |
991 | data = vcpu->arch.mcg_cap; |
992 | break; | |
c7ac679c | 993 | case MSR_IA32_MCG_CTL: |
890ca9ae HY |
994 | if (!(mcg_cap & MCG_CTL_P)) |
995 | return 1; | |
996 | data = vcpu->arch.mcg_ctl; | |
997 | break; | |
998 | case MSR_IA32_MCG_STATUS: | |
999 | data = vcpu->arch.mcg_status; | |
1000 | break; | |
1001 | default: | |
1002 | if (msr >= MSR_IA32_MC0_CTL && | |
1003 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { | |
1004 | u32 offset = msr - MSR_IA32_MC0_CTL; | |
1005 | data = vcpu->arch.mce_banks[offset]; | |
1006 | break; | |
1007 | } | |
1008 | return 1; | |
1009 | } | |
1010 | *pdata = data; | |
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) | |
1015 | { | |
1016 | u64 data; | |
1017 | ||
1018 | switch (msr) { | |
890ca9ae | 1019 | case MSR_IA32_PLATFORM_ID: |
15c4a640 | 1020 | case MSR_IA32_UCODE_REV: |
15c4a640 | 1021 | case MSR_IA32_EBL_CR_POWERON: |
b5e2fec0 AG |
1022 | case MSR_IA32_DEBUGCTLMSR: |
1023 | case MSR_IA32_LASTBRANCHFROMIP: | |
1024 | case MSR_IA32_LASTBRANCHTOIP: | |
1025 | case MSR_IA32_LASTINTFROMIP: | |
1026 | case MSR_IA32_LASTINTTOIP: | |
60af2ecd JSR |
1027 | case MSR_K8_SYSCFG: |
1028 | case MSR_K7_HWCR: | |
61a6bd67 | 1029 | case MSR_VM_HSAVE_PA: |
7fe29e0f AS |
1030 | case MSR_P6_EVNTSEL0: |
1031 | case MSR_P6_EVNTSEL1: | |
9e699624 | 1032 | case MSR_K7_EVNTSEL0: |
15c4a640 CO |
1033 | data = 0; |
1034 | break; | |
9ba075a6 AK |
1035 | case MSR_MTRRcap: |
1036 | data = 0x500 | KVM_NR_VAR_MTRR; | |
1037 | break; | |
1038 | case 0x200 ... 0x2ff: | |
1039 | return get_msr_mtrr(vcpu, msr, pdata); | |
15c4a640 CO |
1040 | case 0xcd: /* fsb frequency */ |
1041 | data = 3; | |
1042 | break; | |
1043 | case MSR_IA32_APICBASE: | |
1044 | data = kvm_get_apic_base(vcpu); | |
1045 | break; | |
1046 | case MSR_IA32_MISC_ENABLE: | |
ad312c7c | 1047 | data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 1048 | break; |
847f0ad8 AG |
1049 | case MSR_IA32_PERF_STATUS: |
1050 | /* TSC increment by tick */ | |
1051 | data = 1000ULL; | |
1052 | /* CPU multiplier */ | |
1053 | data |= (((uint64_t)4ULL) << 40); | |
1054 | break; | |
15c4a640 | 1055 | case MSR_EFER: |
ad312c7c | 1056 | data = vcpu->arch.shadow_efer; |
15c4a640 | 1057 | break; |
18068523 GOC |
1058 | case MSR_KVM_WALL_CLOCK: |
1059 | data = vcpu->kvm->arch.wall_clock; | |
1060 | break; | |
1061 | case MSR_KVM_SYSTEM_TIME: | |
1062 | data = vcpu->arch.time; | |
1063 | break; | |
890ca9ae HY |
1064 | case MSR_IA32_P5_MC_ADDR: |
1065 | case MSR_IA32_P5_MC_TYPE: | |
1066 | case MSR_IA32_MCG_CAP: | |
1067 | case MSR_IA32_MCG_CTL: | |
1068 | case MSR_IA32_MCG_STATUS: | |
1069 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: | |
1070 | return get_msr_mce(vcpu, msr, pdata); | |
15c4a640 CO |
1071 | default: |
1072 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); | |
1073 | return 1; | |
1074 | } | |
1075 | *pdata = data; | |
1076 | return 0; | |
1077 | } | |
1078 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
1079 | ||
313a3dc7 CO |
1080 | /* |
1081 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
1082 | * | |
1083 | * @return number of msrs set successfully. | |
1084 | */ | |
1085 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
1086 | struct kvm_msr_entry *entries, | |
1087 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1088 | unsigned index, u64 *data)) | |
1089 | { | |
1090 | int i; | |
1091 | ||
1092 | vcpu_load(vcpu); | |
1093 | ||
3200f405 | 1094 | down_read(&vcpu->kvm->slots_lock); |
313a3dc7 CO |
1095 | for (i = 0; i < msrs->nmsrs; ++i) |
1096 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
1097 | break; | |
3200f405 | 1098 | up_read(&vcpu->kvm->slots_lock); |
313a3dc7 CO |
1099 | |
1100 | vcpu_put(vcpu); | |
1101 | ||
1102 | return i; | |
1103 | } | |
1104 | ||
1105 | /* | |
1106 | * Read or write a bunch of msrs. Parameters are user addresses. | |
1107 | * | |
1108 | * @return number of msrs set successfully. | |
1109 | */ | |
1110 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
1111 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
1112 | unsigned index, u64 *data), | |
1113 | int writeback) | |
1114 | { | |
1115 | struct kvm_msrs msrs; | |
1116 | struct kvm_msr_entry *entries; | |
1117 | int r, n; | |
1118 | unsigned size; | |
1119 | ||
1120 | r = -EFAULT; | |
1121 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) | |
1122 | goto out; | |
1123 | ||
1124 | r = -E2BIG; | |
1125 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
1126 | goto out; | |
1127 | ||
1128 | r = -ENOMEM; | |
1129 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; | |
1130 | entries = vmalloc(size); | |
1131 | if (!entries) | |
1132 | goto out; | |
1133 | ||
1134 | r = -EFAULT; | |
1135 | if (copy_from_user(entries, user_msrs->entries, size)) | |
1136 | goto out_free; | |
1137 | ||
1138 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
1139 | if (r < 0) | |
1140 | goto out_free; | |
1141 | ||
1142 | r = -EFAULT; | |
1143 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
1144 | goto out_free; | |
1145 | ||
1146 | r = n; | |
1147 | ||
1148 | out_free: | |
1149 | vfree(entries); | |
1150 | out: | |
1151 | return r; | |
1152 | } | |
1153 | ||
018d00d2 ZX |
1154 | int kvm_dev_ioctl_check_extension(long ext) |
1155 | { | |
1156 | int r; | |
1157 | ||
1158 | switch (ext) { | |
1159 | case KVM_CAP_IRQCHIP: | |
1160 | case KVM_CAP_HLT: | |
1161 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
018d00d2 | 1162 | case KVM_CAP_SET_TSS_ADDR: |
07716717 | 1163 | case KVM_CAP_EXT_CPUID: |
c8076604 | 1164 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 1165 | case KVM_CAP_PIT: |
a28e4f5a | 1166 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 1167 | case KVM_CAP_MP_STATE: |
ed848624 | 1168 | case KVM_CAP_SYNC_MMU: |
52d939a0 | 1169 | case KVM_CAP_REINJECT_CONTROL: |
4925663a | 1170 | case KVM_CAP_IRQ_INJECT_STATUS: |
e56d532f | 1171 | case KVM_CAP_ASSIGN_DEV_IRQ: |
721eecbf | 1172 | case KVM_CAP_IRQFD: |
c5ff41ce | 1173 | case KVM_CAP_PIT2: |
018d00d2 ZX |
1174 | r = 1; |
1175 | break; | |
542472b5 LV |
1176 | case KVM_CAP_COALESCED_MMIO: |
1177 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; | |
1178 | break; | |
774ead3a AK |
1179 | case KVM_CAP_VAPIC: |
1180 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); | |
1181 | break; | |
f725230a AK |
1182 | case KVM_CAP_NR_VCPUS: |
1183 | r = KVM_MAX_VCPUS; | |
1184 | break; | |
a988b910 AK |
1185 | case KVM_CAP_NR_MEMSLOTS: |
1186 | r = KVM_MEMORY_SLOTS; | |
1187 | break; | |
2f333bcb MT |
1188 | case KVM_CAP_PV_MMU: |
1189 | r = !tdp_enabled; | |
1190 | break; | |
62c476c7 | 1191 | case KVM_CAP_IOMMU: |
19de40a8 | 1192 | r = iommu_found(); |
62c476c7 | 1193 | break; |
890ca9ae HY |
1194 | case KVM_CAP_MCE: |
1195 | r = KVM_MAX_MCE_BANKS; | |
1196 | break; | |
018d00d2 ZX |
1197 | default: |
1198 | r = 0; | |
1199 | break; | |
1200 | } | |
1201 | return r; | |
1202 | ||
1203 | } | |
1204 | ||
043405e1 CO |
1205 | long kvm_arch_dev_ioctl(struct file *filp, |
1206 | unsigned int ioctl, unsigned long arg) | |
1207 | { | |
1208 | void __user *argp = (void __user *)arg; | |
1209 | long r; | |
1210 | ||
1211 | switch (ioctl) { | |
1212 | case KVM_GET_MSR_INDEX_LIST: { | |
1213 | struct kvm_msr_list __user *user_msr_list = argp; | |
1214 | struct kvm_msr_list msr_list; | |
1215 | unsigned n; | |
1216 | ||
1217 | r = -EFAULT; | |
1218 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) | |
1219 | goto out; | |
1220 | n = msr_list.nmsrs; | |
1221 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); | |
1222 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) | |
1223 | goto out; | |
1224 | r = -E2BIG; | |
e125e7b6 | 1225 | if (n < msr_list.nmsrs) |
043405e1 CO |
1226 | goto out; |
1227 | r = -EFAULT; | |
1228 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
1229 | num_msrs_to_save * sizeof(u32))) | |
1230 | goto out; | |
e125e7b6 | 1231 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
043405e1 CO |
1232 | &emulated_msrs, |
1233 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) | |
1234 | goto out; | |
1235 | r = 0; | |
1236 | break; | |
1237 | } | |
674eea0f AK |
1238 | case KVM_GET_SUPPORTED_CPUID: { |
1239 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1240 | struct kvm_cpuid2 cpuid; | |
1241 | ||
1242 | r = -EFAULT; | |
1243 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1244 | goto out; | |
1245 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, | |
19355475 | 1246 | cpuid_arg->entries); |
674eea0f AK |
1247 | if (r) |
1248 | goto out; | |
1249 | ||
1250 | r = -EFAULT; | |
1251 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
1252 | goto out; | |
1253 | r = 0; | |
1254 | break; | |
1255 | } | |
890ca9ae HY |
1256 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
1257 | u64 mce_cap; | |
1258 | ||
1259 | mce_cap = KVM_MCE_CAP_SUPPORTED; | |
1260 | r = -EFAULT; | |
1261 | if (copy_to_user(argp, &mce_cap, sizeof mce_cap)) | |
1262 | goto out; | |
1263 | r = 0; | |
1264 | break; | |
1265 | } | |
043405e1 CO |
1266 | default: |
1267 | r = -EINVAL; | |
1268 | } | |
1269 | out: | |
1270 | return r; | |
1271 | } | |
1272 | ||
313a3dc7 CO |
1273 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
1274 | { | |
1275 | kvm_x86_ops->vcpu_load(vcpu, cpu); | |
c8076604 | 1276 | kvm_request_guest_time_update(vcpu); |
313a3dc7 CO |
1277 | } |
1278 | ||
1279 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
1280 | { | |
1281 | kvm_x86_ops->vcpu_put(vcpu); | |
9327fd11 | 1282 | kvm_put_guest_fpu(vcpu); |
313a3dc7 CO |
1283 | } |
1284 | ||
07716717 | 1285 | static int is_efer_nx(void) |
313a3dc7 | 1286 | { |
e286e86e | 1287 | unsigned long long efer = 0; |
313a3dc7 | 1288 | |
e286e86e | 1289 | rdmsrl_safe(MSR_EFER, &efer); |
07716717 DK |
1290 | return efer & EFER_NX; |
1291 | } | |
1292 | ||
1293 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) | |
1294 | { | |
1295 | int i; | |
1296 | struct kvm_cpuid_entry2 *e, *entry; | |
1297 | ||
313a3dc7 | 1298 | entry = NULL; |
ad312c7c ZX |
1299 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
1300 | e = &vcpu->arch.cpuid_entries[i]; | |
313a3dc7 CO |
1301 | if (e->function == 0x80000001) { |
1302 | entry = e; | |
1303 | break; | |
1304 | } | |
1305 | } | |
07716717 | 1306 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
313a3dc7 CO |
1307 | entry->edx &= ~(1 << 20); |
1308 | printk(KERN_INFO "kvm: guest NX capability removed\n"); | |
1309 | } | |
1310 | } | |
1311 | ||
07716717 | 1312 | /* when an old userspace process fills a new kernel module */ |
313a3dc7 CO |
1313 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
1314 | struct kvm_cpuid *cpuid, | |
1315 | struct kvm_cpuid_entry __user *entries) | |
07716717 DK |
1316 | { |
1317 | int r, i; | |
1318 | struct kvm_cpuid_entry *cpuid_entries; | |
1319 | ||
1320 | r = -E2BIG; | |
1321 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
1322 | goto out; | |
1323 | r = -ENOMEM; | |
1324 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); | |
1325 | if (!cpuid_entries) | |
1326 | goto out; | |
1327 | r = -EFAULT; | |
1328 | if (copy_from_user(cpuid_entries, entries, | |
1329 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) | |
1330 | goto out_free; | |
1331 | for (i = 0; i < cpuid->nent; i++) { | |
ad312c7c ZX |
1332 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
1333 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; | |
1334 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; | |
1335 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; | |
1336 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; | |
1337 | vcpu->arch.cpuid_entries[i].index = 0; | |
1338 | vcpu->arch.cpuid_entries[i].flags = 0; | |
1339 | vcpu->arch.cpuid_entries[i].padding[0] = 0; | |
1340 | vcpu->arch.cpuid_entries[i].padding[1] = 0; | |
1341 | vcpu->arch.cpuid_entries[i].padding[2] = 0; | |
1342 | } | |
1343 | vcpu->arch.cpuid_nent = cpuid->nent; | |
07716717 DK |
1344 | cpuid_fix_nx_cap(vcpu); |
1345 | r = 0; | |
1346 | ||
1347 | out_free: | |
1348 | vfree(cpuid_entries); | |
1349 | out: | |
1350 | return r; | |
1351 | } | |
1352 | ||
1353 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, | |
19355475 AS |
1354 | struct kvm_cpuid2 *cpuid, |
1355 | struct kvm_cpuid_entry2 __user *entries) | |
313a3dc7 CO |
1356 | { |
1357 | int r; | |
1358 | ||
1359 | r = -E2BIG; | |
1360 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) | |
1361 | goto out; | |
1362 | r = -EFAULT; | |
ad312c7c | 1363 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
07716717 | 1364 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
313a3dc7 | 1365 | goto out; |
ad312c7c | 1366 | vcpu->arch.cpuid_nent = cpuid->nent; |
313a3dc7 CO |
1367 | return 0; |
1368 | ||
1369 | out: | |
1370 | return r; | |
1371 | } | |
1372 | ||
07716717 | 1373 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
19355475 AS |
1374 | struct kvm_cpuid2 *cpuid, |
1375 | struct kvm_cpuid_entry2 __user *entries) | |
07716717 DK |
1376 | { |
1377 | int r; | |
1378 | ||
1379 | r = -E2BIG; | |
ad312c7c | 1380 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
07716717 DK |
1381 | goto out; |
1382 | r = -EFAULT; | |
ad312c7c | 1383 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
19355475 | 1384 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
07716717 DK |
1385 | goto out; |
1386 | return 0; | |
1387 | ||
1388 | out: | |
ad312c7c | 1389 | cpuid->nent = vcpu->arch.cpuid_nent; |
07716717 DK |
1390 | return r; |
1391 | } | |
1392 | ||
07716717 | 1393 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
19355475 | 1394 | u32 index) |
07716717 DK |
1395 | { |
1396 | entry->function = function; | |
1397 | entry->index = index; | |
1398 | cpuid_count(entry->function, entry->index, | |
19355475 | 1399 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
07716717 DK |
1400 | entry->flags = 0; |
1401 | } | |
1402 | ||
7faa4ee1 AK |
1403 | #define F(x) bit(X86_FEATURE_##x) |
1404 | ||
07716717 DK |
1405 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
1406 | u32 index, int *nent, int maxnent) | |
1407 | { | |
7faa4ee1 | 1408 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
07716717 | 1409 | #ifdef CONFIG_X86_64 |
7faa4ee1 AK |
1410 | unsigned f_lm = F(LM); |
1411 | #else | |
1412 | unsigned f_lm = 0; | |
07716717 | 1413 | #endif |
7faa4ee1 AK |
1414 | |
1415 | /* cpuid 1.edx */ | |
1416 | const u32 kvm_supported_word0_x86_features = | |
1417 | F(FPU) | F(VME) | F(DE) | F(PSE) | | |
1418 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
1419 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | | |
1420 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
1421 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | | |
1422 | 0 /* Reserved, DS, ACPI */ | F(MMX) | | |
1423 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | | |
1424 | 0 /* HTT, TM, Reserved, PBE */; | |
1425 | /* cpuid 0x80000001.edx */ | |
1426 | const u32 kvm_supported_word1_x86_features = | |
1427 | F(FPU) | F(VME) | F(DE) | F(PSE) | | |
1428 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | | |
1429 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | | |
1430 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | | |
1431 | F(PAT) | F(PSE36) | 0 /* Reserved */ | | |
1432 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | | |
1433 | F(FXSR) | F(FXSR_OPT) | 0 /* GBPAGES */ | 0 /* RDTSCP */ | | |
1434 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); | |
1435 | /* cpuid 1.ecx */ | |
1436 | const u32 kvm_supported_word4_x86_features = | |
d149c731 AK |
1437 | F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ | |
1438 | 0 /* DS-CPL, VMX, SMX, EST */ | | |
1439 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | | |
1440 | 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ | | |
1441 | 0 /* Reserved, DCA */ | F(XMM4_1) | | |
1442 | F(XMM4_2) | 0 /* x2APIC */ | F(MOVBE) | F(POPCNT) | | |
1443 | 0 /* Reserved, XSAVE, OSXSAVE */; | |
7faa4ee1 | 1444 | /* cpuid 0x80000001.ecx */ |
07716717 | 1445 | const u32 kvm_supported_word6_x86_features = |
7faa4ee1 AK |
1446 | F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ | |
1447 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | | |
1448 | F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) | | |
1449 | 0 /* SKINIT */ | 0 /* WDT */; | |
07716717 | 1450 | |
19355475 | 1451 | /* all calls to cpuid_count() should be made on the same cpu */ |
07716717 DK |
1452 | get_cpu(); |
1453 | do_cpuid_1_ent(entry, function, index); | |
1454 | ++*nent; | |
1455 | ||
1456 | switch (function) { | |
1457 | case 0: | |
1458 | entry->eax = min(entry->eax, (u32)0xb); | |
1459 | break; | |
1460 | case 1: | |
1461 | entry->edx &= kvm_supported_word0_x86_features; | |
7faa4ee1 | 1462 | entry->ecx &= kvm_supported_word4_x86_features; |
07716717 DK |
1463 | break; |
1464 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands | |
1465 | * may return different values. This forces us to get_cpu() before | |
1466 | * issuing the first command, and also to emulate this annoying behavior | |
1467 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ | |
1468 | case 2: { | |
1469 | int t, times = entry->eax & 0xff; | |
1470 | ||
1471 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
0fdf8e59 | 1472 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
07716717 DK |
1473 | for (t = 1; t < times && *nent < maxnent; ++t) { |
1474 | do_cpuid_1_ent(&entry[t], function, 0); | |
1475 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; | |
1476 | ++*nent; | |
1477 | } | |
1478 | break; | |
1479 | } | |
1480 | /* function 4 and 0xb have additional index. */ | |
1481 | case 4: { | |
14af3f3c | 1482 | int i, cache_type; |
07716717 DK |
1483 | |
1484 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
1485 | /* read more entries until cache_type is zero */ | |
14af3f3c HH |
1486 | for (i = 1; *nent < maxnent; ++i) { |
1487 | cache_type = entry[i - 1].eax & 0x1f; | |
07716717 DK |
1488 | if (!cache_type) |
1489 | break; | |
14af3f3c HH |
1490 | do_cpuid_1_ent(&entry[i], function, i); |
1491 | entry[i].flags |= | |
07716717 DK |
1492 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
1493 | ++*nent; | |
1494 | } | |
1495 | break; | |
1496 | } | |
1497 | case 0xb: { | |
14af3f3c | 1498 | int i, level_type; |
07716717 DK |
1499 | |
1500 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
1501 | /* read more entries until level_type is zero */ | |
14af3f3c | 1502 | for (i = 1; *nent < maxnent; ++i) { |
0853d2c1 | 1503 | level_type = entry[i - 1].ecx & 0xff00; |
07716717 DK |
1504 | if (!level_type) |
1505 | break; | |
14af3f3c HH |
1506 | do_cpuid_1_ent(&entry[i], function, i); |
1507 | entry[i].flags |= | |
07716717 DK |
1508 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
1509 | ++*nent; | |
1510 | } | |
1511 | break; | |
1512 | } | |
1513 | case 0x80000000: | |
1514 | entry->eax = min(entry->eax, 0x8000001a); | |
1515 | break; | |
1516 | case 0x80000001: | |
1517 | entry->edx &= kvm_supported_word1_x86_features; | |
1518 | entry->ecx &= kvm_supported_word6_x86_features; | |
1519 | break; | |
1520 | } | |
1521 | put_cpu(); | |
1522 | } | |
1523 | ||
7faa4ee1 AK |
1524 | #undef F |
1525 | ||
674eea0f | 1526 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
19355475 | 1527 | struct kvm_cpuid_entry2 __user *entries) |
07716717 DK |
1528 | { |
1529 | struct kvm_cpuid_entry2 *cpuid_entries; | |
1530 | int limit, nent = 0, r = -E2BIG; | |
1531 | u32 func; | |
1532 | ||
1533 | if (cpuid->nent < 1) | |
1534 | goto out; | |
1535 | r = -ENOMEM; | |
1536 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); | |
1537 | if (!cpuid_entries) | |
1538 | goto out; | |
1539 | ||
1540 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); | |
1541 | limit = cpuid_entries[0].eax; | |
1542 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) | |
1543 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
19355475 | 1544 | &nent, cpuid->nent); |
07716717 DK |
1545 | r = -E2BIG; |
1546 | if (nent >= cpuid->nent) | |
1547 | goto out_free; | |
1548 | ||
1549 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); | |
1550 | limit = cpuid_entries[nent - 1].eax; | |
1551 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) | |
1552 | do_cpuid_ent(&cpuid_entries[nent], func, 0, | |
19355475 | 1553 | &nent, cpuid->nent); |
cb007648 MM |
1554 | r = -E2BIG; |
1555 | if (nent >= cpuid->nent) | |
1556 | goto out_free; | |
1557 | ||
07716717 DK |
1558 | r = -EFAULT; |
1559 | if (copy_to_user(entries, cpuid_entries, | |
19355475 | 1560 | nent * sizeof(struct kvm_cpuid_entry2))) |
07716717 DK |
1561 | goto out_free; |
1562 | cpuid->nent = nent; | |
1563 | r = 0; | |
1564 | ||
1565 | out_free: | |
1566 | vfree(cpuid_entries); | |
1567 | out: | |
1568 | return r; | |
1569 | } | |
1570 | ||
313a3dc7 CO |
1571 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
1572 | struct kvm_lapic_state *s) | |
1573 | { | |
1574 | vcpu_load(vcpu); | |
ad312c7c | 1575 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
313a3dc7 CO |
1576 | vcpu_put(vcpu); |
1577 | ||
1578 | return 0; | |
1579 | } | |
1580 | ||
1581 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
1582 | struct kvm_lapic_state *s) | |
1583 | { | |
1584 | vcpu_load(vcpu); | |
ad312c7c | 1585 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
313a3dc7 CO |
1586 | kvm_apic_post_state_restore(vcpu); |
1587 | vcpu_put(vcpu); | |
1588 | ||
1589 | return 0; | |
1590 | } | |
1591 | ||
f77bc6a4 ZX |
1592 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
1593 | struct kvm_interrupt *irq) | |
1594 | { | |
1595 | if (irq->irq < 0 || irq->irq >= 256) | |
1596 | return -EINVAL; | |
1597 | if (irqchip_in_kernel(vcpu->kvm)) | |
1598 | return -ENXIO; | |
1599 | vcpu_load(vcpu); | |
1600 | ||
66fd3f7f | 1601 | kvm_queue_interrupt(vcpu, irq->irq, false); |
f77bc6a4 ZX |
1602 | |
1603 | vcpu_put(vcpu); | |
1604 | ||
1605 | return 0; | |
1606 | } | |
1607 | ||
c4abb7c9 JK |
1608 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
1609 | { | |
1610 | vcpu_load(vcpu); | |
1611 | kvm_inject_nmi(vcpu); | |
1612 | vcpu_put(vcpu); | |
1613 | ||
1614 | return 0; | |
1615 | } | |
1616 | ||
b209749f AK |
1617 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
1618 | struct kvm_tpr_access_ctl *tac) | |
1619 | { | |
1620 | if (tac->flags) | |
1621 | return -EINVAL; | |
1622 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
1623 | return 0; | |
1624 | } | |
1625 | ||
890ca9ae HY |
1626 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
1627 | u64 mcg_cap) | |
1628 | { | |
1629 | int r; | |
1630 | unsigned bank_num = mcg_cap & 0xff, bank; | |
1631 | ||
1632 | r = -EINVAL; | |
1633 | if (!bank_num) | |
1634 | goto out; | |
1635 | if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000)) | |
1636 | goto out; | |
1637 | r = 0; | |
1638 | vcpu->arch.mcg_cap = mcg_cap; | |
1639 | /* Init IA32_MCG_CTL to all 1s */ | |
1640 | if (mcg_cap & MCG_CTL_P) | |
1641 | vcpu->arch.mcg_ctl = ~(u64)0; | |
1642 | /* Init IA32_MCi_CTL to all 1s */ | |
1643 | for (bank = 0; bank < bank_num; bank++) | |
1644 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; | |
1645 | out: | |
1646 | return r; | |
1647 | } | |
1648 | ||
1649 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, | |
1650 | struct kvm_x86_mce *mce) | |
1651 | { | |
1652 | u64 mcg_cap = vcpu->arch.mcg_cap; | |
1653 | unsigned bank_num = mcg_cap & 0xff; | |
1654 | u64 *banks = vcpu->arch.mce_banks; | |
1655 | ||
1656 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) | |
1657 | return -EINVAL; | |
1658 | /* | |
1659 | * if IA32_MCG_CTL is not all 1s, the uncorrected error | |
1660 | * reporting is disabled | |
1661 | */ | |
1662 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && | |
1663 | vcpu->arch.mcg_ctl != ~(u64)0) | |
1664 | return 0; | |
1665 | banks += 4 * mce->bank; | |
1666 | /* | |
1667 | * if IA32_MCi_CTL is not all 1s, the uncorrected error | |
1668 | * reporting is disabled for the bank | |
1669 | */ | |
1670 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) | |
1671 | return 0; | |
1672 | if (mce->status & MCI_STATUS_UC) { | |
1673 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || | |
1674 | !(vcpu->arch.cr4 & X86_CR4_MCE)) { | |
1675 | printk(KERN_DEBUG "kvm: set_mce: " | |
1676 | "injects mce exception while " | |
1677 | "previous one is in progress!\n"); | |
1678 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); | |
1679 | return 0; | |
1680 | } | |
1681 | if (banks[1] & MCI_STATUS_VAL) | |
1682 | mce->status |= MCI_STATUS_OVER; | |
1683 | banks[2] = mce->addr; | |
1684 | banks[3] = mce->misc; | |
1685 | vcpu->arch.mcg_status = mce->mcg_status; | |
1686 | banks[1] = mce->status; | |
1687 | kvm_queue_exception(vcpu, MC_VECTOR); | |
1688 | } else if (!(banks[1] & MCI_STATUS_VAL) | |
1689 | || !(banks[1] & MCI_STATUS_UC)) { | |
1690 | if (banks[1] & MCI_STATUS_VAL) | |
1691 | mce->status |= MCI_STATUS_OVER; | |
1692 | banks[2] = mce->addr; | |
1693 | banks[3] = mce->misc; | |
1694 | banks[1] = mce->status; | |
1695 | } else | |
1696 | banks[1] |= MCI_STATUS_OVER; | |
1697 | return 0; | |
1698 | } | |
1699 | ||
313a3dc7 CO |
1700 | long kvm_arch_vcpu_ioctl(struct file *filp, |
1701 | unsigned int ioctl, unsigned long arg) | |
1702 | { | |
1703 | struct kvm_vcpu *vcpu = filp->private_data; | |
1704 | void __user *argp = (void __user *)arg; | |
1705 | int r; | |
b772ff36 | 1706 | struct kvm_lapic_state *lapic = NULL; |
313a3dc7 CO |
1707 | |
1708 | switch (ioctl) { | |
1709 | case KVM_GET_LAPIC: { | |
b772ff36 | 1710 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
313a3dc7 | 1711 | |
b772ff36 DH |
1712 | r = -ENOMEM; |
1713 | if (!lapic) | |
1714 | goto out; | |
1715 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); | |
313a3dc7 CO |
1716 | if (r) |
1717 | goto out; | |
1718 | r = -EFAULT; | |
b772ff36 | 1719 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
313a3dc7 CO |
1720 | goto out; |
1721 | r = 0; | |
1722 | break; | |
1723 | } | |
1724 | case KVM_SET_LAPIC: { | |
b772ff36 DH |
1725 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
1726 | r = -ENOMEM; | |
1727 | if (!lapic) | |
1728 | goto out; | |
313a3dc7 | 1729 | r = -EFAULT; |
b772ff36 | 1730 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
313a3dc7 | 1731 | goto out; |
b772ff36 | 1732 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
313a3dc7 CO |
1733 | if (r) |
1734 | goto out; | |
1735 | r = 0; | |
1736 | break; | |
1737 | } | |
f77bc6a4 ZX |
1738 | case KVM_INTERRUPT: { |
1739 | struct kvm_interrupt irq; | |
1740 | ||
1741 | r = -EFAULT; | |
1742 | if (copy_from_user(&irq, argp, sizeof irq)) | |
1743 | goto out; | |
1744 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
1745 | if (r) | |
1746 | goto out; | |
1747 | r = 0; | |
1748 | break; | |
1749 | } | |
c4abb7c9 JK |
1750 | case KVM_NMI: { |
1751 | r = kvm_vcpu_ioctl_nmi(vcpu); | |
1752 | if (r) | |
1753 | goto out; | |
1754 | r = 0; | |
1755 | break; | |
1756 | } | |
313a3dc7 CO |
1757 | case KVM_SET_CPUID: { |
1758 | struct kvm_cpuid __user *cpuid_arg = argp; | |
1759 | struct kvm_cpuid cpuid; | |
1760 | ||
1761 | r = -EFAULT; | |
1762 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1763 | goto out; | |
1764 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
1765 | if (r) | |
1766 | goto out; | |
1767 | break; | |
1768 | } | |
07716717 DK |
1769 | case KVM_SET_CPUID2: { |
1770 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1771 | struct kvm_cpuid2 cpuid; | |
1772 | ||
1773 | r = -EFAULT; | |
1774 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1775 | goto out; | |
1776 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
19355475 | 1777 | cpuid_arg->entries); |
07716717 DK |
1778 | if (r) |
1779 | goto out; | |
1780 | break; | |
1781 | } | |
1782 | case KVM_GET_CPUID2: { | |
1783 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
1784 | struct kvm_cpuid2 cpuid; | |
1785 | ||
1786 | r = -EFAULT; | |
1787 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) | |
1788 | goto out; | |
1789 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
19355475 | 1790 | cpuid_arg->entries); |
07716717 DK |
1791 | if (r) |
1792 | goto out; | |
1793 | r = -EFAULT; | |
1794 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) | |
1795 | goto out; | |
1796 | r = 0; | |
1797 | break; | |
1798 | } | |
313a3dc7 CO |
1799 | case KVM_GET_MSRS: |
1800 | r = msr_io(vcpu, argp, kvm_get_msr, 1); | |
1801 | break; | |
1802 | case KVM_SET_MSRS: | |
1803 | r = msr_io(vcpu, argp, do_set_msr, 0); | |
1804 | break; | |
b209749f AK |
1805 | case KVM_TPR_ACCESS_REPORTING: { |
1806 | struct kvm_tpr_access_ctl tac; | |
1807 | ||
1808 | r = -EFAULT; | |
1809 | if (copy_from_user(&tac, argp, sizeof tac)) | |
1810 | goto out; | |
1811 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
1812 | if (r) | |
1813 | goto out; | |
1814 | r = -EFAULT; | |
1815 | if (copy_to_user(argp, &tac, sizeof tac)) | |
1816 | goto out; | |
1817 | r = 0; | |
1818 | break; | |
1819 | }; | |
b93463aa AK |
1820 | case KVM_SET_VAPIC_ADDR: { |
1821 | struct kvm_vapic_addr va; | |
1822 | ||
1823 | r = -EINVAL; | |
1824 | if (!irqchip_in_kernel(vcpu->kvm)) | |
1825 | goto out; | |
1826 | r = -EFAULT; | |
1827 | if (copy_from_user(&va, argp, sizeof va)) | |
1828 | goto out; | |
1829 | r = 0; | |
1830 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); | |
1831 | break; | |
1832 | } | |
890ca9ae HY |
1833 | case KVM_X86_SETUP_MCE: { |
1834 | u64 mcg_cap; | |
1835 | ||
1836 | r = -EFAULT; | |
1837 | if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap)) | |
1838 | goto out; | |
1839 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); | |
1840 | break; | |
1841 | } | |
1842 | case KVM_X86_SET_MCE: { | |
1843 | struct kvm_x86_mce mce; | |
1844 | ||
1845 | r = -EFAULT; | |
1846 | if (copy_from_user(&mce, argp, sizeof mce)) | |
1847 | goto out; | |
1848 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); | |
1849 | break; | |
1850 | } | |
313a3dc7 CO |
1851 | default: |
1852 | r = -EINVAL; | |
1853 | } | |
1854 | out: | |
7a6ce84c | 1855 | kfree(lapic); |
313a3dc7 CO |
1856 | return r; |
1857 | } | |
1858 | ||
1fe779f8 CO |
1859 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
1860 | { | |
1861 | int ret; | |
1862 | ||
1863 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
1864 | return -1; | |
1865 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); | |
1866 | return ret; | |
1867 | } | |
1868 | ||
1869 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, | |
1870 | u32 kvm_nr_mmu_pages) | |
1871 | { | |
1872 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
1873 | return -EINVAL; | |
1874 | ||
72dc67a6 | 1875 | down_write(&kvm->slots_lock); |
7c8a83b7 | 1876 | spin_lock(&kvm->mmu_lock); |
1fe779f8 CO |
1877 | |
1878 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 1879 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 1880 | |
7c8a83b7 | 1881 | spin_unlock(&kvm->mmu_lock); |
72dc67a6 | 1882 | up_write(&kvm->slots_lock); |
1fe779f8 CO |
1883 | return 0; |
1884 | } | |
1885 | ||
1886 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) | |
1887 | { | |
f05e70ac | 1888 | return kvm->arch.n_alloc_mmu_pages; |
1fe779f8 CO |
1889 | } |
1890 | ||
e9f85cde ZX |
1891 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
1892 | { | |
1893 | int i; | |
1894 | struct kvm_mem_alias *alias; | |
1895 | ||
d69fb81f ZX |
1896 | for (i = 0; i < kvm->arch.naliases; ++i) { |
1897 | alias = &kvm->arch.aliases[i]; | |
e9f85cde ZX |
1898 | if (gfn >= alias->base_gfn |
1899 | && gfn < alias->base_gfn + alias->npages) | |
1900 | return alias->target_gfn + gfn - alias->base_gfn; | |
1901 | } | |
1902 | return gfn; | |
1903 | } | |
1904 | ||
1fe779f8 CO |
1905 | /* |
1906 | * Set a new alias region. Aliases map a portion of physical memory into | |
1907 | * another portion. This is useful for memory windows, for example the PC | |
1908 | * VGA region. | |
1909 | */ | |
1910 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, | |
1911 | struct kvm_memory_alias *alias) | |
1912 | { | |
1913 | int r, n; | |
1914 | struct kvm_mem_alias *p; | |
1915 | ||
1916 | r = -EINVAL; | |
1917 | /* General sanity checks */ | |
1918 | if (alias->memory_size & (PAGE_SIZE - 1)) | |
1919 | goto out; | |
1920 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) | |
1921 | goto out; | |
1922 | if (alias->slot >= KVM_ALIAS_SLOTS) | |
1923 | goto out; | |
1924 | if (alias->guest_phys_addr + alias->memory_size | |
1925 | < alias->guest_phys_addr) | |
1926 | goto out; | |
1927 | if (alias->target_phys_addr + alias->memory_size | |
1928 | < alias->target_phys_addr) | |
1929 | goto out; | |
1930 | ||
72dc67a6 | 1931 | down_write(&kvm->slots_lock); |
a1708ce8 | 1932 | spin_lock(&kvm->mmu_lock); |
1fe779f8 | 1933 | |
d69fb81f | 1934 | p = &kvm->arch.aliases[alias->slot]; |
1fe779f8 CO |
1935 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
1936 | p->npages = alias->memory_size >> PAGE_SHIFT; | |
1937 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; | |
1938 | ||
1939 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) | |
d69fb81f | 1940 | if (kvm->arch.aliases[n - 1].npages) |
1fe779f8 | 1941 | break; |
d69fb81f | 1942 | kvm->arch.naliases = n; |
1fe779f8 | 1943 | |
a1708ce8 | 1944 | spin_unlock(&kvm->mmu_lock); |
1fe779f8 CO |
1945 | kvm_mmu_zap_all(kvm); |
1946 | ||
72dc67a6 | 1947 | up_write(&kvm->slots_lock); |
1fe779f8 CO |
1948 | |
1949 | return 0; | |
1950 | ||
1951 | out: | |
1952 | return r; | |
1953 | } | |
1954 | ||
1955 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1956 | { | |
1957 | int r; | |
1958 | ||
1959 | r = 0; | |
1960 | switch (chip->chip_id) { | |
1961 | case KVM_IRQCHIP_PIC_MASTER: | |
1962 | memcpy(&chip->chip.pic, | |
1963 | &pic_irqchip(kvm)->pics[0], | |
1964 | sizeof(struct kvm_pic_state)); | |
1965 | break; | |
1966 | case KVM_IRQCHIP_PIC_SLAVE: | |
1967 | memcpy(&chip->chip.pic, | |
1968 | &pic_irqchip(kvm)->pics[1], | |
1969 | sizeof(struct kvm_pic_state)); | |
1970 | break; | |
1971 | case KVM_IRQCHIP_IOAPIC: | |
1972 | memcpy(&chip->chip.ioapic, | |
1973 | ioapic_irqchip(kvm), | |
1974 | sizeof(struct kvm_ioapic_state)); | |
1975 | break; | |
1976 | default: | |
1977 | r = -EINVAL; | |
1978 | break; | |
1979 | } | |
1980 | return r; | |
1981 | } | |
1982 | ||
1983 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
1984 | { | |
1985 | int r; | |
1986 | ||
1987 | r = 0; | |
1988 | switch (chip->chip_id) { | |
1989 | case KVM_IRQCHIP_PIC_MASTER: | |
894a9c55 | 1990 | spin_lock(&pic_irqchip(kvm)->lock); |
1fe779f8 CO |
1991 | memcpy(&pic_irqchip(kvm)->pics[0], |
1992 | &chip->chip.pic, | |
1993 | sizeof(struct kvm_pic_state)); | |
894a9c55 | 1994 | spin_unlock(&pic_irqchip(kvm)->lock); |
1fe779f8 CO |
1995 | break; |
1996 | case KVM_IRQCHIP_PIC_SLAVE: | |
894a9c55 | 1997 | spin_lock(&pic_irqchip(kvm)->lock); |
1fe779f8 CO |
1998 | memcpy(&pic_irqchip(kvm)->pics[1], |
1999 | &chip->chip.pic, | |
2000 | sizeof(struct kvm_pic_state)); | |
894a9c55 | 2001 | spin_unlock(&pic_irqchip(kvm)->lock); |
1fe779f8 CO |
2002 | break; |
2003 | case KVM_IRQCHIP_IOAPIC: | |
894a9c55 | 2004 | mutex_lock(&kvm->irq_lock); |
1fe779f8 CO |
2005 | memcpy(ioapic_irqchip(kvm), |
2006 | &chip->chip.ioapic, | |
2007 | sizeof(struct kvm_ioapic_state)); | |
894a9c55 | 2008 | mutex_unlock(&kvm->irq_lock); |
1fe779f8 CO |
2009 | break; |
2010 | default: | |
2011 | r = -EINVAL; | |
2012 | break; | |
2013 | } | |
2014 | kvm_pic_update_irq(pic_irqchip(kvm)); | |
2015 | return r; | |
2016 | } | |
2017 | ||
e0f63cb9 SY |
2018 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
2019 | { | |
2020 | int r = 0; | |
2021 | ||
894a9c55 | 2022 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
e0f63cb9 | 2023 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
894a9c55 | 2024 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
e0f63cb9 SY |
2025 | return r; |
2026 | } | |
2027 | ||
2028 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
2029 | { | |
2030 | int r = 0; | |
2031 | ||
894a9c55 | 2032 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
e0f63cb9 SY |
2033 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
2034 | kvm_pit_load_count(kvm, 0, ps->channels[0].count); | |
894a9c55 | 2035 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
e0f63cb9 SY |
2036 | return r; |
2037 | } | |
2038 | ||
52d939a0 MT |
2039 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
2040 | struct kvm_reinject_control *control) | |
2041 | { | |
2042 | if (!kvm->arch.vpit) | |
2043 | return -ENXIO; | |
894a9c55 | 2044 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
52d939a0 | 2045 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; |
894a9c55 | 2046 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
52d939a0 MT |
2047 | return 0; |
2048 | } | |
2049 | ||
5bb064dc ZX |
2050 | /* |
2051 | * Get (and clear) the dirty memory log for a memory slot. | |
2052 | */ | |
2053 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
2054 | struct kvm_dirty_log *log) | |
2055 | { | |
2056 | int r; | |
2057 | int n; | |
2058 | struct kvm_memory_slot *memslot; | |
2059 | int is_dirty = 0; | |
2060 | ||
72dc67a6 | 2061 | down_write(&kvm->slots_lock); |
5bb064dc ZX |
2062 | |
2063 | r = kvm_get_dirty_log(kvm, log, &is_dirty); | |
2064 | if (r) | |
2065 | goto out; | |
2066 | ||
2067 | /* If nothing is dirty, don't bother messing with page tables. */ | |
2068 | if (is_dirty) { | |
7c8a83b7 | 2069 | spin_lock(&kvm->mmu_lock); |
5bb064dc | 2070 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
7c8a83b7 | 2071 | spin_unlock(&kvm->mmu_lock); |
5bb064dc ZX |
2072 | kvm_flush_remote_tlbs(kvm); |
2073 | memslot = &kvm->memslots[log->slot]; | |
2074 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; | |
2075 | memset(memslot->dirty_bitmap, 0, n); | |
2076 | } | |
2077 | r = 0; | |
2078 | out: | |
72dc67a6 | 2079 | up_write(&kvm->slots_lock); |
5bb064dc ZX |
2080 | return r; |
2081 | } | |
2082 | ||
1fe779f8 CO |
2083 | long kvm_arch_vm_ioctl(struct file *filp, |
2084 | unsigned int ioctl, unsigned long arg) | |
2085 | { | |
2086 | struct kvm *kvm = filp->private_data; | |
2087 | void __user *argp = (void __user *)arg; | |
2088 | int r = -EINVAL; | |
f0d66275 DH |
2089 | /* |
2090 | * This union makes it completely explicit to gcc-3.x | |
2091 | * that these two variables' stack usage should be | |
2092 | * combined, not added together. | |
2093 | */ | |
2094 | union { | |
2095 | struct kvm_pit_state ps; | |
2096 | struct kvm_memory_alias alias; | |
c5ff41ce | 2097 | struct kvm_pit_config pit_config; |
f0d66275 | 2098 | } u; |
1fe779f8 CO |
2099 | |
2100 | switch (ioctl) { | |
2101 | case KVM_SET_TSS_ADDR: | |
2102 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
2103 | if (r < 0) | |
2104 | goto out; | |
2105 | break; | |
2106 | case KVM_SET_MEMORY_REGION: { | |
2107 | struct kvm_memory_region kvm_mem; | |
2108 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
2109 | ||
2110 | r = -EFAULT; | |
2111 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) | |
2112 | goto out; | |
2113 | kvm_userspace_mem.slot = kvm_mem.slot; | |
2114 | kvm_userspace_mem.flags = kvm_mem.flags; | |
2115 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; | |
2116 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; | |
2117 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); | |
2118 | if (r) | |
2119 | goto out; | |
2120 | break; | |
2121 | } | |
2122 | case KVM_SET_NR_MMU_PAGES: | |
2123 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
2124 | if (r) | |
2125 | goto out; | |
2126 | break; | |
2127 | case KVM_GET_NR_MMU_PAGES: | |
2128 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
2129 | break; | |
f0d66275 | 2130 | case KVM_SET_MEMORY_ALIAS: |
1fe779f8 | 2131 | r = -EFAULT; |
f0d66275 | 2132 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
1fe779f8 | 2133 | goto out; |
f0d66275 | 2134 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
1fe779f8 CO |
2135 | if (r) |
2136 | goto out; | |
2137 | break; | |
1fe779f8 CO |
2138 | case KVM_CREATE_IRQCHIP: |
2139 | r = -ENOMEM; | |
d7deeeb0 ZX |
2140 | kvm->arch.vpic = kvm_create_pic(kvm); |
2141 | if (kvm->arch.vpic) { | |
1fe779f8 CO |
2142 | r = kvm_ioapic_init(kvm); |
2143 | if (r) { | |
d7deeeb0 ZX |
2144 | kfree(kvm->arch.vpic); |
2145 | kvm->arch.vpic = NULL; | |
1fe779f8 CO |
2146 | goto out; |
2147 | } | |
2148 | } else | |
2149 | goto out; | |
399ec807 AK |
2150 | r = kvm_setup_default_irq_routing(kvm); |
2151 | if (r) { | |
2152 | kfree(kvm->arch.vpic); | |
2153 | kfree(kvm->arch.vioapic); | |
2154 | goto out; | |
2155 | } | |
1fe779f8 | 2156 | break; |
7837699f | 2157 | case KVM_CREATE_PIT: |
c5ff41ce JK |
2158 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
2159 | goto create_pit; | |
2160 | case KVM_CREATE_PIT2: | |
2161 | r = -EFAULT; | |
2162 | if (copy_from_user(&u.pit_config, argp, | |
2163 | sizeof(struct kvm_pit_config))) | |
2164 | goto out; | |
2165 | create_pit: | |
269e05e4 AK |
2166 | mutex_lock(&kvm->lock); |
2167 | r = -EEXIST; | |
2168 | if (kvm->arch.vpit) | |
2169 | goto create_pit_unlock; | |
7837699f | 2170 | r = -ENOMEM; |
c5ff41ce | 2171 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
7837699f SY |
2172 | if (kvm->arch.vpit) |
2173 | r = 0; | |
269e05e4 AK |
2174 | create_pit_unlock: |
2175 | mutex_unlock(&kvm->lock); | |
7837699f | 2176 | break; |
4925663a | 2177 | case KVM_IRQ_LINE_STATUS: |
1fe779f8 CO |
2178 | case KVM_IRQ_LINE: { |
2179 | struct kvm_irq_level irq_event; | |
2180 | ||
2181 | r = -EFAULT; | |
2182 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) | |
2183 | goto out; | |
2184 | if (irqchip_in_kernel(kvm)) { | |
4925663a | 2185 | __s32 status; |
fa40a821 | 2186 | mutex_lock(&kvm->irq_lock); |
4925663a GN |
2187 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
2188 | irq_event.irq, irq_event.level); | |
fa40a821 | 2189 | mutex_unlock(&kvm->irq_lock); |
4925663a GN |
2190 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
2191 | irq_event.status = status; | |
2192 | if (copy_to_user(argp, &irq_event, | |
2193 | sizeof irq_event)) | |
2194 | goto out; | |
2195 | } | |
1fe779f8 CO |
2196 | r = 0; |
2197 | } | |
2198 | break; | |
2199 | } | |
2200 | case KVM_GET_IRQCHIP: { | |
2201 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
f0d66275 | 2202 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
1fe779f8 | 2203 | |
f0d66275 DH |
2204 | r = -ENOMEM; |
2205 | if (!chip) | |
1fe779f8 | 2206 | goto out; |
f0d66275 DH |
2207 | r = -EFAULT; |
2208 | if (copy_from_user(chip, argp, sizeof *chip)) | |
2209 | goto get_irqchip_out; | |
1fe779f8 CO |
2210 | r = -ENXIO; |
2211 | if (!irqchip_in_kernel(kvm)) | |
f0d66275 DH |
2212 | goto get_irqchip_out; |
2213 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); | |
1fe779f8 | 2214 | if (r) |
f0d66275 | 2215 | goto get_irqchip_out; |
1fe779f8 | 2216 | r = -EFAULT; |
f0d66275 DH |
2217 | if (copy_to_user(argp, chip, sizeof *chip)) |
2218 | goto get_irqchip_out; | |
1fe779f8 | 2219 | r = 0; |
f0d66275 DH |
2220 | get_irqchip_out: |
2221 | kfree(chip); | |
2222 | if (r) | |
2223 | goto out; | |
1fe779f8 CO |
2224 | break; |
2225 | } | |
2226 | case KVM_SET_IRQCHIP: { | |
2227 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
f0d66275 | 2228 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
1fe779f8 | 2229 | |
f0d66275 DH |
2230 | r = -ENOMEM; |
2231 | if (!chip) | |
1fe779f8 | 2232 | goto out; |
f0d66275 DH |
2233 | r = -EFAULT; |
2234 | if (copy_from_user(chip, argp, sizeof *chip)) | |
2235 | goto set_irqchip_out; | |
1fe779f8 CO |
2236 | r = -ENXIO; |
2237 | if (!irqchip_in_kernel(kvm)) | |
f0d66275 DH |
2238 | goto set_irqchip_out; |
2239 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); | |
1fe779f8 | 2240 | if (r) |
f0d66275 | 2241 | goto set_irqchip_out; |
1fe779f8 | 2242 | r = 0; |
f0d66275 DH |
2243 | set_irqchip_out: |
2244 | kfree(chip); | |
2245 | if (r) | |
2246 | goto out; | |
1fe779f8 CO |
2247 | break; |
2248 | } | |
e0f63cb9 | 2249 | case KVM_GET_PIT: { |
e0f63cb9 | 2250 | r = -EFAULT; |
f0d66275 | 2251 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
2252 | goto out; |
2253 | r = -ENXIO; | |
2254 | if (!kvm->arch.vpit) | |
2255 | goto out; | |
f0d66275 | 2256 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
e0f63cb9 SY |
2257 | if (r) |
2258 | goto out; | |
2259 | r = -EFAULT; | |
f0d66275 | 2260 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
2261 | goto out; |
2262 | r = 0; | |
2263 | break; | |
2264 | } | |
2265 | case KVM_SET_PIT: { | |
e0f63cb9 | 2266 | r = -EFAULT; |
f0d66275 | 2267 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
e0f63cb9 SY |
2268 | goto out; |
2269 | r = -ENXIO; | |
2270 | if (!kvm->arch.vpit) | |
2271 | goto out; | |
f0d66275 | 2272 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
e0f63cb9 SY |
2273 | if (r) |
2274 | goto out; | |
2275 | r = 0; | |
2276 | break; | |
2277 | } | |
52d939a0 MT |
2278 | case KVM_REINJECT_CONTROL: { |
2279 | struct kvm_reinject_control control; | |
2280 | r = -EFAULT; | |
2281 | if (copy_from_user(&control, argp, sizeof(control))) | |
2282 | goto out; | |
2283 | r = kvm_vm_ioctl_reinject(kvm, &control); | |
2284 | if (r) | |
2285 | goto out; | |
2286 | r = 0; | |
2287 | break; | |
2288 | } | |
1fe779f8 CO |
2289 | default: |
2290 | ; | |
2291 | } | |
2292 | out: | |
2293 | return r; | |
2294 | } | |
2295 | ||
a16b043c | 2296 | static void kvm_init_msr_list(void) |
043405e1 CO |
2297 | { |
2298 | u32 dummy[2]; | |
2299 | unsigned i, j; | |
2300 | ||
2301 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { | |
2302 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) | |
2303 | continue; | |
2304 | if (j < i) | |
2305 | msrs_to_save[j] = msrs_to_save[i]; | |
2306 | j++; | |
2307 | } | |
2308 | num_msrs_to_save = j; | |
2309 | } | |
2310 | ||
bbd9b64e CO |
2311 | /* |
2312 | * Only apic need an MMIO device hook, so shortcut now.. | |
2313 | */ | |
2314 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, | |
92760499 LV |
2315 | gpa_t addr, int len, |
2316 | int is_write) | |
bbd9b64e CO |
2317 | { |
2318 | struct kvm_io_device *dev; | |
2319 | ||
ad312c7c ZX |
2320 | if (vcpu->arch.apic) { |
2321 | dev = &vcpu->arch.apic->dev; | |
d76685c4 | 2322 | if (kvm_iodevice_in_range(dev, addr, len, is_write)) |
bbd9b64e CO |
2323 | return dev; |
2324 | } | |
2325 | return NULL; | |
2326 | } | |
2327 | ||
2328 | ||
2329 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, | |
92760499 LV |
2330 | gpa_t addr, int len, |
2331 | int is_write) | |
bbd9b64e CO |
2332 | { |
2333 | struct kvm_io_device *dev; | |
2334 | ||
92760499 | 2335 | dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write); |
bbd9b64e | 2336 | if (dev == NULL) |
92760499 LV |
2337 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, |
2338 | is_write); | |
bbd9b64e CO |
2339 | return dev; |
2340 | } | |
2341 | ||
cded19f3 HE |
2342 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
2343 | struct kvm_vcpu *vcpu) | |
bbd9b64e CO |
2344 | { |
2345 | void *data = val; | |
10589a46 | 2346 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
2347 | |
2348 | while (bytes) { | |
ad312c7c | 2349 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e | 2350 | unsigned offset = addr & (PAGE_SIZE-1); |
77c2002e | 2351 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
bbd9b64e CO |
2352 | int ret; |
2353 | ||
10589a46 MT |
2354 | if (gpa == UNMAPPED_GVA) { |
2355 | r = X86EMUL_PROPAGATE_FAULT; | |
2356 | goto out; | |
2357 | } | |
77c2002e | 2358 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); |
10589a46 MT |
2359 | if (ret < 0) { |
2360 | r = X86EMUL_UNHANDLEABLE; | |
2361 | goto out; | |
2362 | } | |
bbd9b64e | 2363 | |
77c2002e IE |
2364 | bytes -= toread; |
2365 | data += toread; | |
2366 | addr += toread; | |
bbd9b64e | 2367 | } |
10589a46 | 2368 | out: |
10589a46 | 2369 | return r; |
bbd9b64e | 2370 | } |
77c2002e | 2371 | |
cded19f3 HE |
2372 | static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, |
2373 | struct kvm_vcpu *vcpu) | |
77c2002e IE |
2374 | { |
2375 | void *data = val; | |
2376 | int r = X86EMUL_CONTINUE; | |
2377 | ||
2378 | while (bytes) { | |
2379 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); | |
2380 | unsigned offset = addr & (PAGE_SIZE-1); | |
2381 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); | |
2382 | int ret; | |
2383 | ||
2384 | if (gpa == UNMAPPED_GVA) { | |
2385 | r = X86EMUL_PROPAGATE_FAULT; | |
2386 | goto out; | |
2387 | } | |
2388 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); | |
2389 | if (ret < 0) { | |
2390 | r = X86EMUL_UNHANDLEABLE; | |
2391 | goto out; | |
2392 | } | |
2393 | ||
2394 | bytes -= towrite; | |
2395 | data += towrite; | |
2396 | addr += towrite; | |
2397 | } | |
2398 | out: | |
2399 | return r; | |
2400 | } | |
2401 | ||
bbd9b64e | 2402 | |
bbd9b64e CO |
2403 | static int emulator_read_emulated(unsigned long addr, |
2404 | void *val, | |
2405 | unsigned int bytes, | |
2406 | struct kvm_vcpu *vcpu) | |
2407 | { | |
2408 | struct kvm_io_device *mmio_dev; | |
2409 | gpa_t gpa; | |
2410 | ||
2411 | if (vcpu->mmio_read_completed) { | |
2412 | memcpy(val, vcpu->mmio_data, bytes); | |
2413 | vcpu->mmio_read_completed = 0; | |
2414 | return X86EMUL_CONTINUE; | |
2415 | } | |
2416 | ||
ad312c7c | 2417 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e CO |
2418 | |
2419 | /* For APIC access vmexit */ | |
2420 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
2421 | goto mmio; | |
2422 | ||
77c2002e IE |
2423 | if (kvm_read_guest_virt(addr, val, bytes, vcpu) |
2424 | == X86EMUL_CONTINUE) | |
bbd9b64e CO |
2425 | return X86EMUL_CONTINUE; |
2426 | if (gpa == UNMAPPED_GVA) | |
2427 | return X86EMUL_PROPAGATE_FAULT; | |
2428 | ||
2429 | mmio: | |
2430 | /* | |
2431 | * Is this MMIO handled locally? | |
2432 | */ | |
10589a46 | 2433 | mutex_lock(&vcpu->kvm->lock); |
92760499 | 2434 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0); |
fa40a821 | 2435 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
2436 | if (mmio_dev) { |
2437 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); | |
2438 | return X86EMUL_CONTINUE; | |
2439 | } | |
2440 | ||
2441 | vcpu->mmio_needed = 1; | |
2442 | vcpu->mmio_phys_addr = gpa; | |
2443 | vcpu->mmio_size = bytes; | |
2444 | vcpu->mmio_is_write = 0; | |
2445 | ||
2446 | return X86EMUL_UNHANDLEABLE; | |
2447 | } | |
2448 | ||
3200f405 | 2449 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
9f811285 | 2450 | const void *val, int bytes) |
bbd9b64e CO |
2451 | { |
2452 | int ret; | |
2453 | ||
2454 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); | |
9f811285 | 2455 | if (ret < 0) |
bbd9b64e | 2456 | return 0; |
ad218f85 | 2457 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
bbd9b64e CO |
2458 | return 1; |
2459 | } | |
2460 | ||
2461 | static int emulator_write_emulated_onepage(unsigned long addr, | |
2462 | const void *val, | |
2463 | unsigned int bytes, | |
2464 | struct kvm_vcpu *vcpu) | |
2465 | { | |
2466 | struct kvm_io_device *mmio_dev; | |
10589a46 MT |
2467 | gpa_t gpa; |
2468 | ||
10589a46 | 2469 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
bbd9b64e CO |
2470 | |
2471 | if (gpa == UNMAPPED_GVA) { | |
c3c91fee | 2472 | kvm_inject_page_fault(vcpu, addr, 2); |
bbd9b64e CO |
2473 | return X86EMUL_PROPAGATE_FAULT; |
2474 | } | |
2475 | ||
2476 | /* For APIC access vmexit */ | |
2477 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
2478 | goto mmio; | |
2479 | ||
2480 | if (emulator_write_phys(vcpu, gpa, val, bytes)) | |
2481 | return X86EMUL_CONTINUE; | |
2482 | ||
2483 | mmio: | |
2484 | /* | |
2485 | * Is this MMIO handled locally? | |
2486 | */ | |
10589a46 | 2487 | mutex_lock(&vcpu->kvm->lock); |
92760499 | 2488 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1); |
fa40a821 | 2489 | mutex_unlock(&vcpu->kvm->lock); |
bbd9b64e CO |
2490 | if (mmio_dev) { |
2491 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); | |
2492 | return X86EMUL_CONTINUE; | |
2493 | } | |
2494 | ||
2495 | vcpu->mmio_needed = 1; | |
2496 | vcpu->mmio_phys_addr = gpa; | |
2497 | vcpu->mmio_size = bytes; | |
2498 | vcpu->mmio_is_write = 1; | |
2499 | memcpy(vcpu->mmio_data, val, bytes); | |
2500 | ||
2501 | return X86EMUL_CONTINUE; | |
2502 | } | |
2503 | ||
2504 | int emulator_write_emulated(unsigned long addr, | |
2505 | const void *val, | |
2506 | unsigned int bytes, | |
2507 | struct kvm_vcpu *vcpu) | |
2508 | { | |
2509 | /* Crossing a page boundary? */ | |
2510 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
2511 | int rc, now; | |
2512 | ||
2513 | now = -addr & ~PAGE_MASK; | |
2514 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); | |
2515 | if (rc != X86EMUL_CONTINUE) | |
2516 | return rc; | |
2517 | addr += now; | |
2518 | val += now; | |
2519 | bytes -= now; | |
2520 | } | |
2521 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); | |
2522 | } | |
2523 | EXPORT_SYMBOL_GPL(emulator_write_emulated); | |
2524 | ||
2525 | static int emulator_cmpxchg_emulated(unsigned long addr, | |
2526 | const void *old, | |
2527 | const void *new, | |
2528 | unsigned int bytes, | |
2529 | struct kvm_vcpu *vcpu) | |
2530 | { | |
2531 | static int reported; | |
2532 | ||
2533 | if (!reported) { | |
2534 | reported = 1; | |
2535 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); | |
2536 | } | |
2bacc55c MT |
2537 | #ifndef CONFIG_X86_64 |
2538 | /* guests cmpxchg8b have to be emulated atomically */ | |
2539 | if (bytes == 8) { | |
10589a46 | 2540 | gpa_t gpa; |
2bacc55c | 2541 | struct page *page; |
c0b49b0d | 2542 | char *kaddr; |
2bacc55c MT |
2543 | u64 val; |
2544 | ||
10589a46 MT |
2545 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
2546 | ||
2bacc55c MT |
2547 | if (gpa == UNMAPPED_GVA || |
2548 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
2549 | goto emul_write; | |
2550 | ||
2551 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) | |
2552 | goto emul_write; | |
2553 | ||
2554 | val = *(u64 *)new; | |
72dc67a6 | 2555 | |
2bacc55c | 2556 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
72dc67a6 | 2557 | |
c0b49b0d AM |
2558 | kaddr = kmap_atomic(page, KM_USER0); |
2559 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); | |
2560 | kunmap_atomic(kaddr, KM_USER0); | |
2bacc55c MT |
2561 | kvm_release_page_dirty(page); |
2562 | } | |
3200f405 | 2563 | emul_write: |
2bacc55c MT |
2564 | #endif |
2565 | ||
bbd9b64e CO |
2566 | return emulator_write_emulated(addr, new, bytes, vcpu); |
2567 | } | |
2568 | ||
2569 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
2570 | { | |
2571 | return kvm_x86_ops->get_segment_base(vcpu, seg); | |
2572 | } | |
2573 | ||
2574 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) | |
2575 | { | |
a7052897 | 2576 | kvm_mmu_invlpg(vcpu, address); |
bbd9b64e CO |
2577 | return X86EMUL_CONTINUE; |
2578 | } | |
2579 | ||
2580 | int emulate_clts(struct kvm_vcpu *vcpu) | |
2581 | { | |
ad312c7c | 2582 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS); |
bbd9b64e CO |
2583 | return X86EMUL_CONTINUE; |
2584 | } | |
2585 | ||
2586 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) | |
2587 | { | |
2588 | struct kvm_vcpu *vcpu = ctxt->vcpu; | |
2589 | ||
2590 | switch (dr) { | |
2591 | case 0 ... 3: | |
2592 | *dest = kvm_x86_ops->get_dr(vcpu, dr); | |
2593 | return X86EMUL_CONTINUE; | |
2594 | default: | |
b8688d51 | 2595 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr); |
bbd9b64e CO |
2596 | return X86EMUL_UNHANDLEABLE; |
2597 | } | |
2598 | } | |
2599 | ||
2600 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) | |
2601 | { | |
2602 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; | |
2603 | int exception; | |
2604 | ||
2605 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); | |
2606 | if (exception) { | |
2607 | /* FIXME: better handling */ | |
2608 | return X86EMUL_UNHANDLEABLE; | |
2609 | } | |
2610 | return X86EMUL_CONTINUE; | |
2611 | } | |
2612 | ||
2613 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | |
2614 | { | |
bbd9b64e | 2615 | u8 opcodes[4]; |
5fdbf976 | 2616 | unsigned long rip = kvm_rip_read(vcpu); |
bbd9b64e CO |
2617 | unsigned long rip_linear; |
2618 | ||
f76c710d | 2619 | if (!printk_ratelimit()) |
bbd9b64e CO |
2620 | return; |
2621 | ||
25be4608 GC |
2622 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
2623 | ||
77c2002e | 2624 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu); |
bbd9b64e CO |
2625 | |
2626 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", | |
2627 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | |
bbd9b64e CO |
2628 | } |
2629 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); | |
2630 | ||
14af3f3c | 2631 | static struct x86_emulate_ops emulate_ops = { |
77c2002e | 2632 | .read_std = kvm_read_guest_virt, |
bbd9b64e CO |
2633 | .read_emulated = emulator_read_emulated, |
2634 | .write_emulated = emulator_write_emulated, | |
2635 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
2636 | }; | |
2637 | ||
5fdbf976 MT |
2638 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
2639 | { | |
2640 | kvm_register_read(vcpu, VCPU_REGS_RAX); | |
2641 | kvm_register_read(vcpu, VCPU_REGS_RSP); | |
2642 | kvm_register_read(vcpu, VCPU_REGS_RIP); | |
2643 | vcpu->arch.regs_dirty = ~0; | |
2644 | } | |
2645 | ||
bbd9b64e CO |
2646 | int emulate_instruction(struct kvm_vcpu *vcpu, |
2647 | struct kvm_run *run, | |
2648 | unsigned long cr2, | |
2649 | u16 error_code, | |
571008da | 2650 | int emulation_type) |
bbd9b64e | 2651 | { |
310b5d30 | 2652 | int r, shadow_mask; |
571008da | 2653 | struct decode_cache *c; |
bbd9b64e | 2654 | |
26eef70c | 2655 | kvm_clear_exception_queue(vcpu); |
ad312c7c | 2656 | vcpu->arch.mmio_fault_cr2 = cr2; |
5fdbf976 MT |
2657 | /* |
2658 | * TODO: fix x86_emulate.c to use guest_read/write_register | |
2659 | * instead of direct ->regs accesses, can save hundred cycles | |
2660 | * on Intel for instructions that don't read/change RSP, for | |
2661 | * for example. | |
2662 | */ | |
2663 | cache_all_regs(vcpu); | |
bbd9b64e CO |
2664 | |
2665 | vcpu->mmio_is_write = 0; | |
ad312c7c | 2666 | vcpu->arch.pio.string = 0; |
bbd9b64e | 2667 | |
571008da | 2668 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
bbd9b64e CO |
2669 | int cs_db, cs_l; |
2670 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); | |
2671 | ||
ad312c7c ZX |
2672 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
2673 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); | |
2674 | vcpu->arch.emulate_ctxt.mode = | |
2675 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) | |
bbd9b64e CO |
2676 | ? X86EMUL_MODE_REAL : cs_l |
2677 | ? X86EMUL_MODE_PROT64 : cs_db | |
2678 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; | |
2679 | ||
ad312c7c | 2680 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
571008da | 2681 | |
0cb5762e AP |
2682 | /* Only allow emulation of specific instructions on #UD |
2683 | * (namely VMMCALL, sysenter, sysexit, syscall)*/ | |
571008da | 2684 | c = &vcpu->arch.emulate_ctxt.decode; |
0cb5762e AP |
2685 | if (emulation_type & EMULTYPE_TRAP_UD) { |
2686 | if (!c->twobyte) | |
2687 | return EMULATE_FAIL; | |
2688 | switch (c->b) { | |
2689 | case 0x01: /* VMMCALL */ | |
2690 | if (c->modrm_mod != 3 || c->modrm_rm != 1) | |
2691 | return EMULATE_FAIL; | |
2692 | break; | |
2693 | case 0x34: /* sysenter */ | |
2694 | case 0x35: /* sysexit */ | |
2695 | if (c->modrm_mod != 0 || c->modrm_rm != 0) | |
2696 | return EMULATE_FAIL; | |
2697 | break; | |
2698 | case 0x05: /* syscall */ | |
2699 | if (c->modrm_mod != 0 || c->modrm_rm != 0) | |
2700 | return EMULATE_FAIL; | |
2701 | break; | |
2702 | default: | |
2703 | return EMULATE_FAIL; | |
2704 | } | |
2705 | ||
2706 | if (!(c->modrm_reg == 0 || c->modrm_reg == 3)) | |
2707 | return EMULATE_FAIL; | |
2708 | } | |
571008da | 2709 | |
f2b5756b | 2710 | ++vcpu->stat.insn_emulation; |
bbd9b64e | 2711 | if (r) { |
f2b5756b | 2712 | ++vcpu->stat.insn_emulation_fail; |
bbd9b64e CO |
2713 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
2714 | return EMULATE_DONE; | |
2715 | return EMULATE_FAIL; | |
2716 | } | |
2717 | } | |
2718 | ||
ba8afb6b GN |
2719 | if (emulation_type & EMULTYPE_SKIP) { |
2720 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip); | |
2721 | return EMULATE_DONE; | |
2722 | } | |
2723 | ||
ad312c7c | 2724 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
310b5d30 GC |
2725 | shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; |
2726 | ||
2727 | if (r == 0) | |
2728 | kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask); | |
bbd9b64e | 2729 | |
ad312c7c | 2730 | if (vcpu->arch.pio.string) |
bbd9b64e CO |
2731 | return EMULATE_DO_MMIO; |
2732 | ||
2733 | if ((r || vcpu->mmio_is_write) && run) { | |
2734 | run->exit_reason = KVM_EXIT_MMIO; | |
2735 | run->mmio.phys_addr = vcpu->mmio_phys_addr; | |
2736 | memcpy(run->mmio.data, vcpu->mmio_data, 8); | |
2737 | run->mmio.len = vcpu->mmio_size; | |
2738 | run->mmio.is_write = vcpu->mmio_is_write; | |
2739 | } | |
2740 | ||
2741 | if (r) { | |
2742 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | |
2743 | return EMULATE_DONE; | |
2744 | if (!vcpu->mmio_needed) { | |
2745 | kvm_report_emulation_failure(vcpu, "mmio"); | |
2746 | return EMULATE_FAIL; | |
2747 | } | |
2748 | return EMULATE_DO_MMIO; | |
2749 | } | |
2750 | ||
ad312c7c | 2751 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
bbd9b64e CO |
2752 | |
2753 | if (vcpu->mmio_is_write) { | |
2754 | vcpu->mmio_needed = 0; | |
2755 | return EMULATE_DO_MMIO; | |
2756 | } | |
2757 | ||
2758 | return EMULATE_DONE; | |
2759 | } | |
2760 | EXPORT_SYMBOL_GPL(emulate_instruction); | |
2761 | ||
de7d789a CO |
2762 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
2763 | { | |
ad312c7c | 2764 | void *p = vcpu->arch.pio_data; |
0f346074 | 2765 | gva_t q = vcpu->arch.pio.guest_gva; |
de7d789a | 2766 | unsigned bytes; |
0f346074 | 2767 | int ret; |
de7d789a | 2768 | |
ad312c7c ZX |
2769 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
2770 | if (vcpu->arch.pio.in) | |
0f346074 | 2771 | ret = kvm_write_guest_virt(q, p, bytes, vcpu); |
de7d789a | 2772 | else |
0f346074 IE |
2773 | ret = kvm_read_guest_virt(q, p, bytes, vcpu); |
2774 | return ret; | |
de7d789a CO |
2775 | } |
2776 | ||
2777 | int complete_pio(struct kvm_vcpu *vcpu) | |
2778 | { | |
ad312c7c | 2779 | struct kvm_pio_request *io = &vcpu->arch.pio; |
de7d789a CO |
2780 | long delta; |
2781 | int r; | |
5fdbf976 | 2782 | unsigned long val; |
de7d789a CO |
2783 | |
2784 | if (!io->string) { | |
5fdbf976 MT |
2785 | if (io->in) { |
2786 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); | |
2787 | memcpy(&val, vcpu->arch.pio_data, io->size); | |
2788 | kvm_register_write(vcpu, VCPU_REGS_RAX, val); | |
2789 | } | |
de7d789a CO |
2790 | } else { |
2791 | if (io->in) { | |
2792 | r = pio_copy_data(vcpu); | |
5fdbf976 | 2793 | if (r) |
de7d789a | 2794 | return r; |
de7d789a CO |
2795 | } |
2796 | ||
2797 | delta = 1; | |
2798 | if (io->rep) { | |
2799 | delta *= io->cur_count; | |
2800 | /* | |
2801 | * The size of the register should really depend on | |
2802 | * current address size. | |
2803 | */ | |
5fdbf976 MT |
2804 | val = kvm_register_read(vcpu, VCPU_REGS_RCX); |
2805 | val -= delta; | |
2806 | kvm_register_write(vcpu, VCPU_REGS_RCX, val); | |
de7d789a CO |
2807 | } |
2808 | if (io->down) | |
2809 | delta = -delta; | |
2810 | delta *= io->size; | |
5fdbf976 MT |
2811 | if (io->in) { |
2812 | val = kvm_register_read(vcpu, VCPU_REGS_RDI); | |
2813 | val += delta; | |
2814 | kvm_register_write(vcpu, VCPU_REGS_RDI, val); | |
2815 | } else { | |
2816 | val = kvm_register_read(vcpu, VCPU_REGS_RSI); | |
2817 | val += delta; | |
2818 | kvm_register_write(vcpu, VCPU_REGS_RSI, val); | |
2819 | } | |
de7d789a CO |
2820 | } |
2821 | ||
de7d789a CO |
2822 | io->count -= io->cur_count; |
2823 | io->cur_count = 0; | |
2824 | ||
2825 | return 0; | |
2826 | } | |
2827 | ||
2828 | static void kernel_pio(struct kvm_io_device *pio_dev, | |
2829 | struct kvm_vcpu *vcpu, | |
2830 | void *pd) | |
2831 | { | |
2832 | /* TODO: String I/O for in kernel device */ | |
2833 | ||
ad312c7c ZX |
2834 | if (vcpu->arch.pio.in) |
2835 | kvm_iodevice_read(pio_dev, vcpu->arch.pio.port, | |
2836 | vcpu->arch.pio.size, | |
de7d789a CO |
2837 | pd); |
2838 | else | |
ad312c7c ZX |
2839 | kvm_iodevice_write(pio_dev, vcpu->arch.pio.port, |
2840 | vcpu->arch.pio.size, | |
de7d789a | 2841 | pd); |
de7d789a CO |
2842 | } |
2843 | ||
2844 | static void pio_string_write(struct kvm_io_device *pio_dev, | |
2845 | struct kvm_vcpu *vcpu) | |
2846 | { | |
ad312c7c ZX |
2847 | struct kvm_pio_request *io = &vcpu->arch.pio; |
2848 | void *pd = vcpu->arch.pio_data; | |
de7d789a CO |
2849 | int i; |
2850 | ||
de7d789a CO |
2851 | for (i = 0; i < io->cur_count; i++) { |
2852 | kvm_iodevice_write(pio_dev, io->port, | |
2853 | io->size, | |
2854 | pd); | |
2855 | pd += io->size; | |
2856 | } | |
de7d789a CO |
2857 | } |
2858 | ||
2859 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, | |
92760499 LV |
2860 | gpa_t addr, int len, |
2861 | int is_write) | |
de7d789a | 2862 | { |
92760499 | 2863 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write); |
de7d789a CO |
2864 | } |
2865 | ||
2866 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
2867 | int size, unsigned port) | |
2868 | { | |
2869 | struct kvm_io_device *pio_dev; | |
5fdbf976 | 2870 | unsigned long val; |
de7d789a CO |
2871 | |
2872 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
2873 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
ad312c7c | 2874 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
de7d789a | 2875 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
ad312c7c ZX |
2876 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
2877 | vcpu->run->io.port = vcpu->arch.pio.port = port; | |
2878 | vcpu->arch.pio.in = in; | |
2879 | vcpu->arch.pio.string = 0; | |
2880 | vcpu->arch.pio.down = 0; | |
ad312c7c | 2881 | vcpu->arch.pio.rep = 0; |
de7d789a | 2882 | |
229456fc MT |
2883 | trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port, |
2884 | size, 1); | |
2714d1d3 | 2885 | |
5fdbf976 MT |
2886 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
2887 | memcpy(vcpu->arch.pio_data, &val, 4); | |
de7d789a | 2888 | |
fa40a821 | 2889 | mutex_lock(&vcpu->kvm->lock); |
92760499 | 2890 | pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in); |
fa40a821 | 2891 | mutex_unlock(&vcpu->kvm->lock); |
de7d789a | 2892 | if (pio_dev) { |
ad312c7c | 2893 | kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data); |
de7d789a CO |
2894 | complete_pio(vcpu); |
2895 | return 1; | |
2896 | } | |
2897 | return 0; | |
2898 | } | |
2899 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); | |
2900 | ||
2901 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, | |
2902 | int size, unsigned long count, int down, | |
2903 | gva_t address, int rep, unsigned port) | |
2904 | { | |
2905 | unsigned now, in_page; | |
0f346074 | 2906 | int ret = 0; |
de7d789a CO |
2907 | struct kvm_io_device *pio_dev; |
2908 | ||
2909 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
2910 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; | |
ad312c7c | 2911 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
de7d789a | 2912 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
ad312c7c ZX |
2913 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
2914 | vcpu->run->io.port = vcpu->arch.pio.port = port; | |
2915 | vcpu->arch.pio.in = in; | |
2916 | vcpu->arch.pio.string = 1; | |
2917 | vcpu->arch.pio.down = down; | |
ad312c7c | 2918 | vcpu->arch.pio.rep = rep; |
de7d789a | 2919 | |
229456fc MT |
2920 | trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port, |
2921 | size, count); | |
2714d1d3 | 2922 | |
de7d789a CO |
2923 | if (!count) { |
2924 | kvm_x86_ops->skip_emulated_instruction(vcpu); | |
2925 | return 1; | |
2926 | } | |
2927 | ||
2928 | if (!down) | |
2929 | in_page = PAGE_SIZE - offset_in_page(address); | |
2930 | else | |
2931 | in_page = offset_in_page(address) + size; | |
2932 | now = min(count, (unsigned long)in_page / size); | |
0f346074 | 2933 | if (!now) |
de7d789a | 2934 | now = 1; |
de7d789a CO |
2935 | if (down) { |
2936 | /* | |
2937 | * String I/O in reverse. Yuck. Kill the guest, fix later. | |
2938 | */ | |
2939 | pr_unimpl(vcpu, "guest string pio down\n"); | |
c1a5d4f9 | 2940 | kvm_inject_gp(vcpu, 0); |
de7d789a CO |
2941 | return 1; |
2942 | } | |
2943 | vcpu->run->io.count = now; | |
ad312c7c | 2944 | vcpu->arch.pio.cur_count = now; |
de7d789a | 2945 | |
ad312c7c | 2946 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
de7d789a CO |
2947 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
2948 | ||
0f346074 | 2949 | vcpu->arch.pio.guest_gva = address; |
de7d789a | 2950 | |
fa40a821 | 2951 | mutex_lock(&vcpu->kvm->lock); |
92760499 LV |
2952 | pio_dev = vcpu_find_pio_dev(vcpu, port, |
2953 | vcpu->arch.pio.cur_count, | |
2954 | !vcpu->arch.pio.in); | |
fa40a821 MT |
2955 | mutex_unlock(&vcpu->kvm->lock); |
2956 | ||
ad312c7c | 2957 | if (!vcpu->arch.pio.in) { |
de7d789a CO |
2958 | /* string PIO write */ |
2959 | ret = pio_copy_data(vcpu); | |
0f346074 IE |
2960 | if (ret == X86EMUL_PROPAGATE_FAULT) { |
2961 | kvm_inject_gp(vcpu, 0); | |
2962 | return 1; | |
2963 | } | |
2964 | if (ret == 0 && pio_dev) { | |
de7d789a CO |
2965 | pio_string_write(pio_dev, vcpu); |
2966 | complete_pio(vcpu); | |
ad312c7c | 2967 | if (vcpu->arch.pio.count == 0) |
de7d789a CO |
2968 | ret = 1; |
2969 | } | |
2970 | } else if (pio_dev) | |
2971 | pr_unimpl(vcpu, "no string pio read support yet, " | |
2972 | "port %x size %d count %ld\n", | |
2973 | port, size, count); | |
2974 | ||
2975 | return ret; | |
2976 | } | |
2977 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); | |
2978 | ||
c8076604 GH |
2979 | static void bounce_off(void *info) |
2980 | { | |
2981 | /* nothing */ | |
2982 | } | |
2983 | ||
2984 | static unsigned int ref_freq; | |
2985 | static unsigned long tsc_khz_ref; | |
2986 | ||
2987 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
2988 | void *data) | |
2989 | { | |
2990 | struct cpufreq_freqs *freq = data; | |
2991 | struct kvm *kvm; | |
2992 | struct kvm_vcpu *vcpu; | |
2993 | int i, send_ipi = 0; | |
2994 | ||
2995 | if (!ref_freq) | |
2996 | ref_freq = freq->old; | |
2997 | ||
2998 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | |
2999 | return 0; | |
3000 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | |
3001 | return 0; | |
3002 | per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new); | |
3003 | ||
3004 | spin_lock(&kvm_lock); | |
3005 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
988a2cae | 3006 | kvm_for_each_vcpu(i, vcpu, kvm) { |
c8076604 GH |
3007 | if (vcpu->cpu != freq->cpu) |
3008 | continue; | |
3009 | if (!kvm_request_guest_time_update(vcpu)) | |
3010 | continue; | |
3011 | if (vcpu->cpu != smp_processor_id()) | |
3012 | send_ipi++; | |
3013 | } | |
3014 | } | |
3015 | spin_unlock(&kvm_lock); | |
3016 | ||
3017 | if (freq->old < freq->new && send_ipi) { | |
3018 | /* | |
3019 | * We upscale the frequency. Must make the guest | |
3020 | * doesn't see old kvmclock values while running with | |
3021 | * the new frequency, otherwise we risk the guest sees | |
3022 | * time go backwards. | |
3023 | * | |
3024 | * In case we update the frequency for another cpu | |
3025 | * (which might be in guest context) send an interrupt | |
3026 | * to kick the cpu out of guest context. Next time | |
3027 | * guest context is entered kvmclock will be updated, | |
3028 | * so the guest will not see stale values. | |
3029 | */ | |
3030 | smp_call_function_single(freq->cpu, bounce_off, NULL, 1); | |
3031 | } | |
3032 | return 0; | |
3033 | } | |
3034 | ||
3035 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | |
3036 | .notifier_call = kvmclock_cpufreq_notifier | |
3037 | }; | |
3038 | ||
f8c16bba | 3039 | int kvm_arch_init(void *opaque) |
043405e1 | 3040 | { |
c8076604 | 3041 | int r, cpu; |
f8c16bba ZX |
3042 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
3043 | ||
f8c16bba ZX |
3044 | if (kvm_x86_ops) { |
3045 | printk(KERN_ERR "kvm: already loaded the other module\n"); | |
56c6d28a ZX |
3046 | r = -EEXIST; |
3047 | goto out; | |
f8c16bba ZX |
3048 | } |
3049 | ||
3050 | if (!ops->cpu_has_kvm_support()) { | |
3051 | printk(KERN_ERR "kvm: no hardware support\n"); | |
56c6d28a ZX |
3052 | r = -EOPNOTSUPP; |
3053 | goto out; | |
f8c16bba ZX |
3054 | } |
3055 | if (ops->disabled_by_bios()) { | |
3056 | printk(KERN_ERR "kvm: disabled by bios\n"); | |
56c6d28a ZX |
3057 | r = -EOPNOTSUPP; |
3058 | goto out; | |
f8c16bba ZX |
3059 | } |
3060 | ||
97db56ce AK |
3061 | r = kvm_mmu_module_init(); |
3062 | if (r) | |
3063 | goto out; | |
3064 | ||
3065 | kvm_init_msr_list(); | |
3066 | ||
f8c16bba | 3067 | kvm_x86_ops = ops; |
56c6d28a | 3068 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
7b52345e SY |
3069 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
3070 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, | |
4b12f0de | 3071 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
c8076604 GH |
3072 | |
3073 | for_each_possible_cpu(cpu) | |
3074 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | |
3075 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { | |
3076 | tsc_khz_ref = tsc_khz; | |
3077 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, | |
3078 | CPUFREQ_TRANSITION_NOTIFIER); | |
3079 | } | |
3080 | ||
f8c16bba | 3081 | return 0; |
56c6d28a ZX |
3082 | |
3083 | out: | |
56c6d28a | 3084 | return r; |
043405e1 | 3085 | } |
8776e519 | 3086 | |
f8c16bba ZX |
3087 | void kvm_arch_exit(void) |
3088 | { | |
888d256e JK |
3089 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
3090 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, | |
3091 | CPUFREQ_TRANSITION_NOTIFIER); | |
f8c16bba | 3092 | kvm_x86_ops = NULL; |
56c6d28a ZX |
3093 | kvm_mmu_module_exit(); |
3094 | } | |
f8c16bba | 3095 | |
8776e519 HB |
3096 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
3097 | { | |
3098 | ++vcpu->stat.halt_exits; | |
3099 | if (irqchip_in_kernel(vcpu->kvm)) { | |
a4535290 | 3100 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
8776e519 HB |
3101 | return 1; |
3102 | } else { | |
3103 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
3104 | return 0; | |
3105 | } | |
3106 | } | |
3107 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); | |
3108 | ||
2f333bcb MT |
3109 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
3110 | unsigned long a1) | |
3111 | { | |
3112 | if (is_long_mode(vcpu)) | |
3113 | return a0; | |
3114 | else | |
3115 | return a0 | ((gpa_t)a1 << 32); | |
3116 | } | |
3117 | ||
8776e519 HB |
3118 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
3119 | { | |
3120 | unsigned long nr, a0, a1, a2, a3, ret; | |
2f333bcb | 3121 | int r = 1; |
8776e519 | 3122 | |
5fdbf976 MT |
3123 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
3124 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); | |
3125 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
3126 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); | |
3127 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); | |
8776e519 | 3128 | |
229456fc | 3129 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
2714d1d3 | 3130 | |
8776e519 HB |
3131 | if (!is_long_mode(vcpu)) { |
3132 | nr &= 0xFFFFFFFF; | |
3133 | a0 &= 0xFFFFFFFF; | |
3134 | a1 &= 0xFFFFFFFF; | |
3135 | a2 &= 0xFFFFFFFF; | |
3136 | a3 &= 0xFFFFFFFF; | |
3137 | } | |
3138 | ||
3139 | switch (nr) { | |
b93463aa AK |
3140 | case KVM_HC_VAPIC_POLL_IRQ: |
3141 | ret = 0; | |
3142 | break; | |
2f333bcb MT |
3143 | case KVM_HC_MMU_OP: |
3144 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); | |
3145 | break; | |
8776e519 HB |
3146 | default: |
3147 | ret = -KVM_ENOSYS; | |
3148 | break; | |
3149 | } | |
5fdbf976 | 3150 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
f11c3a8d | 3151 | ++vcpu->stat.hypercalls; |
2f333bcb | 3152 | return r; |
8776e519 HB |
3153 | } |
3154 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
3155 | ||
3156 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) | |
3157 | { | |
3158 | char instruction[3]; | |
3159 | int ret = 0; | |
5fdbf976 | 3160 | unsigned long rip = kvm_rip_read(vcpu); |
8776e519 | 3161 | |
8776e519 HB |
3162 | |
3163 | /* | |
3164 | * Blow out the MMU to ensure that no other VCPU has an active mapping | |
3165 | * to ensure that the updated hypercall appears atomically across all | |
3166 | * VCPUs. | |
3167 | */ | |
3168 | kvm_mmu_zap_all(vcpu->kvm); | |
3169 | ||
8776e519 | 3170 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
5fdbf976 | 3171 | if (emulator_write_emulated(rip, instruction, 3, vcpu) |
8776e519 HB |
3172 | != X86EMUL_CONTINUE) |
3173 | ret = -EFAULT; | |
3174 | ||
8776e519 HB |
3175 | return ret; |
3176 | } | |
3177 | ||
3178 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | |
3179 | { | |
3180 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | |
3181 | } | |
3182 | ||
3183 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
3184 | { | |
3185 | struct descriptor_table dt = { limit, base }; | |
3186 | ||
3187 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
3188 | } | |
3189 | ||
3190 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) | |
3191 | { | |
3192 | struct descriptor_table dt = { limit, base }; | |
3193 | ||
3194 | kvm_x86_ops->set_idt(vcpu, &dt); | |
3195 | } | |
3196 | ||
3197 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, | |
3198 | unsigned long *rflags) | |
3199 | { | |
2d3ad1f4 | 3200 | kvm_lmsw(vcpu, msw); |
8776e519 HB |
3201 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
3202 | } | |
3203 | ||
3204 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) | |
3205 | { | |
54e445ca JR |
3206 | unsigned long value; |
3207 | ||
8776e519 HB |
3208 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
3209 | switch (cr) { | |
3210 | case 0: | |
54e445ca JR |
3211 | value = vcpu->arch.cr0; |
3212 | break; | |
8776e519 | 3213 | case 2: |
54e445ca JR |
3214 | value = vcpu->arch.cr2; |
3215 | break; | |
8776e519 | 3216 | case 3: |
54e445ca JR |
3217 | value = vcpu->arch.cr3; |
3218 | break; | |
8776e519 | 3219 | case 4: |
54e445ca JR |
3220 | value = vcpu->arch.cr4; |
3221 | break; | |
152ff9be | 3222 | case 8: |
54e445ca JR |
3223 | value = kvm_get_cr8(vcpu); |
3224 | break; | |
8776e519 | 3225 | default: |
b8688d51 | 3226 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
8776e519 HB |
3227 | return 0; |
3228 | } | |
54e445ca JR |
3229 | |
3230 | return value; | |
8776e519 HB |
3231 | } |
3232 | ||
3233 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, | |
3234 | unsigned long *rflags) | |
3235 | { | |
3236 | switch (cr) { | |
3237 | case 0: | |
2d3ad1f4 | 3238 | kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val)); |
8776e519 HB |
3239 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
3240 | break; | |
3241 | case 2: | |
ad312c7c | 3242 | vcpu->arch.cr2 = val; |
8776e519 HB |
3243 | break; |
3244 | case 3: | |
2d3ad1f4 | 3245 | kvm_set_cr3(vcpu, val); |
8776e519 HB |
3246 | break; |
3247 | case 4: | |
2d3ad1f4 | 3248 | kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val)); |
8776e519 | 3249 | break; |
152ff9be | 3250 | case 8: |
2d3ad1f4 | 3251 | kvm_set_cr8(vcpu, val & 0xfUL); |
152ff9be | 3252 | break; |
8776e519 | 3253 | default: |
b8688d51 | 3254 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
8776e519 HB |
3255 | } |
3256 | } | |
3257 | ||
07716717 DK |
3258 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
3259 | { | |
ad312c7c ZX |
3260 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
3261 | int j, nent = vcpu->arch.cpuid_nent; | |
07716717 DK |
3262 | |
3263 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; | |
3264 | /* when no next entry is found, the current entry[i] is reselected */ | |
0fdf8e59 | 3265 | for (j = i + 1; ; j = (j + 1) % nent) { |
ad312c7c | 3266 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
07716717 DK |
3267 | if (ej->function == e->function) { |
3268 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; | |
3269 | return j; | |
3270 | } | |
3271 | } | |
3272 | return 0; /* silence gcc, even though control never reaches here */ | |
3273 | } | |
3274 | ||
3275 | /* find an entry with matching function, matching index (if needed), and that | |
3276 | * should be read next (if it's stateful) */ | |
3277 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, | |
3278 | u32 function, u32 index) | |
3279 | { | |
3280 | if (e->function != function) | |
3281 | return 0; | |
3282 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) | |
3283 | return 0; | |
3284 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && | |
19355475 | 3285 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
07716717 DK |
3286 | return 0; |
3287 | return 1; | |
3288 | } | |
3289 | ||
d8017474 AG |
3290 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
3291 | u32 function, u32 index) | |
8776e519 HB |
3292 | { |
3293 | int i; | |
d8017474 | 3294 | struct kvm_cpuid_entry2 *best = NULL; |
8776e519 | 3295 | |
ad312c7c | 3296 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
d8017474 AG |
3297 | struct kvm_cpuid_entry2 *e; |
3298 | ||
ad312c7c | 3299 | e = &vcpu->arch.cpuid_entries[i]; |
07716717 DK |
3300 | if (is_matching_cpuid_entry(e, function, index)) { |
3301 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) | |
3302 | move_to_next_stateful_cpuid_entry(vcpu, i); | |
8776e519 HB |
3303 | best = e; |
3304 | break; | |
3305 | } | |
3306 | /* | |
3307 | * Both basic or both extended? | |
3308 | */ | |
3309 | if (((e->function ^ function) & 0x80000000) == 0) | |
3310 | if (!best || e->function > best->function) | |
3311 | best = e; | |
3312 | } | |
d8017474 AG |
3313 | return best; |
3314 | } | |
3315 | ||
82725b20 DE |
3316 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
3317 | { | |
3318 | struct kvm_cpuid_entry2 *best; | |
3319 | ||
3320 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); | |
3321 | if (best) | |
3322 | return best->eax & 0xff; | |
3323 | return 36; | |
3324 | } | |
3325 | ||
d8017474 AG |
3326 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
3327 | { | |
3328 | u32 function, index; | |
3329 | struct kvm_cpuid_entry2 *best; | |
3330 | ||
3331 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); | |
3332 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
3333 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); | |
3334 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); | |
3335 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); | |
3336 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); | |
3337 | best = kvm_find_cpuid_entry(vcpu, function, index); | |
8776e519 | 3338 | if (best) { |
5fdbf976 MT |
3339 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
3340 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); | |
3341 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); | |
3342 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); | |
8776e519 | 3343 | } |
8776e519 | 3344 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
229456fc MT |
3345 | trace_kvm_cpuid(function, |
3346 | kvm_register_read(vcpu, VCPU_REGS_RAX), | |
3347 | kvm_register_read(vcpu, VCPU_REGS_RBX), | |
3348 | kvm_register_read(vcpu, VCPU_REGS_RCX), | |
3349 | kvm_register_read(vcpu, VCPU_REGS_RDX)); | |
8776e519 HB |
3350 | } |
3351 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); | |
d0752060 | 3352 | |
b6c7a5dc HB |
3353 | /* |
3354 | * Check if userspace requested an interrupt window, and that the | |
3355 | * interrupt window is open. | |
3356 | * | |
3357 | * No need to exit to userspace if we already have an interrupt queued. | |
3358 | */ | |
3359 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, | |
3360 | struct kvm_run *kvm_run) | |
3361 | { | |
8061823a | 3362 | return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) && |
b6c7a5dc | 3363 | kvm_run->request_interrupt_window && |
5df56646 | 3364 | kvm_arch_interrupt_allowed(vcpu)); |
b6c7a5dc HB |
3365 | } |
3366 | ||
3367 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, | |
3368 | struct kvm_run *kvm_run) | |
3369 | { | |
3370 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; | |
2d3ad1f4 | 3371 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc | 3372 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
4531220b | 3373 | if (irqchip_in_kernel(vcpu->kvm)) |
b6c7a5dc | 3374 | kvm_run->ready_for_interrupt_injection = 1; |
4531220b | 3375 | else |
b6c7a5dc | 3376 | kvm_run->ready_for_interrupt_injection = |
fa9726b0 GN |
3377 | kvm_arch_interrupt_allowed(vcpu) && |
3378 | !kvm_cpu_has_interrupt(vcpu) && | |
3379 | !kvm_event_needs_reinjection(vcpu); | |
b6c7a5dc HB |
3380 | } |
3381 | ||
b93463aa AK |
3382 | static void vapic_enter(struct kvm_vcpu *vcpu) |
3383 | { | |
3384 | struct kvm_lapic *apic = vcpu->arch.apic; | |
3385 | struct page *page; | |
3386 | ||
3387 | if (!apic || !apic->vapic_addr) | |
3388 | return; | |
3389 | ||
3390 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); | |
72dc67a6 IE |
3391 | |
3392 | vcpu->arch.apic->vapic_page = page; | |
b93463aa AK |
3393 | } |
3394 | ||
3395 | static void vapic_exit(struct kvm_vcpu *vcpu) | |
3396 | { | |
3397 | struct kvm_lapic *apic = vcpu->arch.apic; | |
3398 | ||
3399 | if (!apic || !apic->vapic_addr) | |
3400 | return; | |
3401 | ||
f8b78fa3 | 3402 | down_read(&vcpu->kvm->slots_lock); |
b93463aa AK |
3403 | kvm_release_page_dirty(apic->vapic_page); |
3404 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); | |
f8b78fa3 | 3405 | up_read(&vcpu->kvm->slots_lock); |
b93463aa AK |
3406 | } |
3407 | ||
95ba8273 GN |
3408 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
3409 | { | |
3410 | int max_irr, tpr; | |
3411 | ||
3412 | if (!kvm_x86_ops->update_cr8_intercept) | |
3413 | return; | |
3414 | ||
8db3baa2 GN |
3415 | if (!vcpu->arch.apic->vapic_addr) |
3416 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
3417 | else | |
3418 | max_irr = -1; | |
95ba8273 GN |
3419 | |
3420 | if (max_irr != -1) | |
3421 | max_irr >>= 4; | |
3422 | ||
3423 | tpr = kvm_lapic_get_cr8(vcpu); | |
3424 | ||
3425 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); | |
3426 | } | |
3427 | ||
6a8b1d13 | 3428 | static void inject_pending_irq(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
95ba8273 GN |
3429 | { |
3430 | /* try to reinject previous events if any */ | |
3431 | if (vcpu->arch.nmi_injected) { | |
3432 | kvm_x86_ops->set_nmi(vcpu); | |
3433 | return; | |
3434 | } | |
3435 | ||
3436 | if (vcpu->arch.interrupt.pending) { | |
66fd3f7f | 3437 | kvm_x86_ops->set_irq(vcpu); |
95ba8273 GN |
3438 | return; |
3439 | } | |
3440 | ||
3441 | /* try to inject new event if pending */ | |
3442 | if (vcpu->arch.nmi_pending) { | |
3443 | if (kvm_x86_ops->nmi_allowed(vcpu)) { | |
3444 | vcpu->arch.nmi_pending = false; | |
3445 | vcpu->arch.nmi_injected = true; | |
3446 | kvm_x86_ops->set_nmi(vcpu); | |
3447 | } | |
3448 | } else if (kvm_cpu_has_interrupt(vcpu)) { | |
3449 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { | |
66fd3f7f GN |
3450 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
3451 | false); | |
3452 | kvm_x86_ops->set_irq(vcpu); | |
95ba8273 GN |
3453 | } |
3454 | } | |
3455 | } | |
3456 | ||
d7690175 | 3457 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
b6c7a5dc HB |
3458 | { |
3459 | int r; | |
6a8b1d13 GN |
3460 | bool req_int_win = !irqchip_in_kernel(vcpu->kvm) && |
3461 | kvm_run->request_interrupt_window; | |
b6c7a5dc | 3462 | |
2e53d63a MT |
3463 | if (vcpu->requests) |
3464 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) | |
3465 | kvm_mmu_unload(vcpu); | |
3466 | ||
b6c7a5dc HB |
3467 | r = kvm_mmu_reload(vcpu); |
3468 | if (unlikely(r)) | |
3469 | goto out; | |
3470 | ||
2f52d58c AK |
3471 | if (vcpu->requests) { |
3472 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) | |
2f599714 | 3473 | __kvm_migrate_timers(vcpu); |
c8076604 GH |
3474 | if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests)) |
3475 | kvm_write_guest_time(vcpu); | |
4731d4c7 MT |
3476 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
3477 | kvm_mmu_sync_roots(vcpu); | |
d4acf7e7 MT |
3478 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
3479 | kvm_x86_ops->tlb_flush(vcpu); | |
b93463aa AK |
3480 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
3481 | &vcpu->requests)) { | |
3482 | kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS; | |
3483 | r = 0; | |
3484 | goto out; | |
3485 | } | |
71c4dfaf JR |
3486 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
3487 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; | |
3488 | r = 0; | |
3489 | goto out; | |
3490 | } | |
2f52d58c | 3491 | } |
b93463aa | 3492 | |
b6c7a5dc HB |
3493 | preempt_disable(); |
3494 | ||
3495 | kvm_x86_ops->prepare_guest_switch(vcpu); | |
3496 | kvm_load_guest_fpu(vcpu); | |
3497 | ||
3498 | local_irq_disable(); | |
3499 | ||
32f88400 MT |
3500 | clear_bit(KVM_REQ_KICK, &vcpu->requests); |
3501 | smp_mb__after_clear_bit(); | |
3502 | ||
d7690175 | 3503 | if (vcpu->requests || need_resched() || signal_pending(current)) { |
6c142801 AK |
3504 | local_irq_enable(); |
3505 | preempt_enable(); | |
3506 | r = 1; | |
3507 | goto out; | |
3508 | } | |
3509 | ||
ad312c7c | 3510 | if (vcpu->arch.exception.pending) |
298101da | 3511 | __queue_exception(vcpu); |
eb9774f0 | 3512 | else |
95ba8273 | 3513 | inject_pending_irq(vcpu, kvm_run); |
b6c7a5dc | 3514 | |
6a8b1d13 GN |
3515 | /* enable NMI/IRQ window open exits if needed */ |
3516 | if (vcpu->arch.nmi_pending) | |
3517 | kvm_x86_ops->enable_nmi_window(vcpu); | |
3518 | else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) | |
3519 | kvm_x86_ops->enable_irq_window(vcpu); | |
3520 | ||
95ba8273 | 3521 | if (kvm_lapic_enabled(vcpu)) { |
8db3baa2 GN |
3522 | update_cr8_intercept(vcpu); |
3523 | kvm_lapic_sync_to_vapic(vcpu); | |
95ba8273 | 3524 | } |
b93463aa | 3525 | |
3200f405 MT |
3526 | up_read(&vcpu->kvm->slots_lock); |
3527 | ||
b6c7a5dc HB |
3528 | kvm_guest_enter(); |
3529 | ||
42dbaa5a JK |
3530 | get_debugreg(vcpu->arch.host_dr6, 6); |
3531 | get_debugreg(vcpu->arch.host_dr7, 7); | |
3532 | if (unlikely(vcpu->arch.switch_db_regs)) { | |
3533 | get_debugreg(vcpu->arch.host_db[0], 0); | |
3534 | get_debugreg(vcpu->arch.host_db[1], 1); | |
3535 | get_debugreg(vcpu->arch.host_db[2], 2); | |
3536 | get_debugreg(vcpu->arch.host_db[3], 3); | |
3537 | ||
3538 | set_debugreg(0, 7); | |
3539 | set_debugreg(vcpu->arch.eff_db[0], 0); | |
3540 | set_debugreg(vcpu->arch.eff_db[1], 1); | |
3541 | set_debugreg(vcpu->arch.eff_db[2], 2); | |
3542 | set_debugreg(vcpu->arch.eff_db[3], 3); | |
3543 | } | |
b6c7a5dc | 3544 | |
229456fc | 3545 | trace_kvm_entry(vcpu->vcpu_id); |
b6c7a5dc HB |
3546 | kvm_x86_ops->run(vcpu, kvm_run); |
3547 | ||
42dbaa5a JK |
3548 | if (unlikely(vcpu->arch.switch_db_regs)) { |
3549 | set_debugreg(0, 7); | |
3550 | set_debugreg(vcpu->arch.host_db[0], 0); | |
3551 | set_debugreg(vcpu->arch.host_db[1], 1); | |
3552 | set_debugreg(vcpu->arch.host_db[2], 2); | |
3553 | set_debugreg(vcpu->arch.host_db[3], 3); | |
3554 | } | |
3555 | set_debugreg(vcpu->arch.host_dr6, 6); | |
3556 | set_debugreg(vcpu->arch.host_dr7, 7); | |
3557 | ||
32f88400 | 3558 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
b6c7a5dc HB |
3559 | local_irq_enable(); |
3560 | ||
3561 | ++vcpu->stat.exits; | |
3562 | ||
3563 | /* | |
3564 | * We must have an instruction between local_irq_enable() and | |
3565 | * kvm_guest_exit(), so the timer interrupt isn't delayed by | |
3566 | * the interrupt shadow. The stat.exits increment will do nicely. | |
3567 | * But we need to prevent reordering, hence this barrier(): | |
3568 | */ | |
3569 | barrier(); | |
3570 | ||
3571 | kvm_guest_exit(); | |
3572 | ||
3573 | preempt_enable(); | |
3574 | ||
3200f405 MT |
3575 | down_read(&vcpu->kvm->slots_lock); |
3576 | ||
b6c7a5dc HB |
3577 | /* |
3578 | * Profile KVM exit RIPs: | |
3579 | */ | |
3580 | if (unlikely(prof_on == KVM_PROFILING)) { | |
5fdbf976 MT |
3581 | unsigned long rip = kvm_rip_read(vcpu); |
3582 | profile_hit(KVM_PROFILING, (void *)rip); | |
b6c7a5dc HB |
3583 | } |
3584 | ||
298101da | 3585 | |
b93463aa AK |
3586 | kvm_lapic_sync_from_vapic(vcpu); |
3587 | ||
b6c7a5dc | 3588 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
d7690175 MT |
3589 | out: |
3590 | return r; | |
3591 | } | |
b6c7a5dc | 3592 | |
09cec754 | 3593 | |
d7690175 MT |
3594 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
3595 | { | |
3596 | int r; | |
3597 | ||
3598 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { | |
1b10bf31 JK |
3599 | pr_debug("vcpu %d received sipi with vector # %x\n", |
3600 | vcpu->vcpu_id, vcpu->arch.sipi_vector); | |
d7690175 | 3601 | kvm_lapic_reset(vcpu); |
5f179287 | 3602 | r = kvm_arch_vcpu_reset(vcpu); |
d7690175 MT |
3603 | if (r) |
3604 | return r; | |
3605 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
b6c7a5dc HB |
3606 | } |
3607 | ||
d7690175 MT |
3608 | down_read(&vcpu->kvm->slots_lock); |
3609 | vapic_enter(vcpu); | |
3610 | ||
3611 | r = 1; | |
3612 | while (r > 0) { | |
af2152f5 | 3613 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
d7690175 MT |
3614 | r = vcpu_enter_guest(vcpu, kvm_run); |
3615 | else { | |
3616 | up_read(&vcpu->kvm->slots_lock); | |
3617 | kvm_vcpu_block(vcpu); | |
3618 | down_read(&vcpu->kvm->slots_lock); | |
3619 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests)) | |
09cec754 GN |
3620 | { |
3621 | switch(vcpu->arch.mp_state) { | |
3622 | case KVM_MP_STATE_HALTED: | |
d7690175 | 3623 | vcpu->arch.mp_state = |
09cec754 GN |
3624 | KVM_MP_STATE_RUNNABLE; |
3625 | case KVM_MP_STATE_RUNNABLE: | |
3626 | break; | |
3627 | case KVM_MP_STATE_SIPI_RECEIVED: | |
3628 | default: | |
3629 | r = -EINTR; | |
3630 | break; | |
3631 | } | |
3632 | } | |
d7690175 MT |
3633 | } |
3634 | ||
09cec754 GN |
3635 | if (r <= 0) |
3636 | break; | |
3637 | ||
3638 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); | |
3639 | if (kvm_cpu_has_pending_timer(vcpu)) | |
3640 | kvm_inject_pending_timer_irqs(vcpu); | |
3641 | ||
3642 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { | |
3643 | r = -EINTR; | |
3644 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
3645 | ++vcpu->stat.request_irq_exits; | |
3646 | } | |
3647 | if (signal_pending(current)) { | |
3648 | r = -EINTR; | |
3649 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
3650 | ++vcpu->stat.signal_exits; | |
3651 | } | |
3652 | if (need_resched()) { | |
3653 | up_read(&vcpu->kvm->slots_lock); | |
3654 | kvm_resched(vcpu); | |
3655 | down_read(&vcpu->kvm->slots_lock); | |
d7690175 | 3656 | } |
b6c7a5dc HB |
3657 | } |
3658 | ||
d7690175 | 3659 | up_read(&vcpu->kvm->slots_lock); |
b6c7a5dc HB |
3660 | post_kvm_run_save(vcpu, kvm_run); |
3661 | ||
b93463aa AK |
3662 | vapic_exit(vcpu); |
3663 | ||
b6c7a5dc HB |
3664 | return r; |
3665 | } | |
3666 | ||
3667 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
3668 | { | |
3669 | int r; | |
3670 | sigset_t sigsaved; | |
3671 | ||
3672 | vcpu_load(vcpu); | |
3673 | ||
ac9f6dc0 AK |
3674 | if (vcpu->sigset_active) |
3675 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
3676 | ||
a4535290 | 3677 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
b6c7a5dc | 3678 | kvm_vcpu_block(vcpu); |
d7690175 | 3679 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
ac9f6dc0 AK |
3680 | r = -EAGAIN; |
3681 | goto out; | |
b6c7a5dc HB |
3682 | } |
3683 | ||
b6c7a5dc HB |
3684 | /* re-sync apic's tpr */ |
3685 | if (!irqchip_in_kernel(vcpu->kvm)) | |
2d3ad1f4 | 3686 | kvm_set_cr8(vcpu, kvm_run->cr8); |
b6c7a5dc | 3687 | |
ad312c7c | 3688 | if (vcpu->arch.pio.cur_count) { |
b6c7a5dc HB |
3689 | r = complete_pio(vcpu); |
3690 | if (r) | |
3691 | goto out; | |
3692 | } | |
3693 | #if CONFIG_HAS_IOMEM | |
3694 | if (vcpu->mmio_needed) { | |
3695 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); | |
3696 | vcpu->mmio_read_completed = 1; | |
3697 | vcpu->mmio_needed = 0; | |
3200f405 MT |
3698 | |
3699 | down_read(&vcpu->kvm->slots_lock); | |
b6c7a5dc | 3700 | r = emulate_instruction(vcpu, kvm_run, |
571008da SY |
3701 | vcpu->arch.mmio_fault_cr2, 0, |
3702 | EMULTYPE_NO_DECODE); | |
3200f405 | 3703 | up_read(&vcpu->kvm->slots_lock); |
b6c7a5dc HB |
3704 | if (r == EMULATE_DO_MMIO) { |
3705 | /* | |
3706 | * Read-modify-write. Back to userspace. | |
3707 | */ | |
3708 | r = 0; | |
3709 | goto out; | |
3710 | } | |
3711 | } | |
3712 | #endif | |
5fdbf976 MT |
3713 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
3714 | kvm_register_write(vcpu, VCPU_REGS_RAX, | |
3715 | kvm_run->hypercall.ret); | |
b6c7a5dc HB |
3716 | |
3717 | r = __vcpu_run(vcpu, kvm_run); | |
3718 | ||
3719 | out: | |
3720 | if (vcpu->sigset_active) | |
3721 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
3722 | ||
3723 | vcpu_put(vcpu); | |
3724 | return r; | |
3725 | } | |
3726 | ||
3727 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
3728 | { | |
3729 | vcpu_load(vcpu); | |
3730 | ||
5fdbf976 MT |
3731 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
3732 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); | |
3733 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
3734 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); | |
3735 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); | |
3736 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); | |
3737 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); | |
3738 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); | |
b6c7a5dc | 3739 | #ifdef CONFIG_X86_64 |
5fdbf976 MT |
3740 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
3741 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); | |
3742 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); | |
3743 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); | |
3744 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); | |
3745 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); | |
3746 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); | |
3747 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); | |
b6c7a5dc HB |
3748 | #endif |
3749 | ||
5fdbf976 | 3750 | regs->rip = kvm_rip_read(vcpu); |
b6c7a5dc HB |
3751 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
3752 | ||
3753 | /* | |
3754 | * Don't leak debug flags in case they were set for guest debugging | |
3755 | */ | |
d0bfb940 | 3756 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
b6c7a5dc HB |
3757 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
3758 | ||
3759 | vcpu_put(vcpu); | |
3760 | ||
3761 | return 0; | |
3762 | } | |
3763 | ||
3764 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
3765 | { | |
3766 | vcpu_load(vcpu); | |
3767 | ||
5fdbf976 MT |
3768 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
3769 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); | |
3770 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); | |
3771 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); | |
3772 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); | |
3773 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); | |
3774 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); | |
3775 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); | |
b6c7a5dc | 3776 | #ifdef CONFIG_X86_64 |
5fdbf976 MT |
3777 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
3778 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); | |
3779 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); | |
3780 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); | |
3781 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); | |
3782 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); | |
3783 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); | |
3784 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); | |
3785 | ||
b6c7a5dc HB |
3786 | #endif |
3787 | ||
5fdbf976 | 3788 | kvm_rip_write(vcpu, regs->rip); |
b6c7a5dc HB |
3789 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
3790 | ||
b6c7a5dc | 3791 | |
b4f14abd JK |
3792 | vcpu->arch.exception.pending = false; |
3793 | ||
b6c7a5dc HB |
3794 | vcpu_put(vcpu); |
3795 | ||
3796 | return 0; | |
3797 | } | |
3798 | ||
3e6e0aab GT |
3799 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
3800 | struct kvm_segment *var, int seg) | |
b6c7a5dc | 3801 | { |
14af3f3c | 3802 | kvm_x86_ops->get_segment(vcpu, var, seg); |
b6c7a5dc HB |
3803 | } |
3804 | ||
3805 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) | |
3806 | { | |
3807 | struct kvm_segment cs; | |
3808 | ||
3e6e0aab | 3809 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
b6c7a5dc HB |
3810 | *db = cs.db; |
3811 | *l = cs.l; | |
3812 | } | |
3813 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
3814 | ||
3815 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
3816 | struct kvm_sregs *sregs) | |
3817 | { | |
3818 | struct descriptor_table dt; | |
b6c7a5dc HB |
3819 | |
3820 | vcpu_load(vcpu); | |
3821 | ||
3e6e0aab GT |
3822 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
3823 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
3824 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
3825 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
3826 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
3827 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 3828 | |
3e6e0aab GT |
3829 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
3830 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc HB |
3831 | |
3832 | kvm_x86_ops->get_idt(vcpu, &dt); | |
3833 | sregs->idt.limit = dt.limit; | |
3834 | sregs->idt.base = dt.base; | |
3835 | kvm_x86_ops->get_gdt(vcpu, &dt); | |
3836 | sregs->gdt.limit = dt.limit; | |
3837 | sregs->gdt.base = dt.base; | |
3838 | ||
3839 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
ad312c7c ZX |
3840 | sregs->cr0 = vcpu->arch.cr0; |
3841 | sregs->cr2 = vcpu->arch.cr2; | |
3842 | sregs->cr3 = vcpu->arch.cr3; | |
3843 | sregs->cr4 = vcpu->arch.cr4; | |
2d3ad1f4 | 3844 | sregs->cr8 = kvm_get_cr8(vcpu); |
ad312c7c | 3845 | sregs->efer = vcpu->arch.shadow_efer; |
b6c7a5dc HB |
3846 | sregs->apic_base = kvm_get_apic_base(vcpu); |
3847 | ||
923c61bb | 3848 | memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap); |
b6c7a5dc | 3849 | |
36752c9b | 3850 | if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft) |
14d0bc1f GN |
3851 | set_bit(vcpu->arch.interrupt.nr, |
3852 | (unsigned long *)sregs->interrupt_bitmap); | |
16d7a191 | 3853 | |
b6c7a5dc HB |
3854 | vcpu_put(vcpu); |
3855 | ||
3856 | return 0; | |
3857 | } | |
3858 | ||
62d9f0db MT |
3859 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
3860 | struct kvm_mp_state *mp_state) | |
3861 | { | |
3862 | vcpu_load(vcpu); | |
3863 | mp_state->mp_state = vcpu->arch.mp_state; | |
3864 | vcpu_put(vcpu); | |
3865 | return 0; | |
3866 | } | |
3867 | ||
3868 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
3869 | struct kvm_mp_state *mp_state) | |
3870 | { | |
3871 | vcpu_load(vcpu); | |
3872 | vcpu->arch.mp_state = mp_state->mp_state; | |
3873 | vcpu_put(vcpu); | |
3874 | return 0; | |
3875 | } | |
3876 | ||
3e6e0aab | 3877 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
b6c7a5dc HB |
3878 | struct kvm_segment *var, int seg) |
3879 | { | |
14af3f3c | 3880 | kvm_x86_ops->set_segment(vcpu, var, seg); |
b6c7a5dc HB |
3881 | } |
3882 | ||
37817f29 IE |
3883 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
3884 | struct kvm_segment *kvm_desct) | |
3885 | { | |
3886 | kvm_desct->base = seg_desc->base0; | |
3887 | kvm_desct->base |= seg_desc->base1 << 16; | |
3888 | kvm_desct->base |= seg_desc->base2 << 24; | |
3889 | kvm_desct->limit = seg_desc->limit0; | |
3890 | kvm_desct->limit |= seg_desc->limit << 16; | |
c93cd3a5 MT |
3891 | if (seg_desc->g) { |
3892 | kvm_desct->limit <<= 12; | |
3893 | kvm_desct->limit |= 0xfff; | |
3894 | } | |
37817f29 IE |
3895 | kvm_desct->selector = selector; |
3896 | kvm_desct->type = seg_desc->type; | |
3897 | kvm_desct->present = seg_desc->p; | |
3898 | kvm_desct->dpl = seg_desc->dpl; | |
3899 | kvm_desct->db = seg_desc->d; | |
3900 | kvm_desct->s = seg_desc->s; | |
3901 | kvm_desct->l = seg_desc->l; | |
3902 | kvm_desct->g = seg_desc->g; | |
3903 | kvm_desct->avl = seg_desc->avl; | |
3904 | if (!selector) | |
3905 | kvm_desct->unusable = 1; | |
3906 | else | |
3907 | kvm_desct->unusable = 0; | |
3908 | kvm_desct->padding = 0; | |
3909 | } | |
3910 | ||
b8222ad2 AS |
3911 | static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu, |
3912 | u16 selector, | |
3913 | struct descriptor_table *dtable) | |
37817f29 IE |
3914 | { |
3915 | if (selector & 1 << 2) { | |
3916 | struct kvm_segment kvm_seg; | |
3917 | ||
3e6e0aab | 3918 | kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); |
37817f29 IE |
3919 | |
3920 | if (kvm_seg.unusable) | |
3921 | dtable->limit = 0; | |
3922 | else | |
3923 | dtable->limit = kvm_seg.limit; | |
3924 | dtable->base = kvm_seg.base; | |
3925 | } | |
3926 | else | |
3927 | kvm_x86_ops->get_gdt(vcpu, dtable); | |
3928 | } | |
3929 | ||
3930 | /* allowed just for 8 bytes segments */ | |
3931 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, | |
3932 | struct desc_struct *seg_desc) | |
3933 | { | |
98899aa0 | 3934 | gpa_t gpa; |
37817f29 IE |
3935 | struct descriptor_table dtable; |
3936 | u16 index = selector >> 3; | |
3937 | ||
b8222ad2 | 3938 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
37817f29 IE |
3939 | |
3940 | if (dtable.limit < index * 8 + 7) { | |
3941 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); | |
3942 | return 1; | |
3943 | } | |
98899aa0 MT |
3944 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
3945 | gpa += index * 8; | |
3946 | return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8); | |
37817f29 IE |
3947 | } |
3948 | ||
3949 | /* allowed just for 8 bytes segments */ | |
3950 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, | |
3951 | struct desc_struct *seg_desc) | |
3952 | { | |
98899aa0 | 3953 | gpa_t gpa; |
37817f29 IE |
3954 | struct descriptor_table dtable; |
3955 | u16 index = selector >> 3; | |
3956 | ||
b8222ad2 | 3957 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
37817f29 IE |
3958 | |
3959 | if (dtable.limit < index * 8 + 7) | |
3960 | return 1; | |
98899aa0 MT |
3961 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
3962 | gpa += index * 8; | |
3963 | return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8); | |
37817f29 IE |
3964 | } |
3965 | ||
3966 | static u32 get_tss_base_addr(struct kvm_vcpu *vcpu, | |
3967 | struct desc_struct *seg_desc) | |
3968 | { | |
3969 | u32 base_addr; | |
3970 | ||
3971 | base_addr = seg_desc->base0; | |
3972 | base_addr |= (seg_desc->base1 << 16); | |
3973 | base_addr |= (seg_desc->base2 << 24); | |
3974 | ||
98899aa0 | 3975 | return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr); |
37817f29 IE |
3976 | } |
3977 | ||
37817f29 IE |
3978 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) |
3979 | { | |
3980 | struct kvm_segment kvm_seg; | |
3981 | ||
3e6e0aab | 3982 | kvm_get_segment(vcpu, &kvm_seg, seg); |
37817f29 IE |
3983 | return kvm_seg.selector; |
3984 | } | |
3985 | ||
3986 | static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu, | |
3987 | u16 selector, | |
3988 | struct kvm_segment *kvm_seg) | |
3989 | { | |
3990 | struct desc_struct seg_desc; | |
3991 | ||
3992 | if (load_guest_segment_descriptor(vcpu, selector, &seg_desc)) | |
3993 | return 1; | |
3994 | seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg); | |
3995 | return 0; | |
3996 | } | |
3997 | ||
2259e3a7 | 3998 | static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg) |
f4bbd9aa AK |
3999 | { |
4000 | struct kvm_segment segvar = { | |
4001 | .base = selector << 4, | |
4002 | .limit = 0xffff, | |
4003 | .selector = selector, | |
4004 | .type = 3, | |
4005 | .present = 1, | |
4006 | .dpl = 3, | |
4007 | .db = 0, | |
4008 | .s = 1, | |
4009 | .l = 0, | |
4010 | .g = 0, | |
4011 | .avl = 0, | |
4012 | .unusable = 0, | |
4013 | }; | |
4014 | kvm_x86_ops->set_segment(vcpu, &segvar, seg); | |
4015 | return 0; | |
4016 | } | |
4017 | ||
3e6e0aab GT |
4018 | int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
4019 | int type_bits, int seg) | |
37817f29 IE |
4020 | { |
4021 | struct kvm_segment kvm_seg; | |
4022 | ||
f4bbd9aa AK |
4023 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) |
4024 | return kvm_load_realmode_segment(vcpu, selector, seg); | |
37817f29 IE |
4025 | if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg)) |
4026 | return 1; | |
4027 | kvm_seg.type |= type_bits; | |
4028 | ||
4029 | if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS && | |
4030 | seg != VCPU_SREG_LDTR) | |
4031 | if (!kvm_seg.s) | |
4032 | kvm_seg.unusable = 1; | |
4033 | ||
3e6e0aab | 4034 | kvm_set_segment(vcpu, &kvm_seg, seg); |
37817f29 IE |
4035 | return 0; |
4036 | } | |
4037 | ||
4038 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, | |
4039 | struct tss_segment_32 *tss) | |
4040 | { | |
4041 | tss->cr3 = vcpu->arch.cr3; | |
5fdbf976 | 4042 | tss->eip = kvm_rip_read(vcpu); |
37817f29 | 4043 | tss->eflags = kvm_x86_ops->get_rflags(vcpu); |
5fdbf976 MT |
4044 | tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
4045 | tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
4046 | tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX); | |
4047 | tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX); | |
4048 | tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP); | |
4049 | tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP); | |
4050 | tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI); | |
4051 | tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI); | |
37817f29 IE |
4052 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
4053 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); | |
4054 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); | |
4055 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); | |
4056 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); | |
4057 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); | |
4058 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); | |
37817f29 IE |
4059 | } |
4060 | ||
4061 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, | |
4062 | struct tss_segment_32 *tss) | |
4063 | { | |
4064 | kvm_set_cr3(vcpu, tss->cr3); | |
4065 | ||
5fdbf976 | 4066 | kvm_rip_write(vcpu, tss->eip); |
37817f29 IE |
4067 | kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2); |
4068 | ||
5fdbf976 MT |
4069 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax); |
4070 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx); | |
4071 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx); | |
4072 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx); | |
4073 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp); | |
4074 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp); | |
4075 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi); | |
4076 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi); | |
37817f29 | 4077 | |
3e6e0aab | 4078 | if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR)) |
37817f29 IE |
4079 | return 1; |
4080 | ||
3e6e0aab | 4081 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
37817f29 IE |
4082 | return 1; |
4083 | ||
3e6e0aab | 4084 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
37817f29 IE |
4085 | return 1; |
4086 | ||
3e6e0aab | 4087 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
37817f29 IE |
4088 | return 1; |
4089 | ||
3e6e0aab | 4090 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
37817f29 IE |
4091 | return 1; |
4092 | ||
3e6e0aab | 4093 | if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS)) |
37817f29 IE |
4094 | return 1; |
4095 | ||
3e6e0aab | 4096 | if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS)) |
37817f29 IE |
4097 | return 1; |
4098 | return 0; | |
4099 | } | |
4100 | ||
4101 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, | |
4102 | struct tss_segment_16 *tss) | |
4103 | { | |
5fdbf976 | 4104 | tss->ip = kvm_rip_read(vcpu); |
37817f29 | 4105 | tss->flag = kvm_x86_ops->get_rflags(vcpu); |
5fdbf976 MT |
4106 | tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
4107 | tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
4108 | tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX); | |
4109 | tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX); | |
4110 | tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP); | |
4111 | tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP); | |
4112 | tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI); | |
4113 | tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI); | |
37817f29 IE |
4114 | |
4115 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); | |
4116 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); | |
4117 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); | |
4118 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); | |
4119 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); | |
4120 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); | |
4121 | } | |
4122 | ||
4123 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, | |
4124 | struct tss_segment_16 *tss) | |
4125 | { | |
5fdbf976 | 4126 | kvm_rip_write(vcpu, tss->ip); |
37817f29 | 4127 | kvm_x86_ops->set_rflags(vcpu, tss->flag | 2); |
5fdbf976 MT |
4128 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax); |
4129 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx); | |
4130 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx); | |
4131 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx); | |
4132 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp); | |
4133 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp); | |
4134 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si); | |
4135 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di); | |
37817f29 | 4136 | |
3e6e0aab | 4137 | if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR)) |
37817f29 IE |
4138 | return 1; |
4139 | ||
3e6e0aab | 4140 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
37817f29 IE |
4141 | return 1; |
4142 | ||
3e6e0aab | 4143 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
37817f29 IE |
4144 | return 1; |
4145 | ||
3e6e0aab | 4146 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
37817f29 IE |
4147 | return 1; |
4148 | ||
3e6e0aab | 4149 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
37817f29 IE |
4150 | return 1; |
4151 | return 0; | |
4152 | } | |
4153 | ||
8b2cf73c | 4154 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
b237ac37 GN |
4155 | u16 old_tss_sel, u32 old_tss_base, |
4156 | struct desc_struct *nseg_desc) | |
37817f29 IE |
4157 | { |
4158 | struct tss_segment_16 tss_segment_16; | |
4159 | int ret = 0; | |
4160 | ||
34198bf8 MT |
4161 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
4162 | sizeof tss_segment_16)) | |
37817f29 IE |
4163 | goto out; |
4164 | ||
4165 | save_state_to_tss16(vcpu, &tss_segment_16); | |
37817f29 | 4166 | |
34198bf8 MT |
4167 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
4168 | sizeof tss_segment_16)) | |
37817f29 | 4169 | goto out; |
34198bf8 MT |
4170 | |
4171 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), | |
4172 | &tss_segment_16, sizeof tss_segment_16)) | |
4173 | goto out; | |
4174 | ||
b237ac37 GN |
4175 | if (old_tss_sel != 0xffff) { |
4176 | tss_segment_16.prev_task_link = old_tss_sel; | |
4177 | ||
4178 | if (kvm_write_guest(vcpu->kvm, | |
4179 | get_tss_base_addr(vcpu, nseg_desc), | |
4180 | &tss_segment_16.prev_task_link, | |
4181 | sizeof tss_segment_16.prev_task_link)) | |
4182 | goto out; | |
4183 | } | |
4184 | ||
37817f29 IE |
4185 | if (load_state_from_tss16(vcpu, &tss_segment_16)) |
4186 | goto out; | |
4187 | ||
4188 | ret = 1; | |
4189 | out: | |
4190 | return ret; | |
4191 | } | |
4192 | ||
8b2cf73c | 4193 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
b237ac37 | 4194 | u16 old_tss_sel, u32 old_tss_base, |
37817f29 IE |
4195 | struct desc_struct *nseg_desc) |
4196 | { | |
4197 | struct tss_segment_32 tss_segment_32; | |
4198 | int ret = 0; | |
4199 | ||
34198bf8 MT |
4200 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
4201 | sizeof tss_segment_32)) | |
37817f29 IE |
4202 | goto out; |
4203 | ||
4204 | save_state_to_tss32(vcpu, &tss_segment_32); | |
37817f29 | 4205 | |
34198bf8 MT |
4206 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
4207 | sizeof tss_segment_32)) | |
4208 | goto out; | |
4209 | ||
4210 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), | |
4211 | &tss_segment_32, sizeof tss_segment_32)) | |
37817f29 | 4212 | goto out; |
34198bf8 | 4213 | |
b237ac37 GN |
4214 | if (old_tss_sel != 0xffff) { |
4215 | tss_segment_32.prev_task_link = old_tss_sel; | |
4216 | ||
4217 | if (kvm_write_guest(vcpu->kvm, | |
4218 | get_tss_base_addr(vcpu, nseg_desc), | |
4219 | &tss_segment_32.prev_task_link, | |
4220 | sizeof tss_segment_32.prev_task_link)) | |
4221 | goto out; | |
4222 | } | |
4223 | ||
37817f29 IE |
4224 | if (load_state_from_tss32(vcpu, &tss_segment_32)) |
4225 | goto out; | |
4226 | ||
4227 | ret = 1; | |
4228 | out: | |
4229 | return ret; | |
4230 | } | |
4231 | ||
4232 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) | |
4233 | { | |
4234 | struct kvm_segment tr_seg; | |
4235 | struct desc_struct cseg_desc; | |
4236 | struct desc_struct nseg_desc; | |
4237 | int ret = 0; | |
34198bf8 MT |
4238 | u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR); |
4239 | u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR); | |
37817f29 | 4240 | |
34198bf8 | 4241 | old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base); |
37817f29 | 4242 | |
34198bf8 MT |
4243 | /* FIXME: Handle errors. Failure to read either TSS or their |
4244 | * descriptors should generate a pagefault. | |
4245 | */ | |
37817f29 IE |
4246 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) |
4247 | goto out; | |
4248 | ||
34198bf8 | 4249 | if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc)) |
37817f29 IE |
4250 | goto out; |
4251 | ||
37817f29 IE |
4252 | if (reason != TASK_SWITCH_IRET) { |
4253 | int cpl; | |
4254 | ||
4255 | cpl = kvm_x86_ops->get_cpl(vcpu); | |
4256 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { | |
4257 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
4258 | return 1; | |
4259 | } | |
4260 | } | |
4261 | ||
4262 | if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) { | |
4263 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); | |
4264 | return 1; | |
4265 | } | |
4266 | ||
4267 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { | |
3fe913e7 | 4268 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
34198bf8 | 4269 | save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc); |
37817f29 IE |
4270 | } |
4271 | ||
4272 | if (reason == TASK_SWITCH_IRET) { | |
4273 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); | |
4274 | kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); | |
4275 | } | |
4276 | ||
64a7ec06 GN |
4277 | /* set back link to prev task only if NT bit is set in eflags |
4278 | note that old_tss_sel is not used afetr this point */ | |
4279 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) | |
4280 | old_tss_sel = 0xffff; | |
37817f29 | 4281 | |
b237ac37 GN |
4282 | /* set back link to prev task only if NT bit is set in eflags |
4283 | note that old_tss_sel is not used afetr this point */ | |
4284 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) | |
4285 | old_tss_sel = 0xffff; | |
4286 | ||
37817f29 | 4287 | if (nseg_desc.type & 8) |
b237ac37 GN |
4288 | ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel, |
4289 | old_tss_base, &nseg_desc); | |
37817f29 | 4290 | else |
b237ac37 GN |
4291 | ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel, |
4292 | old_tss_base, &nseg_desc); | |
37817f29 IE |
4293 | |
4294 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { | |
4295 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); | |
4296 | kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT); | |
4297 | } | |
4298 | ||
4299 | if (reason != TASK_SWITCH_IRET) { | |
3fe913e7 | 4300 | nseg_desc.type |= (1 << 1); |
37817f29 IE |
4301 | save_guest_segment_descriptor(vcpu, tss_selector, |
4302 | &nseg_desc); | |
4303 | } | |
4304 | ||
4305 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS); | |
4306 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); | |
4307 | tr_seg.type = 11; | |
3e6e0aab | 4308 | kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR); |
37817f29 | 4309 | out: |
37817f29 IE |
4310 | return ret; |
4311 | } | |
4312 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
4313 | ||
b6c7a5dc HB |
4314 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
4315 | struct kvm_sregs *sregs) | |
4316 | { | |
4317 | int mmu_reset_needed = 0; | |
923c61bb | 4318 | int pending_vec, max_bits; |
b6c7a5dc HB |
4319 | struct descriptor_table dt; |
4320 | ||
4321 | vcpu_load(vcpu); | |
4322 | ||
4323 | dt.limit = sregs->idt.limit; | |
4324 | dt.base = sregs->idt.base; | |
4325 | kvm_x86_ops->set_idt(vcpu, &dt); | |
4326 | dt.limit = sregs->gdt.limit; | |
4327 | dt.base = sregs->gdt.base; | |
4328 | kvm_x86_ops->set_gdt(vcpu, &dt); | |
4329 | ||
ad312c7c ZX |
4330 | vcpu->arch.cr2 = sregs->cr2; |
4331 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; | |
59839dff MT |
4332 | |
4333 | down_read(&vcpu->kvm->slots_lock); | |
4334 | if (gfn_to_memslot(vcpu->kvm, sregs->cr3 >> PAGE_SHIFT)) | |
4335 | vcpu->arch.cr3 = sregs->cr3; | |
4336 | else | |
4337 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); | |
4338 | up_read(&vcpu->kvm->slots_lock); | |
b6c7a5dc | 4339 | |
2d3ad1f4 | 4340 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 4341 | |
ad312c7c | 4342 | mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer; |
b6c7a5dc | 4343 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
b6c7a5dc HB |
4344 | kvm_set_apic_base(vcpu, sregs->apic_base); |
4345 | ||
4346 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); | |
4347 | ||
ad312c7c | 4348 | mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0; |
b6c7a5dc | 4349 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
d7306163 | 4350 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 4351 | |
ad312c7c | 4352 | mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4; |
b6c7a5dc HB |
4353 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
4354 | if (!is_long_mode(vcpu) && is_pae(vcpu)) | |
ad312c7c | 4355 | load_pdptrs(vcpu, vcpu->arch.cr3); |
b6c7a5dc HB |
4356 | |
4357 | if (mmu_reset_needed) | |
4358 | kvm_mmu_reset_context(vcpu); | |
4359 | ||
923c61bb GN |
4360 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
4361 | pending_vec = find_first_bit( | |
4362 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); | |
4363 | if (pending_vec < max_bits) { | |
66fd3f7f | 4364 | kvm_queue_interrupt(vcpu, pending_vec, false); |
923c61bb GN |
4365 | pr_debug("Set back pending irq %d\n", pending_vec); |
4366 | if (irqchip_in_kernel(vcpu->kvm)) | |
4367 | kvm_pic_clear_isr_ack(vcpu->kvm); | |
b6c7a5dc HB |
4368 | } |
4369 | ||
3e6e0aab GT |
4370 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
4371 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
4372 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
4373 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
4374 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
4375 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 4376 | |
3e6e0aab GT |
4377 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
4378 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 4379 | |
9c3e4aab | 4380 | /* Older userspace won't unhalt the vcpu on reset. */ |
c5af89b6 | 4381 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
9c3e4aab MT |
4382 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
4383 | !(vcpu->arch.cr0 & X86_CR0_PE)) | |
4384 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4385 | ||
b6c7a5dc HB |
4386 | vcpu_put(vcpu); |
4387 | ||
4388 | return 0; | |
4389 | } | |
4390 | ||
d0bfb940 JK |
4391 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
4392 | struct kvm_guest_debug *dbg) | |
b6c7a5dc | 4393 | { |
ae675ef0 | 4394 | int i, r; |
b6c7a5dc HB |
4395 | |
4396 | vcpu_load(vcpu); | |
4397 | ||
ae675ef0 JK |
4398 | if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) == |
4399 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) { | |
4400 | for (i = 0; i < KVM_NR_DB_REGS; ++i) | |
4401 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | |
4402 | vcpu->arch.switch_db_regs = | |
4403 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); | |
4404 | } else { | |
4405 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
4406 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
4407 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); | |
4408 | } | |
4409 | ||
b6c7a5dc HB |
4410 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
4411 | ||
d0bfb940 JK |
4412 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
4413 | kvm_queue_exception(vcpu, DB_VECTOR); | |
4414 | else if (dbg->control & KVM_GUESTDBG_INJECT_BP) | |
4415 | kvm_queue_exception(vcpu, BP_VECTOR); | |
4416 | ||
b6c7a5dc HB |
4417 | vcpu_put(vcpu); |
4418 | ||
4419 | return r; | |
4420 | } | |
4421 | ||
d0752060 HB |
4422 | /* |
4423 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when | |
4424 | * we have asm/x86/processor.h | |
4425 | */ | |
4426 | struct fxsave { | |
4427 | u16 cwd; | |
4428 | u16 swd; | |
4429 | u16 twd; | |
4430 | u16 fop; | |
4431 | u64 rip; | |
4432 | u64 rdp; | |
4433 | u32 mxcsr; | |
4434 | u32 mxcsr_mask; | |
4435 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ | |
4436 | #ifdef CONFIG_X86_64 | |
4437 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ | |
4438 | #else | |
4439 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ | |
4440 | #endif | |
4441 | }; | |
4442 | ||
8b006791 ZX |
4443 | /* |
4444 | * Translate a guest virtual address to a guest physical address. | |
4445 | */ | |
4446 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
4447 | struct kvm_translation *tr) | |
4448 | { | |
4449 | unsigned long vaddr = tr->linear_address; | |
4450 | gpa_t gpa; | |
4451 | ||
4452 | vcpu_load(vcpu); | |
72dc67a6 | 4453 | down_read(&vcpu->kvm->slots_lock); |
ad312c7c | 4454 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr); |
72dc67a6 | 4455 | up_read(&vcpu->kvm->slots_lock); |
8b006791 ZX |
4456 | tr->physical_address = gpa; |
4457 | tr->valid = gpa != UNMAPPED_GVA; | |
4458 | tr->writeable = 1; | |
4459 | tr->usermode = 0; | |
8b006791 ZX |
4460 | vcpu_put(vcpu); |
4461 | ||
4462 | return 0; | |
4463 | } | |
4464 | ||
d0752060 HB |
4465 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
4466 | { | |
ad312c7c | 4467 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
d0752060 HB |
4468 | |
4469 | vcpu_load(vcpu); | |
4470 | ||
4471 | memcpy(fpu->fpr, fxsave->st_space, 128); | |
4472 | fpu->fcw = fxsave->cwd; | |
4473 | fpu->fsw = fxsave->swd; | |
4474 | fpu->ftwx = fxsave->twd; | |
4475 | fpu->last_opcode = fxsave->fop; | |
4476 | fpu->last_ip = fxsave->rip; | |
4477 | fpu->last_dp = fxsave->rdp; | |
4478 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); | |
4479 | ||
4480 | vcpu_put(vcpu); | |
4481 | ||
4482 | return 0; | |
4483 | } | |
4484 | ||
4485 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
4486 | { | |
ad312c7c | 4487 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
d0752060 HB |
4488 | |
4489 | vcpu_load(vcpu); | |
4490 | ||
4491 | memcpy(fxsave->st_space, fpu->fpr, 128); | |
4492 | fxsave->cwd = fpu->fcw; | |
4493 | fxsave->swd = fpu->fsw; | |
4494 | fxsave->twd = fpu->ftwx; | |
4495 | fxsave->fop = fpu->last_opcode; | |
4496 | fxsave->rip = fpu->last_ip; | |
4497 | fxsave->rdp = fpu->last_dp; | |
4498 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); | |
4499 | ||
4500 | vcpu_put(vcpu); | |
4501 | ||
4502 | return 0; | |
4503 | } | |
4504 | ||
4505 | void fx_init(struct kvm_vcpu *vcpu) | |
4506 | { | |
4507 | unsigned after_mxcsr_mask; | |
4508 | ||
bc1a34f1 AA |
4509 | /* |
4510 | * Touch the fpu the first time in non atomic context as if | |
4511 | * this is the first fpu instruction the exception handler | |
4512 | * will fire before the instruction returns and it'll have to | |
4513 | * allocate ram with GFP_KERNEL. | |
4514 | */ | |
4515 | if (!used_math()) | |
d6e88aec | 4516 | kvm_fx_save(&vcpu->arch.host_fx_image); |
bc1a34f1 | 4517 | |
d0752060 HB |
4518 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
4519 | preempt_disable(); | |
d6e88aec AK |
4520 | kvm_fx_save(&vcpu->arch.host_fx_image); |
4521 | kvm_fx_finit(); | |
4522 | kvm_fx_save(&vcpu->arch.guest_fx_image); | |
4523 | kvm_fx_restore(&vcpu->arch.host_fx_image); | |
d0752060 HB |
4524 | preempt_enable(); |
4525 | ||
ad312c7c | 4526 | vcpu->arch.cr0 |= X86_CR0_ET; |
d0752060 | 4527 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
ad312c7c ZX |
4528 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
4529 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, | |
d0752060 HB |
4530 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
4531 | } | |
4532 | EXPORT_SYMBOL_GPL(fx_init); | |
4533 | ||
4534 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
4535 | { | |
4536 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) | |
4537 | return; | |
4538 | ||
4539 | vcpu->guest_fpu_loaded = 1; | |
d6e88aec AK |
4540 | kvm_fx_save(&vcpu->arch.host_fx_image); |
4541 | kvm_fx_restore(&vcpu->arch.guest_fx_image); | |
d0752060 HB |
4542 | } |
4543 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); | |
4544 | ||
4545 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
4546 | { | |
4547 | if (!vcpu->guest_fpu_loaded) | |
4548 | return; | |
4549 | ||
4550 | vcpu->guest_fpu_loaded = 0; | |
d6e88aec AK |
4551 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
4552 | kvm_fx_restore(&vcpu->arch.host_fx_image); | |
f096ed85 | 4553 | ++vcpu->stat.fpu_reload; |
d0752060 HB |
4554 | } |
4555 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); | |
e9b11c17 ZX |
4556 | |
4557 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
4558 | { | |
7f1ea208 JR |
4559 | if (vcpu->arch.time_page) { |
4560 | kvm_release_page_dirty(vcpu->arch.time_page); | |
4561 | vcpu->arch.time_page = NULL; | |
4562 | } | |
4563 | ||
e9b11c17 ZX |
4564 | kvm_x86_ops->vcpu_free(vcpu); |
4565 | } | |
4566 | ||
4567 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, | |
4568 | unsigned int id) | |
4569 | { | |
26e5215f AK |
4570 | return kvm_x86_ops->vcpu_create(kvm, id); |
4571 | } | |
e9b11c17 | 4572 | |
26e5215f AK |
4573 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
4574 | { | |
4575 | int r; | |
e9b11c17 ZX |
4576 | |
4577 | /* We do fxsave: this must be aligned. */ | |
ad312c7c | 4578 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
e9b11c17 | 4579 | |
0bed3b56 | 4580 | vcpu->arch.mtrr_state.have_fixed = 1; |
e9b11c17 ZX |
4581 | vcpu_load(vcpu); |
4582 | r = kvm_arch_vcpu_reset(vcpu); | |
4583 | if (r == 0) | |
4584 | r = kvm_mmu_setup(vcpu); | |
4585 | vcpu_put(vcpu); | |
4586 | if (r < 0) | |
4587 | goto free_vcpu; | |
4588 | ||
26e5215f | 4589 | return 0; |
e9b11c17 ZX |
4590 | free_vcpu: |
4591 | kvm_x86_ops->vcpu_free(vcpu); | |
26e5215f | 4592 | return r; |
e9b11c17 ZX |
4593 | } |
4594 | ||
d40ccc62 | 4595 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 ZX |
4596 | { |
4597 | vcpu_load(vcpu); | |
4598 | kvm_mmu_unload(vcpu); | |
4599 | vcpu_put(vcpu); | |
4600 | ||
4601 | kvm_x86_ops->vcpu_free(vcpu); | |
4602 | } | |
4603 | ||
4604 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) | |
4605 | { | |
448fa4a9 JK |
4606 | vcpu->arch.nmi_pending = false; |
4607 | vcpu->arch.nmi_injected = false; | |
4608 | ||
42dbaa5a JK |
4609 | vcpu->arch.switch_db_regs = 0; |
4610 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); | |
4611 | vcpu->arch.dr6 = DR6_FIXED_1; | |
4612 | vcpu->arch.dr7 = DR7_FIXED_1; | |
4613 | ||
e9b11c17 ZX |
4614 | return kvm_x86_ops->vcpu_reset(vcpu); |
4615 | } | |
4616 | ||
4617 | void kvm_arch_hardware_enable(void *garbage) | |
4618 | { | |
4619 | kvm_x86_ops->hardware_enable(garbage); | |
4620 | } | |
4621 | ||
4622 | void kvm_arch_hardware_disable(void *garbage) | |
4623 | { | |
4624 | kvm_x86_ops->hardware_disable(garbage); | |
4625 | } | |
4626 | ||
4627 | int kvm_arch_hardware_setup(void) | |
4628 | { | |
4629 | return kvm_x86_ops->hardware_setup(); | |
4630 | } | |
4631 | ||
4632 | void kvm_arch_hardware_unsetup(void) | |
4633 | { | |
4634 | kvm_x86_ops->hardware_unsetup(); | |
4635 | } | |
4636 | ||
4637 | void kvm_arch_check_processor_compat(void *rtn) | |
4638 | { | |
4639 | kvm_x86_ops->check_processor_compatibility(rtn); | |
4640 | } | |
4641 | ||
4642 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) | |
4643 | { | |
4644 | struct page *page; | |
4645 | struct kvm *kvm; | |
4646 | int r; | |
4647 | ||
4648 | BUG_ON(vcpu->kvm == NULL); | |
4649 | kvm = vcpu->kvm; | |
4650 | ||
ad312c7c | 4651 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
c5af89b6 | 4652 | if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu)) |
a4535290 | 4653 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
e9b11c17 | 4654 | else |
a4535290 | 4655 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
e9b11c17 ZX |
4656 | |
4657 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
4658 | if (!page) { | |
4659 | r = -ENOMEM; | |
4660 | goto fail; | |
4661 | } | |
ad312c7c | 4662 | vcpu->arch.pio_data = page_address(page); |
e9b11c17 ZX |
4663 | |
4664 | r = kvm_mmu_create(vcpu); | |
4665 | if (r < 0) | |
4666 | goto fail_free_pio_data; | |
4667 | ||
4668 | if (irqchip_in_kernel(kvm)) { | |
4669 | r = kvm_create_lapic(vcpu); | |
4670 | if (r < 0) | |
4671 | goto fail_mmu_destroy; | |
4672 | } | |
4673 | ||
890ca9ae HY |
4674 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
4675 | GFP_KERNEL); | |
4676 | if (!vcpu->arch.mce_banks) { | |
4677 | r = -ENOMEM; | |
4678 | goto fail_mmu_destroy; | |
4679 | } | |
4680 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; | |
4681 | ||
e9b11c17 ZX |
4682 | return 0; |
4683 | ||
4684 | fail_mmu_destroy: | |
4685 | kvm_mmu_destroy(vcpu); | |
4686 | fail_free_pio_data: | |
ad312c7c | 4687 | free_page((unsigned long)vcpu->arch.pio_data); |
e9b11c17 ZX |
4688 | fail: |
4689 | return r; | |
4690 | } | |
4691 | ||
4692 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
4693 | { | |
4694 | kvm_free_lapic(vcpu); | |
3200f405 | 4695 | down_read(&vcpu->kvm->slots_lock); |
e9b11c17 | 4696 | kvm_mmu_destroy(vcpu); |
3200f405 | 4697 | up_read(&vcpu->kvm->slots_lock); |
ad312c7c | 4698 | free_page((unsigned long)vcpu->arch.pio_data); |
e9b11c17 | 4699 | } |
d19a9cd2 ZX |
4700 | |
4701 | struct kvm *kvm_arch_create_vm(void) | |
4702 | { | |
4703 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
4704 | ||
4705 | if (!kvm) | |
4706 | return ERR_PTR(-ENOMEM); | |
4707 | ||
f05e70ac | 4708 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
4d5c5d0f | 4709 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
d19a9cd2 | 4710 | |
5550af4d SY |
4711 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
4712 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | |
4713 | ||
53f658b3 MT |
4714 | rdtscll(kvm->arch.vm_init_tsc); |
4715 | ||
d19a9cd2 ZX |
4716 | return kvm; |
4717 | } | |
4718 | ||
4719 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) | |
4720 | { | |
4721 | vcpu_load(vcpu); | |
4722 | kvm_mmu_unload(vcpu); | |
4723 | vcpu_put(vcpu); | |
4724 | } | |
4725 | ||
4726 | static void kvm_free_vcpus(struct kvm *kvm) | |
4727 | { | |
4728 | unsigned int i; | |
988a2cae | 4729 | struct kvm_vcpu *vcpu; |
d19a9cd2 ZX |
4730 | |
4731 | /* | |
4732 | * Unpin any mmu pages first. | |
4733 | */ | |
988a2cae GN |
4734 | kvm_for_each_vcpu(i, vcpu, kvm) |
4735 | kvm_unload_vcpu_mmu(vcpu); | |
4736 | kvm_for_each_vcpu(i, vcpu, kvm) | |
4737 | kvm_arch_vcpu_free(vcpu); | |
4738 | ||
4739 | mutex_lock(&kvm->lock); | |
4740 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
4741 | kvm->vcpus[i] = NULL; | |
d19a9cd2 | 4742 | |
988a2cae GN |
4743 | atomic_set(&kvm->online_vcpus, 0); |
4744 | mutex_unlock(&kvm->lock); | |
d19a9cd2 ZX |
4745 | } |
4746 | ||
ad8ba2cd SY |
4747 | void kvm_arch_sync_events(struct kvm *kvm) |
4748 | { | |
ba4cef31 | 4749 | kvm_free_all_assigned_devices(kvm); |
ad8ba2cd SY |
4750 | } |
4751 | ||
d19a9cd2 ZX |
4752 | void kvm_arch_destroy_vm(struct kvm *kvm) |
4753 | { | |
6eb55818 | 4754 | kvm_iommu_unmap_guest(kvm); |
7837699f | 4755 | kvm_free_pit(kvm); |
d7deeeb0 ZX |
4756 | kfree(kvm->arch.vpic); |
4757 | kfree(kvm->arch.vioapic); | |
d19a9cd2 ZX |
4758 | kvm_free_vcpus(kvm); |
4759 | kvm_free_physmem(kvm); | |
3d45830c AK |
4760 | if (kvm->arch.apic_access_page) |
4761 | put_page(kvm->arch.apic_access_page); | |
b7ebfb05 SY |
4762 | if (kvm->arch.ept_identity_pagetable) |
4763 | put_page(kvm->arch.ept_identity_pagetable); | |
d19a9cd2 ZX |
4764 | kfree(kvm); |
4765 | } | |
0de10343 ZX |
4766 | |
4767 | int kvm_arch_set_memory_region(struct kvm *kvm, | |
4768 | struct kvm_userspace_memory_region *mem, | |
4769 | struct kvm_memory_slot old, | |
4770 | int user_alloc) | |
4771 | { | |
4772 | int npages = mem->memory_size >> PAGE_SHIFT; | |
4773 | struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot]; | |
4774 | ||
4775 | /*To keep backward compatibility with older userspace, | |
4776 | *x86 needs to hanlde !user_alloc case. | |
4777 | */ | |
4778 | if (!user_alloc) { | |
4779 | if (npages && !old.rmap) { | |
604b38ac AA |
4780 | unsigned long userspace_addr; |
4781 | ||
72dc67a6 | 4782 | down_write(¤t->mm->mmap_sem); |
604b38ac AA |
4783 | userspace_addr = do_mmap(NULL, 0, |
4784 | npages * PAGE_SIZE, | |
4785 | PROT_READ | PROT_WRITE, | |
acee3c04 | 4786 | MAP_PRIVATE | MAP_ANONYMOUS, |
604b38ac | 4787 | 0); |
72dc67a6 | 4788 | up_write(¤t->mm->mmap_sem); |
0de10343 | 4789 | |
604b38ac AA |
4790 | if (IS_ERR((void *)userspace_addr)) |
4791 | return PTR_ERR((void *)userspace_addr); | |
4792 | ||
4793 | /* set userspace_addr atomically for kvm_hva_to_rmapp */ | |
4794 | spin_lock(&kvm->mmu_lock); | |
4795 | memslot->userspace_addr = userspace_addr; | |
4796 | spin_unlock(&kvm->mmu_lock); | |
0de10343 ZX |
4797 | } else { |
4798 | if (!old.user_alloc && old.rmap) { | |
4799 | int ret; | |
4800 | ||
72dc67a6 | 4801 | down_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4802 | ret = do_munmap(current->mm, old.userspace_addr, |
4803 | old.npages * PAGE_SIZE); | |
72dc67a6 | 4804 | up_write(¤t->mm->mmap_sem); |
0de10343 ZX |
4805 | if (ret < 0) |
4806 | printk(KERN_WARNING | |
4807 | "kvm_vm_ioctl_set_memory_region: " | |
4808 | "failed to munmap memory\n"); | |
4809 | } | |
4810 | } | |
4811 | } | |
4812 | ||
7c8a83b7 | 4813 | spin_lock(&kvm->mmu_lock); |
f05e70ac | 4814 | if (!kvm->arch.n_requested_mmu_pages) { |
0de10343 ZX |
4815 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
4816 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
4817 | } | |
4818 | ||
4819 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); | |
7c8a83b7 | 4820 | spin_unlock(&kvm->mmu_lock); |
0de10343 ZX |
4821 | kvm_flush_remote_tlbs(kvm); |
4822 | ||
4823 | return 0; | |
4824 | } | |
1d737c8a | 4825 | |
34d4cb8f MT |
4826 | void kvm_arch_flush_shadow(struct kvm *kvm) |
4827 | { | |
4828 | kvm_mmu_zap_all(kvm); | |
8986ecc0 | 4829 | kvm_reload_remote_mmus(kvm); |
34d4cb8f MT |
4830 | } |
4831 | ||
1d737c8a ZX |
4832 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
4833 | { | |
a4535290 | 4834 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
0496fbb9 JK |
4835 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
4836 | || vcpu->arch.nmi_pending; | |
1d737c8a | 4837 | } |
5736199a | 4838 | |
5736199a ZX |
4839 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
4840 | { | |
32f88400 MT |
4841 | int me; |
4842 | int cpu = vcpu->cpu; | |
5736199a ZX |
4843 | |
4844 | if (waitqueue_active(&vcpu->wq)) { | |
4845 | wake_up_interruptible(&vcpu->wq); | |
4846 | ++vcpu->stat.halt_wakeup; | |
4847 | } | |
32f88400 MT |
4848 | |
4849 | me = get_cpu(); | |
4850 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) | |
4851 | if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests)) | |
4852 | smp_send_reschedule(cpu); | |
e9571ed5 | 4853 | put_cpu(); |
5736199a | 4854 | } |
78646121 GN |
4855 | |
4856 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) | |
4857 | { | |
4858 | return kvm_x86_ops->interrupt_allowed(vcpu); | |
4859 | } | |
229456fc MT |
4860 | |
4861 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); | |
4862 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); | |
4863 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); | |
4864 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); | |
4865 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |