blob: 9b0758b4d5504e38605f0c12e043047bb1c35abe [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
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
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010034#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080035#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050036#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010037#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030038#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030040#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020041#include <linux/srcu.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030042#include <trace/events/kvm.h>
43#undef TRACE_INCLUDE_FILE
Marcelo Tosatti229456f2009-06-17 09:22:14 -030044#define CREATE_TRACE_POINTS
45#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020046
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020047#include <asm/debugreg.h>
Carsten Otte043405e2007-10-10 17:16:19 +020048#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080049#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020050#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080051#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080052#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020053
Carsten Otte313a3dc2007-10-11 19:16:52 +020054#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010055#define CR0_RESERVED_BITS \
56 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
57 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
58 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
59#define CR4_RESERVED_BITS \
60 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
61 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
62 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
63 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
64
65#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080066
67#define KVM_MAX_MCE_BANKS 32
68#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
69
Joerg Roedel50a37eb2008-01-31 14:57:38 +010070/* EFER defaults:
71 * - enable syscall per default because its emulated by KVM
72 * - enable LME and LMA per default on 64 bit KVM
73 */
74#ifdef CONFIG_X86_64
75static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
76#else
77static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
78#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020079
Avi Kivityba1389b2007-11-18 16:24:12 +020080#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
81#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050082
Gleb Natapovcb142eb2009-08-09 15:17:40 +030083static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020084static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
85 struct kvm_cpuid_entry2 __user *entries);
86
Zhang Xiantao97896d02007-11-14 20:09:30 +080087struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030088EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080089
Andre Przywaraed85c062009-06-25 12:36:49 +020090int ignore_msrs = 0;
91module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
92
Avi Kivity18863bd2009-09-07 11:12:18 +030093#define KVM_NR_SHARED_MSRS 16
94
95struct kvm_shared_msrs_global {
96 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +080097 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +030098};
99
100struct kvm_shared_msrs {
101 struct user_return_notifier urn;
102 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800103 struct kvm_shared_msr_values {
104 u64 host;
105 u64 curr;
106 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300107};
108
109static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
110static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
111
Hollis Blanchard417bc302007-10-31 17:24:23 -0500112struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200113 { "pf_fixed", VCPU_STAT(pf_fixed) },
114 { "pf_guest", VCPU_STAT(pf_guest) },
115 { "tlb_flush", VCPU_STAT(tlb_flush) },
116 { "invlpg", VCPU_STAT(invlpg) },
117 { "exits", VCPU_STAT(exits) },
118 { "io_exits", VCPU_STAT(io_exits) },
119 { "mmio_exits", VCPU_STAT(mmio_exits) },
120 { "signal_exits", VCPU_STAT(signal_exits) },
121 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800122 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200123 { "halt_exits", VCPU_STAT(halt_exits) },
124 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530125 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200126 { "request_irq", VCPU_STAT(request_irq_exits) },
127 { "irq_exits", VCPU_STAT(irq_exits) },
128 { "host_state_reload", VCPU_STAT(host_state_reload) },
129 { "efer_reload", VCPU_STAT(efer_reload) },
130 { "fpu_reload", VCPU_STAT(fpu_reload) },
131 { "insn_emulation", VCPU_STAT(insn_emulation) },
132 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300133 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200134 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200135 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
136 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
137 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
138 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
139 { "mmu_flooded", VM_STAT(mmu_flooded) },
140 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200141 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300142 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200143 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300144 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500145 { NULL }
146};
147
Avi Kivity18863bd2009-09-07 11:12:18 +0300148static void kvm_on_user_return(struct user_return_notifier *urn)
149{
150 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300151 struct kvm_shared_msrs *locals
152 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800153 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300154
155 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800156 values = &locals->values[slot];
157 if (values->host != values->curr) {
158 wrmsrl(shared_msrs_global.msrs[slot], values->host);
159 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300160 }
161 }
162 locals->registered = false;
163 user_return_notifier_unregister(urn);
164}
165
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800166static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300167{
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800168 struct kvm_shared_msrs *smsr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300169 u64 value;
170
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800171 smsr = &__get_cpu_var(shared_msrs);
172 /* only read, and nobody should modify it at this time,
173 * so don't need lock */
174 if (slot >= shared_msrs_global.nr) {
175 printk(KERN_ERR "kvm: invalid MSR slot!");
176 return;
177 }
178 rdmsrl_safe(msr, &value);
179 smsr->values[slot].host = value;
180 smsr->values[slot].curr = value;
181}
182
183void kvm_define_shared_msr(unsigned slot, u32 msr)
184{
Avi Kivity18863bd2009-09-07 11:12:18 +0300185 if (slot >= shared_msrs_global.nr)
186 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800187 shared_msrs_global.msrs[slot] = msr;
188 /* we need ensured the shared_msr_global have been updated */
189 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300190}
191EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
192
193static void kvm_shared_msr_cpu_online(void)
194{
195 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300196
197 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800198 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300199}
200
Avi Kivityd5696722009-12-02 12:28:47 +0200201void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300202{
203 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
204
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800205 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300206 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800207 smsr->values[slot].curr = value;
208 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300209 if (!smsr->registered) {
210 smsr->urn.on_user_return = kvm_on_user_return;
211 user_return_notifier_register(&smsr->urn);
212 smsr->registered = true;
213 }
214}
215EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
216
Avi Kivity3548bab2009-11-28 14:18:47 +0200217static void drop_user_return_notifiers(void *ignore)
218{
219 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
220
221 if (smsr->registered)
222 kvm_on_user_return(&smsr->urn);
223}
224
Carsten Otte5fb76f92007-10-29 16:08:51 +0100225unsigned long segment_base(u16 selector)
226{
227 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200228 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100229 unsigned long table_base;
230 unsigned long v;
231
232 if (selector == 0)
233 return 0;
234
Akinobu Mitab792c342009-07-19 00:00:01 +0900235 kvm_get_gdt(&gdt);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100236 table_base = gdt.base;
237
238 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900239 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100240
Carsten Otte5fb76f92007-10-29 16:08:51 +0100241 table_base = segment_base(ldt_selector);
242 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200243 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900244 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100245#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200246 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
247 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100248#endif
249 return v;
250}
251EXPORT_SYMBOL_GPL(segment_base);
252
Carsten Otte6866b832007-10-29 16:09:10 +0100253u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
254{
255 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800256 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100257 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800258 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100259}
260EXPORT_SYMBOL_GPL(kvm_get_apic_base);
261
262void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
263{
264 /* TODO: reserve bits check */
265 if (irqchip_in_kernel(vcpu->kvm))
266 kvm_lapic_set_base(vcpu, data);
267 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800268 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100269}
270EXPORT_SYMBOL_GPL(kvm_set_apic_base);
271
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200272#define EXCPT_BENIGN 0
273#define EXCPT_CONTRIBUTORY 1
274#define EXCPT_PF 2
275
276static int exception_class(int vector)
277{
278 switch (vector) {
279 case PF_VECTOR:
280 return EXCPT_PF;
281 case DE_VECTOR:
282 case TS_VECTOR:
283 case NP_VECTOR:
284 case SS_VECTOR:
285 case GP_VECTOR:
286 return EXCPT_CONTRIBUTORY;
287 default:
288 break;
289 }
290 return EXCPT_BENIGN;
291}
292
293static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
294 unsigned nr, bool has_error, u32 error_code)
295{
296 u32 prev_nr;
297 int class1, class2;
298
299 if (!vcpu->arch.exception.pending) {
300 queue:
301 vcpu->arch.exception.pending = true;
302 vcpu->arch.exception.has_error_code = has_error;
303 vcpu->arch.exception.nr = nr;
304 vcpu->arch.exception.error_code = error_code;
305 return;
306 }
307
308 /* to check exception */
309 prev_nr = vcpu->arch.exception.nr;
310 if (prev_nr == DF_VECTOR) {
311 /* triple fault -> shutdown */
312 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
313 return;
314 }
315 class1 = exception_class(prev_nr);
316 class2 = exception_class(nr);
317 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
318 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
319 /* generate double fault per SDM Table 5-5 */
320 vcpu->arch.exception.pending = true;
321 vcpu->arch.exception.has_error_code = true;
322 vcpu->arch.exception.nr = DF_VECTOR;
323 vcpu->arch.exception.error_code = 0;
324 } else
325 /* replace previous exception with a new one in a hope
326 that instruction re-execution will regenerate lost
327 exception */
328 goto queue;
329}
330
Avi Kivity298101d2007-11-25 13:41:11 +0200331void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
332{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200333 kvm_multiple_exception(vcpu, nr, false, 0);
Avi Kivity298101d2007-11-25 13:41:11 +0200334}
335EXPORT_SYMBOL_GPL(kvm_queue_exception);
336
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200337void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
338 u32 error_code)
339{
340 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800341 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200342 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
343}
344
Sheng Yang3419ffc2008-05-15 09:52:48 +0800345void kvm_inject_nmi(struct kvm_vcpu *vcpu)
346{
347 vcpu->arch.nmi_pending = 1;
348}
349EXPORT_SYMBOL_GPL(kvm_inject_nmi);
350
Avi Kivity298101d2007-11-25 13:41:11 +0200351void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
352{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200353 kvm_multiple_exception(vcpu, nr, true, error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200354}
355EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
356
Carsten Ottea03490e2007-10-29 16:09:35 +0100357/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300358 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
359 * a #GP and return false.
360 */
361bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200362{
Avi Kivity0a79b002009-09-01 12:03:25 +0300363 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
364 return true;
365 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
366 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100367}
Avi Kivity0a79b002009-09-01 12:03:25 +0300368EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100369
370/*
371 * Load the pae pdptrs. Return true is they are all valid.
372 */
373int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
374{
375 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
376 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
377 int i;
378 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800379 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100380
Carsten Ottea03490e2007-10-29 16:09:35 +0100381 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
382 offset * sizeof(u64), sizeof(pdpte));
383 if (ret < 0) {
384 ret = 0;
385 goto out;
386 }
387 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300388 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800389 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100390 ret = 0;
391 goto out;
392 }
393 }
394 ret = 1;
395
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800396 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300397 __set_bit(VCPU_EXREG_PDPTR,
398 (unsigned long *)&vcpu->arch.regs_avail);
399 __set_bit(VCPU_EXREG_PDPTR,
400 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100401out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100402
403 return ret;
404}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100405EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100406
Avi Kivityd835dfe2007-11-21 02:57:59 +0200407static bool pdptrs_changed(struct kvm_vcpu *vcpu)
408{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800409 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200410 bool changed = true;
411 int r;
412
413 if (is_long_mode(vcpu) || !is_pae(vcpu))
414 return false;
415
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300416 if (!test_bit(VCPU_EXREG_PDPTR,
417 (unsigned long *)&vcpu->arch.regs_avail))
418 return true;
419
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800420 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200421 if (r < 0)
422 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800423 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200424out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200425
426 return changed;
427}
428
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200429void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100430{
Avi Kivityf9a48e62010-01-06 19:10:22 +0200431 cr0 |= X86_CR0_ET;
432
Carsten Ottea03490e2007-10-29 16:09:35 +0100433 if (cr0 & CR0_RESERVED_BITS) {
434 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Avi Kivity4d4ec082009-12-29 18:07:30 +0200435 cr0, kvm_read_cr0(vcpu));
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200436 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100437 return;
438 }
439
440 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
441 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200442 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100443 return;
444 }
445
446 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
447 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
448 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200449 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100450 return;
451 }
452
453 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
454#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800455 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100456 int cs_db, cs_l;
457
458 if (!is_pae(vcpu)) {
459 printk(KERN_DEBUG "set_cr0: #GP, start paging "
460 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200461 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100462 return;
463 }
464 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
465 if (cs_l) {
466 printk(KERN_DEBUG "set_cr0: #GP, start paging "
467 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200468 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100469 return;
470
471 }
472 } else
473#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800474 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100475 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
476 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200477 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100478 return;
479 }
480
481 }
482
483 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800484 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100485
Carsten Ottea03490e2007-10-29 16:09:35 +0100486 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100487 return;
488}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200489EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100490
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200491void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100492{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200493 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100494}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200495EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100496
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200497void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100498{
Avi Kivityfc78f512009-12-07 12:16:48 +0200499 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300500 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
501
Carsten Ottea03490e2007-10-29 16:09:35 +0100502 if (cr4 & CR4_RESERVED_BITS) {
503 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200504 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100505 return;
506 }
507
508 if (is_long_mode(vcpu)) {
509 if (!(cr4 & X86_CR4_PAE)) {
510 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
511 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200512 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100513 return;
514 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300515 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
516 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800517 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100518 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200519 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100520 return;
521 }
522
523 if (cr4 & X86_CR4_VMXE) {
524 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200525 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100526 return;
527 }
528 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800529 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200530 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100531 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100532}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200533EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100534
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200535void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100536{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800537 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300538 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200539 kvm_mmu_flush_tlb(vcpu);
540 return;
541 }
542
Carsten Ottea03490e2007-10-29 16:09:35 +0100543 if (is_long_mode(vcpu)) {
544 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
545 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200546 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100547 return;
548 }
549 } else {
550 if (is_pae(vcpu)) {
551 if (cr3 & CR3_PAE_RESERVED_BITS) {
552 printk(KERN_DEBUG
553 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200554 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100555 return;
556 }
557 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
558 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
559 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200560 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100561 return;
562 }
563 }
564 /*
565 * We don't check reserved bits in nonpae mode, because
566 * this isn't enforced, and VMware depends on this.
567 */
568 }
569
Carsten Ottea03490e2007-10-29 16:09:35 +0100570 /*
571 * Does the new cr3 value map to physical memory? (Note, we
572 * catch an invalid cr3 even in real-mode, because it would
573 * cause trouble later on when we turn on paging anyway.)
574 *
575 * A real CPU would silently accept an invalid cr3 and would
576 * attempt to use it - with largely undefined (and often hard
577 * to debug) behavior on the guest side.
578 */
579 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200580 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100581 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800582 vcpu->arch.cr3 = cr3;
583 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100584 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100585}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200586EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100587
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200588void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100589{
590 if (cr8 & CR8_RESERVED_BITS) {
591 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200592 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100593 return;
594 }
595 if (irqchip_in_kernel(vcpu->kvm))
596 kvm_lapic_set_tpr(vcpu, cr8);
597 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800598 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100599}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200600EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100601
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200602unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100603{
604 if (irqchip_in_kernel(vcpu->kvm))
605 return kvm_lapic_get_cr8(vcpu);
606 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800607 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100608}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200609EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100610
Alexander Grafd8017472008-11-25 20:17:11 +0100611static inline u32 bit(int bitno)
612{
613 return 1 << (bitno & 31);
614}
615
Carsten Otte043405e2007-10-10 17:16:19 +0200616/*
617 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
618 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
619 *
620 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400621 * capabilities of the host cpu. This capabilities test skips MSRs that are
622 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200623 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400624
Gleb Natapov10388a02010-01-17 15:51:23 +0200625#define KVM_SAVE_MSRS_BEGIN 5
Carsten Otte043405e2007-10-10 17:16:19 +0200626static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400627 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200628 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200629 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200630 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
631 MSR_K6_STAR,
632#ifdef CONFIG_X86_64
633 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
634#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400635 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200636};
637
638static unsigned num_msrs_to_save;
639
640static u32 emulated_msrs[] = {
641 MSR_IA32_MISC_ENABLE,
642};
643
Carsten Otte15c4a642007-10-30 18:44:17 +0100644static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
645{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100646 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100647 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
648 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200649 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100650 return;
651 }
652
653 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800654 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100655 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200656 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100657 return;
658 }
659
Alexander Graf1b2fd702009-02-02 16:23:51 +0100660 if (efer & EFER_FFXSR) {
661 struct kvm_cpuid_entry2 *feat;
662
663 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
664 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
665 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
666 kvm_inject_gp(vcpu, 0);
667 return;
668 }
669 }
670
Alexander Grafd8017472008-11-25 20:17:11 +0100671 if (efer & EFER_SVME) {
672 struct kvm_cpuid_entry2 *feat;
673
674 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
675 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
676 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
677 kvm_inject_gp(vcpu, 0);
678 return;
679 }
680 }
681
Carsten Otte15c4a642007-10-30 18:44:17 +0100682 kvm_x86_ops->set_efer(vcpu, efer);
683
684 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800685 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100686
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800687 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300688
689 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
690 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100691}
692
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100693void kvm_enable_efer_bits(u64 mask)
694{
695 efer_reserved_bits &= ~mask;
696}
697EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
698
699
Carsten Otte15c4a642007-10-30 18:44:17 +0100700/*
701 * Writes msr value into into the appropriate "register".
702 * Returns 0 on success, non-0 otherwise.
703 * Assumes vcpu_load() was already called.
704 */
705int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
706{
707 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
708}
709
Carsten Otte313a3dc2007-10-11 19:16:52 +0200710/*
711 * Adapt set_msr() to msr_io()'s calling convention
712 */
713static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
714{
715 return kvm_set_msr(vcpu, index, *data);
716}
717
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200718static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
719{
720 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200721 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800722 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200723
724 if (!wall_clock)
725 return;
726
727 version++;
728
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200729 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
730
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200731 /*
732 * The guest calculates current wall clock time by adding
733 * system time (updated by kvm_write_guest_time below) to the
734 * wall clock specified here. guest system time equals host
735 * system time for us, thus we must fill in host boot time here.
736 */
Jason Wang923de3c2010-01-27 19:13:49 +0800737 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200738
739 wc.sec = boot.tv_sec;
740 wc.nsec = boot.tv_nsec;
741 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200742
743 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
744
745 version++;
746 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200747}
748
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200749static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
750{
751 uint32_t quotient, remainder;
752
753 /* Don't try to replace with do_div(), this one calculates
754 * "(dividend << 32) / divisor" */
755 __asm__ ( "divl %4"
756 : "=a" (quotient), "=d" (remainder)
757 : "0" (0), "1" (dividend), "r" (divisor) );
758 return quotient;
759}
760
761static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
762{
763 uint64_t nsecs = 1000000000LL;
764 int32_t shift = 0;
765 uint64_t tps64;
766 uint32_t tps32;
767
768 tps64 = tsc_khz * 1000LL;
769 while (tps64 > nsecs*2) {
770 tps64 >>= 1;
771 shift--;
772 }
773
774 tps32 = (uint32_t)tps64;
775 while (tps32 <= (uint32_t)nsecs) {
776 tps32 <<= 1;
777 shift++;
778 }
779
780 hv_clock->tsc_shift = shift;
781 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
782
783 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700784 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200785 hv_clock->tsc_to_system_mul);
786}
787
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100788static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
789
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200790static void kvm_write_guest_time(struct kvm_vcpu *v)
791{
792 struct timespec ts;
793 unsigned long flags;
794 struct kvm_vcpu_arch *vcpu = &v->arch;
795 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300796 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200797
798 if ((!vcpu->time_page))
799 return;
800
Avi Kivity463656c2009-04-12 15:49:07 +0300801 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
802 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
803 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
804 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200805 }
Avi Kivity463656c2009-04-12 15:49:07 +0300806 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200807
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200808 /* Keep irq disabled to prevent changes to the clock */
809 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530810 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200811 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800812 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200813 local_irq_restore(flags);
814
815 /* With all the info we got, fill in the values */
816
817 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400818 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
819
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200820 /*
821 * The interface expects us to write an even number signaling that the
822 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200823 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200824 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200825 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200826
827 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
828
829 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200830 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200831
832 kunmap_atomic(shared_kaddr, KM_USER0);
833
834 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
835}
836
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100837static int kvm_request_guest_time_update(struct kvm_vcpu *v)
838{
839 struct kvm_vcpu_arch *vcpu = &v->arch;
840
841 if (!vcpu->time_page)
842 return 0;
843 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
844 return 1;
845}
846
Avi Kivity9ba075a2008-05-26 20:06:35 +0300847static bool msr_mtrr_valid(unsigned msr)
848{
849 switch (msr) {
850 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
851 case MSR_MTRRfix64K_00000:
852 case MSR_MTRRfix16K_80000:
853 case MSR_MTRRfix16K_A0000:
854 case MSR_MTRRfix4K_C0000:
855 case MSR_MTRRfix4K_C8000:
856 case MSR_MTRRfix4K_D0000:
857 case MSR_MTRRfix4K_D8000:
858 case MSR_MTRRfix4K_E0000:
859 case MSR_MTRRfix4K_E8000:
860 case MSR_MTRRfix4K_F0000:
861 case MSR_MTRRfix4K_F8000:
862 case MSR_MTRRdefType:
863 case MSR_IA32_CR_PAT:
864 return true;
865 case 0x2f8:
866 return true;
867 }
868 return false;
869}
870
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300871static bool valid_pat_type(unsigned t)
872{
873 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
874}
875
876static bool valid_mtrr_type(unsigned t)
877{
878 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
879}
880
881static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
882{
883 int i;
884
885 if (!msr_mtrr_valid(msr))
886 return false;
887
888 if (msr == MSR_IA32_CR_PAT) {
889 for (i = 0; i < 8; i++)
890 if (!valid_pat_type((data >> (i * 8)) & 0xff))
891 return false;
892 return true;
893 } else if (msr == MSR_MTRRdefType) {
894 if (data & ~0xcff)
895 return false;
896 return valid_mtrr_type(data & 0xff);
897 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
898 for (i = 0; i < 8 ; i++)
899 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
900 return false;
901 return true;
902 }
903
904 /* variable MTRRs */
905 return valid_mtrr_type(data & 0xff);
906}
907
Avi Kivity9ba075a2008-05-26 20:06:35 +0300908static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
909{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800910 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
911
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300912 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300913 return 1;
914
Sheng Yang0bed3b52008-10-09 16:01:54 +0800915 if (msr == MSR_MTRRdefType) {
916 vcpu->arch.mtrr_state.def_type = data;
917 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
918 } else if (msr == MSR_MTRRfix64K_00000)
919 p[0] = data;
920 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
921 p[1 + msr - MSR_MTRRfix16K_80000] = data;
922 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
923 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
924 else if (msr == MSR_IA32_CR_PAT)
925 vcpu->arch.pat = data;
926 else { /* Variable MTRRs */
927 int idx, is_mtrr_mask;
928 u64 *pt;
929
930 idx = (msr - 0x200) / 2;
931 is_mtrr_mask = msr - 0x200 - 2 * idx;
932 if (!is_mtrr_mask)
933 pt =
934 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
935 else
936 pt =
937 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
938 *pt = data;
939 }
940
941 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300942 return 0;
943}
Carsten Otte15c4a642007-10-30 18:44:17 +0100944
Huang Ying890ca9a2009-05-11 16:48:15 +0800945static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
946{
947 u64 mcg_cap = vcpu->arch.mcg_cap;
948 unsigned bank_num = mcg_cap & 0xff;
949
950 switch (msr) {
951 case MSR_IA32_MCG_STATUS:
952 vcpu->arch.mcg_status = data;
953 break;
954 case MSR_IA32_MCG_CTL:
955 if (!(mcg_cap & MCG_CTL_P))
956 return 1;
957 if (data != 0 && data != ~(u64)0)
958 return -1;
959 vcpu->arch.mcg_ctl = data;
960 break;
961 default:
962 if (msr >= MSR_IA32_MC0_CTL &&
963 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
964 u32 offset = msr - MSR_IA32_MC0_CTL;
965 /* only 0 or all 1s can be written to IA32_MCi_CTL */
966 if ((offset & 0x3) == 0 &&
967 data != 0 && data != ~(u64)0)
968 return -1;
969 vcpu->arch.mce_banks[offset] = data;
970 break;
971 }
972 return 1;
973 }
974 return 0;
975}
976
Ed Swierkffde22a2009-10-15 15:21:43 -0700977static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
978{
979 struct kvm *kvm = vcpu->kvm;
980 int lm = is_long_mode(vcpu);
981 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
982 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
983 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
984 : kvm->arch.xen_hvm_config.blob_size_32;
985 u32 page_num = data & ~PAGE_MASK;
986 u64 page_addr = data & PAGE_MASK;
987 u8 *page;
988 int r;
989
990 r = -E2BIG;
991 if (page_num >= blob_size)
992 goto out;
993 r = -ENOMEM;
994 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
995 if (!page)
996 goto out;
997 r = -EFAULT;
998 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
999 goto out_free;
1000 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1001 goto out_free;
1002 r = 0;
1003out_free:
1004 kfree(page);
1005out:
1006 return r;
1007}
1008
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001009static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1010{
1011 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1012}
1013
1014static bool kvm_hv_msr_partition_wide(u32 msr)
1015{
1016 bool r = false;
1017 switch (msr) {
1018 case HV_X64_MSR_GUEST_OS_ID:
1019 case HV_X64_MSR_HYPERCALL:
1020 r = true;
1021 break;
1022 }
1023
1024 return r;
1025}
1026
1027static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1028{
1029 struct kvm *kvm = vcpu->kvm;
1030
1031 switch (msr) {
1032 case HV_X64_MSR_GUEST_OS_ID:
1033 kvm->arch.hv_guest_os_id = data;
1034 /* setting guest os id to zero disables hypercall page */
1035 if (!kvm->arch.hv_guest_os_id)
1036 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1037 break;
1038 case HV_X64_MSR_HYPERCALL: {
1039 u64 gfn;
1040 unsigned long addr;
1041 u8 instructions[4];
1042
1043 /* if guest os id is not set hypercall should remain disabled */
1044 if (!kvm->arch.hv_guest_os_id)
1045 break;
1046 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1047 kvm->arch.hv_hypercall = data;
1048 break;
1049 }
1050 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1051 addr = gfn_to_hva(kvm, gfn);
1052 if (kvm_is_error_hva(addr))
1053 return 1;
1054 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1055 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1056 if (copy_to_user((void __user *)addr, instructions, 4))
1057 return 1;
1058 kvm->arch.hv_hypercall = data;
1059 break;
1060 }
1061 default:
1062 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1063 "data 0x%llx\n", msr, data);
1064 return 1;
1065 }
1066 return 0;
1067}
1068
1069static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1070{
Gleb Natapov10388a02010-01-17 15:51:23 +02001071 switch (msr) {
1072 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1073 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001074
Gleb Natapov10388a02010-01-17 15:51:23 +02001075 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1076 vcpu->arch.hv_vapic = data;
1077 break;
1078 }
1079 addr = gfn_to_hva(vcpu->kvm, data >>
1080 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1081 if (kvm_is_error_hva(addr))
1082 return 1;
1083 if (clear_user((void __user *)addr, PAGE_SIZE))
1084 return 1;
1085 vcpu->arch.hv_vapic = data;
1086 break;
1087 }
1088 case HV_X64_MSR_EOI:
1089 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1090 case HV_X64_MSR_ICR:
1091 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1092 case HV_X64_MSR_TPR:
1093 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1094 default:
1095 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1096 "data 0x%llx\n", msr, data);
1097 return 1;
1098 }
1099
1100 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001101}
1102
Carsten Otte15c4a642007-10-30 18:44:17 +01001103int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1104{
1105 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001106 case MSR_EFER:
1107 set_efer(vcpu, data);
1108 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +02001109 case MSR_K7_HWCR:
1110 data &= ~(u64)0x40; /* ignore flush filter disable */
1111 if (data != 0) {
1112 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1113 data);
1114 return 1;
1115 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001116 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001117 case MSR_FAM10H_MMIO_CONF_BASE:
1118 if (data != 0) {
1119 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1120 "0x%llx\n", data);
1121 return 1;
1122 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001123 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001124 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001125 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001126 case MSR_IA32_DEBUGCTLMSR:
1127 if (!data) {
1128 /* We support the non-activated case already */
1129 break;
1130 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1131 /* Values other than LBR and BTF are vendor-specific,
1132 thus reserved and should throw a #GP */
1133 return 1;
1134 }
1135 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1136 __func__, data);
1137 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001138 case MSR_IA32_UCODE_REV:
1139 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001140 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001141 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001142 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001143 case 0x200 ... 0x2ff:
1144 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001145 case MSR_IA32_APICBASE:
1146 kvm_set_apic_base(vcpu, data);
1147 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001148 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1149 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001150 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001151 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001152 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001153 case MSR_KVM_WALL_CLOCK:
1154 vcpu->kvm->arch.wall_clock = data;
1155 kvm_write_wall_clock(vcpu->kvm, data);
1156 break;
1157 case MSR_KVM_SYSTEM_TIME: {
1158 if (vcpu->arch.time_page) {
1159 kvm_release_page_dirty(vcpu->arch.time_page);
1160 vcpu->arch.time_page = NULL;
1161 }
1162
1163 vcpu->arch.time = data;
1164
1165 /* we verify if the enable bit is set... */
1166 if (!(data & 1))
1167 break;
1168
1169 /* ...but clean it before doing the actual write */
1170 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1171
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001172 vcpu->arch.time_page =
1173 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001174
1175 if (is_error_page(vcpu->arch.time_page)) {
1176 kvm_release_page_clean(vcpu->arch.time_page);
1177 vcpu->arch.time_page = NULL;
1178 }
1179
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001180 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001181 break;
1182 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001183 case MSR_IA32_MCG_CTL:
1184 case MSR_IA32_MCG_STATUS:
1185 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1186 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001187
1188 /* Performance counters are not protected by a CPUID bit,
1189 * so we should check all of them in the generic path for the sake of
1190 * cross vendor migration.
1191 * Writing a zero into the event select MSRs disables them,
1192 * which we perfectly emulate ;-). Any other value should be at least
1193 * reported, some guests depend on them.
1194 */
1195 case MSR_P6_EVNTSEL0:
1196 case MSR_P6_EVNTSEL1:
1197 case MSR_K7_EVNTSEL0:
1198 case MSR_K7_EVNTSEL1:
1199 case MSR_K7_EVNTSEL2:
1200 case MSR_K7_EVNTSEL3:
1201 if (data != 0)
1202 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1203 "0x%x data 0x%llx\n", msr, data);
1204 break;
1205 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1206 * so we ignore writes to make it happy.
1207 */
1208 case MSR_P6_PERFCTR0:
1209 case MSR_P6_PERFCTR1:
1210 case MSR_K7_PERFCTR0:
1211 case MSR_K7_PERFCTR1:
1212 case MSR_K7_PERFCTR2:
1213 case MSR_K7_PERFCTR3:
1214 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1215 "0x%x data 0x%llx\n", msr, data);
1216 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001217 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1218 if (kvm_hv_msr_partition_wide(msr)) {
1219 int r;
1220 mutex_lock(&vcpu->kvm->lock);
1221 r = set_msr_hyperv_pw(vcpu, msr, data);
1222 mutex_unlock(&vcpu->kvm->lock);
1223 return r;
1224 } else
1225 return set_msr_hyperv(vcpu, msr, data);
1226 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001227 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001228 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1229 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001230 if (!ignore_msrs) {
1231 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1232 msr, data);
1233 return 1;
1234 } else {
1235 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1236 msr, data);
1237 break;
1238 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001239 }
1240 return 0;
1241}
1242EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1243
1244
1245/*
1246 * Reads an msr value (of 'msr_index') into 'pdata'.
1247 * Returns 0 on success, non-0 otherwise.
1248 * Assumes vcpu_load() was already called.
1249 */
1250int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1251{
1252 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1253}
1254
Avi Kivity9ba075a2008-05-26 20:06:35 +03001255static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1256{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001257 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1258
Avi Kivity9ba075a2008-05-26 20:06:35 +03001259 if (!msr_mtrr_valid(msr))
1260 return 1;
1261
Sheng Yang0bed3b52008-10-09 16:01:54 +08001262 if (msr == MSR_MTRRdefType)
1263 *pdata = vcpu->arch.mtrr_state.def_type +
1264 (vcpu->arch.mtrr_state.enabled << 10);
1265 else if (msr == MSR_MTRRfix64K_00000)
1266 *pdata = p[0];
1267 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1268 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1269 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1270 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1271 else if (msr == MSR_IA32_CR_PAT)
1272 *pdata = vcpu->arch.pat;
1273 else { /* Variable MTRRs */
1274 int idx, is_mtrr_mask;
1275 u64 *pt;
1276
1277 idx = (msr - 0x200) / 2;
1278 is_mtrr_mask = msr - 0x200 - 2 * idx;
1279 if (!is_mtrr_mask)
1280 pt =
1281 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1282 else
1283 pt =
1284 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1285 *pdata = *pt;
1286 }
1287
Avi Kivity9ba075a2008-05-26 20:06:35 +03001288 return 0;
1289}
1290
Huang Ying890ca9a2009-05-11 16:48:15 +08001291static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1292{
1293 u64 data;
1294 u64 mcg_cap = vcpu->arch.mcg_cap;
1295 unsigned bank_num = mcg_cap & 0xff;
1296
1297 switch (msr) {
1298 case MSR_IA32_P5_MC_ADDR:
1299 case MSR_IA32_P5_MC_TYPE:
1300 data = 0;
1301 break;
1302 case MSR_IA32_MCG_CAP:
1303 data = vcpu->arch.mcg_cap;
1304 break;
1305 case MSR_IA32_MCG_CTL:
1306 if (!(mcg_cap & MCG_CTL_P))
1307 return 1;
1308 data = vcpu->arch.mcg_ctl;
1309 break;
1310 case MSR_IA32_MCG_STATUS:
1311 data = vcpu->arch.mcg_status;
1312 break;
1313 default:
1314 if (msr >= MSR_IA32_MC0_CTL &&
1315 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1316 u32 offset = msr - MSR_IA32_MC0_CTL;
1317 data = vcpu->arch.mce_banks[offset];
1318 break;
1319 }
1320 return 1;
1321 }
1322 *pdata = data;
1323 return 0;
1324}
1325
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001326static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1327{
1328 u64 data = 0;
1329 struct kvm *kvm = vcpu->kvm;
1330
1331 switch (msr) {
1332 case HV_X64_MSR_GUEST_OS_ID:
1333 data = kvm->arch.hv_guest_os_id;
1334 break;
1335 case HV_X64_MSR_HYPERCALL:
1336 data = kvm->arch.hv_hypercall;
1337 break;
1338 default:
1339 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1340 return 1;
1341 }
1342
1343 *pdata = data;
1344 return 0;
1345}
1346
1347static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1348{
1349 u64 data = 0;
1350
1351 switch (msr) {
1352 case HV_X64_MSR_VP_INDEX: {
1353 int r;
1354 struct kvm_vcpu *v;
1355 kvm_for_each_vcpu(r, v, vcpu->kvm)
1356 if (v == vcpu)
1357 data = r;
1358 break;
1359 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001360 case HV_X64_MSR_EOI:
1361 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1362 case HV_X64_MSR_ICR:
1363 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1364 case HV_X64_MSR_TPR:
1365 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001366 default:
1367 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1368 return 1;
1369 }
1370 *pdata = data;
1371 return 0;
1372}
1373
Carsten Otte15c4a642007-10-30 18:44:17 +01001374int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1375{
1376 u64 data;
1377
1378 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001379 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001380 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001381 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001382 case MSR_IA32_DEBUGCTLMSR:
1383 case MSR_IA32_LASTBRANCHFROMIP:
1384 case MSR_IA32_LASTBRANCHTOIP:
1385 case MSR_IA32_LASTINTFROMIP:
1386 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301387 case MSR_K8_SYSCFG:
1388 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001389 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301390 case MSR_P6_PERFCTR0:
1391 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301392 case MSR_P6_EVNTSEL0:
1393 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301394 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301395 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001396 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001397 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001398 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001399 data = 0;
1400 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001401 case MSR_MTRRcap:
1402 data = 0x500 | KVM_NR_VAR_MTRR;
1403 break;
1404 case 0x200 ... 0x2ff:
1405 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001406 case 0xcd: /* fsb frequency */
1407 data = 3;
1408 break;
1409 case MSR_IA32_APICBASE:
1410 data = kvm_get_apic_base(vcpu);
1411 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001412 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1413 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1414 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001415 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001416 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001417 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001418 case MSR_IA32_PERF_STATUS:
1419 /* TSC increment by tick */
1420 data = 1000ULL;
1421 /* CPU multiplier */
1422 data |= (((uint64_t)4ULL) << 40);
1423 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001424 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001425 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001426 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001427 case MSR_KVM_WALL_CLOCK:
1428 data = vcpu->kvm->arch.wall_clock;
1429 break;
1430 case MSR_KVM_SYSTEM_TIME:
1431 data = vcpu->arch.time;
1432 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001433 case MSR_IA32_P5_MC_ADDR:
1434 case MSR_IA32_P5_MC_TYPE:
1435 case MSR_IA32_MCG_CAP:
1436 case MSR_IA32_MCG_CTL:
1437 case MSR_IA32_MCG_STATUS:
1438 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1439 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001440 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1441 if (kvm_hv_msr_partition_wide(msr)) {
1442 int r;
1443 mutex_lock(&vcpu->kvm->lock);
1444 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1445 mutex_unlock(&vcpu->kvm->lock);
1446 return r;
1447 } else
1448 return get_msr_hyperv(vcpu, msr, pdata);
1449 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001450 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001451 if (!ignore_msrs) {
1452 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1453 return 1;
1454 } else {
1455 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1456 data = 0;
1457 }
1458 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001459 }
1460 *pdata = data;
1461 return 0;
1462}
1463EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1464
Carsten Otte313a3dc2007-10-11 19:16:52 +02001465/*
1466 * Read or write a bunch of msrs. All parameters are kernel addresses.
1467 *
1468 * @return number of msrs set successfully.
1469 */
1470static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1471 struct kvm_msr_entry *entries,
1472 int (*do_msr)(struct kvm_vcpu *vcpu,
1473 unsigned index, u64 *data))
1474{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001475 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001476
1477 vcpu_load(vcpu);
1478
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001479 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001480 for (i = 0; i < msrs->nmsrs; ++i)
1481 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1482 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001483 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001484
1485 vcpu_put(vcpu);
1486
1487 return i;
1488}
1489
1490/*
1491 * Read or write a bunch of msrs. Parameters are user addresses.
1492 *
1493 * @return number of msrs set successfully.
1494 */
1495static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1496 int (*do_msr)(struct kvm_vcpu *vcpu,
1497 unsigned index, u64 *data),
1498 int writeback)
1499{
1500 struct kvm_msrs msrs;
1501 struct kvm_msr_entry *entries;
1502 int r, n;
1503 unsigned size;
1504
1505 r = -EFAULT;
1506 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1507 goto out;
1508
1509 r = -E2BIG;
1510 if (msrs.nmsrs >= MAX_IO_MSRS)
1511 goto out;
1512
1513 r = -ENOMEM;
1514 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1515 entries = vmalloc(size);
1516 if (!entries)
1517 goto out;
1518
1519 r = -EFAULT;
1520 if (copy_from_user(entries, user_msrs->entries, size))
1521 goto out_free;
1522
1523 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1524 if (r < 0)
1525 goto out_free;
1526
1527 r = -EFAULT;
1528 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1529 goto out_free;
1530
1531 r = n;
1532
1533out_free:
1534 vfree(entries);
1535out:
1536 return r;
1537}
1538
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001539int kvm_dev_ioctl_check_extension(long ext)
1540{
1541 int r;
1542
1543 switch (ext) {
1544 case KVM_CAP_IRQCHIP:
1545 case KVM_CAP_HLT:
1546 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001547 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001548 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001549 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001550 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001551 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001552 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001553 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001554 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001555 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001556 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001557 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001558 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001559 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001560 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001561 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001562 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001563 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001564 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001565 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001566 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001567 case KVM_CAP_HYPERV_SPIN:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001568 r = 1;
1569 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001570 case KVM_CAP_COALESCED_MMIO:
1571 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1572 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001573 case KVM_CAP_VAPIC:
1574 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1575 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001576 case KVM_CAP_NR_VCPUS:
1577 r = KVM_MAX_VCPUS;
1578 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001579 case KVM_CAP_NR_MEMSLOTS:
1580 r = KVM_MEMORY_SLOTS;
1581 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001582 case KVM_CAP_PV_MMU: /* obsolete */
1583 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001584 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001585 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001586 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001587 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001588 case KVM_CAP_MCE:
1589 r = KVM_MAX_MCE_BANKS;
1590 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001591 default:
1592 r = 0;
1593 break;
1594 }
1595 return r;
1596
1597}
1598
Carsten Otte043405e2007-10-10 17:16:19 +02001599long kvm_arch_dev_ioctl(struct file *filp,
1600 unsigned int ioctl, unsigned long arg)
1601{
1602 void __user *argp = (void __user *)arg;
1603 long r;
1604
1605 switch (ioctl) {
1606 case KVM_GET_MSR_INDEX_LIST: {
1607 struct kvm_msr_list __user *user_msr_list = argp;
1608 struct kvm_msr_list msr_list;
1609 unsigned n;
1610
1611 r = -EFAULT;
1612 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1613 goto out;
1614 n = msr_list.nmsrs;
1615 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1616 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1617 goto out;
1618 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001619 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001620 goto out;
1621 r = -EFAULT;
1622 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1623 num_msrs_to_save * sizeof(u32)))
1624 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001625 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001626 &emulated_msrs,
1627 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1628 goto out;
1629 r = 0;
1630 break;
1631 }
Avi Kivity674eea02008-02-11 18:37:23 +02001632 case KVM_GET_SUPPORTED_CPUID: {
1633 struct kvm_cpuid2 __user *cpuid_arg = argp;
1634 struct kvm_cpuid2 cpuid;
1635
1636 r = -EFAULT;
1637 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1638 goto out;
1639 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001640 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001641 if (r)
1642 goto out;
1643
1644 r = -EFAULT;
1645 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1646 goto out;
1647 r = 0;
1648 break;
1649 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001650 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1651 u64 mce_cap;
1652
1653 mce_cap = KVM_MCE_CAP_SUPPORTED;
1654 r = -EFAULT;
1655 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1656 goto out;
1657 r = 0;
1658 break;
1659 }
Carsten Otte043405e2007-10-10 17:16:19 +02001660 default:
1661 r = -EINVAL;
1662 }
1663out:
1664 return r;
1665}
1666
Carsten Otte313a3dc2007-10-11 19:16:52 +02001667void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1668{
1669 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001670 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1671 unsigned long khz = cpufreq_quick_get(cpu);
1672 if (!khz)
1673 khz = tsc_khz;
1674 per_cpu(cpu_tsc_khz, cpu) = khz;
1675 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001676 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001677}
1678
1679void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1680{
Amit Shah9327fd12007-11-15 18:38:46 +02001681 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001682 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001683}
1684
Dan Kenigsberg07716712007-11-21 17:10:04 +02001685static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001686{
Avi Kivitye286e862009-05-03 18:50:55 +03001687 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001688
Avi Kivitye286e862009-05-03 18:50:55 +03001689 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001690 return efer & EFER_NX;
1691}
1692
1693static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1694{
1695 int i;
1696 struct kvm_cpuid_entry2 *e, *entry;
1697
Carsten Otte313a3dc2007-10-11 19:16:52 +02001698 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001699 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1700 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001701 if (e->function == 0x80000001) {
1702 entry = e;
1703 break;
1704 }
1705 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001706 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001707 entry->edx &= ~(1 << 20);
1708 printk(KERN_INFO "kvm: guest NX capability removed\n");
1709 }
1710}
1711
Dan Kenigsberg07716712007-11-21 17:10:04 +02001712/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001713static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1714 struct kvm_cpuid *cpuid,
1715 struct kvm_cpuid_entry __user *entries)
1716{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001717 int r, i;
1718 struct kvm_cpuid_entry *cpuid_entries;
1719
1720 r = -E2BIG;
1721 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1722 goto out;
1723 r = -ENOMEM;
1724 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1725 if (!cpuid_entries)
1726 goto out;
1727 r = -EFAULT;
1728 if (copy_from_user(cpuid_entries, entries,
1729 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1730 goto out_free;
1731 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001732 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1733 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1734 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1735 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1736 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1737 vcpu->arch.cpuid_entries[i].index = 0;
1738 vcpu->arch.cpuid_entries[i].flags = 0;
1739 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1740 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1741 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001742 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001743 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001744 cpuid_fix_nx_cap(vcpu);
1745 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001746 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001747 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001748
1749out_free:
1750 vfree(cpuid_entries);
1751out:
1752 return r;
1753}
1754
1755static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001756 struct kvm_cpuid2 *cpuid,
1757 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001758{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001759 int r;
1760
1761 r = -E2BIG;
1762 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1763 goto out;
1764 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001765 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001766 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001767 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001768 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001769 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001770 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001771 return 0;
1772
1773out:
1774 return r;
1775}
1776
Dan Kenigsberg07716712007-11-21 17:10:04 +02001777static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001778 struct kvm_cpuid2 *cpuid,
1779 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001780{
1781 int r;
1782
1783 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001784 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001785 goto out;
1786 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001787 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001788 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001789 goto out;
1790 return 0;
1791
1792out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001793 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001794 return r;
1795}
1796
Dan Kenigsberg07716712007-11-21 17:10:04 +02001797static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001798 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001799{
1800 entry->function = function;
1801 entry->index = index;
1802 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001803 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001804 entry->flags = 0;
1805}
1806
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001807#define F(x) bit(X86_FEATURE_##x)
1808
Dan Kenigsberg07716712007-11-21 17:10:04 +02001809static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1810 u32 index, int *nent, int maxnent)
1811{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001812 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001813#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001814 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1815 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001816 unsigned f_lm = F(LM);
1817#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001818 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001819 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001820#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001821 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001822
1823 /* cpuid 1.edx */
1824 const u32 kvm_supported_word0_x86_features =
1825 F(FPU) | F(VME) | F(DE) | F(PSE) |
1826 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1827 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1828 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1829 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1830 0 /* Reserved, DS, ACPI */ | F(MMX) |
1831 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1832 0 /* HTT, TM, Reserved, PBE */;
1833 /* cpuid 0x80000001.edx */
1834 const u32 kvm_supported_word1_x86_features =
1835 F(FPU) | F(VME) | F(DE) | F(PSE) |
1836 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1837 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1838 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1839 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1840 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001841 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001842 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1843 /* cpuid 1.ecx */
1844 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001845 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1846 0 /* DS-CPL, VMX, SMX, EST */ |
1847 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1848 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1849 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001850 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001851 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001852 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001853 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001854 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1855 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1856 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1857 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001858
Amit Shah19355472009-01-14 16:56:00 +00001859 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001860 get_cpu();
1861 do_cpuid_1_ent(entry, function, index);
1862 ++*nent;
1863
1864 switch (function) {
1865 case 0:
1866 entry->eax = min(entry->eax, (u32)0xb);
1867 break;
1868 case 1:
1869 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001870 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001871 /* we support x2apic emulation even if host does not support
1872 * it since we emulate x2apic in software */
1873 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001874 break;
1875 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1876 * may return different values. This forces us to get_cpu() before
1877 * issuing the first command, and also to emulate this annoying behavior
1878 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1879 case 2: {
1880 int t, times = entry->eax & 0xff;
1881
1882 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001883 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001884 for (t = 1; t < times && *nent < maxnent; ++t) {
1885 do_cpuid_1_ent(&entry[t], function, 0);
1886 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1887 ++*nent;
1888 }
1889 break;
1890 }
1891 /* function 4 and 0xb have additional index. */
1892 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001893 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001894
1895 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1896 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001897 for (i = 1; *nent < maxnent; ++i) {
1898 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001899 if (!cache_type)
1900 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001901 do_cpuid_1_ent(&entry[i], function, i);
1902 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001903 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1904 ++*nent;
1905 }
1906 break;
1907 }
1908 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001909 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001910
1911 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1912 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001913 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001914 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001915 if (!level_type)
1916 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001917 do_cpuid_1_ent(&entry[i], function, i);
1918 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001919 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1920 ++*nent;
1921 }
1922 break;
1923 }
1924 case 0x80000000:
1925 entry->eax = min(entry->eax, 0x8000001a);
1926 break;
1927 case 0x80000001:
1928 entry->edx &= kvm_supported_word1_x86_features;
1929 entry->ecx &= kvm_supported_word6_x86_features;
1930 break;
1931 }
1932 put_cpu();
1933}
1934
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001935#undef F
1936
Avi Kivity674eea02008-02-11 18:37:23 +02001937static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001938 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001939{
1940 struct kvm_cpuid_entry2 *cpuid_entries;
1941 int limit, nent = 0, r = -E2BIG;
1942 u32 func;
1943
1944 if (cpuid->nent < 1)
1945 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001946 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1947 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001948 r = -ENOMEM;
1949 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1950 if (!cpuid_entries)
1951 goto out;
1952
1953 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1954 limit = cpuid_entries[0].eax;
1955 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1956 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001957 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001958 r = -E2BIG;
1959 if (nent >= cpuid->nent)
1960 goto out_free;
1961
1962 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1963 limit = cpuid_entries[nent - 1].eax;
1964 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1965 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001966 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001967 r = -E2BIG;
1968 if (nent >= cpuid->nent)
1969 goto out_free;
1970
Dan Kenigsberg07716712007-11-21 17:10:04 +02001971 r = -EFAULT;
1972 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001973 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001974 goto out_free;
1975 cpuid->nent = nent;
1976 r = 0;
1977
1978out_free:
1979 vfree(cpuid_entries);
1980out:
1981 return r;
1982}
1983
Carsten Otte313a3dc2007-10-11 19:16:52 +02001984static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1985 struct kvm_lapic_state *s)
1986{
1987 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001988 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001989 vcpu_put(vcpu);
1990
1991 return 0;
1992}
1993
1994static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1995 struct kvm_lapic_state *s)
1996{
1997 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001998 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001999 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002000 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002001 vcpu_put(vcpu);
2002
2003 return 0;
2004}
2005
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002006static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2007 struct kvm_interrupt *irq)
2008{
2009 if (irq->irq < 0 || irq->irq >= 256)
2010 return -EINVAL;
2011 if (irqchip_in_kernel(vcpu->kvm))
2012 return -ENXIO;
2013 vcpu_load(vcpu);
2014
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002015 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002016
2017 vcpu_put(vcpu);
2018
2019 return 0;
2020}
2021
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002022static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2023{
2024 vcpu_load(vcpu);
2025 kvm_inject_nmi(vcpu);
2026 vcpu_put(vcpu);
2027
2028 return 0;
2029}
2030
Avi Kivityb209749f2007-10-22 16:50:39 +02002031static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2032 struct kvm_tpr_access_ctl *tac)
2033{
2034 if (tac->flags)
2035 return -EINVAL;
2036 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2037 return 0;
2038}
2039
Huang Ying890ca9a2009-05-11 16:48:15 +08002040static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2041 u64 mcg_cap)
2042{
2043 int r;
2044 unsigned bank_num = mcg_cap & 0xff, bank;
2045
2046 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002047 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002048 goto out;
2049 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2050 goto out;
2051 r = 0;
2052 vcpu->arch.mcg_cap = mcg_cap;
2053 /* Init IA32_MCG_CTL to all 1s */
2054 if (mcg_cap & MCG_CTL_P)
2055 vcpu->arch.mcg_ctl = ~(u64)0;
2056 /* Init IA32_MCi_CTL to all 1s */
2057 for (bank = 0; bank < bank_num; bank++)
2058 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2059out:
2060 return r;
2061}
2062
2063static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2064 struct kvm_x86_mce *mce)
2065{
2066 u64 mcg_cap = vcpu->arch.mcg_cap;
2067 unsigned bank_num = mcg_cap & 0xff;
2068 u64 *banks = vcpu->arch.mce_banks;
2069
2070 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2071 return -EINVAL;
2072 /*
2073 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2074 * reporting is disabled
2075 */
2076 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2077 vcpu->arch.mcg_ctl != ~(u64)0)
2078 return 0;
2079 banks += 4 * mce->bank;
2080 /*
2081 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2082 * reporting is disabled for the bank
2083 */
2084 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2085 return 0;
2086 if (mce->status & MCI_STATUS_UC) {
2087 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002088 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002089 printk(KERN_DEBUG "kvm: set_mce: "
2090 "injects mce exception while "
2091 "previous one is in progress!\n");
2092 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2093 return 0;
2094 }
2095 if (banks[1] & MCI_STATUS_VAL)
2096 mce->status |= MCI_STATUS_OVER;
2097 banks[2] = mce->addr;
2098 banks[3] = mce->misc;
2099 vcpu->arch.mcg_status = mce->mcg_status;
2100 banks[1] = mce->status;
2101 kvm_queue_exception(vcpu, MC_VECTOR);
2102 } else if (!(banks[1] & MCI_STATUS_VAL)
2103 || !(banks[1] & MCI_STATUS_UC)) {
2104 if (banks[1] & MCI_STATUS_VAL)
2105 mce->status |= MCI_STATUS_OVER;
2106 banks[2] = mce->addr;
2107 banks[3] = mce->misc;
2108 banks[1] = mce->status;
2109 } else
2110 banks[1] |= MCI_STATUS_OVER;
2111 return 0;
2112}
2113
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002114static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2115 struct kvm_vcpu_events *events)
2116{
2117 vcpu_load(vcpu);
2118
2119 events->exception.injected = vcpu->arch.exception.pending;
2120 events->exception.nr = vcpu->arch.exception.nr;
2121 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2122 events->exception.error_code = vcpu->arch.exception.error_code;
2123
2124 events->interrupt.injected = vcpu->arch.interrupt.pending;
2125 events->interrupt.nr = vcpu->arch.interrupt.nr;
2126 events->interrupt.soft = vcpu->arch.interrupt.soft;
2127
2128 events->nmi.injected = vcpu->arch.nmi_injected;
2129 events->nmi.pending = vcpu->arch.nmi_pending;
2130 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2131
2132 events->sipi_vector = vcpu->arch.sipi_vector;
2133
Jan Kiszkadab4b912009-12-06 18:24:15 +01002134 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
2135 | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002136
2137 vcpu_put(vcpu);
2138}
2139
2140static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2141 struct kvm_vcpu_events *events)
2142{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002143 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2144 | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002145 return -EINVAL;
2146
2147 vcpu_load(vcpu);
2148
2149 vcpu->arch.exception.pending = events->exception.injected;
2150 vcpu->arch.exception.nr = events->exception.nr;
2151 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2152 vcpu->arch.exception.error_code = events->exception.error_code;
2153
2154 vcpu->arch.interrupt.pending = events->interrupt.injected;
2155 vcpu->arch.interrupt.nr = events->interrupt.nr;
2156 vcpu->arch.interrupt.soft = events->interrupt.soft;
2157 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2158 kvm_pic_clear_isr_ack(vcpu->kvm);
2159
2160 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002161 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2162 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002163 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2164
Jan Kiszkadab4b912009-12-06 18:24:15 +01002165 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2166 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002167
2168 vcpu_put(vcpu);
2169
2170 return 0;
2171}
2172
Carsten Otte313a3dc2007-10-11 19:16:52 +02002173long kvm_arch_vcpu_ioctl(struct file *filp,
2174 unsigned int ioctl, unsigned long arg)
2175{
2176 struct kvm_vcpu *vcpu = filp->private_data;
2177 void __user *argp = (void __user *)arg;
2178 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002179 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002180
2181 switch (ioctl) {
2182 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002183 r = -EINVAL;
2184 if (!vcpu->arch.apic)
2185 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002186 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002187
Dave Hansenb772ff32008-08-11 10:01:47 -07002188 r = -ENOMEM;
2189 if (!lapic)
2190 goto out;
2191 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002192 if (r)
2193 goto out;
2194 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002195 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002196 goto out;
2197 r = 0;
2198 break;
2199 }
2200 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002201 r = -EINVAL;
2202 if (!vcpu->arch.apic)
2203 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002204 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2205 r = -ENOMEM;
2206 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002207 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002208 r = -EFAULT;
2209 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2210 goto out;
2211 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002212 if (r)
2213 goto out;
2214 r = 0;
2215 break;
2216 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002217 case KVM_INTERRUPT: {
2218 struct kvm_interrupt irq;
2219
2220 r = -EFAULT;
2221 if (copy_from_user(&irq, argp, sizeof irq))
2222 goto out;
2223 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2224 if (r)
2225 goto out;
2226 r = 0;
2227 break;
2228 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002229 case KVM_NMI: {
2230 r = kvm_vcpu_ioctl_nmi(vcpu);
2231 if (r)
2232 goto out;
2233 r = 0;
2234 break;
2235 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002236 case KVM_SET_CPUID: {
2237 struct kvm_cpuid __user *cpuid_arg = argp;
2238 struct kvm_cpuid cpuid;
2239
2240 r = -EFAULT;
2241 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2242 goto out;
2243 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2244 if (r)
2245 goto out;
2246 break;
2247 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002248 case KVM_SET_CPUID2: {
2249 struct kvm_cpuid2 __user *cpuid_arg = argp;
2250 struct kvm_cpuid2 cpuid;
2251
2252 r = -EFAULT;
2253 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2254 goto out;
2255 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002256 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002257 if (r)
2258 goto out;
2259 break;
2260 }
2261 case KVM_GET_CPUID2: {
2262 struct kvm_cpuid2 __user *cpuid_arg = argp;
2263 struct kvm_cpuid2 cpuid;
2264
2265 r = -EFAULT;
2266 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2267 goto out;
2268 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002269 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002270 if (r)
2271 goto out;
2272 r = -EFAULT;
2273 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2274 goto out;
2275 r = 0;
2276 break;
2277 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002278 case KVM_GET_MSRS:
2279 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2280 break;
2281 case KVM_SET_MSRS:
2282 r = msr_io(vcpu, argp, do_set_msr, 0);
2283 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002284 case KVM_TPR_ACCESS_REPORTING: {
2285 struct kvm_tpr_access_ctl tac;
2286
2287 r = -EFAULT;
2288 if (copy_from_user(&tac, argp, sizeof tac))
2289 goto out;
2290 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2291 if (r)
2292 goto out;
2293 r = -EFAULT;
2294 if (copy_to_user(argp, &tac, sizeof tac))
2295 goto out;
2296 r = 0;
2297 break;
2298 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002299 case KVM_SET_VAPIC_ADDR: {
2300 struct kvm_vapic_addr va;
2301
2302 r = -EINVAL;
2303 if (!irqchip_in_kernel(vcpu->kvm))
2304 goto out;
2305 r = -EFAULT;
2306 if (copy_from_user(&va, argp, sizeof va))
2307 goto out;
2308 r = 0;
2309 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2310 break;
2311 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002312 case KVM_X86_SETUP_MCE: {
2313 u64 mcg_cap;
2314
2315 r = -EFAULT;
2316 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2317 goto out;
2318 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2319 break;
2320 }
2321 case KVM_X86_SET_MCE: {
2322 struct kvm_x86_mce mce;
2323
2324 r = -EFAULT;
2325 if (copy_from_user(&mce, argp, sizeof mce))
2326 goto out;
2327 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2328 break;
2329 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002330 case KVM_GET_VCPU_EVENTS: {
2331 struct kvm_vcpu_events events;
2332
2333 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2334
2335 r = -EFAULT;
2336 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2337 break;
2338 r = 0;
2339 break;
2340 }
2341 case KVM_SET_VCPU_EVENTS: {
2342 struct kvm_vcpu_events events;
2343
2344 r = -EFAULT;
2345 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2346 break;
2347
2348 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2349 break;
2350 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002351 default:
2352 r = -EINVAL;
2353 }
2354out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002355 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002356 return r;
2357}
2358
Carsten Otte1fe779f2007-10-29 16:08:35 +01002359static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2360{
2361 int ret;
2362
2363 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2364 return -1;
2365 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2366 return ret;
2367}
2368
Sheng Yangb927a3c2009-07-21 10:42:48 +08002369static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2370 u64 ident_addr)
2371{
2372 kvm->arch.ept_identity_map_addr = ident_addr;
2373 return 0;
2374}
2375
Carsten Otte1fe779f2007-10-29 16:08:35 +01002376static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2377 u32 kvm_nr_mmu_pages)
2378{
2379 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2380 return -EINVAL;
2381
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002382 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002383 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002384
2385 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002386 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002387
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002388 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002389 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002390 return 0;
2391}
2392
2393static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2394{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002395 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002396}
2397
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002398gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2399{
2400 int i;
2401 struct kvm_mem_alias *alias;
2402 struct kvm_mem_aliases *aliases;
2403
2404 aliases = rcu_dereference(kvm->arch.aliases);
2405
2406 for (i = 0; i < aliases->naliases; ++i) {
2407 alias = &aliases->aliases[i];
2408 if (alias->flags & KVM_ALIAS_INVALID)
2409 continue;
2410 if (gfn >= alias->base_gfn
2411 && gfn < alias->base_gfn + alias->npages)
2412 return alias->target_gfn + gfn - alias->base_gfn;
2413 }
2414 return gfn;
2415}
2416
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002417gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2418{
2419 int i;
2420 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002421 struct kvm_mem_aliases *aliases;
2422
2423 aliases = rcu_dereference(kvm->arch.aliases);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002424
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002425 for (i = 0; i < aliases->naliases; ++i) {
2426 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002427 if (gfn >= alias->base_gfn
2428 && gfn < alias->base_gfn + alias->npages)
2429 return alias->target_gfn + gfn - alias->base_gfn;
2430 }
2431 return gfn;
2432}
2433
Carsten Otte1fe779f2007-10-29 16:08:35 +01002434/*
2435 * Set a new alias region. Aliases map a portion of physical memory into
2436 * another portion. This is useful for memory windows, for example the PC
2437 * VGA region.
2438 */
2439static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2440 struct kvm_memory_alias *alias)
2441{
2442 int r, n;
2443 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002444 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002445
2446 r = -EINVAL;
2447 /* General sanity checks */
2448 if (alias->memory_size & (PAGE_SIZE - 1))
2449 goto out;
2450 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2451 goto out;
2452 if (alias->slot >= KVM_ALIAS_SLOTS)
2453 goto out;
2454 if (alias->guest_phys_addr + alias->memory_size
2455 < alias->guest_phys_addr)
2456 goto out;
2457 if (alias->target_phys_addr + alias->memory_size
2458 < alias->target_phys_addr)
2459 goto out;
2460
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002461 r = -ENOMEM;
2462 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2463 if (!aliases)
2464 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002465
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002466 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002467
2468 /* invalidate any gfn reference in case of deletion/shrinking */
2469 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2470 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2471 old_aliases = kvm->arch.aliases;
2472 rcu_assign_pointer(kvm->arch.aliases, aliases);
2473 synchronize_srcu_expedited(&kvm->srcu);
2474 kvm_mmu_zap_all(kvm);
2475 kfree(old_aliases);
2476
2477 r = -ENOMEM;
2478 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2479 if (!aliases)
2480 goto out_unlock;
2481
2482 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002483
2484 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002485 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2486 p->npages = alias->memory_size >> PAGE_SHIFT;
2487 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002488 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002489
2490 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002491 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002492 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002493 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002494
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002495 old_aliases = kvm->arch.aliases;
2496 rcu_assign_pointer(kvm->arch.aliases, aliases);
2497 synchronize_srcu_expedited(&kvm->srcu);
2498 kfree(old_aliases);
2499 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002500
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002501out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002502 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002503out:
2504 return r;
2505}
2506
2507static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2508{
2509 int r;
2510
2511 r = 0;
2512 switch (chip->chip_id) {
2513 case KVM_IRQCHIP_PIC_MASTER:
2514 memcpy(&chip->chip.pic,
2515 &pic_irqchip(kvm)->pics[0],
2516 sizeof(struct kvm_pic_state));
2517 break;
2518 case KVM_IRQCHIP_PIC_SLAVE:
2519 memcpy(&chip->chip.pic,
2520 &pic_irqchip(kvm)->pics[1],
2521 sizeof(struct kvm_pic_state));
2522 break;
2523 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002524 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002525 break;
2526 default:
2527 r = -EINVAL;
2528 break;
2529 }
2530 return r;
2531}
2532
2533static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2534{
2535 int r;
2536
2537 r = 0;
2538 switch (chip->chip_id) {
2539 case KVM_IRQCHIP_PIC_MASTER:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002540 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002541 memcpy(&pic_irqchip(kvm)->pics[0],
2542 &chip->chip.pic,
2543 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002544 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002545 break;
2546 case KVM_IRQCHIP_PIC_SLAVE:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002547 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002548 memcpy(&pic_irqchip(kvm)->pics[1],
2549 &chip->chip.pic,
2550 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002551 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002552 break;
2553 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002554 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002555 break;
2556 default:
2557 r = -EINVAL;
2558 break;
2559 }
2560 kvm_pic_update_irq(pic_irqchip(kvm));
2561 return r;
2562}
2563
Sheng Yange0f63cb2008-03-04 00:50:59 +08002564static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2565{
2566 int r = 0;
2567
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002568 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002569 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002570 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002571 return r;
2572}
2573
2574static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2575{
2576 int r = 0;
2577
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002578 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002579 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002580 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2581 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2582 return r;
2583}
2584
2585static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2586{
2587 int r = 0;
2588
2589 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2590 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2591 sizeof(ps->channels));
2592 ps->flags = kvm->arch.vpit->pit_state.flags;
2593 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2594 return r;
2595}
2596
2597static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2598{
2599 int r = 0, start = 0;
2600 u32 prev_legacy, cur_legacy;
2601 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2602 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2603 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2604 if (!prev_legacy && cur_legacy)
2605 start = 1;
2606 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2607 sizeof(kvm->arch.vpit->pit_state.channels));
2608 kvm->arch.vpit->pit_state.flags = ps->flags;
2609 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002610 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002611 return r;
2612}
2613
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002614static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2615 struct kvm_reinject_control *control)
2616{
2617 if (!kvm->arch.vpit)
2618 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002619 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002620 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002621 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002622 return 0;
2623}
2624
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002625/*
2626 * Get (and clear) the dirty memory log for a memory slot.
2627 */
2628int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2629 struct kvm_dirty_log *log)
2630{
Marcelo Tosattib050b012009-12-23 14:35:22 -02002631 int r, n, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002632 struct kvm_memory_slot *memslot;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002633 unsigned long is_dirty = 0;
2634 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002635
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002636 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002637
Marcelo Tosattib050b012009-12-23 14:35:22 -02002638 r = -EINVAL;
2639 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002640 goto out;
2641
Marcelo Tosattib050b012009-12-23 14:35:22 -02002642 memslot = &kvm->memslots->memslots[log->slot];
2643 r = -ENOENT;
2644 if (!memslot->dirty_bitmap)
2645 goto out;
2646
2647 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2648
2649 r = -ENOMEM;
2650 dirty_bitmap = vmalloc(n);
2651 if (!dirty_bitmap)
2652 goto out;
2653 memset(dirty_bitmap, 0, n);
2654
2655 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2656 is_dirty = memslot->dirty_bitmap[i];
2657
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002658 /* If nothing is dirty, don't bother messing with page tables. */
2659 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002660 struct kvm_memslots *slots, *old_slots;
2661
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002662 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002663 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002664 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002665
2666 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2667 if (!slots)
2668 goto out_free;
2669
2670 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2671 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2672
2673 old_slots = kvm->memslots;
2674 rcu_assign_pointer(kvm->memslots, slots);
2675 synchronize_srcu_expedited(&kvm->srcu);
2676 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2677 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002678 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002679
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002680 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002681 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2682 r = -EFAULT;
2683out_free:
2684 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002685out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002686 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002687 return r;
2688}
2689
Carsten Otte1fe779f2007-10-29 16:08:35 +01002690long kvm_arch_vm_ioctl(struct file *filp,
2691 unsigned int ioctl, unsigned long arg)
2692{
2693 struct kvm *kvm = filp->private_data;
2694 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002695 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002696 /*
2697 * This union makes it completely explicit to gcc-3.x
2698 * that these two variables' stack usage should be
2699 * combined, not added together.
2700 */
2701 union {
2702 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002703 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002704 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002705 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002706 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002707
2708 switch (ioctl) {
2709 case KVM_SET_TSS_ADDR:
2710 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2711 if (r < 0)
2712 goto out;
2713 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002714 case KVM_SET_IDENTITY_MAP_ADDR: {
2715 u64 ident_addr;
2716
2717 r = -EFAULT;
2718 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2719 goto out;
2720 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2721 if (r < 0)
2722 goto out;
2723 break;
2724 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002725 case KVM_SET_MEMORY_REGION: {
2726 struct kvm_memory_region kvm_mem;
2727 struct kvm_userspace_memory_region kvm_userspace_mem;
2728
2729 r = -EFAULT;
2730 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2731 goto out;
2732 kvm_userspace_mem.slot = kvm_mem.slot;
2733 kvm_userspace_mem.flags = kvm_mem.flags;
2734 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2735 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2736 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2737 if (r)
2738 goto out;
2739 break;
2740 }
2741 case KVM_SET_NR_MMU_PAGES:
2742 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2743 if (r)
2744 goto out;
2745 break;
2746 case KVM_GET_NR_MMU_PAGES:
2747 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2748 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002749 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002750 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002751 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002752 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002753 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002754 if (r)
2755 goto out;
2756 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002757 case KVM_CREATE_IRQCHIP: {
2758 struct kvm_pic *vpic;
2759
2760 mutex_lock(&kvm->lock);
2761 r = -EEXIST;
2762 if (kvm->arch.vpic)
2763 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002764 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002765 vpic = kvm_create_pic(kvm);
2766 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002767 r = kvm_ioapic_init(kvm);
2768 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002769 kfree(vpic);
2770 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002771 }
2772 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002773 goto create_irqchip_unlock;
2774 smp_wmb();
2775 kvm->arch.vpic = vpic;
2776 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002777 r = kvm_setup_default_irq_routing(kvm);
2778 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002779 mutex_lock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002780 kfree(kvm->arch.vpic);
2781 kfree(kvm->arch.vioapic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002782 kvm->arch.vpic = NULL;
2783 kvm->arch.vioapic = NULL;
2784 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002785 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002786 create_irqchip_unlock:
2787 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002788 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002789 }
Sheng Yang78376992008-01-28 05:10:22 +08002790 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002791 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2792 goto create_pit;
2793 case KVM_CREATE_PIT2:
2794 r = -EFAULT;
2795 if (copy_from_user(&u.pit_config, argp,
2796 sizeof(struct kvm_pit_config)))
2797 goto out;
2798 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002799 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002800 r = -EEXIST;
2801 if (kvm->arch.vpit)
2802 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002803 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002804 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002805 if (kvm->arch.vpit)
2806 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002807 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002808 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002809 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002810 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002811 case KVM_IRQ_LINE: {
2812 struct kvm_irq_level irq_event;
2813
2814 r = -EFAULT;
2815 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2816 goto out;
2817 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002818 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002819 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2820 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002821 if (ioctl == KVM_IRQ_LINE_STATUS) {
2822 irq_event.status = status;
2823 if (copy_to_user(argp, &irq_event,
2824 sizeof irq_event))
2825 goto out;
2826 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002827 r = 0;
2828 }
2829 break;
2830 }
2831 case KVM_GET_IRQCHIP: {
2832 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002833 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002834
Dave Hansenf0d66272008-08-11 10:01:45 -07002835 r = -ENOMEM;
2836 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002837 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002838 r = -EFAULT;
2839 if (copy_from_user(chip, argp, sizeof *chip))
2840 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002841 r = -ENXIO;
2842 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002843 goto get_irqchip_out;
2844 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2845 if (r)
2846 goto get_irqchip_out;
2847 r = -EFAULT;
2848 if (copy_to_user(argp, chip, sizeof *chip))
2849 goto get_irqchip_out;
2850 r = 0;
2851 get_irqchip_out:
2852 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002853 if (r)
2854 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002855 break;
2856 }
2857 case KVM_SET_IRQCHIP: {
2858 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002859 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002860
Dave Hansenf0d66272008-08-11 10:01:45 -07002861 r = -ENOMEM;
2862 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002863 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002864 r = -EFAULT;
2865 if (copy_from_user(chip, argp, sizeof *chip))
2866 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002867 r = -ENXIO;
2868 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002869 goto set_irqchip_out;
2870 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2871 if (r)
2872 goto set_irqchip_out;
2873 r = 0;
2874 set_irqchip_out:
2875 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002876 if (r)
2877 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002878 break;
2879 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002880 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002881 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002882 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002883 goto out;
2884 r = -ENXIO;
2885 if (!kvm->arch.vpit)
2886 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002887 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002888 if (r)
2889 goto out;
2890 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002891 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002892 goto out;
2893 r = 0;
2894 break;
2895 }
2896 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002897 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002898 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002899 goto out;
2900 r = -ENXIO;
2901 if (!kvm->arch.vpit)
2902 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002903 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002904 if (r)
2905 goto out;
2906 r = 0;
2907 break;
2908 }
Beth Kone9f42752009-07-07 11:50:38 -04002909 case KVM_GET_PIT2: {
2910 r = -ENXIO;
2911 if (!kvm->arch.vpit)
2912 goto out;
2913 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2914 if (r)
2915 goto out;
2916 r = -EFAULT;
2917 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2918 goto out;
2919 r = 0;
2920 break;
2921 }
2922 case KVM_SET_PIT2: {
2923 r = -EFAULT;
2924 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2925 goto out;
2926 r = -ENXIO;
2927 if (!kvm->arch.vpit)
2928 goto out;
2929 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2930 if (r)
2931 goto out;
2932 r = 0;
2933 break;
2934 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002935 case KVM_REINJECT_CONTROL: {
2936 struct kvm_reinject_control control;
2937 r = -EFAULT;
2938 if (copy_from_user(&control, argp, sizeof(control)))
2939 goto out;
2940 r = kvm_vm_ioctl_reinject(kvm, &control);
2941 if (r)
2942 goto out;
2943 r = 0;
2944 break;
2945 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002946 case KVM_XEN_HVM_CONFIG: {
2947 r = -EFAULT;
2948 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2949 sizeof(struct kvm_xen_hvm_config)))
2950 goto out;
2951 r = -EINVAL;
2952 if (kvm->arch.xen_hvm_config.flags)
2953 goto out;
2954 r = 0;
2955 break;
2956 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002957 case KVM_SET_CLOCK: {
2958 struct timespec now;
2959 struct kvm_clock_data user_ns;
2960 u64 now_ns;
2961 s64 delta;
2962
2963 r = -EFAULT;
2964 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2965 goto out;
2966
2967 r = -EINVAL;
2968 if (user_ns.flags)
2969 goto out;
2970
2971 r = 0;
2972 ktime_get_ts(&now);
2973 now_ns = timespec_to_ns(&now);
2974 delta = user_ns.clock - now_ns;
2975 kvm->arch.kvmclock_offset = delta;
2976 break;
2977 }
2978 case KVM_GET_CLOCK: {
2979 struct timespec now;
2980 struct kvm_clock_data user_ns;
2981 u64 now_ns;
2982
2983 ktime_get_ts(&now);
2984 now_ns = timespec_to_ns(&now);
2985 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2986 user_ns.flags = 0;
2987
2988 r = -EFAULT;
2989 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2990 goto out;
2991 r = 0;
2992 break;
2993 }
2994
Carsten Otte1fe779f2007-10-29 16:08:35 +01002995 default:
2996 ;
2997 }
2998out:
2999 return r;
3000}
3001
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003002static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003003{
3004 u32 dummy[2];
3005 unsigned i, j;
3006
Glauber Costae3267cb2009-10-06 13:24:50 -04003007 /* skip the first msrs in the list. KVM-specific */
3008 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003009 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3010 continue;
3011 if (j < i)
3012 msrs_to_save[j] = msrs_to_save[i];
3013 j++;
3014 }
3015 num_msrs_to_save = j;
3016}
3017
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003018static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3019 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003020{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003021 if (vcpu->arch.apic &&
3022 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3023 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003024
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003025 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003026}
3027
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003028static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003029{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003030 if (vcpu->arch.apic &&
3031 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3032 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003033
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003034 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003035}
3036
Hannes Edercded19f2009-02-21 02:19:13 +01003037static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3038 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003039{
3040 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003041 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003042
3043 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003044 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003045 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003046 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003047 int ret;
3048
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003049 if (gpa == UNMAPPED_GVA) {
3050 r = X86EMUL_PROPAGATE_FAULT;
3051 goto out;
3052 }
Izik Eidus77c20022008-12-29 01:42:19 +02003053 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003054 if (ret < 0) {
3055 r = X86EMUL_UNHANDLEABLE;
3056 goto out;
3057 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003058
Izik Eidus77c20022008-12-29 01:42:19 +02003059 bytes -= toread;
3060 data += toread;
3061 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003062 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003063out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003064 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003065}
Izik Eidus77c20022008-12-29 01:42:19 +02003066
Hannes Edercded19f2009-02-21 02:19:13 +01003067static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
3068 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02003069{
3070 void *data = val;
3071 int r = X86EMUL_CONTINUE;
3072
3073 while (bytes) {
3074 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
3075 unsigned offset = addr & (PAGE_SIZE-1);
3076 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3077 int ret;
3078
3079 if (gpa == UNMAPPED_GVA) {
3080 r = X86EMUL_PROPAGATE_FAULT;
3081 goto out;
3082 }
3083 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3084 if (ret < 0) {
3085 r = X86EMUL_UNHANDLEABLE;
3086 goto out;
3087 }
3088
3089 bytes -= towrite;
3090 data += towrite;
3091 addr += towrite;
3092 }
3093out:
3094 return r;
3095}
3096
Carsten Ottebbd9b642007-10-30 18:44:21 +01003097
Carsten Ottebbd9b642007-10-30 18:44:21 +01003098static int emulator_read_emulated(unsigned long addr,
3099 void *val,
3100 unsigned int bytes,
3101 struct kvm_vcpu *vcpu)
3102{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003103 gpa_t gpa;
3104
3105 if (vcpu->mmio_read_completed) {
3106 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003107 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3108 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003109 vcpu->mmio_read_completed = 0;
3110 return X86EMUL_CONTINUE;
3111 }
3112
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003113 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003114
3115 /* For APIC access vmexit */
3116 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3117 goto mmio;
3118
Izik Eidus77c20022008-12-29 01:42:19 +02003119 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
3120 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003121 return X86EMUL_CONTINUE;
3122 if (gpa == UNMAPPED_GVA)
3123 return X86EMUL_PROPAGATE_FAULT;
3124
3125mmio:
3126 /*
3127 * Is this MMIO handled locally?
3128 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003129 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3130 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003131 return X86EMUL_CONTINUE;
3132 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003133
3134 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003135
3136 vcpu->mmio_needed = 1;
3137 vcpu->mmio_phys_addr = gpa;
3138 vcpu->mmio_size = bytes;
3139 vcpu->mmio_is_write = 0;
3140
3141 return X86EMUL_UNHANDLEABLE;
3142}
3143
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003144int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003145 const void *val, int bytes)
3146{
3147 int ret;
3148
3149 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3150 if (ret < 0)
3151 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003152 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003153 return 1;
3154}
3155
Carsten Ottebbd9b642007-10-30 18:44:21 +01003156static int emulator_write_emulated_onepage(unsigned long addr,
3157 const void *val,
3158 unsigned int bytes,
3159 struct kvm_vcpu *vcpu)
3160{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003161 gpa_t gpa;
3162
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003163 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003164
3165 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02003166 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003167 return X86EMUL_PROPAGATE_FAULT;
3168 }
3169
3170 /* For APIC access vmexit */
3171 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3172 goto mmio;
3173
3174 if (emulator_write_phys(vcpu, gpa, val, bytes))
3175 return X86EMUL_CONTINUE;
3176
3177mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003178 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003179 /*
3180 * Is this MMIO handled locally?
3181 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003182 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003183 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003184
3185 vcpu->mmio_needed = 1;
3186 vcpu->mmio_phys_addr = gpa;
3187 vcpu->mmio_size = bytes;
3188 vcpu->mmio_is_write = 1;
3189 memcpy(vcpu->mmio_data, val, bytes);
3190
3191 return X86EMUL_CONTINUE;
3192}
3193
3194int emulator_write_emulated(unsigned long addr,
3195 const void *val,
3196 unsigned int bytes,
3197 struct kvm_vcpu *vcpu)
3198{
3199 /* Crossing a page boundary? */
3200 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3201 int rc, now;
3202
3203 now = -addr & ~PAGE_MASK;
3204 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
3205 if (rc != X86EMUL_CONTINUE)
3206 return rc;
3207 addr += now;
3208 val += now;
3209 bytes -= now;
3210 }
3211 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
3212}
3213EXPORT_SYMBOL_GPL(emulator_write_emulated);
3214
3215static int emulator_cmpxchg_emulated(unsigned long addr,
3216 const void *old,
3217 const void *new,
3218 unsigned int bytes,
3219 struct kvm_vcpu *vcpu)
3220{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02003221 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003222#ifndef CONFIG_X86_64
3223 /* guests cmpxchg8b have to be emulated atomically */
3224 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003225 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003226 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003227 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003228 u64 val;
3229
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003230 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
3231
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003232 if (gpa == UNMAPPED_GVA ||
3233 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3234 goto emul_write;
3235
3236 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3237 goto emul_write;
3238
3239 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003240
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003241 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003242
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003243 kaddr = kmap_atomic(page, KM_USER0);
3244 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
3245 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003246 kvm_release_page_dirty(page);
3247 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003248emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003249#endif
3250
Carsten Ottebbd9b642007-10-30 18:44:21 +01003251 return emulator_write_emulated(addr, new, bytes, vcpu);
3252}
3253
3254static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3255{
3256 return kvm_x86_ops->get_segment_base(vcpu, seg);
3257}
3258
3259int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3260{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003261 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003262 return X86EMUL_CONTINUE;
3263}
3264
3265int emulate_clts(struct kvm_vcpu *vcpu)
3266{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003267 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Carsten Ottebbd9b642007-10-30 18:44:21 +01003268 return X86EMUL_CONTINUE;
3269}
3270
3271int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3272{
3273 struct kvm_vcpu *vcpu = ctxt->vcpu;
3274
3275 switch (dr) {
3276 case 0 ... 3:
3277 *dest = kvm_x86_ops->get_dr(vcpu, dr);
3278 return X86EMUL_CONTINUE;
3279 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003280 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003281 return X86EMUL_UNHANDLEABLE;
3282 }
3283}
3284
3285int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3286{
3287 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
3288 int exception;
3289
3290 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
3291 if (exception) {
3292 /* FIXME: better handling */
3293 return X86EMUL_UNHANDLEABLE;
3294 }
3295 return X86EMUL_CONTINUE;
3296}
3297
3298void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3299{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003300 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003301 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003302 unsigned long rip_linear;
3303
Avi Kivityf76c7102008-06-13 22:45:42 +03003304 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003305 return;
3306
Glauber Costa25be4602008-06-10 10:46:53 -03003307 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3308
Izik Eidus77c20022008-12-29 01:42:19 +02003309 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003310
3311 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3312 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003313}
3314EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3315
Harvey Harrison14af3f32008-02-19 10:25:50 -08003316static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02003317 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003318 .read_emulated = emulator_read_emulated,
3319 .write_emulated = emulator_write_emulated,
3320 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3321};
3322
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003323static void cache_all_regs(struct kvm_vcpu *vcpu)
3324{
3325 kvm_register_read(vcpu, VCPU_REGS_RAX);
3326 kvm_register_read(vcpu, VCPU_REGS_RSP);
3327 kvm_register_read(vcpu, VCPU_REGS_RIP);
3328 vcpu->arch.regs_dirty = ~0;
3329}
3330
Carsten Ottebbd9b642007-10-30 18:44:21 +01003331int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003332 unsigned long cr2,
3333 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003334 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003335{
Glauber Costa310b5d32009-05-12 16:21:06 -04003336 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003337 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003338 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003339
Avi Kivity26eef702008-07-03 14:59:22 +03003340 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003341 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003342 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003343 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003344 * instead of direct ->regs accesses, can save hundred cycles
3345 * on Intel for instructions that don't read/change RSP, for
3346 * for example.
3347 */
3348 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003349
3350 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003351 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003352
Sheng Yang571008d2008-01-02 14:49:22 +08003353 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003354 int cs_db, cs_l;
3355 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3356
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003357 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka91586a32009-10-05 13:07:21 +02003358 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003359 vcpu->arch.emulate_ctxt.mode =
3360 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003361 ? X86EMUL_MODE_REAL : cs_l
3362 ? X86EMUL_MODE_PROT64 : cs_db
3363 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3364
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003365 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003366
Andre Przywara0cb57622009-06-17 15:50:31 +02003367 /* Only allow emulation of specific instructions on #UD
3368 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003369 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003370 if (emulation_type & EMULTYPE_TRAP_UD) {
3371 if (!c->twobyte)
3372 return EMULATE_FAIL;
3373 switch (c->b) {
3374 case 0x01: /* VMMCALL */
3375 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3376 return EMULATE_FAIL;
3377 break;
3378 case 0x34: /* sysenter */
3379 case 0x35: /* sysexit */
3380 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3381 return EMULATE_FAIL;
3382 break;
3383 case 0x05: /* syscall */
3384 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3385 return EMULATE_FAIL;
3386 break;
3387 default:
3388 return EMULATE_FAIL;
3389 }
3390
3391 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3392 return EMULATE_FAIL;
3393 }
Sheng Yang571008d2008-01-02 14:49:22 +08003394
Avi Kivityf2b57562007-11-18 15:17:51 +02003395 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003396 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003397 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003398 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3399 return EMULATE_DONE;
3400 return EMULATE_FAIL;
3401 }
3402 }
3403
Gleb Natapovba8afb62009-04-12 13:36:57 +03003404 if (emulation_type & EMULTYPE_SKIP) {
3405 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3406 return EMULATE_DONE;
3407 }
3408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003409 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003410 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3411
3412 if (r == 0)
3413 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003414
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003415 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003416 return EMULATE_DO_MMIO;
3417
3418 if ((r || vcpu->mmio_is_write) && run) {
3419 run->exit_reason = KVM_EXIT_MMIO;
3420 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3421 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3422 run->mmio.len = vcpu->mmio_size;
3423 run->mmio.is_write = vcpu->mmio_is_write;
3424 }
3425
3426 if (r) {
3427 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3428 return EMULATE_DONE;
3429 if (!vcpu->mmio_needed) {
3430 kvm_report_emulation_failure(vcpu, "mmio");
3431 return EMULATE_FAIL;
3432 }
3433 return EMULATE_DO_MMIO;
3434 }
3435
Jan Kiszka91586a32009-10-05 13:07:21 +02003436 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003437
3438 if (vcpu->mmio_is_write) {
3439 vcpu->mmio_needed = 0;
3440 return EMULATE_DO_MMIO;
3441 }
3442
3443 return EMULATE_DONE;
3444}
3445EXPORT_SYMBOL_GPL(emulate_instruction);
3446
Carsten Ottede7d7892007-10-30 18:44:25 +01003447static int pio_copy_data(struct kvm_vcpu *vcpu)
3448{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003449 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003450 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003451 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003452 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003453
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003454 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3455 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02003456 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01003457 else
Izik Eidus0f346072008-12-29 01:42:20 +02003458 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
3459 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003460}
3461
3462int complete_pio(struct kvm_vcpu *vcpu)
3463{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003464 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003465 long delta;
3466 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003467 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003468
3469 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003470 if (io->in) {
3471 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3472 memcpy(&val, vcpu->arch.pio_data, io->size);
3473 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3474 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003475 } else {
3476 if (io->in) {
3477 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003478 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01003479 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003480 }
3481
3482 delta = 1;
3483 if (io->rep) {
3484 delta *= io->cur_count;
3485 /*
3486 * The size of the register should really depend on
3487 * current address size.
3488 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003489 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3490 val -= delta;
3491 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003492 }
3493 if (io->down)
3494 delta = -delta;
3495 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003496 if (io->in) {
3497 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3498 val += delta;
3499 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3500 } else {
3501 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3502 val += delta;
3503 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3504 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003505 }
3506
Carsten Ottede7d7892007-10-30 18:44:25 +01003507 io->count -= io->cur_count;
3508 io->cur_count = 0;
3509
3510 return 0;
3511}
3512
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003513static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003514{
3515 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003516 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003517
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003518 if (vcpu->arch.pio.in)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003519 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003520 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003521 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003522 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3523 vcpu->arch.pio.port, vcpu->arch.pio.size,
3524 pd);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003525 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003526}
3527
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003528static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003529{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003530 struct kvm_pio_request *io = &vcpu->arch.pio;
3531 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003532 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003533
Carsten Ottede7d7892007-10-30 18:44:25 +01003534 for (i = 0; i < io->cur_count; i++) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003535 if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003536 io->port, io->size, pd)) {
3537 r = -EOPNOTSUPP;
3538 break;
3539 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003540 pd += io->size;
3541 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003542 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003543}
3544
Avi Kivity851ba692009-08-24 11:10:17 +03003545int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003546{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003547 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003548
3549 vcpu->run->exit_reason = KVM_EXIT_IO;
3550 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003551 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003552 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003553 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3554 vcpu->run->io.port = vcpu->arch.pio.port = port;
3555 vcpu->arch.pio.in = in;
3556 vcpu->arch.pio.string = 0;
3557 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003558 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003559
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003560 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3561 size, 1);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003562
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003563 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3564 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01003565
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003566 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003567 complete_pio(vcpu);
3568 return 1;
3569 }
3570 return 0;
3571}
3572EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3573
Avi Kivity851ba692009-08-24 11:10:17 +03003574int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003575 int size, unsigned long count, int down,
3576 gva_t address, int rep, unsigned port)
3577{
3578 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003579 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003580
3581 vcpu->run->exit_reason = KVM_EXIT_IO;
3582 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003583 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003584 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003585 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3586 vcpu->run->io.port = vcpu->arch.pio.port = port;
3587 vcpu->arch.pio.in = in;
3588 vcpu->arch.pio.string = 1;
3589 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003590 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003591
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003592 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3593 size, count);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003594
Carsten Ottede7d7892007-10-30 18:44:25 +01003595 if (!count) {
3596 kvm_x86_ops->skip_emulated_instruction(vcpu);
3597 return 1;
3598 }
3599
3600 if (!down)
3601 in_page = PAGE_SIZE - offset_in_page(address);
3602 else
3603 in_page = offset_in_page(address) + size;
3604 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003605 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003606 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003607 if (down) {
3608 /*
3609 * String I/O in reverse. Yuck. Kill the guest, fix later.
3610 */
3611 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003612 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003613 return 1;
3614 }
3615 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003616 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003617
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003618 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003619 kvm_x86_ops->skip_emulated_instruction(vcpu);
3620
Izik Eidus0f346072008-12-29 01:42:20 +02003621 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003622
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003623 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003624 /* string PIO write */
3625 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02003626 if (ret == X86EMUL_PROPAGATE_FAULT) {
3627 kvm_inject_gp(vcpu, 0);
3628 return 1;
3629 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003630 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003631 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003632 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003633 ret = 1;
3634 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003635 }
3636 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003637
3638 return ret;
3639}
3640EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3641
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003642static void bounce_off(void *info)
3643{
3644 /* nothing */
3645}
3646
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003647static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3648 void *data)
3649{
3650 struct cpufreq_freqs *freq = data;
3651 struct kvm *kvm;
3652 struct kvm_vcpu *vcpu;
3653 int i, send_ipi = 0;
3654
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003655 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3656 return 0;
3657 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3658 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003659 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003660
3661 spin_lock(&kvm_lock);
3662 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003663 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003664 if (vcpu->cpu != freq->cpu)
3665 continue;
3666 if (!kvm_request_guest_time_update(vcpu))
3667 continue;
3668 if (vcpu->cpu != smp_processor_id())
3669 send_ipi++;
3670 }
3671 }
3672 spin_unlock(&kvm_lock);
3673
3674 if (freq->old < freq->new && send_ipi) {
3675 /*
3676 * We upscale the frequency. Must make the guest
3677 * doesn't see old kvmclock values while running with
3678 * the new frequency, otherwise we risk the guest sees
3679 * time go backwards.
3680 *
3681 * In case we update the frequency for another cpu
3682 * (which might be in guest context) send an interrupt
3683 * to kick the cpu out of guest context. Next time
3684 * guest context is entered kvmclock will be updated,
3685 * so the guest will not see stale values.
3686 */
3687 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3688 }
3689 return 0;
3690}
3691
3692static struct notifier_block kvmclock_cpufreq_notifier_block = {
3693 .notifier_call = kvmclock_cpufreq_notifier
3694};
3695
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003696static void kvm_timer_init(void)
3697{
3698 int cpu;
3699
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003700 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003701 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3702 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003703 for_each_online_cpu(cpu) {
3704 unsigned long khz = cpufreq_get(cpu);
3705 if (!khz)
3706 khz = tsc_khz;
3707 per_cpu(cpu_tsc_khz, cpu) = khz;
3708 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003709 } else {
3710 for_each_possible_cpu(cpu)
3711 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003712 }
3713}
3714
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003715int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003716{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003717 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003718 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3719
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003720 if (kvm_x86_ops) {
3721 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003722 r = -EEXIST;
3723 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003724 }
3725
3726 if (!ops->cpu_has_kvm_support()) {
3727 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003728 r = -EOPNOTSUPP;
3729 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003730 }
3731 if (ops->disabled_by_bios()) {
3732 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003733 r = -EOPNOTSUPP;
3734 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003735 }
3736
Avi Kivity97db56c2008-01-13 13:23:56 +02003737 r = kvm_mmu_module_init();
3738 if (r)
3739 goto out;
3740
3741 kvm_init_msr_list();
3742
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003743 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003744 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003745 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3746 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003747 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003748
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003749 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003750
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003751 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003752
3753out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003754 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003755}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003756
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003757void kvm_arch_exit(void)
3758{
Jan Kiszka888d2562009-04-17 19:24:58 +02003759 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3760 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3761 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003762 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003763 kvm_mmu_module_exit();
3764}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003765
Hollis Blanchard8776e512007-10-31 17:24:24 -05003766int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3767{
3768 ++vcpu->stat.halt_exits;
3769 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003770 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003771 return 1;
3772 } else {
3773 vcpu->run->exit_reason = KVM_EXIT_HLT;
3774 return 0;
3775 }
3776}
3777EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3778
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003779static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3780 unsigned long a1)
3781{
3782 if (is_long_mode(vcpu))
3783 return a0;
3784 else
3785 return a0 | ((gpa_t)a1 << 32);
3786}
3787
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003788int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
3789{
3790 u64 param, ingpa, outgpa, ret;
3791 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
3792 bool fast, longmode;
3793 int cs_db, cs_l;
3794
3795 /*
3796 * hypercall generates UD from non zero cpl and real mode
3797 * per HYPER-V spec
3798 */
3799 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
3800 !kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
3801 kvm_queue_exception(vcpu, UD_VECTOR);
3802 return 0;
3803 }
3804
3805 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3806 longmode = is_long_mode(vcpu) && cs_l == 1;
3807
3808 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02003809 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
3810 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
3811 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
3812 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
3813 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
3814 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003815 }
3816#ifdef CONFIG_X86_64
3817 else {
3818 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
3819 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
3820 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
3821 }
3822#endif
3823
3824 code = param & 0xffff;
3825 fast = (param >> 16) & 0x1;
3826 rep_cnt = (param >> 32) & 0xfff;
3827 rep_idx = (param >> 48) & 0xfff;
3828
3829 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
3830
Gleb Natapovc25bc162010-01-17 15:51:24 +02003831 switch (code) {
3832 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
3833 kvm_vcpu_on_spin(vcpu);
3834 break;
3835 default:
3836 res = HV_STATUS_INVALID_HYPERCALL_CODE;
3837 break;
3838 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003839
3840 ret = res | (((u64)rep_done & 0xfff) << 32);
3841 if (longmode) {
3842 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3843 } else {
3844 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
3845 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
3846 }
3847
3848 return 1;
3849}
3850
Hollis Blanchard8776e512007-10-31 17:24:24 -05003851int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3852{
3853 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003854 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003855
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003856 if (kvm_hv_hypercall_enabled(vcpu->kvm))
3857 return kvm_hv_hypercall(vcpu);
3858
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003859 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3860 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3861 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3862 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3863 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003864
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003865 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003866
Hollis Blanchard8776e512007-10-31 17:24:24 -05003867 if (!is_long_mode(vcpu)) {
3868 nr &= 0xFFFFFFFF;
3869 a0 &= 0xFFFFFFFF;
3870 a1 &= 0xFFFFFFFF;
3871 a2 &= 0xFFFFFFFF;
3872 a3 &= 0xFFFFFFFF;
3873 }
3874
Jan Kiszka07708c42009-08-03 18:43:28 +02003875 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3876 ret = -KVM_EPERM;
3877 goto out;
3878 }
3879
Hollis Blanchard8776e512007-10-31 17:24:24 -05003880 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003881 case KVM_HC_VAPIC_POLL_IRQ:
3882 ret = 0;
3883 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003884 case KVM_HC_MMU_OP:
3885 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3886 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003887 default:
3888 ret = -KVM_ENOSYS;
3889 break;
3890 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003891out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003892 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303893 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003894 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003895}
3896EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3897
3898int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3899{
3900 char instruction[3];
3901 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003902 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003903
Hollis Blanchard8776e512007-10-31 17:24:24 -05003904
3905 /*
3906 * Blow out the MMU to ensure that no other VCPU has an active mapping
3907 * to ensure that the updated hypercall appears atomically across all
3908 * VCPUs.
3909 */
3910 kvm_mmu_zap_all(vcpu->kvm);
3911
Hollis Blanchard8776e512007-10-31 17:24:24 -05003912 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003913 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003914 != X86EMUL_CONTINUE)
3915 ret = -EFAULT;
3916
Hollis Blanchard8776e512007-10-31 17:24:24 -05003917 return ret;
3918}
3919
3920static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3921{
3922 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3923}
3924
3925void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3926{
3927 struct descriptor_table dt = { limit, base };
3928
3929 kvm_x86_ops->set_gdt(vcpu, &dt);
3930}
3931
3932void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3933{
3934 struct descriptor_table dt = { limit, base };
3935
3936 kvm_x86_ops->set_idt(vcpu, &dt);
3937}
3938
3939void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3940 unsigned long *rflags)
3941{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003942 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02003943 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003944}
3945
3946unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3947{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003948 unsigned long value;
3949
Hollis Blanchard8776e512007-10-31 17:24:24 -05003950 switch (cr) {
3951 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02003952 value = kvm_read_cr0(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02003953 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003954 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003955 value = vcpu->arch.cr2;
3956 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003957 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003958 value = vcpu->arch.cr3;
3959 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003960 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02003961 value = kvm_read_cr4(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02003962 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003963 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003964 value = kvm_get_cr8(vcpu);
3965 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003966 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003967 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003968 return 0;
3969 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003970
3971 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003972}
3973
3974void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3975 unsigned long *rflags)
3976{
3977 switch (cr) {
3978 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02003979 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Jan Kiszka91586a32009-10-05 13:07:21 +02003980 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003981 break;
3982 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003983 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003984 break;
3985 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003986 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003987 break;
3988 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02003989 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003990 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003991 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003992 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003993 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003994 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003995 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003996 }
3997}
3998
Dan Kenigsberg07716712007-11-21 17:10:04 +02003999static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4000{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004001 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4002 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004003
4004 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4005 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004006 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004007 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004008 if (ej->function == e->function) {
4009 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4010 return j;
4011 }
4012 }
4013 return 0; /* silence gcc, even though control never reaches here */
4014}
4015
4016/* find an entry with matching function, matching index (if needed), and that
4017 * should be read next (if it's stateful) */
4018static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4019 u32 function, u32 index)
4020{
4021 if (e->function != function)
4022 return 0;
4023 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4024 return 0;
4025 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004026 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004027 return 0;
4028 return 1;
4029}
4030
Alexander Grafd8017472008-11-25 20:17:11 +01004031struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4032 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004033{
4034 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004035 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004036
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004037 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004038 struct kvm_cpuid_entry2 *e;
4039
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004040 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004041 if (is_matching_cpuid_entry(e, function, index)) {
4042 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4043 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004044 best = e;
4045 break;
4046 }
4047 /*
4048 * Both basic or both extended?
4049 */
4050 if (((e->function ^ function) & 0x80000000) == 0)
4051 if (!best || e->function > best->function)
4052 best = e;
4053 }
Alexander Grafd8017472008-11-25 20:17:11 +01004054 return best;
4055}
Sheng Yang0e851882009-12-18 16:48:46 +08004056EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004057
Dong, Eddie82725b22009-03-30 16:21:08 +08004058int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4059{
4060 struct kvm_cpuid_entry2 *best;
4061
4062 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4063 if (best)
4064 return best->eax & 0xff;
4065 return 36;
4066}
4067
Alexander Grafd8017472008-11-25 20:17:11 +01004068void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4069{
4070 u32 function, index;
4071 struct kvm_cpuid_entry2 *best;
4072
4073 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4074 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4075 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4076 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4077 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4078 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4079 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004080 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004081 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4082 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4083 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4084 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004085 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004086 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004087 trace_kvm_cpuid(function,
4088 kvm_register_read(vcpu, VCPU_REGS_RAX),
4089 kvm_register_read(vcpu, VCPU_REGS_RBX),
4090 kvm_register_read(vcpu, VCPU_REGS_RCX),
4091 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004092}
4093EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004094
4095/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004096 * Check if userspace requested an interrupt window, and that the
4097 * interrupt window is open.
4098 *
4099 * No need to exit to userspace if we already have an interrupt queued.
4100 */
Avi Kivity851ba692009-08-24 11:10:17 +03004101static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004102{
Gleb Natapov80618232009-04-21 17:44:56 +03004103 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004104 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004105 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004106}
4107
Avi Kivity851ba692009-08-24 11:10:17 +03004108static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004109{
Avi Kivity851ba692009-08-24 11:10:17 +03004110 struct kvm_run *kvm_run = vcpu->run;
4111
Jan Kiszka91586a32009-10-05 13:07:21 +02004112 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004113 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004114 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004115 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004116 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004117 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004118 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004119 kvm_arch_interrupt_allowed(vcpu) &&
4120 !kvm_cpu_has_interrupt(vcpu) &&
4121 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004122}
4123
Avi Kivityb93463a2007-10-25 16:52:32 +02004124static void vapic_enter(struct kvm_vcpu *vcpu)
4125{
4126 struct kvm_lapic *apic = vcpu->arch.apic;
4127 struct page *page;
4128
4129 if (!apic || !apic->vapic_addr)
4130 return;
4131
4132 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004133
4134 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004135}
4136
4137static void vapic_exit(struct kvm_vcpu *vcpu)
4138{
4139 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004140 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004141
4142 if (!apic || !apic->vapic_addr)
4143 return;
4144
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004145 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004146 kvm_release_page_dirty(apic->vapic_page);
4147 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004148 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004149}
4150
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004151static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4152{
4153 int max_irr, tpr;
4154
4155 if (!kvm_x86_ops->update_cr8_intercept)
4156 return;
4157
Avi Kivity88c808f2009-08-17 22:49:40 +03004158 if (!vcpu->arch.apic)
4159 return;
4160
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004161 if (!vcpu->arch.apic->vapic_addr)
4162 max_irr = kvm_lapic_find_highest_irr(vcpu);
4163 else
4164 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004165
4166 if (max_irr != -1)
4167 max_irr >>= 4;
4168
4169 tpr = kvm_lapic_get_cr8(vcpu);
4170
4171 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4172}
4173
Avi Kivity851ba692009-08-24 11:10:17 +03004174static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004175{
4176 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004177 if (vcpu->arch.exception.pending) {
4178 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4179 vcpu->arch.exception.has_error_code,
4180 vcpu->arch.exception.error_code);
4181 return;
4182 }
4183
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004184 if (vcpu->arch.nmi_injected) {
4185 kvm_x86_ops->set_nmi(vcpu);
4186 return;
4187 }
4188
4189 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004190 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004191 return;
4192 }
4193
4194 /* try to inject new event if pending */
4195 if (vcpu->arch.nmi_pending) {
4196 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4197 vcpu->arch.nmi_pending = false;
4198 vcpu->arch.nmi_injected = true;
4199 kvm_x86_ops->set_nmi(vcpu);
4200 }
4201 } else if (kvm_cpu_has_interrupt(vcpu)) {
4202 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004203 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4204 false);
4205 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004206 }
4207 }
4208}
4209
Avi Kivity851ba692009-08-24 11:10:17 +03004210static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004211{
4212 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004213 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004214 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004215
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004216 if (vcpu->requests)
4217 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4218 kvm_mmu_unload(vcpu);
4219
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004220 r = kvm_mmu_reload(vcpu);
4221 if (unlikely(r))
4222 goto out;
4223
Avi Kivity2f52d582008-01-16 12:49:30 +02004224 if (vcpu->requests) {
4225 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004226 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004227 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4228 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004229 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4230 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004231 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4232 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004233 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4234 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004235 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004236 r = 0;
4237 goto out;
4238 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004239 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004240 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004241 r = 0;
4242 goto out;
4243 }
Avi Kivity02daab22009-12-30 12:40:26 +02004244 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4245 vcpu->fpu_active = 0;
4246 kvm_x86_ops->fpu_deactivate(vcpu);
4247 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004248 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004249
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004250 preempt_disable();
4251
4252 kvm_x86_ops->prepare_guest_switch(vcpu);
4253 kvm_load_guest_fpu(vcpu);
4254
4255 local_irq_disable();
4256
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004257 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4258 smp_mb__after_clear_bit();
4259
Marcelo Tosattid7690172008-09-08 15:23:48 -03004260 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004261 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c142802008-01-15 18:27:32 +02004262 local_irq_enable();
4263 preempt_enable();
4264 r = 1;
4265 goto out;
4266 }
4267
Avi Kivity851ba692009-08-24 11:10:17 +03004268 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004269
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004270 /* enable NMI/IRQ window open exits if needed */
4271 if (vcpu->arch.nmi_pending)
4272 kvm_x86_ops->enable_nmi_window(vcpu);
4273 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4274 kvm_x86_ops->enable_irq_window(vcpu);
4275
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004276 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004277 update_cr8_intercept(vcpu);
4278 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004279 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004280
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004281 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004282
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004283 kvm_guest_enter();
4284
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004285 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004286 set_debugreg(0, 7);
4287 set_debugreg(vcpu->arch.eff_db[0], 0);
4288 set_debugreg(vcpu->arch.eff_db[1], 1);
4289 set_debugreg(vcpu->arch.eff_db[2], 2);
4290 set_debugreg(vcpu->arch.eff_db[3], 3);
4291 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004292
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004293 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004294 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004295
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004296 /*
4297 * If the guest has used debug registers, at least dr7
4298 * will be disabled while returning to the host.
4299 * If we don't have active breakpoints in the host, we don't
4300 * care about the messed up debug address registers. But if
4301 * we have some of them active, restore the old state.
4302 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004303 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004304 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004305
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004306 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004307 local_irq_enable();
4308
4309 ++vcpu->stat.exits;
4310
4311 /*
4312 * We must have an instruction between local_irq_enable() and
4313 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4314 * the interrupt shadow. The stat.exits increment will do nicely.
4315 * But we need to prevent reordering, hence this barrier():
4316 */
4317 barrier();
4318
4319 kvm_guest_exit();
4320
4321 preempt_enable();
4322
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004323 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004324
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004325 /*
4326 * Profile KVM exit RIPs:
4327 */
4328 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004329 unsigned long rip = kvm_rip_read(vcpu);
4330 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004331 }
4332
Avi Kivity298101d2007-11-25 13:41:11 +02004333
Avi Kivityb93463a2007-10-25 16:52:32 +02004334 kvm_lapic_sync_from_vapic(vcpu);
4335
Avi Kivity851ba692009-08-24 11:10:17 +03004336 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004337out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004338 return r;
4339}
4340
Gleb Natapov09cec752009-03-23 15:11:44 +02004341
Avi Kivity851ba692009-08-24 11:10:17 +03004342static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004343{
4344 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004345 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004346
4347 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004348 pr_debug("vcpu %d received sipi with vector # %x\n",
4349 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004350 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004351 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004352 if (r)
4353 return r;
4354 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004355 }
4356
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004357 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004358 vapic_enter(vcpu);
4359
4360 r = 1;
4361 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004362 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004363 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004364 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004365 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004366 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004367 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004368 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004369 {
4370 switch(vcpu->arch.mp_state) {
4371 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004372 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004373 KVM_MP_STATE_RUNNABLE;
4374 case KVM_MP_STATE_RUNNABLE:
4375 break;
4376 case KVM_MP_STATE_SIPI_RECEIVED:
4377 default:
4378 r = -EINTR;
4379 break;
4380 }
4381 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004382 }
4383
Gleb Natapov09cec752009-03-23 15:11:44 +02004384 if (r <= 0)
4385 break;
4386
4387 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4388 if (kvm_cpu_has_pending_timer(vcpu))
4389 kvm_inject_pending_timer_irqs(vcpu);
4390
Avi Kivity851ba692009-08-24 11:10:17 +03004391 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004392 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004393 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004394 ++vcpu->stat.request_irq_exits;
4395 }
4396 if (signal_pending(current)) {
4397 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004398 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004399 ++vcpu->stat.signal_exits;
4400 }
4401 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004402 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004403 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004404 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004405 }
4406 }
4407
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004408 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Avi Kivity851ba692009-08-24 11:10:17 +03004409 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004410
Avi Kivityb93463a2007-10-25 16:52:32 +02004411 vapic_exit(vcpu);
4412
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004413 return r;
4414}
4415
4416int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4417{
4418 int r;
4419 sigset_t sigsaved;
4420
4421 vcpu_load(vcpu);
4422
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004423 if (vcpu->sigset_active)
4424 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4425
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004426 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4427 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004428 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004429 r = -EAGAIN;
4430 goto out;
4431 }
4432
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004433 /* re-sync apic's tpr */
4434 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004435 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004436
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004437 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004438 r = complete_pio(vcpu);
4439 if (r)
4440 goto out;
4441 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004442 if (vcpu->mmio_needed) {
4443 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4444 vcpu->mmio_read_completed = 1;
4445 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004446
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004447 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity851ba692009-08-24 11:10:17 +03004448 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004449 EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004450 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004451 if (r == EMULATE_DO_MMIO) {
4452 /*
4453 * Read-modify-write. Back to userspace.
4454 */
4455 r = 0;
4456 goto out;
4457 }
4458 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004459 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4460 kvm_register_write(vcpu, VCPU_REGS_RAX,
4461 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004462
Avi Kivity851ba692009-08-24 11:10:17 +03004463 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004464
4465out:
4466 if (vcpu->sigset_active)
4467 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4468
4469 vcpu_put(vcpu);
4470 return r;
4471}
4472
4473int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4474{
4475 vcpu_load(vcpu);
4476
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004477 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4478 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4479 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4480 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4481 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4482 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4483 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4484 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004485#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004486 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4487 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4488 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4489 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4490 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4491 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4492 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4493 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004494#endif
4495
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004496 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004497 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004498
4499 vcpu_put(vcpu);
4500
4501 return 0;
4502}
4503
4504int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4505{
4506 vcpu_load(vcpu);
4507
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004508 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4509 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4510 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4511 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4512 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4513 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4514 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4515 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004516#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004517 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4518 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4519 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4520 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4521 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4522 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4523 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4524 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004525#endif
4526
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004527 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004528 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004529
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004530 vcpu->arch.exception.pending = false;
4531
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004532 vcpu_put(vcpu);
4533
4534 return 0;
4535}
4536
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004537void kvm_get_segment(struct kvm_vcpu *vcpu,
4538 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004539{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004540 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004541}
4542
4543void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4544{
4545 struct kvm_segment cs;
4546
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004547 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004548 *db = cs.db;
4549 *l = cs.l;
4550}
4551EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4552
4553int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4554 struct kvm_sregs *sregs)
4555{
4556 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004557
4558 vcpu_load(vcpu);
4559
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004560 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4561 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4562 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4563 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4564 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4565 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004566
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004567 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4568 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004569
4570 kvm_x86_ops->get_idt(vcpu, &dt);
4571 sregs->idt.limit = dt.limit;
4572 sregs->idt.base = dt.base;
4573 kvm_x86_ops->get_gdt(vcpu, &dt);
4574 sregs->gdt.limit = dt.limit;
4575 sregs->gdt.base = dt.base;
4576
Avi Kivity4d4ec082009-12-29 18:07:30 +02004577 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004578 sregs->cr2 = vcpu->arch.cr2;
4579 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004580 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004581 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004582 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004583 sregs->apic_base = kvm_get_apic_base(vcpu);
4584
Gleb Natapov923c61b2009-05-11 13:35:48 +03004585 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004586
Gleb Natapov36752c92009-05-11 13:35:53 +03004587 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004588 set_bit(vcpu->arch.interrupt.nr,
4589 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004590
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004591 vcpu_put(vcpu);
4592
4593 return 0;
4594}
4595
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004596int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4597 struct kvm_mp_state *mp_state)
4598{
4599 vcpu_load(vcpu);
4600 mp_state->mp_state = vcpu->arch.mp_state;
4601 vcpu_put(vcpu);
4602 return 0;
4603}
4604
4605int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4606 struct kvm_mp_state *mp_state)
4607{
4608 vcpu_load(vcpu);
4609 vcpu->arch.mp_state = mp_state->mp_state;
4610 vcpu_put(vcpu);
4611 return 0;
4612}
4613
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004614static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004615 struct kvm_segment *var, int seg)
4616{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004617 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004618}
4619
Izik Eidus37817f22008-03-24 23:14:53 +02004620static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4621 struct kvm_segment *kvm_desct)
4622{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004623 kvm_desct->base = get_desc_base(seg_desc);
4624 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004625 if (seg_desc->g) {
4626 kvm_desct->limit <<= 12;
4627 kvm_desct->limit |= 0xfff;
4628 }
Izik Eidus37817f22008-03-24 23:14:53 +02004629 kvm_desct->selector = selector;
4630 kvm_desct->type = seg_desc->type;
4631 kvm_desct->present = seg_desc->p;
4632 kvm_desct->dpl = seg_desc->dpl;
4633 kvm_desct->db = seg_desc->d;
4634 kvm_desct->s = seg_desc->s;
4635 kvm_desct->l = seg_desc->l;
4636 kvm_desct->g = seg_desc->g;
4637 kvm_desct->avl = seg_desc->avl;
4638 if (!selector)
4639 kvm_desct->unusable = 1;
4640 else
4641 kvm_desct->unusable = 0;
4642 kvm_desct->padding = 0;
4643}
4644
Amit Shahb8222ad2008-10-22 16:39:47 +05304645static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4646 u16 selector,
4647 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004648{
4649 if (selector & 1 << 2) {
4650 struct kvm_segment kvm_seg;
4651
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004652 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004653
4654 if (kvm_seg.unusable)
4655 dtable->limit = 0;
4656 else
4657 dtable->limit = kvm_seg.limit;
4658 dtable->base = kvm_seg.base;
4659 }
4660 else
4661 kvm_x86_ops->get_gdt(vcpu, dtable);
4662}
4663
4664/* allowed just for 8 bytes segments */
4665static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4666 struct desc_struct *seg_desc)
4667{
4668 struct descriptor_table dtable;
4669 u16 index = selector >> 3;
4670
Amit Shahb8222ad2008-10-22 16:39:47 +05304671 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004672
4673 if (dtable.limit < index * 8 + 7) {
4674 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4675 return 1;
4676 }
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004677 return kvm_read_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004678}
4679
4680/* allowed just for 8 bytes segments */
4681static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4682 struct desc_struct *seg_desc)
4683{
4684 struct descriptor_table dtable;
4685 u16 index = selector >> 3;
4686
Amit Shahb8222ad2008-10-22 16:39:47 +05304687 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004688
4689 if (dtable.limit < index * 8 + 7)
4690 return 1;
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004691 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004692}
4693
Gleb Natapovabb39112009-10-25 17:42:02 +02004694static gpa_t get_tss_base_addr(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004695 struct desc_struct *seg_desc)
4696{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004697 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004698
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004699 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02004700}
4701
Izik Eidus37817f22008-03-24 23:14:53 +02004702static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4703{
4704 struct kvm_segment kvm_seg;
4705
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004706 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004707 return kvm_seg.selector;
4708}
4709
4710static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4711 u16 selector,
4712 struct kvm_segment *kvm_seg)
4713{
4714 struct desc_struct seg_desc;
4715
4716 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4717 return 1;
4718 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4719 return 0;
4720}
4721
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004722static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004723{
4724 struct kvm_segment segvar = {
4725 .base = selector << 4,
4726 .limit = 0xffff,
4727 .selector = selector,
4728 .type = 3,
4729 .present = 1,
4730 .dpl = 3,
4731 .db = 0,
4732 .s = 1,
4733 .l = 0,
4734 .g = 0,
4735 .avl = 0,
4736 .unusable = 0,
4737 };
4738 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4739 return 0;
4740}
4741
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004742static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4743{
4744 return (seg != VCPU_SREG_LDTR) &&
4745 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004746 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004747}
4748
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004749static void kvm_check_segment_descriptor(struct kvm_vcpu *vcpu, int seg,
4750 u16 selector)
4751{
4752 /* NULL selector is not valid for CS and SS */
4753 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4754 if (!selector)
4755 kvm_queue_exception_e(vcpu, TS_VECTOR, selector >> 3);
4756}
4757
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004758int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4759 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004760{
4761 struct kvm_segment kvm_seg;
4762
Avi Kivity4d4ec082009-12-29 18:07:30 +02004763 if (is_vm86_segment(vcpu, seg) || !(kvm_read_cr0_bits(vcpu, X86_CR0_PE)))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004764 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004765 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4766 return 1;
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004767
4768 kvm_check_segment_descriptor(vcpu, seg, selector);
Izik Eidus37817f22008-03-24 23:14:53 +02004769 kvm_seg.type |= type_bits;
4770
4771 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4772 seg != VCPU_SREG_LDTR)
4773 if (!kvm_seg.s)
4774 kvm_seg.unusable = 1;
4775
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004776 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004777 return 0;
4778}
4779
4780static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4781 struct tss_segment_32 *tss)
4782{
4783 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004784 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004785 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004786 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4787 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4788 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4789 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4790 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4791 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4792 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4793 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004794 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4795 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4796 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4797 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4798 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4799 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4800 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004801}
4802
4803static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4804 struct tss_segment_32 *tss)
4805{
4806 kvm_set_cr3(vcpu, tss->cr3);
4807
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004808 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004809 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004810
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004811 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4812 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4813 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4814 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4815 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4816 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4817 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4818 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004819
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004820 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004821 return 1;
4822
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004823 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004824 return 1;
4825
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004826 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004827 return 1;
4828
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004829 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004830 return 1;
4831
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004832 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004833 return 1;
4834
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004835 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004836 return 1;
4837
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004838 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004839 return 1;
4840 return 0;
4841}
4842
4843static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4844 struct tss_segment_16 *tss)
4845{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004846 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004847 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004848 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4849 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4850 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4851 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4852 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4853 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4854 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4855 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004856
4857 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4858 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4859 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4860 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4861 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004862}
4863
4864static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4865 struct tss_segment_16 *tss)
4866{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004867 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004868 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004869 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4870 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4871 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4872 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4873 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4874 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4875 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4876 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02004877
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004878 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004879 return 1;
4880
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004881 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004882 return 1;
4883
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004884 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004885 return 1;
4886
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004887 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004888 return 1;
4889
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004890 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004891 return 1;
4892 return 0;
4893}
4894
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004895static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004896 u16 old_tss_sel, u32 old_tss_base,
4897 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02004898{
4899 struct tss_segment_16 tss_segment_16;
4900 int ret = 0;
4901
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004902 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4903 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004904 goto out;
4905
4906 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02004907
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004908 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4909 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004910 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004911
4912 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4913 &tss_segment_16, sizeof tss_segment_16))
4914 goto out;
4915
Gleb Natapovb237ac32009-03-30 16:03:24 +03004916 if (old_tss_sel != 0xffff) {
4917 tss_segment_16.prev_task_link = old_tss_sel;
4918
4919 if (kvm_write_guest(vcpu->kvm,
4920 get_tss_base_addr(vcpu, nseg_desc),
4921 &tss_segment_16.prev_task_link,
4922 sizeof tss_segment_16.prev_task_link))
4923 goto out;
4924 }
4925
Izik Eidus37817f22008-03-24 23:14:53 +02004926 if (load_state_from_tss16(vcpu, &tss_segment_16))
4927 goto out;
4928
4929 ret = 1;
4930out:
4931 return ret;
4932}
4933
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004934static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004935 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02004936 struct desc_struct *nseg_desc)
4937{
4938 struct tss_segment_32 tss_segment_32;
4939 int ret = 0;
4940
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004941 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4942 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004943 goto out;
4944
4945 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02004946
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004947 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4948 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004949 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004950
4951 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4952 &tss_segment_32, sizeof tss_segment_32))
4953 goto out;
4954
Gleb Natapovb237ac32009-03-30 16:03:24 +03004955 if (old_tss_sel != 0xffff) {
4956 tss_segment_32.prev_task_link = old_tss_sel;
4957
4958 if (kvm_write_guest(vcpu->kvm,
4959 get_tss_base_addr(vcpu, nseg_desc),
4960 &tss_segment_32.prev_task_link,
4961 sizeof tss_segment_32.prev_task_link))
4962 goto out;
4963 }
4964
Izik Eidus37817f22008-03-24 23:14:53 +02004965 if (load_state_from_tss32(vcpu, &tss_segment_32))
4966 goto out;
4967
4968 ret = 1;
4969out:
4970 return ret;
4971}
4972
4973int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4974{
4975 struct kvm_segment tr_seg;
4976 struct desc_struct cseg_desc;
4977 struct desc_struct nseg_desc;
4978 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004979 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4980 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004981
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004982 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02004983
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004984 /* FIXME: Handle errors. Failure to read either TSS or their
4985 * descriptors should generate a pagefault.
4986 */
Izik Eidus37817f22008-03-24 23:14:53 +02004987 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4988 goto out;
4989
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004990 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02004991 goto out;
4992
Izik Eidus37817f22008-03-24 23:14:53 +02004993 if (reason != TASK_SWITCH_IRET) {
4994 int cpl;
4995
4996 cpl = kvm_x86_ops->get_cpl(vcpu);
4997 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4998 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4999 return 1;
5000 }
5001 }
5002
Akinobu Mita46a359e2009-07-18 23:58:32 +09005003 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
Izik Eidus37817f22008-03-24 23:14:53 +02005004 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
5005 return 1;
5006 }
5007
5008 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005009 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005010 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005011 }
5012
5013 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005014 u32 eflags = kvm_get_rflags(vcpu);
5015 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005016 }
5017
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005018 /* set back link to prev task only if NT bit is set in eflags
5019 note that old_tss_sel is not used afetr this point */
5020 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
5021 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02005022
5023 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03005024 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
5025 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005026 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03005027 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
5028 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005029
5030 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005031 u32 eflags = kvm_get_rflags(vcpu);
5032 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005033 }
5034
5035 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005036 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02005037 save_guest_segment_descriptor(vcpu, tss_selector,
5038 &nseg_desc);
5039 }
5040
Avi Kivity4d4ec082009-12-29 18:07:30 +02005041 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS);
Izik Eidus37817f22008-03-24 23:14:53 +02005042 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
5043 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005044 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02005045out:
Izik Eidus37817f22008-03-24 23:14:53 +02005046 return ret;
5047}
5048EXPORT_SYMBOL_GPL(kvm_task_switch);
5049
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005050int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5051 struct kvm_sregs *sregs)
5052{
5053 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03005054 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005055 struct descriptor_table dt;
5056
5057 vcpu_load(vcpu);
5058
5059 dt.limit = sregs->idt.limit;
5060 dt.base = sregs->idt.base;
5061 kvm_x86_ops->set_idt(vcpu, &dt);
5062 dt.limit = sregs->gdt.limit;
5063 dt.base = sregs->gdt.base;
5064 kvm_x86_ops->set_gdt(vcpu, &dt);
5065
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005066 vcpu->arch.cr2 = sregs->cr2;
5067 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02005068 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005069
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005070 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005071
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005072 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005073 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005074 kvm_set_apic_base(vcpu, sregs->apic_base);
5075
Avi Kivity4d4ec082009-12-29 18:07:30 +02005076 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005077 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00005078 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005079
Avi Kivityfc78f512009-12-07 12:16:48 +02005080 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005081 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005082 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005083 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005084 mmu_reset_needed = 1;
5085 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005086
5087 if (mmu_reset_needed)
5088 kvm_mmu_reset_context(vcpu);
5089
Gleb Natapov923c61b2009-05-11 13:35:48 +03005090 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5091 pending_vec = find_first_bit(
5092 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5093 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005094 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03005095 pr_debug("Set back pending irq %d\n", pending_vec);
5096 if (irqchip_in_kernel(vcpu->kvm))
5097 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005098 }
5099
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005100 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5101 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5102 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5103 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5104 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5105 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005106
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005107 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5108 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005109
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03005110 update_cr8_intercept(vcpu);
5111
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005112 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005113 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005114 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity4d4ec082009-12-29 18:07:30 +02005115 !(kvm_read_cr0_bits(vcpu, X86_CR0_PE)))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005116 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5117
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005118 vcpu_put(vcpu);
5119
5120 return 0;
5121}
5122
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005123int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5124 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005125{
Jan Kiszka355be0b2009-10-03 00:31:21 +02005126 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005127 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005128
5129 vcpu_load(vcpu);
5130
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005131 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5132 r = -EBUSY;
5133 if (vcpu->arch.exception.pending)
5134 goto unlock_out;
5135 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5136 kvm_queue_exception(vcpu, DB_VECTOR);
5137 else
5138 kvm_queue_exception(vcpu, BP_VECTOR);
5139 }
5140
Jan Kiszka91586a32009-10-05 13:07:21 +02005141 /*
5142 * Read rflags as long as potentially injected trace flags are still
5143 * filtered out.
5144 */
5145 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02005146
5147 vcpu->guest_debug = dbg->control;
5148 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5149 vcpu->guest_debug = 0;
5150
5151 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005152 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5153 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5154 vcpu->arch.switch_db_regs =
5155 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5156 } else {
5157 for (i = 0; i < KVM_NR_DB_REGS; i++)
5158 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5159 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5160 }
5161
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005162 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
5163 vcpu->arch.singlestep_cs =
5164 get_segment_selector(vcpu, VCPU_SREG_CS);
5165 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
5166 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005167
Jan Kiszka91586a32009-10-05 13:07:21 +02005168 /*
5169 * Trigger an rflags update that will inject or remove the trace
5170 * flags.
5171 */
5172 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005173
Jan Kiszka355be0b2009-10-03 00:31:21 +02005174 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5175
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005176 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005177
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005178unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005179 vcpu_put(vcpu);
5180
5181 return r;
5182}
5183
5184/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005185 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5186 * we have asm/x86/processor.h
5187 */
5188struct fxsave {
5189 u16 cwd;
5190 u16 swd;
5191 u16 twd;
5192 u16 fop;
5193 u64 rip;
5194 u64 rdp;
5195 u32 mxcsr;
5196 u32 mxcsr_mask;
5197 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5198#ifdef CONFIG_X86_64
5199 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5200#else
5201 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5202#endif
5203};
5204
Zhang Xiantao8b006792007-11-16 13:05:55 +08005205/*
5206 * Translate a guest virtual address to a guest physical address.
5207 */
5208int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5209 struct kvm_translation *tr)
5210{
5211 unsigned long vaddr = tr->linear_address;
5212 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005213 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005214
5215 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005216 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005217 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005218 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005219 tr->physical_address = gpa;
5220 tr->valid = gpa != UNMAPPED_GVA;
5221 tr->writeable = 1;
5222 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005223 vcpu_put(vcpu);
5224
5225 return 0;
5226}
5227
Hollis Blanchardd0752062007-10-31 17:24:25 -05005228int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5229{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005230 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005231
5232 vcpu_load(vcpu);
5233
5234 memcpy(fpu->fpr, fxsave->st_space, 128);
5235 fpu->fcw = fxsave->cwd;
5236 fpu->fsw = fxsave->swd;
5237 fpu->ftwx = fxsave->twd;
5238 fpu->last_opcode = fxsave->fop;
5239 fpu->last_ip = fxsave->rip;
5240 fpu->last_dp = fxsave->rdp;
5241 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5242
5243 vcpu_put(vcpu);
5244
5245 return 0;
5246}
5247
5248int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5249{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005250 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005251
5252 vcpu_load(vcpu);
5253
5254 memcpy(fxsave->st_space, fpu->fpr, 128);
5255 fxsave->cwd = fpu->fcw;
5256 fxsave->swd = fpu->fsw;
5257 fxsave->twd = fpu->ftwx;
5258 fxsave->fop = fpu->last_opcode;
5259 fxsave->rip = fpu->last_ip;
5260 fxsave->rdp = fpu->last_dp;
5261 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5262
5263 vcpu_put(vcpu);
5264
5265 return 0;
5266}
5267
5268void fx_init(struct kvm_vcpu *vcpu)
5269{
5270 unsigned after_mxcsr_mask;
5271
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005272 /*
5273 * Touch the fpu the first time in non atomic context as if
5274 * this is the first fpu instruction the exception handler
5275 * will fire before the instruction returns and it'll have to
5276 * allocate ram with GFP_KERNEL.
5277 */
5278 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005279 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005280
Hollis Blanchardd0752062007-10-31 17:24:25 -05005281 /* Initialize guest FPU by resetting ours and saving into guest's */
5282 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005283 kvm_fx_save(&vcpu->arch.host_fx_image);
5284 kvm_fx_finit();
5285 kvm_fx_save(&vcpu->arch.guest_fx_image);
5286 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005287 preempt_enable();
5288
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005289 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005290 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005291 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5292 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005293 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5294}
5295EXPORT_SYMBOL_GPL(fx_init);
5296
5297void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5298{
5299 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
5300 return;
5301
5302 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005303 kvm_fx_save(&vcpu->arch.host_fx_image);
5304 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005305}
5306EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
5307
5308void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5309{
5310 if (!vcpu->guest_fpu_loaded)
5311 return;
5312
5313 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005314 kvm_fx_save(&vcpu->arch.guest_fx_image);
5315 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005316 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005317 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005318}
5319EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005320
5321void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5322{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005323 if (vcpu->arch.time_page) {
5324 kvm_release_page_dirty(vcpu->arch.time_page);
5325 vcpu->arch.time_page = NULL;
5326 }
5327
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005328 kvm_x86_ops->vcpu_free(vcpu);
5329}
5330
5331struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5332 unsigned int id)
5333{
Avi Kivity26e52152007-11-20 15:30:24 +02005334 return kvm_x86_ops->vcpu_create(kvm, id);
5335}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005336
Avi Kivity26e52152007-11-20 15:30:24 +02005337int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5338{
5339 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005340
5341 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005342 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005343
Sheng Yang0bed3b52008-10-09 16:01:54 +08005344 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005345 vcpu_load(vcpu);
5346 r = kvm_arch_vcpu_reset(vcpu);
5347 if (r == 0)
5348 r = kvm_mmu_setup(vcpu);
5349 vcpu_put(vcpu);
5350 if (r < 0)
5351 goto free_vcpu;
5352
Avi Kivity26e52152007-11-20 15:30:24 +02005353 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005354free_vcpu:
5355 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005356 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005357}
5358
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005359void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005360{
5361 vcpu_load(vcpu);
5362 kvm_mmu_unload(vcpu);
5363 vcpu_put(vcpu);
5364
5365 kvm_x86_ops->vcpu_free(vcpu);
5366}
5367
5368int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5369{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005370 vcpu->arch.nmi_pending = false;
5371 vcpu->arch.nmi_injected = false;
5372
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005373 vcpu->arch.switch_db_regs = 0;
5374 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5375 vcpu->arch.dr6 = DR6_FIXED_1;
5376 vcpu->arch.dr7 = DR7_FIXED_1;
5377
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005378 return kvm_x86_ops->vcpu_reset(vcpu);
5379}
5380
Alexander Graf10474ae2009-09-15 11:37:46 +02005381int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005382{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005383 /*
5384 * Since this may be called from a hotplug notifcation,
5385 * we can't get the CPU frequency directly.
5386 */
5387 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5388 int cpu = raw_smp_processor_id();
5389 per_cpu(cpu_tsc_khz, cpu) = 0;
5390 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005391
5392 kvm_shared_msr_cpu_online();
5393
Alexander Graf10474ae2009-09-15 11:37:46 +02005394 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005395}
5396
5397void kvm_arch_hardware_disable(void *garbage)
5398{
5399 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005400 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005401}
5402
5403int kvm_arch_hardware_setup(void)
5404{
5405 return kvm_x86_ops->hardware_setup();
5406}
5407
5408void kvm_arch_hardware_unsetup(void)
5409{
5410 kvm_x86_ops->hardware_unsetup();
5411}
5412
5413void kvm_arch_check_processor_compat(void *rtn)
5414{
5415 kvm_x86_ops->check_processor_compatibility(rtn);
5416}
5417
5418int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5419{
5420 struct page *page;
5421 struct kvm *kvm;
5422 int r;
5423
5424 BUG_ON(vcpu->kvm == NULL);
5425 kvm = vcpu->kvm;
5426
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005427 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005428 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005429 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005430 else
Avi Kivitya4535292008-04-13 17:54:35 +03005431 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005432
5433 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5434 if (!page) {
5435 r = -ENOMEM;
5436 goto fail;
5437 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005438 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005439
5440 r = kvm_mmu_create(vcpu);
5441 if (r < 0)
5442 goto fail_free_pio_data;
5443
5444 if (irqchip_in_kernel(kvm)) {
5445 r = kvm_create_lapic(vcpu);
5446 if (r < 0)
5447 goto fail_mmu_destroy;
5448 }
5449
Huang Ying890ca9a2009-05-11 16:48:15 +08005450 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5451 GFP_KERNEL);
5452 if (!vcpu->arch.mce_banks) {
5453 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005454 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005455 }
5456 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5457
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005458 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005459fail_free_lapic:
5460 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005461fail_mmu_destroy:
5462 kvm_mmu_destroy(vcpu);
5463fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005464 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005465fail:
5466 return r;
5467}
5468
5469void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5470{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005471 int idx;
5472
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005473 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005474 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005475 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005476 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005477 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005478 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005479}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005480
5481struct kvm *kvm_arch_create_vm(void)
5482{
5483 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5484
5485 if (!kvm)
5486 return ERR_PTR(-ENOMEM);
5487
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005488 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5489 if (!kvm->arch.aliases) {
5490 kfree(kvm);
5491 return ERR_PTR(-ENOMEM);
5492 }
5493
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005494 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005495 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005496
Sheng Yang5550af42008-10-15 20:15:06 +08005497 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5498 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5499
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005500 rdtscll(kvm->arch.vm_init_tsc);
5501
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005502 return kvm;
5503}
5504
5505static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5506{
5507 vcpu_load(vcpu);
5508 kvm_mmu_unload(vcpu);
5509 vcpu_put(vcpu);
5510}
5511
5512static void kvm_free_vcpus(struct kvm *kvm)
5513{
5514 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005515 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005516
5517 /*
5518 * Unpin any mmu pages first.
5519 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005520 kvm_for_each_vcpu(i, vcpu, kvm)
5521 kvm_unload_vcpu_mmu(vcpu);
5522 kvm_for_each_vcpu(i, vcpu, kvm)
5523 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005524
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005525 mutex_lock(&kvm->lock);
5526 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5527 kvm->vcpus[i] = NULL;
5528
5529 atomic_set(&kvm->online_vcpus, 0);
5530 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005531}
5532
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005533void kvm_arch_sync_events(struct kvm *kvm)
5534{
Sheng Yangba4cef32009-01-06 10:03:03 +08005535 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005536}
5537
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005538void kvm_arch_destroy_vm(struct kvm *kvm)
5539{
Sheng Yang6eb55812008-10-31 12:37:41 +08005540 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005541 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005542 kfree(kvm->arch.vpic);
5543 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005544 kvm_free_vcpus(kvm);
5545 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005546 if (kvm->arch.apic_access_page)
5547 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005548 if (kvm->arch.ept_identity_pagetable)
5549 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005550 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005551 kfree(kvm);
5552}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005553
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005554int kvm_arch_prepare_memory_region(struct kvm *kvm,
5555 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005556 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005557 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005558 int user_alloc)
5559{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005560 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005561
5562 /*To keep backward compatibility with older userspace,
5563 *x86 needs to hanlde !user_alloc case.
5564 */
5565 if (!user_alloc) {
5566 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005567 unsigned long userspace_addr;
5568
Izik Eidus72dc67a2008-02-10 18:04:15 +02005569 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005570 userspace_addr = do_mmap(NULL, 0,
5571 npages * PAGE_SIZE,
5572 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005573 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005574 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005575 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005576
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005577 if (IS_ERR((void *)userspace_addr))
5578 return PTR_ERR((void *)userspace_addr);
5579
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005580 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005581 }
5582 }
5583
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005584
5585 return 0;
5586}
5587
5588void kvm_arch_commit_memory_region(struct kvm *kvm,
5589 struct kvm_userspace_memory_region *mem,
5590 struct kvm_memory_slot old,
5591 int user_alloc)
5592{
5593
5594 int npages = mem->memory_size >> PAGE_SHIFT;
5595
5596 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5597 int ret;
5598
5599 down_write(&current->mm->mmap_sem);
5600 ret = do_munmap(current->mm, old.userspace_addr,
5601 old.npages * PAGE_SIZE);
5602 up_write(&current->mm->mmap_sem);
5603 if (ret < 0)
5604 printk(KERN_WARNING
5605 "kvm_vm_ioctl_set_memory_region: "
5606 "failed to munmap memory\n");
5607 }
5608
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005609 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005610 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005611 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5612 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5613 }
5614
5615 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005616 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005617}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005618
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005619void kvm_arch_flush_shadow(struct kvm *kvm)
5620{
5621 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005622 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005623}
5624
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005625int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5626{
Avi Kivitya4535292008-04-13 17:54:35 +03005627 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005628 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5629 || vcpu->arch.nmi_pending ||
5630 (kvm_arch_interrupt_allowed(vcpu) &&
5631 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005632}
Zhang Xiantao57361992007-12-17 14:21:40 +08005633
Zhang Xiantao57361992007-12-17 14:21:40 +08005634void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5635{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005636 int me;
5637 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005638
5639 if (waitqueue_active(&vcpu->wq)) {
5640 wake_up_interruptible(&vcpu->wq);
5641 ++vcpu->stat.halt_wakeup;
5642 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005643
5644 me = get_cpu();
5645 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5646 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5647 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005648 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005649}
Gleb Natapov78646122009-03-23 12:12:11 +02005650
5651int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5652{
5653 return kvm_x86_ops->interrupt_allowed(vcpu);
5654}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005655
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005656unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5657{
5658 unsigned long rflags;
5659
5660 rflags = kvm_x86_ops->get_rflags(vcpu);
5661 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5662 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5663 return rflags;
5664}
5665EXPORT_SYMBOL_GPL(kvm_get_rflags);
5666
5667void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5668{
5669 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5670 vcpu->arch.singlestep_cs ==
5671 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5672 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5673 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5674 kvm_x86_ops->set_rflags(vcpu, rflags);
5675}
5676EXPORT_SYMBOL_GPL(kvm_set_rflags);
5677
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005678EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5679EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5680EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5681EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5682EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005683EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005684EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005685EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005686EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005687EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005688EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);