blob: a1cf87fe9f3ac3de4463f13c899ea86e2a325485 [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>
Avi Kivityaec51dc2009-07-01 16:01:02 +030043#include <trace/events/kvm.h>
Xiao Guangrong2ed152a2010-03-10 19:00:43 +080044
Marcelo Tosatti229456f2009-06-17 09:22:14 -030045#define CREATE_TRACE_POINTS
46#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020047
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020048#include <asm/debugreg.h>
Carsten Otte043405e2007-10-10 17:16:19 +020049#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080050#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020051#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080052#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080053#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020054
Carsten Otte313a3dc2007-10-11 19:16:52 +020055#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010056#define CR0_RESERVED_BITS \
57 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
58 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
59 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
60#define CR4_RESERVED_BITS \
61 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
62 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
63 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
64 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
65
66#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080067
68#define KVM_MAX_MCE_BANKS 32
69#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
70
Joerg Roedel50a37eb2008-01-31 14:57:38 +010071/* EFER defaults:
72 * - enable syscall per default because its emulated by KVM
73 * - enable LME and LMA per default on 64 bit KVM
74 */
75#ifdef CONFIG_X86_64
76static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
77#else
78static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
79#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020080
Avi Kivityba1389b2007-11-18 16:24:12 +020081#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
82#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050083
Gleb Natapovcb142eb2009-08-09 15:17:40 +030084static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020085static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
86 struct kvm_cpuid_entry2 __user *entries);
87
Zhang Xiantao97896d02007-11-14 20:09:30 +080088struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030089EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080090
Andre Przywaraed85c062009-06-25 12:36:49 +020091int ignore_msrs = 0;
92module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
93
Avi Kivity18863bd2009-09-07 11:12:18 +030094#define KVM_NR_SHARED_MSRS 16
95
96struct kvm_shared_msrs_global {
97 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +080098 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +030099};
100
101struct kvm_shared_msrs {
102 struct user_return_notifier urn;
103 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800104 struct kvm_shared_msr_values {
105 u64 host;
106 u64 curr;
107 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300108};
109
110static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
111static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
112
Hollis Blanchard417bc302007-10-31 17:24:23 -0500113struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200114 { "pf_fixed", VCPU_STAT(pf_fixed) },
115 { "pf_guest", VCPU_STAT(pf_guest) },
116 { "tlb_flush", VCPU_STAT(tlb_flush) },
117 { "invlpg", VCPU_STAT(invlpg) },
118 { "exits", VCPU_STAT(exits) },
119 { "io_exits", VCPU_STAT(io_exits) },
120 { "mmio_exits", VCPU_STAT(mmio_exits) },
121 { "signal_exits", VCPU_STAT(signal_exits) },
122 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800123 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200124 { "halt_exits", VCPU_STAT(halt_exits) },
125 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530126 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200127 { "request_irq", VCPU_STAT(request_irq_exits) },
128 { "irq_exits", VCPU_STAT(irq_exits) },
129 { "host_state_reload", VCPU_STAT(host_state_reload) },
130 { "efer_reload", VCPU_STAT(efer_reload) },
131 { "fpu_reload", VCPU_STAT(fpu_reload) },
132 { "insn_emulation", VCPU_STAT(insn_emulation) },
133 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300134 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200135 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200136 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
137 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
138 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
139 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
140 { "mmu_flooded", VM_STAT(mmu_flooded) },
141 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200142 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300143 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200144 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300145 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500146 { NULL }
147};
148
Avi Kivity18863bd2009-09-07 11:12:18 +0300149static void kvm_on_user_return(struct user_return_notifier *urn)
150{
151 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300152 struct kvm_shared_msrs *locals
153 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800154 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300155
156 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800157 values = &locals->values[slot];
158 if (values->host != values->curr) {
159 wrmsrl(shared_msrs_global.msrs[slot], values->host);
160 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300161 }
162 }
163 locals->registered = false;
164 user_return_notifier_unregister(urn);
165}
166
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800167static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300168{
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800169 struct kvm_shared_msrs *smsr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300170 u64 value;
171
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800172 smsr = &__get_cpu_var(shared_msrs);
173 /* only read, and nobody should modify it at this time,
174 * so don't need lock */
175 if (slot >= shared_msrs_global.nr) {
176 printk(KERN_ERR "kvm: invalid MSR slot!");
177 return;
178 }
179 rdmsrl_safe(msr, &value);
180 smsr->values[slot].host = value;
181 smsr->values[slot].curr = value;
182}
183
184void kvm_define_shared_msr(unsigned slot, u32 msr)
185{
Avi Kivity18863bd2009-09-07 11:12:18 +0300186 if (slot >= shared_msrs_global.nr)
187 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800188 shared_msrs_global.msrs[slot] = msr;
189 /* we need ensured the shared_msr_global have been updated */
190 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300191}
192EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
193
194static void kvm_shared_msr_cpu_online(void)
195{
196 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300197
198 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800199 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300200}
201
Avi Kivityd5696722009-12-02 12:28:47 +0200202void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300203{
204 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
205
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800206 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300207 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800208 smsr->values[slot].curr = value;
209 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300210 if (!smsr->registered) {
211 smsr->urn.on_user_return = kvm_on_user_return;
212 user_return_notifier_register(&smsr->urn);
213 smsr->registered = true;
214 }
215}
216EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
217
Avi Kivity3548bab2009-11-28 14:18:47 +0200218static void drop_user_return_notifiers(void *ignore)
219{
220 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
221
222 if (smsr->registered)
223 kvm_on_user_return(&smsr->urn);
224}
225
Carsten Otte6866b832007-10-29 16:09:10 +0100226u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
227{
228 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800229 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100230 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800231 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100232}
233EXPORT_SYMBOL_GPL(kvm_get_apic_base);
234
235void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
236{
237 /* TODO: reserve bits check */
238 if (irqchip_in_kernel(vcpu->kvm))
239 kvm_lapic_set_base(vcpu, data);
240 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800241 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100242}
243EXPORT_SYMBOL_GPL(kvm_set_apic_base);
244
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200245#define EXCPT_BENIGN 0
246#define EXCPT_CONTRIBUTORY 1
247#define EXCPT_PF 2
248
249static int exception_class(int vector)
250{
251 switch (vector) {
252 case PF_VECTOR:
253 return EXCPT_PF;
254 case DE_VECTOR:
255 case TS_VECTOR:
256 case NP_VECTOR:
257 case SS_VECTOR:
258 case GP_VECTOR:
259 return EXCPT_CONTRIBUTORY;
260 default:
261 break;
262 }
263 return EXCPT_BENIGN;
264}
265
266static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
267 unsigned nr, bool has_error, u32 error_code)
268{
269 u32 prev_nr;
270 int class1, class2;
271
272 if (!vcpu->arch.exception.pending) {
273 queue:
274 vcpu->arch.exception.pending = true;
275 vcpu->arch.exception.has_error_code = has_error;
276 vcpu->arch.exception.nr = nr;
277 vcpu->arch.exception.error_code = error_code;
278 return;
279 }
280
281 /* to check exception */
282 prev_nr = vcpu->arch.exception.nr;
283 if (prev_nr == DF_VECTOR) {
284 /* triple fault -> shutdown */
285 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
286 return;
287 }
288 class1 = exception_class(prev_nr);
289 class2 = exception_class(nr);
290 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
291 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
292 /* generate double fault per SDM Table 5-5 */
293 vcpu->arch.exception.pending = true;
294 vcpu->arch.exception.has_error_code = true;
295 vcpu->arch.exception.nr = DF_VECTOR;
296 vcpu->arch.exception.error_code = 0;
297 } else
298 /* replace previous exception with a new one in a hope
299 that instruction re-execution will regenerate lost
300 exception */
301 goto queue;
302}
303
Avi Kivity298101d2007-11-25 13:41:11 +0200304void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
305{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200306 kvm_multiple_exception(vcpu, nr, false, 0);
Avi Kivity298101d2007-11-25 13:41:11 +0200307}
308EXPORT_SYMBOL_GPL(kvm_queue_exception);
309
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200310void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
311 u32 error_code)
312{
313 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800314 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200315 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
316}
317
Sheng Yang3419ffc2008-05-15 09:52:48 +0800318void kvm_inject_nmi(struct kvm_vcpu *vcpu)
319{
320 vcpu->arch.nmi_pending = 1;
321}
322EXPORT_SYMBOL_GPL(kvm_inject_nmi);
323
Avi Kivity298101d2007-11-25 13:41:11 +0200324void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
325{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200326 kvm_multiple_exception(vcpu, nr, true, error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200327}
328EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
329
Carsten Ottea03490e2007-10-29 16:09:35 +0100330/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300331 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
332 * a #GP and return false.
333 */
334bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200335{
Avi Kivity0a79b002009-09-01 12:03:25 +0300336 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
337 return true;
338 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
339 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100340}
Avi Kivity0a79b002009-09-01 12:03:25 +0300341EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100342
343/*
344 * Load the pae pdptrs. Return true is they are all valid.
345 */
346int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
347{
348 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
349 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
350 int i;
351 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800352 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100353
Carsten Ottea03490e2007-10-29 16:09:35 +0100354 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
355 offset * sizeof(u64), sizeof(pdpte));
356 if (ret < 0) {
357 ret = 0;
358 goto out;
359 }
360 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300361 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800362 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100363 ret = 0;
364 goto out;
365 }
366 }
367 ret = 1;
368
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300370 __set_bit(VCPU_EXREG_PDPTR,
371 (unsigned long *)&vcpu->arch.regs_avail);
372 __set_bit(VCPU_EXREG_PDPTR,
373 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100374out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100375
376 return ret;
377}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100378EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100379
Avi Kivityd835dfe2007-11-21 02:57:59 +0200380static bool pdptrs_changed(struct kvm_vcpu *vcpu)
381{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800382 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200383 bool changed = true;
384 int r;
385
386 if (is_long_mode(vcpu) || !is_pae(vcpu))
387 return false;
388
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300389 if (!test_bit(VCPU_EXREG_PDPTR,
390 (unsigned long *)&vcpu->arch.regs_avail))
391 return true;
392
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800393 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200394 if (r < 0)
395 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800396 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200397out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200398
399 return changed;
400}
401
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200402void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100403{
Avi Kivityf9a48e62010-01-06 19:10:22 +0200404 cr0 |= X86_CR0_ET;
405
Gleb Natapovab344822010-01-21 15:28:46 +0200406#ifdef CONFIG_X86_64
407 if (cr0 & 0xffffffff00000000UL) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200408 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100409 return;
410 }
Gleb Natapovab344822010-01-21 15:28:46 +0200411#endif
412
413 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100414
415 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200416 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100417 return;
418 }
419
420 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200421 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100422 return;
423 }
424
425 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
426#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200427 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100428 int cs_db, cs_l;
429
430 if (!is_pae(vcpu)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200431 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100432 return;
433 }
434 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
435 if (cs_l) {
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 } else
441#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800442 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200443 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100444 return;
445 }
446
447 }
448
449 kvm_x86_ops->set_cr0(vcpu, cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100450
Carsten Ottea03490e2007-10-29 16:09:35 +0100451 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100452 return;
453}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200454EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100455
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200456void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100457{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200458 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100459}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200460EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100461
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200462void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100463{
Avi Kivityfc78f512009-12-07 12:16:48 +0200464 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300465 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
466
Carsten Ottea03490e2007-10-29 16:09:35 +0100467 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200468 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100469 return;
470 }
471
472 if (is_long_mode(vcpu)) {
473 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200474 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100475 return;
476 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300477 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
478 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800479 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200480 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100481 return;
482 }
483
484 if (cr4 & X86_CR4_VMXE) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200485 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100486 return;
487 }
488 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800489 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200490 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100491 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100492}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200493EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100494
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200495void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100496{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800497 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300498 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200499 kvm_mmu_flush_tlb(vcpu);
500 return;
501 }
502
Carsten Ottea03490e2007-10-29 16:09:35 +0100503 if (is_long_mode(vcpu)) {
504 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200505 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100506 return;
507 }
508 } else {
509 if (is_pae(vcpu)) {
510 if (cr3 & CR3_PAE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200511 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100512 return;
513 }
514 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200515 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100516 return;
517 }
518 }
519 /*
520 * We don't check reserved bits in nonpae mode, because
521 * this isn't enforced, and VMware depends on this.
522 */
523 }
524
Carsten Ottea03490e2007-10-29 16:09:35 +0100525 /*
526 * Does the new cr3 value map to physical memory? (Note, we
527 * catch an invalid cr3 even in real-mode, because it would
528 * cause trouble later on when we turn on paging anyway.)
529 *
530 * A real CPU would silently accept an invalid cr3 and would
531 * attempt to use it - with largely undefined (and often hard
532 * to debug) behavior on the guest side.
533 */
534 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200535 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100536 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800537 vcpu->arch.cr3 = cr3;
538 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100539 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100540}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200541EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100542
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200543void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100544{
545 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200546 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100547 return;
548 }
549 if (irqchip_in_kernel(vcpu->kvm))
550 kvm_lapic_set_tpr(vcpu, cr8);
551 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800552 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100553}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200554EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100555
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200556unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100557{
558 if (irqchip_in_kernel(vcpu->kvm))
559 return kvm_lapic_get_cr8(vcpu);
560 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800561 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100562}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200563EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100564
Alexander Grafd8017472008-11-25 20:17:11 +0100565static inline u32 bit(int bitno)
566{
567 return 1 << (bitno & 31);
568}
569
Carsten Otte043405e2007-10-10 17:16:19 +0200570/*
571 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
572 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
573 *
574 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400575 * capabilities of the host cpu. This capabilities test skips MSRs that are
576 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200577 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400578
Gleb Natapov10388a02010-01-17 15:51:23 +0200579#define KVM_SAVE_MSRS_BEGIN 5
Carsten Otte043405e2007-10-10 17:16:19 +0200580static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400581 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200582 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200583 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200584 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
585 MSR_K6_STAR,
586#ifdef CONFIG_X86_64
587 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
588#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400589 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200590};
591
592static unsigned num_msrs_to_save;
593
594static u32 emulated_msrs[] = {
595 MSR_IA32_MISC_ENABLE,
596};
597
Carsten Otte15c4a642007-10-30 18:44:17 +0100598static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
599{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100600 if (efer & efer_reserved_bits) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200601 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100602 return;
603 }
604
605 if (is_paging(vcpu)
Avi Kivityf6801df2010-01-21 15:31:50 +0200606 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200607 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100608 return;
609 }
610
Alexander Graf1b2fd702009-02-02 16:23:51 +0100611 if (efer & EFER_FFXSR) {
612 struct kvm_cpuid_entry2 *feat;
613
614 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
615 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
Alexander Graf1b2fd702009-02-02 16:23:51 +0100616 kvm_inject_gp(vcpu, 0);
617 return;
618 }
619 }
620
Alexander Grafd8017472008-11-25 20:17:11 +0100621 if (efer & EFER_SVME) {
622 struct kvm_cpuid_entry2 *feat;
623
624 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
625 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
Alexander Grafd8017472008-11-25 20:17:11 +0100626 kvm_inject_gp(vcpu, 0);
627 return;
628 }
629 }
630
Carsten Otte15c4a642007-10-30 18:44:17 +0100631 kvm_x86_ops->set_efer(vcpu, efer);
632
633 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200634 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100635
Avi Kivityf6801df2010-01-21 15:31:50 +0200636 vcpu->arch.efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300637
638 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
639 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100640}
641
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100642void kvm_enable_efer_bits(u64 mask)
643{
644 efer_reserved_bits &= ~mask;
645}
646EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
647
648
Carsten Otte15c4a642007-10-30 18:44:17 +0100649/*
650 * Writes msr value into into the appropriate "register".
651 * Returns 0 on success, non-0 otherwise.
652 * Assumes vcpu_load() was already called.
653 */
654int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
655{
656 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
657}
658
Carsten Otte313a3dc2007-10-11 19:16:52 +0200659/*
660 * Adapt set_msr() to msr_io()'s calling convention
661 */
662static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
663{
664 return kvm_set_msr(vcpu, index, *data);
665}
666
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200667static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
668{
669 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200670 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800671 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200672
673 if (!wall_clock)
674 return;
675
676 version++;
677
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200678 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
679
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200680 /*
681 * The guest calculates current wall clock time by adding
682 * system time (updated by kvm_write_guest_time below) to the
683 * wall clock specified here. guest system time equals host
684 * system time for us, thus we must fill in host boot time here.
685 */
Jason Wang923de3c2010-01-27 19:13:49 +0800686 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200687
688 wc.sec = boot.tv_sec;
689 wc.nsec = boot.tv_nsec;
690 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200691
692 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
693
694 version++;
695 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200696}
697
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200698static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
699{
700 uint32_t quotient, remainder;
701
702 /* Don't try to replace with do_div(), this one calculates
703 * "(dividend << 32) / divisor" */
704 __asm__ ( "divl %4"
705 : "=a" (quotient), "=d" (remainder)
706 : "0" (0), "1" (dividend), "r" (divisor) );
707 return quotient;
708}
709
710static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
711{
712 uint64_t nsecs = 1000000000LL;
713 int32_t shift = 0;
714 uint64_t tps64;
715 uint32_t tps32;
716
717 tps64 = tsc_khz * 1000LL;
718 while (tps64 > nsecs*2) {
719 tps64 >>= 1;
720 shift--;
721 }
722
723 tps32 = (uint32_t)tps64;
724 while (tps32 <= (uint32_t)nsecs) {
725 tps32 <<= 1;
726 shift++;
727 }
728
729 hv_clock->tsc_shift = shift;
730 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
731
732 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700733 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200734 hv_clock->tsc_to_system_mul);
735}
736
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100737static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
738
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200739static void kvm_write_guest_time(struct kvm_vcpu *v)
740{
741 struct timespec ts;
742 unsigned long flags;
743 struct kvm_vcpu_arch *vcpu = &v->arch;
744 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300745 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200746
747 if ((!vcpu->time_page))
748 return;
749
Avi Kivity463656c2009-04-12 15:49:07 +0300750 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
751 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
752 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
753 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200754 }
Avi Kivity463656c2009-04-12 15:49:07 +0300755 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200756
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200757 /* Keep irq disabled to prevent changes to the clock */
758 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530759 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200760 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800761 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200762 local_irq_restore(flags);
763
764 /* With all the info we got, fill in the values */
765
766 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400767 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
768
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200769 /*
770 * The interface expects us to write an even number signaling that the
771 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200772 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200773 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200774 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200775
776 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
777
778 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200779 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200780
781 kunmap_atomic(shared_kaddr, KM_USER0);
782
783 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
784}
785
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100786static int kvm_request_guest_time_update(struct kvm_vcpu *v)
787{
788 struct kvm_vcpu_arch *vcpu = &v->arch;
789
790 if (!vcpu->time_page)
791 return 0;
792 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
793 return 1;
794}
795
Avi Kivity9ba075a2008-05-26 20:06:35 +0300796static bool msr_mtrr_valid(unsigned msr)
797{
798 switch (msr) {
799 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
800 case MSR_MTRRfix64K_00000:
801 case MSR_MTRRfix16K_80000:
802 case MSR_MTRRfix16K_A0000:
803 case MSR_MTRRfix4K_C0000:
804 case MSR_MTRRfix4K_C8000:
805 case MSR_MTRRfix4K_D0000:
806 case MSR_MTRRfix4K_D8000:
807 case MSR_MTRRfix4K_E0000:
808 case MSR_MTRRfix4K_E8000:
809 case MSR_MTRRfix4K_F0000:
810 case MSR_MTRRfix4K_F8000:
811 case MSR_MTRRdefType:
812 case MSR_IA32_CR_PAT:
813 return true;
814 case 0x2f8:
815 return true;
816 }
817 return false;
818}
819
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300820static bool valid_pat_type(unsigned t)
821{
822 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
823}
824
825static bool valid_mtrr_type(unsigned t)
826{
827 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
828}
829
830static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
831{
832 int i;
833
834 if (!msr_mtrr_valid(msr))
835 return false;
836
837 if (msr == MSR_IA32_CR_PAT) {
838 for (i = 0; i < 8; i++)
839 if (!valid_pat_type((data >> (i * 8)) & 0xff))
840 return false;
841 return true;
842 } else if (msr == MSR_MTRRdefType) {
843 if (data & ~0xcff)
844 return false;
845 return valid_mtrr_type(data & 0xff);
846 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
847 for (i = 0; i < 8 ; i++)
848 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
849 return false;
850 return true;
851 }
852
853 /* variable MTRRs */
854 return valid_mtrr_type(data & 0xff);
855}
856
Avi Kivity9ba075a2008-05-26 20:06:35 +0300857static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
858{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800859 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
860
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300861 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300862 return 1;
863
Sheng Yang0bed3b52008-10-09 16:01:54 +0800864 if (msr == MSR_MTRRdefType) {
865 vcpu->arch.mtrr_state.def_type = data;
866 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
867 } else if (msr == MSR_MTRRfix64K_00000)
868 p[0] = data;
869 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
870 p[1 + msr - MSR_MTRRfix16K_80000] = data;
871 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
872 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
873 else if (msr == MSR_IA32_CR_PAT)
874 vcpu->arch.pat = data;
875 else { /* Variable MTRRs */
876 int idx, is_mtrr_mask;
877 u64 *pt;
878
879 idx = (msr - 0x200) / 2;
880 is_mtrr_mask = msr - 0x200 - 2 * idx;
881 if (!is_mtrr_mask)
882 pt =
883 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
884 else
885 pt =
886 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
887 *pt = data;
888 }
889
890 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300891 return 0;
892}
Carsten Otte15c4a642007-10-30 18:44:17 +0100893
Huang Ying890ca9a2009-05-11 16:48:15 +0800894static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
895{
896 u64 mcg_cap = vcpu->arch.mcg_cap;
897 unsigned bank_num = mcg_cap & 0xff;
898
899 switch (msr) {
900 case MSR_IA32_MCG_STATUS:
901 vcpu->arch.mcg_status = data;
902 break;
903 case MSR_IA32_MCG_CTL:
904 if (!(mcg_cap & MCG_CTL_P))
905 return 1;
906 if (data != 0 && data != ~(u64)0)
907 return -1;
908 vcpu->arch.mcg_ctl = data;
909 break;
910 default:
911 if (msr >= MSR_IA32_MC0_CTL &&
912 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
913 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +0100914 /* only 0 or all 1s can be written to IA32_MCi_CTL
915 * some Linux kernels though clear bit 10 in bank 4 to
916 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
917 * this to avoid an uncatched #GP in the guest
918 */
Huang Ying890ca9a2009-05-11 16:48:15 +0800919 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +0100920 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +0800921 return -1;
922 vcpu->arch.mce_banks[offset] = data;
923 break;
924 }
925 return 1;
926 }
927 return 0;
928}
929
Ed Swierkffde22a2009-10-15 15:21:43 -0700930static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
931{
932 struct kvm *kvm = vcpu->kvm;
933 int lm = is_long_mode(vcpu);
934 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
935 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
936 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
937 : kvm->arch.xen_hvm_config.blob_size_32;
938 u32 page_num = data & ~PAGE_MASK;
939 u64 page_addr = data & PAGE_MASK;
940 u8 *page;
941 int r;
942
943 r = -E2BIG;
944 if (page_num >= blob_size)
945 goto out;
946 r = -ENOMEM;
947 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
948 if (!page)
949 goto out;
950 r = -EFAULT;
951 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
952 goto out_free;
953 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
954 goto out_free;
955 r = 0;
956out_free:
957 kfree(page);
958out:
959 return r;
960}
961
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200962static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
963{
964 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
965}
966
967static bool kvm_hv_msr_partition_wide(u32 msr)
968{
969 bool r = false;
970 switch (msr) {
971 case HV_X64_MSR_GUEST_OS_ID:
972 case HV_X64_MSR_HYPERCALL:
973 r = true;
974 break;
975 }
976
977 return r;
978}
979
980static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
981{
982 struct kvm *kvm = vcpu->kvm;
983
984 switch (msr) {
985 case HV_X64_MSR_GUEST_OS_ID:
986 kvm->arch.hv_guest_os_id = data;
987 /* setting guest os id to zero disables hypercall page */
988 if (!kvm->arch.hv_guest_os_id)
989 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
990 break;
991 case HV_X64_MSR_HYPERCALL: {
992 u64 gfn;
993 unsigned long addr;
994 u8 instructions[4];
995
996 /* if guest os id is not set hypercall should remain disabled */
997 if (!kvm->arch.hv_guest_os_id)
998 break;
999 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1000 kvm->arch.hv_hypercall = data;
1001 break;
1002 }
1003 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1004 addr = gfn_to_hva(kvm, gfn);
1005 if (kvm_is_error_hva(addr))
1006 return 1;
1007 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1008 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1009 if (copy_to_user((void __user *)addr, instructions, 4))
1010 return 1;
1011 kvm->arch.hv_hypercall = data;
1012 break;
1013 }
1014 default:
1015 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1016 "data 0x%llx\n", msr, data);
1017 return 1;
1018 }
1019 return 0;
1020}
1021
1022static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1023{
Gleb Natapov10388a02010-01-17 15:51:23 +02001024 switch (msr) {
1025 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1026 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001027
Gleb Natapov10388a02010-01-17 15:51:23 +02001028 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1029 vcpu->arch.hv_vapic = data;
1030 break;
1031 }
1032 addr = gfn_to_hva(vcpu->kvm, data >>
1033 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1034 if (kvm_is_error_hva(addr))
1035 return 1;
1036 if (clear_user((void __user *)addr, PAGE_SIZE))
1037 return 1;
1038 vcpu->arch.hv_vapic = data;
1039 break;
1040 }
1041 case HV_X64_MSR_EOI:
1042 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1043 case HV_X64_MSR_ICR:
1044 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1045 case HV_X64_MSR_TPR:
1046 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1047 default:
1048 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1049 "data 0x%llx\n", msr, data);
1050 return 1;
1051 }
1052
1053 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001054}
1055
Carsten Otte15c4a642007-10-30 18:44:17 +01001056int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1057{
1058 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001059 case MSR_EFER:
1060 set_efer(vcpu, data);
1061 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +02001062 case MSR_K7_HWCR:
1063 data &= ~(u64)0x40; /* ignore flush filter disable */
Joerg Roedel82494022010-02-24 18:59:16 +01001064 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02001065 if (data != 0) {
1066 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1067 data);
1068 return 1;
1069 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001070 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001071 case MSR_FAM10H_MMIO_CONF_BASE:
1072 if (data != 0) {
1073 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1074 "0x%llx\n", data);
1075 return 1;
1076 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001077 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001078 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001079 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001080 case MSR_IA32_DEBUGCTLMSR:
1081 if (!data) {
1082 /* We support the non-activated case already */
1083 break;
1084 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1085 /* Values other than LBR and BTF are vendor-specific,
1086 thus reserved and should throw a #GP */
1087 return 1;
1088 }
1089 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1090 __func__, data);
1091 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001092 case MSR_IA32_UCODE_REV:
1093 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001094 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001095 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001096 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001097 case 0x200 ... 0x2ff:
1098 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001099 case MSR_IA32_APICBASE:
1100 kvm_set_apic_base(vcpu, data);
1101 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001102 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1103 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001104 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001105 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001106 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001107 case MSR_KVM_WALL_CLOCK:
1108 vcpu->kvm->arch.wall_clock = data;
1109 kvm_write_wall_clock(vcpu->kvm, data);
1110 break;
1111 case MSR_KVM_SYSTEM_TIME: {
1112 if (vcpu->arch.time_page) {
1113 kvm_release_page_dirty(vcpu->arch.time_page);
1114 vcpu->arch.time_page = NULL;
1115 }
1116
1117 vcpu->arch.time = data;
1118
1119 /* we verify if the enable bit is set... */
1120 if (!(data & 1))
1121 break;
1122
1123 /* ...but clean it before doing the actual write */
1124 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1125
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001126 vcpu->arch.time_page =
1127 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001128
1129 if (is_error_page(vcpu->arch.time_page)) {
1130 kvm_release_page_clean(vcpu->arch.time_page);
1131 vcpu->arch.time_page = NULL;
1132 }
1133
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001134 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001135 break;
1136 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001137 case MSR_IA32_MCG_CTL:
1138 case MSR_IA32_MCG_STATUS:
1139 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1140 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001141
1142 /* Performance counters are not protected by a CPUID bit,
1143 * so we should check all of them in the generic path for the sake of
1144 * cross vendor migration.
1145 * Writing a zero into the event select MSRs disables them,
1146 * which we perfectly emulate ;-). Any other value should be at least
1147 * reported, some guests depend on them.
1148 */
1149 case MSR_P6_EVNTSEL0:
1150 case MSR_P6_EVNTSEL1:
1151 case MSR_K7_EVNTSEL0:
1152 case MSR_K7_EVNTSEL1:
1153 case MSR_K7_EVNTSEL2:
1154 case MSR_K7_EVNTSEL3:
1155 if (data != 0)
1156 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1157 "0x%x data 0x%llx\n", msr, data);
1158 break;
1159 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1160 * so we ignore writes to make it happy.
1161 */
1162 case MSR_P6_PERFCTR0:
1163 case MSR_P6_PERFCTR1:
1164 case MSR_K7_PERFCTR0:
1165 case MSR_K7_PERFCTR1:
1166 case MSR_K7_PERFCTR2:
1167 case MSR_K7_PERFCTR3:
1168 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1169 "0x%x data 0x%llx\n", msr, data);
1170 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001171 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1172 if (kvm_hv_msr_partition_wide(msr)) {
1173 int r;
1174 mutex_lock(&vcpu->kvm->lock);
1175 r = set_msr_hyperv_pw(vcpu, msr, data);
1176 mutex_unlock(&vcpu->kvm->lock);
1177 return r;
1178 } else
1179 return set_msr_hyperv(vcpu, msr, data);
1180 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001181 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001182 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1183 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001184 if (!ignore_msrs) {
1185 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1186 msr, data);
1187 return 1;
1188 } else {
1189 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1190 msr, data);
1191 break;
1192 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001193 }
1194 return 0;
1195}
1196EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1197
1198
1199/*
1200 * Reads an msr value (of 'msr_index') into 'pdata'.
1201 * Returns 0 on success, non-0 otherwise.
1202 * Assumes vcpu_load() was already called.
1203 */
1204int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1205{
1206 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1207}
1208
Avi Kivity9ba075a2008-05-26 20:06:35 +03001209static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1210{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001211 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1212
Avi Kivity9ba075a2008-05-26 20:06:35 +03001213 if (!msr_mtrr_valid(msr))
1214 return 1;
1215
Sheng Yang0bed3b52008-10-09 16:01:54 +08001216 if (msr == MSR_MTRRdefType)
1217 *pdata = vcpu->arch.mtrr_state.def_type +
1218 (vcpu->arch.mtrr_state.enabled << 10);
1219 else if (msr == MSR_MTRRfix64K_00000)
1220 *pdata = p[0];
1221 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1222 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1223 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1224 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1225 else if (msr == MSR_IA32_CR_PAT)
1226 *pdata = vcpu->arch.pat;
1227 else { /* Variable MTRRs */
1228 int idx, is_mtrr_mask;
1229 u64 *pt;
1230
1231 idx = (msr - 0x200) / 2;
1232 is_mtrr_mask = msr - 0x200 - 2 * idx;
1233 if (!is_mtrr_mask)
1234 pt =
1235 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1236 else
1237 pt =
1238 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1239 *pdata = *pt;
1240 }
1241
Avi Kivity9ba075a2008-05-26 20:06:35 +03001242 return 0;
1243}
1244
Huang Ying890ca9a2009-05-11 16:48:15 +08001245static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1246{
1247 u64 data;
1248 u64 mcg_cap = vcpu->arch.mcg_cap;
1249 unsigned bank_num = mcg_cap & 0xff;
1250
1251 switch (msr) {
1252 case MSR_IA32_P5_MC_ADDR:
1253 case MSR_IA32_P5_MC_TYPE:
1254 data = 0;
1255 break;
1256 case MSR_IA32_MCG_CAP:
1257 data = vcpu->arch.mcg_cap;
1258 break;
1259 case MSR_IA32_MCG_CTL:
1260 if (!(mcg_cap & MCG_CTL_P))
1261 return 1;
1262 data = vcpu->arch.mcg_ctl;
1263 break;
1264 case MSR_IA32_MCG_STATUS:
1265 data = vcpu->arch.mcg_status;
1266 break;
1267 default:
1268 if (msr >= MSR_IA32_MC0_CTL &&
1269 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1270 u32 offset = msr - MSR_IA32_MC0_CTL;
1271 data = vcpu->arch.mce_banks[offset];
1272 break;
1273 }
1274 return 1;
1275 }
1276 *pdata = data;
1277 return 0;
1278}
1279
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001280static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1281{
1282 u64 data = 0;
1283 struct kvm *kvm = vcpu->kvm;
1284
1285 switch (msr) {
1286 case HV_X64_MSR_GUEST_OS_ID:
1287 data = kvm->arch.hv_guest_os_id;
1288 break;
1289 case HV_X64_MSR_HYPERCALL:
1290 data = kvm->arch.hv_hypercall;
1291 break;
1292 default:
1293 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1294 return 1;
1295 }
1296
1297 *pdata = data;
1298 return 0;
1299}
1300
1301static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1302{
1303 u64 data = 0;
1304
1305 switch (msr) {
1306 case HV_X64_MSR_VP_INDEX: {
1307 int r;
1308 struct kvm_vcpu *v;
1309 kvm_for_each_vcpu(r, v, vcpu->kvm)
1310 if (v == vcpu)
1311 data = r;
1312 break;
1313 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001314 case HV_X64_MSR_EOI:
1315 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1316 case HV_X64_MSR_ICR:
1317 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1318 case HV_X64_MSR_TPR:
1319 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001320 default:
1321 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1322 return 1;
1323 }
1324 *pdata = data;
1325 return 0;
1326}
1327
Carsten Otte15c4a642007-10-30 18:44:17 +01001328int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1329{
1330 u64 data;
1331
1332 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001333 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001334 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001335 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001336 case MSR_IA32_DEBUGCTLMSR:
1337 case MSR_IA32_LASTBRANCHFROMIP:
1338 case MSR_IA32_LASTBRANCHTOIP:
1339 case MSR_IA32_LASTINTFROMIP:
1340 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301341 case MSR_K8_SYSCFG:
1342 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001343 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301344 case MSR_P6_PERFCTR0:
1345 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301346 case MSR_P6_EVNTSEL0:
1347 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301348 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301349 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001350 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001351 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001352 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001353 data = 0;
1354 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001355 case MSR_MTRRcap:
1356 data = 0x500 | KVM_NR_VAR_MTRR;
1357 break;
1358 case 0x200 ... 0x2ff:
1359 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001360 case 0xcd: /* fsb frequency */
1361 data = 3;
1362 break;
1363 case MSR_IA32_APICBASE:
1364 data = kvm_get_apic_base(vcpu);
1365 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001366 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1367 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1368 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001369 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001370 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001371 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001372 case MSR_IA32_PERF_STATUS:
1373 /* TSC increment by tick */
1374 data = 1000ULL;
1375 /* CPU multiplier */
1376 data |= (((uint64_t)4ULL) << 40);
1377 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001378 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001379 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001380 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001381 case MSR_KVM_WALL_CLOCK:
1382 data = vcpu->kvm->arch.wall_clock;
1383 break;
1384 case MSR_KVM_SYSTEM_TIME:
1385 data = vcpu->arch.time;
1386 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001387 case MSR_IA32_P5_MC_ADDR:
1388 case MSR_IA32_P5_MC_TYPE:
1389 case MSR_IA32_MCG_CAP:
1390 case MSR_IA32_MCG_CTL:
1391 case MSR_IA32_MCG_STATUS:
1392 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1393 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001394 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1395 if (kvm_hv_msr_partition_wide(msr)) {
1396 int r;
1397 mutex_lock(&vcpu->kvm->lock);
1398 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1399 mutex_unlock(&vcpu->kvm->lock);
1400 return r;
1401 } else
1402 return get_msr_hyperv(vcpu, msr, pdata);
1403 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001404 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001405 if (!ignore_msrs) {
1406 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1407 return 1;
1408 } else {
1409 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1410 data = 0;
1411 }
1412 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001413 }
1414 *pdata = data;
1415 return 0;
1416}
1417EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1418
Carsten Otte313a3dc2007-10-11 19:16:52 +02001419/*
1420 * Read or write a bunch of msrs. All parameters are kernel addresses.
1421 *
1422 * @return number of msrs set successfully.
1423 */
1424static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1425 struct kvm_msr_entry *entries,
1426 int (*do_msr)(struct kvm_vcpu *vcpu,
1427 unsigned index, u64 *data))
1428{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001429 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001430
1431 vcpu_load(vcpu);
1432
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001433 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001434 for (i = 0; i < msrs->nmsrs; ++i)
1435 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1436 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001437 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001438
1439 vcpu_put(vcpu);
1440
1441 return i;
1442}
1443
1444/*
1445 * Read or write a bunch of msrs. Parameters are user addresses.
1446 *
1447 * @return number of msrs set successfully.
1448 */
1449static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1450 int (*do_msr)(struct kvm_vcpu *vcpu,
1451 unsigned index, u64 *data),
1452 int writeback)
1453{
1454 struct kvm_msrs msrs;
1455 struct kvm_msr_entry *entries;
1456 int r, n;
1457 unsigned size;
1458
1459 r = -EFAULT;
1460 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1461 goto out;
1462
1463 r = -E2BIG;
1464 if (msrs.nmsrs >= MAX_IO_MSRS)
1465 goto out;
1466
1467 r = -ENOMEM;
1468 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1469 entries = vmalloc(size);
1470 if (!entries)
1471 goto out;
1472
1473 r = -EFAULT;
1474 if (copy_from_user(entries, user_msrs->entries, size))
1475 goto out_free;
1476
1477 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1478 if (r < 0)
1479 goto out_free;
1480
1481 r = -EFAULT;
1482 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1483 goto out_free;
1484
1485 r = n;
1486
1487out_free:
1488 vfree(entries);
1489out:
1490 return r;
1491}
1492
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001493int kvm_dev_ioctl_check_extension(long ext)
1494{
1495 int r;
1496
1497 switch (ext) {
1498 case KVM_CAP_IRQCHIP:
1499 case KVM_CAP_HLT:
1500 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001501 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001502 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001503 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001504 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001505 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001506 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001507 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001508 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001509 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001510 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001511 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001512 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001513 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001514 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001515 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001516 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001517 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001518 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001519 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001520 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001521 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001522 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01001523 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001524 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001525 r = 1;
1526 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001527 case KVM_CAP_COALESCED_MMIO:
1528 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1529 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001530 case KVM_CAP_VAPIC:
1531 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1532 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001533 case KVM_CAP_NR_VCPUS:
1534 r = KVM_MAX_VCPUS;
1535 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001536 case KVM_CAP_NR_MEMSLOTS:
1537 r = KVM_MEMORY_SLOTS;
1538 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001539 case KVM_CAP_PV_MMU: /* obsolete */
1540 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001541 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001542 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001543 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001544 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001545 case KVM_CAP_MCE:
1546 r = KVM_MAX_MCE_BANKS;
1547 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001548 default:
1549 r = 0;
1550 break;
1551 }
1552 return r;
1553
1554}
1555
Carsten Otte043405e2007-10-10 17:16:19 +02001556long kvm_arch_dev_ioctl(struct file *filp,
1557 unsigned int ioctl, unsigned long arg)
1558{
1559 void __user *argp = (void __user *)arg;
1560 long r;
1561
1562 switch (ioctl) {
1563 case KVM_GET_MSR_INDEX_LIST: {
1564 struct kvm_msr_list __user *user_msr_list = argp;
1565 struct kvm_msr_list msr_list;
1566 unsigned n;
1567
1568 r = -EFAULT;
1569 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1570 goto out;
1571 n = msr_list.nmsrs;
1572 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1573 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1574 goto out;
1575 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001576 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001577 goto out;
1578 r = -EFAULT;
1579 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1580 num_msrs_to_save * sizeof(u32)))
1581 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001582 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001583 &emulated_msrs,
1584 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1585 goto out;
1586 r = 0;
1587 break;
1588 }
Avi Kivity674eea02008-02-11 18:37:23 +02001589 case KVM_GET_SUPPORTED_CPUID: {
1590 struct kvm_cpuid2 __user *cpuid_arg = argp;
1591 struct kvm_cpuid2 cpuid;
1592
1593 r = -EFAULT;
1594 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1595 goto out;
1596 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001597 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001598 if (r)
1599 goto out;
1600
1601 r = -EFAULT;
1602 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1603 goto out;
1604 r = 0;
1605 break;
1606 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001607 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1608 u64 mce_cap;
1609
1610 mce_cap = KVM_MCE_CAP_SUPPORTED;
1611 r = -EFAULT;
1612 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1613 goto out;
1614 r = 0;
1615 break;
1616 }
Carsten Otte043405e2007-10-10 17:16:19 +02001617 default:
1618 r = -EINVAL;
1619 }
1620out:
1621 return r;
1622}
1623
Carsten Otte313a3dc2007-10-11 19:16:52 +02001624void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1625{
1626 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001627 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1628 unsigned long khz = cpufreq_quick_get(cpu);
1629 if (!khz)
1630 khz = tsc_khz;
1631 per_cpu(cpu_tsc_khz, cpu) = khz;
1632 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001633 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001634}
1635
1636void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1637{
Amit Shah9327fd12007-11-15 18:38:46 +02001638 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001639 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001640}
1641
Dan Kenigsberg07716712007-11-21 17:10:04 +02001642static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001643{
Avi Kivitye286e862009-05-03 18:50:55 +03001644 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001645
Avi Kivitye286e862009-05-03 18:50:55 +03001646 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001647 return efer & EFER_NX;
1648}
1649
1650static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1651{
1652 int i;
1653 struct kvm_cpuid_entry2 *e, *entry;
1654
Carsten Otte313a3dc2007-10-11 19:16:52 +02001655 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001656 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1657 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001658 if (e->function == 0x80000001) {
1659 entry = e;
1660 break;
1661 }
1662 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001663 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001664 entry->edx &= ~(1 << 20);
1665 printk(KERN_INFO "kvm: guest NX capability removed\n");
1666 }
1667}
1668
Dan Kenigsberg07716712007-11-21 17:10:04 +02001669/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001670static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1671 struct kvm_cpuid *cpuid,
1672 struct kvm_cpuid_entry __user *entries)
1673{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001674 int r, i;
1675 struct kvm_cpuid_entry *cpuid_entries;
1676
1677 r = -E2BIG;
1678 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1679 goto out;
1680 r = -ENOMEM;
1681 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1682 if (!cpuid_entries)
1683 goto out;
1684 r = -EFAULT;
1685 if (copy_from_user(cpuid_entries, entries,
1686 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1687 goto out_free;
1688 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001689 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1690 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1691 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1692 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1693 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1694 vcpu->arch.cpuid_entries[i].index = 0;
1695 vcpu->arch.cpuid_entries[i].flags = 0;
1696 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1697 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1698 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001699 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001700 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001701 cpuid_fix_nx_cap(vcpu);
1702 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001703 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001704 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001705
1706out_free:
1707 vfree(cpuid_entries);
1708out:
1709 return r;
1710}
1711
1712static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001713 struct kvm_cpuid2 *cpuid,
1714 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001715{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001716 int r;
1717
1718 r = -E2BIG;
1719 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1720 goto out;
1721 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001722 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001723 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001724 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001725 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001726 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001727 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001728 return 0;
1729
1730out:
1731 return r;
1732}
1733
Dan Kenigsberg07716712007-11-21 17:10:04 +02001734static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001735 struct kvm_cpuid2 *cpuid,
1736 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001737{
1738 int r;
1739
1740 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001741 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001742 goto out;
1743 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001744 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001745 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001746 goto out;
1747 return 0;
1748
1749out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001750 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001751 return r;
1752}
1753
Dan Kenigsberg07716712007-11-21 17:10:04 +02001754static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001755 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001756{
1757 entry->function = function;
1758 entry->index = index;
1759 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001760 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001761 entry->flags = 0;
1762}
1763
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001764#define F(x) bit(X86_FEATURE_##x)
1765
Dan Kenigsberg07716712007-11-21 17:10:04 +02001766static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1767 u32 index, int *nent, int maxnent)
1768{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001769 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001770#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001771 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1772 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001773 unsigned f_lm = F(LM);
1774#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001775 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001776 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001777#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001778 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001779
1780 /* cpuid 1.edx */
1781 const u32 kvm_supported_word0_x86_features =
1782 F(FPU) | F(VME) | F(DE) | F(PSE) |
1783 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1784 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1785 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1786 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1787 0 /* Reserved, DS, ACPI */ | F(MMX) |
1788 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1789 0 /* HTT, TM, Reserved, PBE */;
1790 /* cpuid 0x80000001.edx */
1791 const u32 kvm_supported_word1_x86_features =
1792 F(FPU) | F(VME) | F(DE) | F(PSE) |
1793 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1794 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1795 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1796 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1797 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001798 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001799 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1800 /* cpuid 1.ecx */
1801 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001802 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1803 0 /* DS-CPL, VMX, SMX, EST */ |
1804 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1805 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1806 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001807 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001808 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001809 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001810 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001811 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1812 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1813 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1814 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001815
Amit Shah19355472009-01-14 16:56:00 +00001816 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001817 get_cpu();
1818 do_cpuid_1_ent(entry, function, index);
1819 ++*nent;
1820
1821 switch (function) {
1822 case 0:
1823 entry->eax = min(entry->eax, (u32)0xb);
1824 break;
1825 case 1:
1826 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001827 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001828 /* we support x2apic emulation even if host does not support
1829 * it since we emulate x2apic in software */
1830 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001831 break;
1832 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1833 * may return different values. This forces us to get_cpu() before
1834 * issuing the first command, and also to emulate this annoying behavior
1835 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1836 case 2: {
1837 int t, times = entry->eax & 0xff;
1838
1839 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001840 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001841 for (t = 1; t < times && *nent < maxnent; ++t) {
1842 do_cpuid_1_ent(&entry[t], function, 0);
1843 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1844 ++*nent;
1845 }
1846 break;
1847 }
1848 /* function 4 and 0xb have additional index. */
1849 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001850 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001851
1852 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1853 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001854 for (i = 1; *nent < maxnent; ++i) {
1855 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001856 if (!cache_type)
1857 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001858 do_cpuid_1_ent(&entry[i], function, i);
1859 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001860 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1861 ++*nent;
1862 }
1863 break;
1864 }
1865 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001866 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001867
1868 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1869 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001870 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001871 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001872 if (!level_type)
1873 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001874 do_cpuid_1_ent(&entry[i], function, i);
1875 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001876 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1877 ++*nent;
1878 }
1879 break;
1880 }
1881 case 0x80000000:
1882 entry->eax = min(entry->eax, 0x8000001a);
1883 break;
1884 case 0x80000001:
1885 entry->edx &= kvm_supported_word1_x86_features;
1886 entry->ecx &= kvm_supported_word6_x86_features;
1887 break;
1888 }
1889 put_cpu();
1890}
1891
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001892#undef F
1893
Avi Kivity674eea02008-02-11 18:37:23 +02001894static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001895 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001896{
1897 struct kvm_cpuid_entry2 *cpuid_entries;
1898 int limit, nent = 0, r = -E2BIG;
1899 u32 func;
1900
1901 if (cpuid->nent < 1)
1902 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001903 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1904 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001905 r = -ENOMEM;
1906 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1907 if (!cpuid_entries)
1908 goto out;
1909
1910 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1911 limit = cpuid_entries[0].eax;
1912 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1913 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001914 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001915 r = -E2BIG;
1916 if (nent >= cpuid->nent)
1917 goto out_free;
1918
1919 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1920 limit = cpuid_entries[nent - 1].eax;
1921 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1922 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001923 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001924 r = -E2BIG;
1925 if (nent >= cpuid->nent)
1926 goto out_free;
1927
Dan Kenigsberg07716712007-11-21 17:10:04 +02001928 r = -EFAULT;
1929 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001930 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001931 goto out_free;
1932 cpuid->nent = nent;
1933 r = 0;
1934
1935out_free:
1936 vfree(cpuid_entries);
1937out:
1938 return r;
1939}
1940
Carsten Otte313a3dc2007-10-11 19:16:52 +02001941static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1942 struct kvm_lapic_state *s)
1943{
1944 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001945 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001946 vcpu_put(vcpu);
1947
1948 return 0;
1949}
1950
1951static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1952 struct kvm_lapic_state *s)
1953{
1954 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001955 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001956 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03001957 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001958 vcpu_put(vcpu);
1959
1960 return 0;
1961}
1962
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001963static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1964 struct kvm_interrupt *irq)
1965{
1966 if (irq->irq < 0 || irq->irq >= 256)
1967 return -EINVAL;
1968 if (irqchip_in_kernel(vcpu->kvm))
1969 return -ENXIO;
1970 vcpu_load(vcpu);
1971
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001972 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001973
1974 vcpu_put(vcpu);
1975
1976 return 0;
1977}
1978
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001979static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1980{
1981 vcpu_load(vcpu);
1982 kvm_inject_nmi(vcpu);
1983 vcpu_put(vcpu);
1984
1985 return 0;
1986}
1987
Avi Kivityb209749f2007-10-22 16:50:39 +02001988static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1989 struct kvm_tpr_access_ctl *tac)
1990{
1991 if (tac->flags)
1992 return -EINVAL;
1993 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1994 return 0;
1995}
1996
Huang Ying890ca9a2009-05-11 16:48:15 +08001997static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1998 u64 mcg_cap)
1999{
2000 int r;
2001 unsigned bank_num = mcg_cap & 0xff, bank;
2002
2003 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002004 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002005 goto out;
2006 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2007 goto out;
2008 r = 0;
2009 vcpu->arch.mcg_cap = mcg_cap;
2010 /* Init IA32_MCG_CTL to all 1s */
2011 if (mcg_cap & MCG_CTL_P)
2012 vcpu->arch.mcg_ctl = ~(u64)0;
2013 /* Init IA32_MCi_CTL to all 1s */
2014 for (bank = 0; bank < bank_num; bank++)
2015 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2016out:
2017 return r;
2018}
2019
2020static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2021 struct kvm_x86_mce *mce)
2022{
2023 u64 mcg_cap = vcpu->arch.mcg_cap;
2024 unsigned bank_num = mcg_cap & 0xff;
2025 u64 *banks = vcpu->arch.mce_banks;
2026
2027 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2028 return -EINVAL;
2029 /*
2030 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2031 * reporting is disabled
2032 */
2033 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2034 vcpu->arch.mcg_ctl != ~(u64)0)
2035 return 0;
2036 banks += 4 * mce->bank;
2037 /*
2038 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2039 * reporting is disabled for the bank
2040 */
2041 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2042 return 0;
2043 if (mce->status & MCI_STATUS_UC) {
2044 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002045 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002046 printk(KERN_DEBUG "kvm: set_mce: "
2047 "injects mce exception while "
2048 "previous one is in progress!\n");
2049 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2050 return 0;
2051 }
2052 if (banks[1] & MCI_STATUS_VAL)
2053 mce->status |= MCI_STATUS_OVER;
2054 banks[2] = mce->addr;
2055 banks[3] = mce->misc;
2056 vcpu->arch.mcg_status = mce->mcg_status;
2057 banks[1] = mce->status;
2058 kvm_queue_exception(vcpu, MC_VECTOR);
2059 } else if (!(banks[1] & MCI_STATUS_VAL)
2060 || !(banks[1] & MCI_STATUS_UC)) {
2061 if (banks[1] & MCI_STATUS_VAL)
2062 mce->status |= MCI_STATUS_OVER;
2063 banks[2] = mce->addr;
2064 banks[3] = mce->misc;
2065 banks[1] = mce->status;
2066 } else
2067 banks[1] |= MCI_STATUS_OVER;
2068 return 0;
2069}
2070
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002071static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2072 struct kvm_vcpu_events *events)
2073{
2074 vcpu_load(vcpu);
2075
Jan Kiszka03b82a32010-02-15 10:45:41 +01002076 events->exception.injected =
2077 vcpu->arch.exception.pending &&
2078 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002079 events->exception.nr = vcpu->arch.exception.nr;
2080 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2081 events->exception.error_code = vcpu->arch.exception.error_code;
2082
Jan Kiszka03b82a32010-02-15 10:45:41 +01002083 events->interrupt.injected =
2084 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002085 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002086 events->interrupt.soft = 0;
Jan Kiszka48005f62010-02-19 19:38:07 +01002087 events->interrupt.shadow =
2088 kvm_x86_ops->get_interrupt_shadow(vcpu,
2089 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002090
2091 events->nmi.injected = vcpu->arch.nmi_injected;
2092 events->nmi.pending = vcpu->arch.nmi_pending;
2093 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2094
2095 events->sipi_vector = vcpu->arch.sipi_vector;
2096
Jan Kiszkadab4b912009-12-06 18:24:15 +01002097 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002098 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2099 | KVM_VCPUEVENT_VALID_SHADOW);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002100
2101 vcpu_put(vcpu);
2102}
2103
2104static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2105 struct kvm_vcpu_events *events)
2106{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002107 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002108 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2109 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002110 return -EINVAL;
2111
2112 vcpu_load(vcpu);
2113
2114 vcpu->arch.exception.pending = events->exception.injected;
2115 vcpu->arch.exception.nr = events->exception.nr;
2116 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2117 vcpu->arch.exception.error_code = events->exception.error_code;
2118
2119 vcpu->arch.interrupt.pending = events->interrupt.injected;
2120 vcpu->arch.interrupt.nr = events->interrupt.nr;
2121 vcpu->arch.interrupt.soft = events->interrupt.soft;
2122 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2123 kvm_pic_clear_isr_ack(vcpu->kvm);
Jan Kiszka48005f62010-02-19 19:38:07 +01002124 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2125 kvm_x86_ops->set_interrupt_shadow(vcpu,
2126 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002127
2128 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002129 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2130 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002131 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2132
Jan Kiszkadab4b912009-12-06 18:24:15 +01002133 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2134 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002135
2136 vcpu_put(vcpu);
2137
2138 return 0;
2139}
2140
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002141static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2142 struct kvm_debugregs *dbgregs)
2143{
2144 vcpu_load(vcpu);
2145
2146 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2147 dbgregs->dr6 = vcpu->arch.dr6;
2148 dbgregs->dr7 = vcpu->arch.dr7;
2149 dbgregs->flags = 0;
2150
2151 vcpu_put(vcpu);
2152}
2153
2154static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2155 struct kvm_debugregs *dbgregs)
2156{
2157 if (dbgregs->flags)
2158 return -EINVAL;
2159
2160 vcpu_load(vcpu);
2161
2162 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2163 vcpu->arch.dr6 = dbgregs->dr6;
2164 vcpu->arch.dr7 = dbgregs->dr7;
2165
2166 vcpu_put(vcpu);
2167
2168 return 0;
2169}
2170
Carsten Otte313a3dc2007-10-11 19:16:52 +02002171long kvm_arch_vcpu_ioctl(struct file *filp,
2172 unsigned int ioctl, unsigned long arg)
2173{
2174 struct kvm_vcpu *vcpu = filp->private_data;
2175 void __user *argp = (void __user *)arg;
2176 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002177 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002178
2179 switch (ioctl) {
2180 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002181 r = -EINVAL;
2182 if (!vcpu->arch.apic)
2183 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002184 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002185
Dave Hansenb772ff32008-08-11 10:01:47 -07002186 r = -ENOMEM;
2187 if (!lapic)
2188 goto out;
2189 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002190 if (r)
2191 goto out;
2192 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002193 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002194 goto out;
2195 r = 0;
2196 break;
2197 }
2198 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002199 r = -EINVAL;
2200 if (!vcpu->arch.apic)
2201 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002202 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2203 r = -ENOMEM;
2204 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002205 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002206 r = -EFAULT;
2207 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2208 goto out;
2209 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002210 if (r)
2211 goto out;
2212 r = 0;
2213 break;
2214 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002215 case KVM_INTERRUPT: {
2216 struct kvm_interrupt irq;
2217
2218 r = -EFAULT;
2219 if (copy_from_user(&irq, argp, sizeof irq))
2220 goto out;
2221 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2222 if (r)
2223 goto out;
2224 r = 0;
2225 break;
2226 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002227 case KVM_NMI: {
2228 r = kvm_vcpu_ioctl_nmi(vcpu);
2229 if (r)
2230 goto out;
2231 r = 0;
2232 break;
2233 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002234 case KVM_SET_CPUID: {
2235 struct kvm_cpuid __user *cpuid_arg = argp;
2236 struct kvm_cpuid cpuid;
2237
2238 r = -EFAULT;
2239 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2240 goto out;
2241 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2242 if (r)
2243 goto out;
2244 break;
2245 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002246 case KVM_SET_CPUID2: {
2247 struct kvm_cpuid2 __user *cpuid_arg = argp;
2248 struct kvm_cpuid2 cpuid;
2249
2250 r = -EFAULT;
2251 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2252 goto out;
2253 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002254 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002255 if (r)
2256 goto out;
2257 break;
2258 }
2259 case KVM_GET_CPUID2: {
2260 struct kvm_cpuid2 __user *cpuid_arg = argp;
2261 struct kvm_cpuid2 cpuid;
2262
2263 r = -EFAULT;
2264 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2265 goto out;
2266 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002267 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002268 if (r)
2269 goto out;
2270 r = -EFAULT;
2271 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2272 goto out;
2273 r = 0;
2274 break;
2275 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002276 case KVM_GET_MSRS:
2277 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2278 break;
2279 case KVM_SET_MSRS:
2280 r = msr_io(vcpu, argp, do_set_msr, 0);
2281 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002282 case KVM_TPR_ACCESS_REPORTING: {
2283 struct kvm_tpr_access_ctl tac;
2284
2285 r = -EFAULT;
2286 if (copy_from_user(&tac, argp, sizeof tac))
2287 goto out;
2288 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2289 if (r)
2290 goto out;
2291 r = -EFAULT;
2292 if (copy_to_user(argp, &tac, sizeof tac))
2293 goto out;
2294 r = 0;
2295 break;
2296 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002297 case KVM_SET_VAPIC_ADDR: {
2298 struct kvm_vapic_addr va;
2299
2300 r = -EINVAL;
2301 if (!irqchip_in_kernel(vcpu->kvm))
2302 goto out;
2303 r = -EFAULT;
2304 if (copy_from_user(&va, argp, sizeof va))
2305 goto out;
2306 r = 0;
2307 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2308 break;
2309 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002310 case KVM_X86_SETUP_MCE: {
2311 u64 mcg_cap;
2312
2313 r = -EFAULT;
2314 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2315 goto out;
2316 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2317 break;
2318 }
2319 case KVM_X86_SET_MCE: {
2320 struct kvm_x86_mce mce;
2321
2322 r = -EFAULT;
2323 if (copy_from_user(&mce, argp, sizeof mce))
2324 goto out;
2325 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2326 break;
2327 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002328 case KVM_GET_VCPU_EVENTS: {
2329 struct kvm_vcpu_events events;
2330
2331 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2332
2333 r = -EFAULT;
2334 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2335 break;
2336 r = 0;
2337 break;
2338 }
2339 case KVM_SET_VCPU_EVENTS: {
2340 struct kvm_vcpu_events events;
2341
2342 r = -EFAULT;
2343 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2344 break;
2345
2346 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2347 break;
2348 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002349 case KVM_GET_DEBUGREGS: {
2350 struct kvm_debugregs dbgregs;
2351
2352 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2353
2354 r = -EFAULT;
2355 if (copy_to_user(argp, &dbgregs,
2356 sizeof(struct kvm_debugregs)))
2357 break;
2358 r = 0;
2359 break;
2360 }
2361 case KVM_SET_DEBUGREGS: {
2362 struct kvm_debugregs dbgregs;
2363
2364 r = -EFAULT;
2365 if (copy_from_user(&dbgregs, argp,
2366 sizeof(struct kvm_debugregs)))
2367 break;
2368
2369 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2370 break;
2371 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002372 default:
2373 r = -EINVAL;
2374 }
2375out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002376 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002377 return r;
2378}
2379
Carsten Otte1fe779f2007-10-29 16:08:35 +01002380static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2381{
2382 int ret;
2383
2384 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2385 return -1;
2386 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2387 return ret;
2388}
2389
Sheng Yangb927a3c2009-07-21 10:42:48 +08002390static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2391 u64 ident_addr)
2392{
2393 kvm->arch.ept_identity_map_addr = ident_addr;
2394 return 0;
2395}
2396
Carsten Otte1fe779f2007-10-29 16:08:35 +01002397static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2398 u32 kvm_nr_mmu_pages)
2399{
2400 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2401 return -EINVAL;
2402
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002403 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002404 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002405
2406 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002407 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002408
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002409 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002410 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002411 return 0;
2412}
2413
2414static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2415{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002416 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002417}
2418
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002419gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2420{
2421 int i;
2422 struct kvm_mem_alias *alias;
2423 struct kvm_mem_aliases *aliases;
2424
2425 aliases = rcu_dereference(kvm->arch.aliases);
2426
2427 for (i = 0; i < aliases->naliases; ++i) {
2428 alias = &aliases->aliases[i];
2429 if (alias->flags & KVM_ALIAS_INVALID)
2430 continue;
2431 if (gfn >= alias->base_gfn
2432 && gfn < alias->base_gfn + alias->npages)
2433 return alias->target_gfn + gfn - alias->base_gfn;
2434 }
2435 return gfn;
2436}
2437
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002438gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2439{
2440 int i;
2441 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002442 struct kvm_mem_aliases *aliases;
2443
2444 aliases = rcu_dereference(kvm->arch.aliases);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002445
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002446 for (i = 0; i < aliases->naliases; ++i) {
2447 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002448 if (gfn >= alias->base_gfn
2449 && gfn < alias->base_gfn + alias->npages)
2450 return alias->target_gfn + gfn - alias->base_gfn;
2451 }
2452 return gfn;
2453}
2454
Carsten Otte1fe779f2007-10-29 16:08:35 +01002455/*
2456 * Set a new alias region. Aliases map a portion of physical memory into
2457 * another portion. This is useful for memory windows, for example the PC
2458 * VGA region.
2459 */
2460static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2461 struct kvm_memory_alias *alias)
2462{
2463 int r, n;
2464 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002465 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002466
2467 r = -EINVAL;
2468 /* General sanity checks */
2469 if (alias->memory_size & (PAGE_SIZE - 1))
2470 goto out;
2471 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2472 goto out;
2473 if (alias->slot >= KVM_ALIAS_SLOTS)
2474 goto out;
2475 if (alias->guest_phys_addr + alias->memory_size
2476 < alias->guest_phys_addr)
2477 goto out;
2478 if (alias->target_phys_addr + alias->memory_size
2479 < alias->target_phys_addr)
2480 goto out;
2481
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002482 r = -ENOMEM;
2483 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2484 if (!aliases)
2485 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002486
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002487 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002488
2489 /* invalidate any gfn reference in case of deletion/shrinking */
2490 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2491 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2492 old_aliases = kvm->arch.aliases;
2493 rcu_assign_pointer(kvm->arch.aliases, aliases);
2494 synchronize_srcu_expedited(&kvm->srcu);
2495 kvm_mmu_zap_all(kvm);
2496 kfree(old_aliases);
2497
2498 r = -ENOMEM;
2499 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2500 if (!aliases)
2501 goto out_unlock;
2502
2503 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002504
2505 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002506 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2507 p->npages = alias->memory_size >> PAGE_SHIFT;
2508 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002509 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002510
2511 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002512 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002513 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002514 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002515
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002516 old_aliases = kvm->arch.aliases;
2517 rcu_assign_pointer(kvm->arch.aliases, aliases);
2518 synchronize_srcu_expedited(&kvm->srcu);
2519 kfree(old_aliases);
2520 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002521
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002522out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002523 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002524out:
2525 return r;
2526}
2527
2528static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2529{
2530 int r;
2531
2532 r = 0;
2533 switch (chip->chip_id) {
2534 case KVM_IRQCHIP_PIC_MASTER:
2535 memcpy(&chip->chip.pic,
2536 &pic_irqchip(kvm)->pics[0],
2537 sizeof(struct kvm_pic_state));
2538 break;
2539 case KVM_IRQCHIP_PIC_SLAVE:
2540 memcpy(&chip->chip.pic,
2541 &pic_irqchip(kvm)->pics[1],
2542 sizeof(struct kvm_pic_state));
2543 break;
2544 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002545 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002546 break;
2547 default:
2548 r = -EINVAL;
2549 break;
2550 }
2551 return r;
2552}
2553
2554static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2555{
2556 int r;
2557
2558 r = 0;
2559 switch (chip->chip_id) {
2560 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002561 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002562 memcpy(&pic_irqchip(kvm)->pics[0],
2563 &chip->chip.pic,
2564 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002565 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002566 break;
2567 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002568 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002569 memcpy(&pic_irqchip(kvm)->pics[1],
2570 &chip->chip.pic,
2571 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002572 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002573 break;
2574 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002575 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002576 break;
2577 default:
2578 r = -EINVAL;
2579 break;
2580 }
2581 kvm_pic_update_irq(pic_irqchip(kvm));
2582 return r;
2583}
2584
Sheng Yange0f63cb2008-03-04 00:50:59 +08002585static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2586{
2587 int r = 0;
2588
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002589 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002590 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002591 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002592 return r;
2593}
2594
2595static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2596{
2597 int r = 0;
2598
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002599 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002600 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002601 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2602 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2603 return r;
2604}
2605
2606static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2607{
2608 int r = 0;
2609
2610 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2611 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2612 sizeof(ps->channels));
2613 ps->flags = kvm->arch.vpit->pit_state.flags;
2614 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2615 return r;
2616}
2617
2618static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2619{
2620 int r = 0, start = 0;
2621 u32 prev_legacy, cur_legacy;
2622 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2623 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2624 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2625 if (!prev_legacy && cur_legacy)
2626 start = 1;
2627 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2628 sizeof(kvm->arch.vpit->pit_state.channels));
2629 kvm->arch.vpit->pit_state.flags = ps->flags;
2630 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002631 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002632 return r;
2633}
2634
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002635static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2636 struct kvm_reinject_control *control)
2637{
2638 if (!kvm->arch.vpit)
2639 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002640 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002641 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002642 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002643 return 0;
2644}
2645
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002646/*
2647 * Get (and clear) the dirty memory log for a memory slot.
2648 */
2649int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2650 struct kvm_dirty_log *log)
2651{
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002652 int r, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002653 struct kvm_memory_slot *memslot;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002654 unsigned long n;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002655 unsigned long is_dirty = 0;
2656 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002657
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002658 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002659
Marcelo Tosattib050b012009-12-23 14:35:22 -02002660 r = -EINVAL;
2661 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002662 goto out;
2663
Marcelo Tosattib050b012009-12-23 14:35:22 -02002664 memslot = &kvm->memslots->memslots[log->slot];
2665 r = -ENOENT;
2666 if (!memslot->dirty_bitmap)
2667 goto out;
2668
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002669 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002670
2671 r = -ENOMEM;
2672 dirty_bitmap = vmalloc(n);
2673 if (!dirty_bitmap)
2674 goto out;
2675 memset(dirty_bitmap, 0, n);
2676
2677 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2678 is_dirty = memslot->dirty_bitmap[i];
2679
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002680 /* If nothing is dirty, don't bother messing with page tables. */
2681 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002682 struct kvm_memslots *slots, *old_slots;
2683
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002684 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002685 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002686 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002687
2688 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2689 if (!slots)
2690 goto out_free;
2691
2692 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2693 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2694
2695 old_slots = kvm->memslots;
2696 rcu_assign_pointer(kvm->memslots, slots);
2697 synchronize_srcu_expedited(&kvm->srcu);
2698 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2699 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002700 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002701
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002702 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002703 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2704 r = -EFAULT;
2705out_free:
2706 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002707out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002708 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002709 return r;
2710}
2711
Carsten Otte1fe779f2007-10-29 16:08:35 +01002712long kvm_arch_vm_ioctl(struct file *filp,
2713 unsigned int ioctl, unsigned long arg)
2714{
2715 struct kvm *kvm = filp->private_data;
2716 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002717 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002718 /*
2719 * This union makes it completely explicit to gcc-3.x
2720 * that these two variables' stack usage should be
2721 * combined, not added together.
2722 */
2723 union {
2724 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002725 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002726 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002727 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002728 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002729
2730 switch (ioctl) {
2731 case KVM_SET_TSS_ADDR:
2732 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2733 if (r < 0)
2734 goto out;
2735 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002736 case KVM_SET_IDENTITY_MAP_ADDR: {
2737 u64 ident_addr;
2738
2739 r = -EFAULT;
2740 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2741 goto out;
2742 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2743 if (r < 0)
2744 goto out;
2745 break;
2746 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002747 case KVM_SET_MEMORY_REGION: {
2748 struct kvm_memory_region kvm_mem;
2749 struct kvm_userspace_memory_region kvm_userspace_mem;
2750
2751 r = -EFAULT;
2752 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2753 goto out;
2754 kvm_userspace_mem.slot = kvm_mem.slot;
2755 kvm_userspace_mem.flags = kvm_mem.flags;
2756 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2757 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2758 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2759 if (r)
2760 goto out;
2761 break;
2762 }
2763 case KVM_SET_NR_MMU_PAGES:
2764 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2765 if (r)
2766 goto out;
2767 break;
2768 case KVM_GET_NR_MMU_PAGES:
2769 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2770 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002771 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002772 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002773 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002774 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002775 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002776 if (r)
2777 goto out;
2778 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002779 case KVM_CREATE_IRQCHIP: {
2780 struct kvm_pic *vpic;
2781
2782 mutex_lock(&kvm->lock);
2783 r = -EEXIST;
2784 if (kvm->arch.vpic)
2785 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002786 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002787 vpic = kvm_create_pic(kvm);
2788 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002789 r = kvm_ioapic_init(kvm);
2790 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002791 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2792 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002793 kfree(vpic);
2794 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002795 }
2796 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002797 goto create_irqchip_unlock;
2798 smp_wmb();
2799 kvm->arch.vpic = vpic;
2800 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002801 r = kvm_setup_default_irq_routing(kvm);
2802 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002803 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002804 kvm_ioapic_destroy(kvm);
2805 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002806 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002807 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002808 create_irqchip_unlock:
2809 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002810 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002811 }
Sheng Yang78376992008-01-28 05:10:22 +08002812 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002813 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2814 goto create_pit;
2815 case KVM_CREATE_PIT2:
2816 r = -EFAULT;
2817 if (copy_from_user(&u.pit_config, argp,
2818 sizeof(struct kvm_pit_config)))
2819 goto out;
2820 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002821 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002822 r = -EEXIST;
2823 if (kvm->arch.vpit)
2824 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002825 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002826 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002827 if (kvm->arch.vpit)
2828 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002829 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002830 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002831 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002832 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002833 case KVM_IRQ_LINE: {
2834 struct kvm_irq_level irq_event;
2835
2836 r = -EFAULT;
2837 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2838 goto out;
2839 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002840 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002841 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2842 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002843 if (ioctl == KVM_IRQ_LINE_STATUS) {
2844 irq_event.status = status;
2845 if (copy_to_user(argp, &irq_event,
2846 sizeof irq_event))
2847 goto out;
2848 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002849 r = 0;
2850 }
2851 break;
2852 }
2853 case KVM_GET_IRQCHIP: {
2854 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002855 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002856
Dave Hansenf0d66272008-08-11 10:01:45 -07002857 r = -ENOMEM;
2858 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002859 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002860 r = -EFAULT;
2861 if (copy_from_user(chip, argp, sizeof *chip))
2862 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002863 r = -ENXIO;
2864 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002865 goto get_irqchip_out;
2866 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2867 if (r)
2868 goto get_irqchip_out;
2869 r = -EFAULT;
2870 if (copy_to_user(argp, chip, sizeof *chip))
2871 goto get_irqchip_out;
2872 r = 0;
2873 get_irqchip_out:
2874 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002875 if (r)
2876 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002877 break;
2878 }
2879 case KVM_SET_IRQCHIP: {
2880 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002881 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002882
Dave Hansenf0d66272008-08-11 10:01:45 -07002883 r = -ENOMEM;
2884 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002885 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002886 r = -EFAULT;
2887 if (copy_from_user(chip, argp, sizeof *chip))
2888 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002889 r = -ENXIO;
2890 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002891 goto set_irqchip_out;
2892 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2893 if (r)
2894 goto set_irqchip_out;
2895 r = 0;
2896 set_irqchip_out:
2897 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002898 if (r)
2899 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002900 break;
2901 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002902 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002903 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002904 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002905 goto out;
2906 r = -ENXIO;
2907 if (!kvm->arch.vpit)
2908 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002909 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002910 if (r)
2911 goto out;
2912 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002913 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002914 goto out;
2915 r = 0;
2916 break;
2917 }
2918 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002919 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002920 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002921 goto out;
2922 r = -ENXIO;
2923 if (!kvm->arch.vpit)
2924 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002925 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002926 if (r)
2927 goto out;
2928 r = 0;
2929 break;
2930 }
Beth Kone9f42752009-07-07 11:50:38 -04002931 case KVM_GET_PIT2: {
2932 r = -ENXIO;
2933 if (!kvm->arch.vpit)
2934 goto out;
2935 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2936 if (r)
2937 goto out;
2938 r = -EFAULT;
2939 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2940 goto out;
2941 r = 0;
2942 break;
2943 }
2944 case KVM_SET_PIT2: {
2945 r = -EFAULT;
2946 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2947 goto out;
2948 r = -ENXIO;
2949 if (!kvm->arch.vpit)
2950 goto out;
2951 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2952 if (r)
2953 goto out;
2954 r = 0;
2955 break;
2956 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002957 case KVM_REINJECT_CONTROL: {
2958 struct kvm_reinject_control control;
2959 r = -EFAULT;
2960 if (copy_from_user(&control, argp, sizeof(control)))
2961 goto out;
2962 r = kvm_vm_ioctl_reinject(kvm, &control);
2963 if (r)
2964 goto out;
2965 r = 0;
2966 break;
2967 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002968 case KVM_XEN_HVM_CONFIG: {
2969 r = -EFAULT;
2970 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2971 sizeof(struct kvm_xen_hvm_config)))
2972 goto out;
2973 r = -EINVAL;
2974 if (kvm->arch.xen_hvm_config.flags)
2975 goto out;
2976 r = 0;
2977 break;
2978 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002979 case KVM_SET_CLOCK: {
2980 struct timespec now;
2981 struct kvm_clock_data user_ns;
2982 u64 now_ns;
2983 s64 delta;
2984
2985 r = -EFAULT;
2986 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2987 goto out;
2988
2989 r = -EINVAL;
2990 if (user_ns.flags)
2991 goto out;
2992
2993 r = 0;
2994 ktime_get_ts(&now);
2995 now_ns = timespec_to_ns(&now);
2996 delta = user_ns.clock - now_ns;
2997 kvm->arch.kvmclock_offset = delta;
2998 break;
2999 }
3000 case KVM_GET_CLOCK: {
3001 struct timespec now;
3002 struct kvm_clock_data user_ns;
3003 u64 now_ns;
3004
3005 ktime_get_ts(&now);
3006 now_ns = timespec_to_ns(&now);
3007 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3008 user_ns.flags = 0;
3009
3010 r = -EFAULT;
3011 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3012 goto out;
3013 r = 0;
3014 break;
3015 }
3016
Carsten Otte1fe779f2007-10-29 16:08:35 +01003017 default:
3018 ;
3019 }
3020out:
3021 return r;
3022}
3023
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003024static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003025{
3026 u32 dummy[2];
3027 unsigned i, j;
3028
Glauber Costae3267cb2009-10-06 13:24:50 -04003029 /* skip the first msrs in the list. KVM-specific */
3030 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003031 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3032 continue;
3033 if (j < i)
3034 msrs_to_save[j] = msrs_to_save[i];
3035 j++;
3036 }
3037 num_msrs_to_save = j;
3038}
3039
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003040static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3041 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003042{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003043 if (vcpu->arch.apic &&
3044 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3045 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003046
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003047 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003048}
3049
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003050static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003051{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003052 if (vcpu->arch.apic &&
3053 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3054 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003055
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003056 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003057}
3058
Gleb Natapov1871c602010-02-10 14:21:32 +02003059gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3060{
3061 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3062 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3063}
3064
3065 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3066{
3067 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3068 access |= PFERR_FETCH_MASK;
3069 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3070}
3071
3072gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3073{
3074 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3075 access |= PFERR_WRITE_MASK;
3076 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3077}
3078
3079/* uses this to access any guest's mapped memory without checking CPL */
3080gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3081{
3082 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3083}
3084
3085static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3086 struct kvm_vcpu *vcpu, u32 access,
3087 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003088{
3089 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003090 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003091
3092 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003093 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003094 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003095 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003096 int ret;
3097
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003098 if (gpa == UNMAPPED_GVA) {
3099 r = X86EMUL_PROPAGATE_FAULT;
3100 goto out;
3101 }
Izik Eidus77c20022008-12-29 01:42:19 +02003102 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003103 if (ret < 0) {
3104 r = X86EMUL_UNHANDLEABLE;
3105 goto out;
3106 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003107
Izik Eidus77c20022008-12-29 01:42:19 +02003108 bytes -= toread;
3109 data += toread;
3110 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003111 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003112out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003113 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003114}
Izik Eidus77c20022008-12-29 01:42:19 +02003115
Gleb Natapov1871c602010-02-10 14:21:32 +02003116/* used for instruction fetching */
3117static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3118 struct kvm_vcpu *vcpu, u32 *error)
3119{
3120 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3121 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3122 access | PFERR_FETCH_MASK, error);
3123}
3124
3125static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3126 struct kvm_vcpu *vcpu, u32 *error)
3127{
3128 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3129 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3130 error);
3131}
3132
3133static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3134 struct kvm_vcpu *vcpu, u32 *error)
3135{
3136 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3137}
3138
Hannes Edercded19f2009-02-21 02:19:13 +01003139static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +02003140 struct kvm_vcpu *vcpu, u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003141{
3142 void *data = val;
3143 int r = X86EMUL_CONTINUE;
3144
3145 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003146 gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003147 unsigned offset = addr & (PAGE_SIZE-1);
3148 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3149 int ret;
3150
3151 if (gpa == UNMAPPED_GVA) {
3152 r = X86EMUL_PROPAGATE_FAULT;
3153 goto out;
3154 }
3155 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3156 if (ret < 0) {
3157 r = X86EMUL_UNHANDLEABLE;
3158 goto out;
3159 }
3160
3161 bytes -= towrite;
3162 data += towrite;
3163 addr += towrite;
3164 }
3165out:
3166 return r;
3167}
3168
Carsten Ottebbd9b642007-10-30 18:44:21 +01003169
Carsten Ottebbd9b642007-10-30 18:44:21 +01003170static int emulator_read_emulated(unsigned long addr,
3171 void *val,
3172 unsigned int bytes,
3173 struct kvm_vcpu *vcpu)
3174{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003175 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003176 u32 error_code;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003177
3178 if (vcpu->mmio_read_completed) {
3179 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003180 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3181 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003182 vcpu->mmio_read_completed = 0;
3183 return X86EMUL_CONTINUE;
3184 }
3185
Gleb Natapov1871c602010-02-10 14:21:32 +02003186 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
3187
3188 if (gpa == UNMAPPED_GVA) {
3189 kvm_inject_page_fault(vcpu, addr, error_code);
3190 return X86EMUL_PROPAGATE_FAULT;
3191 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003192
3193 /* For APIC access vmexit */
3194 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3195 goto mmio;
3196
Gleb Natapov1871c602010-02-10 14:21:32 +02003197 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003198 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003199 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003200
3201mmio:
3202 /*
3203 * Is this MMIO handled locally?
3204 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003205 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3206 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003207 return X86EMUL_CONTINUE;
3208 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003209
3210 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003211
3212 vcpu->mmio_needed = 1;
3213 vcpu->mmio_phys_addr = gpa;
3214 vcpu->mmio_size = bytes;
3215 vcpu->mmio_is_write = 0;
3216
3217 return X86EMUL_UNHANDLEABLE;
3218}
3219
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003220int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003221 const void *val, int bytes)
3222{
3223 int ret;
3224
3225 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3226 if (ret < 0)
3227 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003228 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003229 return 1;
3230}
3231
Carsten Ottebbd9b642007-10-30 18:44:21 +01003232static int emulator_write_emulated_onepage(unsigned long addr,
3233 const void *val,
3234 unsigned int bytes,
3235 struct kvm_vcpu *vcpu)
3236{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003237 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003238 u32 error_code;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003239
Gleb Natapov1871c602010-02-10 14:21:32 +02003240 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003241
3242 if (gpa == UNMAPPED_GVA) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003243 kvm_inject_page_fault(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003244 return X86EMUL_PROPAGATE_FAULT;
3245 }
3246
3247 /* For APIC access vmexit */
3248 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3249 goto mmio;
3250
3251 if (emulator_write_phys(vcpu, gpa, val, bytes))
3252 return X86EMUL_CONTINUE;
3253
3254mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003255 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003256 /*
3257 * Is this MMIO handled locally?
3258 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003259 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003260 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003261
3262 vcpu->mmio_needed = 1;
3263 vcpu->mmio_phys_addr = gpa;
3264 vcpu->mmio_size = bytes;
3265 vcpu->mmio_is_write = 1;
3266 memcpy(vcpu->mmio_data, val, bytes);
3267
3268 return X86EMUL_CONTINUE;
3269}
3270
3271int emulator_write_emulated(unsigned long addr,
3272 const void *val,
3273 unsigned int bytes,
3274 struct kvm_vcpu *vcpu)
3275{
3276 /* Crossing a page boundary? */
3277 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3278 int rc, now;
3279
3280 now = -addr & ~PAGE_MASK;
3281 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
3282 if (rc != X86EMUL_CONTINUE)
3283 return rc;
3284 addr += now;
3285 val += now;
3286 bytes -= now;
3287 }
3288 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
3289}
3290EXPORT_SYMBOL_GPL(emulator_write_emulated);
3291
3292static int emulator_cmpxchg_emulated(unsigned long addr,
3293 const void *old,
3294 const void *new,
3295 unsigned int bytes,
3296 struct kvm_vcpu *vcpu)
3297{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02003298 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003299#ifndef CONFIG_X86_64
3300 /* guests cmpxchg8b have to be emulated atomically */
3301 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003302 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003303 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003304 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003305 u64 val;
3306
Gleb Natapov1871c602010-02-10 14:21:32 +02003307 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003308
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003309 if (gpa == UNMAPPED_GVA ||
3310 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3311 goto emul_write;
3312
3313 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3314 goto emul_write;
3315
3316 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003317
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003318 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003319
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003320 kaddr = kmap_atomic(page, KM_USER0);
3321 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
3322 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003323 kvm_release_page_dirty(page);
3324 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003325emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003326#endif
3327
Carsten Ottebbd9b642007-10-30 18:44:21 +01003328 return emulator_write_emulated(addr, new, bytes, vcpu);
3329}
3330
3331static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3332{
3333 return kvm_x86_ops->get_segment_base(vcpu, seg);
3334}
3335
3336int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3337{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003338 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003339 return X86EMUL_CONTINUE;
3340}
3341
3342int emulate_clts(struct kvm_vcpu *vcpu)
3343{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003344 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003345 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003346 return X86EMUL_CONTINUE;
3347}
3348
3349int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3350{
Jan Kiszkac76de352010-01-20 18:20:20 +01003351 return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003352}
3353
3354int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3355{
3356 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003357
Jan Kiszkac76de352010-01-20 18:20:20 +01003358 return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003359}
3360
3361void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3362{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003363 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003364 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003365 unsigned long rip_linear;
3366
Avi Kivityf76c7102008-06-13 22:45:42 +03003367 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003368 return;
3369
Glauber Costa25be4602008-06-10 10:46:53 -03003370 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3371
Gleb Natapov1871c602010-02-10 14:21:32 +02003372 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003373
3374 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3375 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003376}
3377EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3378
Harvey Harrison14af3f32008-02-19 10:25:50 -08003379static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003380 .read_std = kvm_read_guest_virt_system,
3381 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003382 .read_emulated = emulator_read_emulated,
3383 .write_emulated = emulator_write_emulated,
3384 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3385};
3386
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003387static void cache_all_regs(struct kvm_vcpu *vcpu)
3388{
3389 kvm_register_read(vcpu, VCPU_REGS_RAX);
3390 kvm_register_read(vcpu, VCPU_REGS_RSP);
3391 kvm_register_read(vcpu, VCPU_REGS_RIP);
3392 vcpu->arch.regs_dirty = ~0;
3393}
3394
Carsten Ottebbd9b642007-10-30 18:44:21 +01003395int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003396 unsigned long cr2,
3397 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003398 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003399{
Glauber Costa310b5d32009-05-12 16:21:06 -04003400 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003401 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003402 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003403
Avi Kivity26eef702008-07-03 14:59:22 +03003404 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003405 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003406 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003407 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003408 * instead of direct ->regs accesses, can save hundred cycles
3409 * on Intel for instructions that don't read/change RSP, for
3410 * for example.
3411 */
3412 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003413
3414 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003415 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003416
Sheng Yang571008d2008-01-02 14:49:22 +08003417 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003418 int cs_db, cs_l;
3419 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3420
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003421 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka83bf0002010-02-23 17:47:59 +01003422 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003423 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003424 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003425 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003426 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003427 ? X86EMUL_MODE_PROT64 : cs_db
3428 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3429
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003430 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003431
Andre Przywara0cb57622009-06-17 15:50:31 +02003432 /* Only allow emulation of specific instructions on #UD
3433 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003434 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003435 if (emulation_type & EMULTYPE_TRAP_UD) {
3436 if (!c->twobyte)
3437 return EMULATE_FAIL;
3438 switch (c->b) {
3439 case 0x01: /* VMMCALL */
3440 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3441 return EMULATE_FAIL;
3442 break;
3443 case 0x34: /* sysenter */
3444 case 0x35: /* sysexit */
3445 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3446 return EMULATE_FAIL;
3447 break;
3448 case 0x05: /* syscall */
3449 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3450 return EMULATE_FAIL;
3451 break;
3452 default:
3453 return EMULATE_FAIL;
3454 }
3455
3456 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3457 return EMULATE_FAIL;
3458 }
Sheng Yang571008d2008-01-02 14:49:22 +08003459
Avi Kivityf2b57562007-11-18 15:17:51 +02003460 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003461 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003462 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003463 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3464 return EMULATE_DONE;
3465 return EMULATE_FAIL;
3466 }
3467 }
3468
Gleb Natapovba8afb62009-04-12 13:36:57 +03003469 if (emulation_type & EMULTYPE_SKIP) {
3470 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3471 return EMULATE_DONE;
3472 }
3473
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003474 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003475 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3476
3477 if (r == 0)
3478 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003479
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003480 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003481 return EMULATE_DO_MMIO;
3482
Gleb Natapov112592d2010-02-21 15:00:47 +02003483 if (r || vcpu->mmio_is_write) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003484 run->exit_reason = KVM_EXIT_MMIO;
3485 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3486 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3487 run->mmio.len = vcpu->mmio_size;
3488 run->mmio.is_write = vcpu->mmio_is_write;
3489 }
3490
3491 if (r) {
3492 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3493 return EMULATE_DONE;
3494 if (!vcpu->mmio_needed) {
3495 kvm_report_emulation_failure(vcpu, "mmio");
3496 return EMULATE_FAIL;
3497 }
3498 return EMULATE_DO_MMIO;
3499 }
3500
Jan Kiszka83bf0002010-02-23 17:47:59 +01003501 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003502
3503 if (vcpu->mmio_is_write) {
3504 vcpu->mmio_needed = 0;
3505 return EMULATE_DO_MMIO;
3506 }
3507
3508 return EMULATE_DONE;
3509}
3510EXPORT_SYMBOL_GPL(emulate_instruction);
3511
Carsten Ottede7d7892007-10-30 18:44:25 +01003512static int pio_copy_data(struct kvm_vcpu *vcpu)
3513{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003514 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003515 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003516 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003517 int ret;
Gleb Natapov1871c602010-02-10 14:21:32 +02003518 u32 error_code;
Carsten Ottede7d7892007-10-30 18:44:25 +01003519
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003520 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3521 if (vcpu->arch.pio.in)
Gleb Natapov1871c602010-02-10 14:21:32 +02003522 ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code);
Carsten Ottede7d7892007-10-30 18:44:25 +01003523 else
Gleb Natapov1871c602010-02-10 14:21:32 +02003524 ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code);
3525
3526 if (ret == X86EMUL_PROPAGATE_FAULT)
3527 kvm_inject_page_fault(vcpu, q, error_code);
3528
Izik Eidus0f346072008-12-29 01:42:20 +02003529 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003530}
3531
3532int complete_pio(struct kvm_vcpu *vcpu)
3533{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003534 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003535 long delta;
3536 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003537 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003538
3539 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003540 if (io->in) {
3541 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3542 memcpy(&val, vcpu->arch.pio_data, io->size);
3543 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3544 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003545 } else {
3546 if (io->in) {
3547 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003548 if (r)
Gleb Natapov1871c602010-02-10 14:21:32 +02003549 goto out;
Carsten Ottede7d7892007-10-30 18:44:25 +01003550 }
3551
3552 delta = 1;
3553 if (io->rep) {
3554 delta *= io->cur_count;
3555 /*
3556 * The size of the register should really depend on
3557 * current address size.
3558 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003559 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3560 val -= delta;
3561 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003562 }
3563 if (io->down)
3564 delta = -delta;
3565 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003566 if (io->in) {
3567 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3568 val += delta;
3569 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3570 } else {
3571 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3572 val += delta;
3573 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3574 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003575 }
Gleb Natapov1871c602010-02-10 14:21:32 +02003576out:
Carsten Ottede7d7892007-10-30 18:44:25 +01003577 io->count -= io->cur_count;
3578 io->cur_count = 0;
3579
3580 return 0;
3581}
3582
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003583static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003584{
3585 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003586 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003587
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003588 if (vcpu->arch.pio.in)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003589 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003590 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003591 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003592 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3593 vcpu->arch.pio.port, vcpu->arch.pio.size,
3594 pd);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003595 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003596}
3597
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003598static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003599{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003600 struct kvm_pio_request *io = &vcpu->arch.pio;
3601 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003602 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003603
Carsten Ottede7d7892007-10-30 18:44:25 +01003604 for (i = 0; i < io->cur_count; i++) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003605 if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003606 io->port, io->size, pd)) {
3607 r = -EOPNOTSUPP;
3608 break;
3609 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003610 pd += io->size;
3611 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003612 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003613}
3614
Avi Kivity851ba692009-08-24 11:10:17 +03003615int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003616{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003617 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003618
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003619 trace_kvm_pio(!in, port, size, 1);
3620
Carsten Ottede7d7892007-10-30 18:44:25 +01003621 vcpu->run->exit_reason = KVM_EXIT_IO;
3622 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003623 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003624 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003625 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3626 vcpu->run->io.port = vcpu->arch.pio.port = port;
3627 vcpu->arch.pio.in = in;
3628 vcpu->arch.pio.string = 0;
3629 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003630 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003631
Takuya Yoshikawa1976d2d2010-02-05 17:52:46 +09003632 if (!vcpu->arch.pio.in) {
3633 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3634 memcpy(vcpu->arch.pio_data, &val, 4);
3635 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003636
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003637 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003638 complete_pio(vcpu);
3639 return 1;
3640 }
3641 return 0;
3642}
3643EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3644
Avi Kivity851ba692009-08-24 11:10:17 +03003645int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003646 int size, unsigned long count, int down,
3647 gva_t address, int rep, unsigned port)
3648{
3649 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003650 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003651
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003652 trace_kvm_pio(!in, port, size, count);
3653
Carsten Ottede7d7892007-10-30 18:44:25 +01003654 vcpu->run->exit_reason = KVM_EXIT_IO;
3655 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003656 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003657 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003658 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3659 vcpu->run->io.port = vcpu->arch.pio.port = port;
3660 vcpu->arch.pio.in = in;
3661 vcpu->arch.pio.string = 1;
3662 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003663 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003664
3665 if (!count) {
3666 kvm_x86_ops->skip_emulated_instruction(vcpu);
3667 return 1;
3668 }
3669
3670 if (!down)
3671 in_page = PAGE_SIZE - offset_in_page(address);
3672 else
3673 in_page = offset_in_page(address) + size;
3674 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003675 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003676 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003677 if (down) {
3678 /*
3679 * String I/O in reverse. Yuck. Kill the guest, fix later.
3680 */
3681 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003682 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003683 return 1;
3684 }
3685 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003686 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003687
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003688 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003689 kvm_x86_ops->skip_emulated_instruction(vcpu);
3690
Izik Eidus0f346072008-12-29 01:42:20 +02003691 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003692
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003693 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003694 /* string PIO write */
3695 ret = pio_copy_data(vcpu);
Gleb Natapov1871c602010-02-10 14:21:32 +02003696 if (ret == X86EMUL_PROPAGATE_FAULT)
Izik Eidus0f346072008-12-29 01:42:20 +02003697 return 1;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003698 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003699 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003700 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003701 ret = 1;
3702 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003703 }
3704 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003705
3706 return ret;
3707}
3708EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3709
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003710static void bounce_off(void *info)
3711{
3712 /* nothing */
3713}
3714
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003715static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3716 void *data)
3717{
3718 struct cpufreq_freqs *freq = data;
3719 struct kvm *kvm;
3720 struct kvm_vcpu *vcpu;
3721 int i, send_ipi = 0;
3722
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003723 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3724 return 0;
3725 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3726 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003727 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003728
3729 spin_lock(&kvm_lock);
3730 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003731 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003732 if (vcpu->cpu != freq->cpu)
3733 continue;
3734 if (!kvm_request_guest_time_update(vcpu))
3735 continue;
3736 if (vcpu->cpu != smp_processor_id())
3737 send_ipi++;
3738 }
3739 }
3740 spin_unlock(&kvm_lock);
3741
3742 if (freq->old < freq->new && send_ipi) {
3743 /*
3744 * We upscale the frequency. Must make the guest
3745 * doesn't see old kvmclock values while running with
3746 * the new frequency, otherwise we risk the guest sees
3747 * time go backwards.
3748 *
3749 * In case we update the frequency for another cpu
3750 * (which might be in guest context) send an interrupt
3751 * to kick the cpu out of guest context. Next time
3752 * guest context is entered kvmclock will be updated,
3753 * so the guest will not see stale values.
3754 */
3755 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3756 }
3757 return 0;
3758}
3759
3760static struct notifier_block kvmclock_cpufreq_notifier_block = {
3761 .notifier_call = kvmclock_cpufreq_notifier
3762};
3763
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003764static void kvm_timer_init(void)
3765{
3766 int cpu;
3767
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003768 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003769 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3770 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003771 for_each_online_cpu(cpu) {
3772 unsigned long khz = cpufreq_get(cpu);
3773 if (!khz)
3774 khz = tsc_khz;
3775 per_cpu(cpu_tsc_khz, cpu) = khz;
3776 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003777 } else {
3778 for_each_possible_cpu(cpu)
3779 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003780 }
3781}
3782
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003783int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003784{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003785 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003786 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3787
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003788 if (kvm_x86_ops) {
3789 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003790 r = -EEXIST;
3791 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003792 }
3793
3794 if (!ops->cpu_has_kvm_support()) {
3795 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003796 r = -EOPNOTSUPP;
3797 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003798 }
3799 if (ops->disabled_by_bios()) {
3800 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003801 r = -EOPNOTSUPP;
3802 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003803 }
3804
Avi Kivity97db56c2008-01-13 13:23:56 +02003805 r = kvm_mmu_module_init();
3806 if (r)
3807 goto out;
3808
3809 kvm_init_msr_list();
3810
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003811 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003812 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003813 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3814 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003815 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003816
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003817 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003818
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003819 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003820
3821out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003822 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003823}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003824
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003825void kvm_arch_exit(void)
3826{
Jan Kiszka888d2562009-04-17 19:24:58 +02003827 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3828 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3829 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003830 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003831 kvm_mmu_module_exit();
3832}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003833
Hollis Blanchard8776e512007-10-31 17:24:24 -05003834int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3835{
3836 ++vcpu->stat.halt_exits;
3837 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003838 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003839 return 1;
3840 } else {
3841 vcpu->run->exit_reason = KVM_EXIT_HLT;
3842 return 0;
3843 }
3844}
3845EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3846
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003847static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3848 unsigned long a1)
3849{
3850 if (is_long_mode(vcpu))
3851 return a0;
3852 else
3853 return a0 | ((gpa_t)a1 << 32);
3854}
3855
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003856int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
3857{
3858 u64 param, ingpa, outgpa, ret;
3859 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
3860 bool fast, longmode;
3861 int cs_db, cs_l;
3862
3863 /*
3864 * hypercall generates UD from non zero cpl and real mode
3865 * per HYPER-V spec
3866 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003867 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003868 kvm_queue_exception(vcpu, UD_VECTOR);
3869 return 0;
3870 }
3871
3872 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3873 longmode = is_long_mode(vcpu) && cs_l == 1;
3874
3875 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02003876 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
3877 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
3878 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
3879 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
3880 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
3881 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003882 }
3883#ifdef CONFIG_X86_64
3884 else {
3885 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
3886 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
3887 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
3888 }
3889#endif
3890
3891 code = param & 0xffff;
3892 fast = (param >> 16) & 0x1;
3893 rep_cnt = (param >> 32) & 0xfff;
3894 rep_idx = (param >> 48) & 0xfff;
3895
3896 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
3897
Gleb Natapovc25bc162010-01-17 15:51:24 +02003898 switch (code) {
3899 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
3900 kvm_vcpu_on_spin(vcpu);
3901 break;
3902 default:
3903 res = HV_STATUS_INVALID_HYPERCALL_CODE;
3904 break;
3905 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003906
3907 ret = res | (((u64)rep_done & 0xfff) << 32);
3908 if (longmode) {
3909 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3910 } else {
3911 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
3912 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
3913 }
3914
3915 return 1;
3916}
3917
Hollis Blanchard8776e512007-10-31 17:24:24 -05003918int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3919{
3920 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003921 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003922
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003923 if (kvm_hv_hypercall_enabled(vcpu->kvm))
3924 return kvm_hv_hypercall(vcpu);
3925
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003926 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3927 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3928 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3929 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3930 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003931
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003932 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003933
Hollis Blanchard8776e512007-10-31 17:24:24 -05003934 if (!is_long_mode(vcpu)) {
3935 nr &= 0xFFFFFFFF;
3936 a0 &= 0xFFFFFFFF;
3937 a1 &= 0xFFFFFFFF;
3938 a2 &= 0xFFFFFFFF;
3939 a3 &= 0xFFFFFFFF;
3940 }
3941
Jan Kiszka07708c42009-08-03 18:43:28 +02003942 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3943 ret = -KVM_EPERM;
3944 goto out;
3945 }
3946
Hollis Blanchard8776e512007-10-31 17:24:24 -05003947 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003948 case KVM_HC_VAPIC_POLL_IRQ:
3949 ret = 0;
3950 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003951 case KVM_HC_MMU_OP:
3952 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3953 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003954 default:
3955 ret = -KVM_ENOSYS;
3956 break;
3957 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003958out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003959 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303960 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003961 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003962}
3963EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3964
3965int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3966{
3967 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003968 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003969
Hollis Blanchard8776e512007-10-31 17:24:24 -05003970 /*
3971 * Blow out the MMU to ensure that no other VCPU has an active mapping
3972 * to ensure that the updated hypercall appears atomically across all
3973 * VCPUs.
3974 */
3975 kvm_mmu_zap_all(vcpu->kvm);
3976
Hollis Blanchard8776e512007-10-31 17:24:24 -05003977 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003978
Takuya Yoshikawa7edcfac2010-02-01 22:11:52 +09003979 return emulator_write_emulated(rip, instruction, 3, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003980}
3981
3982static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3983{
3984 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3985}
3986
3987void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3988{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003989 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05003990
3991 kvm_x86_ops->set_gdt(vcpu, &dt);
3992}
3993
3994void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3995{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003996 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05003997
3998 kvm_x86_ops->set_idt(vcpu, &dt);
3999}
4000
4001void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
4002 unsigned long *rflags)
4003{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004004 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02004005 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004006}
4007
4008unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
4009{
Joerg Roedel54e445c2008-04-30 17:56:02 +02004010 unsigned long value;
4011
Hollis Blanchard8776e512007-10-31 17:24:24 -05004012 switch (cr) {
4013 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004014 value = kvm_read_cr0(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004015 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004016 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004017 value = vcpu->arch.cr2;
4018 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004019 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004020 value = vcpu->arch.cr3;
4021 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004022 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004023 value = kvm_read_cr4(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004024 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004025 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004026 value = kvm_get_cr8(vcpu);
4027 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004028 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004029 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004030 return 0;
4031 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02004032
4033 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004034}
4035
4036void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
4037 unsigned long *rflags)
4038{
4039 switch (cr) {
4040 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004041 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Jan Kiszka91586a32009-10-05 13:07:21 +02004042 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004043 break;
4044 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004045 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004046 break;
4047 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004048 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004049 break;
4050 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004051 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004052 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004053 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004054 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004055 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004056 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004057 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004058 }
4059}
4060
Dan Kenigsberg07716712007-11-21 17:10:04 +02004061static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4062{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004063 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4064 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004065
4066 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4067 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004068 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004069 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004070 if (ej->function == e->function) {
4071 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4072 return j;
4073 }
4074 }
4075 return 0; /* silence gcc, even though control never reaches here */
4076}
4077
4078/* find an entry with matching function, matching index (if needed), and that
4079 * should be read next (if it's stateful) */
4080static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4081 u32 function, u32 index)
4082{
4083 if (e->function != function)
4084 return 0;
4085 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4086 return 0;
4087 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004088 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004089 return 0;
4090 return 1;
4091}
4092
Alexander Grafd8017472008-11-25 20:17:11 +01004093struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4094 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004095{
4096 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004097 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004098
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004099 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004100 struct kvm_cpuid_entry2 *e;
4101
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004102 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004103 if (is_matching_cpuid_entry(e, function, index)) {
4104 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4105 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004106 best = e;
4107 break;
4108 }
4109 /*
4110 * Both basic or both extended?
4111 */
4112 if (((e->function ^ function) & 0x80000000) == 0)
4113 if (!best || e->function > best->function)
4114 best = e;
4115 }
Alexander Grafd8017472008-11-25 20:17:11 +01004116 return best;
4117}
Sheng Yang0e851882009-12-18 16:48:46 +08004118EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004119
Dong, Eddie82725b22009-03-30 16:21:08 +08004120int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4121{
4122 struct kvm_cpuid_entry2 *best;
4123
4124 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4125 if (best)
4126 return best->eax & 0xff;
4127 return 36;
4128}
4129
Alexander Grafd8017472008-11-25 20:17:11 +01004130void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4131{
4132 u32 function, index;
4133 struct kvm_cpuid_entry2 *best;
4134
4135 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4136 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4137 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4138 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4139 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4140 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4141 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004142 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004143 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4144 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4145 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4146 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004147 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004148 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004149 trace_kvm_cpuid(function,
4150 kvm_register_read(vcpu, VCPU_REGS_RAX),
4151 kvm_register_read(vcpu, VCPU_REGS_RBX),
4152 kvm_register_read(vcpu, VCPU_REGS_RCX),
4153 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004154}
4155EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004156
4157/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004158 * Check if userspace requested an interrupt window, and that the
4159 * interrupt window is open.
4160 *
4161 * No need to exit to userspace if we already have an interrupt queued.
4162 */
Avi Kivity851ba692009-08-24 11:10:17 +03004163static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004164{
Gleb Natapov80618232009-04-21 17:44:56 +03004165 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004166 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004167 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004168}
4169
Avi Kivity851ba692009-08-24 11:10:17 +03004170static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004171{
Avi Kivity851ba692009-08-24 11:10:17 +03004172 struct kvm_run *kvm_run = vcpu->run;
4173
Jan Kiszka91586a32009-10-05 13:07:21 +02004174 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004175 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004176 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004177 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004178 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004179 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004180 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004181 kvm_arch_interrupt_allowed(vcpu) &&
4182 !kvm_cpu_has_interrupt(vcpu) &&
4183 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004184}
4185
Avi Kivityb93463a2007-10-25 16:52:32 +02004186static void vapic_enter(struct kvm_vcpu *vcpu)
4187{
4188 struct kvm_lapic *apic = vcpu->arch.apic;
4189 struct page *page;
4190
4191 if (!apic || !apic->vapic_addr)
4192 return;
4193
4194 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004195
4196 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004197}
4198
4199static void vapic_exit(struct kvm_vcpu *vcpu)
4200{
4201 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004202 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004203
4204 if (!apic || !apic->vapic_addr)
4205 return;
4206
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004207 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004208 kvm_release_page_dirty(apic->vapic_page);
4209 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004210 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004211}
4212
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004213static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4214{
4215 int max_irr, tpr;
4216
4217 if (!kvm_x86_ops->update_cr8_intercept)
4218 return;
4219
Avi Kivity88c808f2009-08-17 22:49:40 +03004220 if (!vcpu->arch.apic)
4221 return;
4222
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004223 if (!vcpu->arch.apic->vapic_addr)
4224 max_irr = kvm_lapic_find_highest_irr(vcpu);
4225 else
4226 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004227
4228 if (max_irr != -1)
4229 max_irr >>= 4;
4230
4231 tpr = kvm_lapic_get_cr8(vcpu);
4232
4233 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4234}
4235
Avi Kivity851ba692009-08-24 11:10:17 +03004236static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004237{
4238 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004239 if (vcpu->arch.exception.pending) {
Avi Kivity5c1c85d02010-03-11 13:01:59 +02004240 trace_kvm_inj_exception(vcpu->arch.exception.nr,
4241 vcpu->arch.exception.has_error_code,
4242 vcpu->arch.exception.error_code);
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004243 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4244 vcpu->arch.exception.has_error_code,
4245 vcpu->arch.exception.error_code);
4246 return;
4247 }
4248
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004249 if (vcpu->arch.nmi_injected) {
4250 kvm_x86_ops->set_nmi(vcpu);
4251 return;
4252 }
4253
4254 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004255 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004256 return;
4257 }
4258
4259 /* try to inject new event if pending */
4260 if (vcpu->arch.nmi_pending) {
4261 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4262 vcpu->arch.nmi_pending = false;
4263 vcpu->arch.nmi_injected = true;
4264 kvm_x86_ops->set_nmi(vcpu);
4265 }
4266 } else if (kvm_cpu_has_interrupt(vcpu)) {
4267 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004268 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4269 false);
4270 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004271 }
4272 }
4273}
4274
Avi Kivity851ba692009-08-24 11:10:17 +03004275static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004276{
4277 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004278 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004279 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004280
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004281 if (vcpu->requests)
4282 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4283 kvm_mmu_unload(vcpu);
4284
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004285 r = kvm_mmu_reload(vcpu);
4286 if (unlikely(r))
4287 goto out;
4288
Avi Kivity2f52d582008-01-16 12:49:30 +02004289 if (vcpu->requests) {
4290 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004291 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004292 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4293 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004294 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4295 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004296 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4297 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004298 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4299 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004300 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004301 r = 0;
4302 goto out;
4303 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004304 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004305 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004306 r = 0;
4307 goto out;
4308 }
Avi Kivity02daab22009-12-30 12:40:26 +02004309 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4310 vcpu->fpu_active = 0;
4311 kvm_x86_ops->fpu_deactivate(vcpu);
4312 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004313 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004314
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004315 preempt_disable();
4316
4317 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004318 if (vcpu->fpu_active)
4319 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004320
4321 local_irq_disable();
4322
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004323 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4324 smp_mb__after_clear_bit();
4325
Marcelo Tosattid7690172008-09-08 15:23:48 -03004326 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004327 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c1428012008-01-15 18:27:32 +02004328 local_irq_enable();
4329 preempt_enable();
4330 r = 1;
4331 goto out;
4332 }
4333
Avi Kivity851ba692009-08-24 11:10:17 +03004334 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004335
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004336 /* enable NMI/IRQ window open exits if needed */
4337 if (vcpu->arch.nmi_pending)
4338 kvm_x86_ops->enable_nmi_window(vcpu);
4339 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4340 kvm_x86_ops->enable_irq_window(vcpu);
4341
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004342 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004343 update_cr8_intercept(vcpu);
4344 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004345 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004346
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004347 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004348
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004349 kvm_guest_enter();
4350
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004351 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004352 set_debugreg(0, 7);
4353 set_debugreg(vcpu->arch.eff_db[0], 0);
4354 set_debugreg(vcpu->arch.eff_db[1], 1);
4355 set_debugreg(vcpu->arch.eff_db[2], 2);
4356 set_debugreg(vcpu->arch.eff_db[3], 3);
4357 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004358
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004359 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004360 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004361
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004362 /*
4363 * If the guest has used debug registers, at least dr7
4364 * will be disabled while returning to the host.
4365 * If we don't have active breakpoints in the host, we don't
4366 * care about the messed up debug address registers. But if
4367 * we have some of them active, restore the old state.
4368 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004369 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004370 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004371
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004372 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004373 local_irq_enable();
4374
4375 ++vcpu->stat.exits;
4376
4377 /*
4378 * We must have an instruction between local_irq_enable() and
4379 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4380 * the interrupt shadow. The stat.exits increment will do nicely.
4381 * But we need to prevent reordering, hence this barrier():
4382 */
4383 barrier();
4384
4385 kvm_guest_exit();
4386
4387 preempt_enable();
4388
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004389 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004390
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004391 /*
4392 * Profile KVM exit RIPs:
4393 */
4394 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004395 unsigned long rip = kvm_rip_read(vcpu);
4396 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004397 }
4398
Avi Kivity298101d2007-11-25 13:41:11 +02004399
Avi Kivityb93463a2007-10-25 16:52:32 +02004400 kvm_lapic_sync_from_vapic(vcpu);
4401
Avi Kivity851ba692009-08-24 11:10:17 +03004402 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004403out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004404 return r;
4405}
4406
Gleb Natapov09cec752009-03-23 15:11:44 +02004407
Avi Kivity851ba692009-08-24 11:10:17 +03004408static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004409{
4410 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004411 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004412
4413 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004414 pr_debug("vcpu %d received sipi with vector # %x\n",
4415 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004416 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004417 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004418 if (r)
4419 return r;
4420 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004421 }
4422
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004423 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004424 vapic_enter(vcpu);
4425
4426 r = 1;
4427 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004428 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004429 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004430 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004431 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004432 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004433 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004434 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004435 {
4436 switch(vcpu->arch.mp_state) {
4437 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004438 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004439 KVM_MP_STATE_RUNNABLE;
4440 case KVM_MP_STATE_RUNNABLE:
4441 break;
4442 case KVM_MP_STATE_SIPI_RECEIVED:
4443 default:
4444 r = -EINTR;
4445 break;
4446 }
4447 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004448 }
4449
Gleb Natapov09cec752009-03-23 15:11:44 +02004450 if (r <= 0)
4451 break;
4452
4453 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4454 if (kvm_cpu_has_pending_timer(vcpu))
4455 kvm_inject_pending_timer_irqs(vcpu);
4456
Avi Kivity851ba692009-08-24 11:10:17 +03004457 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004458 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004459 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004460 ++vcpu->stat.request_irq_exits;
4461 }
4462 if (signal_pending(current)) {
4463 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004464 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004465 ++vcpu->stat.signal_exits;
4466 }
4467 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004468 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004469 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004470 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004471 }
4472 }
4473
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004474 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Avi Kivity851ba692009-08-24 11:10:17 +03004475 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004476
Avi Kivityb93463a2007-10-25 16:52:32 +02004477 vapic_exit(vcpu);
4478
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004479 return r;
4480}
4481
4482int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4483{
4484 int r;
4485 sigset_t sigsaved;
4486
4487 vcpu_load(vcpu);
4488
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004489 if (vcpu->sigset_active)
4490 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4491
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004492 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4493 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004494 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004495 r = -EAGAIN;
4496 goto out;
4497 }
4498
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004499 /* re-sync apic's tpr */
4500 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004501 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004502
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004503 if (vcpu->arch.pio.cur_count) {
Gleb Natapov7567cae2010-03-09 12:01:10 +02004504 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004505 r = complete_pio(vcpu);
Gleb Natapov7567cae2010-03-09 12:01:10 +02004506 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004507 if (r)
4508 goto out;
4509 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004510 if (vcpu->mmio_needed) {
4511 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4512 vcpu->mmio_read_completed = 1;
4513 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004514
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004515 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity851ba692009-08-24 11:10:17 +03004516 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004517 EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004518 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004519 if (r == EMULATE_DO_MMIO) {
4520 /*
4521 * Read-modify-write. Back to userspace.
4522 */
4523 r = 0;
4524 goto out;
4525 }
4526 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004527 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4528 kvm_register_write(vcpu, VCPU_REGS_RAX,
4529 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004530
Avi Kivity851ba692009-08-24 11:10:17 +03004531 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004532
4533out:
4534 if (vcpu->sigset_active)
4535 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4536
4537 vcpu_put(vcpu);
4538 return r;
4539}
4540
4541int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4542{
4543 vcpu_load(vcpu);
4544
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004545 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4546 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4547 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4548 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4549 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4550 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4551 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4552 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004553#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004554 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4555 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4556 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4557 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4558 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4559 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4560 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4561 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004562#endif
4563
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004564 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004565 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004566
4567 vcpu_put(vcpu);
4568
4569 return 0;
4570}
4571
4572int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4573{
4574 vcpu_load(vcpu);
4575
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004576 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4577 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4578 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4579 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4580 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4581 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4582 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4583 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004584#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004585 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4586 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4587 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4588 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4589 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4590 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4591 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4592 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004593#endif
4594
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004595 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004596 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004597
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004598 vcpu->arch.exception.pending = false;
4599
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004600 vcpu_put(vcpu);
4601
4602 return 0;
4603}
4604
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004605void kvm_get_segment(struct kvm_vcpu *vcpu,
4606 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004607{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004608 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004609}
4610
4611void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4612{
4613 struct kvm_segment cs;
4614
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004615 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004616 *db = cs.db;
4617 *l = cs.l;
4618}
4619EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4620
4621int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4622 struct kvm_sregs *sregs)
4623{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004624 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004625
4626 vcpu_load(vcpu);
4627
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004628 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4629 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4630 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4631 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4632 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4633 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004634
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004635 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4636 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004637
4638 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004639 sregs->idt.limit = dt.size;
4640 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004641 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004642 sregs->gdt.limit = dt.size;
4643 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004644
Avi Kivity4d4ec082009-12-29 18:07:30 +02004645 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004646 sregs->cr2 = vcpu->arch.cr2;
4647 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004648 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004649 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02004650 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004651 sregs->apic_base = kvm_get_apic_base(vcpu);
4652
Gleb Natapov923c61b2009-05-11 13:35:48 +03004653 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004654
Gleb Natapov36752c92009-05-11 13:35:53 +03004655 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004656 set_bit(vcpu->arch.interrupt.nr,
4657 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004658
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004659 vcpu_put(vcpu);
4660
4661 return 0;
4662}
4663
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004664int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4665 struct kvm_mp_state *mp_state)
4666{
4667 vcpu_load(vcpu);
4668 mp_state->mp_state = vcpu->arch.mp_state;
4669 vcpu_put(vcpu);
4670 return 0;
4671}
4672
4673int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4674 struct kvm_mp_state *mp_state)
4675{
4676 vcpu_load(vcpu);
4677 vcpu->arch.mp_state = mp_state->mp_state;
4678 vcpu_put(vcpu);
4679 return 0;
4680}
4681
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004682static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004683 struct kvm_segment *var, int seg)
4684{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004685 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004686}
4687
Izik Eidus37817f22008-03-24 23:14:53 +02004688static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4689 struct kvm_segment *kvm_desct)
4690{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004691 kvm_desct->base = get_desc_base(seg_desc);
4692 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004693 if (seg_desc->g) {
4694 kvm_desct->limit <<= 12;
4695 kvm_desct->limit |= 0xfff;
4696 }
Izik Eidus37817f22008-03-24 23:14:53 +02004697 kvm_desct->selector = selector;
4698 kvm_desct->type = seg_desc->type;
4699 kvm_desct->present = seg_desc->p;
4700 kvm_desct->dpl = seg_desc->dpl;
4701 kvm_desct->db = seg_desc->d;
4702 kvm_desct->s = seg_desc->s;
4703 kvm_desct->l = seg_desc->l;
4704 kvm_desct->g = seg_desc->g;
4705 kvm_desct->avl = seg_desc->avl;
4706 if (!selector)
4707 kvm_desct->unusable = 1;
4708 else
4709 kvm_desct->unusable = 0;
4710 kvm_desct->padding = 0;
4711}
4712
Amit Shahb8222ad2008-10-22 16:39:47 +05304713static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4714 u16 selector,
Gleb Natapov89a27f42010-02-16 10:51:48 +02004715 struct desc_ptr *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004716{
4717 if (selector & 1 << 2) {
4718 struct kvm_segment kvm_seg;
4719
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004720 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004721
4722 if (kvm_seg.unusable)
Gleb Natapov89a27f42010-02-16 10:51:48 +02004723 dtable->size = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004724 else
Gleb Natapov89a27f42010-02-16 10:51:48 +02004725 dtable->size = kvm_seg.limit;
4726 dtable->address = kvm_seg.base;
Izik Eidus37817f22008-03-24 23:14:53 +02004727 }
4728 else
4729 kvm_x86_ops->get_gdt(vcpu, dtable);
4730}
4731
4732/* allowed just for 8 bytes segments */
4733static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4734 struct desc_struct *seg_desc)
4735{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004736 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004737 u16 index = selector >> 3;
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004738 int ret;
4739 u32 err;
4740 gva_t addr;
Izik Eidus37817f22008-03-24 23:14:53 +02004741
Amit Shahb8222ad2008-10-22 16:39:47 +05304742 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004743
Gleb Natapov89a27f42010-02-16 10:51:48 +02004744 if (dtable.size < index * 8 + 7) {
Izik Eidus37817f22008-03-24 23:14:53 +02004745 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
Takuya Yoshikawac125c602010-02-01 22:11:04 +09004746 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004747 }
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004748 addr = dtable.base + index * 8;
4749 ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc),
4750 vcpu, &err);
4751 if (ret == X86EMUL_PROPAGATE_FAULT)
4752 kvm_inject_page_fault(vcpu, addr, err);
4753
4754 return ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004755}
4756
4757/* allowed just for 8 bytes segments */
4758static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4759 struct desc_struct *seg_desc)
4760{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004761 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004762 u16 index = selector >> 3;
4763
Amit Shahb8222ad2008-10-22 16:39:47 +05304764 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004765
Gleb Natapov89a27f42010-02-16 10:51:48 +02004766 if (dtable.size < index * 8 + 7)
Izik Eidus37817f22008-03-24 23:14:53 +02004767 return 1;
Gleb Natapov89a27f42010-02-16 10:51:48 +02004768 return kvm_write_guest_virt(dtable.address + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004769}
4770
Gleb Natapov1871c602010-02-10 14:21:32 +02004771static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu,
4772 struct desc_struct *seg_desc)
4773{
4774 u32 base_addr = get_desc_base(seg_desc);
4775
4776 return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL);
4777}
4778
4779static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004780 struct desc_struct *seg_desc)
4781{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004782 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004783
Gleb Natapov1871c602010-02-10 14:21:32 +02004784 return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004785}
4786
Izik Eidus37817f22008-03-24 23:14:53 +02004787static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4788{
4789 struct kvm_segment kvm_seg;
4790
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004791 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004792 return kvm_seg.selector;
4793}
4794
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004795static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004796{
4797 struct kvm_segment segvar = {
4798 .base = selector << 4,
4799 .limit = 0xffff,
4800 .selector = selector,
4801 .type = 3,
4802 .present = 1,
4803 .dpl = 3,
4804 .db = 0,
4805 .s = 1,
4806 .l = 0,
4807 .g = 0,
4808 .avl = 0,
4809 .unusable = 0,
4810 };
4811 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
Gleb Natapovc6975182010-02-18 12:15:01 +02004812 return X86EMUL_CONTINUE;
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004813}
4814
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004815static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4816{
4817 return (seg != VCPU_SREG_LDTR) &&
4818 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004819 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004820}
4821
Gleb Natapovc6975182010-02-18 12:15:01 +02004822int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004823{
4824 struct kvm_segment kvm_seg;
Gleb Natapove01c2422010-01-25 12:01:04 +02004825 struct desc_struct seg_desc;
Gleb Natapovc6975182010-02-18 12:15:01 +02004826 u8 dpl, rpl, cpl;
4827 unsigned err_vec = GP_VECTOR;
4828 u32 err_code = 0;
4829 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
4830 int ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004831
Avi Kivity3eeb3282010-01-21 15:31:48 +02004832 if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004833 return kvm_load_realmode_segment(vcpu, selector, seg);
Gleb Natapove01c2422010-01-25 12:01:04 +02004834
Gleb Natapovc6975182010-02-18 12:15:01 +02004835 /* NULL selector is not valid for TR, CS and SS */
4836 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
4837 && null_selector)
4838 goto exception;
4839
4840 /* TR should be in GDT only */
4841 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
4842 goto exception;
4843
4844 ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc);
4845 if (ret)
4846 return ret;
4847
Gleb Natapove01c2422010-01-25 12:01:04 +02004848 seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg);
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004849
Gleb Natapovc6975182010-02-18 12:15:01 +02004850 if (null_selector) { /* for NULL selector skip all following checks */
4851 kvm_seg.unusable = 1;
4852 goto load;
4853 }
Izik Eidus37817f22008-03-24 23:14:53 +02004854
Gleb Natapovc6975182010-02-18 12:15:01 +02004855 err_code = selector & 0xfffc;
4856 err_vec = GP_VECTOR;
Izik Eidus37817f22008-03-24 23:14:53 +02004857
Gleb Natapovc6975182010-02-18 12:15:01 +02004858 /* can't load system descriptor into segment selecor */
4859 if (seg <= VCPU_SREG_GS && !kvm_seg.s)
4860 goto exception;
4861
4862 if (!kvm_seg.present) {
4863 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
4864 goto exception;
4865 }
4866
4867 rpl = selector & 3;
4868 dpl = kvm_seg.dpl;
4869 cpl = kvm_x86_ops->get_cpl(vcpu);
4870
4871 switch (seg) {
4872 case VCPU_SREG_SS:
4873 /*
4874 * segment is not a writable data segment or segment
4875 * selector's RPL != CPL or segment selector's RPL != CPL
4876 */
4877 if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl)
4878 goto exception;
4879 break;
4880 case VCPU_SREG_CS:
4881 if (!(kvm_seg.type & 8))
4882 goto exception;
4883
4884 if (kvm_seg.type & 4) {
4885 /* conforming */
4886 if (dpl > cpl)
4887 goto exception;
4888 } else {
4889 /* nonconforming */
4890 if (rpl > cpl || dpl != cpl)
4891 goto exception;
4892 }
4893 /* CS(RPL) <- CPL */
4894 selector = (selector & 0xfffc) | cpl;
4895 break;
4896 case VCPU_SREG_TR:
4897 if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9))
4898 goto exception;
4899 break;
4900 case VCPU_SREG_LDTR:
4901 if (kvm_seg.s || kvm_seg.type != 2)
4902 goto exception;
4903 break;
4904 default: /* DS, ES, FS, or GS */
4905 /*
4906 * segment is not a data or readable code segment or
4907 * ((segment is a data or nonconforming code segment)
4908 * and (both RPL and CPL > DPL))
4909 */
4910 if ((kvm_seg.type & 0xa) == 0x8 ||
4911 (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl)))
4912 goto exception;
4913 break;
4914 }
4915
4916 if (!kvm_seg.unusable && kvm_seg.s) {
Gleb Natapove01c2422010-01-25 12:01:04 +02004917 /* mark segment as accessed */
Gleb Natapovc6975182010-02-18 12:15:01 +02004918 kvm_seg.type |= 1;
Gleb Natapove01c2422010-01-25 12:01:04 +02004919 seg_desc.type |= 1;
4920 save_guest_segment_descriptor(vcpu, selector, &seg_desc);
4921 }
Gleb Natapovc6975182010-02-18 12:15:01 +02004922load:
4923 kvm_set_segment(vcpu, &kvm_seg, seg);
4924 return X86EMUL_CONTINUE;
4925exception:
4926 kvm_queue_exception_e(vcpu, err_vec, err_code);
4927 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004928}
4929
4930static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4931 struct tss_segment_32 *tss)
4932{
4933 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004934 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004935 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004936 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4937 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4938 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4939 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4940 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4941 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4942 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4943 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004944 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4945 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4946 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4947 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4948 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4949 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4950 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004951}
4952
Gleb Natapovc6975182010-02-18 12:15:01 +02004953static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg)
4954{
4955 struct kvm_segment kvm_seg;
4956 kvm_get_segment(vcpu, &kvm_seg, seg);
4957 kvm_seg.selector = sel;
4958 kvm_set_segment(vcpu, &kvm_seg, seg);
4959}
4960
Izik Eidus37817f22008-03-24 23:14:53 +02004961static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4962 struct tss_segment_32 *tss)
4963{
4964 kvm_set_cr3(vcpu, tss->cr3);
4965
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004966 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004967 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004968
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004969 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4970 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4971 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4972 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4973 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4974 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4975 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4976 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004977
Gleb Natapovc6975182010-02-18 12:15:01 +02004978 /*
4979 * SDM says that segment selectors are loaded before segment
4980 * descriptors
4981 */
4982 kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR);
4983 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
4984 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
4985 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
4986 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
4987 kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS);
4988 kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS);
4989
4990 /*
4991 * Now load segment descriptors. If fault happenes at this stage
4992 * it is handled in a context of new task
4993 */
4994 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004995 return 1;
4996
Gleb Natapovc6975182010-02-18 12:15:01 +02004997 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004998 return 1;
4999
Gleb Natapovc6975182010-02-18 12:15:01 +02005000 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005001 return 1;
5002
Gleb Natapovc6975182010-02-18 12:15:01 +02005003 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005004 return 1;
5005
Gleb Natapovc6975182010-02-18 12:15:01 +02005006 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005007 return 1;
5008
Gleb Natapovc6975182010-02-18 12:15:01 +02005009 if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02005010 return 1;
5011
Gleb Natapovc6975182010-02-18 12:15:01 +02005012 if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02005013 return 1;
5014 return 0;
5015}
5016
5017static void save_state_to_tss16(struct kvm_vcpu *vcpu,
5018 struct tss_segment_16 *tss)
5019{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005020 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02005021 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005022 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
5023 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
5024 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
5025 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
5026 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
5027 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
5028 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
5029 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02005030
5031 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
5032 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
5033 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
5034 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
5035 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02005036}
5037
5038static int load_state_from_tss16(struct kvm_vcpu *vcpu,
5039 struct tss_segment_16 *tss)
5040{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005041 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02005042 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005043 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
5044 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
5045 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
5046 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
5047 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
5048 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
5049 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
5050 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02005051
Gleb Natapovc6975182010-02-18 12:15:01 +02005052 /*
5053 * SDM says that segment selectors are loaded before segment
5054 * descriptors
5055 */
5056 kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR);
5057 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5058 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5059 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5060 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5061
5062 /*
5063 * Now load segment descriptors. If fault happenes at this stage
5064 * it is handled in a context of new task
5065 */
5066 if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005067 return 1;
5068
Gleb Natapovc6975182010-02-18 12:15:01 +02005069 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005070 return 1;
5071
Gleb Natapovc6975182010-02-18 12:15:01 +02005072 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005073 return 1;
5074
Gleb Natapovc6975182010-02-18 12:15:01 +02005075 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005076 return 1;
5077
Gleb Natapovc6975182010-02-18 12:15:01 +02005078 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005079 return 1;
5080 return 0;
5081}
5082
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005083static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005084 u16 old_tss_sel, u32 old_tss_base,
5085 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02005086{
5087 struct tss_segment_16 tss_segment_16;
5088 int ret = 0;
5089
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005090 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5091 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005092 goto out;
5093
5094 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02005095
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005096 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5097 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005098 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005099
Gleb Natapov1871c602010-02-10 14:21:32 +02005100 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005101 &tss_segment_16, sizeof tss_segment_16))
5102 goto out;
5103
Gleb Natapovb237ac32009-03-30 16:03:24 +03005104 if (old_tss_sel != 0xffff) {
5105 tss_segment_16.prev_task_link = old_tss_sel;
5106
5107 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005108 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005109 &tss_segment_16.prev_task_link,
5110 sizeof tss_segment_16.prev_task_link))
5111 goto out;
5112 }
5113
Izik Eidus37817f22008-03-24 23:14:53 +02005114 if (load_state_from_tss16(vcpu, &tss_segment_16))
5115 goto out;
5116
5117 ret = 1;
5118out:
5119 return ret;
5120}
5121
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005122static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005123 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02005124 struct desc_struct *nseg_desc)
5125{
5126 struct tss_segment_32 tss_segment_32;
5127 int ret = 0;
5128
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005129 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5130 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005131 goto out;
5132
5133 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02005134
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005135 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5136 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005137 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005138
Gleb Natapov1871c602010-02-10 14:21:32 +02005139 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005140 &tss_segment_32, sizeof tss_segment_32))
5141 goto out;
5142
Gleb Natapovb237ac32009-03-30 16:03:24 +03005143 if (old_tss_sel != 0xffff) {
5144 tss_segment_32.prev_task_link = old_tss_sel;
5145
5146 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005147 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005148 &tss_segment_32.prev_task_link,
5149 sizeof tss_segment_32.prev_task_link))
5150 goto out;
5151 }
5152
Izik Eidus37817f22008-03-24 23:14:53 +02005153 if (load_state_from_tss32(vcpu, &tss_segment_32))
5154 goto out;
5155
5156 ret = 1;
5157out:
5158 return ret;
5159}
5160
5161int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
5162{
5163 struct kvm_segment tr_seg;
5164 struct desc_struct cseg_desc;
5165 struct desc_struct nseg_desc;
5166 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005167 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
5168 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005169 u32 desc_limit;
Izik Eidus37817f22008-03-24 23:14:53 +02005170
Gleb Natapov1871c602010-02-10 14:21:32 +02005171 old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02005172
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005173 /* FIXME: Handle errors. Failure to read either TSS or their
5174 * descriptors should generate a pagefault.
5175 */
Izik Eidus37817f22008-03-24 23:14:53 +02005176 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
5177 goto out;
5178
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005179 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02005180 goto out;
5181
Izik Eidus37817f22008-03-24 23:14:53 +02005182 if (reason != TASK_SWITCH_IRET) {
5183 int cpl;
5184
5185 cpl = kvm_x86_ops->get_cpl(vcpu);
5186 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
5187 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
5188 return 1;
5189 }
5190 }
5191
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005192 desc_limit = get_desc_limit(&nseg_desc);
5193 if (!nseg_desc.p ||
5194 ((desc_limit < 0x67 && (nseg_desc.type & 8)) ||
5195 desc_limit < 0x2b)) {
Izik Eidus37817f22008-03-24 23:14:53 +02005196 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
5197 return 1;
5198 }
5199
5200 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005201 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005202 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005203 }
5204
5205 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005206 u32 eflags = kvm_get_rflags(vcpu);
5207 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005208 }
5209
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005210 /* set back link to prev task only if NT bit is set in eflags
5211 note that old_tss_sel is not used afetr this point */
5212 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
5213 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02005214
5215 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03005216 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
5217 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005218 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03005219 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
5220 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005221
5222 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005223 u32 eflags = kvm_get_rflags(vcpu);
5224 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005225 }
5226
5227 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005228 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02005229 save_guest_segment_descriptor(vcpu, tss_selector,
5230 &nseg_desc);
5231 }
5232
Avi Kivity4d4ec082009-12-29 18:07:30 +02005233 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS);
Izik Eidus37817f22008-03-24 23:14:53 +02005234 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
5235 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005236 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02005237out:
Izik Eidus37817f22008-03-24 23:14:53 +02005238 return ret;
5239}
5240EXPORT_SYMBOL_GPL(kvm_task_switch);
5241
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005242int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5243 struct kvm_sregs *sregs)
5244{
5245 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03005246 int pending_vec, max_bits;
Gleb Natapov89a27f42010-02-16 10:51:48 +02005247 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005248
5249 vcpu_load(vcpu);
5250
Gleb Natapov89a27f42010-02-16 10:51:48 +02005251 dt.size = sregs->idt.limit;
5252 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005253 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005254 dt.size = sregs->gdt.limit;
5255 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005256 kvm_x86_ops->set_gdt(vcpu, &dt);
5257
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005258 vcpu->arch.cr2 = sregs->cr2;
5259 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02005260 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005261
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005262 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005263
Avi Kivityf6801df2010-01-21 15:31:50 +02005264 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005265 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005266 kvm_set_apic_base(vcpu, sregs->apic_base);
5267
Avi Kivity4d4ec082009-12-29 18:07:30 +02005268 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005269 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00005270 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005271
Avi Kivityfc78f512009-12-07 12:16:48 +02005272 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005273 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005274 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005275 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005276 mmu_reset_needed = 1;
5277 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005278
5279 if (mmu_reset_needed)
5280 kvm_mmu_reset_context(vcpu);
5281
Gleb Natapov923c61b2009-05-11 13:35:48 +03005282 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5283 pending_vec = find_first_bit(
5284 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5285 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005286 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03005287 pr_debug("Set back pending irq %d\n", pending_vec);
5288 if (irqchip_in_kernel(vcpu->kvm))
5289 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005290 }
5291
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005292 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5293 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5294 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5295 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5296 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5297 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005298
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005299 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5300 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005301
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03005302 update_cr8_intercept(vcpu);
5303
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005304 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005305 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005306 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02005307 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005308 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5309
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005310 vcpu_put(vcpu);
5311
5312 return 0;
5313}
5314
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005315int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5316 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005317{
Jan Kiszka355be0b2009-10-03 00:31:21 +02005318 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005319 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005320
5321 vcpu_load(vcpu);
5322
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005323 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5324 r = -EBUSY;
5325 if (vcpu->arch.exception.pending)
5326 goto unlock_out;
5327 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5328 kvm_queue_exception(vcpu, DB_VECTOR);
5329 else
5330 kvm_queue_exception(vcpu, BP_VECTOR);
5331 }
5332
Jan Kiszka91586a32009-10-05 13:07:21 +02005333 /*
5334 * Read rflags as long as potentially injected trace flags are still
5335 * filtered out.
5336 */
5337 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02005338
5339 vcpu->guest_debug = dbg->control;
5340 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5341 vcpu->guest_debug = 0;
5342
5343 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005344 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5345 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5346 vcpu->arch.switch_db_regs =
5347 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5348 } else {
5349 for (i = 0; i < KVM_NR_DB_REGS; i++)
5350 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5351 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5352 }
5353
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005354 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5355 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5356 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005357
Jan Kiszka91586a32009-10-05 13:07:21 +02005358 /*
5359 * Trigger an rflags update that will inject or remove the trace
5360 * flags.
5361 */
5362 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005363
Jan Kiszka355be0b2009-10-03 00:31:21 +02005364 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5365
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005366 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005367
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005368unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005369 vcpu_put(vcpu);
5370
5371 return r;
5372}
5373
5374/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005375 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5376 * we have asm/x86/processor.h
5377 */
5378struct fxsave {
5379 u16 cwd;
5380 u16 swd;
5381 u16 twd;
5382 u16 fop;
5383 u64 rip;
5384 u64 rdp;
5385 u32 mxcsr;
5386 u32 mxcsr_mask;
5387 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5388#ifdef CONFIG_X86_64
5389 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5390#else
5391 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5392#endif
5393};
5394
Zhang Xiantao8b006792007-11-16 13:05:55 +08005395/*
5396 * Translate a guest virtual address to a guest physical address.
5397 */
5398int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5399 struct kvm_translation *tr)
5400{
5401 unsigned long vaddr = tr->linear_address;
5402 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005403 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005404
5405 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005406 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005407 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005408 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005409 tr->physical_address = gpa;
5410 tr->valid = gpa != UNMAPPED_GVA;
5411 tr->writeable = 1;
5412 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005413 vcpu_put(vcpu);
5414
5415 return 0;
5416}
5417
Hollis Blanchardd0752062007-10-31 17:24:25 -05005418int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5419{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005420 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005421
5422 vcpu_load(vcpu);
5423
5424 memcpy(fpu->fpr, fxsave->st_space, 128);
5425 fpu->fcw = fxsave->cwd;
5426 fpu->fsw = fxsave->swd;
5427 fpu->ftwx = fxsave->twd;
5428 fpu->last_opcode = fxsave->fop;
5429 fpu->last_ip = fxsave->rip;
5430 fpu->last_dp = fxsave->rdp;
5431 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5432
5433 vcpu_put(vcpu);
5434
5435 return 0;
5436}
5437
5438int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5439{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005440 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005441
5442 vcpu_load(vcpu);
5443
5444 memcpy(fxsave->st_space, fpu->fpr, 128);
5445 fxsave->cwd = fpu->fcw;
5446 fxsave->swd = fpu->fsw;
5447 fxsave->twd = fpu->ftwx;
5448 fxsave->fop = fpu->last_opcode;
5449 fxsave->rip = fpu->last_ip;
5450 fxsave->rdp = fpu->last_dp;
5451 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5452
5453 vcpu_put(vcpu);
5454
5455 return 0;
5456}
5457
5458void fx_init(struct kvm_vcpu *vcpu)
5459{
5460 unsigned after_mxcsr_mask;
5461
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005462 /*
5463 * Touch the fpu the first time in non atomic context as if
5464 * this is the first fpu instruction the exception handler
5465 * will fire before the instruction returns and it'll have to
5466 * allocate ram with GFP_KERNEL.
5467 */
5468 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005469 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005470
Hollis Blanchardd0752062007-10-31 17:24:25 -05005471 /* Initialize guest FPU by resetting ours and saving into guest's */
5472 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005473 kvm_fx_save(&vcpu->arch.host_fx_image);
5474 kvm_fx_finit();
5475 kvm_fx_save(&vcpu->arch.guest_fx_image);
5476 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005477 preempt_enable();
5478
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005479 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005480 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005481 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5482 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005483 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5484}
5485EXPORT_SYMBOL_GPL(fx_init);
5486
5487void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5488{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005489 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005490 return;
5491
5492 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005493 kvm_fx_save(&vcpu->arch.host_fx_image);
5494 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Avi Kivity0c048512010-01-21 15:31:52 +02005495 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005496}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005497
5498void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5499{
5500 if (!vcpu->guest_fpu_loaded)
5501 return;
5502
5503 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005504 kvm_fx_save(&vcpu->arch.guest_fx_image);
5505 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005506 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005507 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Avi Kivity0c048512010-01-21 15:31:52 +02005508 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005509}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005510
5511void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5512{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005513 if (vcpu->arch.time_page) {
5514 kvm_release_page_dirty(vcpu->arch.time_page);
5515 vcpu->arch.time_page = NULL;
5516 }
5517
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005518 kvm_x86_ops->vcpu_free(vcpu);
5519}
5520
5521struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5522 unsigned int id)
5523{
Avi Kivity26e52152007-11-20 15:30:24 +02005524 return kvm_x86_ops->vcpu_create(kvm, id);
5525}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005526
Avi Kivity26e52152007-11-20 15:30:24 +02005527int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5528{
5529 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005530
5531 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005532 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005533
Sheng Yang0bed3b52008-10-09 16:01:54 +08005534 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005535 vcpu_load(vcpu);
5536 r = kvm_arch_vcpu_reset(vcpu);
5537 if (r == 0)
5538 r = kvm_mmu_setup(vcpu);
5539 vcpu_put(vcpu);
5540 if (r < 0)
5541 goto free_vcpu;
5542
Avi Kivity26e52152007-11-20 15:30:24 +02005543 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005544free_vcpu:
5545 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005546 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005547}
5548
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005549void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005550{
5551 vcpu_load(vcpu);
5552 kvm_mmu_unload(vcpu);
5553 vcpu_put(vcpu);
5554
5555 kvm_x86_ops->vcpu_free(vcpu);
5556}
5557
5558int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5559{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005560 vcpu->arch.nmi_pending = false;
5561 vcpu->arch.nmi_injected = false;
5562
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005563 vcpu->arch.switch_db_regs = 0;
5564 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5565 vcpu->arch.dr6 = DR6_FIXED_1;
5566 vcpu->arch.dr7 = DR7_FIXED_1;
5567
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005568 return kvm_x86_ops->vcpu_reset(vcpu);
5569}
5570
Alexander Graf10474ae2009-09-15 11:37:46 +02005571int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005572{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005573 /*
5574 * Since this may be called from a hotplug notifcation,
5575 * we can't get the CPU frequency directly.
5576 */
5577 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5578 int cpu = raw_smp_processor_id();
5579 per_cpu(cpu_tsc_khz, cpu) = 0;
5580 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005581
5582 kvm_shared_msr_cpu_online();
5583
Alexander Graf10474ae2009-09-15 11:37:46 +02005584 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005585}
5586
5587void kvm_arch_hardware_disable(void *garbage)
5588{
5589 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005590 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005591}
5592
5593int kvm_arch_hardware_setup(void)
5594{
5595 return kvm_x86_ops->hardware_setup();
5596}
5597
5598void kvm_arch_hardware_unsetup(void)
5599{
5600 kvm_x86_ops->hardware_unsetup();
5601}
5602
5603void kvm_arch_check_processor_compat(void *rtn)
5604{
5605 kvm_x86_ops->check_processor_compatibility(rtn);
5606}
5607
5608int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5609{
5610 struct page *page;
5611 struct kvm *kvm;
5612 int r;
5613
5614 BUG_ON(vcpu->kvm == NULL);
5615 kvm = vcpu->kvm;
5616
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005617 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005618 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005619 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005620 else
Avi Kivitya4535292008-04-13 17:54:35 +03005621 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005622
5623 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5624 if (!page) {
5625 r = -ENOMEM;
5626 goto fail;
5627 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005628 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005629
5630 r = kvm_mmu_create(vcpu);
5631 if (r < 0)
5632 goto fail_free_pio_data;
5633
5634 if (irqchip_in_kernel(kvm)) {
5635 r = kvm_create_lapic(vcpu);
5636 if (r < 0)
5637 goto fail_mmu_destroy;
5638 }
5639
Huang Ying890ca9a2009-05-11 16:48:15 +08005640 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5641 GFP_KERNEL);
5642 if (!vcpu->arch.mce_banks) {
5643 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005644 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005645 }
5646 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5647
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005648 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005649fail_free_lapic:
5650 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005651fail_mmu_destroy:
5652 kvm_mmu_destroy(vcpu);
5653fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005654 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005655fail:
5656 return r;
5657}
5658
5659void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5660{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005661 int idx;
5662
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005663 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005664 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005665 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005666 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005667 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005668 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005669}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005670
5671struct kvm *kvm_arch_create_vm(void)
5672{
5673 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5674
5675 if (!kvm)
5676 return ERR_PTR(-ENOMEM);
5677
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005678 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5679 if (!kvm->arch.aliases) {
5680 kfree(kvm);
5681 return ERR_PTR(-ENOMEM);
5682 }
5683
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005684 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005685 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005686
Sheng Yang5550af42008-10-15 20:15:06 +08005687 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5688 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5689
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005690 rdtscll(kvm->arch.vm_init_tsc);
5691
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005692 return kvm;
5693}
5694
5695static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5696{
5697 vcpu_load(vcpu);
5698 kvm_mmu_unload(vcpu);
5699 vcpu_put(vcpu);
5700}
5701
5702static void kvm_free_vcpus(struct kvm *kvm)
5703{
5704 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005705 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005706
5707 /*
5708 * Unpin any mmu pages first.
5709 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005710 kvm_for_each_vcpu(i, vcpu, kvm)
5711 kvm_unload_vcpu_mmu(vcpu);
5712 kvm_for_each_vcpu(i, vcpu, kvm)
5713 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005714
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005715 mutex_lock(&kvm->lock);
5716 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5717 kvm->vcpus[i] = NULL;
5718
5719 atomic_set(&kvm->online_vcpus, 0);
5720 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005721}
5722
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005723void kvm_arch_sync_events(struct kvm *kvm)
5724{
Sheng Yangba4cef32009-01-06 10:03:03 +08005725 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005726}
5727
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005728void kvm_arch_destroy_vm(struct kvm *kvm)
5729{
Sheng Yang6eb55812008-10-31 12:37:41 +08005730 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005731 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005732 kfree(kvm->arch.vpic);
5733 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005734 kvm_free_vcpus(kvm);
5735 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005736 if (kvm->arch.apic_access_page)
5737 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005738 if (kvm->arch.ept_identity_pagetable)
5739 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosatti64749202010-01-19 12:45:23 -02005740 cleanup_srcu_struct(&kvm->srcu);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005741 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005742 kfree(kvm);
5743}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005744
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005745int kvm_arch_prepare_memory_region(struct kvm *kvm,
5746 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005747 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005748 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005749 int user_alloc)
5750{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005751 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005752
5753 /*To keep backward compatibility with older userspace,
5754 *x86 needs to hanlde !user_alloc case.
5755 */
5756 if (!user_alloc) {
5757 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005758 unsigned long userspace_addr;
5759
Izik Eidus72dc67a2008-02-10 18:04:15 +02005760 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005761 userspace_addr = do_mmap(NULL, 0,
5762 npages * PAGE_SIZE,
5763 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005764 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005765 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005766 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005767
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005768 if (IS_ERR((void *)userspace_addr))
5769 return PTR_ERR((void *)userspace_addr);
5770
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005771 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005772 }
5773 }
5774
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005775
5776 return 0;
5777}
5778
5779void kvm_arch_commit_memory_region(struct kvm *kvm,
5780 struct kvm_userspace_memory_region *mem,
5781 struct kvm_memory_slot old,
5782 int user_alloc)
5783{
5784
5785 int npages = mem->memory_size >> PAGE_SHIFT;
5786
5787 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5788 int ret;
5789
5790 down_write(&current->mm->mmap_sem);
5791 ret = do_munmap(current->mm, old.userspace_addr,
5792 old.npages * PAGE_SIZE);
5793 up_write(&current->mm->mmap_sem);
5794 if (ret < 0)
5795 printk(KERN_WARNING
5796 "kvm_vm_ioctl_set_memory_region: "
5797 "failed to munmap memory\n");
5798 }
5799
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005800 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005801 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005802 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5803 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5804 }
5805
5806 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005807 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005808}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005809
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005810void kvm_arch_flush_shadow(struct kvm *kvm)
5811{
5812 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005813 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005814}
5815
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005816int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5817{
Avi Kivitya4535292008-04-13 17:54:35 +03005818 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005819 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5820 || vcpu->arch.nmi_pending ||
5821 (kvm_arch_interrupt_allowed(vcpu) &&
5822 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005823}
Zhang Xiantao57361992007-12-17 14:21:40 +08005824
Zhang Xiantao57361992007-12-17 14:21:40 +08005825void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5826{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005827 int me;
5828 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005829
5830 if (waitqueue_active(&vcpu->wq)) {
5831 wake_up_interruptible(&vcpu->wq);
5832 ++vcpu->stat.halt_wakeup;
5833 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005834
5835 me = get_cpu();
5836 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5837 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5838 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005839 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005840}
Gleb Natapov78646122009-03-23 12:12:11 +02005841
5842int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5843{
5844 return kvm_x86_ops->interrupt_allowed(vcpu);
5845}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005846
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005847bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5848{
5849 unsigned long current_rip = kvm_rip_read(vcpu) +
5850 get_segment_base(vcpu, VCPU_SREG_CS);
5851
5852 return current_rip == linear_rip;
5853}
5854EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5855
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005856unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5857{
5858 unsigned long rflags;
5859
5860 rflags = kvm_x86_ops->get_rflags(vcpu);
5861 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01005862 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005863 return rflags;
5864}
5865EXPORT_SYMBOL_GPL(kvm_get_rflags);
5866
5867void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5868{
5869 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005870 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01005871 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005872 kvm_x86_ops->set_rflags(vcpu, rflags);
5873}
5874EXPORT_SYMBOL_GPL(kvm_set_rflags);
5875
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005876EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5877EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5878EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5879EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5880EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005881EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005882EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005883EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005884EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005885EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005886EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01005887EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);