blob: 6c2ecf0a806d67040335f7c0c57c0b57fed3384d [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
Avi Kivity221d0592010-05-23 18:37:00 +03009 * Copyright 2010 Red Hat, Inc. and/or its affilates.
Carsten Otte043405e2007-10-10 17:16:19 +020010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030014 * Amit Shah <amit.shah@qumranet.com>
15 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020016 *
17 * This work is licensed under the terms of the GNU GPL, version 2. See
18 * the COPYING file in the top-level directory.
19 *
20 */
21
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020023#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080024#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080025#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020026#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030027#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030028#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020029
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020030#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030031#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020032#include <linux/kvm.h>
33#include <linux/fs.h>
34#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010035#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080036#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050037#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010038#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030039#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010040#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030041#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020042#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080044#include <linux/perf_event.h>
Lai Jiangshan7bee3422010-06-02 17:06:03 +080045#include <linux/uaccess.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030046#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080047
Marcelo Tosatti229456f2009-06-17 09:22:14 -030048#define CREATE_TRACE_POINTS
49#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020050
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020051#include <asm/debugreg.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080052#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020053#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080054#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080055#include <asm/mce.h>
Sheng Yang7cf30852010-05-17 17:08:27 +080056#include <asm/i387.h>
Sheng Yang98918832010-05-17 17:08:28 +080057#include <asm/xcr.h>
Carsten Otte043405e2007-10-10 17:16:19 +020058
Carsten Otte313a3dc2007-10-11 19:16:52 +020059#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010060#define CR0_RESERVED_BITS \
61 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
62 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
63 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
64#define CR4_RESERVED_BITS \
65 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
66 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
67 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
Dexuan Cui2acf9232010-06-10 11:27:12 +080068 | X86_CR4_OSXSAVE \
Carsten Ottea03490e2007-10-29 16:09:35 +010069 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
70
71#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080072
73#define KVM_MAX_MCE_BANKS 32
74#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
75
Joerg Roedel50a37eb2008-01-31 14:57:38 +010076/* EFER defaults:
77 * - enable syscall per default because its emulated by KVM
78 * - enable LME and LMA per default on 64 bit KVM
79 */
80#ifdef CONFIG_X86_64
81static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
82#else
83static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
84#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020085
Avi Kivityba1389b2007-11-18 16:24:12 +020086#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
87#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050088
Gleb Natapovcb142eb2009-08-09 15:17:40 +030089static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020090static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
91 struct kvm_cpuid_entry2 __user *entries);
92
Zhang Xiantao97896d02007-11-14 20:09:30 +080093struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030094EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080095
Andre Przywaraed85c062009-06-25 12:36:49 +020096int ignore_msrs = 0;
97module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
98
Avi Kivity18863bd2009-09-07 11:12:18 +030099#define KVM_NR_SHARED_MSRS 16
100
101struct kvm_shared_msrs_global {
102 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800103 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300104};
105
106struct kvm_shared_msrs {
107 struct user_return_notifier urn;
108 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800109 struct kvm_shared_msr_values {
110 u64 host;
111 u64 curr;
112 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300113};
114
115static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
116static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
117
Hollis Blanchard417bc302007-10-31 17:24:23 -0500118struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200119 { "pf_fixed", VCPU_STAT(pf_fixed) },
120 { "pf_guest", VCPU_STAT(pf_guest) },
121 { "tlb_flush", VCPU_STAT(tlb_flush) },
122 { "invlpg", VCPU_STAT(invlpg) },
123 { "exits", VCPU_STAT(exits) },
124 { "io_exits", VCPU_STAT(io_exits) },
125 { "mmio_exits", VCPU_STAT(mmio_exits) },
126 { "signal_exits", VCPU_STAT(signal_exits) },
127 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800128 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200129 { "halt_exits", VCPU_STAT(halt_exits) },
130 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530131 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200132 { "request_irq", VCPU_STAT(request_irq_exits) },
133 { "irq_exits", VCPU_STAT(irq_exits) },
134 { "host_state_reload", VCPU_STAT(host_state_reload) },
135 { "efer_reload", VCPU_STAT(efer_reload) },
136 { "fpu_reload", VCPU_STAT(fpu_reload) },
137 { "insn_emulation", VCPU_STAT(insn_emulation) },
138 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300139 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200140 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200141 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
142 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
143 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
144 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
145 { "mmu_flooded", VM_STAT(mmu_flooded) },
146 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200147 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300148 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200149 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300150 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500151 { NULL }
152};
153
Dexuan Cui2acf9232010-06-10 11:27:12 +0800154u64 __read_mostly host_xcr0;
155
156static inline u32 bit(int bitno)
157{
158 return 1 << (bitno & 31);
159}
160
Avi Kivity18863bd2009-09-07 11:12:18 +0300161static void kvm_on_user_return(struct user_return_notifier *urn)
162{
163 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300164 struct kvm_shared_msrs *locals
165 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800166 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300167
168 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800169 values = &locals->values[slot];
170 if (values->host != values->curr) {
171 wrmsrl(shared_msrs_global.msrs[slot], values->host);
172 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300173 }
174 }
175 locals->registered = false;
176 user_return_notifier_unregister(urn);
177}
178
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800179static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300180{
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800181 struct kvm_shared_msrs *smsr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300182 u64 value;
183
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800184 smsr = &__get_cpu_var(shared_msrs);
185 /* only read, and nobody should modify it at this time,
186 * so don't need lock */
187 if (slot >= shared_msrs_global.nr) {
188 printk(KERN_ERR "kvm: invalid MSR slot!");
189 return;
190 }
191 rdmsrl_safe(msr, &value);
192 smsr->values[slot].host = value;
193 smsr->values[slot].curr = value;
194}
195
196void kvm_define_shared_msr(unsigned slot, u32 msr)
197{
Avi Kivity18863bd2009-09-07 11:12:18 +0300198 if (slot >= shared_msrs_global.nr)
199 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800200 shared_msrs_global.msrs[slot] = msr;
201 /* we need ensured the shared_msr_global have been updated */
202 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300203}
204EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
205
206static void kvm_shared_msr_cpu_online(void)
207{
208 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300209
210 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800211 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300212}
213
Avi Kivityd5696722009-12-02 12:28:47 +0200214void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300215{
216 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
217
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800218 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300219 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800220 smsr->values[slot].curr = value;
221 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300222 if (!smsr->registered) {
223 smsr->urn.on_user_return = kvm_on_user_return;
224 user_return_notifier_register(&smsr->urn);
225 smsr->registered = true;
226 }
227}
228EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
229
Avi Kivity3548bab2009-11-28 14:18:47 +0200230static void drop_user_return_notifiers(void *ignore)
231{
232 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
233
234 if (smsr->registered)
235 kvm_on_user_return(&smsr->urn);
236}
237
Carsten Otte6866b832007-10-29 16:09:10 +0100238u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
239{
240 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800241 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100242 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100244}
245EXPORT_SYMBOL_GPL(kvm_get_apic_base);
246
247void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
248{
249 /* TODO: reserve bits check */
250 if (irqchip_in_kernel(vcpu->kvm))
251 kvm_lapic_set_base(vcpu, data);
252 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800253 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100254}
255EXPORT_SYMBOL_GPL(kvm_set_apic_base);
256
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200257#define EXCPT_BENIGN 0
258#define EXCPT_CONTRIBUTORY 1
259#define EXCPT_PF 2
260
261static int exception_class(int vector)
262{
263 switch (vector) {
264 case PF_VECTOR:
265 return EXCPT_PF;
266 case DE_VECTOR:
267 case TS_VECTOR:
268 case NP_VECTOR:
269 case SS_VECTOR:
270 case GP_VECTOR:
271 return EXCPT_CONTRIBUTORY;
272 default:
273 break;
274 }
275 return EXCPT_BENIGN;
276}
277
278static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200279 unsigned nr, bool has_error, u32 error_code,
280 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200281{
282 u32 prev_nr;
283 int class1, class2;
284
285 if (!vcpu->arch.exception.pending) {
286 queue:
287 vcpu->arch.exception.pending = true;
288 vcpu->arch.exception.has_error_code = has_error;
289 vcpu->arch.exception.nr = nr;
290 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200291 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200292 return;
293 }
294
295 /* to check exception */
296 prev_nr = vcpu->arch.exception.nr;
297 if (prev_nr == DF_VECTOR) {
298 /* triple fault -> shutdown */
Avi Kivitya8eeb042010-05-10 12:34:53 +0300299 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200300 return;
301 }
302 class1 = exception_class(prev_nr);
303 class2 = exception_class(nr);
304 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
305 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
306 /* generate double fault per SDM Table 5-5 */
307 vcpu->arch.exception.pending = true;
308 vcpu->arch.exception.has_error_code = true;
309 vcpu->arch.exception.nr = DF_VECTOR;
310 vcpu->arch.exception.error_code = 0;
311 } else
312 /* replace previous exception with a new one in a hope
313 that instruction re-execution will regenerate lost
314 exception */
315 goto queue;
316}
317
Avi Kivity298101d2007-11-25 13:41:11 +0200318void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
319{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200320 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200321}
322EXPORT_SYMBOL_GPL(kvm_queue_exception);
323
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200324void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
325{
326 kvm_multiple_exception(vcpu, nr, false, 0, true);
327}
328EXPORT_SYMBOL_GPL(kvm_requeue_exception);
329
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200330void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
331 u32 error_code)
332{
333 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200335 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
336}
337
Sheng Yang3419ffc2008-05-15 09:52:48 +0800338void kvm_inject_nmi(struct kvm_vcpu *vcpu)
339{
340 vcpu->arch.nmi_pending = 1;
341}
342EXPORT_SYMBOL_GPL(kvm_inject_nmi);
343
Avi Kivity298101d2007-11-25 13:41:11 +0200344void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
345{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200346 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200347}
348EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
349
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200350void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
351{
352 kvm_multiple_exception(vcpu, nr, true, error_code, true);
353}
354EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
355
Carsten Ottea03490e2007-10-29 16:09:35 +0100356/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300357 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
358 * a #GP and return false.
359 */
360bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200361{
Avi Kivity0a79b002009-09-01 12:03:25 +0300362 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
363 return true;
364 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
365 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100366}
Avi Kivity0a79b002009-09-01 12:03:25 +0300367EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100368
369/*
370 * Load the pae pdptrs. Return true is they are all valid.
371 */
372int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
373{
374 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
375 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
376 int i;
377 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800378 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100379
Carsten Ottea03490e2007-10-29 16:09:35 +0100380 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
381 offset * sizeof(u64), sizeof(pdpte));
382 if (ret < 0) {
383 ret = 0;
384 goto out;
385 }
386 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300387 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800388 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100389 ret = 0;
390 goto out;
391 }
392 }
393 ret = 1;
394
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800395 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300396 __set_bit(VCPU_EXREG_PDPTR,
397 (unsigned long *)&vcpu->arch.regs_avail);
398 __set_bit(VCPU_EXREG_PDPTR,
399 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100400out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100401
402 return ret;
403}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100404EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100405
Avi Kivityd835dfe2007-11-21 02:57:59 +0200406static bool pdptrs_changed(struct kvm_vcpu *vcpu)
407{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800408 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200409 bool changed = true;
410 int r;
411
412 if (is_long_mode(vcpu) || !is_pae(vcpu))
413 return false;
414
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300415 if (!test_bit(VCPU_EXREG_PDPTR,
416 (unsigned long *)&vcpu->arch.regs_avail))
417 return true;
418
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800419 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200420 if (r < 0)
421 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800422 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200423out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200424
425 return changed;
426}
427
Avi Kivity49a9b072010-06-10 17:02:14 +0300428int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100429{
Sheng Yangaad82702010-05-12 16:40:42 +0800430 unsigned long old_cr0 = kvm_read_cr0(vcpu);
431 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
432 X86_CR0_CD | X86_CR0_NW;
433
Avi Kivityf9a48e62010-01-06 19:10:22 +0200434 cr0 |= X86_CR0_ET;
435
Gleb Natapovab344822010-01-21 15:28:46 +0200436#ifdef CONFIG_X86_64
Gleb Natapov0f122442010-04-28 19:15:31 +0300437 if (cr0 & 0xffffffff00000000UL)
438 return 1;
Gleb Natapovab344822010-01-21 15:28:46 +0200439#endif
440
441 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100442
Gleb Natapov0f122442010-04-28 19:15:31 +0300443 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
444 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100445
Gleb Natapov0f122442010-04-28 19:15:31 +0300446 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
447 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100448
449 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
450#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200451 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100452 int cs_db, cs_l;
453
Gleb Natapov0f122442010-04-28 19:15:31 +0300454 if (!is_pae(vcpu))
455 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100456 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Gleb Natapov0f122442010-04-28 19:15:31 +0300457 if (cs_l)
458 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100459 } else
460#endif
Gleb Natapov0f122442010-04-28 19:15:31 +0300461 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3))
462 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100463 }
464
465 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100466
Sheng Yangaad82702010-05-12 16:40:42 +0800467 if ((cr0 ^ old_cr0) & update_bits)
468 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300469 return 0;
470}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200471EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100472
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200473void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100474{
Avi Kivity49a9b072010-06-10 17:02:14 +0300475 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100476}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200477EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100478
Dexuan Cui2acf9232010-06-10 11:27:12 +0800479int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
480{
481 u64 xcr0;
482
483 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
484 if (index != XCR_XFEATURE_ENABLED_MASK)
485 return 1;
486 xcr0 = xcr;
487 if (kvm_x86_ops->get_cpl(vcpu) != 0)
488 return 1;
489 if (!(xcr0 & XSTATE_FP))
490 return 1;
491 if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
492 return 1;
493 if (xcr0 & ~host_xcr0)
494 return 1;
495 vcpu->arch.xcr0 = xcr0;
496 vcpu->guest_xcr0_loaded = 0;
497 return 0;
498}
499
500int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
501{
502 if (__kvm_set_xcr(vcpu, index, xcr)) {
503 kvm_inject_gp(vcpu, 0);
504 return 1;
505 }
506 return 0;
507}
508EXPORT_SYMBOL_GPL(kvm_set_xcr);
509
510static bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu)
511{
512 struct kvm_cpuid_entry2 *best;
513
514 best = kvm_find_cpuid_entry(vcpu, 1, 0);
515 return best && (best->ecx & bit(X86_FEATURE_XSAVE));
516}
517
518static void update_cpuid(struct kvm_vcpu *vcpu)
519{
520 struct kvm_cpuid_entry2 *best;
521
522 best = kvm_find_cpuid_entry(vcpu, 1, 0);
523 if (!best)
524 return;
525
526 /* Update OSXSAVE bit */
527 if (cpu_has_xsave && best->function == 0x1) {
528 best->ecx &= ~(bit(X86_FEATURE_OSXSAVE));
529 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE))
530 best->ecx |= bit(X86_FEATURE_OSXSAVE);
531 }
532}
533
Avi Kivitya83b29c2010-06-10 17:02:15 +0300534int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100535{
Avi Kivityfc78f512009-12-07 12:16:48 +0200536 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300537 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
538
Gleb Natapov0f122442010-04-28 19:15:31 +0300539 if (cr4 & CR4_RESERVED_BITS)
540 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100541
Dexuan Cui2acf9232010-06-10 11:27:12 +0800542 if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE))
543 return 1;
544
Carsten Ottea03490e2007-10-29 16:09:35 +0100545 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300546 if (!(cr4 & X86_CR4_PAE))
547 return 1;
Avi Kivitya2edf572009-05-24 22:19:00 +0300548 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
549 && ((cr4 ^ old_cr4) & pdptr_bits)
Gleb Natapov0f122442010-04-28 19:15:31 +0300550 && !load_pdptrs(vcpu, vcpu->arch.cr3))
551 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100552
Gleb Natapov0f122442010-04-28 19:15:31 +0300553 if (cr4 & X86_CR4_VMXE)
554 return 1;
555
Carsten Ottea03490e2007-10-29 16:09:35 +0100556 kvm_x86_ops->set_cr4(vcpu, cr4);
Sheng Yang62ad0752010-05-12 16:40:41 +0800557
Sheng Yangaad82702010-05-12 16:40:42 +0800558 if ((cr4 ^ old_cr4) & pdptr_bits)
559 kvm_mmu_reset_context(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300560
Dexuan Cui2acf9232010-06-10 11:27:12 +0800561 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
562 update_cpuid(vcpu);
563
Gleb Natapov0f122442010-04-28 19:15:31 +0300564 return 0;
565}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200566EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100567
Avi Kivity23902182010-06-10 17:02:16 +0300568int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100569{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800570 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300571 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200572 kvm_mmu_flush_tlb(vcpu);
Gleb Natapov0f122442010-04-28 19:15:31 +0300573 return 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200574 }
575
Carsten Ottea03490e2007-10-29 16:09:35 +0100576 if (is_long_mode(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300577 if (cr3 & CR3_L_MODE_RESERVED_BITS)
578 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100579 } else {
580 if (is_pae(vcpu)) {
Gleb Natapov0f122442010-04-28 19:15:31 +0300581 if (cr3 & CR3_PAE_RESERVED_BITS)
582 return 1;
583 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3))
584 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100585 }
586 /*
587 * We don't check reserved bits in nonpae mode, because
588 * this isn't enforced, and VMware depends on this.
589 */
590 }
591
Carsten Ottea03490e2007-10-29 16:09:35 +0100592 /*
593 * Does the new cr3 value map to physical memory? (Note, we
594 * catch an invalid cr3 even in real-mode, because it would
595 * cause trouble later on when we turn on paging anyway.)
596 *
597 * A real CPU would silently accept an invalid cr3 and would
598 * attempt to use it - with largely undefined (and often hard
599 * to debug) behavior on the guest side.
600 */
601 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Gleb Natapov0f122442010-04-28 19:15:31 +0300602 return 1;
603 vcpu->arch.cr3 = cr3;
604 vcpu->arch.mmu.new_cr3(vcpu);
605 return 0;
606}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200607EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100608
Gleb Natapov0f122442010-04-28 19:15:31 +0300609int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100610{
Gleb Natapov0f122442010-04-28 19:15:31 +0300611 if (cr8 & CR8_RESERVED_BITS)
612 return 1;
Carsten Ottea03490e2007-10-29 16:09:35 +0100613 if (irqchip_in_kernel(vcpu->kvm))
614 kvm_lapic_set_tpr(vcpu, cr8);
615 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800616 vcpu->arch.cr8 = cr8;
Gleb Natapov0f122442010-04-28 19:15:31 +0300617 return 0;
618}
619
620void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
621{
622 if (__kvm_set_cr8(vcpu, cr8))
623 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100624}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200625EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100626
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200627unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100628{
629 if (irqchip_in_kernel(vcpu->kvm))
630 return kvm_lapic_get_cr8(vcpu);
631 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800632 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100633}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200634EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100635
Gleb Natapov338dbc92010-04-28 19:15:32 +0300636static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
Gleb Natapov020df072010-04-13 10:05:23 +0300637{
638 switch (dr) {
639 case 0 ... 3:
640 vcpu->arch.db[dr] = val;
641 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
642 vcpu->arch.eff_db[dr] = val;
643 break;
644 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300645 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
646 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300647 /* fall through */
648 case 6:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300649 if (val & 0xffffffff00000000ULL)
650 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300651 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
652 break;
653 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300654 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
655 return 1; /* #UD */
Gleb Natapov020df072010-04-13 10:05:23 +0300656 /* fall through */
657 default: /* 7 */
Gleb Natapov338dbc92010-04-28 19:15:32 +0300658 if (val & 0xffffffff00000000ULL)
659 return -1; /* #GP */
Gleb Natapov020df072010-04-13 10:05:23 +0300660 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
661 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
662 kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
663 vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
664 }
665 break;
666 }
667
668 return 0;
669}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300670
671int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
672{
673 int res;
674
675 res = __kvm_set_dr(vcpu, dr, val);
676 if (res > 0)
677 kvm_queue_exception(vcpu, UD_VECTOR);
678 else if (res < 0)
679 kvm_inject_gp(vcpu, 0);
680
681 return res;
682}
Gleb Natapov020df072010-04-13 10:05:23 +0300683EXPORT_SYMBOL_GPL(kvm_set_dr);
684
Gleb Natapov338dbc92010-04-28 19:15:32 +0300685static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
Gleb Natapov020df072010-04-13 10:05:23 +0300686{
687 switch (dr) {
688 case 0 ... 3:
689 *val = vcpu->arch.db[dr];
690 break;
691 case 4:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300692 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300693 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300694 /* fall through */
695 case 6:
696 *val = vcpu->arch.dr6;
697 break;
698 case 5:
Gleb Natapov338dbc92010-04-28 19:15:32 +0300699 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Gleb Natapov020df072010-04-13 10:05:23 +0300700 return 1;
Gleb Natapov020df072010-04-13 10:05:23 +0300701 /* fall through */
702 default: /* 7 */
703 *val = vcpu->arch.dr7;
704 break;
705 }
706
707 return 0;
708}
Gleb Natapov338dbc92010-04-28 19:15:32 +0300709
710int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
711{
712 if (_kvm_get_dr(vcpu, dr, val)) {
713 kvm_queue_exception(vcpu, UD_VECTOR);
714 return 1;
715 }
716 return 0;
717}
Gleb Natapov020df072010-04-13 10:05:23 +0300718EXPORT_SYMBOL_GPL(kvm_get_dr);
719
Carsten Otte043405e2007-10-10 17:16:19 +0200720/*
721 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
722 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
723 *
724 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400725 * capabilities of the host cpu. This capabilities test skips MSRs that are
726 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200727 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400728
Glauber Costa11c6bff2010-05-11 12:17:41 -0400729#define KVM_SAVE_MSRS_BEGIN 7
Carsten Otte043405e2007-10-10 17:16:19 +0200730static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400731 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Glauber Costa11c6bff2010-05-11 12:17:41 -0400732 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200733 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200734 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200735 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Brian Gerst8c065852010-07-17 09:03:26 -0400736 MSR_STAR,
Carsten Otte043405e2007-10-10 17:16:19 +0200737#ifdef CONFIG_X86_64
738 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
739#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400740 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200741};
742
743static unsigned num_msrs_to_save;
744
745static u32 emulated_msrs[] = {
746 MSR_IA32_MISC_ENABLE,
Avi Kivity908e75f2010-07-07 14:09:38 +0300747 MSR_IA32_MCG_STATUS,
748 MSR_IA32_MCG_CTL,
Carsten Otte043405e2007-10-10 17:16:19 +0200749};
750
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200751static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
Carsten Otte15c4a642007-10-30 18:44:17 +0100752{
Sheng Yangaad82702010-05-12 16:40:42 +0800753 u64 old_efer = vcpu->arch.efer;
754
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200755 if (efer & efer_reserved_bits)
756 return 1;
Carsten Otte15c4a642007-10-30 18:44:17 +0100757
758 if (is_paging(vcpu)
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200759 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
760 return 1;
Carsten Otte15c4a642007-10-30 18:44:17 +0100761
Alexander Graf1b2fd702009-02-02 16:23:51 +0100762 if (efer & EFER_FFXSR) {
763 struct kvm_cpuid_entry2 *feat;
764
765 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200766 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
767 return 1;
Alexander Graf1b2fd702009-02-02 16:23:51 +0100768 }
769
Alexander Grafd8017472008-11-25 20:17:11 +0100770 if (efer & EFER_SVME) {
771 struct kvm_cpuid_entry2 *feat;
772
773 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200774 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
775 return 1;
Alexander Grafd8017472008-11-25 20:17:11 +0100776 }
777
Carsten Otte15c4a642007-10-30 18:44:17 +0100778 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200779 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100780
Sheng Yanga3d204e2010-05-12 16:40:40 +0800781 kvm_x86_ops->set_efer(vcpu, efer);
782
Avi Kivity9645bb562009-03-31 11:31:54 +0300783 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
784 kvm_mmu_reset_context(vcpu);
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200785
Sheng Yangaad82702010-05-12 16:40:42 +0800786 /* Update reserved bits */
787 if ((efer ^ old_efer) & EFER_NX)
788 kvm_mmu_reset_context(vcpu);
789
Roedel, Joergb69e8ca2010-05-06 11:38:43 +0200790 return 0;
Carsten Otte15c4a642007-10-30 18:44:17 +0100791}
792
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100793void kvm_enable_efer_bits(u64 mask)
794{
795 efer_reserved_bits &= ~mask;
796}
797EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
798
799
Carsten Otte15c4a642007-10-30 18:44:17 +0100800/*
801 * Writes msr value into into the appropriate "register".
802 * Returns 0 on success, non-0 otherwise.
803 * Assumes vcpu_load() was already called.
804 */
805int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
806{
807 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
808}
809
Carsten Otte313a3dc2007-10-11 19:16:52 +0200810/*
811 * Adapt set_msr() to msr_io()'s calling convention
812 */
813static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
814{
815 return kvm_set_msr(vcpu, index, *data);
816}
817
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200818static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
819{
Avi Kivity9ed3c442010-05-04 15:00:37 +0300820 int version;
821 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200822 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800823 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200824
825 if (!wall_clock)
826 return;
827
Avi Kivity9ed3c442010-05-04 15:00:37 +0300828 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
829 if (r)
830 return;
831
832 if (version & 1)
833 ++version; /* first time write, random junk */
834
835 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200836
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200837 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
838
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200839 /*
840 * The guest calculates current wall clock time by adding
841 * system time (updated by kvm_write_guest_time below) to the
842 * wall clock specified here. guest system time equals host
843 * system time for us, thus we must fill in host boot time here.
844 */
Jason Wang923de3c2010-01-27 19:13:49 +0800845 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200846
847 wc.sec = boot.tv_sec;
848 wc.nsec = boot.tv_nsec;
849 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200850
851 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
852
853 version++;
854 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200855}
856
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200857static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
858{
859 uint32_t quotient, remainder;
860
861 /* Don't try to replace with do_div(), this one calculates
862 * "(dividend << 32) / divisor" */
863 __asm__ ( "divl %4"
864 : "=a" (quotient), "=d" (remainder)
865 : "0" (0), "1" (dividend), "r" (divisor) );
866 return quotient;
867}
868
869static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
870{
871 uint64_t nsecs = 1000000000LL;
872 int32_t shift = 0;
873 uint64_t tps64;
874 uint32_t tps32;
875
876 tps64 = tsc_khz * 1000LL;
877 while (tps64 > nsecs*2) {
878 tps64 >>= 1;
879 shift--;
880 }
881
882 tps32 = (uint32_t)tps64;
883 while (tps32 <= (uint32_t)nsecs) {
884 tps32 <<= 1;
885 shift++;
886 }
887
888 hv_clock->tsc_shift = shift;
889 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
890
891 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700892 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200893 hv_clock->tsc_to_system_mul);
894}
895
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100896static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
897
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200898static void kvm_write_guest_time(struct kvm_vcpu *v)
899{
900 struct timespec ts;
901 unsigned long flags;
902 struct kvm_vcpu_arch *vcpu = &v->arch;
903 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300904 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200905
906 if ((!vcpu->time_page))
907 return;
908
Avi Kivity463656c2009-04-12 15:49:07 +0300909 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
910 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
911 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
912 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200913 }
Avi Kivity463656c2009-04-12 15:49:07 +0300914 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200915
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200916 /* Keep irq disabled to prevent changes to the clock */
917 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530918 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200919 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800920 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200921 local_irq_restore(flags);
922
923 /* With all the info we got, fill in the values */
924
925 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400926 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
927
Glauber Costa371bcf62010-05-11 12:17:46 -0400928 vcpu->hv_clock.flags = 0;
929
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200930 /*
931 * The interface expects us to write an even number signaling that the
932 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200933 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200934 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200935 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200936
937 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
938
939 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200940 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200941
942 kunmap_atomic(shared_kaddr, KM_USER0);
943
944 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
945}
946
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100947static int kvm_request_guest_time_update(struct kvm_vcpu *v)
948{
949 struct kvm_vcpu_arch *vcpu = &v->arch;
950
951 if (!vcpu->time_page)
952 return 0;
Avi Kivitya8eeb042010-05-10 12:34:53 +0300953 kvm_make_request(KVM_REQ_KVMCLOCK_UPDATE, v);
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100954 return 1;
955}
956
Avi Kivity9ba075a2008-05-26 20:06:35 +0300957static bool msr_mtrr_valid(unsigned msr)
958{
959 switch (msr) {
960 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
961 case MSR_MTRRfix64K_00000:
962 case MSR_MTRRfix16K_80000:
963 case MSR_MTRRfix16K_A0000:
964 case MSR_MTRRfix4K_C0000:
965 case MSR_MTRRfix4K_C8000:
966 case MSR_MTRRfix4K_D0000:
967 case MSR_MTRRfix4K_D8000:
968 case MSR_MTRRfix4K_E0000:
969 case MSR_MTRRfix4K_E8000:
970 case MSR_MTRRfix4K_F0000:
971 case MSR_MTRRfix4K_F8000:
972 case MSR_MTRRdefType:
973 case MSR_IA32_CR_PAT:
974 return true;
975 case 0x2f8:
976 return true;
977 }
978 return false;
979}
980
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300981static bool valid_pat_type(unsigned t)
982{
983 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
984}
985
986static bool valid_mtrr_type(unsigned t)
987{
988 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
989}
990
991static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
992{
993 int i;
994
995 if (!msr_mtrr_valid(msr))
996 return false;
997
998 if (msr == MSR_IA32_CR_PAT) {
999 for (i = 0; i < 8; i++)
1000 if (!valid_pat_type((data >> (i * 8)) & 0xff))
1001 return false;
1002 return true;
1003 } else if (msr == MSR_MTRRdefType) {
1004 if (data & ~0xcff)
1005 return false;
1006 return valid_mtrr_type(data & 0xff);
1007 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
1008 for (i = 0; i < 8 ; i++)
1009 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
1010 return false;
1011 return true;
1012 }
1013
1014 /* variable MTRRs */
1015 return valid_mtrr_type(data & 0xff);
1016}
1017
Avi Kivity9ba075a2008-05-26 20:06:35 +03001018static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1019{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001020 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1021
Marcelo Tosattid6289b92009-06-22 15:27:56 -03001022 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +03001023 return 1;
1024
Sheng Yang0bed3b52008-10-09 16:01:54 +08001025 if (msr == MSR_MTRRdefType) {
1026 vcpu->arch.mtrr_state.def_type = data;
1027 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
1028 } else if (msr == MSR_MTRRfix64K_00000)
1029 p[0] = data;
1030 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1031 p[1 + msr - MSR_MTRRfix16K_80000] = data;
1032 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1033 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
1034 else if (msr == MSR_IA32_CR_PAT)
1035 vcpu->arch.pat = data;
1036 else { /* Variable MTRRs */
1037 int idx, is_mtrr_mask;
1038 u64 *pt;
1039
1040 idx = (msr - 0x200) / 2;
1041 is_mtrr_mask = msr - 0x200 - 2 * idx;
1042 if (!is_mtrr_mask)
1043 pt =
1044 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1045 else
1046 pt =
1047 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1048 *pt = data;
1049 }
1050
1051 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +03001052 return 0;
1053}
Carsten Otte15c4a642007-10-30 18:44:17 +01001054
Huang Ying890ca9a2009-05-11 16:48:15 +08001055static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1056{
1057 u64 mcg_cap = vcpu->arch.mcg_cap;
1058 unsigned bank_num = mcg_cap & 0xff;
1059
1060 switch (msr) {
1061 case MSR_IA32_MCG_STATUS:
1062 vcpu->arch.mcg_status = data;
1063 break;
1064 case MSR_IA32_MCG_CTL:
1065 if (!(mcg_cap & MCG_CTL_P))
1066 return 1;
1067 if (data != 0 && data != ~(u64)0)
1068 return -1;
1069 vcpu->arch.mcg_ctl = data;
1070 break;
1071 default:
1072 if (msr >= MSR_IA32_MC0_CTL &&
1073 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1074 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001075 /* only 0 or all 1s can be written to IA32_MCi_CTL
1076 * some Linux kernels though clear bit 10 in bank 4 to
1077 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1078 * this to avoid an uncatched #GP in the guest
1079 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001080 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001081 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001082 return -1;
1083 vcpu->arch.mce_banks[offset] = data;
1084 break;
1085 }
1086 return 1;
1087 }
1088 return 0;
1089}
1090
Ed Swierkffde22a2009-10-15 15:21:43 -07001091static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1092{
1093 struct kvm *kvm = vcpu->kvm;
1094 int lm = is_long_mode(vcpu);
1095 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1096 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1097 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1098 : kvm->arch.xen_hvm_config.blob_size_32;
1099 u32 page_num = data & ~PAGE_MASK;
1100 u64 page_addr = data & PAGE_MASK;
1101 u8 *page;
1102 int r;
1103
1104 r = -E2BIG;
1105 if (page_num >= blob_size)
1106 goto out;
1107 r = -ENOMEM;
1108 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1109 if (!page)
1110 goto out;
1111 r = -EFAULT;
1112 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
1113 goto out_free;
1114 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1115 goto out_free;
1116 r = 0;
1117out_free:
1118 kfree(page);
1119out:
1120 return r;
1121}
1122
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001123static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1124{
1125 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1126}
1127
1128static bool kvm_hv_msr_partition_wide(u32 msr)
1129{
1130 bool r = false;
1131 switch (msr) {
1132 case HV_X64_MSR_GUEST_OS_ID:
1133 case HV_X64_MSR_HYPERCALL:
1134 r = true;
1135 break;
1136 }
1137
1138 return r;
1139}
1140
1141static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1142{
1143 struct kvm *kvm = vcpu->kvm;
1144
1145 switch (msr) {
1146 case HV_X64_MSR_GUEST_OS_ID:
1147 kvm->arch.hv_guest_os_id = data;
1148 /* setting guest os id to zero disables hypercall page */
1149 if (!kvm->arch.hv_guest_os_id)
1150 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1151 break;
1152 case HV_X64_MSR_HYPERCALL: {
1153 u64 gfn;
1154 unsigned long addr;
1155 u8 instructions[4];
1156
1157 /* if guest os id is not set hypercall should remain disabled */
1158 if (!kvm->arch.hv_guest_os_id)
1159 break;
1160 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1161 kvm->arch.hv_hypercall = data;
1162 break;
1163 }
1164 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1165 addr = gfn_to_hva(kvm, gfn);
1166 if (kvm_is_error_hva(addr))
1167 return 1;
1168 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1169 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1170 if (copy_to_user((void __user *)addr, instructions, 4))
1171 return 1;
1172 kvm->arch.hv_hypercall = data;
1173 break;
1174 }
1175 default:
1176 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1177 "data 0x%llx\n", msr, data);
1178 return 1;
1179 }
1180 return 0;
1181}
1182
1183static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1184{
Gleb Natapov10388a02010-01-17 15:51:23 +02001185 switch (msr) {
1186 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1187 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001188
Gleb Natapov10388a02010-01-17 15:51:23 +02001189 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1190 vcpu->arch.hv_vapic = data;
1191 break;
1192 }
1193 addr = gfn_to_hva(vcpu->kvm, data >>
1194 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1195 if (kvm_is_error_hva(addr))
1196 return 1;
1197 if (clear_user((void __user *)addr, PAGE_SIZE))
1198 return 1;
1199 vcpu->arch.hv_vapic = data;
1200 break;
1201 }
1202 case HV_X64_MSR_EOI:
1203 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1204 case HV_X64_MSR_ICR:
1205 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1206 case HV_X64_MSR_TPR:
1207 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1208 default:
1209 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1210 "data 0x%llx\n", msr, data);
1211 return 1;
1212 }
1213
1214 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001215}
1216
Carsten Otte15c4a642007-10-30 18:44:17 +01001217int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1218{
1219 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001220 case MSR_EFER:
Roedel, Joergb69e8ca2010-05-06 11:38:43 +02001221 return set_efer(vcpu, data);
Andre Przywara8f1589d2009-06-24 12:44:33 +02001222 case MSR_K7_HWCR:
1223 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01001224 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02001225 if (data != 0) {
1226 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1227 data);
1228 return 1;
1229 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001230 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001231 case MSR_FAM10H_MMIO_CONF_BASE:
1232 if (data != 0) {
1233 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1234 "0x%llx\n", data);
1235 return 1;
1236 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001237 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001238 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001239 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001240 case MSR_IA32_DEBUGCTLMSR:
1241 if (!data) {
1242 /* We support the non-activated case already */
1243 break;
1244 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1245 /* Values other than LBR and BTF are vendor-specific,
1246 thus reserved and should throw a #GP */
1247 return 1;
1248 }
1249 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1250 __func__, data);
1251 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001252 case MSR_IA32_UCODE_REV:
1253 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001254 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001255 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001256 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001257 case 0x200 ... 0x2ff:
1258 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001259 case MSR_IA32_APICBASE:
1260 kvm_set_apic_base(vcpu, data);
1261 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001262 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1263 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001264 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001265 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001266 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04001267 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001268 case MSR_KVM_WALL_CLOCK:
1269 vcpu->kvm->arch.wall_clock = data;
1270 kvm_write_wall_clock(vcpu->kvm, data);
1271 break;
Glauber Costa11c6bff2010-05-11 12:17:41 -04001272 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001273 case MSR_KVM_SYSTEM_TIME: {
1274 if (vcpu->arch.time_page) {
1275 kvm_release_page_dirty(vcpu->arch.time_page);
1276 vcpu->arch.time_page = NULL;
1277 }
1278
1279 vcpu->arch.time = data;
1280
1281 /* we verify if the enable bit is set... */
1282 if (!(data & 1))
1283 break;
1284
1285 /* ...but clean it before doing the actual write */
1286 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1287
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001288 vcpu->arch.time_page =
1289 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001290
1291 if (is_error_page(vcpu->arch.time_page)) {
1292 kvm_release_page_clean(vcpu->arch.time_page);
1293 vcpu->arch.time_page = NULL;
1294 }
1295
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001296 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001297 break;
1298 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001299 case MSR_IA32_MCG_CTL:
1300 case MSR_IA32_MCG_STATUS:
1301 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1302 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001303
1304 /* Performance counters are not protected by a CPUID bit,
1305 * so we should check all of them in the generic path for the sake of
1306 * cross vendor migration.
1307 * Writing a zero into the event select MSRs disables them,
1308 * which we perfectly emulate ;-). Any other value should be at least
1309 * reported, some guests depend on them.
1310 */
1311 case MSR_P6_EVNTSEL0:
1312 case MSR_P6_EVNTSEL1:
1313 case MSR_K7_EVNTSEL0:
1314 case MSR_K7_EVNTSEL1:
1315 case MSR_K7_EVNTSEL2:
1316 case MSR_K7_EVNTSEL3:
1317 if (data != 0)
1318 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1319 "0x%x data 0x%llx\n", msr, data);
1320 break;
1321 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1322 * so we ignore writes to make it happy.
1323 */
1324 case MSR_P6_PERFCTR0:
1325 case MSR_P6_PERFCTR1:
1326 case MSR_K7_PERFCTR0:
1327 case MSR_K7_PERFCTR1:
1328 case MSR_K7_PERFCTR2:
1329 case MSR_K7_PERFCTR3:
1330 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1331 "0x%x data 0x%llx\n", msr, data);
1332 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001333 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1334 if (kvm_hv_msr_partition_wide(msr)) {
1335 int r;
1336 mutex_lock(&vcpu->kvm->lock);
1337 r = set_msr_hyperv_pw(vcpu, msr, data);
1338 mutex_unlock(&vcpu->kvm->lock);
1339 return r;
1340 } else
1341 return set_msr_hyperv(vcpu, msr, data);
1342 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001343 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001344 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1345 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001346 if (!ignore_msrs) {
1347 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1348 msr, data);
1349 return 1;
1350 } else {
1351 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1352 msr, data);
1353 break;
1354 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001355 }
1356 return 0;
1357}
1358EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1359
1360
1361/*
1362 * Reads an msr value (of 'msr_index') into 'pdata'.
1363 * Returns 0 on success, non-0 otherwise.
1364 * Assumes vcpu_load() was already called.
1365 */
1366int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1367{
1368 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1369}
1370
Avi Kivity9ba075a2008-05-26 20:06:35 +03001371static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1372{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001373 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1374
Avi Kivity9ba075a2008-05-26 20:06:35 +03001375 if (!msr_mtrr_valid(msr))
1376 return 1;
1377
Sheng Yang0bed3b52008-10-09 16:01:54 +08001378 if (msr == MSR_MTRRdefType)
1379 *pdata = vcpu->arch.mtrr_state.def_type +
1380 (vcpu->arch.mtrr_state.enabled << 10);
1381 else if (msr == MSR_MTRRfix64K_00000)
1382 *pdata = p[0];
1383 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1384 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1385 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1386 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1387 else if (msr == MSR_IA32_CR_PAT)
1388 *pdata = vcpu->arch.pat;
1389 else { /* Variable MTRRs */
1390 int idx, is_mtrr_mask;
1391 u64 *pt;
1392
1393 idx = (msr - 0x200) / 2;
1394 is_mtrr_mask = msr - 0x200 - 2 * idx;
1395 if (!is_mtrr_mask)
1396 pt =
1397 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1398 else
1399 pt =
1400 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1401 *pdata = *pt;
1402 }
1403
Avi Kivity9ba075a2008-05-26 20:06:35 +03001404 return 0;
1405}
1406
Huang Ying890ca9a2009-05-11 16:48:15 +08001407static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1408{
1409 u64 data;
1410 u64 mcg_cap = vcpu->arch.mcg_cap;
1411 unsigned bank_num = mcg_cap & 0xff;
1412
1413 switch (msr) {
1414 case MSR_IA32_P5_MC_ADDR:
1415 case MSR_IA32_P5_MC_TYPE:
1416 data = 0;
1417 break;
1418 case MSR_IA32_MCG_CAP:
1419 data = vcpu->arch.mcg_cap;
1420 break;
1421 case MSR_IA32_MCG_CTL:
1422 if (!(mcg_cap & MCG_CTL_P))
1423 return 1;
1424 data = vcpu->arch.mcg_ctl;
1425 break;
1426 case MSR_IA32_MCG_STATUS:
1427 data = vcpu->arch.mcg_status;
1428 break;
1429 default:
1430 if (msr >= MSR_IA32_MC0_CTL &&
1431 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1432 u32 offset = msr - MSR_IA32_MC0_CTL;
1433 data = vcpu->arch.mce_banks[offset];
1434 break;
1435 }
1436 return 1;
1437 }
1438 *pdata = data;
1439 return 0;
1440}
1441
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001442static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1443{
1444 u64 data = 0;
1445 struct kvm *kvm = vcpu->kvm;
1446
1447 switch (msr) {
1448 case HV_X64_MSR_GUEST_OS_ID:
1449 data = kvm->arch.hv_guest_os_id;
1450 break;
1451 case HV_X64_MSR_HYPERCALL:
1452 data = kvm->arch.hv_hypercall;
1453 break;
1454 default:
1455 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1456 return 1;
1457 }
1458
1459 *pdata = data;
1460 return 0;
1461}
1462
1463static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1464{
1465 u64 data = 0;
1466
1467 switch (msr) {
1468 case HV_X64_MSR_VP_INDEX: {
1469 int r;
1470 struct kvm_vcpu *v;
1471 kvm_for_each_vcpu(r, v, vcpu->kvm)
1472 if (v == vcpu)
1473 data = r;
1474 break;
1475 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001476 case HV_X64_MSR_EOI:
1477 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1478 case HV_X64_MSR_ICR:
1479 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1480 case HV_X64_MSR_TPR:
1481 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001482 default:
1483 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1484 return 1;
1485 }
1486 *pdata = data;
1487 return 0;
1488}
1489
Carsten Otte15c4a642007-10-30 18:44:17 +01001490int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1491{
1492 u64 data;
1493
1494 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001495 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001496 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001497 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001498 case MSR_IA32_DEBUGCTLMSR:
1499 case MSR_IA32_LASTBRANCHFROMIP:
1500 case MSR_IA32_LASTBRANCHTOIP:
1501 case MSR_IA32_LASTINTFROMIP:
1502 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301503 case MSR_K8_SYSCFG:
1504 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001505 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301506 case MSR_P6_PERFCTR0:
1507 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301508 case MSR_P6_EVNTSEL0:
1509 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301510 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301511 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001512 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001513 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001514 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001515 data = 0;
1516 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001517 case MSR_MTRRcap:
1518 data = 0x500 | KVM_NR_VAR_MTRR;
1519 break;
1520 case 0x200 ... 0x2ff:
1521 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001522 case 0xcd: /* fsb frequency */
1523 data = 3;
1524 break;
1525 case MSR_IA32_APICBASE:
1526 data = kvm_get_apic_base(vcpu);
1527 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001528 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1529 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1530 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001531 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001532 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001533 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001534 case MSR_IA32_PERF_STATUS:
1535 /* TSC increment by tick */
1536 data = 1000ULL;
1537 /* CPU multiplier */
1538 data |= (((uint64_t)4ULL) << 40);
1539 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001540 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001541 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001542 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001543 case MSR_KVM_WALL_CLOCK:
Glauber Costa11c6bff2010-05-11 12:17:41 -04001544 case MSR_KVM_WALL_CLOCK_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001545 data = vcpu->kvm->arch.wall_clock;
1546 break;
1547 case MSR_KVM_SYSTEM_TIME:
Glauber Costa11c6bff2010-05-11 12:17:41 -04001548 case MSR_KVM_SYSTEM_TIME_NEW:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001549 data = vcpu->arch.time;
1550 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001551 case MSR_IA32_P5_MC_ADDR:
1552 case MSR_IA32_P5_MC_TYPE:
1553 case MSR_IA32_MCG_CAP:
1554 case MSR_IA32_MCG_CTL:
1555 case MSR_IA32_MCG_STATUS:
1556 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1557 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001558 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1559 if (kvm_hv_msr_partition_wide(msr)) {
1560 int r;
1561 mutex_lock(&vcpu->kvm->lock);
1562 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1563 mutex_unlock(&vcpu->kvm->lock);
1564 return r;
1565 } else
1566 return get_msr_hyperv(vcpu, msr, pdata);
1567 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001568 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001569 if (!ignore_msrs) {
1570 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1571 return 1;
1572 } else {
1573 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1574 data = 0;
1575 }
1576 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001577 }
1578 *pdata = data;
1579 return 0;
1580}
1581EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1582
Carsten Otte313a3dc2007-10-11 19:16:52 +02001583/*
1584 * Read or write a bunch of msrs. All parameters are kernel addresses.
1585 *
1586 * @return number of msrs set successfully.
1587 */
1588static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1589 struct kvm_msr_entry *entries,
1590 int (*do_msr)(struct kvm_vcpu *vcpu,
1591 unsigned index, u64 *data))
1592{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001593 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001594
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001595 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001596 for (i = 0; i < msrs->nmsrs; ++i)
1597 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1598 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001599 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001600
Carsten Otte313a3dc2007-10-11 19:16:52 +02001601 return i;
1602}
1603
1604/*
1605 * Read or write a bunch of msrs. Parameters are user addresses.
1606 *
1607 * @return number of msrs set successfully.
1608 */
1609static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1610 int (*do_msr)(struct kvm_vcpu *vcpu,
1611 unsigned index, u64 *data),
1612 int writeback)
1613{
1614 struct kvm_msrs msrs;
1615 struct kvm_msr_entry *entries;
1616 int r, n;
1617 unsigned size;
1618
1619 r = -EFAULT;
1620 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1621 goto out;
1622
1623 r = -E2BIG;
1624 if (msrs.nmsrs >= MAX_IO_MSRS)
1625 goto out;
1626
1627 r = -ENOMEM;
1628 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
Avi Kivity7a73c022010-07-22 23:24:52 +03001629 entries = kmalloc(size, GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001630 if (!entries)
1631 goto out;
1632
1633 r = -EFAULT;
1634 if (copy_from_user(entries, user_msrs->entries, size))
1635 goto out_free;
1636
1637 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1638 if (r < 0)
1639 goto out_free;
1640
1641 r = -EFAULT;
1642 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1643 goto out_free;
1644
1645 r = n;
1646
1647out_free:
Avi Kivity7a73c022010-07-22 23:24:52 +03001648 kfree(entries);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001649out:
1650 return r;
1651}
1652
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001653int kvm_dev_ioctl_check_extension(long ext)
1654{
1655 int r;
1656
1657 switch (ext) {
1658 case KVM_CAP_IRQCHIP:
1659 case KVM_CAP_HLT:
1660 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001661 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001662 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001663 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001664 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001665 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001666 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001667 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001668 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001669 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001670 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001671 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001672 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001673 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001674 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001675 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001676 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001677 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001678 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001679 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001680 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001681 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001682 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01001683 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001684 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Sheng Yang2d5b5a62010-06-13 17:29:39 +08001685 case KVM_CAP_XSAVE:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001686 r = 1;
1687 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001688 case KVM_CAP_COALESCED_MMIO:
1689 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1690 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001691 case KVM_CAP_VAPIC:
1692 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1693 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001694 case KVM_CAP_NR_VCPUS:
1695 r = KVM_MAX_VCPUS;
1696 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001697 case KVM_CAP_NR_MEMSLOTS:
1698 r = KVM_MEMORY_SLOTS;
1699 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001700 case KVM_CAP_PV_MMU: /* obsolete */
1701 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001702 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001703 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001704 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001705 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001706 case KVM_CAP_MCE:
1707 r = KVM_MAX_MCE_BANKS;
1708 break;
Sheng Yang2d5b5a62010-06-13 17:29:39 +08001709 case KVM_CAP_XCRS:
1710 r = cpu_has_xsave;
1711 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001712 default:
1713 r = 0;
1714 break;
1715 }
1716 return r;
1717
1718}
1719
Carsten Otte043405e2007-10-10 17:16:19 +02001720long kvm_arch_dev_ioctl(struct file *filp,
1721 unsigned int ioctl, unsigned long arg)
1722{
1723 void __user *argp = (void __user *)arg;
1724 long r;
1725
1726 switch (ioctl) {
1727 case KVM_GET_MSR_INDEX_LIST: {
1728 struct kvm_msr_list __user *user_msr_list = argp;
1729 struct kvm_msr_list msr_list;
1730 unsigned n;
1731
1732 r = -EFAULT;
1733 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1734 goto out;
1735 n = msr_list.nmsrs;
1736 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1737 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1738 goto out;
1739 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001740 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001741 goto out;
1742 r = -EFAULT;
1743 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1744 num_msrs_to_save * sizeof(u32)))
1745 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001746 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001747 &emulated_msrs,
1748 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1749 goto out;
1750 r = 0;
1751 break;
1752 }
Avi Kivity674eea02008-02-11 18:37:23 +02001753 case KVM_GET_SUPPORTED_CPUID: {
1754 struct kvm_cpuid2 __user *cpuid_arg = argp;
1755 struct kvm_cpuid2 cpuid;
1756
1757 r = -EFAULT;
1758 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1759 goto out;
1760 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001761 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001762 if (r)
1763 goto out;
1764
1765 r = -EFAULT;
1766 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1767 goto out;
1768 r = 0;
1769 break;
1770 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001771 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1772 u64 mce_cap;
1773
1774 mce_cap = KVM_MCE_CAP_SUPPORTED;
1775 r = -EFAULT;
1776 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1777 goto out;
1778 r = 0;
1779 break;
1780 }
Carsten Otte043405e2007-10-10 17:16:19 +02001781 default:
1782 r = -EINVAL;
1783 }
1784out:
1785 return r;
1786}
1787
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001788static void wbinvd_ipi(void *garbage)
1789{
1790 wbinvd();
1791}
1792
1793static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
1794{
1795 return vcpu->kvm->arch.iommu_domain &&
1796 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
1797}
1798
Carsten Otte313a3dc2007-10-11 19:16:52 +02001799void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1800{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001801 /* Address WBINVD may be executed by guest */
1802 if (need_emulate_wbinvd(vcpu)) {
1803 if (kvm_x86_ops->has_wbinvd_exit())
1804 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
1805 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
1806 smp_call_function_single(vcpu->cpu,
1807 wbinvd_ipi, NULL, 1);
1808 }
1809
Carsten Otte313a3dc2007-10-11 19:16:52 +02001810 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001811 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1812 unsigned long khz = cpufreq_quick_get(cpu);
1813 if (!khz)
1814 khz = tsc_khz;
1815 per_cpu(cpu_tsc_khz, cpu) = khz;
1816 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001817 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001818}
1819
1820void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1821{
Avi Kivity02daab22009-12-30 12:40:26 +02001822 kvm_x86_ops->vcpu_put(vcpu);
Avi Kivity1c11e712010-05-03 16:05:44 +03001823 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001824}
1825
Dan Kenigsberg07716712007-11-21 17:10:04 +02001826static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001827{
Avi Kivitye286e862009-05-03 18:50:55 +03001828 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001829
Avi Kivitye286e862009-05-03 18:50:55 +03001830 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001831 return efer & EFER_NX;
1832}
1833
1834static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1835{
1836 int i;
1837 struct kvm_cpuid_entry2 *e, *entry;
1838
Carsten Otte313a3dc2007-10-11 19:16:52 +02001839 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001840 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1841 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001842 if (e->function == 0x80000001) {
1843 entry = e;
1844 break;
1845 }
1846 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001847 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001848 entry->edx &= ~(1 << 20);
1849 printk(KERN_INFO "kvm: guest NX capability removed\n");
1850 }
1851}
1852
Dan Kenigsberg07716712007-11-21 17:10:04 +02001853/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001854static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1855 struct kvm_cpuid *cpuid,
1856 struct kvm_cpuid_entry __user *entries)
1857{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001858 int r, i;
1859 struct kvm_cpuid_entry *cpuid_entries;
1860
1861 r = -E2BIG;
1862 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1863 goto out;
1864 r = -ENOMEM;
1865 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1866 if (!cpuid_entries)
1867 goto out;
1868 r = -EFAULT;
1869 if (copy_from_user(cpuid_entries, entries,
1870 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1871 goto out_free;
1872 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001873 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1874 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1875 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1876 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1877 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1878 vcpu->arch.cpuid_entries[i].index = 0;
1879 vcpu->arch.cpuid_entries[i].flags = 0;
1880 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1881 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1882 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001883 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001884 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001885 cpuid_fix_nx_cap(vcpu);
1886 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001887 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001888 kvm_x86_ops->cpuid_update(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001889 update_cpuid(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001890
1891out_free:
1892 vfree(cpuid_entries);
1893out:
1894 return r;
1895}
1896
1897static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001898 struct kvm_cpuid2 *cpuid,
1899 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001900{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001901 int r;
1902
1903 r = -E2BIG;
1904 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1905 goto out;
1906 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001907 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001908 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001909 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001910 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001911 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001912 kvm_x86_ops->cpuid_update(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08001913 update_cpuid(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001914 return 0;
1915
1916out:
1917 return r;
1918}
1919
Dan Kenigsberg07716712007-11-21 17:10:04 +02001920static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001921 struct kvm_cpuid2 *cpuid,
1922 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001923{
1924 int r;
1925
1926 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001927 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001928 goto out;
1929 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001930 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001931 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001932 goto out;
1933 return 0;
1934
1935out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001936 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001937 return r;
1938}
1939
Dan Kenigsberg07716712007-11-21 17:10:04 +02001940static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001941 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001942{
1943 entry->function = function;
1944 entry->index = index;
1945 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001946 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001947 entry->flags = 0;
1948}
1949
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001950#define F(x) bit(X86_FEATURE_##x)
1951
Dan Kenigsberg07716712007-11-21 17:10:04 +02001952static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1953 u32 index, int *nent, int maxnent)
1954{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001955 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001956#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001957 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1958 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001959 unsigned f_lm = F(LM);
1960#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001961 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001962 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001963#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001964 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001965
1966 /* cpuid 1.edx */
1967 const u32 kvm_supported_word0_x86_features =
1968 F(FPU) | F(VME) | F(DE) | F(PSE) |
1969 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1970 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1971 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1972 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1973 0 /* Reserved, DS, ACPI */ | F(MMX) |
1974 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1975 0 /* HTT, TM, Reserved, PBE */;
1976 /* cpuid 0x80000001.edx */
1977 const u32 kvm_supported_word1_x86_features =
1978 F(FPU) | F(VME) | F(DE) | F(PSE) |
1979 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1980 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1981 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1982 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1983 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001984 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001985 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1986 /* cpuid 1.ecx */
1987 const u32 kvm_supported_word4_x86_features =
Sheng Yang6c3f6042010-06-22 13:49:21 +08001988 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
Avi Kivityd149c732009-05-10 14:41:56 +03001989 0 /* DS-CPL, VMX, SMX, EST */ |
1990 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1991 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1992 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001993 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Andre Przywara6d886fd2010-09-06 15:14:19 +02001994 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
1995 F(F16C);
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001996 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001997 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001998 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1999 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
Andre Przywara7ef8aa72010-09-06 15:14:17 +02002000 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(XOP) |
Andre Przywara6d886fd2010-09-06 15:14:19 +02002001 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002002
Amit Shah19355472009-01-14 16:56:00 +00002003 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02002004 get_cpu();
2005 do_cpuid_1_ent(entry, function, index);
2006 ++*nent;
2007
2008 switch (function) {
2009 case 0:
Dexuan Cui2acf9232010-06-10 11:27:12 +08002010 entry->eax = min(entry->eax, (u32)0xd);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002011 break;
2012 case 1:
2013 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03002014 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03002015 /* we support x2apic emulation even if host does not support
2016 * it since we emulate x2apic in software */
2017 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002018 break;
2019 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
2020 * may return different values. This forces us to get_cpu() before
2021 * issuing the first command, and also to emulate this annoying behavior
2022 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
2023 case 2: {
2024 int t, times = entry->eax & 0xff;
2025
2026 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08002027 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002028 for (t = 1; t < times && *nent < maxnent; ++t) {
2029 do_cpuid_1_ent(&entry[t], function, 0);
2030 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
2031 ++*nent;
2032 }
2033 break;
2034 }
2035 /* function 4 and 0xb have additional index. */
2036 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08002037 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002038
2039 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2040 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08002041 for (i = 1; *nent < maxnent; ++i) {
2042 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002043 if (!cache_type)
2044 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08002045 do_cpuid_1_ent(&entry[i], function, i);
2046 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02002047 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2048 ++*nent;
2049 }
2050 break;
2051 }
2052 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08002053 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002054
2055 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2056 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08002057 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08002058 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002059 if (!level_type)
2060 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08002061 do_cpuid_1_ent(&entry[i], function, i);
2062 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02002063 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2064 ++*nent;
2065 }
2066 break;
2067 }
Dexuan Cui2acf9232010-06-10 11:27:12 +08002068 case 0xd: {
2069 int i;
2070
2071 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2072 for (i = 1; *nent < maxnent; ++i) {
2073 if (entry[i - 1].eax == 0 && i != 2)
2074 break;
2075 do_cpuid_1_ent(&entry[i], function, i);
2076 entry[i].flags |=
2077 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
2078 ++*nent;
2079 }
2080 break;
2081 }
Glauber Costa84478c82010-05-11 12:17:43 -04002082 case KVM_CPUID_SIGNATURE: {
2083 char signature[12] = "KVMKVMKVM\0\0";
2084 u32 *sigptr = (u32 *)signature;
2085 entry->eax = 0;
2086 entry->ebx = sigptr[0];
2087 entry->ecx = sigptr[1];
2088 entry->edx = sigptr[2];
2089 break;
2090 }
2091 case KVM_CPUID_FEATURES:
2092 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
2093 (1 << KVM_FEATURE_NOP_IO_DELAY) |
Glauber Costa371bcf62010-05-11 12:17:46 -04002094 (1 << KVM_FEATURE_CLOCKSOURCE2) |
2095 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
Glauber Costa84478c82010-05-11 12:17:43 -04002096 entry->ebx = 0;
2097 entry->ecx = 0;
2098 entry->edx = 0;
2099 break;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002100 case 0x80000000:
2101 entry->eax = min(entry->eax, 0x8000001a);
2102 break;
2103 case 0x80000001:
2104 entry->edx &= kvm_supported_word1_x86_features;
2105 entry->ecx &= kvm_supported_word6_x86_features;
2106 break;
2107 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02002108
2109 kvm_x86_ops->set_supported_cpuid(function, entry);
2110
Dan Kenigsberg07716712007-11-21 17:10:04 +02002111 put_cpu();
2112}
2113
Avi Kivity7faa4ee2009-05-10 13:55:35 +03002114#undef F
2115
Avi Kivity674eea02008-02-11 18:37:23 +02002116static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002117 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02002118{
2119 struct kvm_cpuid_entry2 *cpuid_entries;
2120 int limit, nent = 0, r = -E2BIG;
2121 u32 func;
2122
2123 if (cpuid->nent < 1)
2124 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02002125 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2126 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002127 r = -ENOMEM;
2128 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
2129 if (!cpuid_entries)
2130 goto out;
2131
2132 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
2133 limit = cpuid_entries[0].eax;
2134 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
2135 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00002136 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002137 r = -E2BIG;
2138 if (nent >= cpuid->nent)
2139 goto out_free;
2140
2141 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
2142 limit = cpuid_entries[nent - 1].eax;
2143 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
2144 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00002145 &nent, cpuid->nent);
Glauber Costa84478c82010-05-11 12:17:43 -04002146
2147
2148
2149 r = -E2BIG;
2150 if (nent >= cpuid->nent)
2151 goto out_free;
2152
2153 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
2154 cpuid->nent);
2155
2156 r = -E2BIG;
2157 if (nent >= cpuid->nent)
2158 goto out_free;
2159
2160 do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
2161 cpuid->nent);
2162
Mark McLoughlincb007642009-05-12 12:36:44 +01002163 r = -E2BIG;
2164 if (nent >= cpuid->nent)
2165 goto out_free;
2166
Dan Kenigsberg07716712007-11-21 17:10:04 +02002167 r = -EFAULT;
2168 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00002169 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02002170 goto out_free;
2171 cpuid->nent = nent;
2172 r = 0;
2173
2174out_free:
2175 vfree(cpuid_entries);
2176out:
2177 return r;
2178}
2179
Carsten Otte313a3dc2007-10-11 19:16:52 +02002180static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2181 struct kvm_lapic_state *s)
2182{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002184
2185 return 0;
2186}
2187
2188static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2189 struct kvm_lapic_state *s)
2190{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002191 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002192 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002193 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002194
2195 return 0;
2196}
2197
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002198static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2199 struct kvm_interrupt *irq)
2200{
2201 if (irq->irq < 0 || irq->irq >= 256)
2202 return -EINVAL;
2203 if (irqchip_in_kernel(vcpu->kvm))
2204 return -ENXIO;
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002205
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002206 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002207
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002208 return 0;
2209}
2210
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002211static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2212{
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002213 kvm_inject_nmi(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002214
2215 return 0;
2216}
2217
Avi Kivityb209749f2007-10-22 16:50:39 +02002218static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2219 struct kvm_tpr_access_ctl *tac)
2220{
2221 if (tac->flags)
2222 return -EINVAL;
2223 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2224 return 0;
2225}
2226
Huang Ying890ca9a2009-05-11 16:48:15 +08002227static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2228 u64 mcg_cap)
2229{
2230 int r;
2231 unsigned bank_num = mcg_cap & 0xff, bank;
2232
2233 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002234 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002235 goto out;
2236 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2237 goto out;
2238 r = 0;
2239 vcpu->arch.mcg_cap = mcg_cap;
2240 /* Init IA32_MCG_CTL to all 1s */
2241 if (mcg_cap & MCG_CTL_P)
2242 vcpu->arch.mcg_ctl = ~(u64)0;
2243 /* Init IA32_MCi_CTL to all 1s */
2244 for (bank = 0; bank < bank_num; bank++)
2245 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2246out:
2247 return r;
2248}
2249
2250static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2251 struct kvm_x86_mce *mce)
2252{
2253 u64 mcg_cap = vcpu->arch.mcg_cap;
2254 unsigned bank_num = mcg_cap & 0xff;
2255 u64 *banks = vcpu->arch.mce_banks;
2256
2257 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2258 return -EINVAL;
2259 /*
2260 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2261 * reporting is disabled
2262 */
2263 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2264 vcpu->arch.mcg_ctl != ~(u64)0)
2265 return 0;
2266 banks += 4 * mce->bank;
2267 /*
2268 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2269 * reporting is disabled for the bank
2270 */
2271 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2272 return 0;
2273 if (mce->status & MCI_STATUS_UC) {
2274 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002275 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002276 printk(KERN_DEBUG "kvm: set_mce: "
2277 "injects mce exception while "
2278 "previous one is in progress!\n");
Avi Kivitya8eeb042010-05-10 12:34:53 +03002279 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Huang Ying890ca9a2009-05-11 16:48:15 +08002280 return 0;
2281 }
2282 if (banks[1] & MCI_STATUS_VAL)
2283 mce->status |= MCI_STATUS_OVER;
2284 banks[2] = mce->addr;
2285 banks[3] = mce->misc;
2286 vcpu->arch.mcg_status = mce->mcg_status;
2287 banks[1] = mce->status;
2288 kvm_queue_exception(vcpu, MC_VECTOR);
2289 } else if (!(banks[1] & MCI_STATUS_VAL)
2290 || !(banks[1] & MCI_STATUS_UC)) {
2291 if (banks[1] & MCI_STATUS_VAL)
2292 mce->status |= MCI_STATUS_OVER;
2293 banks[2] = mce->addr;
2294 banks[3] = mce->misc;
2295 banks[1] = mce->status;
2296 } else
2297 banks[1] |= MCI_STATUS_OVER;
2298 return 0;
2299}
2300
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002301static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2302 struct kvm_vcpu_events *events)
2303{
Jan Kiszka03b82a32010-02-15 10:45:41 +01002304 events->exception.injected =
2305 vcpu->arch.exception.pending &&
2306 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002307 events->exception.nr = vcpu->arch.exception.nr;
2308 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2309 events->exception.error_code = vcpu->arch.exception.error_code;
2310
Jan Kiszka03b82a32010-02-15 10:45:41 +01002311 events->interrupt.injected =
2312 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002313 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002314 events->interrupt.soft = 0;
Jan Kiszka48005f62010-02-19 19:38:07 +01002315 events->interrupt.shadow =
2316 kvm_x86_ops->get_interrupt_shadow(vcpu,
2317 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002318
2319 events->nmi.injected = vcpu->arch.nmi_injected;
2320 events->nmi.pending = vcpu->arch.nmi_pending;
2321 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2322
2323 events->sipi_vector = vcpu->arch.sipi_vector;
2324
Jan Kiszkadab4b912009-12-06 18:24:15 +01002325 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002326 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2327 | KVM_VCPUEVENT_VALID_SHADOW);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002328}
2329
2330static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2331 struct kvm_vcpu_events *events)
2332{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002333 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002334 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2335 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002336 return -EINVAL;
2337
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002338 vcpu->arch.exception.pending = events->exception.injected;
2339 vcpu->arch.exception.nr = events->exception.nr;
2340 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2341 vcpu->arch.exception.error_code = events->exception.error_code;
2342
2343 vcpu->arch.interrupt.pending = events->interrupt.injected;
2344 vcpu->arch.interrupt.nr = events->interrupt.nr;
2345 vcpu->arch.interrupt.soft = events->interrupt.soft;
2346 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2347 kvm_pic_clear_isr_ack(vcpu->kvm);
Jan Kiszka48005f62010-02-19 19:38:07 +01002348 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2349 kvm_x86_ops->set_interrupt_shadow(vcpu,
2350 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002351
2352 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002353 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2354 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002355 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2356
Jan Kiszkadab4b912009-12-06 18:24:15 +01002357 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2358 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002359
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002360 return 0;
2361}
2362
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002363static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2364 struct kvm_debugregs *dbgregs)
2365{
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002366 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2367 dbgregs->dr6 = vcpu->arch.dr6;
2368 dbgregs->dr7 = vcpu->arch.dr7;
2369 dbgregs->flags = 0;
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002370}
2371
2372static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2373 struct kvm_debugregs *dbgregs)
2374{
2375 if (dbgregs->flags)
2376 return -EINVAL;
2377
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002378 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2379 vcpu->arch.dr6 = dbgregs->dr6;
2380 vcpu->arch.dr7 = dbgregs->dr7;
2381
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002382 return 0;
2383}
2384
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002385static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
2386 struct kvm_xsave *guest_xsave)
2387{
2388 if (cpu_has_xsave)
2389 memcpy(guest_xsave->region,
2390 &vcpu->arch.guest_fpu.state->xsave,
Xiaotian Fengf45755b2010-08-13 15:19:11 +08002391 xstate_size);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002392 else {
2393 memcpy(guest_xsave->region,
2394 &vcpu->arch.guest_fpu.state->fxsave,
2395 sizeof(struct i387_fxsave_struct));
2396 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
2397 XSTATE_FPSSE;
2398 }
2399}
2400
2401static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
2402 struct kvm_xsave *guest_xsave)
2403{
2404 u64 xstate_bv =
2405 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
2406
2407 if (cpu_has_xsave)
2408 memcpy(&vcpu->arch.guest_fpu.state->xsave,
Xiaotian Fengf45755b2010-08-13 15:19:11 +08002409 guest_xsave->region, xstate_size);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002410 else {
2411 if (xstate_bv & ~XSTATE_FPSSE)
2412 return -EINVAL;
2413 memcpy(&vcpu->arch.guest_fpu.state->fxsave,
2414 guest_xsave->region, sizeof(struct i387_fxsave_struct));
2415 }
2416 return 0;
2417}
2418
2419static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
2420 struct kvm_xcrs *guest_xcrs)
2421{
2422 if (!cpu_has_xsave) {
2423 guest_xcrs->nr_xcrs = 0;
2424 return;
2425 }
2426
2427 guest_xcrs->nr_xcrs = 1;
2428 guest_xcrs->flags = 0;
2429 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
2430 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
2431}
2432
2433static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
2434 struct kvm_xcrs *guest_xcrs)
2435{
2436 int i, r = 0;
2437
2438 if (!cpu_has_xsave)
2439 return -EINVAL;
2440
2441 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
2442 return -EINVAL;
2443
2444 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
2445 /* Only support XCR0 currently */
2446 if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) {
2447 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
2448 guest_xcrs->xcrs[0].value);
2449 break;
2450 }
2451 if (r)
2452 r = -EINVAL;
2453 return r;
2454}
2455
Carsten Otte313a3dc2007-10-11 19:16:52 +02002456long kvm_arch_vcpu_ioctl(struct file *filp,
2457 unsigned int ioctl, unsigned long arg)
2458{
2459 struct kvm_vcpu *vcpu = filp->private_data;
2460 void __user *argp = (void __user *)arg;
2461 int r;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002462 union {
2463 struct kvm_lapic_state *lapic;
2464 struct kvm_xsave *xsave;
2465 struct kvm_xcrs *xcrs;
2466 void *buffer;
2467 } u;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002468
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002469 u.buffer = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002470 switch (ioctl) {
2471 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002472 r = -EINVAL;
2473 if (!vcpu->arch.apic)
2474 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002475 u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002476
Dave Hansenb772ff32008-08-11 10:01:47 -07002477 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002478 if (!u.lapic)
Dave Hansenb772ff32008-08-11 10:01:47 -07002479 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002480 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002481 if (r)
2482 goto out;
2483 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002484 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002485 goto out;
2486 r = 0;
2487 break;
2488 }
2489 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002490 r = -EINVAL;
2491 if (!vcpu->arch.apic)
2492 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002493 u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Dave Hansenb772ff32008-08-11 10:01:47 -07002494 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002495 if (!u.lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002496 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002497 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002498 if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state)))
Dave Hansenb772ff32008-08-11 10:01:47 -07002499 goto out;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002500 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002501 if (r)
2502 goto out;
2503 r = 0;
2504 break;
2505 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002506 case KVM_INTERRUPT: {
2507 struct kvm_interrupt irq;
2508
2509 r = -EFAULT;
2510 if (copy_from_user(&irq, argp, sizeof irq))
2511 goto out;
2512 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2513 if (r)
2514 goto out;
2515 r = 0;
2516 break;
2517 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002518 case KVM_NMI: {
2519 r = kvm_vcpu_ioctl_nmi(vcpu);
2520 if (r)
2521 goto out;
2522 r = 0;
2523 break;
2524 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002525 case KVM_SET_CPUID: {
2526 struct kvm_cpuid __user *cpuid_arg = argp;
2527 struct kvm_cpuid cpuid;
2528
2529 r = -EFAULT;
2530 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2531 goto out;
2532 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2533 if (r)
2534 goto out;
2535 break;
2536 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002537 case KVM_SET_CPUID2: {
2538 struct kvm_cpuid2 __user *cpuid_arg = argp;
2539 struct kvm_cpuid2 cpuid;
2540
2541 r = -EFAULT;
2542 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2543 goto out;
2544 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002545 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002546 if (r)
2547 goto out;
2548 break;
2549 }
2550 case KVM_GET_CPUID2: {
2551 struct kvm_cpuid2 __user *cpuid_arg = argp;
2552 struct kvm_cpuid2 cpuid;
2553
2554 r = -EFAULT;
2555 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2556 goto out;
2557 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002558 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002559 if (r)
2560 goto out;
2561 r = -EFAULT;
2562 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2563 goto out;
2564 r = 0;
2565 break;
2566 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002567 case KVM_GET_MSRS:
2568 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2569 break;
2570 case KVM_SET_MSRS:
2571 r = msr_io(vcpu, argp, do_set_msr, 0);
2572 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002573 case KVM_TPR_ACCESS_REPORTING: {
2574 struct kvm_tpr_access_ctl tac;
2575
2576 r = -EFAULT;
2577 if (copy_from_user(&tac, argp, sizeof tac))
2578 goto out;
2579 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2580 if (r)
2581 goto out;
2582 r = -EFAULT;
2583 if (copy_to_user(argp, &tac, sizeof tac))
2584 goto out;
2585 r = 0;
2586 break;
2587 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002588 case KVM_SET_VAPIC_ADDR: {
2589 struct kvm_vapic_addr va;
2590
2591 r = -EINVAL;
2592 if (!irqchip_in_kernel(vcpu->kvm))
2593 goto out;
2594 r = -EFAULT;
2595 if (copy_from_user(&va, argp, sizeof va))
2596 goto out;
2597 r = 0;
2598 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2599 break;
2600 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002601 case KVM_X86_SETUP_MCE: {
2602 u64 mcg_cap;
2603
2604 r = -EFAULT;
2605 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2606 goto out;
2607 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2608 break;
2609 }
2610 case KVM_X86_SET_MCE: {
2611 struct kvm_x86_mce mce;
2612
2613 r = -EFAULT;
2614 if (copy_from_user(&mce, argp, sizeof mce))
2615 goto out;
2616 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2617 break;
2618 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002619 case KVM_GET_VCPU_EVENTS: {
2620 struct kvm_vcpu_events events;
2621
2622 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2623
2624 r = -EFAULT;
2625 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2626 break;
2627 r = 0;
2628 break;
2629 }
2630 case KVM_SET_VCPU_EVENTS: {
2631 struct kvm_vcpu_events events;
2632
2633 r = -EFAULT;
2634 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2635 break;
2636
2637 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2638 break;
2639 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002640 case KVM_GET_DEBUGREGS: {
2641 struct kvm_debugregs dbgregs;
2642
2643 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2644
2645 r = -EFAULT;
2646 if (copy_to_user(argp, &dbgregs,
2647 sizeof(struct kvm_debugregs)))
2648 break;
2649 r = 0;
2650 break;
2651 }
2652 case KVM_SET_DEBUGREGS: {
2653 struct kvm_debugregs dbgregs;
2654
2655 r = -EFAULT;
2656 if (copy_from_user(&dbgregs, argp,
2657 sizeof(struct kvm_debugregs)))
2658 break;
2659
2660 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2661 break;
2662 }
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002663 case KVM_GET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002664 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002665 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002666 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002667 break;
2668
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002669 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002670
2671 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002672 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002673 break;
2674 r = 0;
2675 break;
2676 }
2677 case KVM_SET_XSAVE: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002678 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002679 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002680 if (!u.xsave)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002681 break;
2682
2683 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002684 if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave)))
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002685 break;
2686
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002687 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002688 break;
2689 }
2690 case KVM_GET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002691 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002692 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002693 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002694 break;
2695
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002696 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002697
2698 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002699 if (copy_to_user(argp, u.xcrs,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002700 sizeof(struct kvm_xcrs)))
2701 break;
2702 r = 0;
2703 break;
2704 }
2705 case KVM_SET_XCRS: {
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002706 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002707 r = -ENOMEM;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002708 if (!u.xcrs)
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002709 break;
2710
2711 r = -EFAULT;
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002712 if (copy_from_user(u.xcrs, argp,
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002713 sizeof(struct kvm_xcrs)))
2714 break;
2715
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002716 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
Sheng Yang2d5b5a62010-06-13 17:29:39 +08002717 break;
2718 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002719 default:
2720 r = -EINVAL;
2721 }
2722out:
Avi Kivityd1ac91d2010-06-20 15:54:43 +03002723 kfree(u.buffer);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002724 return r;
2725}
2726
Carsten Otte1fe779f2007-10-29 16:08:35 +01002727static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2728{
2729 int ret;
2730
2731 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2732 return -1;
2733 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2734 return ret;
2735}
2736
Sheng Yangb927a3c2009-07-21 10:42:48 +08002737static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2738 u64 ident_addr)
2739{
2740 kvm->arch.ept_identity_map_addr = ident_addr;
2741 return 0;
2742}
2743
Carsten Otte1fe779f2007-10-29 16:08:35 +01002744static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2745 u32 kvm_nr_mmu_pages)
2746{
2747 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2748 return -EINVAL;
2749
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002750 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002751 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002752
2753 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002754 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002755
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002756 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002757 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002758 return 0;
2759}
2760
2761static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2762{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002763 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002764}
2765
Carsten Otte1fe779f2007-10-29 16:08:35 +01002766static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2767{
2768 int r;
2769
2770 r = 0;
2771 switch (chip->chip_id) {
2772 case KVM_IRQCHIP_PIC_MASTER:
2773 memcpy(&chip->chip.pic,
2774 &pic_irqchip(kvm)->pics[0],
2775 sizeof(struct kvm_pic_state));
2776 break;
2777 case KVM_IRQCHIP_PIC_SLAVE:
2778 memcpy(&chip->chip.pic,
2779 &pic_irqchip(kvm)->pics[1],
2780 sizeof(struct kvm_pic_state));
2781 break;
2782 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002783 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002784 break;
2785 default:
2786 r = -EINVAL;
2787 break;
2788 }
2789 return r;
2790}
2791
2792static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2793{
2794 int r;
2795
2796 r = 0;
2797 switch (chip->chip_id) {
2798 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002799 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002800 memcpy(&pic_irqchip(kvm)->pics[0],
2801 &chip->chip.pic,
2802 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002803 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002804 break;
2805 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002806 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002807 memcpy(&pic_irqchip(kvm)->pics[1],
2808 &chip->chip.pic,
2809 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002810 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002811 break;
2812 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002813 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002814 break;
2815 default:
2816 r = -EINVAL;
2817 break;
2818 }
2819 kvm_pic_update_irq(pic_irqchip(kvm));
2820 return r;
2821}
2822
Sheng Yange0f63cb2008-03-04 00:50:59 +08002823static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2824{
2825 int r = 0;
2826
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002827 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002828 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002829 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002830 return r;
2831}
2832
2833static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2834{
2835 int r = 0;
2836
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002837 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002838 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002839 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2840 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2841 return r;
2842}
2843
2844static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2845{
2846 int r = 0;
2847
2848 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2849 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2850 sizeof(ps->channels));
2851 ps->flags = kvm->arch.vpit->pit_state.flags;
2852 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2853 return r;
2854}
2855
2856static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2857{
2858 int r = 0, start = 0;
2859 u32 prev_legacy, cur_legacy;
2860 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2861 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2862 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2863 if (!prev_legacy && cur_legacy)
2864 start = 1;
2865 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2866 sizeof(kvm->arch.vpit->pit_state.channels));
2867 kvm->arch.vpit->pit_state.flags = ps->flags;
2868 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002869 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002870 return r;
2871}
2872
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002873static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2874 struct kvm_reinject_control *control)
2875{
2876 if (!kvm->arch.vpit)
2877 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002878 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002879 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002880 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002881 return 0;
2882}
2883
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002884/*
2885 * Get (and clear) the dirty memory log for a memory slot.
2886 */
2887int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2888 struct kvm_dirty_log *log)
2889{
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002890 int r, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002891 struct kvm_memory_slot *memslot;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002892 unsigned long n;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002893 unsigned long is_dirty = 0;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002894
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002895 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002896
Marcelo Tosattib050b012009-12-23 14:35:22 -02002897 r = -EINVAL;
2898 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002899 goto out;
2900
Marcelo Tosattib050b012009-12-23 14:35:22 -02002901 memslot = &kvm->memslots->memslots[log->slot];
2902 r = -ENOENT;
2903 if (!memslot->dirty_bitmap)
2904 goto out;
2905
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002906 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002907
Marcelo Tosattib050b012009-12-23 14:35:22 -02002908 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2909 is_dirty = memslot->dirty_bitmap[i];
2910
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002911 /* If nothing is dirty, don't bother messing with page tables. */
2912 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002913 struct kvm_memslots *slots, *old_slots;
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09002914 unsigned long *dirty_bitmap;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002915
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002916 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002917 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002918 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002919
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09002920 r = -ENOMEM;
2921 dirty_bitmap = vmalloc(n);
2922 if (!dirty_bitmap)
2923 goto out;
2924 memset(dirty_bitmap, 0, n);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002925
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09002926 r = -ENOMEM;
2927 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2928 if (!slots) {
2929 vfree(dirty_bitmap);
2930 goto out;
2931 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002932 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2933 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2934
2935 old_slots = kvm->memslots;
2936 rcu_assign_pointer(kvm->memslots, slots);
2937 synchronize_srcu_expedited(&kvm->srcu);
2938 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2939 kfree(old_slots);
Takuya Yoshikawa914ebcc2010-04-28 18:50:36 +09002940
2941 r = -EFAULT;
2942 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) {
2943 vfree(dirty_bitmap);
2944 goto out;
2945 }
2946 vfree(dirty_bitmap);
2947 } else {
2948 r = -EFAULT;
2949 if (clear_user(log->dirty_bitmap, n))
2950 goto out;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002951 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002952
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002953 r = 0;
2954out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002955 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002956 return r;
2957}
2958
Carsten Otte1fe779f2007-10-29 16:08:35 +01002959long kvm_arch_vm_ioctl(struct file *filp,
2960 unsigned int ioctl, unsigned long arg)
2961{
2962 struct kvm *kvm = filp->private_data;
2963 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002964 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002965 /*
2966 * This union makes it completely explicit to gcc-3.x
2967 * that these two variables' stack usage should be
2968 * combined, not added together.
2969 */
2970 union {
2971 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002972 struct kvm_pit_state2 ps2;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002973 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002974 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002975
2976 switch (ioctl) {
2977 case KVM_SET_TSS_ADDR:
2978 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2979 if (r < 0)
2980 goto out;
2981 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002982 case KVM_SET_IDENTITY_MAP_ADDR: {
2983 u64 ident_addr;
2984
2985 r = -EFAULT;
2986 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2987 goto out;
2988 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2989 if (r < 0)
2990 goto out;
2991 break;
2992 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002993 case KVM_SET_NR_MMU_PAGES:
2994 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2995 if (r)
2996 goto out;
2997 break;
2998 case KVM_GET_NR_MMU_PAGES:
2999 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3000 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003001 case KVM_CREATE_IRQCHIP: {
3002 struct kvm_pic *vpic;
3003
3004 mutex_lock(&kvm->lock);
3005 r = -EEXIST;
3006 if (kvm->arch.vpic)
3007 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003008 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003009 vpic = kvm_create_pic(kvm);
3010 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01003011 r = kvm_ioapic_init(kvm);
3012 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003013 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
3014 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003015 kfree(vpic);
3016 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003017 }
3018 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003019 goto create_irqchip_unlock;
3020 smp_wmb();
3021 kvm->arch.vpic = vpic;
3022 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02003023 r = kvm_setup_default_irq_routing(kvm);
3024 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003025 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08003026 kvm_ioapic_destroy(kvm);
3027 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003028 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02003029 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003030 create_irqchip_unlock:
3031 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003032 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02003033 }
Sheng Yang78376992008-01-28 05:10:22 +08003034 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003035 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
3036 goto create_pit;
3037 case KVM_CREATE_PIT2:
3038 r = -EFAULT;
3039 if (copy_from_user(&u.pit_config, argp,
3040 sizeof(struct kvm_pit_config)))
3041 goto out;
3042 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003043 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02003044 r = -EEXIST;
3045 if (kvm->arch.vpit)
3046 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08003047 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02003048 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08003049 if (kvm->arch.vpit)
3050 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02003051 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003052 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08003053 break;
Gleb Natapov49256632009-02-04 17:28:14 +02003054 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01003055 case KVM_IRQ_LINE: {
3056 struct kvm_irq_level irq_event;
3057
3058 r = -EFAULT;
3059 if (copy_from_user(&irq_event, argp, sizeof irq_event))
3060 goto out;
Wei Yongjun160d2f62010-03-12 10:09:45 +08003061 r = -ENXIO;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003062 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02003063 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02003064 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
3065 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02003066 if (ioctl == KVM_IRQ_LINE_STATUS) {
Wei Yongjun160d2f62010-03-12 10:09:45 +08003067 r = -EFAULT;
Gleb Natapov49256632009-02-04 17:28:14 +02003068 irq_event.status = status;
3069 if (copy_to_user(argp, &irq_event,
3070 sizeof irq_event))
3071 goto out;
3072 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01003073 r = 0;
3074 }
3075 break;
3076 }
3077 case KVM_GET_IRQCHIP: {
3078 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07003079 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003080
Dave Hansenf0d66272008-08-11 10:01:45 -07003081 r = -ENOMEM;
3082 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01003083 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003084 r = -EFAULT;
3085 if (copy_from_user(chip, argp, sizeof *chip))
3086 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003087 r = -ENXIO;
3088 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003089 goto get_irqchip_out;
3090 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3091 if (r)
3092 goto get_irqchip_out;
3093 r = -EFAULT;
3094 if (copy_to_user(argp, chip, sizeof *chip))
3095 goto get_irqchip_out;
3096 r = 0;
3097 get_irqchip_out:
3098 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003099 if (r)
3100 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003101 break;
3102 }
3103 case KVM_SET_IRQCHIP: {
3104 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07003105 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003106
Dave Hansenf0d66272008-08-11 10:01:45 -07003107 r = -ENOMEM;
3108 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01003109 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003110 r = -EFAULT;
3111 if (copy_from_user(chip, argp, sizeof *chip))
3112 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003113 r = -ENXIO;
3114 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07003115 goto set_irqchip_out;
3116 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3117 if (r)
3118 goto set_irqchip_out;
3119 r = 0;
3120 set_irqchip_out:
3121 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01003122 if (r)
3123 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003124 break;
3125 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003126 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003127 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003128 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003129 goto out;
3130 r = -ENXIO;
3131 if (!kvm->arch.vpit)
3132 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003133 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003134 if (r)
3135 goto out;
3136 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003137 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003138 goto out;
3139 r = 0;
3140 break;
3141 }
3142 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003143 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003144 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003145 goto out;
3146 r = -ENXIO;
3147 if (!kvm->arch.vpit)
3148 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003149 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003150 if (r)
3151 goto out;
3152 r = 0;
3153 break;
3154 }
Beth Kone9f42752009-07-07 11:50:38 -04003155 case KVM_GET_PIT2: {
3156 r = -ENXIO;
3157 if (!kvm->arch.vpit)
3158 goto out;
3159 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3160 if (r)
3161 goto out;
3162 r = -EFAULT;
3163 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3164 goto out;
3165 r = 0;
3166 break;
3167 }
3168 case KVM_SET_PIT2: {
3169 r = -EFAULT;
3170 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3171 goto out;
3172 r = -ENXIO;
3173 if (!kvm->arch.vpit)
3174 goto out;
3175 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
3176 if (r)
3177 goto out;
3178 r = 0;
3179 break;
3180 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003181 case KVM_REINJECT_CONTROL: {
3182 struct kvm_reinject_control control;
3183 r = -EFAULT;
3184 if (copy_from_user(&control, argp, sizeof(control)))
3185 goto out;
3186 r = kvm_vm_ioctl_reinject(kvm, &control);
3187 if (r)
3188 goto out;
3189 r = 0;
3190 break;
3191 }
Ed Swierkffde22a2009-10-15 15:21:43 -07003192 case KVM_XEN_HVM_CONFIG: {
3193 r = -EFAULT;
3194 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3195 sizeof(struct kvm_xen_hvm_config)))
3196 goto out;
3197 r = -EINVAL;
3198 if (kvm->arch.xen_hvm_config.flags)
3199 goto out;
3200 r = 0;
3201 break;
3202 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003203 case KVM_SET_CLOCK: {
3204 struct timespec now;
3205 struct kvm_clock_data user_ns;
3206 u64 now_ns;
3207 s64 delta;
3208
3209 r = -EFAULT;
3210 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3211 goto out;
3212
3213 r = -EINVAL;
3214 if (user_ns.flags)
3215 goto out;
3216
3217 r = 0;
3218 ktime_get_ts(&now);
3219 now_ns = timespec_to_ns(&now);
3220 delta = user_ns.clock - now_ns;
3221 kvm->arch.kvmclock_offset = delta;
3222 break;
3223 }
3224 case KVM_GET_CLOCK: {
3225 struct timespec now;
3226 struct kvm_clock_data user_ns;
3227 u64 now_ns;
3228
3229 ktime_get_ts(&now);
3230 now_ns = timespec_to_ns(&now);
3231 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3232 user_ns.flags = 0;
3233
3234 r = -EFAULT;
3235 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3236 goto out;
3237 r = 0;
3238 break;
3239 }
3240
Carsten Otte1fe779f2007-10-29 16:08:35 +01003241 default:
3242 ;
3243 }
3244out:
3245 return r;
3246}
3247
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003248static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003249{
3250 u32 dummy[2];
3251 unsigned i, j;
3252
Glauber Costae3267cb2009-10-06 13:24:50 -04003253 /* skip the first msrs in the list. KVM-specific */
3254 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003255 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3256 continue;
3257 if (j < i)
3258 msrs_to_save[j] = msrs_to_save[i];
3259 j++;
3260 }
3261 num_msrs_to_save = j;
3262}
3263
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003264static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3265 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003266{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003267 if (vcpu->arch.apic &&
3268 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3269 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003270
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003271 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003272}
3273
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003274static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003275{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003276 if (vcpu->arch.apic &&
3277 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3278 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003279
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003280 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003281}
3282
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003283static void kvm_set_segment(struct kvm_vcpu *vcpu,
3284 struct kvm_segment *var, int seg)
3285{
3286 kvm_x86_ops->set_segment(vcpu, var, seg);
3287}
3288
3289void kvm_get_segment(struct kvm_vcpu *vcpu,
3290 struct kvm_segment *var, int seg)
3291{
3292 kvm_x86_ops->get_segment(vcpu, var, seg);
3293}
3294
Gleb Natapov1871c602010-02-10 14:21:32 +02003295gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3296{
3297 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3298 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3299}
3300
3301 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3302{
3303 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3304 access |= PFERR_FETCH_MASK;
3305 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3306}
3307
3308gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3309{
3310 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3311 access |= PFERR_WRITE_MASK;
3312 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3313}
3314
3315/* uses this to access any guest's mapped memory without checking CPL */
3316gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3317{
3318 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3319}
3320
3321static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3322 struct kvm_vcpu *vcpu, u32 access,
3323 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003324{
3325 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003326 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003327
3328 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003329 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003330 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003331 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003332 int ret;
3333
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003334 if (gpa == UNMAPPED_GVA) {
3335 r = X86EMUL_PROPAGATE_FAULT;
3336 goto out;
3337 }
Izik Eidus77c20022008-12-29 01:42:19 +02003338 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003339 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03003340 r = X86EMUL_IO_NEEDED;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003341 goto out;
3342 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003343
Izik Eidus77c20022008-12-29 01:42:19 +02003344 bytes -= toread;
3345 data += toread;
3346 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003347 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003348out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003349 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003350}
Izik Eidus77c20022008-12-29 01:42:19 +02003351
Gleb Natapov1871c602010-02-10 14:21:32 +02003352/* used for instruction fetching */
3353static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3354 struct kvm_vcpu *vcpu, u32 *error)
3355{
3356 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3357 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3358 access | PFERR_FETCH_MASK, error);
3359}
3360
3361static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3362 struct kvm_vcpu *vcpu, u32 *error)
3363{
3364 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3365 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3366 error);
3367}
3368
3369static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3370 struct kvm_vcpu *vcpu, u32 *error)
3371{
3372 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3373}
3374
Gleb Natapov79729952010-03-18 15:20:24 +02003375static int kvm_write_guest_virt_system(gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003376 unsigned int bytes,
Gleb Natapov79729952010-03-18 15:20:24 +02003377 struct kvm_vcpu *vcpu,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003378 u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003379{
3380 void *data = val;
3381 int r = X86EMUL_CONTINUE;
3382
3383 while (bytes) {
Gleb Natapov79729952010-03-18 15:20:24 +02003384 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr,
3385 PFERR_WRITE_MASK, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003386 unsigned offset = addr & (PAGE_SIZE-1);
3387 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3388 int ret;
3389
3390 if (gpa == UNMAPPED_GVA) {
3391 r = X86EMUL_PROPAGATE_FAULT;
3392 goto out;
3393 }
3394 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3395 if (ret < 0) {
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03003396 r = X86EMUL_IO_NEEDED;
Izik Eidus77c20022008-12-29 01:42:19 +02003397 goto out;
3398 }
3399
3400 bytes -= towrite;
3401 data += towrite;
3402 addr += towrite;
3403 }
3404out:
3405 return r;
3406}
3407
Carsten Ottebbd9b642007-10-30 18:44:21 +01003408static int emulator_read_emulated(unsigned long addr,
3409 void *val,
3410 unsigned int bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003411 unsigned int *error_code,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003412 struct kvm_vcpu *vcpu)
3413{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003414 gpa_t gpa;
3415
3416 if (vcpu->mmio_read_completed) {
3417 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003418 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3419 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003420 vcpu->mmio_read_completed = 0;
3421 return X86EMUL_CONTINUE;
3422 }
3423
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003424 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, error_code);
Gleb Natapov1871c602010-02-10 14:21:32 +02003425
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003426 if (gpa == UNMAPPED_GVA)
Gleb Natapov1871c602010-02-10 14:21:32 +02003427 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003428
3429 /* For APIC access vmexit */
3430 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3431 goto mmio;
3432
Gleb Natapov1871c602010-02-10 14:21:32 +02003433 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003434 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003435 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003436
3437mmio:
3438 /*
3439 * Is this MMIO handled locally?
3440 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003441 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3442 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003443 return X86EMUL_CONTINUE;
3444 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003445
3446 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003447
3448 vcpu->mmio_needed = 1;
Gleb Natapov411c35b2010-04-28 19:15:34 +03003449 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3450 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3451 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3452 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003453
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03003454 return X86EMUL_IO_NEEDED;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003455}
3456
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003457int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003458 const void *val, int bytes)
3459{
3460 int ret;
3461
3462 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3463 if (ret < 0)
3464 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003465 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003466 return 1;
3467}
3468
Carsten Ottebbd9b642007-10-30 18:44:21 +01003469static int emulator_write_emulated_onepage(unsigned long addr,
3470 const void *val,
3471 unsigned int bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003472 unsigned int *error_code,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003473 struct kvm_vcpu *vcpu)
3474{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003475 gpa_t gpa;
3476
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003477 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003478
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003479 if (gpa == UNMAPPED_GVA)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003480 return X86EMUL_PROPAGATE_FAULT;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003481
3482 /* For APIC access vmexit */
3483 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3484 goto mmio;
3485
3486 if (emulator_write_phys(vcpu, gpa, val, bytes))
3487 return X86EMUL_CONTINUE;
3488
3489mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003490 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003491 /*
3492 * Is this MMIO handled locally?
3493 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003494 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003495 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003496
3497 vcpu->mmio_needed = 1;
Gleb Natapov411c35b2010-04-28 19:15:34 +03003498 vcpu->run->exit_reason = KVM_EXIT_MMIO;
3499 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
3500 vcpu->run->mmio.len = vcpu->mmio_size = bytes;
3501 vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1;
3502 memcpy(vcpu->run->mmio.data, val, bytes);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003503
3504 return X86EMUL_CONTINUE;
3505}
3506
3507int emulator_write_emulated(unsigned long addr,
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003508 const void *val,
3509 unsigned int bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003510 unsigned int *error_code,
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003511 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003512{
3513 /* Crossing a page boundary? */
3514 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3515 int rc, now;
3516
3517 now = -addr & ~PAGE_MASK;
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003518 rc = emulator_write_emulated_onepage(addr, val, now, error_code,
3519 vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003520 if (rc != X86EMUL_CONTINUE)
3521 return rc;
3522 addr += now;
3523 val += now;
3524 bytes -= now;
3525 }
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003526 return emulator_write_emulated_onepage(addr, val, bytes, error_code,
3527 vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003528}
Carsten Ottebbd9b642007-10-30 18:44:21 +01003529
Avi Kivitydaea3e72010-03-15 13:59:54 +02003530#define CMPXCHG_TYPE(t, ptr, old, new) \
3531 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3532
3533#ifdef CONFIG_X86_64
3534# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3535#else
3536# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01003537 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02003538#endif
3539
Carsten Ottebbd9b642007-10-30 18:44:21 +01003540static int emulator_cmpxchg_emulated(unsigned long addr,
3541 const void *old,
3542 const void *new,
3543 unsigned int bytes,
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003544 unsigned int *error_code,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003545 struct kvm_vcpu *vcpu)
3546{
Avi Kivitydaea3e72010-03-15 13:59:54 +02003547 gpa_t gpa;
3548 struct page *page;
3549 char *kaddr;
3550 bool exchanged;
3551
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003552 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02003553 if (bytes > 8 || (bytes & (bytes - 1)))
3554 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003555
Avi Kivitydaea3e72010-03-15 13:59:54 +02003556 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003557
Avi Kivitydaea3e72010-03-15 13:59:54 +02003558 if (gpa == UNMAPPED_GVA ||
3559 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3560 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003561
Avi Kivitydaea3e72010-03-15 13:59:54 +02003562 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3563 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003564
Avi Kivitydaea3e72010-03-15 13:59:54 +02003565 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Wei Yongjunc19b8bd2010-07-15 08:51:58 +08003566 if (is_error_page(page)) {
3567 kvm_release_page_clean(page);
3568 goto emul_write;
3569 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02003570
Avi Kivitydaea3e72010-03-15 13:59:54 +02003571 kaddr = kmap_atomic(page, KM_USER0);
3572 kaddr += offset_in_page(gpa);
3573 switch (bytes) {
3574 case 1:
3575 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
3576 break;
3577 case 2:
3578 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
3579 break;
3580 case 4:
3581 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
3582 break;
3583 case 8:
3584 exchanged = CMPXCHG64(kaddr, old, new);
3585 break;
3586 default:
3587 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003588 }
Avi Kivitydaea3e72010-03-15 13:59:54 +02003589 kunmap_atomic(kaddr, KM_USER0);
3590 kvm_release_page_dirty(page);
3591
3592 if (!exchanged)
3593 return X86EMUL_CMPXCHG_FAILED;
3594
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003595 kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
3596
3597 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02003598
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003599emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02003600 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003601
Gleb Natapov8fe681e2010-04-28 19:15:37 +03003602 return emulator_write_emulated(addr, new, bytes, error_code, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003603}
3604
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003605static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3606{
3607 /* TODO: String I/O for in kernel device */
3608 int r;
3609
3610 if (vcpu->arch.pio.in)
3611 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
3612 vcpu->arch.pio.size, pd);
3613 else
3614 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3615 vcpu->arch.pio.port, vcpu->arch.pio.size,
3616 pd);
3617 return r;
3618}
3619
3620
3621static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
3622 unsigned int count, struct kvm_vcpu *vcpu)
3623{
Gleb Natapov79729952010-03-18 15:20:24 +02003624 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003625 goto data_avail;
3626
3627 trace_kvm_pio(1, port, size, 1);
3628
3629 vcpu->arch.pio.port = port;
3630 vcpu->arch.pio.in = 1;
Gleb Natapov79729952010-03-18 15:20:24 +02003631 vcpu->arch.pio.count = count;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003632 vcpu->arch.pio.size = size;
3633
3634 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3635 data_avail:
3636 memcpy(val, vcpu->arch.pio_data, size * count);
Gleb Natapov79729952010-03-18 15:20:24 +02003637 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003638 return 1;
3639 }
3640
3641 vcpu->run->exit_reason = KVM_EXIT_IO;
3642 vcpu->run->io.direction = KVM_EXIT_IO_IN;
3643 vcpu->run->io.size = size;
3644 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3645 vcpu->run->io.count = count;
3646 vcpu->run->io.port = port;
3647
3648 return 0;
3649}
3650
3651static int emulator_pio_out_emulated(int size, unsigned short port,
3652 const void *val, unsigned int count,
3653 struct kvm_vcpu *vcpu)
3654{
3655 trace_kvm_pio(0, port, size, 1);
3656
3657 vcpu->arch.pio.port = port;
3658 vcpu->arch.pio.in = 0;
Gleb Natapov79729952010-03-18 15:20:24 +02003659 vcpu->arch.pio.count = count;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003660 vcpu->arch.pio.size = size;
3661
3662 memcpy(vcpu->arch.pio_data, val, size * count);
3663
3664 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Gleb Natapov79729952010-03-18 15:20:24 +02003665 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003666 return 1;
3667 }
3668
3669 vcpu->run->exit_reason = KVM_EXIT_IO;
3670 vcpu->run->io.direction = KVM_EXIT_IO_OUT;
3671 vcpu->run->io.size = size;
3672 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3673 vcpu->run->io.count = count;
3674 vcpu->run->io.port = port;
3675
3676 return 0;
3677}
3678
Carsten Ottebbd9b642007-10-30 18:44:21 +01003679static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3680{
3681 return kvm_x86_ops->get_segment_base(vcpu, seg);
3682}
3683
3684int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3685{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003686 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003687 return X86EMUL_CONTINUE;
3688}
3689
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003690int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
3691{
3692 if (!need_emulate_wbinvd(vcpu))
3693 return X86EMUL_CONTINUE;
3694
3695 if (kvm_x86_ops->has_wbinvd_exit()) {
3696 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
3697 wbinvd_ipi, NULL, 1);
3698 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
3699 }
3700 wbinvd();
3701 return X86EMUL_CONTINUE;
3702}
3703EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
3704
Carsten Ottebbd9b642007-10-30 18:44:21 +01003705int emulate_clts(struct kvm_vcpu *vcpu)
3706{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003707 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003708 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003709 return X86EMUL_CONTINUE;
3710}
3711
Gleb Natapov35aa5372010-04-28 19:15:27 +03003712int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003713{
Gleb Natapov338dbc92010-04-28 19:15:32 +03003714 return _kvm_get_dr(vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003715}
3716
Gleb Natapov35aa5372010-04-28 19:15:27 +03003717int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003718{
Gleb Natapov338dbc92010-04-28 19:15:32 +03003719
3720 return __kvm_set_dr(vcpu, dr, value);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003721}
3722
Gleb Natapov52a46612010-03-18 15:20:03 +02003723static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3724{
3725 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3726}
3727
3728static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
3729{
3730 unsigned long value;
3731
3732 switch (cr) {
3733 case 0:
3734 value = kvm_read_cr0(vcpu);
3735 break;
3736 case 2:
3737 value = vcpu->arch.cr2;
3738 break;
3739 case 3:
3740 value = vcpu->arch.cr3;
3741 break;
3742 case 4:
3743 value = kvm_read_cr4(vcpu);
3744 break;
3745 case 8:
3746 value = kvm_get_cr8(vcpu);
3747 break;
3748 default:
3749 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3750 return 0;
3751 }
3752
3753 return value;
3754}
3755
Gleb Natapov0f122442010-04-28 19:15:31 +03003756static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
Gleb Natapov52a46612010-03-18 15:20:03 +02003757{
Gleb Natapov0f122442010-04-28 19:15:31 +03003758 int res = 0;
3759
Gleb Natapov52a46612010-03-18 15:20:03 +02003760 switch (cr) {
3761 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003762 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02003763 break;
3764 case 2:
3765 vcpu->arch.cr2 = val;
3766 break;
3767 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003768 res = kvm_set_cr3(vcpu, val);
Gleb Natapov52a46612010-03-18 15:20:03 +02003769 break;
3770 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003771 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Gleb Natapov52a46612010-03-18 15:20:03 +02003772 break;
3773 case 8:
Gleb Natapov0f122442010-04-28 19:15:31 +03003774 res = __kvm_set_cr8(vcpu, val & 0xfUL);
Gleb Natapov52a46612010-03-18 15:20:03 +02003775 break;
3776 default:
3777 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Gleb Natapov0f122442010-04-28 19:15:31 +03003778 res = -1;
Gleb Natapov52a46612010-03-18 15:20:03 +02003779 }
Gleb Natapov0f122442010-04-28 19:15:31 +03003780
3781 return res;
Gleb Natapov52a46612010-03-18 15:20:03 +02003782}
3783
Gleb Natapov9c537242010-03-18 15:20:05 +02003784static int emulator_get_cpl(struct kvm_vcpu *vcpu)
3785{
3786 return kvm_x86_ops->get_cpl(vcpu);
3787}
3788
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003789static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
3790{
3791 kvm_x86_ops->get_gdt(vcpu, dt);
3792}
3793
Gleb Natapov5951c442010-04-28 19:15:29 +03003794static unsigned long emulator_get_cached_segment_base(int seg,
3795 struct kvm_vcpu *vcpu)
3796{
3797 return get_segment_base(vcpu, seg);
3798}
3799
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003800static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg,
3801 struct kvm_vcpu *vcpu)
3802{
3803 struct kvm_segment var;
3804
3805 kvm_get_segment(vcpu, &var, seg);
3806
3807 if (var.unusable)
3808 return false;
3809
3810 if (var.g)
3811 var.limit >>= 12;
3812 set_desc_limit(desc, var.limit);
3813 set_desc_base(desc, (unsigned long)var.base);
3814 desc->type = var.type;
3815 desc->s = var.s;
3816 desc->dpl = var.dpl;
3817 desc->p = var.present;
3818 desc->avl = var.avl;
3819 desc->l = var.l;
3820 desc->d = var.db;
3821 desc->g = var.g;
3822
3823 return true;
3824}
3825
3826static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg,
3827 struct kvm_vcpu *vcpu)
3828{
3829 struct kvm_segment var;
3830
3831 /* needed to preserve selector */
3832 kvm_get_segment(vcpu, &var, seg);
3833
3834 var.base = get_desc_base(desc);
3835 var.limit = get_desc_limit(desc);
3836 if (desc->g)
3837 var.limit = (var.limit << 12) | 0xfff;
3838 var.type = desc->type;
3839 var.present = desc->p;
3840 var.dpl = desc->dpl;
3841 var.db = desc->d;
3842 var.s = desc->s;
3843 var.l = desc->l;
3844 var.g = desc->g;
3845 var.avl = desc->avl;
3846 var.present = desc->p;
3847 var.unusable = !var.present;
3848 var.padding = 0;
3849
3850 kvm_set_segment(vcpu, &var, seg);
3851 return;
3852}
3853
3854static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
3855{
3856 struct kvm_segment kvm_seg;
3857
3858 kvm_get_segment(vcpu, &kvm_seg, seg);
3859 return kvm_seg.selector;
3860}
3861
3862static void emulator_set_segment_selector(u16 sel, int seg,
3863 struct kvm_vcpu *vcpu)
3864{
3865 struct kvm_segment kvm_seg;
3866
3867 kvm_get_segment(vcpu, &kvm_seg, seg);
3868 kvm_seg.selector = sel;
3869 kvm_set_segment(vcpu, &kvm_seg, seg);
3870}
3871
Harvey Harrison14af3f32008-02-19 10:25:50 -08003872static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003873 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003874 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02003875 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003876 .read_emulated = emulator_read_emulated,
3877 .write_emulated = emulator_write_emulated,
3878 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003879 .pio_in_emulated = emulator_pio_in_emulated,
3880 .pio_out_emulated = emulator_pio_out_emulated,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003881 .get_cached_descriptor = emulator_get_cached_descriptor,
3882 .set_cached_descriptor = emulator_set_cached_descriptor,
3883 .get_segment_selector = emulator_get_segment_selector,
3884 .set_segment_selector = emulator_set_segment_selector,
Gleb Natapov5951c442010-04-28 19:15:29 +03003885 .get_cached_segment_base = emulator_get_cached_segment_base,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003886 .get_gdt = emulator_get_gdt,
Gleb Natapov52a46612010-03-18 15:20:03 +02003887 .get_cr = emulator_get_cr,
3888 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02003889 .cpl = emulator_get_cpl,
Gleb Natapov35aa5372010-04-28 19:15:27 +03003890 .get_dr = emulator_get_dr,
3891 .set_dr = emulator_set_dr,
Gleb Natapov3fb1b5d2010-04-28 19:15:28 +03003892 .set_msr = kvm_set_msr,
3893 .get_msr = kvm_get_msr,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003894};
3895
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003896static void cache_all_regs(struct kvm_vcpu *vcpu)
3897{
3898 kvm_register_read(vcpu, VCPU_REGS_RAX);
3899 kvm_register_read(vcpu, VCPU_REGS_RSP);
3900 kvm_register_read(vcpu, VCPU_REGS_RIP);
3901 vcpu->arch.regs_dirty = ~0;
3902}
3903
Gleb Natapov95cb2292010-04-28 19:15:43 +03003904static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
3905{
3906 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask);
3907 /*
3908 * an sti; sti; sequence only disable interrupts for the first
3909 * instruction. So, if the last instruction, be it emulated or
3910 * not, left the system with the INT_STI flag enabled, it
3911 * means that the last instruction is an sti. We should not
3912 * leave the flag on in this case. The same goes for mov ss
3913 */
3914 if (!(int_shadow & mask))
3915 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
3916}
3917
Gleb Natapov54b84862010-04-28 19:15:44 +03003918static void inject_emulated_exception(struct kvm_vcpu *vcpu)
3919{
3920 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
3921 if (ctxt->exception == PF_VECTOR)
3922 kvm_inject_page_fault(vcpu, ctxt->cr2, ctxt->error_code);
3923 else if (ctxt->error_code_valid)
3924 kvm_queue_exception_e(vcpu, ctxt->exception, ctxt->error_code);
3925 else
3926 kvm_queue_exception(vcpu, ctxt->exception);
3927}
3928
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003929static int handle_emulation_failure(struct kvm_vcpu *vcpu)
3930{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003931 ++vcpu->stat.insn_emulation_fail;
3932 trace_kvm_emulate_insn_failed(vcpu);
3933 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3934 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3935 vcpu->run->internal.ndata = 0;
3936 kvm_queue_exception(vcpu, UD_VECTOR);
3937 return EMULATE_FAIL;
3938}
3939
Gleb Natapova6f177e2010-07-08 12:41:12 +03003940static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
3941{
3942 gpa_t gpa;
3943
Gleb Natapov68be0802010-07-14 19:05:45 +03003944 if (tdp_enabled)
3945 return false;
3946
Gleb Natapova6f177e2010-07-08 12:41:12 +03003947 /*
3948 * if emulation was due to access to shadowed page table
3949 * and it failed try to unshadow page and re-entetr the
3950 * guest to let CPU execute the instruction.
3951 */
3952 if (kvm_mmu_unprotect_page_virt(vcpu, gva))
3953 return true;
3954
3955 gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
3956
3957 if (gpa == UNMAPPED_GVA)
3958 return true; /* let cpu generate fault */
3959
3960 if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
3961 return true;
3962
3963 return false;
3964}
3965
Carsten Ottebbd9b642007-10-30 18:44:21 +01003966int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003967 unsigned long cr2,
3968 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003969 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003970{
Gleb Natapov95cb2292010-04-28 19:15:43 +03003971 int r;
Gleb Natapov4d2179e2010-04-28 19:15:42 +03003972 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003973
Avi Kivity26eef702008-07-03 14:59:22 +03003974 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003975 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003976 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003977 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003978 * instead of direct ->regs accesses, can save hundred cycles
3979 * on Intel for instructions that don't read/change RSP, for
3980 * for example.
3981 */
3982 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003983
Sheng Yang571008d2008-01-02 14:49:22 +08003984 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003985 int cs_db, cs_l;
3986 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3987
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003988 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka83bf0002010-02-23 17:47:59 +01003989 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Gleb Natapov063db062010-03-18 15:20:06 +02003990 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003991 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003992 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003993 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003994 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003995 ? X86EMUL_MODE_PROT64 : cs_db
3996 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Gleb Natapov4d2179e2010-04-28 19:15:42 +03003997 memset(c, 0, sizeof(struct decode_cache));
3998 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
Gleb Natapov95cb2292010-04-28 19:15:43 +03003999 vcpu->arch.emulate_ctxt.interruptibility = 0;
Gleb Natapov54b84862010-04-28 19:15:44 +03004000 vcpu->arch.emulate_ctxt.exception = -1;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004001
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004002 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Avi Kivitye46479f2010-04-11 13:05:16 +03004003 trace_kvm_emulate_insn_start(vcpu);
Sheng Yang571008d2008-01-02 14:49:22 +08004004
Andre Przywara0cb57622009-06-17 15:50:31 +02004005 /* Only allow emulation of specific instructions on #UD
4006 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Andre Przywara0cb57622009-06-17 15:50:31 +02004007 if (emulation_type & EMULTYPE_TRAP_UD) {
4008 if (!c->twobyte)
4009 return EMULATE_FAIL;
4010 switch (c->b) {
4011 case 0x01: /* VMMCALL */
4012 if (c->modrm_mod != 3 || c->modrm_rm != 1)
4013 return EMULATE_FAIL;
4014 break;
4015 case 0x34: /* sysenter */
4016 case 0x35: /* sysexit */
4017 if (c->modrm_mod != 0 || c->modrm_rm != 0)
4018 return EMULATE_FAIL;
4019 break;
4020 case 0x05: /* syscall */
4021 if (c->modrm_mod != 0 || c->modrm_rm != 0)
4022 return EMULATE_FAIL;
4023 break;
4024 default:
4025 return EMULATE_FAIL;
4026 }
4027
4028 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
4029 return EMULATE_FAIL;
4030 }
Sheng Yang571008d2008-01-02 14:49:22 +08004031
Avi Kivityf2b57562007-11-18 15:17:51 +02004032 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01004033 if (r) {
Gleb Natapova6f177e2010-07-08 12:41:12 +03004034 if (reexecute_instruction(vcpu, cr2))
Carsten Ottebbd9b642007-10-30 18:44:21 +01004035 return EMULATE_DONE;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004036 if (emulation_type & EMULTYPE_SKIP)
4037 return EMULATE_FAIL;
4038 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004039 }
4040 }
4041
Gleb Natapovba8afb62009-04-12 13:36:57 +03004042 if (emulation_type & EMULTYPE_SKIP) {
4043 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
4044 return EMULATE_DONE;
4045 }
4046
Gleb Natapov4d2179e2010-04-28 19:15:42 +03004047 /* this is needed for vmware backdor interface to work since it
4048 changes registers values during IO operation */
4049 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
4050
Gleb Natapov5cd21912010-03-18 15:20:26 +02004051restart:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004052 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004053
4054 if (r) { /* emulation failed */
Gleb Natapova6f177e2010-07-08 12:41:12 +03004055 if (reexecute_instruction(vcpu, cr2))
Gleb Natapovc3cd7ff2010-04-28 19:15:35 +03004056 return EMULATE_DONE;
4057
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004058 return handle_emulation_failure(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01004059 }
4060
Gleb Natapov95cb2292010-04-28 19:15:43 +03004061 toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility);
Gleb Natapovef050dc2010-04-28 19:15:40 +03004062 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Gleb Natapov4d2179e2010-04-28 19:15:42 +03004063 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
Gleb Natapov95c55882010-04-28 19:15:39 +03004064 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
Gleb Natapov3457e412010-04-28 19:15:38 +03004065
Gleb Natapov54b84862010-04-28 19:15:44 +03004066 if (vcpu->arch.emulate_ctxt.exception >= 0) {
4067 inject_emulated_exception(vcpu);
4068 return EMULATE_DONE;
4069 }
4070
Gleb Natapov3457e412010-04-28 19:15:38 +03004071 if (vcpu->arch.pio.count) {
4072 if (!vcpu->arch.pio.in)
4073 vcpu->arch.pio.count = 0;
4074 return EMULATE_DO_MMIO;
4075 }
4076
4077 if (vcpu->mmio_needed) {
4078 if (vcpu->mmio_is_write)
4079 vcpu->mmio_needed = 0;
4080 return EMULATE_DO_MMIO;
4081 }
4082
Gleb Natapov5cd21912010-03-18 15:20:26 +02004083 if (vcpu->arch.emulate_ctxt.restart)
4084 goto restart;
4085
Carsten Ottebbd9b642007-10-30 18:44:21 +01004086 return EMULATE_DONE;
4087}
4088EXPORT_SYMBOL_GPL(emulate_instruction);
4089
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004090int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
Carsten Ottede7d7892007-10-30 18:44:25 +01004091{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004092 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
4093 int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
4094 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02004095 vcpu->arch.pio.count = 0;
Izik Eidus0f346072008-12-29 01:42:20 +02004096 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01004097}
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004098EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
Carsten Ottede7d7892007-10-30 18:44:25 +01004099
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004100static void bounce_off(void *info)
4101{
4102 /* nothing */
4103}
4104
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004105static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
4106 void *data)
4107{
4108 struct cpufreq_freqs *freq = data;
4109 struct kvm *kvm;
4110 struct kvm_vcpu *vcpu;
4111 int i, send_ipi = 0;
4112
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004113 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
4114 return 0;
4115 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
4116 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10004117 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004118
4119 spin_lock(&kvm_lock);
4120 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03004121 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004122 if (vcpu->cpu != freq->cpu)
4123 continue;
4124 if (!kvm_request_guest_time_update(vcpu))
4125 continue;
4126 if (vcpu->cpu != smp_processor_id())
4127 send_ipi++;
4128 }
4129 }
4130 spin_unlock(&kvm_lock);
4131
4132 if (freq->old < freq->new && send_ipi) {
4133 /*
4134 * We upscale the frequency. Must make the guest
4135 * doesn't see old kvmclock values while running with
4136 * the new frequency, otherwise we risk the guest sees
4137 * time go backwards.
4138 *
4139 * In case we update the frequency for another cpu
4140 * (which might be in guest context) send an interrupt
4141 * to kick the cpu out of guest context. Next time
4142 * guest context is entered kvmclock will be updated,
4143 * so the guest will not see stale values.
4144 */
4145 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
4146 }
4147 return 0;
4148}
4149
4150static struct notifier_block kvmclock_cpufreq_notifier_block = {
4151 .notifier_call = kvmclock_cpufreq_notifier
4152};
4153
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004154static void kvm_timer_init(void)
4155{
4156 int cpu;
4157
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004158 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004159 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
4160 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10004161 for_each_online_cpu(cpu) {
4162 unsigned long khz = cpufreq_get(cpu);
4163 if (!khz)
4164 khz = tsc_khz;
4165 per_cpu(cpu_tsc_khz, cpu) = khz;
4166 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10004167 } else {
4168 for_each_possible_cpu(cpu)
4169 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004170 }
4171}
4172
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004173static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
4174
4175static int kvm_is_in_guest(void)
4176{
4177 return percpu_read(current_vcpu) != NULL;
4178}
4179
4180static int kvm_is_user_mode(void)
4181{
4182 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004183
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004184 if (percpu_read(current_vcpu))
4185 user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004186
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004187 return user_mode != 0;
4188}
4189
4190static unsigned long kvm_get_guest_ip(void)
4191{
4192 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004193
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004194 if (percpu_read(current_vcpu))
4195 ip = kvm_rip_read(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004196
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004197 return ip;
4198}
4199
4200static struct perf_guest_info_callbacks kvm_guest_cbs = {
4201 .is_in_guest = kvm_is_in_guest,
4202 .is_user_mode = kvm_is_user_mode,
4203 .get_guest_ip = kvm_get_guest_ip,
4204};
4205
4206void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
4207{
4208 percpu_write(current_vcpu, vcpu);
4209}
4210EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
4211
4212void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
4213{
4214 percpu_write(current_vcpu, NULL);
4215}
4216EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
4217
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004218int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02004219{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004220 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004221 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
4222
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004223 if (kvm_x86_ops) {
4224 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004225 r = -EEXIST;
4226 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004227 }
4228
4229 if (!ops->cpu_has_kvm_support()) {
4230 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004231 r = -EOPNOTSUPP;
4232 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004233 }
4234 if (ops->disabled_by_bios()) {
4235 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004236 r = -EOPNOTSUPP;
4237 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004238 }
4239
Avi Kivity97db56c2008-01-13 13:23:56 +02004240 r = kvm_mmu_module_init();
4241 if (r)
4242 goto out;
4243
4244 kvm_init_msr_list();
4245
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004246 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004247 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08004248 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
4249 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004250 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004251
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004252 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004253
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004254 perf_register_guest_info_callbacks(&kvm_guest_cbs);
4255
Dexuan Cui2acf9232010-06-10 11:27:12 +08004256 if (cpu_has_xsave)
4257 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
4258
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004259 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004260
4261out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004262 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02004263}
Hollis Blanchard8776e512007-10-31 17:24:24 -05004264
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004265void kvm_arch_exit(void)
4266{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004267 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
4268
Jan Kiszka888d2562009-04-17 19:24:58 +02004269 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
4270 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
4271 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004272 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004273 kvm_mmu_module_exit();
4274}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004275
Hollis Blanchard8776e512007-10-31 17:24:24 -05004276int kvm_emulate_halt(struct kvm_vcpu *vcpu)
4277{
4278 ++vcpu->stat.halt_exits;
4279 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03004280 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004281 return 1;
4282 } else {
4283 vcpu->run->exit_reason = KVM_EXIT_HLT;
4284 return 0;
4285 }
4286}
4287EXPORT_SYMBOL_GPL(kvm_emulate_halt);
4288
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004289static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
4290 unsigned long a1)
4291{
4292 if (is_long_mode(vcpu))
4293 return a0;
4294 else
4295 return a0 | ((gpa_t)a1 << 32);
4296}
4297
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004298int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
4299{
4300 u64 param, ingpa, outgpa, ret;
4301 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
4302 bool fast, longmode;
4303 int cs_db, cs_l;
4304
4305 /*
4306 * hypercall generates UD from non zero cpl and real mode
4307 * per HYPER-V spec
4308 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02004309 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004310 kvm_queue_exception(vcpu, UD_VECTOR);
4311 return 0;
4312 }
4313
4314 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4315 longmode = is_long_mode(vcpu) && cs_l == 1;
4316
4317 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02004318 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
4319 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
4320 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
4321 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
4322 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
4323 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004324 }
4325#ifdef CONFIG_X86_64
4326 else {
4327 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
4328 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
4329 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
4330 }
4331#endif
4332
4333 code = param & 0xffff;
4334 fast = (param >> 16) & 0x1;
4335 rep_cnt = (param >> 32) & 0xfff;
4336 rep_idx = (param >> 48) & 0xfff;
4337
4338 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
4339
Gleb Natapovc25bc162010-01-17 15:51:24 +02004340 switch (code) {
4341 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
4342 kvm_vcpu_on_spin(vcpu);
4343 break;
4344 default:
4345 res = HV_STATUS_INVALID_HYPERCALL_CODE;
4346 break;
4347 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004348
4349 ret = res | (((u64)rep_done & 0xfff) << 32);
4350 if (longmode) {
4351 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4352 } else {
4353 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
4354 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
4355 }
4356
4357 return 1;
4358}
4359
Hollis Blanchard8776e512007-10-31 17:24:24 -05004360int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
4361{
4362 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004363 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004364
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004365 if (kvm_hv_hypercall_enabled(vcpu->kvm))
4366 return kvm_hv_hypercall(vcpu);
4367
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004368 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
4369 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
4370 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
4371 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
4372 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004373
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004374 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004375
Hollis Blanchard8776e512007-10-31 17:24:24 -05004376 if (!is_long_mode(vcpu)) {
4377 nr &= 0xFFFFFFFF;
4378 a0 &= 0xFFFFFFFF;
4379 a1 &= 0xFFFFFFFF;
4380 a2 &= 0xFFFFFFFF;
4381 a3 &= 0xFFFFFFFF;
4382 }
4383
Jan Kiszka07708c42009-08-03 18:43:28 +02004384 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
4385 ret = -KVM_EPERM;
4386 goto out;
4387 }
4388
Hollis Blanchard8776e512007-10-31 17:24:24 -05004389 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02004390 case KVM_HC_VAPIC_POLL_IRQ:
4391 ret = 0;
4392 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004393 case KVM_HC_MMU_OP:
4394 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
4395 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004396 default:
4397 ret = -KVM_ENOSYS;
4398 break;
4399 }
Jan Kiszka07708c42009-08-03 18:43:28 +02004400out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004401 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05304402 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004403 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004404}
4405EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
4406
4407int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
4408{
4409 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004410 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004411
Hollis Blanchard8776e512007-10-31 17:24:24 -05004412 /*
4413 * Blow out the MMU to ensure that no other VCPU has an active mapping
4414 * to ensure that the updated hypercall appears atomically across all
4415 * VCPUs.
4416 */
4417 kvm_mmu_zap_all(vcpu->kvm);
4418
Hollis Blanchard8776e512007-10-31 17:24:24 -05004419 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004420
Gleb Natapov8fe681e2010-04-28 19:15:37 +03004421 return emulator_write_emulated(rip, instruction, 3, NULL, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004422}
4423
Hollis Blanchard8776e512007-10-31 17:24:24 -05004424void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4425{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004426 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004427
4428 kvm_x86_ops->set_gdt(vcpu, &dt);
4429}
4430
4431void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4432{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004433 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004434
4435 kvm_x86_ops->set_idt(vcpu, &dt);
4436}
4437
Dan Kenigsberg07716712007-11-21 17:10:04 +02004438static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4439{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004440 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4441 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004442
4443 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4444 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004445 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004446 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004447 if (ej->function == e->function) {
4448 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4449 return j;
4450 }
4451 }
4452 return 0; /* silence gcc, even though control never reaches here */
4453}
4454
4455/* find an entry with matching function, matching index (if needed), and that
4456 * should be read next (if it's stateful) */
4457static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4458 u32 function, u32 index)
4459{
4460 if (e->function != function)
4461 return 0;
4462 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4463 return 0;
4464 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004465 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004466 return 0;
4467 return 1;
4468}
4469
Alexander Grafd8017472008-11-25 20:17:11 +01004470struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4471 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004472{
4473 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004474 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004475
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004476 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004477 struct kvm_cpuid_entry2 *e;
4478
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004479 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004480 if (is_matching_cpuid_entry(e, function, index)) {
4481 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4482 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004483 best = e;
4484 break;
4485 }
4486 /*
4487 * Both basic or both extended?
4488 */
4489 if (((e->function ^ function) & 0x80000000) == 0)
4490 if (!best || e->function > best->function)
4491 best = e;
4492 }
Alexander Grafd8017472008-11-25 20:17:11 +01004493 return best;
4494}
Sheng Yang0e851882009-12-18 16:48:46 +08004495EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004496
Dong, Eddie82725b22009-03-30 16:21:08 +08004497int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4498{
4499 struct kvm_cpuid_entry2 *best;
4500
Avi Kivityf7a71192010-04-11 15:33:32 +03004501 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
4502 if (!best || best->eax < 0x80000008)
4503 goto not_found;
Dong, Eddie82725b22009-03-30 16:21:08 +08004504 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4505 if (best)
4506 return best->eax & 0xff;
Avi Kivityf7a71192010-04-11 15:33:32 +03004507not_found:
Dong, Eddie82725b22009-03-30 16:21:08 +08004508 return 36;
4509}
4510
Alexander Grafd8017472008-11-25 20:17:11 +01004511void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4512{
4513 u32 function, index;
4514 struct kvm_cpuid_entry2 *best;
4515
4516 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4517 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4518 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4519 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4520 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4521 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4522 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004523 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004524 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4525 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4526 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4527 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004528 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004529 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004530 trace_kvm_cpuid(function,
4531 kvm_register_read(vcpu, VCPU_REGS_RAX),
4532 kvm_register_read(vcpu, VCPU_REGS_RBX),
4533 kvm_register_read(vcpu, VCPU_REGS_RCX),
4534 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004535}
4536EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004537
4538/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004539 * Check if userspace requested an interrupt window, and that the
4540 * interrupt window is open.
4541 *
4542 * No need to exit to userspace if we already have an interrupt queued.
4543 */
Avi Kivity851ba692009-08-24 11:10:17 +03004544static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004545{
Gleb Natapov80618232009-04-21 17:44:56 +03004546 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004547 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004548 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004549}
4550
Avi Kivity851ba692009-08-24 11:10:17 +03004551static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004552{
Avi Kivity851ba692009-08-24 11:10:17 +03004553 struct kvm_run *kvm_run = vcpu->run;
4554
Jan Kiszka91586a32009-10-05 13:07:21 +02004555 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004556 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004557 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004558 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004559 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004560 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004561 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004562 kvm_arch_interrupt_allowed(vcpu) &&
4563 !kvm_cpu_has_interrupt(vcpu) &&
4564 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004565}
4566
Avi Kivityb93463a2007-10-25 16:52:32 +02004567static void vapic_enter(struct kvm_vcpu *vcpu)
4568{
4569 struct kvm_lapic *apic = vcpu->arch.apic;
4570 struct page *page;
4571
4572 if (!apic || !apic->vapic_addr)
4573 return;
4574
4575 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004576
4577 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004578}
4579
4580static void vapic_exit(struct kvm_vcpu *vcpu)
4581{
4582 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004583 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004584
4585 if (!apic || !apic->vapic_addr)
4586 return;
4587
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004588 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004589 kvm_release_page_dirty(apic->vapic_page);
4590 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004591 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004592}
4593
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004594static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4595{
4596 int max_irr, tpr;
4597
4598 if (!kvm_x86_ops->update_cr8_intercept)
4599 return;
4600
Avi Kivity88c808f2009-08-17 22:49:40 +03004601 if (!vcpu->arch.apic)
4602 return;
4603
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004604 if (!vcpu->arch.apic->vapic_addr)
4605 max_irr = kvm_lapic_find_highest_irr(vcpu);
4606 else
4607 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004608
4609 if (max_irr != -1)
4610 max_irr >>= 4;
4611
4612 tpr = kvm_lapic_get_cr8(vcpu);
4613
4614 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4615}
4616
Avi Kivity851ba692009-08-24 11:10:17 +03004617static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004618{
4619 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004620 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02004621 trace_kvm_inj_exception(vcpu->arch.exception.nr,
4622 vcpu->arch.exception.has_error_code,
4623 vcpu->arch.exception.error_code);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004624 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4625 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004626 vcpu->arch.exception.error_code,
4627 vcpu->arch.exception.reinject);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004628 return;
4629 }
4630
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004631 if (vcpu->arch.nmi_injected) {
4632 kvm_x86_ops->set_nmi(vcpu);
4633 return;
4634 }
4635
4636 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004637 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004638 return;
4639 }
4640
4641 /* try to inject new event if pending */
4642 if (vcpu->arch.nmi_pending) {
4643 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4644 vcpu->arch.nmi_pending = false;
4645 vcpu->arch.nmi_injected = true;
4646 kvm_x86_ops->set_nmi(vcpu);
4647 }
4648 } else if (kvm_cpu_has_interrupt(vcpu)) {
4649 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004650 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4651 false);
4652 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004653 }
4654 }
4655}
4656
Dexuan Cui2acf9232010-06-10 11:27:12 +08004657static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
4658{
4659 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
4660 !vcpu->guest_xcr0_loaded) {
4661 /* kvm_set_xcr() also depends on this */
4662 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
4663 vcpu->guest_xcr0_loaded = 1;
4664 }
4665}
4666
4667static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
4668{
4669 if (vcpu->guest_xcr0_loaded) {
4670 if (vcpu->arch.xcr0 != host_xcr0)
4671 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
4672 vcpu->guest_xcr0_loaded = 0;
4673 }
4674}
4675
Avi Kivity851ba692009-08-24 11:10:17 +03004676static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004677{
4678 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004679 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004680 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004681
Avi Kivity3e007502010-06-23 14:26:18 +03004682 if (vcpu->requests) {
Avi Kivitya8eeb042010-05-10 12:34:53 +03004683 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004684 kvm_mmu_unload(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004685 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004686 __kvm_migrate_timers(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004687 if (kvm_check_request(KVM_REQ_KVMCLOCK_UPDATE, vcpu))
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004688 kvm_write_guest_time(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004689 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004690 kvm_mmu_sync_roots(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004691 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004692 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004693 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004694 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004695 r = 0;
4696 goto out;
4697 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03004698 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004699 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004700 r = 0;
4701 goto out;
4702 }
Avi Kivitya8eeb042010-05-10 12:34:53 +03004703 if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) {
Avi Kivity02daab22009-12-30 12:40:26 +02004704 vcpu->fpu_active = 0;
4705 kvm_x86_ops->fpu_deactivate(vcpu);
4706 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004707 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004708
Avi Kivity3e007502010-06-23 14:26:18 +03004709 r = kvm_mmu_reload(vcpu);
4710 if (unlikely(r))
4711 goto out;
4712
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004713 preempt_disable();
4714
4715 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004716 if (vcpu->fpu_active)
4717 kvm_load_guest_fpu(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08004718 kvm_load_guest_xcr0(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004719
Avi Kivityd94e1dc2010-05-03 16:54:48 +03004720 atomic_set(&vcpu->guest_mode, 1);
4721 smp_wmb();
4722
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004723 local_irq_disable();
4724
Avi Kivityd94e1dc2010-05-03 16:54:48 +03004725 if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
4726 || need_resched() || signal_pending(current)) {
4727 atomic_set(&vcpu->guest_mode, 0);
4728 smp_wmb();
Avi Kivity6c142802008-01-15 18:27:32 +02004729 local_irq_enable();
4730 preempt_enable();
4731 r = 1;
4732 goto out;
4733 }
4734
Avi Kivity851ba692009-08-24 11:10:17 +03004735 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004736
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004737 /* enable NMI/IRQ window open exits if needed */
4738 if (vcpu->arch.nmi_pending)
4739 kvm_x86_ops->enable_nmi_window(vcpu);
4740 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4741 kvm_x86_ops->enable_irq_window(vcpu);
4742
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004743 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004744 update_cr8_intercept(vcpu);
4745 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004746 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004747
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004748 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004749
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004750 kvm_guest_enter();
4751
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004752 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004753 set_debugreg(0, 7);
4754 set_debugreg(vcpu->arch.eff_db[0], 0);
4755 set_debugreg(vcpu->arch.eff_db[1], 1);
4756 set_debugreg(vcpu->arch.eff_db[2], 2);
4757 set_debugreg(vcpu->arch.eff_db[3], 3);
4758 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004759
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004760 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004761 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004762
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004763 /*
4764 * If the guest has used debug registers, at least dr7
4765 * will be disabled while returning to the host.
4766 * If we don't have active breakpoints in the host, we don't
4767 * care about the messed up debug address registers. But if
4768 * we have some of them active, restore the old state.
4769 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004770 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004771 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004772
Avi Kivityd94e1dc2010-05-03 16:54:48 +03004773 atomic_set(&vcpu->guest_mode, 0);
4774 smp_wmb();
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004775 local_irq_enable();
4776
4777 ++vcpu->stat.exits;
4778
4779 /*
4780 * We must have an instruction between local_irq_enable() and
4781 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4782 * the interrupt shadow. The stat.exits increment will do nicely.
4783 * But we need to prevent reordering, hence this barrier():
4784 */
4785 barrier();
4786
4787 kvm_guest_exit();
4788
4789 preempt_enable();
4790
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004791 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004792
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004793 /*
4794 * Profile KVM exit RIPs:
4795 */
4796 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004797 unsigned long rip = kvm_rip_read(vcpu);
4798 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004799 }
4800
Avi Kivity298101d2007-11-25 13:41:11 +02004801
Avi Kivityb93463a2007-10-25 16:52:32 +02004802 kvm_lapic_sync_from_vapic(vcpu);
4803
Avi Kivity851ba692009-08-24 11:10:17 +03004804 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004805out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004806 return r;
4807}
4808
Gleb Natapov09cec752009-03-23 15:11:44 +02004809
Avi Kivity851ba692009-08-24 11:10:17 +03004810static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004811{
4812 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004813 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004814
4815 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004816 pr_debug("vcpu %d received sipi with vector # %x\n",
4817 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004818 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004819 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004820 if (r)
4821 return r;
4822 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004823 }
4824
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004825 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004826 vapic_enter(vcpu);
4827
4828 r = 1;
4829 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004830 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004831 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004832 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004833 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004834 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004835 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Avi Kivitya8eeb042010-05-10 12:34:53 +03004836 if (kvm_check_request(KVM_REQ_UNHALT, vcpu))
Gleb Natapov09cec752009-03-23 15:11:44 +02004837 {
4838 switch(vcpu->arch.mp_state) {
4839 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004840 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004841 KVM_MP_STATE_RUNNABLE;
4842 case KVM_MP_STATE_RUNNABLE:
4843 break;
4844 case KVM_MP_STATE_SIPI_RECEIVED:
4845 default:
4846 r = -EINTR;
4847 break;
4848 }
4849 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004850 }
4851
Gleb Natapov09cec752009-03-23 15:11:44 +02004852 if (r <= 0)
4853 break;
4854
4855 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4856 if (kvm_cpu_has_pending_timer(vcpu))
4857 kvm_inject_pending_timer_irqs(vcpu);
4858
Avi Kivity851ba692009-08-24 11:10:17 +03004859 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004860 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004861 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004862 ++vcpu->stat.request_irq_exits;
4863 }
4864 if (signal_pending(current)) {
4865 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004866 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004867 ++vcpu->stat.signal_exits;
4868 }
4869 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004870 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004871 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004872 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004873 }
4874 }
4875
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004876 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004877
Avi Kivityb93463a2007-10-25 16:52:32 +02004878 vapic_exit(vcpu);
4879
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004880 return r;
4881}
4882
4883int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4884{
4885 int r;
4886 sigset_t sigsaved;
4887
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004888 if (vcpu->sigset_active)
4889 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4890
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004891 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4892 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004893 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004894 r = -EAGAIN;
4895 goto out;
4896 }
4897
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004898 /* re-sync apic's tpr */
4899 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004900 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004901
Gleb Natapov92bf9742010-03-18 15:20:28 +02004902 if (vcpu->arch.pio.count || vcpu->mmio_needed ||
4903 vcpu->arch.emulate_ctxt.restart) {
4904 if (vcpu->mmio_needed) {
4905 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4906 vcpu->mmio_read_completed = 1;
4907 vcpu->mmio_needed = 0;
Gleb Natapov79729952010-03-18 15:20:24 +02004908 }
Gleb Natapov7567cae2010-03-09 12:01:10 +02004909 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov5cd21912010-03-18 15:20:26 +02004910 r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004911 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004912 if (r != EMULATE_DONE) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004913 r = 0;
4914 goto out;
4915 }
4916 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004917 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4918 kvm_register_write(vcpu, VCPU_REGS_RAX,
4919 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004920
Avi Kivity851ba692009-08-24 11:10:17 +03004921 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004922
4923out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03004924 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004925 if (vcpu->sigset_active)
4926 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4927
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004928 return r;
4929}
4930
4931int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4932{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004933 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4934 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4935 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4936 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4937 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4938 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4939 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4940 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004941#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004942 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4943 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4944 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4945 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4946 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4947 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4948 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4949 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004950#endif
4951
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004952 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004953 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004954
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004955 return 0;
4956}
4957
4958int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4959{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004960 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4961 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4962 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4963 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4964 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4965 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4966 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4967 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004968#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004969 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4970 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4971 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4972 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4973 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4974 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4975 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4976 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004977#endif
4978
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004979 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004980 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004981
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004982 vcpu->arch.exception.pending = false;
4983
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004984 return 0;
4985}
4986
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004987void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4988{
4989 struct kvm_segment cs;
4990
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004991 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004992 *db = cs.db;
4993 *l = cs.l;
4994}
4995EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4996
4997int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4998 struct kvm_sregs *sregs)
4999{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005000 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005001
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005002 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5003 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5004 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5005 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5006 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5007 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005008
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005009 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5010 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005011
5012 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005013 sregs->idt.limit = dt.size;
5014 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005015 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005016 sregs->gdt.limit = dt.size;
5017 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005018
Avi Kivity4d4ec082009-12-29 18:07:30 +02005019 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005020 sregs->cr2 = vcpu->arch.cr2;
5021 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02005022 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005023 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02005024 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005025 sregs->apic_base = kvm_get_apic_base(vcpu);
5026
Gleb Natapov923c61b2009-05-11 13:35:48 +03005027 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005028
Gleb Natapov36752c92009-05-11 13:35:53 +03005029 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03005030 set_bit(vcpu->arch.interrupt.nr,
5031 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03005032
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005033 return 0;
5034}
5035
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03005036int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
5037 struct kvm_mp_state *mp_state)
5038{
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03005039 mp_state->mp_state = vcpu->arch.mp_state;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03005040 return 0;
5041}
5042
5043int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
5044 struct kvm_mp_state *mp_state)
5045{
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03005046 vcpu->arch.mp_state = mp_state->mp_state;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03005047 return 0;
5048}
5049
Jan Kiszkae269fb22010-04-14 15:51:09 +02005050int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
5051 bool has_error_code, u32 error_code)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005052{
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005053 struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
Gleb Natapovceffb452010-03-18 15:20:19 +02005054 int cs_db, cs_l, ret;
5055 cache_all_regs(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005056
Gleb Natapovceffb452010-03-18 15:20:19 +02005057 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Izik Eidus37817f22008-03-24 23:14:53 +02005058
Gleb Natapovceffb452010-03-18 15:20:19 +02005059 vcpu->arch.emulate_ctxt.vcpu = vcpu;
5060 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
5061 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
5062 vcpu->arch.emulate_ctxt.mode =
5063 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
5064 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
5065 ? X86EMUL_MODE_VM86 : cs_l
5066 ? X86EMUL_MODE_PROT64 : cs_db
5067 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005068 memset(c, 0, sizeof(struct decode_cache));
5069 memcpy(c->regs, vcpu->arch.regs, sizeof c->regs);
Izik Eidus37817f22008-03-24 23:14:53 +02005070
Gleb Natapovceffb452010-03-18 15:20:19 +02005071 ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02005072 tss_selector, reason, has_error_code,
5073 error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005074
Gleb Natapovc6975182010-02-18 12:15:01 +02005075 if (ret)
Gleb Natapov19d04432010-04-15 12:29:50 +03005076 return EMULATE_FAIL;
Gleb Natapovc6975182010-02-18 12:15:01 +02005077
Gleb Natapov4d2179e2010-04-28 19:15:42 +03005078 memcpy(vcpu->arch.regs, c->regs, sizeof c->regs);
Gleb Natapov95c55882010-04-28 19:15:39 +03005079 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
Gleb Natapov19d04432010-04-15 12:29:50 +03005080 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
5081 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02005082}
5083EXPORT_SYMBOL_GPL(kvm_task_switch);
5084
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005085int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5086 struct kvm_sregs *sregs)
5087{
5088 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03005089 int pending_vec, max_bits;
Gleb Natapov89a27f42010-02-16 10:51:48 +02005090 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005091
Gleb Natapov89a27f42010-02-16 10:51:48 +02005092 dt.size = sregs->idt.limit;
5093 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005094 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005095 dt.size = sregs->gdt.limit;
5096 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005097 kvm_x86_ops->set_gdt(vcpu, &dt);
5098
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005099 vcpu->arch.cr2 = sregs->cr2;
5100 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02005101 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005102
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005103 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005104
Avi Kivityf6801df2010-01-21 15:31:50 +02005105 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005106 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005107 kvm_set_apic_base(vcpu, sregs->apic_base);
5108
Avi Kivity4d4ec082009-12-29 18:07:30 +02005109 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005110 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00005111 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005112
Avi Kivityfc78f512009-12-07 12:16:48 +02005113 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005114 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005115 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005116 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005117 mmu_reset_needed = 1;
5118 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005119
5120 if (mmu_reset_needed)
5121 kvm_mmu_reset_context(vcpu);
5122
Gleb Natapov923c61b2009-05-11 13:35:48 +03005123 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5124 pending_vec = find_first_bit(
5125 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5126 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005127 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03005128 pr_debug("Set back pending irq %d\n", pending_vec);
5129 if (irqchip_in_kernel(vcpu->kvm))
5130 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005131 }
5132
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005133 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5134 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5135 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5136 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5137 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5138 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005139
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005140 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5141 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005142
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03005143 update_cr8_intercept(vcpu);
5144
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005145 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005146 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005147 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02005148 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005149 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5150
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005151 return 0;
5152}
5153
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005154int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5155 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005156{
Jan Kiszka355be0b2009-10-03 00:31:21 +02005157 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005158 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005159
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005160 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5161 r = -EBUSY;
5162 if (vcpu->arch.exception.pending)
Avi Kivity2122ff52010-05-13 11:25:04 +03005163 goto out;
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005164 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5165 kvm_queue_exception(vcpu, DB_VECTOR);
5166 else
5167 kvm_queue_exception(vcpu, BP_VECTOR);
5168 }
5169
Jan Kiszka91586a32009-10-05 13:07:21 +02005170 /*
5171 * Read rflags as long as potentially injected trace flags are still
5172 * filtered out.
5173 */
5174 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02005175
5176 vcpu->guest_debug = dbg->control;
5177 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5178 vcpu->guest_debug = 0;
5179
5180 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005181 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5182 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5183 vcpu->arch.switch_db_regs =
5184 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5185 } else {
5186 for (i = 0; i < KVM_NR_DB_REGS; i++)
5187 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5188 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5189 }
5190
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005191 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5192 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5193 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005194
Jan Kiszka91586a32009-10-05 13:07:21 +02005195 /*
5196 * Trigger an rflags update that will inject or remove the trace
5197 * flags.
5198 */
5199 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005200
Jan Kiszka355be0b2009-10-03 00:31:21 +02005201 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5202
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005203 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005204
Avi Kivity2122ff52010-05-13 11:25:04 +03005205out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005206
5207 return r;
5208}
5209
5210/*
Zhang Xiantao8b006792007-11-16 13:05:55 +08005211 * Translate a guest virtual address to a guest physical address.
5212 */
5213int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5214 struct kvm_translation *tr)
5215{
5216 unsigned long vaddr = tr->linear_address;
5217 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005218 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005219
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005220 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005221 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005222 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005223 tr->physical_address = gpa;
5224 tr->valid = gpa != UNMAPPED_GVA;
5225 tr->writeable = 1;
5226 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005227
5228 return 0;
5229}
5230
Hollis Blanchardd0752062007-10-31 17:24:25 -05005231int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5232{
Sheng Yang98918832010-05-17 17:08:28 +08005233 struct i387_fxsave_struct *fxsave =
5234 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005235
Hollis Blanchardd0752062007-10-31 17:24:25 -05005236 memcpy(fpu->fpr, fxsave->st_space, 128);
5237 fpu->fcw = fxsave->cwd;
5238 fpu->fsw = fxsave->swd;
5239 fpu->ftwx = fxsave->twd;
5240 fpu->last_opcode = fxsave->fop;
5241 fpu->last_ip = fxsave->rip;
5242 fpu->last_dp = fxsave->rdp;
5243 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5244
Hollis Blanchardd0752062007-10-31 17:24:25 -05005245 return 0;
5246}
5247
5248int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5249{
Sheng Yang98918832010-05-17 17:08:28 +08005250 struct i387_fxsave_struct *fxsave =
5251 &vcpu->arch.guest_fpu.state->fxsave;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005252
Hollis Blanchardd0752062007-10-31 17:24:25 -05005253 memcpy(fxsave->st_space, fpu->fpr, 128);
5254 fxsave->cwd = fpu->fcw;
5255 fxsave->swd = fpu->fsw;
5256 fxsave->twd = fpu->ftwx;
5257 fxsave->fop = fpu->last_opcode;
5258 fxsave->rip = fpu->last_ip;
5259 fxsave->rdp = fpu->last_dp;
5260 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5261
Hollis Blanchardd0752062007-10-31 17:24:25 -05005262 return 0;
5263}
5264
Jan Kiszka10ab25c2010-05-25 16:01:50 +02005265int fx_init(struct kvm_vcpu *vcpu)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005266{
Jan Kiszka10ab25c2010-05-25 16:01:50 +02005267 int err;
5268
5269 err = fpu_alloc(&vcpu->arch.guest_fpu);
5270 if (err)
5271 return err;
5272
Sheng Yang98918832010-05-17 17:08:28 +08005273 fpu_finit(&vcpu->arch.guest_fpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005274
Dexuan Cui2acf9232010-06-10 11:27:12 +08005275 /*
5276 * Ensure guest xcr0 is valid for loading
5277 */
5278 vcpu->arch.xcr0 = XSTATE_FP;
5279
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005280 vcpu->arch.cr0 |= X86_CR0_ET;
Jan Kiszka10ab25c2010-05-25 16:01:50 +02005281
5282 return 0;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005283}
5284EXPORT_SYMBOL_GPL(fx_init);
5285
Sheng Yang98918832010-05-17 17:08:28 +08005286static void fx_free(struct kvm_vcpu *vcpu)
5287{
5288 fpu_free(&vcpu->arch.guest_fpu);
5289}
5290
Hollis Blanchardd0752062007-10-31 17:24:25 -05005291void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5292{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005293 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005294 return;
5295
Dexuan Cui2acf9232010-06-10 11:27:12 +08005296 /*
5297 * Restore all possible states in the guest,
5298 * and assume host would use all available bits.
5299 * Guest xcr0 would be loaded later.
5300 */
5301 kvm_put_guest_xcr0(vcpu);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005302 vcpu->guest_fpu_loaded = 1;
Sheng Yang7cf30852010-05-17 17:08:27 +08005303 unlazy_fpu(current);
Sheng Yang98918832010-05-17 17:08:28 +08005304 fpu_restore_checking(&vcpu->arch.guest_fpu);
Avi Kivity0c048512010-01-21 15:31:52 +02005305 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005306}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005307
5308void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5309{
Dexuan Cui2acf9232010-06-10 11:27:12 +08005310 kvm_put_guest_xcr0(vcpu);
5311
Hollis Blanchardd0752062007-10-31 17:24:25 -05005312 if (!vcpu->guest_fpu_loaded)
5313 return;
5314
5315 vcpu->guest_fpu_loaded = 0;
Sheng Yang98918832010-05-17 17:08:28 +08005316 fpu_save_init(&vcpu->arch.guest_fpu);
Avi Kivityf096ed82007-11-18 13:54:33 +02005317 ++vcpu->stat.fpu_reload;
Avi Kivitya8eeb042010-05-10 12:34:53 +03005318 kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu);
Avi Kivity0c048512010-01-21 15:31:52 +02005319 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005320}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005321
5322void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5323{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005324 if (vcpu->arch.time_page) {
5325 kvm_release_page_dirty(vcpu->arch.time_page);
5326 vcpu->arch.time_page = NULL;
5327 }
5328
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005329 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
Sheng Yang98918832010-05-17 17:08:28 +08005330 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005331 kvm_x86_ops->vcpu_free(vcpu);
5332}
5333
5334struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5335 unsigned int id)
5336{
Avi Kivity26e52152007-11-20 15:30:24 +02005337 return kvm_x86_ops->vcpu_create(kvm, id);
5338}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005339
Avi Kivity26e52152007-11-20 15:30:24 +02005340int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5341{
5342 int r;
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
Sheng Yang98918832010-05-17 17:08:28 +08005365 fx_free(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005366 kvm_x86_ops->vcpu_free(vcpu);
5367}
5368
5369int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5370{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005371 vcpu->arch.nmi_pending = false;
5372 vcpu->arch.nmi_injected = false;
5373
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005374 vcpu->arch.switch_db_regs = 0;
5375 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5376 vcpu->arch.dr6 = DR6_FIXED_1;
5377 vcpu->arch.dr7 = DR7_FIXED_1;
5378
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005379 return kvm_x86_ops->vcpu_reset(vcpu);
5380}
5381
Alexander Graf10474ae2009-09-15 11:37:46 +02005382int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005383{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005384 /*
5385 * Since this may be called from a hotplug notifcation,
5386 * we can't get the CPU frequency directly.
5387 */
5388 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5389 int cpu = raw_smp_processor_id();
5390 per_cpu(cpu_tsc_khz, cpu) = 0;
5391 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005392
5393 kvm_shared_msr_cpu_online();
5394
Alexander Graf10474ae2009-09-15 11:37:46 +02005395 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005396}
5397
5398void kvm_arch_hardware_disable(void *garbage)
5399{
5400 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005401 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005402}
5403
5404int kvm_arch_hardware_setup(void)
5405{
5406 return kvm_x86_ops->hardware_setup();
5407}
5408
5409void kvm_arch_hardware_unsetup(void)
5410{
5411 kvm_x86_ops->hardware_unsetup();
5412}
5413
5414void kvm_arch_check_processor_compat(void *rtn)
5415{
5416 kvm_x86_ops->check_processor_compatibility(rtn);
5417}
5418
5419int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5420{
5421 struct page *page;
5422 struct kvm *kvm;
5423 int r;
5424
5425 BUG_ON(vcpu->kvm == NULL);
5426 kvm = vcpu->kvm;
5427
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005428 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005429 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005430 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005431 else
Avi Kivitya4535292008-04-13 17:54:35 +03005432 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005433
5434 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5435 if (!page) {
5436 r = -ENOMEM;
5437 goto fail;
5438 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005439 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005440
5441 r = kvm_mmu_create(vcpu);
5442 if (r < 0)
5443 goto fail_free_pio_data;
5444
5445 if (irqchip_in_kernel(kvm)) {
5446 r = kvm_create_lapic(vcpu);
5447 if (r < 0)
5448 goto fail_mmu_destroy;
5449 }
5450
Huang Ying890ca9a2009-05-11 16:48:15 +08005451 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5452 GFP_KERNEL);
5453 if (!vcpu->arch.mce_banks) {
5454 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005455 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005456 }
5457 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5458
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005459 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
5460 goto fail_free_mce_banks;
5461
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005462 return 0;
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005463fail_free_mce_banks:
5464 kfree(vcpu->arch.mce_banks);
Wei Yongjun443c39b2010-01-22 14:21:29 +08005465fail_free_lapic:
5466 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005467fail_mmu_destroy:
5468 kvm_mmu_destroy(vcpu);
5469fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005470 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005471fail:
5472 return r;
5473}
5474
5475void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5476{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005477 int idx;
5478
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005479 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005480 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005481 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005482 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005483 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005484 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005485}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005486
5487struct kvm *kvm_arch_create_vm(void)
5488{
5489 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5490
5491 if (!kvm)
5492 return ERR_PTR(-ENOMEM);
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);
Xiao Guangrongaea924f2010-07-10 17:37:56 +08005536 kvm_free_pit(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005537}
5538
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005539void kvm_arch_destroy_vm(struct kvm *kvm)
5540{
Sheng Yang6eb55812008-10-31 12:37:41 +08005541 kvm_iommu_unmap_guest(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 Tosatti64749202010-01-19 12:45:23 -02005550 cleanup_srcu_struct(&kvm->srcu);
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;
Avi Kivity7ac77092010-06-21 10:57:45 +03005561 int map_flags = MAP_PRIVATE | MAP_ANONYMOUS;
5562
5563 /* Prevent internal slot pages from being moved by fork()/COW. */
5564 if (memslot->id >= KVM_MEMORY_SLOTS)
5565 map_flags = MAP_SHARED | MAP_ANONYMOUS;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005566
5567 /*To keep backward compatibility with older userspace,
5568 *x86 needs to hanlde !user_alloc case.
5569 */
5570 if (!user_alloc) {
5571 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005572 unsigned long userspace_addr;
5573
Izik Eidus72dc67a2008-02-10 18:04:15 +02005574 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005575 userspace_addr = do_mmap(NULL, 0,
5576 npages * PAGE_SIZE,
5577 PROT_READ | PROT_WRITE,
Avi Kivity7ac77092010-06-21 10:57:45 +03005578 map_flags,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005579 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005580 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005581
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005582 if (IS_ERR((void *)userspace_addr))
5583 return PTR_ERR((void *)userspace_addr);
5584
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005585 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005586 }
5587 }
5588
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005589
5590 return 0;
5591}
5592
5593void kvm_arch_commit_memory_region(struct kvm *kvm,
5594 struct kvm_userspace_memory_region *mem,
5595 struct kvm_memory_slot old,
5596 int user_alloc)
5597{
5598
5599 int npages = mem->memory_size >> PAGE_SHIFT;
5600
5601 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5602 int ret;
5603
5604 down_write(&current->mm->mmap_sem);
5605 ret = do_munmap(current->mm, old.userspace_addr,
5606 old.npages * PAGE_SIZE);
5607 up_write(&current->mm->mmap_sem);
5608 if (ret < 0)
5609 printk(KERN_WARNING
5610 "kvm_vm_ioctl_set_memory_region: "
5611 "failed to munmap memory\n");
5612 }
5613
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005614 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005615 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005616 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5617 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5618 }
5619
5620 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005621 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005622}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005623
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005624void kvm_arch_flush_shadow(struct kvm *kvm)
5625{
5626 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005627 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005628}
5629
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005630int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5631{
Avi Kivitya4535292008-04-13 17:54:35 +03005632 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005633 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5634 || vcpu->arch.nmi_pending ||
5635 (kvm_arch_interrupt_allowed(vcpu) &&
5636 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005637}
Zhang Xiantao57361992007-12-17 14:21:40 +08005638
Zhang Xiantao57361992007-12-17 14:21:40 +08005639void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5640{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005641 int me;
5642 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005643
5644 if (waitqueue_active(&vcpu->wq)) {
5645 wake_up_interruptible(&vcpu->wq);
5646 ++vcpu->stat.halt_wakeup;
5647 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005648
5649 me = get_cpu();
5650 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
Avi Kivityd94e1dc2010-05-03 16:54:48 +03005651 if (atomic_xchg(&vcpu->guest_mode, 0))
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005652 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005653 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005654}
Gleb Natapov78646122009-03-23 12:12:11 +02005655
5656int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5657{
5658 return kvm_x86_ops->interrupt_allowed(vcpu);
5659}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005660
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005661bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5662{
5663 unsigned long current_rip = kvm_rip_read(vcpu) +
5664 get_segment_base(vcpu, VCPU_SREG_CS);
5665
5666 return current_rip == linear_rip;
5667}
5668EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5669
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005670unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5671{
5672 unsigned long rflags;
5673
5674 rflags = kvm_x86_ops->get_rflags(vcpu);
5675 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01005676 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005677 return rflags;
5678}
5679EXPORT_SYMBOL_GPL(kvm_get_rflags);
5680
5681void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5682{
5683 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005684 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01005685 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005686 kvm_x86_ops->set_rflags(vcpu, rflags);
5687}
5688EXPORT_SYMBOL_GPL(kvm_set_rflags);
5689
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005690EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5691EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5692EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5693EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5694EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005695EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005696EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005697EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005698EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005699EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005700EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01005701EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);