blob: 161ede2b5f91441fe213714ede61e571799ab05f [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010034#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080035#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050036#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010037#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030038#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030040#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020041#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080043#include <linux/perf_event.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030044#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080045
Marcelo Tosatti229456f2009-06-17 09:22:14 -030046#define CREATE_TRACE_POINTS
47#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020048
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020049#include <asm/debugreg.h>
Carsten Otte043405e2007-10-10 17:16:19 +020050#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080051#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020052#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080053#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080054#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020055
Carsten Otte313a3dc2007-10-11 19:16:52 +020056#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010057#define CR0_RESERVED_BITS \
58 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
59 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
60 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
61#define CR4_RESERVED_BITS \
62 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
63 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
64 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
65 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
66
67#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080068
69#define KVM_MAX_MCE_BANKS 32
70#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
71
Joerg Roedel50a37eb2008-01-31 14:57:38 +010072/* EFER defaults:
73 * - enable syscall per default because its emulated by KVM
74 * - enable LME and LMA per default on 64 bit KVM
75 */
76#ifdef CONFIG_X86_64
77static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
78#else
79static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
80#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020081
Avi Kivityba1389b2007-11-18 16:24:12 +020082#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
83#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050084
Gleb Natapovcb142eb2009-08-09 15:17:40 +030085static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020086static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
87 struct kvm_cpuid_entry2 __user *entries);
88
Zhang Xiantao97896d02007-11-14 20:09:30 +080089struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030090EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080091
Andre Przywaraed85c062009-06-25 12:36:49 +020092int ignore_msrs = 0;
93module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
94
Avi Kivity18863bd2009-09-07 11:12:18 +030095#define KVM_NR_SHARED_MSRS 16
96
97struct kvm_shared_msrs_global {
98 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +080099 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300100};
101
102struct kvm_shared_msrs {
103 struct user_return_notifier urn;
104 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800105 struct kvm_shared_msr_values {
106 u64 host;
107 u64 curr;
108 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300109};
110
111static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
112static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
113
Hollis Blanchard417bc302007-10-31 17:24:23 -0500114struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200115 { "pf_fixed", VCPU_STAT(pf_fixed) },
116 { "pf_guest", VCPU_STAT(pf_guest) },
117 { "tlb_flush", VCPU_STAT(tlb_flush) },
118 { "invlpg", VCPU_STAT(invlpg) },
119 { "exits", VCPU_STAT(exits) },
120 { "io_exits", VCPU_STAT(io_exits) },
121 { "mmio_exits", VCPU_STAT(mmio_exits) },
122 { "signal_exits", VCPU_STAT(signal_exits) },
123 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800124 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200125 { "halt_exits", VCPU_STAT(halt_exits) },
126 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530127 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200128 { "request_irq", VCPU_STAT(request_irq_exits) },
129 { "irq_exits", VCPU_STAT(irq_exits) },
130 { "host_state_reload", VCPU_STAT(host_state_reload) },
131 { "efer_reload", VCPU_STAT(efer_reload) },
132 { "fpu_reload", VCPU_STAT(fpu_reload) },
133 { "insn_emulation", VCPU_STAT(insn_emulation) },
134 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300135 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200136 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200137 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
138 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
139 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
140 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
141 { "mmu_flooded", VM_STAT(mmu_flooded) },
142 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200143 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300144 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200145 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300146 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500147 { NULL }
148};
149
Avi Kivity18863bd2009-09-07 11:12:18 +0300150static void kvm_on_user_return(struct user_return_notifier *urn)
151{
152 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300153 struct kvm_shared_msrs *locals
154 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800155 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300156
157 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800158 values = &locals->values[slot];
159 if (values->host != values->curr) {
160 wrmsrl(shared_msrs_global.msrs[slot], values->host);
161 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300162 }
163 }
164 locals->registered = false;
165 user_return_notifier_unregister(urn);
166}
167
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800168static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300169{
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800170 struct kvm_shared_msrs *smsr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300171 u64 value;
172
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800173 smsr = &__get_cpu_var(shared_msrs);
174 /* only read, and nobody should modify it at this time,
175 * so don't need lock */
176 if (slot >= shared_msrs_global.nr) {
177 printk(KERN_ERR "kvm: invalid MSR slot!");
178 return;
179 }
180 rdmsrl_safe(msr, &value);
181 smsr->values[slot].host = value;
182 smsr->values[slot].curr = value;
183}
184
185void kvm_define_shared_msr(unsigned slot, u32 msr)
186{
Avi Kivity18863bd2009-09-07 11:12:18 +0300187 if (slot >= shared_msrs_global.nr)
188 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800189 shared_msrs_global.msrs[slot] = msr;
190 /* we need ensured the shared_msr_global have been updated */
191 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300192}
193EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
194
195static void kvm_shared_msr_cpu_online(void)
196{
197 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300198
199 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800200 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300201}
202
Avi Kivityd5696722009-12-02 12:28:47 +0200203void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300204{
205 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
206
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800207 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300208 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800209 smsr->values[slot].curr = value;
210 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300211 if (!smsr->registered) {
212 smsr->urn.on_user_return = kvm_on_user_return;
213 user_return_notifier_register(&smsr->urn);
214 smsr->registered = true;
215 }
216}
217EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
218
Avi Kivity3548bab2009-11-28 14:18:47 +0200219static void drop_user_return_notifiers(void *ignore)
220{
221 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
222
223 if (smsr->registered)
224 kvm_on_user_return(&smsr->urn);
225}
226
Carsten Otte6866b832007-10-29 16:09:10 +0100227u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
228{
229 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800230 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100231 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800232 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100233}
234EXPORT_SYMBOL_GPL(kvm_get_apic_base);
235
236void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
237{
238 /* TODO: reserve bits check */
239 if (irqchip_in_kernel(vcpu->kvm))
240 kvm_lapic_set_base(vcpu, data);
241 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800242 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100243}
244EXPORT_SYMBOL_GPL(kvm_set_apic_base);
245
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200246#define EXCPT_BENIGN 0
247#define EXCPT_CONTRIBUTORY 1
248#define EXCPT_PF 2
249
250static int exception_class(int vector)
251{
252 switch (vector) {
253 case PF_VECTOR:
254 return EXCPT_PF;
255 case DE_VECTOR:
256 case TS_VECTOR:
257 case NP_VECTOR:
258 case SS_VECTOR:
259 case GP_VECTOR:
260 return EXCPT_CONTRIBUTORY;
261 default:
262 break;
263 }
264 return EXCPT_BENIGN;
265}
266
267static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200268 unsigned nr, bool has_error, u32 error_code,
269 bool reinject)
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200270{
271 u32 prev_nr;
272 int class1, class2;
273
274 if (!vcpu->arch.exception.pending) {
275 queue:
276 vcpu->arch.exception.pending = true;
277 vcpu->arch.exception.has_error_code = has_error;
278 vcpu->arch.exception.nr = nr;
279 vcpu->arch.exception.error_code = error_code;
Joerg Roedel3f0fd292010-05-05 16:04:41 +0200280 vcpu->arch.exception.reinject = reinject;
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200281 return;
282 }
283
284 /* to check exception */
285 prev_nr = vcpu->arch.exception.nr;
286 if (prev_nr == DF_VECTOR) {
287 /* triple fault -> shutdown */
288 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
289 return;
290 }
291 class1 = exception_class(prev_nr);
292 class2 = exception_class(nr);
293 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
294 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
295 /* generate double fault per SDM Table 5-5 */
296 vcpu->arch.exception.pending = true;
297 vcpu->arch.exception.has_error_code = true;
298 vcpu->arch.exception.nr = DF_VECTOR;
299 vcpu->arch.exception.error_code = 0;
300 } else
301 /* replace previous exception with a new one in a hope
302 that instruction re-execution will regenerate lost
303 exception */
304 goto queue;
305}
306
Avi Kivity298101d2007-11-25 13:41:11 +0200307void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
308{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200309 kvm_multiple_exception(vcpu, nr, false, 0, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200310}
311EXPORT_SYMBOL_GPL(kvm_queue_exception);
312
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200313void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
314{
315 kvm_multiple_exception(vcpu, nr, false, 0, true);
316}
317EXPORT_SYMBOL_GPL(kvm_requeue_exception);
318
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200319void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
320 u32 error_code)
321{
322 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200324 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
325}
326
Sheng Yang3419ffc2008-05-15 09:52:48 +0800327void kvm_inject_nmi(struct kvm_vcpu *vcpu)
328{
329 vcpu->arch.nmi_pending = 1;
330}
331EXPORT_SYMBOL_GPL(kvm_inject_nmi);
332
Avi Kivity298101d2007-11-25 13:41:11 +0200333void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
334{
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200335 kvm_multiple_exception(vcpu, nr, true, error_code, false);
Avi Kivity298101d2007-11-25 13:41:11 +0200336}
337EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
338
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200339void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
340{
341 kvm_multiple_exception(vcpu, nr, true, error_code, true);
342}
343EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
344
Carsten Ottea03490e2007-10-29 16:09:35 +0100345/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300346 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
347 * a #GP and return false.
348 */
349bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200350{
Avi Kivity0a79b002009-09-01 12:03:25 +0300351 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
352 return true;
353 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
354 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100355}
Avi Kivity0a79b002009-09-01 12:03:25 +0300356EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100357
358/*
359 * Load the pae pdptrs. Return true is they are all valid.
360 */
361int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
362{
363 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
364 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
365 int i;
366 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800367 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100368
Carsten Ottea03490e2007-10-29 16:09:35 +0100369 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
370 offset * sizeof(u64), sizeof(pdpte));
371 if (ret < 0) {
372 ret = 0;
373 goto out;
374 }
375 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300376 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800377 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100378 ret = 0;
379 goto out;
380 }
381 }
382 ret = 1;
383
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800384 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300385 __set_bit(VCPU_EXREG_PDPTR,
386 (unsigned long *)&vcpu->arch.regs_avail);
387 __set_bit(VCPU_EXREG_PDPTR,
388 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100389out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100390
391 return ret;
392}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100393EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100394
Avi Kivityd835dfe2007-11-21 02:57:59 +0200395static bool pdptrs_changed(struct kvm_vcpu *vcpu)
396{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800397 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200398 bool changed = true;
399 int r;
400
401 if (is_long_mode(vcpu) || !is_pae(vcpu))
402 return false;
403
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300404 if (!test_bit(VCPU_EXREG_PDPTR,
405 (unsigned long *)&vcpu->arch.regs_avail))
406 return true;
407
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800408 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200409 if (r < 0)
410 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200412out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200413
414 return changed;
415}
416
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200417void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100418{
Avi Kivityf9a48e62010-01-06 19:10:22 +0200419 cr0 |= X86_CR0_ET;
420
Gleb Natapovab344822010-01-21 15:28:46 +0200421#ifdef CONFIG_X86_64
422 if (cr0 & 0xffffffff00000000UL) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200423 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100424 return;
425 }
Gleb Natapovab344822010-01-21 15:28:46 +0200426#endif
427
428 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100429
430 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200431 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100432 return;
433 }
434
435 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200436 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100437 return;
438 }
439
440 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
441#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200442 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100443 int cs_db, cs_l;
444
445 if (!is_pae(vcpu)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200446 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100447 return;
448 }
449 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
450 if (cs_l) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200451 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100452 return;
453
454 }
455 } else
456#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800457 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200458 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100459 return;
460 }
461
462 }
463
464 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100465
Carsten Ottea03490e2007-10-29 16:09:35 +0100466 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100467 return;
468}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200469EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100470
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200471void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100472{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200473 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100474}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200475EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100476
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200477void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100478{
Avi Kivityfc78f512009-12-07 12:16:48 +0200479 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300480 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
481
Carsten Ottea03490e2007-10-29 16:09:35 +0100482 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200483 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100484 return;
485 }
486
487 if (is_long_mode(vcpu)) {
488 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200489 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100490 return;
491 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300492 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
493 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800494 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200495 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100496 return;
497 }
498
499 if (cr4 & X86_CR4_VMXE) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200500 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100501 return;
502 }
503 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800504 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100505 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100506}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200507EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100508
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200509void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100510{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800511 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300512 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200513 kvm_mmu_flush_tlb(vcpu);
514 return;
515 }
516
Carsten Ottea03490e2007-10-29 16:09:35 +0100517 if (is_long_mode(vcpu)) {
518 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200519 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100520 return;
521 }
522 } else {
523 if (is_pae(vcpu)) {
524 if (cr3 & CR3_PAE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200525 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100526 return;
527 }
528 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200529 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100530 return;
531 }
532 }
533 /*
534 * We don't check reserved bits in nonpae mode, because
535 * this isn't enforced, and VMware depends on this.
536 */
537 }
538
Carsten Ottea03490e2007-10-29 16:09:35 +0100539 /*
540 * Does the new cr3 value map to physical memory? (Note, we
541 * catch an invalid cr3 even in real-mode, because it would
542 * cause trouble later on when we turn on paging anyway.)
543 *
544 * A real CPU would silently accept an invalid cr3 and would
545 * attempt to use it - with largely undefined (and often hard
546 * to debug) behavior on the guest side.
547 */
548 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200549 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100550 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800551 vcpu->arch.cr3 = cr3;
552 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100553 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100554}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200555EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100556
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200557void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100558{
559 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200560 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100561 return;
562 }
563 if (irqchip_in_kernel(vcpu->kvm))
564 kvm_lapic_set_tpr(vcpu, cr8);
565 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800566 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100567}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200568EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100569
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200570unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100571{
572 if (irqchip_in_kernel(vcpu->kvm))
573 return kvm_lapic_get_cr8(vcpu);
574 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800575 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100576}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200577EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100578
Gleb Natapov020df072010-04-13 10:05:23 +0300579int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
580{
581 switch (dr) {
582 case 0 ... 3:
583 vcpu->arch.db[dr] = val;
584 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
585 vcpu->arch.eff_db[dr] = val;
586 break;
587 case 4:
588 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
589 kvm_queue_exception(vcpu, UD_VECTOR);
590 return 1;
591 }
592 /* fall through */
593 case 6:
594 if (val & 0xffffffff00000000ULL) {
595 kvm_inject_gp(vcpu, 0);
596 return 1;
597 }
598 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
599 break;
600 case 5:
601 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
602 kvm_queue_exception(vcpu, UD_VECTOR);
603 return 1;
604 }
605 /* fall through */
606 default: /* 7 */
607 if (val & 0xffffffff00000000ULL) {
608 kvm_inject_gp(vcpu, 0);
609 return 1;
610 }
611 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
612 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
613 kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
614 vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
615 }
616 break;
617 }
618
619 return 0;
620}
621EXPORT_SYMBOL_GPL(kvm_set_dr);
622
623int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
624{
625 switch (dr) {
626 case 0 ... 3:
627 *val = vcpu->arch.db[dr];
628 break;
629 case 4:
630 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
631 kvm_queue_exception(vcpu, UD_VECTOR);
632 return 1;
633 }
634 /* fall through */
635 case 6:
636 *val = vcpu->arch.dr6;
637 break;
638 case 5:
639 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
640 kvm_queue_exception(vcpu, UD_VECTOR);
641 return 1;
642 }
643 /* fall through */
644 default: /* 7 */
645 *val = vcpu->arch.dr7;
646 break;
647 }
648
649 return 0;
650}
651EXPORT_SYMBOL_GPL(kvm_get_dr);
652
Alexander Grafd8017472008-11-25 20:17:11 +0100653static inline u32 bit(int bitno)
654{
655 return 1 << (bitno & 31);
656}
657
Carsten Otte043405e2007-10-10 17:16:19 +0200658/*
659 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
660 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
661 *
662 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400663 * capabilities of the host cpu. This capabilities test skips MSRs that are
664 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200665 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400666
Gleb Natapov10388a02010-01-17 15:51:23 +0200667#define KVM_SAVE_MSRS_BEGIN 5
Carsten Otte043405e2007-10-10 17:16:19 +0200668static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400669 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200670 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200671 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200672 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
673 MSR_K6_STAR,
674#ifdef CONFIG_X86_64
675 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
676#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400677 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200678};
679
680static unsigned num_msrs_to_save;
681
682static u32 emulated_msrs[] = {
683 MSR_IA32_MISC_ENABLE,
684};
685
Carsten Otte15c4a642007-10-30 18:44:17 +0100686static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
687{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100688 if (efer & efer_reserved_bits) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200689 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100690 return;
691 }
692
693 if (is_paging(vcpu)
Avi Kivityf6801df2010-01-21 15:31:50 +0200694 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200695 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100696 return;
697 }
698
Alexander Graf1b2fd702009-02-02 16:23:51 +0100699 if (efer & EFER_FFXSR) {
700 struct kvm_cpuid_entry2 *feat;
701
702 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
703 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
Alexander Graf1b2fd702009-02-02 16:23:51 +0100704 kvm_inject_gp(vcpu, 0);
705 return;
706 }
707 }
708
Alexander Grafd8017472008-11-25 20:17:11 +0100709 if (efer & EFER_SVME) {
710 struct kvm_cpuid_entry2 *feat;
711
712 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
713 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
Alexander Grafd8017472008-11-25 20:17:11 +0100714 kvm_inject_gp(vcpu, 0);
715 return;
716 }
717 }
718
Carsten Otte15c4a642007-10-30 18:44:17 +0100719 kvm_x86_ops->set_efer(vcpu, efer);
720
721 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200722 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100723
Avi Kivityf6801df2010-01-21 15:31:50 +0200724 vcpu->arch.efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300725
726 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
727 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100728}
729
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100730void kvm_enable_efer_bits(u64 mask)
731{
732 efer_reserved_bits &= ~mask;
733}
734EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
735
736
Carsten Otte15c4a642007-10-30 18:44:17 +0100737/*
738 * Writes msr value into into the appropriate "register".
739 * Returns 0 on success, non-0 otherwise.
740 * Assumes vcpu_load() was already called.
741 */
742int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
743{
744 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
745}
746
Carsten Otte313a3dc2007-10-11 19:16:52 +0200747/*
748 * Adapt set_msr() to msr_io()'s calling convention
749 */
750static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
751{
752 return kvm_set_msr(vcpu, index, *data);
753}
754
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200755static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
756{
Avi Kivity9ed3c442010-05-04 15:00:37 +0300757 int version;
758 int r;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200759 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800760 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200761
762 if (!wall_clock)
763 return;
764
Avi Kivity9ed3c442010-05-04 15:00:37 +0300765 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
766 if (r)
767 return;
768
769 if (version & 1)
770 ++version; /* first time write, random junk */
771
772 ++version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200773
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200774 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
775
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200776 /*
777 * The guest calculates current wall clock time by adding
778 * system time (updated by kvm_write_guest_time below) to the
779 * wall clock specified here. guest system time equals host
780 * system time for us, thus we must fill in host boot time here.
781 */
Jason Wang923de3c2010-01-27 19:13:49 +0800782 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200783
784 wc.sec = boot.tv_sec;
785 wc.nsec = boot.tv_nsec;
786 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200787
788 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
789
790 version++;
791 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200792}
793
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200794static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
795{
796 uint32_t quotient, remainder;
797
798 /* Don't try to replace with do_div(), this one calculates
799 * "(dividend << 32) / divisor" */
800 __asm__ ( "divl %4"
801 : "=a" (quotient), "=d" (remainder)
802 : "0" (0), "1" (dividend), "r" (divisor) );
803 return quotient;
804}
805
806static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
807{
808 uint64_t nsecs = 1000000000LL;
809 int32_t shift = 0;
810 uint64_t tps64;
811 uint32_t tps32;
812
813 tps64 = tsc_khz * 1000LL;
814 while (tps64 > nsecs*2) {
815 tps64 >>= 1;
816 shift--;
817 }
818
819 tps32 = (uint32_t)tps64;
820 while (tps32 <= (uint32_t)nsecs) {
821 tps32 <<= 1;
822 shift++;
823 }
824
825 hv_clock->tsc_shift = shift;
826 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
827
828 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700829 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200830 hv_clock->tsc_to_system_mul);
831}
832
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100833static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
834
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200835static void kvm_write_guest_time(struct kvm_vcpu *v)
836{
837 struct timespec ts;
838 unsigned long flags;
839 struct kvm_vcpu_arch *vcpu = &v->arch;
840 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300841 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200842
843 if ((!vcpu->time_page))
844 return;
845
Avi Kivity463656c2009-04-12 15:49:07 +0300846 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
847 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
848 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
849 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200850 }
Avi Kivity463656c2009-04-12 15:49:07 +0300851 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200852
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200853 /* Keep irq disabled to prevent changes to the clock */
854 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530855 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200856 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800857 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200858 local_irq_restore(flags);
859
860 /* With all the info we got, fill in the values */
861
862 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400863 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
864
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200865 /*
866 * The interface expects us to write an even number signaling that the
867 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200868 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200869 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200870 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200871
872 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
873
874 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200875 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200876
877 kunmap_atomic(shared_kaddr, KM_USER0);
878
879 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
880}
881
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100882static int kvm_request_guest_time_update(struct kvm_vcpu *v)
883{
884 struct kvm_vcpu_arch *vcpu = &v->arch;
885
886 if (!vcpu->time_page)
887 return 0;
888 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
889 return 1;
890}
891
Avi Kivity9ba075a2008-05-26 20:06:35 +0300892static bool msr_mtrr_valid(unsigned msr)
893{
894 switch (msr) {
895 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
896 case MSR_MTRRfix64K_00000:
897 case MSR_MTRRfix16K_80000:
898 case MSR_MTRRfix16K_A0000:
899 case MSR_MTRRfix4K_C0000:
900 case MSR_MTRRfix4K_C8000:
901 case MSR_MTRRfix4K_D0000:
902 case MSR_MTRRfix4K_D8000:
903 case MSR_MTRRfix4K_E0000:
904 case MSR_MTRRfix4K_E8000:
905 case MSR_MTRRfix4K_F0000:
906 case MSR_MTRRfix4K_F8000:
907 case MSR_MTRRdefType:
908 case MSR_IA32_CR_PAT:
909 return true;
910 case 0x2f8:
911 return true;
912 }
913 return false;
914}
915
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300916static bool valid_pat_type(unsigned t)
917{
918 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
919}
920
921static bool valid_mtrr_type(unsigned t)
922{
923 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
924}
925
926static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
927{
928 int i;
929
930 if (!msr_mtrr_valid(msr))
931 return false;
932
933 if (msr == MSR_IA32_CR_PAT) {
934 for (i = 0; i < 8; i++)
935 if (!valid_pat_type((data >> (i * 8)) & 0xff))
936 return false;
937 return true;
938 } else if (msr == MSR_MTRRdefType) {
939 if (data & ~0xcff)
940 return false;
941 return valid_mtrr_type(data & 0xff);
942 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
943 for (i = 0; i < 8 ; i++)
944 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
945 return false;
946 return true;
947 }
948
949 /* variable MTRRs */
950 return valid_mtrr_type(data & 0xff);
951}
952
Avi Kivity9ba075a2008-05-26 20:06:35 +0300953static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
954{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800955 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
956
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300957 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300958 return 1;
959
Sheng Yang0bed3b52008-10-09 16:01:54 +0800960 if (msr == MSR_MTRRdefType) {
961 vcpu->arch.mtrr_state.def_type = data;
962 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
963 } else if (msr == MSR_MTRRfix64K_00000)
964 p[0] = data;
965 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
966 p[1 + msr - MSR_MTRRfix16K_80000] = data;
967 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
968 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
969 else if (msr == MSR_IA32_CR_PAT)
970 vcpu->arch.pat = data;
971 else { /* Variable MTRRs */
972 int idx, is_mtrr_mask;
973 u64 *pt;
974
975 idx = (msr - 0x200) / 2;
976 is_mtrr_mask = msr - 0x200 - 2 * idx;
977 if (!is_mtrr_mask)
978 pt =
979 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
980 else
981 pt =
982 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
983 *pt = data;
984 }
985
986 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300987 return 0;
988}
Carsten Otte15c4a642007-10-30 18:44:17 +0100989
Huang Ying890ca9a2009-05-11 16:48:15 +0800990static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
991{
992 u64 mcg_cap = vcpu->arch.mcg_cap;
993 unsigned bank_num = mcg_cap & 0xff;
994
995 switch (msr) {
996 case MSR_IA32_MCG_STATUS:
997 vcpu->arch.mcg_status = data;
998 break;
999 case MSR_IA32_MCG_CTL:
1000 if (!(mcg_cap & MCG_CTL_P))
1001 return 1;
1002 if (data != 0 && data != ~(u64)0)
1003 return -1;
1004 vcpu->arch.mcg_ctl = data;
1005 break;
1006 default:
1007 if (msr >= MSR_IA32_MC0_CTL &&
1008 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1009 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +01001010 /* only 0 or all 1s can be written to IA32_MCi_CTL
1011 * some Linux kernels though clear bit 10 in bank 4 to
1012 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
1013 * this to avoid an uncatched #GP in the guest
1014 */
Huang Ying890ca9a2009-05-11 16:48:15 +08001015 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +01001016 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +08001017 return -1;
1018 vcpu->arch.mce_banks[offset] = data;
1019 break;
1020 }
1021 return 1;
1022 }
1023 return 0;
1024}
1025
Ed Swierkffde22a2009-10-15 15:21:43 -07001026static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
1027{
1028 struct kvm *kvm = vcpu->kvm;
1029 int lm = is_long_mode(vcpu);
1030 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
1031 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
1032 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
1033 : kvm->arch.xen_hvm_config.blob_size_32;
1034 u32 page_num = data & ~PAGE_MASK;
1035 u64 page_addr = data & PAGE_MASK;
1036 u8 *page;
1037 int r;
1038
1039 r = -E2BIG;
1040 if (page_num >= blob_size)
1041 goto out;
1042 r = -ENOMEM;
1043 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1044 if (!page)
1045 goto out;
1046 r = -EFAULT;
1047 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
1048 goto out_free;
1049 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1050 goto out_free;
1051 r = 0;
1052out_free:
1053 kfree(page);
1054out:
1055 return r;
1056}
1057
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001058static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1059{
1060 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1061}
1062
1063static bool kvm_hv_msr_partition_wide(u32 msr)
1064{
1065 bool r = false;
1066 switch (msr) {
1067 case HV_X64_MSR_GUEST_OS_ID:
1068 case HV_X64_MSR_HYPERCALL:
1069 r = true;
1070 break;
1071 }
1072
1073 return r;
1074}
1075
1076static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1077{
1078 struct kvm *kvm = vcpu->kvm;
1079
1080 switch (msr) {
1081 case HV_X64_MSR_GUEST_OS_ID:
1082 kvm->arch.hv_guest_os_id = data;
1083 /* setting guest os id to zero disables hypercall page */
1084 if (!kvm->arch.hv_guest_os_id)
1085 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1086 break;
1087 case HV_X64_MSR_HYPERCALL: {
1088 u64 gfn;
1089 unsigned long addr;
1090 u8 instructions[4];
1091
1092 /* if guest os id is not set hypercall should remain disabled */
1093 if (!kvm->arch.hv_guest_os_id)
1094 break;
1095 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1096 kvm->arch.hv_hypercall = data;
1097 break;
1098 }
1099 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1100 addr = gfn_to_hva(kvm, gfn);
1101 if (kvm_is_error_hva(addr))
1102 return 1;
1103 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1104 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1105 if (copy_to_user((void __user *)addr, instructions, 4))
1106 return 1;
1107 kvm->arch.hv_hypercall = data;
1108 break;
1109 }
1110 default:
1111 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1112 "data 0x%llx\n", msr, data);
1113 return 1;
1114 }
1115 return 0;
1116}
1117
1118static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1119{
Gleb Natapov10388a02010-01-17 15:51:23 +02001120 switch (msr) {
1121 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1122 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001123
Gleb Natapov10388a02010-01-17 15:51:23 +02001124 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1125 vcpu->arch.hv_vapic = data;
1126 break;
1127 }
1128 addr = gfn_to_hva(vcpu->kvm, data >>
1129 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1130 if (kvm_is_error_hva(addr))
1131 return 1;
1132 if (clear_user((void __user *)addr, PAGE_SIZE))
1133 return 1;
1134 vcpu->arch.hv_vapic = data;
1135 break;
1136 }
1137 case HV_X64_MSR_EOI:
1138 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1139 case HV_X64_MSR_ICR:
1140 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1141 case HV_X64_MSR_TPR:
1142 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1143 default:
1144 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1145 "data 0x%llx\n", msr, data);
1146 return 1;
1147 }
1148
1149 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001150}
1151
Carsten Otte15c4a642007-10-30 18:44:17 +01001152int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1153{
1154 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001155 case MSR_EFER:
1156 set_efer(vcpu, data);
1157 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +02001158 case MSR_K7_HWCR:
1159 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01001160 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02001161 if (data != 0) {
1162 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1163 data);
1164 return 1;
1165 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001166 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001167 case MSR_FAM10H_MMIO_CONF_BASE:
1168 if (data != 0) {
1169 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1170 "0x%llx\n", data);
1171 return 1;
1172 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001173 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001174 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001175 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001176 case MSR_IA32_DEBUGCTLMSR:
1177 if (!data) {
1178 /* We support the non-activated case already */
1179 break;
1180 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1181 /* Values other than LBR and BTF are vendor-specific,
1182 thus reserved and should throw a #GP */
1183 return 1;
1184 }
1185 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1186 __func__, data);
1187 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001188 case MSR_IA32_UCODE_REV:
1189 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001190 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001191 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001192 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001193 case 0x200 ... 0x2ff:
1194 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001195 case MSR_IA32_APICBASE:
1196 kvm_set_apic_base(vcpu, data);
1197 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001198 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1199 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001200 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001201 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001202 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001203 case MSR_KVM_WALL_CLOCK:
1204 vcpu->kvm->arch.wall_clock = data;
1205 kvm_write_wall_clock(vcpu->kvm, data);
1206 break;
1207 case MSR_KVM_SYSTEM_TIME: {
1208 if (vcpu->arch.time_page) {
1209 kvm_release_page_dirty(vcpu->arch.time_page);
1210 vcpu->arch.time_page = NULL;
1211 }
1212
1213 vcpu->arch.time = data;
1214
1215 /* we verify if the enable bit is set... */
1216 if (!(data & 1))
1217 break;
1218
1219 /* ...but clean it before doing the actual write */
1220 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1221
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001222 vcpu->arch.time_page =
1223 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001224
1225 if (is_error_page(vcpu->arch.time_page)) {
1226 kvm_release_page_clean(vcpu->arch.time_page);
1227 vcpu->arch.time_page = NULL;
1228 }
1229
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001230 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001231 break;
1232 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001233 case MSR_IA32_MCG_CTL:
1234 case MSR_IA32_MCG_STATUS:
1235 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1236 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001237
1238 /* Performance counters are not protected by a CPUID bit,
1239 * so we should check all of them in the generic path for the sake of
1240 * cross vendor migration.
1241 * Writing a zero into the event select MSRs disables them,
1242 * which we perfectly emulate ;-). Any other value should be at least
1243 * reported, some guests depend on them.
1244 */
1245 case MSR_P6_EVNTSEL0:
1246 case MSR_P6_EVNTSEL1:
1247 case MSR_K7_EVNTSEL0:
1248 case MSR_K7_EVNTSEL1:
1249 case MSR_K7_EVNTSEL2:
1250 case MSR_K7_EVNTSEL3:
1251 if (data != 0)
1252 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1253 "0x%x data 0x%llx\n", msr, data);
1254 break;
1255 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1256 * so we ignore writes to make it happy.
1257 */
1258 case MSR_P6_PERFCTR0:
1259 case MSR_P6_PERFCTR1:
1260 case MSR_K7_PERFCTR0:
1261 case MSR_K7_PERFCTR1:
1262 case MSR_K7_PERFCTR2:
1263 case MSR_K7_PERFCTR3:
1264 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1265 "0x%x data 0x%llx\n", msr, data);
1266 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001267 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1268 if (kvm_hv_msr_partition_wide(msr)) {
1269 int r;
1270 mutex_lock(&vcpu->kvm->lock);
1271 r = set_msr_hyperv_pw(vcpu, msr, data);
1272 mutex_unlock(&vcpu->kvm->lock);
1273 return r;
1274 } else
1275 return set_msr_hyperv(vcpu, msr, data);
1276 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001277 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001278 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1279 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001280 if (!ignore_msrs) {
1281 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1282 msr, data);
1283 return 1;
1284 } else {
1285 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1286 msr, data);
1287 break;
1288 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001289 }
1290 return 0;
1291}
1292EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1293
1294
1295/*
1296 * Reads an msr value (of 'msr_index') into 'pdata'.
1297 * Returns 0 on success, non-0 otherwise.
1298 * Assumes vcpu_load() was already called.
1299 */
1300int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1301{
1302 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1303}
1304
Avi Kivity9ba075a2008-05-26 20:06:35 +03001305static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1306{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001307 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1308
Avi Kivity9ba075a2008-05-26 20:06:35 +03001309 if (!msr_mtrr_valid(msr))
1310 return 1;
1311
Sheng Yang0bed3b52008-10-09 16:01:54 +08001312 if (msr == MSR_MTRRdefType)
1313 *pdata = vcpu->arch.mtrr_state.def_type +
1314 (vcpu->arch.mtrr_state.enabled << 10);
1315 else if (msr == MSR_MTRRfix64K_00000)
1316 *pdata = p[0];
1317 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1318 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1319 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1320 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1321 else if (msr == MSR_IA32_CR_PAT)
1322 *pdata = vcpu->arch.pat;
1323 else { /* Variable MTRRs */
1324 int idx, is_mtrr_mask;
1325 u64 *pt;
1326
1327 idx = (msr - 0x200) / 2;
1328 is_mtrr_mask = msr - 0x200 - 2 * idx;
1329 if (!is_mtrr_mask)
1330 pt =
1331 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1332 else
1333 pt =
1334 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1335 *pdata = *pt;
1336 }
1337
Avi Kivity9ba075a2008-05-26 20:06:35 +03001338 return 0;
1339}
1340
Huang Ying890ca9a2009-05-11 16:48:15 +08001341static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1342{
1343 u64 data;
1344 u64 mcg_cap = vcpu->arch.mcg_cap;
1345 unsigned bank_num = mcg_cap & 0xff;
1346
1347 switch (msr) {
1348 case MSR_IA32_P5_MC_ADDR:
1349 case MSR_IA32_P5_MC_TYPE:
1350 data = 0;
1351 break;
1352 case MSR_IA32_MCG_CAP:
1353 data = vcpu->arch.mcg_cap;
1354 break;
1355 case MSR_IA32_MCG_CTL:
1356 if (!(mcg_cap & MCG_CTL_P))
1357 return 1;
1358 data = vcpu->arch.mcg_ctl;
1359 break;
1360 case MSR_IA32_MCG_STATUS:
1361 data = vcpu->arch.mcg_status;
1362 break;
1363 default:
1364 if (msr >= MSR_IA32_MC0_CTL &&
1365 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1366 u32 offset = msr - MSR_IA32_MC0_CTL;
1367 data = vcpu->arch.mce_banks[offset];
1368 break;
1369 }
1370 return 1;
1371 }
1372 *pdata = data;
1373 return 0;
1374}
1375
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001376static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1377{
1378 u64 data = 0;
1379 struct kvm *kvm = vcpu->kvm;
1380
1381 switch (msr) {
1382 case HV_X64_MSR_GUEST_OS_ID:
1383 data = kvm->arch.hv_guest_os_id;
1384 break;
1385 case HV_X64_MSR_HYPERCALL:
1386 data = kvm->arch.hv_hypercall;
1387 break;
1388 default:
1389 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1390 return 1;
1391 }
1392
1393 *pdata = data;
1394 return 0;
1395}
1396
1397static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1398{
1399 u64 data = 0;
1400
1401 switch (msr) {
1402 case HV_X64_MSR_VP_INDEX: {
1403 int r;
1404 struct kvm_vcpu *v;
1405 kvm_for_each_vcpu(r, v, vcpu->kvm)
1406 if (v == vcpu)
1407 data = r;
1408 break;
1409 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001410 case HV_X64_MSR_EOI:
1411 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1412 case HV_X64_MSR_ICR:
1413 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1414 case HV_X64_MSR_TPR:
1415 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001416 default:
1417 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1418 return 1;
1419 }
1420 *pdata = data;
1421 return 0;
1422}
1423
Carsten Otte15c4a642007-10-30 18:44:17 +01001424int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1425{
1426 u64 data;
1427
1428 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001429 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001430 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001431 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001432 case MSR_IA32_DEBUGCTLMSR:
1433 case MSR_IA32_LASTBRANCHFROMIP:
1434 case MSR_IA32_LASTBRANCHTOIP:
1435 case MSR_IA32_LASTINTFROMIP:
1436 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301437 case MSR_K8_SYSCFG:
1438 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001439 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301440 case MSR_P6_PERFCTR0:
1441 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301442 case MSR_P6_EVNTSEL0:
1443 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301444 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301445 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001446 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001447 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001448 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001449 data = 0;
1450 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001451 case MSR_MTRRcap:
1452 data = 0x500 | KVM_NR_VAR_MTRR;
1453 break;
1454 case 0x200 ... 0x2ff:
1455 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001456 case 0xcd: /* fsb frequency */
1457 data = 3;
1458 break;
1459 case MSR_IA32_APICBASE:
1460 data = kvm_get_apic_base(vcpu);
1461 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001462 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1463 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1464 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001465 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001466 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001467 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001468 case MSR_IA32_PERF_STATUS:
1469 /* TSC increment by tick */
1470 data = 1000ULL;
1471 /* CPU multiplier */
1472 data |= (((uint64_t)4ULL) << 40);
1473 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001474 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001475 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001476 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001477 case MSR_KVM_WALL_CLOCK:
1478 data = vcpu->kvm->arch.wall_clock;
1479 break;
1480 case MSR_KVM_SYSTEM_TIME:
1481 data = vcpu->arch.time;
1482 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001483 case MSR_IA32_P5_MC_ADDR:
1484 case MSR_IA32_P5_MC_TYPE:
1485 case MSR_IA32_MCG_CAP:
1486 case MSR_IA32_MCG_CTL:
1487 case MSR_IA32_MCG_STATUS:
1488 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1489 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001490 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1491 if (kvm_hv_msr_partition_wide(msr)) {
1492 int r;
1493 mutex_lock(&vcpu->kvm->lock);
1494 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1495 mutex_unlock(&vcpu->kvm->lock);
1496 return r;
1497 } else
1498 return get_msr_hyperv(vcpu, msr, pdata);
1499 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001500 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001501 if (!ignore_msrs) {
1502 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1503 return 1;
1504 } else {
1505 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1506 data = 0;
1507 }
1508 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001509 }
1510 *pdata = data;
1511 return 0;
1512}
1513EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1514
Carsten Otte313a3dc2007-10-11 19:16:52 +02001515/*
1516 * Read or write a bunch of msrs. All parameters are kernel addresses.
1517 *
1518 * @return number of msrs set successfully.
1519 */
1520static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1521 struct kvm_msr_entry *entries,
1522 int (*do_msr)(struct kvm_vcpu *vcpu,
1523 unsigned index, u64 *data))
1524{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001525 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001526
1527 vcpu_load(vcpu);
1528
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001529 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001530 for (i = 0; i < msrs->nmsrs; ++i)
1531 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1532 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001533 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001534
1535 vcpu_put(vcpu);
1536
1537 return i;
1538}
1539
1540/*
1541 * Read or write a bunch of msrs. Parameters are user addresses.
1542 *
1543 * @return number of msrs set successfully.
1544 */
1545static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1546 int (*do_msr)(struct kvm_vcpu *vcpu,
1547 unsigned index, u64 *data),
1548 int writeback)
1549{
1550 struct kvm_msrs msrs;
1551 struct kvm_msr_entry *entries;
1552 int r, n;
1553 unsigned size;
1554
1555 r = -EFAULT;
1556 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1557 goto out;
1558
1559 r = -E2BIG;
1560 if (msrs.nmsrs >= MAX_IO_MSRS)
1561 goto out;
1562
1563 r = -ENOMEM;
1564 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1565 entries = vmalloc(size);
1566 if (!entries)
1567 goto out;
1568
1569 r = -EFAULT;
1570 if (copy_from_user(entries, user_msrs->entries, size))
1571 goto out_free;
1572
1573 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1574 if (r < 0)
1575 goto out_free;
1576
1577 r = -EFAULT;
1578 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1579 goto out_free;
1580
1581 r = n;
1582
1583out_free:
1584 vfree(entries);
1585out:
1586 return r;
1587}
1588
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001589int kvm_dev_ioctl_check_extension(long ext)
1590{
1591 int r;
1592
1593 switch (ext) {
1594 case KVM_CAP_IRQCHIP:
1595 case KVM_CAP_HLT:
1596 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001597 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001598 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001599 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001600 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001601 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001602 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001603 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001604 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001605 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001606 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001607 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001608 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001609 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001610 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001611 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001612 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001613 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001614 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001615 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001616 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001617 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001618 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01001619 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001620 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001621 r = 1;
1622 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001623 case KVM_CAP_COALESCED_MMIO:
1624 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1625 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001626 case KVM_CAP_VAPIC:
1627 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1628 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001629 case KVM_CAP_NR_VCPUS:
1630 r = KVM_MAX_VCPUS;
1631 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001632 case KVM_CAP_NR_MEMSLOTS:
1633 r = KVM_MEMORY_SLOTS;
1634 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001635 case KVM_CAP_PV_MMU: /* obsolete */
1636 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001637 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001638 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001639 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001640 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001641 case KVM_CAP_MCE:
1642 r = KVM_MAX_MCE_BANKS;
1643 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001644 default:
1645 r = 0;
1646 break;
1647 }
1648 return r;
1649
1650}
1651
Carsten Otte043405e2007-10-10 17:16:19 +02001652long kvm_arch_dev_ioctl(struct file *filp,
1653 unsigned int ioctl, unsigned long arg)
1654{
1655 void __user *argp = (void __user *)arg;
1656 long r;
1657
1658 switch (ioctl) {
1659 case KVM_GET_MSR_INDEX_LIST: {
1660 struct kvm_msr_list __user *user_msr_list = argp;
1661 struct kvm_msr_list msr_list;
1662 unsigned n;
1663
1664 r = -EFAULT;
1665 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1666 goto out;
1667 n = msr_list.nmsrs;
1668 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1669 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1670 goto out;
1671 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001672 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001673 goto out;
1674 r = -EFAULT;
1675 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1676 num_msrs_to_save * sizeof(u32)))
1677 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001678 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001679 &emulated_msrs,
1680 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1681 goto out;
1682 r = 0;
1683 break;
1684 }
Avi Kivity674eea02008-02-11 18:37:23 +02001685 case KVM_GET_SUPPORTED_CPUID: {
1686 struct kvm_cpuid2 __user *cpuid_arg = argp;
1687 struct kvm_cpuid2 cpuid;
1688
1689 r = -EFAULT;
1690 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1691 goto out;
1692 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001693 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001694 if (r)
1695 goto out;
1696
1697 r = -EFAULT;
1698 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1699 goto out;
1700 r = 0;
1701 break;
1702 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001703 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1704 u64 mce_cap;
1705
1706 mce_cap = KVM_MCE_CAP_SUPPORTED;
1707 r = -EFAULT;
1708 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1709 goto out;
1710 r = 0;
1711 break;
1712 }
Carsten Otte043405e2007-10-10 17:16:19 +02001713 default:
1714 r = -EINVAL;
1715 }
1716out:
1717 return r;
1718}
1719
Carsten Otte313a3dc2007-10-11 19:16:52 +02001720void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1721{
1722 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001723 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1724 unsigned long khz = cpufreq_quick_get(cpu);
1725 if (!khz)
1726 khz = tsc_khz;
1727 per_cpu(cpu_tsc_khz, cpu) = khz;
1728 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001729 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001730}
1731
1732void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1733{
Amit Shah9327fd12007-11-15 18:38:46 +02001734 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001735 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001736}
1737
Dan Kenigsberg07716712007-11-21 17:10:04 +02001738static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001739{
Avi Kivitye286e862009-05-03 18:50:55 +03001740 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001741
Avi Kivitye286e862009-05-03 18:50:55 +03001742 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001743 return efer & EFER_NX;
1744}
1745
1746static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1747{
1748 int i;
1749 struct kvm_cpuid_entry2 *e, *entry;
1750
Carsten Otte313a3dc2007-10-11 19:16:52 +02001751 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001752 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1753 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001754 if (e->function == 0x80000001) {
1755 entry = e;
1756 break;
1757 }
1758 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001759 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001760 entry->edx &= ~(1 << 20);
1761 printk(KERN_INFO "kvm: guest NX capability removed\n");
1762 }
1763}
1764
Dan Kenigsberg07716712007-11-21 17:10:04 +02001765/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001766static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1767 struct kvm_cpuid *cpuid,
1768 struct kvm_cpuid_entry __user *entries)
1769{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001770 int r, i;
1771 struct kvm_cpuid_entry *cpuid_entries;
1772
1773 r = -E2BIG;
1774 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1775 goto out;
1776 r = -ENOMEM;
1777 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1778 if (!cpuid_entries)
1779 goto out;
1780 r = -EFAULT;
1781 if (copy_from_user(cpuid_entries, entries,
1782 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1783 goto out_free;
1784 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001785 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1786 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1787 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1788 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1789 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1790 vcpu->arch.cpuid_entries[i].index = 0;
1791 vcpu->arch.cpuid_entries[i].flags = 0;
1792 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1793 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1794 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001795 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001796 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001797 cpuid_fix_nx_cap(vcpu);
1798 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001799 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001800 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001801
1802out_free:
1803 vfree(cpuid_entries);
1804out:
1805 return r;
1806}
1807
1808static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001809 struct kvm_cpuid2 *cpuid,
1810 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001811{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001812 int r;
1813
1814 r = -E2BIG;
1815 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1816 goto out;
1817 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001818 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001819 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001820 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001821 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001822 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001823 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001824 return 0;
1825
1826out:
1827 return r;
1828}
1829
Dan Kenigsberg07716712007-11-21 17:10:04 +02001830static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001831 struct kvm_cpuid2 *cpuid,
1832 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001833{
1834 int r;
1835
1836 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001837 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001838 goto out;
1839 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001840 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001841 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001842 goto out;
1843 return 0;
1844
1845out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001846 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001847 return r;
1848}
1849
Dan Kenigsberg07716712007-11-21 17:10:04 +02001850static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001851 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001852{
1853 entry->function = function;
1854 entry->index = index;
1855 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001856 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001857 entry->flags = 0;
1858}
1859
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001860#define F(x) bit(X86_FEATURE_##x)
1861
Dan Kenigsberg07716712007-11-21 17:10:04 +02001862static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1863 u32 index, int *nent, int maxnent)
1864{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001865 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001866#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001867 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1868 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001869 unsigned f_lm = F(LM);
1870#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001871 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001872 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001873#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001874 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001875
1876 /* cpuid 1.edx */
1877 const u32 kvm_supported_word0_x86_features =
1878 F(FPU) | F(VME) | F(DE) | F(PSE) |
1879 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1880 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1881 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1882 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1883 0 /* Reserved, DS, ACPI */ | F(MMX) |
1884 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1885 0 /* HTT, TM, Reserved, PBE */;
1886 /* cpuid 0x80000001.edx */
1887 const u32 kvm_supported_word1_x86_features =
1888 F(FPU) | F(VME) | F(DE) | F(PSE) |
1889 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1890 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1891 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1892 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1893 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001894 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001895 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1896 /* cpuid 1.ecx */
1897 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001898 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1899 0 /* DS-CPL, VMX, SMX, EST */ |
1900 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1901 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1902 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001903 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001904 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001905 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001906 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001907 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1908 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1909 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1910 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001911
Amit Shah19355472009-01-14 16:56:00 +00001912 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001913 get_cpu();
1914 do_cpuid_1_ent(entry, function, index);
1915 ++*nent;
1916
1917 switch (function) {
1918 case 0:
1919 entry->eax = min(entry->eax, (u32)0xb);
1920 break;
1921 case 1:
1922 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001923 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001924 /* we support x2apic emulation even if host does not support
1925 * it since we emulate x2apic in software */
1926 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001927 break;
1928 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1929 * may return different values. This forces us to get_cpu() before
1930 * issuing the first command, and also to emulate this annoying behavior
1931 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1932 case 2: {
1933 int t, times = entry->eax & 0xff;
1934
1935 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001936 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001937 for (t = 1; t < times && *nent < maxnent; ++t) {
1938 do_cpuid_1_ent(&entry[t], function, 0);
1939 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1940 ++*nent;
1941 }
1942 break;
1943 }
1944 /* function 4 and 0xb have additional index. */
1945 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001946 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001947
1948 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1949 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001950 for (i = 1; *nent < maxnent; ++i) {
1951 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001952 if (!cache_type)
1953 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001954 do_cpuid_1_ent(&entry[i], function, i);
1955 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001956 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1957 ++*nent;
1958 }
1959 break;
1960 }
1961 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001962 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001963
1964 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1965 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001966 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001967 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001968 if (!level_type)
1969 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001970 do_cpuid_1_ent(&entry[i], function, i);
1971 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001972 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1973 ++*nent;
1974 }
1975 break;
1976 }
1977 case 0x80000000:
1978 entry->eax = min(entry->eax, 0x8000001a);
1979 break;
1980 case 0x80000001:
1981 entry->edx &= kvm_supported_word1_x86_features;
1982 entry->ecx &= kvm_supported_word6_x86_features;
1983 break;
1984 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02001985
1986 kvm_x86_ops->set_supported_cpuid(function, entry);
1987
Dan Kenigsberg07716712007-11-21 17:10:04 +02001988 put_cpu();
1989}
1990
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001991#undef F
1992
Avi Kivity674eea02008-02-11 18:37:23 +02001993static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001994 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001995{
1996 struct kvm_cpuid_entry2 *cpuid_entries;
1997 int limit, nent = 0, r = -E2BIG;
1998 u32 func;
1999
2000 if (cpuid->nent < 1)
2001 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02002002 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2003 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002004 r = -ENOMEM;
2005 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
2006 if (!cpuid_entries)
2007 goto out;
2008
2009 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
2010 limit = cpuid_entries[0].eax;
2011 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
2012 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00002013 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002014 r = -E2BIG;
2015 if (nent >= cpuid->nent)
2016 goto out_free;
2017
2018 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
2019 limit = cpuid_entries[nent - 1].eax;
2020 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
2021 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00002022 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01002023 r = -E2BIG;
2024 if (nent >= cpuid->nent)
2025 goto out_free;
2026
Dan Kenigsberg07716712007-11-21 17:10:04 +02002027 r = -EFAULT;
2028 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00002029 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02002030 goto out_free;
2031 cpuid->nent = nent;
2032 r = 0;
2033
2034out_free:
2035 vfree(cpuid_entries);
2036out:
2037 return r;
2038}
2039
Carsten Otte313a3dc2007-10-11 19:16:52 +02002040static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
2041 struct kvm_lapic_state *s)
2042{
2043 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002044 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002045 vcpu_put(vcpu);
2046
2047 return 0;
2048}
2049
2050static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2051 struct kvm_lapic_state *s)
2052{
2053 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002054 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002055 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002056 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002057 vcpu_put(vcpu);
2058
2059 return 0;
2060}
2061
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002062static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2063 struct kvm_interrupt *irq)
2064{
2065 if (irq->irq < 0 || irq->irq >= 256)
2066 return -EINVAL;
2067 if (irqchip_in_kernel(vcpu->kvm))
2068 return -ENXIO;
2069 vcpu_load(vcpu);
2070
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002071 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002072
2073 vcpu_put(vcpu);
2074
2075 return 0;
2076}
2077
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002078static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2079{
2080 vcpu_load(vcpu);
2081 kvm_inject_nmi(vcpu);
2082 vcpu_put(vcpu);
2083
2084 return 0;
2085}
2086
Avi Kivityb209749f2007-10-22 16:50:39 +02002087static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2088 struct kvm_tpr_access_ctl *tac)
2089{
2090 if (tac->flags)
2091 return -EINVAL;
2092 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2093 return 0;
2094}
2095
Huang Ying890ca9a2009-05-11 16:48:15 +08002096static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2097 u64 mcg_cap)
2098{
2099 int r;
2100 unsigned bank_num = mcg_cap & 0xff, bank;
2101
2102 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002103 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002104 goto out;
2105 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2106 goto out;
2107 r = 0;
2108 vcpu->arch.mcg_cap = mcg_cap;
2109 /* Init IA32_MCG_CTL to all 1s */
2110 if (mcg_cap & MCG_CTL_P)
2111 vcpu->arch.mcg_ctl = ~(u64)0;
2112 /* Init IA32_MCi_CTL to all 1s */
2113 for (bank = 0; bank < bank_num; bank++)
2114 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2115out:
2116 return r;
2117}
2118
2119static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2120 struct kvm_x86_mce *mce)
2121{
2122 u64 mcg_cap = vcpu->arch.mcg_cap;
2123 unsigned bank_num = mcg_cap & 0xff;
2124 u64 *banks = vcpu->arch.mce_banks;
2125
2126 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2127 return -EINVAL;
2128 /*
2129 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2130 * reporting is disabled
2131 */
2132 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2133 vcpu->arch.mcg_ctl != ~(u64)0)
2134 return 0;
2135 banks += 4 * mce->bank;
2136 /*
2137 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2138 * reporting is disabled for the bank
2139 */
2140 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2141 return 0;
2142 if (mce->status & MCI_STATUS_UC) {
2143 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002144 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002145 printk(KERN_DEBUG "kvm: set_mce: "
2146 "injects mce exception while "
2147 "previous one is in progress!\n");
2148 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2149 return 0;
2150 }
2151 if (banks[1] & MCI_STATUS_VAL)
2152 mce->status |= MCI_STATUS_OVER;
2153 banks[2] = mce->addr;
2154 banks[3] = mce->misc;
2155 vcpu->arch.mcg_status = mce->mcg_status;
2156 banks[1] = mce->status;
2157 kvm_queue_exception(vcpu, MC_VECTOR);
2158 } else if (!(banks[1] & MCI_STATUS_VAL)
2159 || !(banks[1] & MCI_STATUS_UC)) {
2160 if (banks[1] & MCI_STATUS_VAL)
2161 mce->status |= MCI_STATUS_OVER;
2162 banks[2] = mce->addr;
2163 banks[3] = mce->misc;
2164 banks[1] = mce->status;
2165 } else
2166 banks[1] |= MCI_STATUS_OVER;
2167 return 0;
2168}
2169
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002170static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2171 struct kvm_vcpu_events *events)
2172{
2173 vcpu_load(vcpu);
2174
Jan Kiszka03b82a32010-02-15 10:45:41 +01002175 events->exception.injected =
2176 vcpu->arch.exception.pending &&
2177 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002178 events->exception.nr = vcpu->arch.exception.nr;
2179 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2180 events->exception.error_code = vcpu->arch.exception.error_code;
2181
Jan Kiszka03b82a32010-02-15 10:45:41 +01002182 events->interrupt.injected =
2183 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002184 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002185 events->interrupt.soft = 0;
Jan Kiszka48005f62010-02-19 19:38:07 +01002186 events->interrupt.shadow =
2187 kvm_x86_ops->get_interrupt_shadow(vcpu,
2188 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002189
2190 events->nmi.injected = vcpu->arch.nmi_injected;
2191 events->nmi.pending = vcpu->arch.nmi_pending;
2192 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2193
2194 events->sipi_vector = vcpu->arch.sipi_vector;
2195
Jan Kiszkadab4b912009-12-06 18:24:15 +01002196 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002197 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2198 | KVM_VCPUEVENT_VALID_SHADOW);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002199
2200 vcpu_put(vcpu);
2201}
2202
2203static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2204 struct kvm_vcpu_events *events)
2205{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002206 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002207 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2208 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002209 return -EINVAL;
2210
2211 vcpu_load(vcpu);
2212
2213 vcpu->arch.exception.pending = events->exception.injected;
2214 vcpu->arch.exception.nr = events->exception.nr;
2215 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2216 vcpu->arch.exception.error_code = events->exception.error_code;
2217
2218 vcpu->arch.interrupt.pending = events->interrupt.injected;
2219 vcpu->arch.interrupt.nr = events->interrupt.nr;
2220 vcpu->arch.interrupt.soft = events->interrupt.soft;
2221 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2222 kvm_pic_clear_isr_ack(vcpu->kvm);
Jan Kiszka48005f62010-02-19 19:38:07 +01002223 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2224 kvm_x86_ops->set_interrupt_shadow(vcpu,
2225 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002226
2227 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002228 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2229 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002230 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2231
Jan Kiszkadab4b912009-12-06 18:24:15 +01002232 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2233 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002234
2235 vcpu_put(vcpu);
2236
2237 return 0;
2238}
2239
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002240static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2241 struct kvm_debugregs *dbgregs)
2242{
2243 vcpu_load(vcpu);
2244
2245 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2246 dbgregs->dr6 = vcpu->arch.dr6;
2247 dbgregs->dr7 = vcpu->arch.dr7;
2248 dbgregs->flags = 0;
2249
2250 vcpu_put(vcpu);
2251}
2252
2253static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2254 struct kvm_debugregs *dbgregs)
2255{
2256 if (dbgregs->flags)
2257 return -EINVAL;
2258
2259 vcpu_load(vcpu);
2260
2261 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2262 vcpu->arch.dr6 = dbgregs->dr6;
2263 vcpu->arch.dr7 = dbgregs->dr7;
2264
2265 vcpu_put(vcpu);
2266
2267 return 0;
2268}
2269
Carsten Otte313a3dc2007-10-11 19:16:52 +02002270long kvm_arch_vcpu_ioctl(struct file *filp,
2271 unsigned int ioctl, unsigned long arg)
2272{
2273 struct kvm_vcpu *vcpu = filp->private_data;
2274 void __user *argp = (void __user *)arg;
2275 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002276 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002277
2278 switch (ioctl) {
2279 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002280 r = -EINVAL;
2281 if (!vcpu->arch.apic)
2282 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002283 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002284
Dave Hansenb772ff32008-08-11 10:01:47 -07002285 r = -ENOMEM;
2286 if (!lapic)
2287 goto out;
2288 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002289 if (r)
2290 goto out;
2291 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002292 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002293 goto out;
2294 r = 0;
2295 break;
2296 }
2297 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002298 r = -EINVAL;
2299 if (!vcpu->arch.apic)
2300 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002301 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2302 r = -ENOMEM;
2303 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002304 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002305 r = -EFAULT;
2306 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2307 goto out;
2308 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002309 if (r)
2310 goto out;
2311 r = 0;
2312 break;
2313 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002314 case KVM_INTERRUPT: {
2315 struct kvm_interrupt irq;
2316
2317 r = -EFAULT;
2318 if (copy_from_user(&irq, argp, sizeof irq))
2319 goto out;
2320 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2321 if (r)
2322 goto out;
2323 r = 0;
2324 break;
2325 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002326 case KVM_NMI: {
2327 r = kvm_vcpu_ioctl_nmi(vcpu);
2328 if (r)
2329 goto out;
2330 r = 0;
2331 break;
2332 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002333 case KVM_SET_CPUID: {
2334 struct kvm_cpuid __user *cpuid_arg = argp;
2335 struct kvm_cpuid cpuid;
2336
2337 r = -EFAULT;
2338 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2339 goto out;
2340 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2341 if (r)
2342 goto out;
2343 break;
2344 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002345 case KVM_SET_CPUID2: {
2346 struct kvm_cpuid2 __user *cpuid_arg = argp;
2347 struct kvm_cpuid2 cpuid;
2348
2349 r = -EFAULT;
2350 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2351 goto out;
2352 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002353 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002354 if (r)
2355 goto out;
2356 break;
2357 }
2358 case KVM_GET_CPUID2: {
2359 struct kvm_cpuid2 __user *cpuid_arg = argp;
2360 struct kvm_cpuid2 cpuid;
2361
2362 r = -EFAULT;
2363 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2364 goto out;
2365 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002366 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002367 if (r)
2368 goto out;
2369 r = -EFAULT;
2370 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2371 goto out;
2372 r = 0;
2373 break;
2374 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002375 case KVM_GET_MSRS:
2376 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2377 break;
2378 case KVM_SET_MSRS:
2379 r = msr_io(vcpu, argp, do_set_msr, 0);
2380 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002381 case KVM_TPR_ACCESS_REPORTING: {
2382 struct kvm_tpr_access_ctl tac;
2383
2384 r = -EFAULT;
2385 if (copy_from_user(&tac, argp, sizeof tac))
2386 goto out;
2387 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2388 if (r)
2389 goto out;
2390 r = -EFAULT;
2391 if (copy_to_user(argp, &tac, sizeof tac))
2392 goto out;
2393 r = 0;
2394 break;
2395 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002396 case KVM_SET_VAPIC_ADDR: {
2397 struct kvm_vapic_addr va;
2398
2399 r = -EINVAL;
2400 if (!irqchip_in_kernel(vcpu->kvm))
2401 goto out;
2402 r = -EFAULT;
2403 if (copy_from_user(&va, argp, sizeof va))
2404 goto out;
2405 r = 0;
2406 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2407 break;
2408 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002409 case KVM_X86_SETUP_MCE: {
2410 u64 mcg_cap;
2411
2412 r = -EFAULT;
2413 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2414 goto out;
2415 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2416 break;
2417 }
2418 case KVM_X86_SET_MCE: {
2419 struct kvm_x86_mce mce;
2420
2421 r = -EFAULT;
2422 if (copy_from_user(&mce, argp, sizeof mce))
2423 goto out;
2424 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2425 break;
2426 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002427 case KVM_GET_VCPU_EVENTS: {
2428 struct kvm_vcpu_events events;
2429
2430 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2431
2432 r = -EFAULT;
2433 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2434 break;
2435 r = 0;
2436 break;
2437 }
2438 case KVM_SET_VCPU_EVENTS: {
2439 struct kvm_vcpu_events events;
2440
2441 r = -EFAULT;
2442 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2443 break;
2444
2445 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2446 break;
2447 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002448 case KVM_GET_DEBUGREGS: {
2449 struct kvm_debugregs dbgregs;
2450
2451 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2452
2453 r = -EFAULT;
2454 if (copy_to_user(argp, &dbgregs,
2455 sizeof(struct kvm_debugregs)))
2456 break;
2457 r = 0;
2458 break;
2459 }
2460 case KVM_SET_DEBUGREGS: {
2461 struct kvm_debugregs dbgregs;
2462
2463 r = -EFAULT;
2464 if (copy_from_user(&dbgregs, argp,
2465 sizeof(struct kvm_debugregs)))
2466 break;
2467
2468 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2469 break;
2470 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002471 default:
2472 r = -EINVAL;
2473 }
2474out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002475 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002476 return r;
2477}
2478
Carsten Otte1fe779f2007-10-29 16:08:35 +01002479static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2480{
2481 int ret;
2482
2483 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2484 return -1;
2485 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2486 return ret;
2487}
2488
Sheng Yangb927a3c2009-07-21 10:42:48 +08002489static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2490 u64 ident_addr)
2491{
2492 kvm->arch.ept_identity_map_addr = ident_addr;
2493 return 0;
2494}
2495
Carsten Otte1fe779f2007-10-29 16:08:35 +01002496static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2497 u32 kvm_nr_mmu_pages)
2498{
2499 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2500 return -EINVAL;
2501
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002502 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002503 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002504
2505 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002506 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002507
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002508 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002509 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002510 return 0;
2511}
2512
2513static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2514{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002515 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002516}
2517
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002518gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2519{
2520 int i;
2521 struct kvm_mem_alias *alias;
2522 struct kvm_mem_aliases *aliases;
2523
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002524 aliases = kvm_aliases(kvm);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002525
2526 for (i = 0; i < aliases->naliases; ++i) {
2527 alias = &aliases->aliases[i];
2528 if (alias->flags & KVM_ALIAS_INVALID)
2529 continue;
2530 if (gfn >= alias->base_gfn
2531 && gfn < alias->base_gfn + alias->npages)
2532 return alias->target_gfn + gfn - alias->base_gfn;
2533 }
2534 return gfn;
2535}
2536
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002537gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2538{
2539 int i;
2540 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002541 struct kvm_mem_aliases *aliases;
2542
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002543 aliases = kvm_aliases(kvm);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002544
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002545 for (i = 0; i < aliases->naliases; ++i) {
2546 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002547 if (gfn >= alias->base_gfn
2548 && gfn < alias->base_gfn + alias->npages)
2549 return alias->target_gfn + gfn - alias->base_gfn;
2550 }
2551 return gfn;
2552}
2553
Carsten Otte1fe779f2007-10-29 16:08:35 +01002554/*
2555 * Set a new alias region. Aliases map a portion of physical memory into
2556 * another portion. This is useful for memory windows, for example the PC
2557 * VGA region.
2558 */
2559static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2560 struct kvm_memory_alias *alias)
2561{
2562 int r, n;
2563 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002564 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002565
2566 r = -EINVAL;
2567 /* General sanity checks */
2568 if (alias->memory_size & (PAGE_SIZE - 1))
2569 goto out;
2570 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2571 goto out;
2572 if (alias->slot >= KVM_ALIAS_SLOTS)
2573 goto out;
2574 if (alias->guest_phys_addr + alias->memory_size
2575 < alias->guest_phys_addr)
2576 goto out;
2577 if (alias->target_phys_addr + alias->memory_size
2578 < alias->target_phys_addr)
2579 goto out;
2580
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002581 r = -ENOMEM;
2582 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2583 if (!aliases)
2584 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002585
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002586 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002587
2588 /* invalidate any gfn reference in case of deletion/shrinking */
2589 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2590 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2591 old_aliases = kvm->arch.aliases;
2592 rcu_assign_pointer(kvm->arch.aliases, aliases);
2593 synchronize_srcu_expedited(&kvm->srcu);
2594 kvm_mmu_zap_all(kvm);
2595 kfree(old_aliases);
2596
2597 r = -ENOMEM;
2598 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2599 if (!aliases)
2600 goto out_unlock;
2601
2602 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002603
2604 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002605 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2606 p->npages = alias->memory_size >> PAGE_SHIFT;
2607 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002608 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002609
2610 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002611 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002612 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002613 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002614
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002615 old_aliases = kvm->arch.aliases;
2616 rcu_assign_pointer(kvm->arch.aliases, aliases);
2617 synchronize_srcu_expedited(&kvm->srcu);
2618 kfree(old_aliases);
2619 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002620
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002621out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002622 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002623out:
2624 return r;
2625}
2626
2627static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2628{
2629 int r;
2630
2631 r = 0;
2632 switch (chip->chip_id) {
2633 case KVM_IRQCHIP_PIC_MASTER:
2634 memcpy(&chip->chip.pic,
2635 &pic_irqchip(kvm)->pics[0],
2636 sizeof(struct kvm_pic_state));
2637 break;
2638 case KVM_IRQCHIP_PIC_SLAVE:
2639 memcpy(&chip->chip.pic,
2640 &pic_irqchip(kvm)->pics[1],
2641 sizeof(struct kvm_pic_state));
2642 break;
2643 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002644 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002645 break;
2646 default:
2647 r = -EINVAL;
2648 break;
2649 }
2650 return r;
2651}
2652
2653static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2654{
2655 int r;
2656
2657 r = 0;
2658 switch (chip->chip_id) {
2659 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002660 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002661 memcpy(&pic_irqchip(kvm)->pics[0],
2662 &chip->chip.pic,
2663 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002664 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002665 break;
2666 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002667 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002668 memcpy(&pic_irqchip(kvm)->pics[1],
2669 &chip->chip.pic,
2670 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002671 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002672 break;
2673 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002674 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002675 break;
2676 default:
2677 r = -EINVAL;
2678 break;
2679 }
2680 kvm_pic_update_irq(pic_irqchip(kvm));
2681 return r;
2682}
2683
Sheng Yange0f63cb2008-03-04 00:50:59 +08002684static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2685{
2686 int r = 0;
2687
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002688 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002689 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002690 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002691 return r;
2692}
2693
2694static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2695{
2696 int r = 0;
2697
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002698 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002699 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002700 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2701 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2702 return r;
2703}
2704
2705static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2706{
2707 int r = 0;
2708
2709 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2710 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2711 sizeof(ps->channels));
2712 ps->flags = kvm->arch.vpit->pit_state.flags;
2713 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2714 return r;
2715}
2716
2717static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2718{
2719 int r = 0, start = 0;
2720 u32 prev_legacy, cur_legacy;
2721 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2722 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2723 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2724 if (!prev_legacy && cur_legacy)
2725 start = 1;
2726 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2727 sizeof(kvm->arch.vpit->pit_state.channels));
2728 kvm->arch.vpit->pit_state.flags = ps->flags;
2729 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002730 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002731 return r;
2732}
2733
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002734static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2735 struct kvm_reinject_control *control)
2736{
2737 if (!kvm->arch.vpit)
2738 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002739 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002740 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002741 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002742 return 0;
2743}
2744
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002745/*
2746 * Get (and clear) the dirty memory log for a memory slot.
2747 */
2748int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2749 struct kvm_dirty_log *log)
2750{
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002751 int r, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002752 struct kvm_memory_slot *memslot;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002753 unsigned long n;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002754 unsigned long is_dirty = 0;
2755 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002756
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002757 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002758
Marcelo Tosattib050b012009-12-23 14:35:22 -02002759 r = -EINVAL;
2760 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002761 goto out;
2762
Marcelo Tosattib050b012009-12-23 14:35:22 -02002763 memslot = &kvm->memslots->memslots[log->slot];
2764 r = -ENOENT;
2765 if (!memslot->dirty_bitmap)
2766 goto out;
2767
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002768 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002769
2770 r = -ENOMEM;
2771 dirty_bitmap = vmalloc(n);
2772 if (!dirty_bitmap)
2773 goto out;
2774 memset(dirty_bitmap, 0, n);
2775
2776 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2777 is_dirty = memslot->dirty_bitmap[i];
2778
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002779 /* If nothing is dirty, don't bother messing with page tables. */
2780 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002781 struct kvm_memslots *slots, *old_slots;
2782
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002783 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002784 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002785 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002786
2787 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2788 if (!slots)
2789 goto out_free;
2790
2791 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2792 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2793
2794 old_slots = kvm->memslots;
2795 rcu_assign_pointer(kvm->memslots, slots);
2796 synchronize_srcu_expedited(&kvm->srcu);
2797 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2798 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002799 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002800
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002801 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002802 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2803 r = -EFAULT;
2804out_free:
2805 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002806out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002807 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002808 return r;
2809}
2810
Carsten Otte1fe779f2007-10-29 16:08:35 +01002811long kvm_arch_vm_ioctl(struct file *filp,
2812 unsigned int ioctl, unsigned long arg)
2813{
2814 struct kvm *kvm = filp->private_data;
2815 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002816 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002817 /*
2818 * This union makes it completely explicit to gcc-3.x
2819 * that these two variables' stack usage should be
2820 * combined, not added together.
2821 */
2822 union {
2823 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002824 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002825 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002826 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002827 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002828
2829 switch (ioctl) {
2830 case KVM_SET_TSS_ADDR:
2831 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2832 if (r < 0)
2833 goto out;
2834 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002835 case KVM_SET_IDENTITY_MAP_ADDR: {
2836 u64 ident_addr;
2837
2838 r = -EFAULT;
2839 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2840 goto out;
2841 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2842 if (r < 0)
2843 goto out;
2844 break;
2845 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002846 case KVM_SET_MEMORY_REGION: {
2847 struct kvm_memory_region kvm_mem;
2848 struct kvm_userspace_memory_region kvm_userspace_mem;
2849
2850 r = -EFAULT;
2851 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2852 goto out;
2853 kvm_userspace_mem.slot = kvm_mem.slot;
2854 kvm_userspace_mem.flags = kvm_mem.flags;
2855 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2856 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2857 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2858 if (r)
2859 goto out;
2860 break;
2861 }
2862 case KVM_SET_NR_MMU_PAGES:
2863 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2864 if (r)
2865 goto out;
2866 break;
2867 case KVM_GET_NR_MMU_PAGES:
2868 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2869 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002870 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002871 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002872 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002873 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002874 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002875 if (r)
2876 goto out;
2877 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002878 case KVM_CREATE_IRQCHIP: {
2879 struct kvm_pic *vpic;
2880
2881 mutex_lock(&kvm->lock);
2882 r = -EEXIST;
2883 if (kvm->arch.vpic)
2884 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002885 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002886 vpic = kvm_create_pic(kvm);
2887 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002888 r = kvm_ioapic_init(kvm);
2889 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002890 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2891 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002892 kfree(vpic);
2893 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002894 }
2895 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002896 goto create_irqchip_unlock;
2897 smp_wmb();
2898 kvm->arch.vpic = vpic;
2899 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002900 r = kvm_setup_default_irq_routing(kvm);
2901 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002902 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002903 kvm_ioapic_destroy(kvm);
2904 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002905 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002906 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002907 create_irqchip_unlock:
2908 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002909 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002910 }
Sheng Yang78376992008-01-28 05:10:22 +08002911 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002912 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2913 goto create_pit;
2914 case KVM_CREATE_PIT2:
2915 r = -EFAULT;
2916 if (copy_from_user(&u.pit_config, argp,
2917 sizeof(struct kvm_pit_config)))
2918 goto out;
2919 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002920 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002921 r = -EEXIST;
2922 if (kvm->arch.vpit)
2923 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002924 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002925 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002926 if (kvm->arch.vpit)
2927 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002928 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002929 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002930 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002931 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002932 case KVM_IRQ_LINE: {
2933 struct kvm_irq_level irq_event;
2934
2935 r = -EFAULT;
2936 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2937 goto out;
Wei Yongjun160d2f62010-03-12 10:09:45 +08002938 r = -ENXIO;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002939 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002940 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002941 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2942 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002943 if (ioctl == KVM_IRQ_LINE_STATUS) {
Wei Yongjun160d2f62010-03-12 10:09:45 +08002944 r = -EFAULT;
Gleb Natapov49256632009-02-04 17:28:14 +02002945 irq_event.status = status;
2946 if (copy_to_user(argp, &irq_event,
2947 sizeof irq_event))
2948 goto out;
2949 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002950 r = 0;
2951 }
2952 break;
2953 }
2954 case KVM_GET_IRQCHIP: {
2955 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002956 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002957
Dave Hansenf0d66272008-08-11 10:01:45 -07002958 r = -ENOMEM;
2959 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002960 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002961 r = -EFAULT;
2962 if (copy_from_user(chip, argp, sizeof *chip))
2963 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002964 r = -ENXIO;
2965 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002966 goto get_irqchip_out;
2967 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2968 if (r)
2969 goto get_irqchip_out;
2970 r = -EFAULT;
2971 if (copy_to_user(argp, chip, sizeof *chip))
2972 goto get_irqchip_out;
2973 r = 0;
2974 get_irqchip_out:
2975 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002976 if (r)
2977 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002978 break;
2979 }
2980 case KVM_SET_IRQCHIP: {
2981 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002982 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002983
Dave Hansenf0d66272008-08-11 10:01:45 -07002984 r = -ENOMEM;
2985 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002986 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002987 r = -EFAULT;
2988 if (copy_from_user(chip, argp, sizeof *chip))
2989 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002990 r = -ENXIO;
2991 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002992 goto set_irqchip_out;
2993 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2994 if (r)
2995 goto set_irqchip_out;
2996 r = 0;
2997 set_irqchip_out:
2998 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002999 if (r)
3000 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003001 break;
3002 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08003003 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003004 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003005 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003006 goto out;
3007 r = -ENXIO;
3008 if (!kvm->arch.vpit)
3009 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003010 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003011 if (r)
3012 goto out;
3013 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003014 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003015 goto out;
3016 r = 0;
3017 break;
3018 }
3019 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08003020 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07003021 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08003022 goto out;
3023 r = -ENXIO;
3024 if (!kvm->arch.vpit)
3025 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07003026 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08003027 if (r)
3028 goto out;
3029 r = 0;
3030 break;
3031 }
Beth Kone9f42752009-07-07 11:50:38 -04003032 case KVM_GET_PIT2: {
3033 r = -ENXIO;
3034 if (!kvm->arch.vpit)
3035 goto out;
3036 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
3037 if (r)
3038 goto out;
3039 r = -EFAULT;
3040 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
3041 goto out;
3042 r = 0;
3043 break;
3044 }
3045 case KVM_SET_PIT2: {
3046 r = -EFAULT;
3047 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
3048 goto out;
3049 r = -ENXIO;
3050 if (!kvm->arch.vpit)
3051 goto out;
3052 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
3053 if (r)
3054 goto out;
3055 r = 0;
3056 break;
3057 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02003058 case KVM_REINJECT_CONTROL: {
3059 struct kvm_reinject_control control;
3060 r = -EFAULT;
3061 if (copy_from_user(&control, argp, sizeof(control)))
3062 goto out;
3063 r = kvm_vm_ioctl_reinject(kvm, &control);
3064 if (r)
3065 goto out;
3066 r = 0;
3067 break;
3068 }
Ed Swierkffde22a2009-10-15 15:21:43 -07003069 case KVM_XEN_HVM_CONFIG: {
3070 r = -EFAULT;
3071 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3072 sizeof(struct kvm_xen_hvm_config)))
3073 goto out;
3074 r = -EINVAL;
3075 if (kvm->arch.xen_hvm_config.flags)
3076 goto out;
3077 r = 0;
3078 break;
3079 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003080 case KVM_SET_CLOCK: {
3081 struct timespec now;
3082 struct kvm_clock_data user_ns;
3083 u64 now_ns;
3084 s64 delta;
3085
3086 r = -EFAULT;
3087 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3088 goto out;
3089
3090 r = -EINVAL;
3091 if (user_ns.flags)
3092 goto out;
3093
3094 r = 0;
3095 ktime_get_ts(&now);
3096 now_ns = timespec_to_ns(&now);
3097 delta = user_ns.clock - now_ns;
3098 kvm->arch.kvmclock_offset = delta;
3099 break;
3100 }
3101 case KVM_GET_CLOCK: {
3102 struct timespec now;
3103 struct kvm_clock_data user_ns;
3104 u64 now_ns;
3105
3106 ktime_get_ts(&now);
3107 now_ns = timespec_to_ns(&now);
3108 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3109 user_ns.flags = 0;
3110
3111 r = -EFAULT;
3112 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3113 goto out;
3114 r = 0;
3115 break;
3116 }
3117
Carsten Otte1fe779f2007-10-29 16:08:35 +01003118 default:
3119 ;
3120 }
3121out:
3122 return r;
3123}
3124
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003125static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003126{
3127 u32 dummy[2];
3128 unsigned i, j;
3129
Glauber Costae3267cb2009-10-06 13:24:50 -04003130 /* skip the first msrs in the list. KVM-specific */
3131 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003132 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3133 continue;
3134 if (j < i)
3135 msrs_to_save[j] = msrs_to_save[i];
3136 j++;
3137 }
3138 num_msrs_to_save = j;
3139}
3140
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003141static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3142 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003143{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003144 if (vcpu->arch.apic &&
3145 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3146 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003147
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003148 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003149}
3150
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003151static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003152{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003153 if (vcpu->arch.apic &&
3154 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3155 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003156
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003157 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003158}
3159
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003160static void kvm_set_segment(struct kvm_vcpu *vcpu,
3161 struct kvm_segment *var, int seg)
3162{
3163 kvm_x86_ops->set_segment(vcpu, var, seg);
3164}
3165
3166void kvm_get_segment(struct kvm_vcpu *vcpu,
3167 struct kvm_segment *var, int seg)
3168{
3169 kvm_x86_ops->get_segment(vcpu, var, seg);
3170}
3171
Gleb Natapov1871c602010-02-10 14:21:32 +02003172gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3173{
3174 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3175 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3176}
3177
3178 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3179{
3180 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3181 access |= PFERR_FETCH_MASK;
3182 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3183}
3184
3185gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3186{
3187 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3188 access |= PFERR_WRITE_MASK;
3189 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3190}
3191
3192/* uses this to access any guest's mapped memory without checking CPL */
3193gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3194{
3195 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3196}
3197
3198static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3199 struct kvm_vcpu *vcpu, u32 access,
3200 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003201{
3202 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003203 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003204
3205 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003206 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003207 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003208 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003209 int ret;
3210
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003211 if (gpa == UNMAPPED_GVA) {
3212 r = X86EMUL_PROPAGATE_FAULT;
3213 goto out;
3214 }
Izik Eidus77c20022008-12-29 01:42:19 +02003215 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003216 if (ret < 0) {
3217 r = X86EMUL_UNHANDLEABLE;
3218 goto out;
3219 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003220
Izik Eidus77c20022008-12-29 01:42:19 +02003221 bytes -= toread;
3222 data += toread;
3223 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003224 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003225out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003226 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003227}
Izik Eidus77c20022008-12-29 01:42:19 +02003228
Gleb Natapov1871c602010-02-10 14:21:32 +02003229/* used for instruction fetching */
3230static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3231 struct kvm_vcpu *vcpu, u32 *error)
3232{
3233 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3234 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3235 access | PFERR_FETCH_MASK, error);
3236}
3237
3238static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3239 struct kvm_vcpu *vcpu, u32 *error)
3240{
3241 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3242 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3243 error);
3244}
3245
3246static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3247 struct kvm_vcpu *vcpu, u32 *error)
3248{
3249 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3250}
3251
Gleb Natapov79729952010-03-18 15:20:24 +02003252static int kvm_write_guest_virt_system(gva_t addr, void *val,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003253 unsigned int bytes,
Gleb Natapov79729952010-03-18 15:20:24 +02003254 struct kvm_vcpu *vcpu,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003255 u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003256{
3257 void *data = val;
3258 int r = X86EMUL_CONTINUE;
3259
3260 while (bytes) {
Gleb Natapov79729952010-03-18 15:20:24 +02003261 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr,
3262 PFERR_WRITE_MASK, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003263 unsigned offset = addr & (PAGE_SIZE-1);
3264 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3265 int ret;
3266
3267 if (gpa == UNMAPPED_GVA) {
3268 r = X86EMUL_PROPAGATE_FAULT;
3269 goto out;
3270 }
3271 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3272 if (ret < 0) {
3273 r = X86EMUL_UNHANDLEABLE;
3274 goto out;
3275 }
3276
3277 bytes -= towrite;
3278 data += towrite;
3279 addr += towrite;
3280 }
3281out:
3282 return r;
3283}
3284
Carsten Ottebbd9b642007-10-30 18:44:21 +01003285static int emulator_read_emulated(unsigned long addr,
3286 void *val,
3287 unsigned int bytes,
3288 struct kvm_vcpu *vcpu)
3289{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003290 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003291 u32 error_code;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003292
3293 if (vcpu->mmio_read_completed) {
3294 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003295 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3296 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003297 vcpu->mmio_read_completed = 0;
3298 return X86EMUL_CONTINUE;
3299 }
3300
Gleb Natapov1871c602010-02-10 14:21:32 +02003301 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
3302
3303 if (gpa == UNMAPPED_GVA) {
3304 kvm_inject_page_fault(vcpu, addr, error_code);
3305 return X86EMUL_PROPAGATE_FAULT;
3306 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003307
3308 /* For APIC access vmexit */
3309 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3310 goto mmio;
3311
Gleb Natapov1871c602010-02-10 14:21:32 +02003312 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003313 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003314 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003315
3316mmio:
3317 /*
3318 * Is this MMIO handled locally?
3319 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003320 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3321 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003322 return X86EMUL_CONTINUE;
3323 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003324
3325 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003326
3327 vcpu->mmio_needed = 1;
3328 vcpu->mmio_phys_addr = gpa;
3329 vcpu->mmio_size = bytes;
3330 vcpu->mmio_is_write = 0;
3331
3332 return X86EMUL_UNHANDLEABLE;
3333}
3334
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003335int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003336 const void *val, int bytes)
3337{
3338 int ret;
3339
3340 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3341 if (ret < 0)
3342 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003343 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003344 return 1;
3345}
3346
Carsten Ottebbd9b642007-10-30 18:44:21 +01003347static int emulator_write_emulated_onepage(unsigned long addr,
3348 const void *val,
3349 unsigned int bytes,
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003350 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003351{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003352 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003353 u32 error_code;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003354
Gleb Natapov1871c602010-02-10 14:21:32 +02003355 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003356
3357 if (gpa == UNMAPPED_GVA) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003358 kvm_inject_page_fault(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003359 return X86EMUL_PROPAGATE_FAULT;
3360 }
3361
3362 /* For APIC access vmexit */
3363 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3364 goto mmio;
3365
3366 if (emulator_write_phys(vcpu, gpa, val, bytes))
3367 return X86EMUL_CONTINUE;
3368
3369mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003370 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003371 /*
3372 * Is this MMIO handled locally?
3373 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003374 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003375 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003376
3377 vcpu->mmio_needed = 1;
3378 vcpu->mmio_phys_addr = gpa;
3379 vcpu->mmio_size = bytes;
3380 vcpu->mmio_is_write = 1;
3381 memcpy(vcpu->mmio_data, val, bytes);
3382
3383 return X86EMUL_CONTINUE;
3384}
3385
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003386int emulator_write_emulated(unsigned long addr,
3387 const void *val,
3388 unsigned int bytes,
3389 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003390{
3391 /* Crossing a page boundary? */
3392 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3393 int rc, now;
3394
3395 now = -addr & ~PAGE_MASK;
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003396 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003397 if (rc != X86EMUL_CONTINUE)
3398 return rc;
3399 addr += now;
3400 val += now;
3401 bytes -= now;
3402 }
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003403 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003404}
3405EXPORT_SYMBOL_GPL(emulator_write_emulated);
3406
Avi Kivitydaea3e72010-03-15 13:59:54 +02003407#define CMPXCHG_TYPE(t, ptr, old, new) \
3408 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3409
3410#ifdef CONFIG_X86_64
3411# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3412#else
3413# define CMPXCHG64(ptr, old, new) \
Jan Kiszka9749a6c2010-03-20 10:14:13 +01003414 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
Avi Kivitydaea3e72010-03-15 13:59:54 +02003415#endif
3416
Carsten Ottebbd9b642007-10-30 18:44:21 +01003417static int emulator_cmpxchg_emulated(unsigned long addr,
3418 const void *old,
3419 const void *new,
3420 unsigned int bytes,
3421 struct kvm_vcpu *vcpu)
3422{
Avi Kivitydaea3e72010-03-15 13:59:54 +02003423 gpa_t gpa;
3424 struct page *page;
3425 char *kaddr;
3426 bool exchanged;
3427
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003428 /* guests cmpxchg8b have to be emulated atomically */
Avi Kivitydaea3e72010-03-15 13:59:54 +02003429 if (bytes > 8 || (bytes & (bytes - 1)))
3430 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003431
Avi Kivitydaea3e72010-03-15 13:59:54 +02003432 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003433
Avi Kivitydaea3e72010-03-15 13:59:54 +02003434 if (gpa == UNMAPPED_GVA ||
3435 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3436 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003437
Avi Kivitydaea3e72010-03-15 13:59:54 +02003438 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3439 goto emul_write;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003440
Avi Kivitydaea3e72010-03-15 13:59:54 +02003441 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003442
Avi Kivitydaea3e72010-03-15 13:59:54 +02003443 kaddr = kmap_atomic(page, KM_USER0);
3444 kaddr += offset_in_page(gpa);
3445 switch (bytes) {
3446 case 1:
3447 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
3448 break;
3449 case 2:
3450 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
3451 break;
3452 case 4:
3453 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
3454 break;
3455 case 8:
3456 exchanged = CMPXCHG64(kaddr, old, new);
3457 break;
3458 default:
3459 BUG();
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003460 }
Avi Kivitydaea3e72010-03-15 13:59:54 +02003461 kunmap_atomic(kaddr, KM_USER0);
3462 kvm_release_page_dirty(page);
3463
3464 if (!exchanged)
3465 return X86EMUL_CMPXCHG_FAILED;
3466
Gleb Natapov8f6abd02010-04-13 10:21:56 +03003467 kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
3468
3469 return X86EMUL_CONTINUE;
Avi Kivity4a5f48f2010-03-15 13:59:55 +02003470
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003471emul_write:
Avi Kivitydaea3e72010-03-15 13:59:54 +02003472 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003473
Carsten Ottebbd9b642007-10-30 18:44:21 +01003474 return emulator_write_emulated(addr, new, bytes, vcpu);
3475}
3476
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003477static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3478{
3479 /* TODO: String I/O for in kernel device */
3480 int r;
3481
3482 if (vcpu->arch.pio.in)
3483 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
3484 vcpu->arch.pio.size, pd);
3485 else
3486 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3487 vcpu->arch.pio.port, vcpu->arch.pio.size,
3488 pd);
3489 return r;
3490}
3491
3492
3493static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
3494 unsigned int count, struct kvm_vcpu *vcpu)
3495{
Gleb Natapov79729952010-03-18 15:20:24 +02003496 if (vcpu->arch.pio.count)
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003497 goto data_avail;
3498
3499 trace_kvm_pio(1, port, size, 1);
3500
3501 vcpu->arch.pio.port = port;
3502 vcpu->arch.pio.in = 1;
Gleb Natapov79729952010-03-18 15:20:24 +02003503 vcpu->arch.pio.count = count;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003504 vcpu->arch.pio.size = size;
3505
3506 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3507 data_avail:
3508 memcpy(val, vcpu->arch.pio_data, size * count);
Gleb Natapov79729952010-03-18 15:20:24 +02003509 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003510 return 1;
3511 }
3512
3513 vcpu->run->exit_reason = KVM_EXIT_IO;
3514 vcpu->run->io.direction = KVM_EXIT_IO_IN;
3515 vcpu->run->io.size = size;
3516 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3517 vcpu->run->io.count = count;
3518 vcpu->run->io.port = port;
3519
3520 return 0;
3521}
3522
3523static int emulator_pio_out_emulated(int size, unsigned short port,
3524 const void *val, unsigned int count,
3525 struct kvm_vcpu *vcpu)
3526{
3527 trace_kvm_pio(0, port, size, 1);
3528
3529 vcpu->arch.pio.port = port;
3530 vcpu->arch.pio.in = 0;
Gleb Natapov79729952010-03-18 15:20:24 +02003531 vcpu->arch.pio.count = count;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003532 vcpu->arch.pio.size = size;
3533
3534 memcpy(vcpu->arch.pio_data, val, size * count);
3535
3536 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Gleb Natapov79729952010-03-18 15:20:24 +02003537 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003538 return 1;
3539 }
3540
3541 vcpu->run->exit_reason = KVM_EXIT_IO;
3542 vcpu->run->io.direction = KVM_EXIT_IO_OUT;
3543 vcpu->run->io.size = size;
3544 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3545 vcpu->run->io.count = count;
3546 vcpu->run->io.port = port;
3547
3548 return 0;
3549}
3550
Carsten Ottebbd9b642007-10-30 18:44:21 +01003551static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3552{
3553 return kvm_x86_ops->get_segment_base(vcpu, seg);
3554}
3555
3556int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3557{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003558 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003559 return X86EMUL_CONTINUE;
3560}
3561
3562int emulate_clts(struct kvm_vcpu *vcpu)
3563{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003564 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003565 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003566 return X86EMUL_CONTINUE;
3567}
3568
3569int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3570{
Gleb Natapov020df072010-04-13 10:05:23 +03003571 return kvm_get_dr(ctxt->vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003572}
3573
3574int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3575{
3576 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003577
Gleb Natapov020df072010-04-13 10:05:23 +03003578 return kvm_set_dr(ctxt->vcpu, dr, value & mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003579}
3580
3581void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3582{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003583 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003584 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003585 unsigned long rip_linear;
3586
Avi Kivityf76c7102008-06-13 22:45:42 +03003587 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003588 return;
3589
Glauber Costa25be4602008-06-10 10:46:53 -03003590 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3591
Gleb Natapov1871c602010-02-10 14:21:32 +02003592 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003593
3594 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3595 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003596}
3597EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3598
Gleb Natapov52a46612010-03-18 15:20:03 +02003599static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3600{
3601 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3602}
3603
3604static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
3605{
3606 unsigned long value;
3607
3608 switch (cr) {
3609 case 0:
3610 value = kvm_read_cr0(vcpu);
3611 break;
3612 case 2:
3613 value = vcpu->arch.cr2;
3614 break;
3615 case 3:
3616 value = vcpu->arch.cr3;
3617 break;
3618 case 4:
3619 value = kvm_read_cr4(vcpu);
3620 break;
3621 case 8:
3622 value = kvm_get_cr8(vcpu);
3623 break;
3624 default:
3625 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3626 return 0;
3627 }
3628
3629 return value;
3630}
3631
3632static void emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
3633{
3634 switch (cr) {
3635 case 0:
3636 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
3637 break;
3638 case 2:
3639 vcpu->arch.cr2 = val;
3640 break;
3641 case 3:
3642 kvm_set_cr3(vcpu, val);
3643 break;
3644 case 4:
3645 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
3646 break;
3647 case 8:
3648 kvm_set_cr8(vcpu, val & 0xfUL);
3649 break;
3650 default:
3651 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3652 }
3653}
3654
Gleb Natapov9c537242010-03-18 15:20:05 +02003655static int emulator_get_cpl(struct kvm_vcpu *vcpu)
3656{
3657 return kvm_x86_ops->get_cpl(vcpu);
3658}
3659
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003660static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
3661{
3662 kvm_x86_ops->get_gdt(vcpu, dt);
3663}
3664
3665static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg,
3666 struct kvm_vcpu *vcpu)
3667{
3668 struct kvm_segment var;
3669
3670 kvm_get_segment(vcpu, &var, seg);
3671
3672 if (var.unusable)
3673 return false;
3674
3675 if (var.g)
3676 var.limit >>= 12;
3677 set_desc_limit(desc, var.limit);
3678 set_desc_base(desc, (unsigned long)var.base);
3679 desc->type = var.type;
3680 desc->s = var.s;
3681 desc->dpl = var.dpl;
3682 desc->p = var.present;
3683 desc->avl = var.avl;
3684 desc->l = var.l;
3685 desc->d = var.db;
3686 desc->g = var.g;
3687
3688 return true;
3689}
3690
3691static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg,
3692 struct kvm_vcpu *vcpu)
3693{
3694 struct kvm_segment var;
3695
3696 /* needed to preserve selector */
3697 kvm_get_segment(vcpu, &var, seg);
3698
3699 var.base = get_desc_base(desc);
3700 var.limit = get_desc_limit(desc);
3701 if (desc->g)
3702 var.limit = (var.limit << 12) | 0xfff;
3703 var.type = desc->type;
3704 var.present = desc->p;
3705 var.dpl = desc->dpl;
3706 var.db = desc->d;
3707 var.s = desc->s;
3708 var.l = desc->l;
3709 var.g = desc->g;
3710 var.avl = desc->avl;
3711 var.present = desc->p;
3712 var.unusable = !var.present;
3713 var.padding = 0;
3714
3715 kvm_set_segment(vcpu, &var, seg);
3716 return;
3717}
3718
3719static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
3720{
3721 struct kvm_segment kvm_seg;
3722
3723 kvm_get_segment(vcpu, &kvm_seg, seg);
3724 return kvm_seg.selector;
3725}
3726
3727static void emulator_set_segment_selector(u16 sel, int seg,
3728 struct kvm_vcpu *vcpu)
3729{
3730 struct kvm_segment kvm_seg;
3731
3732 kvm_get_segment(vcpu, &kvm_seg, seg);
3733 kvm_seg.selector = sel;
3734 kvm_set_segment(vcpu, &kvm_seg, seg);
3735}
3736
Gleb Natapov482ac182010-03-21 13:08:20 +02003737static void emulator_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3738{
3739 kvm_x86_ops->set_rflags(vcpu, rflags);
3740}
3741
Harvey Harrison14af3f32008-02-19 10:25:50 -08003742static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003743 .read_std = kvm_read_guest_virt_system,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003744 .write_std = kvm_write_guest_virt_system,
Gleb Natapov1871c602010-02-10 14:21:32 +02003745 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003746 .read_emulated = emulator_read_emulated,
3747 .write_emulated = emulator_write_emulated,
3748 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003749 .pio_in_emulated = emulator_pio_in_emulated,
3750 .pio_out_emulated = emulator_pio_out_emulated,
Gleb Natapov2dafc6c2010-03-18 15:20:16 +02003751 .get_cached_descriptor = emulator_get_cached_descriptor,
3752 .set_cached_descriptor = emulator_set_cached_descriptor,
3753 .get_segment_selector = emulator_get_segment_selector,
3754 .set_segment_selector = emulator_set_segment_selector,
3755 .get_gdt = emulator_get_gdt,
Gleb Natapov52a46612010-03-18 15:20:03 +02003756 .get_cr = emulator_get_cr,
3757 .set_cr = emulator_set_cr,
Gleb Natapov9c537242010-03-18 15:20:05 +02003758 .cpl = emulator_get_cpl,
Gleb Natapov482ac182010-03-21 13:08:20 +02003759 .set_rflags = emulator_set_rflags,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003760};
3761
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003762static void cache_all_regs(struct kvm_vcpu *vcpu)
3763{
3764 kvm_register_read(vcpu, VCPU_REGS_RAX);
3765 kvm_register_read(vcpu, VCPU_REGS_RSP);
3766 kvm_register_read(vcpu, VCPU_REGS_RIP);
3767 vcpu->arch.regs_dirty = ~0;
3768}
3769
Carsten Ottebbd9b642007-10-30 18:44:21 +01003770int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003771 unsigned long cr2,
3772 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003773 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003774{
Glauber Costa310b5d32009-05-12 16:21:06 -04003775 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003776 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003777 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003778
Avi Kivity26eef702008-07-03 14:59:22 +03003779 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003780 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003781 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003782 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003783 * instead of direct ->regs accesses, can save hundred cycles
3784 * on Intel for instructions that don't read/change RSP, for
3785 * for example.
3786 */
3787 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003788
3789 vcpu->mmio_is_write = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003790
Sheng Yang571008d2008-01-02 14:49:22 +08003791 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003792 int cs_db, cs_l;
3793 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3794
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003795 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka83bf0002010-02-23 17:47:59 +01003796 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Gleb Natapov063db062010-03-18 15:20:06 +02003797 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003798 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003799 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003800 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003801 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003802 ? X86EMUL_MODE_PROT64 : cs_db
3803 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3804
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003805 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Avi Kivitye46479f2010-04-11 13:05:16 +03003806 trace_kvm_emulate_insn_start(vcpu);
Sheng Yang571008d2008-01-02 14:49:22 +08003807
Andre Przywara0cb57622009-06-17 15:50:31 +02003808 /* Only allow emulation of specific instructions on #UD
3809 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003810 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003811 if (emulation_type & EMULTYPE_TRAP_UD) {
3812 if (!c->twobyte)
3813 return EMULATE_FAIL;
3814 switch (c->b) {
3815 case 0x01: /* VMMCALL */
3816 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3817 return EMULATE_FAIL;
3818 break;
3819 case 0x34: /* sysenter */
3820 case 0x35: /* sysexit */
3821 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3822 return EMULATE_FAIL;
3823 break;
3824 case 0x05: /* syscall */
3825 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3826 return EMULATE_FAIL;
3827 break;
3828 default:
3829 return EMULATE_FAIL;
3830 }
3831
3832 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3833 return EMULATE_FAIL;
3834 }
Sheng Yang571008d2008-01-02 14:49:22 +08003835
Avi Kivityf2b57562007-11-18 15:17:51 +02003836 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003837 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003838 ++vcpu->stat.insn_emulation_fail;
Avi Kivitye46479f2010-04-11 13:05:16 +03003839 trace_kvm_emulate_insn_failed(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003840 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3841 return EMULATE_DONE;
3842 return EMULATE_FAIL;
3843 }
3844 }
3845
Gleb Natapovba8afb62009-04-12 13:36:57 +03003846 if (emulation_type & EMULTYPE_SKIP) {
3847 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3848 return EMULATE_DONE;
3849 }
3850
Gleb Natapov5cd21912010-03-18 15:20:26 +02003851restart:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003852 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003853 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3854
3855 if (r == 0)
3856 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003857
Gleb Natapov79729952010-03-18 15:20:24 +02003858 if (vcpu->arch.pio.count) {
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003859 if (!vcpu->arch.pio.in)
Gleb Natapov79729952010-03-18 15:20:24 +02003860 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003861 return EMULATE_DO_MMIO;
3862 }
3863
Gleb Natapov112592d2010-02-21 15:00:47 +02003864 if (r || vcpu->mmio_is_write) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003865 run->exit_reason = KVM_EXIT_MMIO;
3866 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3867 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3868 run->mmio.len = vcpu->mmio_size;
3869 run->mmio.is_write = vcpu->mmio_is_write;
3870 }
3871
3872 if (r) {
3873 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
Gleb Natapov5cd21912010-03-18 15:20:26 +02003874 goto done;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003875 if (!vcpu->mmio_needed) {
Avi Kivitye46479f2010-04-11 13:05:16 +03003876 ++vcpu->stat.insn_emulation_fail;
3877 trace_kvm_emulate_insn_failed(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003878 kvm_report_emulation_failure(vcpu, "mmio");
3879 return EMULATE_FAIL;
3880 }
3881 return EMULATE_DO_MMIO;
3882 }
3883
Carsten Ottebbd9b642007-10-30 18:44:21 +01003884 if (vcpu->mmio_is_write) {
3885 vcpu->mmio_needed = 0;
3886 return EMULATE_DO_MMIO;
3887 }
3888
Gleb Natapov5cd21912010-03-18 15:20:26 +02003889done:
3890 if (vcpu->arch.exception.pending)
3891 vcpu->arch.emulate_ctxt.restart = false;
3892
3893 if (vcpu->arch.emulate_ctxt.restart)
3894 goto restart;
3895
Carsten Ottebbd9b642007-10-30 18:44:21 +01003896 return EMULATE_DONE;
3897}
3898EXPORT_SYMBOL_GPL(emulate_instruction);
3899
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003900int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
3901{
3902 unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3903 int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
3904 /* do not return to emulator after return from userspace */
Gleb Natapov79729952010-03-18 15:20:24 +02003905 vcpu->arch.pio.count = 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003906 return ret;
3907}
3908EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
3909
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003910static void bounce_off(void *info)
3911{
3912 /* nothing */
3913}
3914
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003915static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3916 void *data)
3917{
3918 struct cpufreq_freqs *freq = data;
3919 struct kvm *kvm;
3920 struct kvm_vcpu *vcpu;
3921 int i, send_ipi = 0;
3922
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003923 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3924 return 0;
3925 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3926 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003927 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003928
3929 spin_lock(&kvm_lock);
3930 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003931 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003932 if (vcpu->cpu != freq->cpu)
3933 continue;
3934 if (!kvm_request_guest_time_update(vcpu))
3935 continue;
3936 if (vcpu->cpu != smp_processor_id())
3937 send_ipi++;
3938 }
3939 }
3940 spin_unlock(&kvm_lock);
3941
3942 if (freq->old < freq->new && send_ipi) {
3943 /*
3944 * We upscale the frequency. Must make the guest
3945 * doesn't see old kvmclock values while running with
3946 * the new frequency, otherwise we risk the guest sees
3947 * time go backwards.
3948 *
3949 * In case we update the frequency for another cpu
3950 * (which might be in guest context) send an interrupt
3951 * to kick the cpu out of guest context. Next time
3952 * guest context is entered kvmclock will be updated,
3953 * so the guest will not see stale values.
3954 */
3955 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3956 }
3957 return 0;
3958}
3959
3960static struct notifier_block kvmclock_cpufreq_notifier_block = {
3961 .notifier_call = kvmclock_cpufreq_notifier
3962};
3963
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003964static void kvm_timer_init(void)
3965{
3966 int cpu;
3967
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003968 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003969 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3970 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003971 for_each_online_cpu(cpu) {
3972 unsigned long khz = cpufreq_get(cpu);
3973 if (!khz)
3974 khz = tsc_khz;
3975 per_cpu(cpu_tsc_khz, cpu) = khz;
3976 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003977 } else {
3978 for_each_possible_cpu(cpu)
3979 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003980 }
3981}
3982
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003983static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
3984
3985static int kvm_is_in_guest(void)
3986{
3987 return percpu_read(current_vcpu) != NULL;
3988}
3989
3990static int kvm_is_user_mode(void)
3991{
3992 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003993
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003994 if (percpu_read(current_vcpu))
3995 user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003996
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003997 return user_mode != 0;
3998}
3999
4000static unsigned long kvm_get_guest_ip(void)
4001{
4002 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004003
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004004 if (percpu_read(current_vcpu))
4005 ip = kvm_rip_read(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08004006
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004007 return ip;
4008}
4009
4010static struct perf_guest_info_callbacks kvm_guest_cbs = {
4011 .is_in_guest = kvm_is_in_guest,
4012 .is_user_mode = kvm_is_user_mode,
4013 .get_guest_ip = kvm_get_guest_ip,
4014};
4015
4016void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
4017{
4018 percpu_write(current_vcpu, vcpu);
4019}
4020EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
4021
4022void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
4023{
4024 percpu_write(current_vcpu, NULL);
4025}
4026EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
4027
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004028int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02004029{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004030 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004031 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
4032
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004033 if (kvm_x86_ops) {
4034 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004035 r = -EEXIST;
4036 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004037 }
4038
4039 if (!ops->cpu_has_kvm_support()) {
4040 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004041 r = -EOPNOTSUPP;
4042 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004043 }
4044 if (ops->disabled_by_bios()) {
4045 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004046 r = -EOPNOTSUPP;
4047 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004048 }
4049
Avi Kivity97db56c2008-01-13 13:23:56 +02004050 r = kvm_mmu_module_init();
4051 if (r)
4052 goto out;
4053
4054 kvm_init_msr_list();
4055
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004056 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004057 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08004058 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
4059 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004060 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004061
Zachary Amsdenb820cc02009-09-29 11:38:34 -10004062 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004063
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004064 perf_register_guest_info_callbacks(&kvm_guest_cbs);
4065
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004066 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004067
4068out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004069 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02004070}
Hollis Blanchard8776e512007-10-31 17:24:24 -05004071
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004072void kvm_arch_exit(void)
4073{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004074 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
4075
Jan Kiszka888d2562009-04-17 19:24:58 +02004076 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
4077 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
4078 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004079 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08004080 kvm_mmu_module_exit();
4081}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004082
Hollis Blanchard8776e512007-10-31 17:24:24 -05004083int kvm_emulate_halt(struct kvm_vcpu *vcpu)
4084{
4085 ++vcpu->stat.halt_exits;
4086 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03004087 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004088 return 1;
4089 } else {
4090 vcpu->run->exit_reason = KVM_EXIT_HLT;
4091 return 0;
4092 }
4093}
4094EXPORT_SYMBOL_GPL(kvm_emulate_halt);
4095
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004096static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
4097 unsigned long a1)
4098{
4099 if (is_long_mode(vcpu))
4100 return a0;
4101 else
4102 return a0 | ((gpa_t)a1 << 32);
4103}
4104
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004105int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
4106{
4107 u64 param, ingpa, outgpa, ret;
4108 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
4109 bool fast, longmode;
4110 int cs_db, cs_l;
4111
4112 /*
4113 * hypercall generates UD from non zero cpl and real mode
4114 * per HYPER-V spec
4115 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02004116 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004117 kvm_queue_exception(vcpu, UD_VECTOR);
4118 return 0;
4119 }
4120
4121 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
4122 longmode = is_long_mode(vcpu) && cs_l == 1;
4123
4124 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02004125 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
4126 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
4127 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
4128 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
4129 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
4130 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004131 }
4132#ifdef CONFIG_X86_64
4133 else {
4134 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
4135 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
4136 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
4137 }
4138#endif
4139
4140 code = param & 0xffff;
4141 fast = (param >> 16) & 0x1;
4142 rep_cnt = (param >> 32) & 0xfff;
4143 rep_idx = (param >> 48) & 0xfff;
4144
4145 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
4146
Gleb Natapovc25bc162010-01-17 15:51:24 +02004147 switch (code) {
4148 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
4149 kvm_vcpu_on_spin(vcpu);
4150 break;
4151 default:
4152 res = HV_STATUS_INVALID_HYPERCALL_CODE;
4153 break;
4154 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004155
4156 ret = res | (((u64)rep_done & 0xfff) << 32);
4157 if (longmode) {
4158 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
4159 } else {
4160 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
4161 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
4162 }
4163
4164 return 1;
4165}
4166
Hollis Blanchard8776e512007-10-31 17:24:24 -05004167int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
4168{
4169 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004170 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004171
Gleb Natapov55cd8e52010-01-17 15:51:22 +02004172 if (kvm_hv_hypercall_enabled(vcpu->kvm))
4173 return kvm_hv_hypercall(vcpu);
4174
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004175 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
4176 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
4177 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
4178 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
4179 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004180
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004181 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004182
Hollis Blanchard8776e512007-10-31 17:24:24 -05004183 if (!is_long_mode(vcpu)) {
4184 nr &= 0xFFFFFFFF;
4185 a0 &= 0xFFFFFFFF;
4186 a1 &= 0xFFFFFFFF;
4187 a2 &= 0xFFFFFFFF;
4188 a3 &= 0xFFFFFFFF;
4189 }
4190
Jan Kiszka07708c42009-08-03 18:43:28 +02004191 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
4192 ret = -KVM_EPERM;
4193 goto out;
4194 }
4195
Hollis Blanchard8776e512007-10-31 17:24:24 -05004196 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02004197 case KVM_HC_VAPIC_POLL_IRQ:
4198 ret = 0;
4199 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004200 case KVM_HC_MMU_OP:
4201 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
4202 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004203 default:
4204 ret = -KVM_ENOSYS;
4205 break;
4206 }
Jan Kiszka07708c42009-08-03 18:43:28 +02004207out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004208 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05304209 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05004210 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004211}
4212EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
4213
4214int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
4215{
4216 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004217 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004218
Hollis Blanchard8776e512007-10-31 17:24:24 -05004219 /*
4220 * Blow out the MMU to ensure that no other VCPU has an active mapping
4221 * to ensure that the updated hypercall appears atomically across all
4222 * VCPUs.
4223 */
4224 kvm_mmu_zap_all(vcpu->kvm);
4225
Hollis Blanchard8776e512007-10-31 17:24:24 -05004226 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004227
Gleb Natapov8f6abd02010-04-13 10:21:56 +03004228 return emulator_write_emulated(rip, instruction, 3, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004229}
4230
Hollis Blanchard8776e512007-10-31 17:24:24 -05004231void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4232{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004233 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004234
4235 kvm_x86_ops->set_gdt(vcpu, &dt);
4236}
4237
4238void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4239{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004240 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004241
4242 kvm_x86_ops->set_idt(vcpu, &dt);
4243}
4244
Dan Kenigsberg07716712007-11-21 17:10:04 +02004245static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4246{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004247 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4248 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004249
4250 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4251 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004252 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004253 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004254 if (ej->function == e->function) {
4255 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4256 return j;
4257 }
4258 }
4259 return 0; /* silence gcc, even though control never reaches here */
4260}
4261
4262/* find an entry with matching function, matching index (if needed), and that
4263 * should be read next (if it's stateful) */
4264static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4265 u32 function, u32 index)
4266{
4267 if (e->function != function)
4268 return 0;
4269 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4270 return 0;
4271 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004272 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004273 return 0;
4274 return 1;
4275}
4276
Alexander Grafd8017472008-11-25 20:17:11 +01004277struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4278 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004279{
4280 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004281 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004282
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004283 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004284 struct kvm_cpuid_entry2 *e;
4285
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004286 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004287 if (is_matching_cpuid_entry(e, function, index)) {
4288 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4289 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004290 best = e;
4291 break;
4292 }
4293 /*
4294 * Both basic or both extended?
4295 */
4296 if (((e->function ^ function) & 0x80000000) == 0)
4297 if (!best || e->function > best->function)
4298 best = e;
4299 }
Alexander Grafd8017472008-11-25 20:17:11 +01004300 return best;
4301}
Sheng Yang0e851882009-12-18 16:48:46 +08004302EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004303
Dong, Eddie82725b22009-03-30 16:21:08 +08004304int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4305{
4306 struct kvm_cpuid_entry2 *best;
4307
Avi Kivityf7a71192010-04-11 15:33:32 +03004308 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
4309 if (!best || best->eax < 0x80000008)
4310 goto not_found;
Dong, Eddie82725b22009-03-30 16:21:08 +08004311 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4312 if (best)
4313 return best->eax & 0xff;
Avi Kivityf7a71192010-04-11 15:33:32 +03004314not_found:
Dong, Eddie82725b22009-03-30 16:21:08 +08004315 return 36;
4316}
4317
Alexander Grafd8017472008-11-25 20:17:11 +01004318void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4319{
4320 u32 function, index;
4321 struct kvm_cpuid_entry2 *best;
4322
4323 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4324 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4325 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4326 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4327 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4328 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4329 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004330 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004331 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4332 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4333 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4334 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004335 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004336 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004337 trace_kvm_cpuid(function,
4338 kvm_register_read(vcpu, VCPU_REGS_RAX),
4339 kvm_register_read(vcpu, VCPU_REGS_RBX),
4340 kvm_register_read(vcpu, VCPU_REGS_RCX),
4341 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004342}
4343EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004344
4345/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004346 * Check if userspace requested an interrupt window, and that the
4347 * interrupt window is open.
4348 *
4349 * No need to exit to userspace if we already have an interrupt queued.
4350 */
Avi Kivity851ba692009-08-24 11:10:17 +03004351static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004352{
Gleb Natapov80618232009-04-21 17:44:56 +03004353 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004354 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004355 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004356}
4357
Avi Kivity851ba692009-08-24 11:10:17 +03004358static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004359{
Avi Kivity851ba692009-08-24 11:10:17 +03004360 struct kvm_run *kvm_run = vcpu->run;
4361
Jan Kiszka91586a32009-10-05 13:07:21 +02004362 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004363 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004364 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004365 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004366 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004367 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004368 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004369 kvm_arch_interrupt_allowed(vcpu) &&
4370 !kvm_cpu_has_interrupt(vcpu) &&
4371 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004372}
4373
Avi Kivityb93463a2007-10-25 16:52:32 +02004374static void vapic_enter(struct kvm_vcpu *vcpu)
4375{
4376 struct kvm_lapic *apic = vcpu->arch.apic;
4377 struct page *page;
4378
4379 if (!apic || !apic->vapic_addr)
4380 return;
4381
4382 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004383
4384 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004385}
4386
4387static void vapic_exit(struct kvm_vcpu *vcpu)
4388{
4389 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004390 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004391
4392 if (!apic || !apic->vapic_addr)
4393 return;
4394
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004395 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004396 kvm_release_page_dirty(apic->vapic_page);
4397 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004398 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004399}
4400
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004401static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4402{
4403 int max_irr, tpr;
4404
4405 if (!kvm_x86_ops->update_cr8_intercept)
4406 return;
4407
Avi Kivity88c808f2009-08-17 22:49:40 +03004408 if (!vcpu->arch.apic)
4409 return;
4410
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004411 if (!vcpu->arch.apic->vapic_addr)
4412 max_irr = kvm_lapic_find_highest_irr(vcpu);
4413 else
4414 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004415
4416 if (max_irr != -1)
4417 max_irr >>= 4;
4418
4419 tpr = kvm_lapic_get_cr8(vcpu);
4420
4421 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4422}
4423
Avi Kivity851ba692009-08-24 11:10:17 +03004424static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004425{
4426 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004427 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02004428 trace_kvm_inj_exception(vcpu->arch.exception.nr,
4429 vcpu->arch.exception.has_error_code,
4430 vcpu->arch.exception.error_code);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004431 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4432 vcpu->arch.exception.has_error_code,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004433 vcpu->arch.exception.error_code,
4434 vcpu->arch.exception.reinject);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004435 return;
4436 }
4437
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004438 if (vcpu->arch.nmi_injected) {
4439 kvm_x86_ops->set_nmi(vcpu);
4440 return;
4441 }
4442
4443 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004444 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004445 return;
4446 }
4447
4448 /* try to inject new event if pending */
4449 if (vcpu->arch.nmi_pending) {
4450 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4451 vcpu->arch.nmi_pending = false;
4452 vcpu->arch.nmi_injected = true;
4453 kvm_x86_ops->set_nmi(vcpu);
4454 }
4455 } else if (kvm_cpu_has_interrupt(vcpu)) {
4456 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004457 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4458 false);
4459 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004460 }
4461 }
4462}
4463
Avi Kivity851ba692009-08-24 11:10:17 +03004464static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004465{
4466 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004467 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004468 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004469
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004470 if (vcpu->requests)
4471 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4472 kvm_mmu_unload(vcpu);
4473
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004474 r = kvm_mmu_reload(vcpu);
4475 if (unlikely(r))
4476 goto out;
4477
Avi Kivity2f52d582008-01-16 12:49:30 +02004478 if (vcpu->requests) {
4479 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004480 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004481 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4482 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004483 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4484 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004485 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4486 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004487 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4488 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004489 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004490 r = 0;
4491 goto out;
4492 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004493 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004494 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004495 r = 0;
4496 goto out;
4497 }
Avi Kivity02daab22009-12-30 12:40:26 +02004498 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4499 vcpu->fpu_active = 0;
4500 kvm_x86_ops->fpu_deactivate(vcpu);
4501 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004502 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004503
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004504 preempt_disable();
4505
4506 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004507 if (vcpu->fpu_active)
4508 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004509
4510 local_irq_disable();
4511
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004512 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4513 smp_mb__after_clear_bit();
4514
Marcelo Tosattid7690172008-09-08 15:23:48 -03004515 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004516 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c142802008-01-15 18:27:32 +02004517 local_irq_enable();
4518 preempt_enable();
4519 r = 1;
4520 goto out;
4521 }
4522
Avi Kivity851ba692009-08-24 11:10:17 +03004523 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004524
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004525 /* enable NMI/IRQ window open exits if needed */
4526 if (vcpu->arch.nmi_pending)
4527 kvm_x86_ops->enable_nmi_window(vcpu);
4528 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4529 kvm_x86_ops->enable_irq_window(vcpu);
4530
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004531 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004532 update_cr8_intercept(vcpu);
4533 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004534 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004535
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004536 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004537
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004538 kvm_guest_enter();
4539
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004540 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004541 set_debugreg(0, 7);
4542 set_debugreg(vcpu->arch.eff_db[0], 0);
4543 set_debugreg(vcpu->arch.eff_db[1], 1);
4544 set_debugreg(vcpu->arch.eff_db[2], 2);
4545 set_debugreg(vcpu->arch.eff_db[3], 3);
4546 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004547
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004548 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004549 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004550
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004551 /*
4552 * If the guest has used debug registers, at least dr7
4553 * will be disabled while returning to the host.
4554 * If we don't have active breakpoints in the host, we don't
4555 * care about the messed up debug address registers. But if
4556 * we have some of them active, restore the old state.
4557 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004558 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004559 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004560
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004561 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004562 local_irq_enable();
4563
4564 ++vcpu->stat.exits;
4565
4566 /*
4567 * We must have an instruction between local_irq_enable() and
4568 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4569 * the interrupt shadow. The stat.exits increment will do nicely.
4570 * But we need to prevent reordering, hence this barrier():
4571 */
4572 barrier();
4573
4574 kvm_guest_exit();
4575
4576 preempt_enable();
4577
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004578 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004579
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004580 /*
4581 * Profile KVM exit RIPs:
4582 */
4583 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004584 unsigned long rip = kvm_rip_read(vcpu);
4585 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004586 }
4587
Avi Kivity298101d2007-11-25 13:41:11 +02004588
Avi Kivityb93463a2007-10-25 16:52:32 +02004589 kvm_lapic_sync_from_vapic(vcpu);
4590
Avi Kivity851ba692009-08-24 11:10:17 +03004591 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004592out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004593 return r;
4594}
4595
Gleb Natapov09cec752009-03-23 15:11:44 +02004596
Avi Kivity851ba692009-08-24 11:10:17 +03004597static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004598{
4599 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004600 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004601
4602 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004603 pr_debug("vcpu %d received sipi with vector # %x\n",
4604 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004605 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004606 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004607 if (r)
4608 return r;
4609 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004610 }
4611
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004612 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004613 vapic_enter(vcpu);
4614
4615 r = 1;
4616 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004617 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004618 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004619 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004620 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004621 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004622 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004623 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004624 {
4625 switch(vcpu->arch.mp_state) {
4626 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004627 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004628 KVM_MP_STATE_RUNNABLE;
4629 case KVM_MP_STATE_RUNNABLE:
4630 break;
4631 case KVM_MP_STATE_SIPI_RECEIVED:
4632 default:
4633 r = -EINTR;
4634 break;
4635 }
4636 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004637 }
4638
Gleb Natapov09cec752009-03-23 15:11:44 +02004639 if (r <= 0)
4640 break;
4641
4642 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4643 if (kvm_cpu_has_pending_timer(vcpu))
4644 kvm_inject_pending_timer_irqs(vcpu);
4645
Avi Kivity851ba692009-08-24 11:10:17 +03004646 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004647 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004648 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004649 ++vcpu->stat.request_irq_exits;
4650 }
4651 if (signal_pending(current)) {
4652 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004653 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004654 ++vcpu->stat.signal_exits;
4655 }
4656 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004657 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004658 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004659 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004660 }
4661 }
4662
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004663 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004664
Avi Kivityb93463a2007-10-25 16:52:32 +02004665 vapic_exit(vcpu);
4666
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004667 return r;
4668}
4669
4670int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4671{
4672 int r;
4673 sigset_t sigsaved;
4674
4675 vcpu_load(vcpu);
4676
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004677 if (vcpu->sigset_active)
4678 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4679
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004680 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4681 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004682 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004683 r = -EAGAIN;
4684 goto out;
4685 }
4686
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004687 /* re-sync apic's tpr */
4688 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004689 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004690
Gleb Natapov92bf9742010-03-18 15:20:28 +02004691 if (vcpu->arch.pio.count || vcpu->mmio_needed ||
4692 vcpu->arch.emulate_ctxt.restart) {
4693 if (vcpu->mmio_needed) {
4694 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4695 vcpu->mmio_read_completed = 1;
4696 vcpu->mmio_needed = 0;
Gleb Natapov79729952010-03-18 15:20:24 +02004697 }
Gleb Natapov5cd21912010-03-18 15:20:26 +02004698 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4699 r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE);
4700 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4701 if (r == EMULATE_DO_MMIO) {
4702 r = 0;
4703 goto out;
4704 }
4705 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004706 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4707 kvm_register_write(vcpu, VCPU_REGS_RAX,
4708 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004709
Avi Kivity851ba692009-08-24 11:10:17 +03004710 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004711
4712out:
Marcelo Tosattif1d86e42010-05-03 23:04:27 -03004713 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004714 if (vcpu->sigset_active)
4715 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4716
4717 vcpu_put(vcpu);
4718 return r;
4719}
4720
4721int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4722{
4723 vcpu_load(vcpu);
4724
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004725 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4726 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4727 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4728 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4729 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4730 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4731 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4732 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004733#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004734 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4735 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4736 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4737 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4738 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4739 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4740 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4741 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004742#endif
4743
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004744 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004745 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004746
4747 vcpu_put(vcpu);
4748
4749 return 0;
4750}
4751
4752int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4753{
4754 vcpu_load(vcpu);
4755
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004756 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4757 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4758 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4759 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4760 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4761 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4762 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4763 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004764#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004765 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4766 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4767 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4768 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4769 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4770 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4771 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4772 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004773#endif
4774
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004775 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004776 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004777
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004778 vcpu->arch.exception.pending = false;
4779
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004780 vcpu_put(vcpu);
4781
4782 return 0;
4783}
4784
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004785void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4786{
4787 struct kvm_segment cs;
4788
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004789 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004790 *db = cs.db;
4791 *l = cs.l;
4792}
4793EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4794
4795int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4796 struct kvm_sregs *sregs)
4797{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004798 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004799
4800 vcpu_load(vcpu);
4801
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004802 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4803 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4804 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4805 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4806 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4807 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004808
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004809 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4810 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004811
4812 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004813 sregs->idt.limit = dt.size;
4814 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004815 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004816 sregs->gdt.limit = dt.size;
4817 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004818
Avi Kivity4d4ec082009-12-29 18:07:30 +02004819 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004820 sregs->cr2 = vcpu->arch.cr2;
4821 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004822 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004823 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02004824 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004825 sregs->apic_base = kvm_get_apic_base(vcpu);
4826
Gleb Natapov923c61b2009-05-11 13:35:48 +03004827 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004828
Gleb Natapov36752c92009-05-11 13:35:53 +03004829 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004830 set_bit(vcpu->arch.interrupt.nr,
4831 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004832
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004833 vcpu_put(vcpu);
4834
4835 return 0;
4836}
4837
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004838int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4839 struct kvm_mp_state *mp_state)
4840{
4841 vcpu_load(vcpu);
4842 mp_state->mp_state = vcpu->arch.mp_state;
4843 vcpu_put(vcpu);
4844 return 0;
4845}
4846
4847int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4848 struct kvm_mp_state *mp_state)
4849{
4850 vcpu_load(vcpu);
4851 vcpu->arch.mp_state = mp_state->mp_state;
4852 vcpu_put(vcpu);
4853 return 0;
4854}
4855
Jan Kiszkae269fb22010-04-14 15:51:09 +02004856int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
4857 bool has_error_code, u32 error_code)
Izik Eidus37817f22008-03-24 23:14:53 +02004858{
Gleb Natapovceffb452010-03-18 15:20:19 +02004859 int cs_db, cs_l, ret;
4860 cache_all_regs(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004861
Gleb Natapovceffb452010-03-18 15:20:19 +02004862 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
Izik Eidus37817f22008-03-24 23:14:53 +02004863
Gleb Natapovceffb452010-03-18 15:20:19 +02004864 vcpu->arch.emulate_ctxt.vcpu = vcpu;
4865 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
4866 vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
4867 vcpu->arch.emulate_ctxt.mode =
4868 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
4869 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
4870 ? X86EMUL_MODE_VM86 : cs_l
4871 ? X86EMUL_MODE_PROT64 : cs_db
4872 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
Izik Eidus37817f22008-03-24 23:14:53 +02004873
Gleb Natapovceffb452010-03-18 15:20:19 +02004874 ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops,
Jan Kiszkae269fb22010-04-14 15:51:09 +02004875 tss_selector, reason, has_error_code,
4876 error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02004877
Gleb Natapov19d04432010-04-15 12:29:50 +03004878 if (ret)
4879 return EMULATE_FAIL;
Izik Eidus37817f22008-03-24 23:14:53 +02004880
Gleb Natapov19d04432010-04-15 12:29:50 +03004881 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
4882 return EMULATE_DONE;
Izik Eidus37817f22008-03-24 23:14:53 +02004883}
4884EXPORT_SYMBOL_GPL(kvm_task_switch);
4885
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004886int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4887 struct kvm_sregs *sregs)
4888{
4889 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004890 int pending_vec, max_bits;
Gleb Natapov89a27f42010-02-16 10:51:48 +02004891 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004892
4893 vcpu_load(vcpu);
4894
Gleb Natapov89a27f42010-02-16 10:51:48 +02004895 dt.size = sregs->idt.limit;
4896 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004897 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004898 dt.size = sregs->gdt.limit;
4899 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004900 kvm_x86_ops->set_gdt(vcpu, &dt);
4901
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004902 vcpu->arch.cr2 = sregs->cr2;
4903 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02004904 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004905
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004906 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004907
Avi Kivityf6801df2010-01-21 15:31:50 +02004908 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004909 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004910 kvm_set_apic_base(vcpu, sregs->apic_base);
4911
Avi Kivity4d4ec082009-12-29 18:07:30 +02004912 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004913 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004914 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004915
Avi Kivityfc78f512009-12-07 12:16:48 +02004916 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004917 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004918 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004919 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004920 mmu_reset_needed = 1;
4921 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004922
4923 if (mmu_reset_needed)
4924 kvm_mmu_reset_context(vcpu);
4925
Gleb Natapov923c61b2009-05-11 13:35:48 +03004926 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4927 pending_vec = find_first_bit(
4928 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4929 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004930 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004931 pr_debug("Set back pending irq %d\n", pending_vec);
4932 if (irqchip_in_kernel(vcpu->kvm))
4933 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004934 }
4935
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004936 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4937 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4938 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4939 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4940 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4941 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004942
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004943 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4944 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004945
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03004946 update_cr8_intercept(vcpu);
4947
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004948 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004949 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004950 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02004951 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004952 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4953
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004954 vcpu_put(vcpu);
4955
4956 return 0;
4957}
4958
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004959int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4960 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004961{
Jan Kiszka355be0b2009-10-03 00:31:21 +02004962 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004963 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004964
4965 vcpu_load(vcpu);
4966
Jan Kiszka4f926bf22009-10-30 12:46:59 +01004967 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
4968 r = -EBUSY;
4969 if (vcpu->arch.exception.pending)
4970 goto unlock_out;
4971 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4972 kvm_queue_exception(vcpu, DB_VECTOR);
4973 else
4974 kvm_queue_exception(vcpu, BP_VECTOR);
4975 }
4976
Jan Kiszka91586a32009-10-05 13:07:21 +02004977 /*
4978 * Read rflags as long as potentially injected trace flags are still
4979 * filtered out.
4980 */
4981 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02004982
4983 vcpu->guest_debug = dbg->control;
4984 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
4985 vcpu->guest_debug = 0;
4986
4987 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004988 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4989 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4990 vcpu->arch.switch_db_regs =
4991 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4992 } else {
4993 for (i = 0; i < KVM_NR_DB_REGS; i++)
4994 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4995 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4996 }
4997
Jan Kiszkaf92653e2010-02-23 17:47:55 +01004998 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4999 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5000 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005001
Jan Kiszka91586a32009-10-05 13:07:21 +02005002 /*
5003 * Trigger an rflags update that will inject or remove the trace
5004 * flags.
5005 */
5006 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005007
Jan Kiszka355be0b2009-10-03 00:31:21 +02005008 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5009
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005010 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005011
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005012unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005013 vcpu_put(vcpu);
5014
5015 return r;
5016}
5017
5018/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005019 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5020 * we have asm/x86/processor.h
5021 */
5022struct fxsave {
5023 u16 cwd;
5024 u16 swd;
5025 u16 twd;
5026 u16 fop;
5027 u64 rip;
5028 u64 rdp;
5029 u32 mxcsr;
5030 u32 mxcsr_mask;
5031 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5032#ifdef CONFIG_X86_64
5033 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5034#else
5035 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5036#endif
5037};
5038
Zhang Xiantao8b006792007-11-16 13:05:55 +08005039/*
5040 * Translate a guest virtual address to a guest physical address.
5041 */
5042int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5043 struct kvm_translation *tr)
5044{
5045 unsigned long vaddr = tr->linear_address;
5046 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005047 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005048
5049 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005050 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005051 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005052 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005053 tr->physical_address = gpa;
5054 tr->valid = gpa != UNMAPPED_GVA;
5055 tr->writeable = 1;
5056 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005057 vcpu_put(vcpu);
5058
5059 return 0;
5060}
5061
Hollis Blanchardd0752062007-10-31 17:24:25 -05005062int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5063{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005064 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005065
5066 vcpu_load(vcpu);
5067
5068 memcpy(fpu->fpr, fxsave->st_space, 128);
5069 fpu->fcw = fxsave->cwd;
5070 fpu->fsw = fxsave->swd;
5071 fpu->ftwx = fxsave->twd;
5072 fpu->last_opcode = fxsave->fop;
5073 fpu->last_ip = fxsave->rip;
5074 fpu->last_dp = fxsave->rdp;
5075 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5076
5077 vcpu_put(vcpu);
5078
5079 return 0;
5080}
5081
5082int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5083{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005084 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005085
5086 vcpu_load(vcpu);
5087
5088 memcpy(fxsave->st_space, fpu->fpr, 128);
5089 fxsave->cwd = fpu->fcw;
5090 fxsave->swd = fpu->fsw;
5091 fxsave->twd = fpu->ftwx;
5092 fxsave->fop = fpu->last_opcode;
5093 fxsave->rip = fpu->last_ip;
5094 fxsave->rdp = fpu->last_dp;
5095 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5096
5097 vcpu_put(vcpu);
5098
5099 return 0;
5100}
5101
5102void fx_init(struct kvm_vcpu *vcpu)
5103{
5104 unsigned after_mxcsr_mask;
5105
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005106 /*
5107 * Touch the fpu the first time in non atomic context as if
5108 * this is the first fpu instruction the exception handler
5109 * will fire before the instruction returns and it'll have to
5110 * allocate ram with GFP_KERNEL.
5111 */
5112 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005113 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005114
Hollis Blanchardd0752062007-10-31 17:24:25 -05005115 /* Initialize guest FPU by resetting ours and saving into guest's */
5116 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005117 kvm_fx_save(&vcpu->arch.host_fx_image);
5118 kvm_fx_finit();
5119 kvm_fx_save(&vcpu->arch.guest_fx_image);
5120 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005121 preempt_enable();
5122
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005123 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005124 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005125 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5126 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005127 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5128}
5129EXPORT_SYMBOL_GPL(fx_init);
5130
5131void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5132{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005133 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005134 return;
5135
5136 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005137 kvm_fx_save(&vcpu->arch.host_fx_image);
5138 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Avi Kivity0c048512010-01-21 15:31:52 +02005139 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005140}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005141
5142void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5143{
5144 if (!vcpu->guest_fpu_loaded)
5145 return;
5146
5147 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005148 kvm_fx_save(&vcpu->arch.guest_fx_image);
5149 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005150 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005151 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Avi Kivity0c048512010-01-21 15:31:52 +02005152 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005153}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005154
5155void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5156{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005157 if (vcpu->arch.time_page) {
5158 kvm_release_page_dirty(vcpu->arch.time_page);
5159 vcpu->arch.time_page = NULL;
5160 }
5161
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005162 kvm_x86_ops->vcpu_free(vcpu);
5163}
5164
5165struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5166 unsigned int id)
5167{
Avi Kivity26e52152007-11-20 15:30:24 +02005168 return kvm_x86_ops->vcpu_create(kvm, id);
5169}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005170
Avi Kivity26e52152007-11-20 15:30:24 +02005171int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5172{
5173 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005174
5175 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005176 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005177
Sheng Yang0bed3b52008-10-09 16:01:54 +08005178 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005179 vcpu_load(vcpu);
5180 r = kvm_arch_vcpu_reset(vcpu);
5181 if (r == 0)
5182 r = kvm_mmu_setup(vcpu);
5183 vcpu_put(vcpu);
5184 if (r < 0)
5185 goto free_vcpu;
5186
Avi Kivity26e52152007-11-20 15:30:24 +02005187 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005188free_vcpu:
5189 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005190 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005191}
5192
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005193void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005194{
5195 vcpu_load(vcpu);
5196 kvm_mmu_unload(vcpu);
5197 vcpu_put(vcpu);
5198
5199 kvm_x86_ops->vcpu_free(vcpu);
5200}
5201
5202int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5203{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005204 vcpu->arch.nmi_pending = false;
5205 vcpu->arch.nmi_injected = false;
5206
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005207 vcpu->arch.switch_db_regs = 0;
5208 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5209 vcpu->arch.dr6 = DR6_FIXED_1;
5210 vcpu->arch.dr7 = DR7_FIXED_1;
5211
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005212 return kvm_x86_ops->vcpu_reset(vcpu);
5213}
5214
Alexander Graf10474ae2009-09-15 11:37:46 +02005215int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005216{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005217 /*
5218 * Since this may be called from a hotplug notifcation,
5219 * we can't get the CPU frequency directly.
5220 */
5221 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5222 int cpu = raw_smp_processor_id();
5223 per_cpu(cpu_tsc_khz, cpu) = 0;
5224 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005225
5226 kvm_shared_msr_cpu_online();
5227
Alexander Graf10474ae2009-09-15 11:37:46 +02005228 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005229}
5230
5231void kvm_arch_hardware_disable(void *garbage)
5232{
5233 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005234 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005235}
5236
5237int kvm_arch_hardware_setup(void)
5238{
5239 return kvm_x86_ops->hardware_setup();
5240}
5241
5242void kvm_arch_hardware_unsetup(void)
5243{
5244 kvm_x86_ops->hardware_unsetup();
5245}
5246
5247void kvm_arch_check_processor_compat(void *rtn)
5248{
5249 kvm_x86_ops->check_processor_compatibility(rtn);
5250}
5251
5252int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5253{
5254 struct page *page;
5255 struct kvm *kvm;
5256 int r;
5257
5258 BUG_ON(vcpu->kvm == NULL);
5259 kvm = vcpu->kvm;
5260
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005261 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005262 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005263 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005264 else
Avi Kivitya4535292008-04-13 17:54:35 +03005265 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005266
5267 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5268 if (!page) {
5269 r = -ENOMEM;
5270 goto fail;
5271 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005272 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005273
5274 r = kvm_mmu_create(vcpu);
5275 if (r < 0)
5276 goto fail_free_pio_data;
5277
5278 if (irqchip_in_kernel(kvm)) {
5279 r = kvm_create_lapic(vcpu);
5280 if (r < 0)
5281 goto fail_mmu_destroy;
5282 }
5283
Huang Ying890ca9a2009-05-11 16:48:15 +08005284 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5285 GFP_KERNEL);
5286 if (!vcpu->arch.mce_banks) {
5287 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005288 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005289 }
5290 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5291
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005292 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005293fail_free_lapic:
5294 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005295fail_mmu_destroy:
5296 kvm_mmu_destroy(vcpu);
5297fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005298 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005299fail:
5300 return r;
5301}
5302
5303void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5304{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005305 int idx;
5306
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005307 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005308 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005309 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005310 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005311 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005312 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005313}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005314
5315struct kvm *kvm_arch_create_vm(void)
5316{
5317 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5318
5319 if (!kvm)
5320 return ERR_PTR(-ENOMEM);
5321
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005322 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5323 if (!kvm->arch.aliases) {
5324 kfree(kvm);
5325 return ERR_PTR(-ENOMEM);
5326 }
5327
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005328 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005329 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005330
Sheng Yang5550af42008-10-15 20:15:06 +08005331 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5332 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5333
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005334 rdtscll(kvm->arch.vm_init_tsc);
5335
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005336 return kvm;
5337}
5338
5339static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5340{
5341 vcpu_load(vcpu);
5342 kvm_mmu_unload(vcpu);
5343 vcpu_put(vcpu);
5344}
5345
5346static void kvm_free_vcpus(struct kvm *kvm)
5347{
5348 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005349 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005350
5351 /*
5352 * Unpin any mmu pages first.
5353 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005354 kvm_for_each_vcpu(i, vcpu, kvm)
5355 kvm_unload_vcpu_mmu(vcpu);
5356 kvm_for_each_vcpu(i, vcpu, kvm)
5357 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005358
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005359 mutex_lock(&kvm->lock);
5360 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5361 kvm->vcpus[i] = NULL;
5362
5363 atomic_set(&kvm->online_vcpus, 0);
5364 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005365}
5366
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005367void kvm_arch_sync_events(struct kvm *kvm)
5368{
Sheng Yangba4cef32009-01-06 10:03:03 +08005369 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005370}
5371
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005372void kvm_arch_destroy_vm(struct kvm *kvm)
5373{
Sheng Yang6eb55812008-10-31 12:37:41 +08005374 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005375 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005376 kfree(kvm->arch.vpic);
5377 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005378 kvm_free_vcpus(kvm);
5379 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005380 if (kvm->arch.apic_access_page)
5381 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005382 if (kvm->arch.ept_identity_pagetable)
5383 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosatti64749202010-01-19 12:45:23 -02005384 cleanup_srcu_struct(&kvm->srcu);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005385 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005386 kfree(kvm);
5387}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005388
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005389int kvm_arch_prepare_memory_region(struct kvm *kvm,
5390 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005391 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005392 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005393 int user_alloc)
5394{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005395 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005396
5397 /*To keep backward compatibility with older userspace,
5398 *x86 needs to hanlde !user_alloc case.
5399 */
5400 if (!user_alloc) {
5401 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005402 unsigned long userspace_addr;
5403
Izik Eidus72dc67a2008-02-10 18:04:15 +02005404 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005405 userspace_addr = do_mmap(NULL, 0,
5406 npages * PAGE_SIZE,
5407 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005408 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005409 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005410 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005411
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005412 if (IS_ERR((void *)userspace_addr))
5413 return PTR_ERR((void *)userspace_addr);
5414
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005415 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005416 }
5417 }
5418
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005419
5420 return 0;
5421}
5422
5423void kvm_arch_commit_memory_region(struct kvm *kvm,
5424 struct kvm_userspace_memory_region *mem,
5425 struct kvm_memory_slot old,
5426 int user_alloc)
5427{
5428
5429 int npages = mem->memory_size >> PAGE_SHIFT;
5430
5431 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5432 int ret;
5433
5434 down_write(&current->mm->mmap_sem);
5435 ret = do_munmap(current->mm, old.userspace_addr,
5436 old.npages * PAGE_SIZE);
5437 up_write(&current->mm->mmap_sem);
5438 if (ret < 0)
5439 printk(KERN_WARNING
5440 "kvm_vm_ioctl_set_memory_region: "
5441 "failed to munmap memory\n");
5442 }
5443
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005444 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005445 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005446 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5447 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5448 }
5449
5450 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005451 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005452}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005453
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005454void kvm_arch_flush_shadow(struct kvm *kvm)
5455{
5456 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005457 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005458}
5459
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005460int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5461{
Avi Kivitya4535292008-04-13 17:54:35 +03005462 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005463 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5464 || vcpu->arch.nmi_pending ||
5465 (kvm_arch_interrupt_allowed(vcpu) &&
5466 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005467}
Zhang Xiantao57361992007-12-17 14:21:40 +08005468
Zhang Xiantao57361992007-12-17 14:21:40 +08005469void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5470{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005471 int me;
5472 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005473
5474 if (waitqueue_active(&vcpu->wq)) {
5475 wake_up_interruptible(&vcpu->wq);
5476 ++vcpu->stat.halt_wakeup;
5477 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005478
5479 me = get_cpu();
5480 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5481 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5482 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005483 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005484}
Gleb Natapov78646122009-03-23 12:12:11 +02005485
5486int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5487{
5488 return kvm_x86_ops->interrupt_allowed(vcpu);
5489}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005490
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005491bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5492{
5493 unsigned long current_rip = kvm_rip_read(vcpu) +
5494 get_segment_base(vcpu, VCPU_SREG_CS);
5495
5496 return current_rip == linear_rip;
5497}
5498EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5499
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005500unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5501{
5502 unsigned long rflags;
5503
5504 rflags = kvm_x86_ops->get_rflags(vcpu);
5505 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01005506 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005507 return rflags;
5508}
5509EXPORT_SYMBOL_GPL(kvm_get_rflags);
5510
5511void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5512{
5513 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005514 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01005515 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005516 kvm_x86_ops->set_rflags(vcpu, rflags);
5517}
5518EXPORT_SYMBOL_GPL(kvm_set_rflags);
5519
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005520EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5521EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5522EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5523EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5524EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005525EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005526EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005527EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005528EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005529EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005530EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01005531EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);