blob: 274a8e39bca7309ec57c48bb01f6ca8df448503c [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>
44#undef TRACE_INCLUDE_FILE
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 Otte5fb76f92007-10-29 16:08:51 +0100226unsigned long segment_base(u16 selector)
227{
Gleb Natapov89a27f42010-02-16 10:51:48 +0200228 struct desc_ptr gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200229 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100230 unsigned long table_base;
231 unsigned long v;
232
233 if (selector == 0)
234 return 0;
235
Akinobu Mitab792c342009-07-19 00:00:01 +0900236 kvm_get_gdt(&gdt);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200237 table_base = gdt.address;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100238
239 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900240 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100241
Carsten Otte5fb76f92007-10-29 16:08:51 +0100242 table_base = segment_base(ldt_selector);
243 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200244 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900245 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100246#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200247 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
248 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100249#endif
250 return v;
251}
252EXPORT_SYMBOL_GPL(segment_base);
253
Carsten Otte6866b832007-10-29 16:09:10 +0100254u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
255{
256 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800257 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100258 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800259 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100260}
261EXPORT_SYMBOL_GPL(kvm_get_apic_base);
262
263void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
264{
265 /* TODO: reserve bits check */
266 if (irqchip_in_kernel(vcpu->kvm))
267 kvm_lapic_set_base(vcpu, data);
268 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800269 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100270}
271EXPORT_SYMBOL_GPL(kvm_set_apic_base);
272
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200273#define EXCPT_BENIGN 0
274#define EXCPT_CONTRIBUTORY 1
275#define EXCPT_PF 2
276
277static int exception_class(int vector)
278{
279 switch (vector) {
280 case PF_VECTOR:
281 return EXCPT_PF;
282 case DE_VECTOR:
283 case TS_VECTOR:
284 case NP_VECTOR:
285 case SS_VECTOR:
286 case GP_VECTOR:
287 return EXCPT_CONTRIBUTORY;
288 default:
289 break;
290 }
291 return EXCPT_BENIGN;
292}
293
294static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
295 unsigned nr, bool has_error, u32 error_code)
296{
297 u32 prev_nr;
298 int class1, class2;
299
300 if (!vcpu->arch.exception.pending) {
301 queue:
302 vcpu->arch.exception.pending = true;
303 vcpu->arch.exception.has_error_code = has_error;
304 vcpu->arch.exception.nr = nr;
305 vcpu->arch.exception.error_code = error_code;
306 return;
307 }
308
309 /* to check exception */
310 prev_nr = vcpu->arch.exception.nr;
311 if (prev_nr == DF_VECTOR) {
312 /* triple fault -> shutdown */
313 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
314 return;
315 }
316 class1 = exception_class(prev_nr);
317 class2 = exception_class(nr);
318 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
319 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
320 /* generate double fault per SDM Table 5-5 */
321 vcpu->arch.exception.pending = true;
322 vcpu->arch.exception.has_error_code = true;
323 vcpu->arch.exception.nr = DF_VECTOR;
324 vcpu->arch.exception.error_code = 0;
325 } else
326 /* replace previous exception with a new one in a hope
327 that instruction re-execution will regenerate lost
328 exception */
329 goto queue;
330}
331
Avi Kivity298101d2007-11-25 13:41:11 +0200332void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
333{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200334 kvm_multiple_exception(vcpu, nr, false, 0);
Avi Kivity298101d2007-11-25 13:41:11 +0200335}
336EXPORT_SYMBOL_GPL(kvm_queue_exception);
337
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200338void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
339 u32 error_code)
340{
341 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200343 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
344}
345
Sheng Yang3419ffc2008-05-15 09:52:48 +0800346void kvm_inject_nmi(struct kvm_vcpu *vcpu)
347{
348 vcpu->arch.nmi_pending = 1;
349}
350EXPORT_SYMBOL_GPL(kvm_inject_nmi);
351
Avi Kivity298101d2007-11-25 13:41:11 +0200352void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
353{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200354 kvm_multiple_exception(vcpu, nr, true, error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200355}
356EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
357
Carsten Ottea03490e2007-10-29 16:09:35 +0100358/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300359 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
360 * a #GP and return false.
361 */
362bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200363{
Avi Kivity0a79b002009-09-01 12:03:25 +0300364 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
365 return true;
366 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
367 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100368}
Avi Kivity0a79b002009-09-01 12:03:25 +0300369EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100370
371/*
372 * Load the pae pdptrs. Return true is they are all valid.
373 */
374int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
375{
376 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
377 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
378 int i;
379 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800380 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100381
Carsten Ottea03490e2007-10-29 16:09:35 +0100382 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
383 offset * sizeof(u64), sizeof(pdpte));
384 if (ret < 0) {
385 ret = 0;
386 goto out;
387 }
388 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300389 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800390 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100391 ret = 0;
392 goto out;
393 }
394 }
395 ret = 1;
396
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800397 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300398 __set_bit(VCPU_EXREG_PDPTR,
399 (unsigned long *)&vcpu->arch.regs_avail);
400 __set_bit(VCPU_EXREG_PDPTR,
401 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100402out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100403
404 return ret;
405}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100406EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100407
Avi Kivityd835dfe2007-11-21 02:57:59 +0200408static bool pdptrs_changed(struct kvm_vcpu *vcpu)
409{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800410 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200411 bool changed = true;
412 int r;
413
414 if (is_long_mode(vcpu) || !is_pae(vcpu))
415 return false;
416
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300417 if (!test_bit(VCPU_EXREG_PDPTR,
418 (unsigned long *)&vcpu->arch.regs_avail))
419 return true;
420
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800421 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200422 if (r < 0)
423 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800424 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200425out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200426
427 return changed;
428}
429
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200430void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100431{
Avi Kivityf9a48e62010-01-06 19:10:22 +0200432 cr0 |= X86_CR0_ET;
433
Gleb Natapovab344822010-01-21 15:28:46 +0200434#ifdef CONFIG_X86_64
435 if (cr0 & 0xffffffff00000000UL) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200436 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100437 return;
438 }
Gleb Natapovab344822010-01-21 15:28:46 +0200439#endif
440
441 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100442
443 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200444 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100445 return;
446 }
447
448 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200449 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100450 return;
451 }
452
453 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
454#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200455 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100456 int cs_db, cs_l;
457
458 if (!is_pae(vcpu)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200459 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100460 return;
461 }
462 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
463 if (cs_l) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200464 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100465 return;
466
467 }
468 } else
469#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800470 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200471 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100472 return;
473 }
474
475 }
476
477 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800478 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100479
Carsten Ottea03490e2007-10-29 16:09:35 +0100480 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100481 return;
482}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200483EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100484
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200485void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100486{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200487 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100488}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200489EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100490
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200491void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100492{
Avi Kivityfc78f512009-12-07 12:16:48 +0200493 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300494 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
495
Carsten Ottea03490e2007-10-29 16:09:35 +0100496 if (cr4 & CR4_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200497 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100498 return;
499 }
500
501 if (is_long_mode(vcpu)) {
502 if (!(cr4 & X86_CR4_PAE)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200503 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100504 return;
505 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300506 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
507 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800508 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200509 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100510 return;
511 }
512
513 if (cr4 & X86_CR4_VMXE) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200514 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100515 return;
516 }
517 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800518 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200519 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100520 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100521}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200522EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100523
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200524void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100525{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800526 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300527 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200528 kvm_mmu_flush_tlb(vcpu);
529 return;
530 }
531
Carsten Ottea03490e2007-10-29 16:09:35 +0100532 if (is_long_mode(vcpu)) {
533 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200534 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100535 return;
536 }
537 } else {
538 if (is_pae(vcpu)) {
539 if (cr3 & CR3_PAE_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200540 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100541 return;
542 }
543 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200544 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100545 return;
546 }
547 }
548 /*
549 * We don't check reserved bits in nonpae mode, because
550 * this isn't enforced, and VMware depends on this.
551 */
552 }
553
Carsten Ottea03490e2007-10-29 16:09:35 +0100554 /*
555 * Does the new cr3 value map to physical memory? (Note, we
556 * catch an invalid cr3 even in real-mode, because it would
557 * cause trouble later on when we turn on paging anyway.)
558 *
559 * A real CPU would silently accept an invalid cr3 and would
560 * attempt to use it - with largely undefined (and often hard
561 * to debug) behavior on the guest side.
562 */
563 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200564 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100565 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800566 vcpu->arch.cr3 = cr3;
567 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100568 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100569}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200570EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100571
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200572void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100573{
574 if (cr8 & CR8_RESERVED_BITS) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200575 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100576 return;
577 }
578 if (irqchip_in_kernel(vcpu->kvm))
579 kvm_lapic_set_tpr(vcpu, cr8);
580 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800581 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100582}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200583EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100584
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200585unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100586{
587 if (irqchip_in_kernel(vcpu->kvm))
588 return kvm_lapic_get_cr8(vcpu);
589 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800590 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100591}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200592EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100593
Alexander Grafd8017472008-11-25 20:17:11 +0100594static inline u32 bit(int bitno)
595{
596 return 1 << (bitno & 31);
597}
598
Carsten Otte043405e2007-10-10 17:16:19 +0200599/*
600 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
601 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
602 *
603 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400604 * capabilities of the host cpu. This capabilities test skips MSRs that are
605 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200606 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400607
Gleb Natapov10388a02010-01-17 15:51:23 +0200608#define KVM_SAVE_MSRS_BEGIN 5
Carsten Otte043405e2007-10-10 17:16:19 +0200609static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400610 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200611 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200612 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200613 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
614 MSR_K6_STAR,
615#ifdef CONFIG_X86_64
616 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
617#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400618 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200619};
620
621static unsigned num_msrs_to_save;
622
623static u32 emulated_msrs[] = {
624 MSR_IA32_MISC_ENABLE,
625};
626
Carsten Otte15c4a642007-10-30 18:44:17 +0100627static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
628{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100629 if (efer & efer_reserved_bits) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200630 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100631 return;
632 }
633
634 if (is_paging(vcpu)
Avi Kivityf6801df2010-01-21 15:31:50 +0200635 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200636 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100637 return;
638 }
639
Alexander Graf1b2fd702009-02-02 16:23:51 +0100640 if (efer & EFER_FFXSR) {
641 struct kvm_cpuid_entry2 *feat;
642
643 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
644 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
Alexander Graf1b2fd702009-02-02 16:23:51 +0100645 kvm_inject_gp(vcpu, 0);
646 return;
647 }
648 }
649
Alexander Grafd8017472008-11-25 20:17:11 +0100650 if (efer & EFER_SVME) {
651 struct kvm_cpuid_entry2 *feat;
652
653 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
654 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
Alexander Grafd8017472008-11-25 20:17:11 +0100655 kvm_inject_gp(vcpu, 0);
656 return;
657 }
658 }
659
Carsten Otte15c4a642007-10-30 18:44:17 +0100660 kvm_x86_ops->set_efer(vcpu, efer);
661
662 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200663 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100664
Avi Kivityf6801df2010-01-21 15:31:50 +0200665 vcpu->arch.efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300666
667 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
668 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100669}
670
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100671void kvm_enable_efer_bits(u64 mask)
672{
673 efer_reserved_bits &= ~mask;
674}
675EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
676
677
Carsten Otte15c4a642007-10-30 18:44:17 +0100678/*
679 * Writes msr value into into the appropriate "register".
680 * Returns 0 on success, non-0 otherwise.
681 * Assumes vcpu_load() was already called.
682 */
683int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
684{
685 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
686}
687
Carsten Otte313a3dc2007-10-11 19:16:52 +0200688/*
689 * Adapt set_msr() to msr_io()'s calling convention
690 */
691static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
692{
693 return kvm_set_msr(vcpu, index, *data);
694}
695
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200696static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
697{
698 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200699 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800700 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200701
702 if (!wall_clock)
703 return;
704
705 version++;
706
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200707 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
708
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200709 /*
710 * The guest calculates current wall clock time by adding
711 * system time (updated by kvm_write_guest_time below) to the
712 * wall clock specified here. guest system time equals host
713 * system time for us, thus we must fill in host boot time here.
714 */
Jason Wang923de3c2010-01-27 19:13:49 +0800715 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200716
717 wc.sec = boot.tv_sec;
718 wc.nsec = boot.tv_nsec;
719 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200720
721 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
722
723 version++;
724 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200725}
726
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200727static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
728{
729 uint32_t quotient, remainder;
730
731 /* Don't try to replace with do_div(), this one calculates
732 * "(dividend << 32) / divisor" */
733 __asm__ ( "divl %4"
734 : "=a" (quotient), "=d" (remainder)
735 : "0" (0), "1" (dividend), "r" (divisor) );
736 return quotient;
737}
738
739static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
740{
741 uint64_t nsecs = 1000000000LL;
742 int32_t shift = 0;
743 uint64_t tps64;
744 uint32_t tps32;
745
746 tps64 = tsc_khz * 1000LL;
747 while (tps64 > nsecs*2) {
748 tps64 >>= 1;
749 shift--;
750 }
751
752 tps32 = (uint32_t)tps64;
753 while (tps32 <= (uint32_t)nsecs) {
754 tps32 <<= 1;
755 shift++;
756 }
757
758 hv_clock->tsc_shift = shift;
759 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
760
761 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700762 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200763 hv_clock->tsc_to_system_mul);
764}
765
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100766static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
767
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200768static void kvm_write_guest_time(struct kvm_vcpu *v)
769{
770 struct timespec ts;
771 unsigned long flags;
772 struct kvm_vcpu_arch *vcpu = &v->arch;
773 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300774 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200775
776 if ((!vcpu->time_page))
777 return;
778
Avi Kivity463656c2009-04-12 15:49:07 +0300779 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
780 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
781 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
782 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200783 }
Avi Kivity463656c2009-04-12 15:49:07 +0300784 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200785
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200786 /* Keep irq disabled to prevent changes to the clock */
787 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530788 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200789 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800790 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200791 local_irq_restore(flags);
792
793 /* With all the info we got, fill in the values */
794
795 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400796 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
797
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200798 /*
799 * The interface expects us to write an even number signaling that the
800 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200801 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200802 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200803 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200804
805 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
806
807 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200808 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200809
810 kunmap_atomic(shared_kaddr, KM_USER0);
811
812 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
813}
814
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100815static int kvm_request_guest_time_update(struct kvm_vcpu *v)
816{
817 struct kvm_vcpu_arch *vcpu = &v->arch;
818
819 if (!vcpu->time_page)
820 return 0;
821 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
822 return 1;
823}
824
Avi Kivity9ba075a2008-05-26 20:06:35 +0300825static bool msr_mtrr_valid(unsigned msr)
826{
827 switch (msr) {
828 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
829 case MSR_MTRRfix64K_00000:
830 case MSR_MTRRfix16K_80000:
831 case MSR_MTRRfix16K_A0000:
832 case MSR_MTRRfix4K_C0000:
833 case MSR_MTRRfix4K_C8000:
834 case MSR_MTRRfix4K_D0000:
835 case MSR_MTRRfix4K_D8000:
836 case MSR_MTRRfix4K_E0000:
837 case MSR_MTRRfix4K_E8000:
838 case MSR_MTRRfix4K_F0000:
839 case MSR_MTRRfix4K_F8000:
840 case MSR_MTRRdefType:
841 case MSR_IA32_CR_PAT:
842 return true;
843 case 0x2f8:
844 return true;
845 }
846 return false;
847}
848
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300849static bool valid_pat_type(unsigned t)
850{
851 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
852}
853
854static bool valid_mtrr_type(unsigned t)
855{
856 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
857}
858
859static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
860{
861 int i;
862
863 if (!msr_mtrr_valid(msr))
864 return false;
865
866 if (msr == MSR_IA32_CR_PAT) {
867 for (i = 0; i < 8; i++)
868 if (!valid_pat_type((data >> (i * 8)) & 0xff))
869 return false;
870 return true;
871 } else if (msr == MSR_MTRRdefType) {
872 if (data & ~0xcff)
873 return false;
874 return valid_mtrr_type(data & 0xff);
875 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
876 for (i = 0; i < 8 ; i++)
877 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
878 return false;
879 return true;
880 }
881
882 /* variable MTRRs */
883 return valid_mtrr_type(data & 0xff);
884}
885
Avi Kivity9ba075a2008-05-26 20:06:35 +0300886static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
887{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800888 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
889
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300890 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300891 return 1;
892
Sheng Yang0bed3b52008-10-09 16:01:54 +0800893 if (msr == MSR_MTRRdefType) {
894 vcpu->arch.mtrr_state.def_type = data;
895 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
896 } else if (msr == MSR_MTRRfix64K_00000)
897 p[0] = data;
898 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
899 p[1 + msr - MSR_MTRRfix16K_80000] = data;
900 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
901 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
902 else if (msr == MSR_IA32_CR_PAT)
903 vcpu->arch.pat = data;
904 else { /* Variable MTRRs */
905 int idx, is_mtrr_mask;
906 u64 *pt;
907
908 idx = (msr - 0x200) / 2;
909 is_mtrr_mask = msr - 0x200 - 2 * idx;
910 if (!is_mtrr_mask)
911 pt =
912 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
913 else
914 pt =
915 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
916 *pt = data;
917 }
918
919 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300920 return 0;
921}
Carsten Otte15c4a642007-10-30 18:44:17 +0100922
Huang Ying890ca9a2009-05-11 16:48:15 +0800923static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
924{
925 u64 mcg_cap = vcpu->arch.mcg_cap;
926 unsigned bank_num = mcg_cap & 0xff;
927
928 switch (msr) {
929 case MSR_IA32_MCG_STATUS:
930 vcpu->arch.mcg_status = data;
931 break;
932 case MSR_IA32_MCG_CTL:
933 if (!(mcg_cap & MCG_CTL_P))
934 return 1;
935 if (data != 0 && data != ~(u64)0)
936 return -1;
937 vcpu->arch.mcg_ctl = data;
938 break;
939 default:
940 if (msr >= MSR_IA32_MC0_CTL &&
941 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
942 u32 offset = msr - MSR_IA32_MC0_CTL;
Andre Przywara114be422010-03-24 17:46:42 +0100943 /* only 0 or all 1s can be written to IA32_MCi_CTL
944 * some Linux kernels though clear bit 10 in bank 4 to
945 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
946 * this to avoid an uncatched #GP in the guest
947 */
Huang Ying890ca9a2009-05-11 16:48:15 +0800948 if ((offset & 0x3) == 0 &&
Andre Przywara114be422010-03-24 17:46:42 +0100949 data != 0 && (data | (1 << 10)) != ~(u64)0)
Huang Ying890ca9a2009-05-11 16:48:15 +0800950 return -1;
951 vcpu->arch.mce_banks[offset] = data;
952 break;
953 }
954 return 1;
955 }
956 return 0;
957}
958
Ed Swierkffde22a2009-10-15 15:21:43 -0700959static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
960{
961 struct kvm *kvm = vcpu->kvm;
962 int lm = is_long_mode(vcpu);
963 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
964 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
965 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
966 : kvm->arch.xen_hvm_config.blob_size_32;
967 u32 page_num = data & ~PAGE_MASK;
968 u64 page_addr = data & PAGE_MASK;
969 u8 *page;
970 int r;
971
972 r = -E2BIG;
973 if (page_num >= blob_size)
974 goto out;
975 r = -ENOMEM;
976 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
977 if (!page)
978 goto out;
979 r = -EFAULT;
980 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
981 goto out_free;
982 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
983 goto out_free;
984 r = 0;
985out_free:
986 kfree(page);
987out:
988 return r;
989}
990
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200991static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
992{
993 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
994}
995
996static bool kvm_hv_msr_partition_wide(u32 msr)
997{
998 bool r = false;
999 switch (msr) {
1000 case HV_X64_MSR_GUEST_OS_ID:
1001 case HV_X64_MSR_HYPERCALL:
1002 r = true;
1003 break;
1004 }
1005
1006 return r;
1007}
1008
1009static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1010{
1011 struct kvm *kvm = vcpu->kvm;
1012
1013 switch (msr) {
1014 case HV_X64_MSR_GUEST_OS_ID:
1015 kvm->arch.hv_guest_os_id = data;
1016 /* setting guest os id to zero disables hypercall page */
1017 if (!kvm->arch.hv_guest_os_id)
1018 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1019 break;
1020 case HV_X64_MSR_HYPERCALL: {
1021 u64 gfn;
1022 unsigned long addr;
1023 u8 instructions[4];
1024
1025 /* if guest os id is not set hypercall should remain disabled */
1026 if (!kvm->arch.hv_guest_os_id)
1027 break;
1028 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1029 kvm->arch.hv_hypercall = data;
1030 break;
1031 }
1032 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1033 addr = gfn_to_hva(kvm, gfn);
1034 if (kvm_is_error_hva(addr))
1035 return 1;
1036 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1037 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1038 if (copy_to_user((void __user *)addr, instructions, 4))
1039 return 1;
1040 kvm->arch.hv_hypercall = data;
1041 break;
1042 }
1043 default:
1044 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1045 "data 0x%llx\n", msr, data);
1046 return 1;
1047 }
1048 return 0;
1049}
1050
1051static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1052{
Gleb Natapov10388a02010-01-17 15:51:23 +02001053 switch (msr) {
1054 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1055 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001056
Gleb Natapov10388a02010-01-17 15:51:23 +02001057 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1058 vcpu->arch.hv_vapic = data;
1059 break;
1060 }
1061 addr = gfn_to_hva(vcpu->kvm, data >>
1062 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1063 if (kvm_is_error_hva(addr))
1064 return 1;
1065 if (clear_user((void __user *)addr, PAGE_SIZE))
1066 return 1;
1067 vcpu->arch.hv_vapic = data;
1068 break;
1069 }
1070 case HV_X64_MSR_EOI:
1071 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1072 case HV_X64_MSR_ICR:
1073 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1074 case HV_X64_MSR_TPR:
1075 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1076 default:
1077 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1078 "data 0x%llx\n", msr, data);
1079 return 1;
1080 }
1081
1082 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001083}
1084
Carsten Otte15c4a642007-10-30 18:44:17 +01001085int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1086{
1087 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001088 case MSR_EFER:
1089 set_efer(vcpu, data);
1090 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +02001091 case MSR_K7_HWCR:
1092 data &= ~(u64)0x40; /* ignore flush filter disable */
1093 if (data != 0) {
1094 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1095 data);
1096 return 1;
1097 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001098 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001099 case MSR_FAM10H_MMIO_CONF_BASE:
1100 if (data != 0) {
1101 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1102 "0x%llx\n", data);
1103 return 1;
1104 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001105 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001106 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001107 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001108 case MSR_IA32_DEBUGCTLMSR:
1109 if (!data) {
1110 /* We support the non-activated case already */
1111 break;
1112 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1113 /* Values other than LBR and BTF are vendor-specific,
1114 thus reserved and should throw a #GP */
1115 return 1;
1116 }
1117 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1118 __func__, data);
1119 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001120 case MSR_IA32_UCODE_REV:
1121 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001122 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001123 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001124 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001125 case 0x200 ... 0x2ff:
1126 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001127 case MSR_IA32_APICBASE:
1128 kvm_set_apic_base(vcpu, data);
1129 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001130 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1131 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001132 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001133 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001134 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001135 case MSR_KVM_WALL_CLOCK:
1136 vcpu->kvm->arch.wall_clock = data;
1137 kvm_write_wall_clock(vcpu->kvm, data);
1138 break;
1139 case MSR_KVM_SYSTEM_TIME: {
1140 if (vcpu->arch.time_page) {
1141 kvm_release_page_dirty(vcpu->arch.time_page);
1142 vcpu->arch.time_page = NULL;
1143 }
1144
1145 vcpu->arch.time = data;
1146
1147 /* we verify if the enable bit is set... */
1148 if (!(data & 1))
1149 break;
1150
1151 /* ...but clean it before doing the actual write */
1152 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1153
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001154 vcpu->arch.time_page =
1155 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001156
1157 if (is_error_page(vcpu->arch.time_page)) {
1158 kvm_release_page_clean(vcpu->arch.time_page);
1159 vcpu->arch.time_page = NULL;
1160 }
1161
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001162 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001163 break;
1164 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001165 case MSR_IA32_MCG_CTL:
1166 case MSR_IA32_MCG_STATUS:
1167 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1168 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001169
1170 /* Performance counters are not protected by a CPUID bit,
1171 * so we should check all of them in the generic path for the sake of
1172 * cross vendor migration.
1173 * Writing a zero into the event select MSRs disables them,
1174 * which we perfectly emulate ;-). Any other value should be at least
1175 * reported, some guests depend on them.
1176 */
1177 case MSR_P6_EVNTSEL0:
1178 case MSR_P6_EVNTSEL1:
1179 case MSR_K7_EVNTSEL0:
1180 case MSR_K7_EVNTSEL1:
1181 case MSR_K7_EVNTSEL2:
1182 case MSR_K7_EVNTSEL3:
1183 if (data != 0)
1184 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1185 "0x%x data 0x%llx\n", msr, data);
1186 break;
1187 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1188 * so we ignore writes to make it happy.
1189 */
1190 case MSR_P6_PERFCTR0:
1191 case MSR_P6_PERFCTR1:
1192 case MSR_K7_PERFCTR0:
1193 case MSR_K7_PERFCTR1:
1194 case MSR_K7_PERFCTR2:
1195 case MSR_K7_PERFCTR3:
1196 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1197 "0x%x data 0x%llx\n", msr, data);
1198 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001199 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1200 if (kvm_hv_msr_partition_wide(msr)) {
1201 int r;
1202 mutex_lock(&vcpu->kvm->lock);
1203 r = set_msr_hyperv_pw(vcpu, msr, data);
1204 mutex_unlock(&vcpu->kvm->lock);
1205 return r;
1206 } else
1207 return set_msr_hyperv(vcpu, msr, data);
1208 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001209 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001210 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1211 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001212 if (!ignore_msrs) {
1213 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1214 msr, data);
1215 return 1;
1216 } else {
1217 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1218 msr, data);
1219 break;
1220 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001221 }
1222 return 0;
1223}
1224EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1225
1226
1227/*
1228 * Reads an msr value (of 'msr_index') into 'pdata'.
1229 * Returns 0 on success, non-0 otherwise.
1230 * Assumes vcpu_load() was already called.
1231 */
1232int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1233{
1234 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1235}
1236
Avi Kivity9ba075a2008-05-26 20:06:35 +03001237static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1238{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001239 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1240
Avi Kivity9ba075a2008-05-26 20:06:35 +03001241 if (!msr_mtrr_valid(msr))
1242 return 1;
1243
Sheng Yang0bed3b52008-10-09 16:01:54 +08001244 if (msr == MSR_MTRRdefType)
1245 *pdata = vcpu->arch.mtrr_state.def_type +
1246 (vcpu->arch.mtrr_state.enabled << 10);
1247 else if (msr == MSR_MTRRfix64K_00000)
1248 *pdata = p[0];
1249 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1250 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1251 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1252 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1253 else if (msr == MSR_IA32_CR_PAT)
1254 *pdata = vcpu->arch.pat;
1255 else { /* Variable MTRRs */
1256 int idx, is_mtrr_mask;
1257 u64 *pt;
1258
1259 idx = (msr - 0x200) / 2;
1260 is_mtrr_mask = msr - 0x200 - 2 * idx;
1261 if (!is_mtrr_mask)
1262 pt =
1263 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1264 else
1265 pt =
1266 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1267 *pdata = *pt;
1268 }
1269
Avi Kivity9ba075a2008-05-26 20:06:35 +03001270 return 0;
1271}
1272
Huang Ying890ca9a2009-05-11 16:48:15 +08001273static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1274{
1275 u64 data;
1276 u64 mcg_cap = vcpu->arch.mcg_cap;
1277 unsigned bank_num = mcg_cap & 0xff;
1278
1279 switch (msr) {
1280 case MSR_IA32_P5_MC_ADDR:
1281 case MSR_IA32_P5_MC_TYPE:
1282 data = 0;
1283 break;
1284 case MSR_IA32_MCG_CAP:
1285 data = vcpu->arch.mcg_cap;
1286 break;
1287 case MSR_IA32_MCG_CTL:
1288 if (!(mcg_cap & MCG_CTL_P))
1289 return 1;
1290 data = vcpu->arch.mcg_ctl;
1291 break;
1292 case MSR_IA32_MCG_STATUS:
1293 data = vcpu->arch.mcg_status;
1294 break;
1295 default:
1296 if (msr >= MSR_IA32_MC0_CTL &&
1297 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1298 u32 offset = msr - MSR_IA32_MC0_CTL;
1299 data = vcpu->arch.mce_banks[offset];
1300 break;
1301 }
1302 return 1;
1303 }
1304 *pdata = data;
1305 return 0;
1306}
1307
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001308static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1309{
1310 u64 data = 0;
1311 struct kvm *kvm = vcpu->kvm;
1312
1313 switch (msr) {
1314 case HV_X64_MSR_GUEST_OS_ID:
1315 data = kvm->arch.hv_guest_os_id;
1316 break;
1317 case HV_X64_MSR_HYPERCALL:
1318 data = kvm->arch.hv_hypercall;
1319 break;
1320 default:
1321 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1322 return 1;
1323 }
1324
1325 *pdata = data;
1326 return 0;
1327}
1328
1329static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1330{
1331 u64 data = 0;
1332
1333 switch (msr) {
1334 case HV_X64_MSR_VP_INDEX: {
1335 int r;
1336 struct kvm_vcpu *v;
1337 kvm_for_each_vcpu(r, v, vcpu->kvm)
1338 if (v == vcpu)
1339 data = r;
1340 break;
1341 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001342 case HV_X64_MSR_EOI:
1343 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1344 case HV_X64_MSR_ICR:
1345 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1346 case HV_X64_MSR_TPR:
1347 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001348 default:
1349 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1350 return 1;
1351 }
1352 *pdata = data;
1353 return 0;
1354}
1355
Carsten Otte15c4a642007-10-30 18:44:17 +01001356int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1357{
1358 u64 data;
1359
1360 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001361 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001362 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001363 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001364 case MSR_IA32_DEBUGCTLMSR:
1365 case MSR_IA32_LASTBRANCHFROMIP:
1366 case MSR_IA32_LASTBRANCHTOIP:
1367 case MSR_IA32_LASTINTFROMIP:
1368 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301369 case MSR_K8_SYSCFG:
1370 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001371 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301372 case MSR_P6_PERFCTR0:
1373 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301374 case MSR_P6_EVNTSEL0:
1375 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301376 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301377 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001378 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001379 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001380 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001381 data = 0;
1382 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001383 case MSR_MTRRcap:
1384 data = 0x500 | KVM_NR_VAR_MTRR;
1385 break;
1386 case 0x200 ... 0x2ff:
1387 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001388 case 0xcd: /* fsb frequency */
1389 data = 3;
1390 break;
1391 case MSR_IA32_APICBASE:
1392 data = kvm_get_apic_base(vcpu);
1393 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001394 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1395 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1396 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001397 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001398 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001399 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001400 case MSR_IA32_PERF_STATUS:
1401 /* TSC increment by tick */
1402 data = 1000ULL;
1403 /* CPU multiplier */
1404 data |= (((uint64_t)4ULL) << 40);
1405 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001406 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001407 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001408 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001409 case MSR_KVM_WALL_CLOCK:
1410 data = vcpu->kvm->arch.wall_clock;
1411 break;
1412 case MSR_KVM_SYSTEM_TIME:
1413 data = vcpu->arch.time;
1414 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001415 case MSR_IA32_P5_MC_ADDR:
1416 case MSR_IA32_P5_MC_TYPE:
1417 case MSR_IA32_MCG_CAP:
1418 case MSR_IA32_MCG_CTL:
1419 case MSR_IA32_MCG_STATUS:
1420 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1421 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001422 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1423 if (kvm_hv_msr_partition_wide(msr)) {
1424 int r;
1425 mutex_lock(&vcpu->kvm->lock);
1426 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1427 mutex_unlock(&vcpu->kvm->lock);
1428 return r;
1429 } else
1430 return get_msr_hyperv(vcpu, msr, pdata);
1431 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001432 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001433 if (!ignore_msrs) {
1434 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1435 return 1;
1436 } else {
1437 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1438 data = 0;
1439 }
1440 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001441 }
1442 *pdata = data;
1443 return 0;
1444}
1445EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1446
Carsten Otte313a3dc2007-10-11 19:16:52 +02001447/*
1448 * Read or write a bunch of msrs. All parameters are kernel addresses.
1449 *
1450 * @return number of msrs set successfully.
1451 */
1452static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1453 struct kvm_msr_entry *entries,
1454 int (*do_msr)(struct kvm_vcpu *vcpu,
1455 unsigned index, u64 *data))
1456{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001457 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001458
1459 vcpu_load(vcpu);
1460
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001461 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001462 for (i = 0; i < msrs->nmsrs; ++i)
1463 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1464 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001465 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001466
1467 vcpu_put(vcpu);
1468
1469 return i;
1470}
1471
1472/*
1473 * Read or write a bunch of msrs. Parameters are user addresses.
1474 *
1475 * @return number of msrs set successfully.
1476 */
1477static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1478 int (*do_msr)(struct kvm_vcpu *vcpu,
1479 unsigned index, u64 *data),
1480 int writeback)
1481{
1482 struct kvm_msrs msrs;
1483 struct kvm_msr_entry *entries;
1484 int r, n;
1485 unsigned size;
1486
1487 r = -EFAULT;
1488 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1489 goto out;
1490
1491 r = -E2BIG;
1492 if (msrs.nmsrs >= MAX_IO_MSRS)
1493 goto out;
1494
1495 r = -ENOMEM;
1496 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1497 entries = vmalloc(size);
1498 if (!entries)
1499 goto out;
1500
1501 r = -EFAULT;
1502 if (copy_from_user(entries, user_msrs->entries, size))
1503 goto out_free;
1504
1505 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1506 if (r < 0)
1507 goto out_free;
1508
1509 r = -EFAULT;
1510 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1511 goto out_free;
1512
1513 r = n;
1514
1515out_free:
1516 vfree(entries);
1517out:
1518 return r;
1519}
1520
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001521int kvm_dev_ioctl_check_extension(long ext)
1522{
1523 int r;
1524
1525 switch (ext) {
1526 case KVM_CAP_IRQCHIP:
1527 case KVM_CAP_HLT:
1528 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001529 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001530 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001531 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001532 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001533 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001534 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001535 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001536 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001537 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001538 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001539 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001540 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001541 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001542 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001543 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001544 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001545 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001546 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001547 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001548 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001549 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001550 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001551 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001552 r = 1;
1553 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001554 case KVM_CAP_COALESCED_MMIO:
1555 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1556 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001557 case KVM_CAP_VAPIC:
1558 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1559 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001560 case KVM_CAP_NR_VCPUS:
1561 r = KVM_MAX_VCPUS;
1562 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001563 case KVM_CAP_NR_MEMSLOTS:
1564 r = KVM_MEMORY_SLOTS;
1565 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001566 case KVM_CAP_PV_MMU: /* obsolete */
1567 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001568 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001569 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001570 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001571 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001572 case KVM_CAP_MCE:
1573 r = KVM_MAX_MCE_BANKS;
1574 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001575 default:
1576 r = 0;
1577 break;
1578 }
1579 return r;
1580
1581}
1582
Carsten Otte043405e2007-10-10 17:16:19 +02001583long kvm_arch_dev_ioctl(struct file *filp,
1584 unsigned int ioctl, unsigned long arg)
1585{
1586 void __user *argp = (void __user *)arg;
1587 long r;
1588
1589 switch (ioctl) {
1590 case KVM_GET_MSR_INDEX_LIST: {
1591 struct kvm_msr_list __user *user_msr_list = argp;
1592 struct kvm_msr_list msr_list;
1593 unsigned n;
1594
1595 r = -EFAULT;
1596 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1597 goto out;
1598 n = msr_list.nmsrs;
1599 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1600 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1601 goto out;
1602 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001603 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001604 goto out;
1605 r = -EFAULT;
1606 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1607 num_msrs_to_save * sizeof(u32)))
1608 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001609 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001610 &emulated_msrs,
1611 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1612 goto out;
1613 r = 0;
1614 break;
1615 }
Avi Kivity674eea02008-02-11 18:37:23 +02001616 case KVM_GET_SUPPORTED_CPUID: {
1617 struct kvm_cpuid2 __user *cpuid_arg = argp;
1618 struct kvm_cpuid2 cpuid;
1619
1620 r = -EFAULT;
1621 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1622 goto out;
1623 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001624 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001625 if (r)
1626 goto out;
1627
1628 r = -EFAULT;
1629 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1630 goto out;
1631 r = 0;
1632 break;
1633 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001634 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1635 u64 mce_cap;
1636
1637 mce_cap = KVM_MCE_CAP_SUPPORTED;
1638 r = -EFAULT;
1639 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1640 goto out;
1641 r = 0;
1642 break;
1643 }
Carsten Otte043405e2007-10-10 17:16:19 +02001644 default:
1645 r = -EINVAL;
1646 }
1647out:
1648 return r;
1649}
1650
Carsten Otte313a3dc2007-10-11 19:16:52 +02001651void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1652{
1653 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001654 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1655 unsigned long khz = cpufreq_quick_get(cpu);
1656 if (!khz)
1657 khz = tsc_khz;
1658 per_cpu(cpu_tsc_khz, cpu) = khz;
1659 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001660 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001661}
1662
1663void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1664{
Amit Shah9327fd12007-11-15 18:38:46 +02001665 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001666 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001667}
1668
Dan Kenigsberg07716712007-11-21 17:10:04 +02001669static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001670{
Avi Kivitye286e862009-05-03 18:50:55 +03001671 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001672
Avi Kivitye286e862009-05-03 18:50:55 +03001673 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001674 return efer & EFER_NX;
1675}
1676
1677static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1678{
1679 int i;
1680 struct kvm_cpuid_entry2 *e, *entry;
1681
Carsten Otte313a3dc2007-10-11 19:16:52 +02001682 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001683 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1684 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001685 if (e->function == 0x80000001) {
1686 entry = e;
1687 break;
1688 }
1689 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001690 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001691 entry->edx &= ~(1 << 20);
1692 printk(KERN_INFO "kvm: guest NX capability removed\n");
1693 }
1694}
1695
Dan Kenigsberg07716712007-11-21 17:10:04 +02001696/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001697static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1698 struct kvm_cpuid *cpuid,
1699 struct kvm_cpuid_entry __user *entries)
1700{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001701 int r, i;
1702 struct kvm_cpuid_entry *cpuid_entries;
1703
1704 r = -E2BIG;
1705 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1706 goto out;
1707 r = -ENOMEM;
1708 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1709 if (!cpuid_entries)
1710 goto out;
1711 r = -EFAULT;
1712 if (copy_from_user(cpuid_entries, entries,
1713 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1714 goto out_free;
1715 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001716 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1717 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1718 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1719 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1720 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1721 vcpu->arch.cpuid_entries[i].index = 0;
1722 vcpu->arch.cpuid_entries[i].flags = 0;
1723 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1724 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1725 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001726 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001727 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001728 cpuid_fix_nx_cap(vcpu);
1729 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001730 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001731 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001732
1733out_free:
1734 vfree(cpuid_entries);
1735out:
1736 return r;
1737}
1738
1739static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001740 struct kvm_cpuid2 *cpuid,
1741 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001742{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001743 int r;
1744
1745 r = -E2BIG;
1746 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1747 goto out;
1748 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001749 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001750 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001751 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001752 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001753 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001754 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001755 return 0;
1756
1757out:
1758 return r;
1759}
1760
Dan Kenigsberg07716712007-11-21 17:10:04 +02001761static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001762 struct kvm_cpuid2 *cpuid,
1763 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001764{
1765 int r;
1766
1767 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001768 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001769 goto out;
1770 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001771 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001772 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001773 goto out;
1774 return 0;
1775
1776out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001777 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001778 return r;
1779}
1780
Dan Kenigsberg07716712007-11-21 17:10:04 +02001781static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001782 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001783{
1784 entry->function = function;
1785 entry->index = index;
1786 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001787 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001788 entry->flags = 0;
1789}
1790
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001791#define F(x) bit(X86_FEATURE_##x)
1792
Dan Kenigsberg07716712007-11-21 17:10:04 +02001793static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1794 u32 index, int *nent, int maxnent)
1795{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001796 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001797#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001798 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1799 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001800 unsigned f_lm = F(LM);
1801#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001802 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001803 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001804#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001805 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001806
1807 /* cpuid 1.edx */
1808 const u32 kvm_supported_word0_x86_features =
1809 F(FPU) | F(VME) | F(DE) | F(PSE) |
1810 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1811 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1812 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1813 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1814 0 /* Reserved, DS, ACPI */ | F(MMX) |
1815 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1816 0 /* HTT, TM, Reserved, PBE */;
1817 /* cpuid 0x80000001.edx */
1818 const u32 kvm_supported_word1_x86_features =
1819 F(FPU) | F(VME) | F(DE) | F(PSE) |
1820 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1821 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1822 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1823 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1824 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001825 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001826 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1827 /* cpuid 1.ecx */
1828 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001829 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1830 0 /* DS-CPL, VMX, SMX, EST */ |
1831 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1832 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1833 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001834 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001835 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001836 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001837 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001838 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1839 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1840 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1841 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001842
Amit Shah19355472009-01-14 16:56:00 +00001843 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001844 get_cpu();
1845 do_cpuid_1_ent(entry, function, index);
1846 ++*nent;
1847
1848 switch (function) {
1849 case 0:
1850 entry->eax = min(entry->eax, (u32)0xb);
1851 break;
1852 case 1:
1853 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001854 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001855 /* we support x2apic emulation even if host does not support
1856 * it since we emulate x2apic in software */
1857 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001858 break;
1859 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1860 * may return different values. This forces us to get_cpu() before
1861 * issuing the first command, and also to emulate this annoying behavior
1862 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1863 case 2: {
1864 int t, times = entry->eax & 0xff;
1865
1866 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001867 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001868 for (t = 1; t < times && *nent < maxnent; ++t) {
1869 do_cpuid_1_ent(&entry[t], function, 0);
1870 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1871 ++*nent;
1872 }
1873 break;
1874 }
1875 /* function 4 and 0xb have additional index. */
1876 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001877 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001878
1879 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1880 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001881 for (i = 1; *nent < maxnent; ++i) {
1882 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001883 if (!cache_type)
1884 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001885 do_cpuid_1_ent(&entry[i], function, i);
1886 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001887 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1888 ++*nent;
1889 }
1890 break;
1891 }
1892 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001893 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001894
1895 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1896 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001897 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001898 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001899 if (!level_type)
1900 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001901 do_cpuid_1_ent(&entry[i], function, i);
1902 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001903 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1904 ++*nent;
1905 }
1906 break;
1907 }
1908 case 0x80000000:
1909 entry->eax = min(entry->eax, 0x8000001a);
1910 break;
1911 case 0x80000001:
1912 entry->edx &= kvm_supported_word1_x86_features;
1913 entry->ecx &= kvm_supported_word6_x86_features;
1914 break;
1915 }
1916 put_cpu();
1917}
1918
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001919#undef F
1920
Avi Kivity674eea02008-02-11 18:37:23 +02001921static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001922 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001923{
1924 struct kvm_cpuid_entry2 *cpuid_entries;
1925 int limit, nent = 0, r = -E2BIG;
1926 u32 func;
1927
1928 if (cpuid->nent < 1)
1929 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001930 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1931 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001932 r = -ENOMEM;
1933 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1934 if (!cpuid_entries)
1935 goto out;
1936
1937 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1938 limit = cpuid_entries[0].eax;
1939 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1940 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001941 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001942 r = -E2BIG;
1943 if (nent >= cpuid->nent)
1944 goto out_free;
1945
1946 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1947 limit = cpuid_entries[nent - 1].eax;
1948 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1949 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001950 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001951 r = -E2BIG;
1952 if (nent >= cpuid->nent)
1953 goto out_free;
1954
Dan Kenigsberg07716712007-11-21 17:10:04 +02001955 r = -EFAULT;
1956 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001957 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001958 goto out_free;
1959 cpuid->nent = nent;
1960 r = 0;
1961
1962out_free:
1963 vfree(cpuid_entries);
1964out:
1965 return r;
1966}
1967
Carsten Otte313a3dc2007-10-11 19:16:52 +02001968static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1969 struct kvm_lapic_state *s)
1970{
1971 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001972 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001973 vcpu_put(vcpu);
1974
1975 return 0;
1976}
1977
1978static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1979 struct kvm_lapic_state *s)
1980{
1981 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001982 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001983 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03001984 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001985 vcpu_put(vcpu);
1986
1987 return 0;
1988}
1989
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001990static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1991 struct kvm_interrupt *irq)
1992{
1993 if (irq->irq < 0 || irq->irq >= 256)
1994 return -EINVAL;
1995 if (irqchip_in_kernel(vcpu->kvm))
1996 return -ENXIO;
1997 vcpu_load(vcpu);
1998
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001999 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002000
2001 vcpu_put(vcpu);
2002
2003 return 0;
2004}
2005
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002006static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2007{
2008 vcpu_load(vcpu);
2009 kvm_inject_nmi(vcpu);
2010 vcpu_put(vcpu);
2011
2012 return 0;
2013}
2014
Avi Kivityb209749f2007-10-22 16:50:39 +02002015static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2016 struct kvm_tpr_access_ctl *tac)
2017{
2018 if (tac->flags)
2019 return -EINVAL;
2020 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2021 return 0;
2022}
2023
Huang Ying890ca9a2009-05-11 16:48:15 +08002024static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2025 u64 mcg_cap)
2026{
2027 int r;
2028 unsigned bank_num = mcg_cap & 0xff, bank;
2029
2030 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002031 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002032 goto out;
2033 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2034 goto out;
2035 r = 0;
2036 vcpu->arch.mcg_cap = mcg_cap;
2037 /* Init IA32_MCG_CTL to all 1s */
2038 if (mcg_cap & MCG_CTL_P)
2039 vcpu->arch.mcg_ctl = ~(u64)0;
2040 /* Init IA32_MCi_CTL to all 1s */
2041 for (bank = 0; bank < bank_num; bank++)
2042 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2043out:
2044 return r;
2045}
2046
2047static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2048 struct kvm_x86_mce *mce)
2049{
2050 u64 mcg_cap = vcpu->arch.mcg_cap;
2051 unsigned bank_num = mcg_cap & 0xff;
2052 u64 *banks = vcpu->arch.mce_banks;
2053
2054 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2055 return -EINVAL;
2056 /*
2057 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2058 * reporting is disabled
2059 */
2060 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2061 vcpu->arch.mcg_ctl != ~(u64)0)
2062 return 0;
2063 banks += 4 * mce->bank;
2064 /*
2065 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2066 * reporting is disabled for the bank
2067 */
2068 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2069 return 0;
2070 if (mce->status & MCI_STATUS_UC) {
2071 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002072 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002073 printk(KERN_DEBUG "kvm: set_mce: "
2074 "injects mce exception while "
2075 "previous one is in progress!\n");
2076 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2077 return 0;
2078 }
2079 if (banks[1] & MCI_STATUS_VAL)
2080 mce->status |= MCI_STATUS_OVER;
2081 banks[2] = mce->addr;
2082 banks[3] = mce->misc;
2083 vcpu->arch.mcg_status = mce->mcg_status;
2084 banks[1] = mce->status;
2085 kvm_queue_exception(vcpu, MC_VECTOR);
2086 } else if (!(banks[1] & MCI_STATUS_VAL)
2087 || !(banks[1] & MCI_STATUS_UC)) {
2088 if (banks[1] & MCI_STATUS_VAL)
2089 mce->status |= MCI_STATUS_OVER;
2090 banks[2] = mce->addr;
2091 banks[3] = mce->misc;
2092 banks[1] = mce->status;
2093 } else
2094 banks[1] |= MCI_STATUS_OVER;
2095 return 0;
2096}
2097
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002098static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2099 struct kvm_vcpu_events *events)
2100{
2101 vcpu_load(vcpu);
2102
2103 events->exception.injected = vcpu->arch.exception.pending;
2104 events->exception.nr = vcpu->arch.exception.nr;
2105 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2106 events->exception.error_code = vcpu->arch.exception.error_code;
2107
2108 events->interrupt.injected = vcpu->arch.interrupt.pending;
2109 events->interrupt.nr = vcpu->arch.interrupt.nr;
2110 events->interrupt.soft = vcpu->arch.interrupt.soft;
2111
2112 events->nmi.injected = vcpu->arch.nmi_injected;
2113 events->nmi.pending = vcpu->arch.nmi_pending;
2114 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2115
2116 events->sipi_vector = vcpu->arch.sipi_vector;
2117
Jan Kiszkadab4b912009-12-06 18:24:15 +01002118 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
2119 | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002120
2121 vcpu_put(vcpu);
2122}
2123
2124static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2125 struct kvm_vcpu_events *events)
2126{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002127 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2128 | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002129 return -EINVAL;
2130
2131 vcpu_load(vcpu);
2132
2133 vcpu->arch.exception.pending = events->exception.injected;
2134 vcpu->arch.exception.nr = events->exception.nr;
2135 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2136 vcpu->arch.exception.error_code = events->exception.error_code;
2137
2138 vcpu->arch.interrupt.pending = events->interrupt.injected;
2139 vcpu->arch.interrupt.nr = events->interrupt.nr;
2140 vcpu->arch.interrupt.soft = events->interrupt.soft;
2141 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2142 kvm_pic_clear_isr_ack(vcpu->kvm);
2143
2144 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002145 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2146 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002147 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2148
Jan Kiszkadab4b912009-12-06 18:24:15 +01002149 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2150 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002151
2152 vcpu_put(vcpu);
2153
2154 return 0;
2155}
2156
Carsten Otte313a3dc2007-10-11 19:16:52 +02002157long kvm_arch_vcpu_ioctl(struct file *filp,
2158 unsigned int ioctl, unsigned long arg)
2159{
2160 struct kvm_vcpu *vcpu = filp->private_data;
2161 void __user *argp = (void __user *)arg;
2162 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002163 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002164
2165 switch (ioctl) {
2166 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002167 r = -EINVAL;
2168 if (!vcpu->arch.apic)
2169 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002170 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002171
Dave Hansenb772ff32008-08-11 10:01:47 -07002172 r = -ENOMEM;
2173 if (!lapic)
2174 goto out;
2175 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002176 if (r)
2177 goto out;
2178 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002179 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002180 goto out;
2181 r = 0;
2182 break;
2183 }
2184 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002185 r = -EINVAL;
2186 if (!vcpu->arch.apic)
2187 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002188 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2189 r = -ENOMEM;
2190 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002191 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002192 r = -EFAULT;
2193 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2194 goto out;
2195 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002196 if (r)
2197 goto out;
2198 r = 0;
2199 break;
2200 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002201 case KVM_INTERRUPT: {
2202 struct kvm_interrupt irq;
2203
2204 r = -EFAULT;
2205 if (copy_from_user(&irq, argp, sizeof irq))
2206 goto out;
2207 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2208 if (r)
2209 goto out;
2210 r = 0;
2211 break;
2212 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002213 case KVM_NMI: {
2214 r = kvm_vcpu_ioctl_nmi(vcpu);
2215 if (r)
2216 goto out;
2217 r = 0;
2218 break;
2219 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002220 case KVM_SET_CPUID: {
2221 struct kvm_cpuid __user *cpuid_arg = argp;
2222 struct kvm_cpuid cpuid;
2223
2224 r = -EFAULT;
2225 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2226 goto out;
2227 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2228 if (r)
2229 goto out;
2230 break;
2231 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002232 case KVM_SET_CPUID2: {
2233 struct kvm_cpuid2 __user *cpuid_arg = argp;
2234 struct kvm_cpuid2 cpuid;
2235
2236 r = -EFAULT;
2237 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2238 goto out;
2239 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002240 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002241 if (r)
2242 goto out;
2243 break;
2244 }
2245 case KVM_GET_CPUID2: {
2246 struct kvm_cpuid2 __user *cpuid_arg = argp;
2247 struct kvm_cpuid2 cpuid;
2248
2249 r = -EFAULT;
2250 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2251 goto out;
2252 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002253 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002254 if (r)
2255 goto out;
2256 r = -EFAULT;
2257 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2258 goto out;
2259 r = 0;
2260 break;
2261 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002262 case KVM_GET_MSRS:
2263 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2264 break;
2265 case KVM_SET_MSRS:
2266 r = msr_io(vcpu, argp, do_set_msr, 0);
2267 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002268 case KVM_TPR_ACCESS_REPORTING: {
2269 struct kvm_tpr_access_ctl tac;
2270
2271 r = -EFAULT;
2272 if (copy_from_user(&tac, argp, sizeof tac))
2273 goto out;
2274 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2275 if (r)
2276 goto out;
2277 r = -EFAULT;
2278 if (copy_to_user(argp, &tac, sizeof tac))
2279 goto out;
2280 r = 0;
2281 break;
2282 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002283 case KVM_SET_VAPIC_ADDR: {
2284 struct kvm_vapic_addr va;
2285
2286 r = -EINVAL;
2287 if (!irqchip_in_kernel(vcpu->kvm))
2288 goto out;
2289 r = -EFAULT;
2290 if (copy_from_user(&va, argp, sizeof va))
2291 goto out;
2292 r = 0;
2293 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2294 break;
2295 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002296 case KVM_X86_SETUP_MCE: {
2297 u64 mcg_cap;
2298
2299 r = -EFAULT;
2300 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2301 goto out;
2302 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2303 break;
2304 }
2305 case KVM_X86_SET_MCE: {
2306 struct kvm_x86_mce mce;
2307
2308 r = -EFAULT;
2309 if (copy_from_user(&mce, argp, sizeof mce))
2310 goto out;
2311 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2312 break;
2313 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002314 case KVM_GET_VCPU_EVENTS: {
2315 struct kvm_vcpu_events events;
2316
2317 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2318
2319 r = -EFAULT;
2320 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2321 break;
2322 r = 0;
2323 break;
2324 }
2325 case KVM_SET_VCPU_EVENTS: {
2326 struct kvm_vcpu_events events;
2327
2328 r = -EFAULT;
2329 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2330 break;
2331
2332 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2333 break;
2334 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002335 default:
2336 r = -EINVAL;
2337 }
2338out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002339 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002340 return r;
2341}
2342
Carsten Otte1fe779f2007-10-29 16:08:35 +01002343static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2344{
2345 int ret;
2346
2347 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2348 return -1;
2349 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2350 return ret;
2351}
2352
Sheng Yangb927a3c2009-07-21 10:42:48 +08002353static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2354 u64 ident_addr)
2355{
2356 kvm->arch.ept_identity_map_addr = ident_addr;
2357 return 0;
2358}
2359
Carsten Otte1fe779f2007-10-29 16:08:35 +01002360static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2361 u32 kvm_nr_mmu_pages)
2362{
2363 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2364 return -EINVAL;
2365
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002366 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002367 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002368
2369 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002370 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002371
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002372 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002373 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002374 return 0;
2375}
2376
2377static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2378{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002379 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002380}
2381
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002382gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2383{
2384 int i;
2385 struct kvm_mem_alias *alias;
2386 struct kvm_mem_aliases *aliases;
2387
2388 aliases = rcu_dereference(kvm->arch.aliases);
2389
2390 for (i = 0; i < aliases->naliases; ++i) {
2391 alias = &aliases->aliases[i];
2392 if (alias->flags & KVM_ALIAS_INVALID)
2393 continue;
2394 if (gfn >= alias->base_gfn
2395 && gfn < alias->base_gfn + alias->npages)
2396 return alias->target_gfn + gfn - alias->base_gfn;
2397 }
2398 return gfn;
2399}
2400
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002401gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2402{
2403 int i;
2404 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002405 struct kvm_mem_aliases *aliases;
2406
2407 aliases = rcu_dereference(kvm->arch.aliases);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002408
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002409 for (i = 0; i < aliases->naliases; ++i) {
2410 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002411 if (gfn >= alias->base_gfn
2412 && gfn < alias->base_gfn + alias->npages)
2413 return alias->target_gfn + gfn - alias->base_gfn;
2414 }
2415 return gfn;
2416}
2417
Carsten Otte1fe779f2007-10-29 16:08:35 +01002418/*
2419 * Set a new alias region. Aliases map a portion of physical memory into
2420 * another portion. This is useful for memory windows, for example the PC
2421 * VGA region.
2422 */
2423static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2424 struct kvm_memory_alias *alias)
2425{
2426 int r, n;
2427 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002428 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002429
2430 r = -EINVAL;
2431 /* General sanity checks */
2432 if (alias->memory_size & (PAGE_SIZE - 1))
2433 goto out;
2434 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2435 goto out;
2436 if (alias->slot >= KVM_ALIAS_SLOTS)
2437 goto out;
2438 if (alias->guest_phys_addr + alias->memory_size
2439 < alias->guest_phys_addr)
2440 goto out;
2441 if (alias->target_phys_addr + alias->memory_size
2442 < alias->target_phys_addr)
2443 goto out;
2444
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002445 r = -ENOMEM;
2446 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2447 if (!aliases)
2448 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002449
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002450 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002451
2452 /* invalidate any gfn reference in case of deletion/shrinking */
2453 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2454 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2455 old_aliases = kvm->arch.aliases;
2456 rcu_assign_pointer(kvm->arch.aliases, aliases);
2457 synchronize_srcu_expedited(&kvm->srcu);
2458 kvm_mmu_zap_all(kvm);
2459 kfree(old_aliases);
2460
2461 r = -ENOMEM;
2462 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2463 if (!aliases)
2464 goto out_unlock;
2465
2466 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002467
2468 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002469 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2470 p->npages = alias->memory_size >> PAGE_SHIFT;
2471 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002472 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002473
2474 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002475 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002476 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002477 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002478
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002479 old_aliases = kvm->arch.aliases;
2480 rcu_assign_pointer(kvm->arch.aliases, aliases);
2481 synchronize_srcu_expedited(&kvm->srcu);
2482 kfree(old_aliases);
2483 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002484
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002485out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002486 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002487out:
2488 return r;
2489}
2490
2491static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2492{
2493 int r;
2494
2495 r = 0;
2496 switch (chip->chip_id) {
2497 case KVM_IRQCHIP_PIC_MASTER:
2498 memcpy(&chip->chip.pic,
2499 &pic_irqchip(kvm)->pics[0],
2500 sizeof(struct kvm_pic_state));
2501 break;
2502 case KVM_IRQCHIP_PIC_SLAVE:
2503 memcpy(&chip->chip.pic,
2504 &pic_irqchip(kvm)->pics[1],
2505 sizeof(struct kvm_pic_state));
2506 break;
2507 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002508 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002509 break;
2510 default:
2511 r = -EINVAL;
2512 break;
2513 }
2514 return r;
2515}
2516
2517static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2518{
2519 int r;
2520
2521 r = 0;
2522 switch (chip->chip_id) {
2523 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002524 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002525 memcpy(&pic_irqchip(kvm)->pics[0],
2526 &chip->chip.pic,
2527 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002528 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002529 break;
2530 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002531 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002532 memcpy(&pic_irqchip(kvm)->pics[1],
2533 &chip->chip.pic,
2534 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002535 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002536 break;
2537 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002538 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002539 break;
2540 default:
2541 r = -EINVAL;
2542 break;
2543 }
2544 kvm_pic_update_irq(pic_irqchip(kvm));
2545 return r;
2546}
2547
Sheng Yange0f63cb2008-03-04 00:50:59 +08002548static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2549{
2550 int r = 0;
2551
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002552 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002553 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002554 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002555 return r;
2556}
2557
2558static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2559{
2560 int r = 0;
2561
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002562 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002563 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002564 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2565 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2566 return r;
2567}
2568
2569static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2570{
2571 int r = 0;
2572
2573 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2574 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2575 sizeof(ps->channels));
2576 ps->flags = kvm->arch.vpit->pit_state.flags;
2577 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2578 return r;
2579}
2580
2581static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2582{
2583 int r = 0, start = 0;
2584 u32 prev_legacy, cur_legacy;
2585 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2586 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2587 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2588 if (!prev_legacy && cur_legacy)
2589 start = 1;
2590 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2591 sizeof(kvm->arch.vpit->pit_state.channels));
2592 kvm->arch.vpit->pit_state.flags = ps->flags;
2593 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002594 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002595 return r;
2596}
2597
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002598static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2599 struct kvm_reinject_control *control)
2600{
2601 if (!kvm->arch.vpit)
2602 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002603 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002604 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002605 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002606 return 0;
2607}
2608
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002609/*
2610 * Get (and clear) the dirty memory log for a memory slot.
2611 */
2612int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2613 struct kvm_dirty_log *log)
2614{
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002615 int r, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002616 struct kvm_memory_slot *memslot;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002617 unsigned long n;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002618 unsigned long is_dirty = 0;
2619 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002620
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002621 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002622
Marcelo Tosattib050b012009-12-23 14:35:22 -02002623 r = -EINVAL;
2624 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002625 goto out;
2626
Marcelo Tosattib050b012009-12-23 14:35:22 -02002627 memslot = &kvm->memslots->memslots[log->slot];
2628 r = -ENOENT;
2629 if (!memslot->dirty_bitmap)
2630 goto out;
2631
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002632 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002633
2634 r = -ENOMEM;
2635 dirty_bitmap = vmalloc(n);
2636 if (!dirty_bitmap)
2637 goto out;
2638 memset(dirty_bitmap, 0, n);
2639
2640 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2641 is_dirty = memslot->dirty_bitmap[i];
2642
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002643 /* If nothing is dirty, don't bother messing with page tables. */
2644 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002645 struct kvm_memslots *slots, *old_slots;
2646
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002647 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002648 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002649 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002650
2651 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2652 if (!slots)
2653 goto out_free;
2654
2655 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2656 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2657
2658 old_slots = kvm->memslots;
2659 rcu_assign_pointer(kvm->memslots, slots);
2660 synchronize_srcu_expedited(&kvm->srcu);
2661 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2662 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002663 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002664
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002665 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002666 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2667 r = -EFAULT;
2668out_free:
2669 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002670out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002671 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002672 return r;
2673}
2674
Carsten Otte1fe779f2007-10-29 16:08:35 +01002675long kvm_arch_vm_ioctl(struct file *filp,
2676 unsigned int ioctl, unsigned long arg)
2677{
2678 struct kvm *kvm = filp->private_data;
2679 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002680 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002681 /*
2682 * This union makes it completely explicit to gcc-3.x
2683 * that these two variables' stack usage should be
2684 * combined, not added together.
2685 */
2686 union {
2687 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002688 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002689 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002690 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002691 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002692
2693 switch (ioctl) {
2694 case KVM_SET_TSS_ADDR:
2695 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2696 if (r < 0)
2697 goto out;
2698 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002699 case KVM_SET_IDENTITY_MAP_ADDR: {
2700 u64 ident_addr;
2701
2702 r = -EFAULT;
2703 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2704 goto out;
2705 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2706 if (r < 0)
2707 goto out;
2708 break;
2709 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002710 case KVM_SET_MEMORY_REGION: {
2711 struct kvm_memory_region kvm_mem;
2712 struct kvm_userspace_memory_region kvm_userspace_mem;
2713
2714 r = -EFAULT;
2715 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2716 goto out;
2717 kvm_userspace_mem.slot = kvm_mem.slot;
2718 kvm_userspace_mem.flags = kvm_mem.flags;
2719 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2720 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2721 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2722 if (r)
2723 goto out;
2724 break;
2725 }
2726 case KVM_SET_NR_MMU_PAGES:
2727 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2728 if (r)
2729 goto out;
2730 break;
2731 case KVM_GET_NR_MMU_PAGES:
2732 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2733 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002734 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002735 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002736 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002737 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002738 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002739 if (r)
2740 goto out;
2741 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002742 case KVM_CREATE_IRQCHIP: {
2743 struct kvm_pic *vpic;
2744
2745 mutex_lock(&kvm->lock);
2746 r = -EEXIST;
2747 if (kvm->arch.vpic)
2748 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002749 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002750 vpic = kvm_create_pic(kvm);
2751 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002752 r = kvm_ioapic_init(kvm);
2753 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002754 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2755 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002756 kfree(vpic);
2757 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002758 }
2759 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002760 goto create_irqchip_unlock;
2761 smp_wmb();
2762 kvm->arch.vpic = vpic;
2763 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002764 r = kvm_setup_default_irq_routing(kvm);
2765 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002766 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002767 kvm_ioapic_destroy(kvm);
2768 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002769 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002770 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002771 create_irqchip_unlock:
2772 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002773 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002774 }
Sheng Yang78376992008-01-28 05:10:22 +08002775 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002776 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2777 goto create_pit;
2778 case KVM_CREATE_PIT2:
2779 r = -EFAULT;
2780 if (copy_from_user(&u.pit_config, argp,
2781 sizeof(struct kvm_pit_config)))
2782 goto out;
2783 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002784 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002785 r = -EEXIST;
2786 if (kvm->arch.vpit)
2787 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002788 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002789 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002790 if (kvm->arch.vpit)
2791 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002792 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002793 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002794 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002795 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002796 case KVM_IRQ_LINE: {
2797 struct kvm_irq_level irq_event;
2798
2799 r = -EFAULT;
2800 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2801 goto out;
2802 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002803 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002804 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2805 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002806 if (ioctl == KVM_IRQ_LINE_STATUS) {
2807 irq_event.status = status;
2808 if (copy_to_user(argp, &irq_event,
2809 sizeof irq_event))
2810 goto out;
2811 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002812 r = 0;
2813 }
2814 break;
2815 }
2816 case KVM_GET_IRQCHIP: {
2817 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002818 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002819
Dave Hansenf0d66272008-08-11 10:01:45 -07002820 r = -ENOMEM;
2821 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002822 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002823 r = -EFAULT;
2824 if (copy_from_user(chip, argp, sizeof *chip))
2825 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002826 r = -ENXIO;
2827 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002828 goto get_irqchip_out;
2829 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2830 if (r)
2831 goto get_irqchip_out;
2832 r = -EFAULT;
2833 if (copy_to_user(argp, chip, sizeof *chip))
2834 goto get_irqchip_out;
2835 r = 0;
2836 get_irqchip_out:
2837 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002838 if (r)
2839 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002840 break;
2841 }
2842 case KVM_SET_IRQCHIP: {
2843 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002844 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002845
Dave Hansenf0d66272008-08-11 10:01:45 -07002846 r = -ENOMEM;
2847 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002848 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002849 r = -EFAULT;
2850 if (copy_from_user(chip, argp, sizeof *chip))
2851 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002852 r = -ENXIO;
2853 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002854 goto set_irqchip_out;
2855 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2856 if (r)
2857 goto set_irqchip_out;
2858 r = 0;
2859 set_irqchip_out:
2860 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002861 if (r)
2862 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002863 break;
2864 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002865 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002866 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002867 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002868 goto out;
2869 r = -ENXIO;
2870 if (!kvm->arch.vpit)
2871 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002872 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002873 if (r)
2874 goto out;
2875 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002876 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002877 goto out;
2878 r = 0;
2879 break;
2880 }
2881 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002882 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002883 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002884 goto out;
2885 r = -ENXIO;
2886 if (!kvm->arch.vpit)
2887 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002888 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002889 if (r)
2890 goto out;
2891 r = 0;
2892 break;
2893 }
Beth Kone9f42752009-07-07 11:50:38 -04002894 case KVM_GET_PIT2: {
2895 r = -ENXIO;
2896 if (!kvm->arch.vpit)
2897 goto out;
2898 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2899 if (r)
2900 goto out;
2901 r = -EFAULT;
2902 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2903 goto out;
2904 r = 0;
2905 break;
2906 }
2907 case KVM_SET_PIT2: {
2908 r = -EFAULT;
2909 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2910 goto out;
2911 r = -ENXIO;
2912 if (!kvm->arch.vpit)
2913 goto out;
2914 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2915 if (r)
2916 goto out;
2917 r = 0;
2918 break;
2919 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002920 case KVM_REINJECT_CONTROL: {
2921 struct kvm_reinject_control control;
2922 r = -EFAULT;
2923 if (copy_from_user(&control, argp, sizeof(control)))
2924 goto out;
2925 r = kvm_vm_ioctl_reinject(kvm, &control);
2926 if (r)
2927 goto out;
2928 r = 0;
2929 break;
2930 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002931 case KVM_XEN_HVM_CONFIG: {
2932 r = -EFAULT;
2933 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2934 sizeof(struct kvm_xen_hvm_config)))
2935 goto out;
2936 r = -EINVAL;
2937 if (kvm->arch.xen_hvm_config.flags)
2938 goto out;
2939 r = 0;
2940 break;
2941 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002942 case KVM_SET_CLOCK: {
2943 struct timespec now;
2944 struct kvm_clock_data user_ns;
2945 u64 now_ns;
2946 s64 delta;
2947
2948 r = -EFAULT;
2949 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2950 goto out;
2951
2952 r = -EINVAL;
2953 if (user_ns.flags)
2954 goto out;
2955
2956 r = 0;
2957 ktime_get_ts(&now);
2958 now_ns = timespec_to_ns(&now);
2959 delta = user_ns.clock - now_ns;
2960 kvm->arch.kvmclock_offset = delta;
2961 break;
2962 }
2963 case KVM_GET_CLOCK: {
2964 struct timespec now;
2965 struct kvm_clock_data user_ns;
2966 u64 now_ns;
2967
2968 ktime_get_ts(&now);
2969 now_ns = timespec_to_ns(&now);
2970 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2971 user_ns.flags = 0;
2972
2973 r = -EFAULT;
2974 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2975 goto out;
2976 r = 0;
2977 break;
2978 }
2979
Carsten Otte1fe779f2007-10-29 16:08:35 +01002980 default:
2981 ;
2982 }
2983out:
2984 return r;
2985}
2986
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002987static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002988{
2989 u32 dummy[2];
2990 unsigned i, j;
2991
Glauber Costae3267cb2009-10-06 13:24:50 -04002992 /* skip the first msrs in the list. KVM-specific */
2993 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02002994 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2995 continue;
2996 if (j < i)
2997 msrs_to_save[j] = msrs_to_save[i];
2998 j++;
2999 }
3000 num_msrs_to_save = j;
3001}
3002
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003003static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3004 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003005{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003006 if (vcpu->arch.apic &&
3007 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3008 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003009
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003010 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003011}
3012
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003013static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003014{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003015 if (vcpu->arch.apic &&
3016 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3017 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003018
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003019 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003020}
3021
Gleb Natapov1871c602010-02-10 14:21:32 +02003022gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3023{
3024 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3025 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3026}
3027
3028 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3029{
3030 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3031 access |= PFERR_FETCH_MASK;
3032 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3033}
3034
3035gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3036{
3037 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3038 access |= PFERR_WRITE_MASK;
3039 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3040}
3041
3042/* uses this to access any guest's mapped memory without checking CPL */
3043gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3044{
3045 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3046}
3047
3048static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3049 struct kvm_vcpu *vcpu, u32 access,
3050 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003051{
3052 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003053 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003054
3055 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003056 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003057 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003058 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003059 int ret;
3060
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003061 if (gpa == UNMAPPED_GVA) {
3062 r = X86EMUL_PROPAGATE_FAULT;
3063 goto out;
3064 }
Izik Eidus77c20022008-12-29 01:42:19 +02003065 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003066 if (ret < 0) {
3067 r = X86EMUL_UNHANDLEABLE;
3068 goto out;
3069 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003070
Izik Eidus77c20022008-12-29 01:42:19 +02003071 bytes -= toread;
3072 data += toread;
3073 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003074 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003075out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003076 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003077}
Izik Eidus77c20022008-12-29 01:42:19 +02003078
Gleb Natapov1871c602010-02-10 14:21:32 +02003079/* used for instruction fetching */
3080static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3081 struct kvm_vcpu *vcpu, u32 *error)
3082{
3083 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3084 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3085 access | PFERR_FETCH_MASK, error);
3086}
3087
3088static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3089 struct kvm_vcpu *vcpu, u32 *error)
3090{
3091 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3092 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3093 error);
3094}
3095
3096static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3097 struct kvm_vcpu *vcpu, u32 *error)
3098{
3099 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3100}
3101
Hannes Edercded19f2009-02-21 02:19:13 +01003102static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +02003103 struct kvm_vcpu *vcpu, u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003104{
3105 void *data = val;
3106 int r = X86EMUL_CONTINUE;
3107
3108 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003109 gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003110 unsigned offset = addr & (PAGE_SIZE-1);
3111 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3112 int ret;
3113
3114 if (gpa == UNMAPPED_GVA) {
3115 r = X86EMUL_PROPAGATE_FAULT;
3116 goto out;
3117 }
3118 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3119 if (ret < 0) {
3120 r = X86EMUL_UNHANDLEABLE;
3121 goto out;
3122 }
3123
3124 bytes -= towrite;
3125 data += towrite;
3126 addr += towrite;
3127 }
3128out:
3129 return r;
3130}
3131
Carsten Ottebbd9b642007-10-30 18:44:21 +01003132
Carsten Ottebbd9b642007-10-30 18:44:21 +01003133static int emulator_read_emulated(unsigned long addr,
3134 void *val,
3135 unsigned int bytes,
3136 struct kvm_vcpu *vcpu)
3137{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003138 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003139 u32 error_code;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003140
3141 if (vcpu->mmio_read_completed) {
3142 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003143 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3144 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003145 vcpu->mmio_read_completed = 0;
3146 return X86EMUL_CONTINUE;
3147 }
3148
Gleb Natapov1871c602010-02-10 14:21:32 +02003149 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
3150
3151 if (gpa == UNMAPPED_GVA) {
3152 kvm_inject_page_fault(vcpu, addr, error_code);
3153 return X86EMUL_PROPAGATE_FAULT;
3154 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003155
3156 /* For APIC access vmexit */
3157 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3158 goto mmio;
3159
Gleb Natapov1871c602010-02-10 14:21:32 +02003160 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003161 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003162 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003163
3164mmio:
3165 /*
3166 * Is this MMIO handled locally?
3167 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003168 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3169 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003170 return X86EMUL_CONTINUE;
3171 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003172
3173 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003174
3175 vcpu->mmio_needed = 1;
3176 vcpu->mmio_phys_addr = gpa;
3177 vcpu->mmio_size = bytes;
3178 vcpu->mmio_is_write = 0;
3179
3180 return X86EMUL_UNHANDLEABLE;
3181}
3182
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003183int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003184 const void *val, int bytes)
3185{
3186 int ret;
3187
3188 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3189 if (ret < 0)
3190 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003191 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003192 return 1;
3193}
3194
Carsten Ottebbd9b642007-10-30 18:44:21 +01003195static int emulator_write_emulated_onepage(unsigned long addr,
3196 const void *val,
3197 unsigned int bytes,
3198 struct kvm_vcpu *vcpu)
3199{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003200 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003201 u32 error_code;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003202
Gleb Natapov1871c602010-02-10 14:21:32 +02003203 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003204
3205 if (gpa == UNMAPPED_GVA) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003206 kvm_inject_page_fault(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003207 return X86EMUL_PROPAGATE_FAULT;
3208 }
3209
3210 /* For APIC access vmexit */
3211 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3212 goto mmio;
3213
3214 if (emulator_write_phys(vcpu, gpa, val, bytes))
3215 return X86EMUL_CONTINUE;
3216
3217mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003218 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003219 /*
3220 * Is this MMIO handled locally?
3221 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003222 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003223 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003224
3225 vcpu->mmio_needed = 1;
3226 vcpu->mmio_phys_addr = gpa;
3227 vcpu->mmio_size = bytes;
3228 vcpu->mmio_is_write = 1;
3229 memcpy(vcpu->mmio_data, val, bytes);
3230
3231 return X86EMUL_CONTINUE;
3232}
3233
3234int emulator_write_emulated(unsigned long addr,
3235 const void *val,
3236 unsigned int bytes,
3237 struct kvm_vcpu *vcpu)
3238{
3239 /* Crossing a page boundary? */
3240 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3241 int rc, now;
3242
3243 now = -addr & ~PAGE_MASK;
3244 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
3245 if (rc != X86EMUL_CONTINUE)
3246 return rc;
3247 addr += now;
3248 val += now;
3249 bytes -= now;
3250 }
3251 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
3252}
3253EXPORT_SYMBOL_GPL(emulator_write_emulated);
3254
3255static int emulator_cmpxchg_emulated(unsigned long addr,
3256 const void *old,
3257 const void *new,
3258 unsigned int bytes,
3259 struct kvm_vcpu *vcpu)
3260{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02003261 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003262#ifndef CONFIG_X86_64
3263 /* guests cmpxchg8b have to be emulated atomically */
3264 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003265 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003266 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003267 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003268 u64 val;
3269
Gleb Natapov1871c602010-02-10 14:21:32 +02003270 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003271
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003272 if (gpa == UNMAPPED_GVA ||
3273 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3274 goto emul_write;
3275
3276 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3277 goto emul_write;
3278
3279 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003280
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003281 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003282
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003283 kaddr = kmap_atomic(page, KM_USER0);
3284 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
3285 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003286 kvm_release_page_dirty(page);
3287 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003288emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003289#endif
3290
Carsten Ottebbd9b642007-10-30 18:44:21 +01003291 return emulator_write_emulated(addr, new, bytes, vcpu);
3292}
3293
3294static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3295{
3296 return kvm_x86_ops->get_segment_base(vcpu, seg);
3297}
3298
3299int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3300{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003301 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003302 return X86EMUL_CONTINUE;
3303}
3304
3305int emulate_clts(struct kvm_vcpu *vcpu)
3306{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003307 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003308 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003309 return X86EMUL_CONTINUE;
3310}
3311
3312int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3313{
Jan Kiszkac76de352010-01-20 18:20:20 +01003314 return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003315}
3316
3317int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3318{
3319 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003320
Jan Kiszkac76de352010-01-20 18:20:20 +01003321 return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003322}
3323
3324void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3325{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003326 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003327 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003328 unsigned long rip_linear;
3329
Avi Kivityf76c7102008-06-13 22:45:42 +03003330 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003331 return;
3332
Glauber Costa25be4602008-06-10 10:46:53 -03003333 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3334
Gleb Natapov1871c602010-02-10 14:21:32 +02003335 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003336
3337 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3338 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003339}
3340EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3341
Harvey Harrison14af3f32008-02-19 10:25:50 -08003342static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003343 .read_std = kvm_read_guest_virt_system,
3344 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003345 .read_emulated = emulator_read_emulated,
3346 .write_emulated = emulator_write_emulated,
3347 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3348};
3349
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003350static void cache_all_regs(struct kvm_vcpu *vcpu)
3351{
3352 kvm_register_read(vcpu, VCPU_REGS_RAX);
3353 kvm_register_read(vcpu, VCPU_REGS_RSP);
3354 kvm_register_read(vcpu, VCPU_REGS_RIP);
3355 vcpu->arch.regs_dirty = ~0;
3356}
3357
Carsten Ottebbd9b642007-10-30 18:44:21 +01003358int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003359 unsigned long cr2,
3360 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003361 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003362{
Glauber Costa310b5d32009-05-12 16:21:06 -04003363 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003364 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003365 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003366
Avi Kivity26eef702008-07-03 14:59:22 +03003367 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003368 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003369 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003370 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003371 * instead of direct ->regs accesses, can save hundred cycles
3372 * on Intel for instructions that don't read/change RSP, for
3373 * for example.
3374 */
3375 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003376
3377 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003378 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003379
Sheng Yang571008d2008-01-02 14:49:22 +08003380 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003381 int cs_db, cs_l;
3382 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3383
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003384 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka91586a32009-10-05 13:07:21 +02003385 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003386 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003387 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003388 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003389 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003390 ? X86EMUL_MODE_PROT64 : cs_db
3391 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3392
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003393 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003394
Andre Przywara0cb57622009-06-17 15:50:31 +02003395 /* Only allow emulation of specific instructions on #UD
3396 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003397 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003398 if (emulation_type & EMULTYPE_TRAP_UD) {
3399 if (!c->twobyte)
3400 return EMULATE_FAIL;
3401 switch (c->b) {
3402 case 0x01: /* VMMCALL */
3403 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3404 return EMULATE_FAIL;
3405 break;
3406 case 0x34: /* sysenter */
3407 case 0x35: /* sysexit */
3408 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3409 return EMULATE_FAIL;
3410 break;
3411 case 0x05: /* syscall */
3412 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3413 return EMULATE_FAIL;
3414 break;
3415 default:
3416 return EMULATE_FAIL;
3417 }
3418
3419 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3420 return EMULATE_FAIL;
3421 }
Sheng Yang571008d2008-01-02 14:49:22 +08003422
Avi Kivityf2b57562007-11-18 15:17:51 +02003423 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003424 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003425 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003426 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3427 return EMULATE_DONE;
3428 return EMULATE_FAIL;
3429 }
3430 }
3431
Gleb Natapovba8afb62009-04-12 13:36:57 +03003432 if (emulation_type & EMULTYPE_SKIP) {
3433 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3434 return EMULATE_DONE;
3435 }
3436
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003437 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003438 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3439
3440 if (r == 0)
3441 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003442
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003443 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003444 return EMULATE_DO_MMIO;
3445
3446 if ((r || vcpu->mmio_is_write) && run) {
3447 run->exit_reason = KVM_EXIT_MMIO;
3448 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3449 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3450 run->mmio.len = vcpu->mmio_size;
3451 run->mmio.is_write = vcpu->mmio_is_write;
3452 }
3453
3454 if (r) {
3455 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3456 return EMULATE_DONE;
3457 if (!vcpu->mmio_needed) {
3458 kvm_report_emulation_failure(vcpu, "mmio");
3459 return EMULATE_FAIL;
3460 }
3461 return EMULATE_DO_MMIO;
3462 }
3463
Jan Kiszka91586a32009-10-05 13:07:21 +02003464 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003465
3466 if (vcpu->mmio_is_write) {
3467 vcpu->mmio_needed = 0;
3468 return EMULATE_DO_MMIO;
3469 }
3470
3471 return EMULATE_DONE;
3472}
3473EXPORT_SYMBOL_GPL(emulate_instruction);
3474
Carsten Ottede7d7892007-10-30 18:44:25 +01003475static int pio_copy_data(struct kvm_vcpu *vcpu)
3476{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003477 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003478 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003479 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003480 int ret;
Gleb Natapov1871c602010-02-10 14:21:32 +02003481 u32 error_code;
Carsten Ottede7d7892007-10-30 18:44:25 +01003482
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003483 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3484 if (vcpu->arch.pio.in)
Gleb Natapov1871c602010-02-10 14:21:32 +02003485 ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code);
Carsten Ottede7d7892007-10-30 18:44:25 +01003486 else
Gleb Natapov1871c602010-02-10 14:21:32 +02003487 ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code);
3488
3489 if (ret == X86EMUL_PROPAGATE_FAULT)
3490 kvm_inject_page_fault(vcpu, q, error_code);
3491
Izik Eidus0f346072008-12-29 01:42:20 +02003492 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003493}
3494
3495int complete_pio(struct kvm_vcpu *vcpu)
3496{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003497 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003498 long delta;
3499 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003500 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003501
3502 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003503 if (io->in) {
3504 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3505 memcpy(&val, vcpu->arch.pio_data, io->size);
3506 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3507 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003508 } else {
3509 if (io->in) {
3510 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003511 if (r)
Gleb Natapov1871c602010-02-10 14:21:32 +02003512 goto out;
Carsten Ottede7d7892007-10-30 18:44:25 +01003513 }
3514
3515 delta = 1;
3516 if (io->rep) {
3517 delta *= io->cur_count;
3518 /*
3519 * The size of the register should really depend on
3520 * current address size.
3521 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003522 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3523 val -= delta;
3524 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003525 }
3526 if (io->down)
3527 delta = -delta;
3528 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003529 if (io->in) {
3530 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3531 val += delta;
3532 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3533 } else {
3534 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3535 val += delta;
3536 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3537 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003538 }
Gleb Natapov1871c602010-02-10 14:21:32 +02003539out:
Carsten Ottede7d7892007-10-30 18:44:25 +01003540 io->count -= io->cur_count;
3541 io->cur_count = 0;
3542
3543 return 0;
3544}
3545
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003546static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003547{
3548 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003549 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003550
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003551 if (vcpu->arch.pio.in)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003552 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003553 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003554 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003555 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3556 vcpu->arch.pio.port, vcpu->arch.pio.size,
3557 pd);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003558 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003559}
3560
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003561static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003562{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003563 struct kvm_pio_request *io = &vcpu->arch.pio;
3564 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003565 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003566
Carsten Ottede7d7892007-10-30 18:44:25 +01003567 for (i = 0; i < io->cur_count; i++) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003568 if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003569 io->port, io->size, pd)) {
3570 r = -EOPNOTSUPP;
3571 break;
3572 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003573 pd += io->size;
3574 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003575 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003576}
3577
Avi Kivity851ba692009-08-24 11:10:17 +03003578int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003579{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003580 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003581
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003582 trace_kvm_pio(!in, port, size, 1);
3583
Carsten Ottede7d7892007-10-30 18:44:25 +01003584 vcpu->run->exit_reason = KVM_EXIT_IO;
3585 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003586 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003587 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003588 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3589 vcpu->run->io.port = vcpu->arch.pio.port = port;
3590 vcpu->arch.pio.in = in;
3591 vcpu->arch.pio.string = 0;
3592 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003593 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003594
Takuya Yoshikawa1976d2d2010-02-05 17:52:46 +09003595 if (!vcpu->arch.pio.in) {
3596 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3597 memcpy(vcpu->arch.pio_data, &val, 4);
3598 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003599
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003600 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003601 complete_pio(vcpu);
3602 return 1;
3603 }
3604 return 0;
3605}
3606EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3607
Avi Kivity851ba692009-08-24 11:10:17 +03003608int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003609 int size, unsigned long count, int down,
3610 gva_t address, int rep, unsigned port)
3611{
3612 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003613 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003614
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003615 trace_kvm_pio(!in, port, size, count);
3616
Carsten Ottede7d7892007-10-30 18:44:25 +01003617 vcpu->run->exit_reason = KVM_EXIT_IO;
3618 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003619 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003620 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003621 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3622 vcpu->run->io.port = vcpu->arch.pio.port = port;
3623 vcpu->arch.pio.in = in;
3624 vcpu->arch.pio.string = 1;
3625 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003626 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003627
3628 if (!count) {
3629 kvm_x86_ops->skip_emulated_instruction(vcpu);
3630 return 1;
3631 }
3632
3633 if (!down)
3634 in_page = PAGE_SIZE - offset_in_page(address);
3635 else
3636 in_page = offset_in_page(address) + size;
3637 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003638 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003639 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003640 if (down) {
3641 /*
3642 * String I/O in reverse. Yuck. Kill the guest, fix later.
3643 */
3644 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003645 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003646 return 1;
3647 }
3648 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003649 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003650
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003651 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003652 kvm_x86_ops->skip_emulated_instruction(vcpu);
3653
Izik Eidus0f346072008-12-29 01:42:20 +02003654 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003655
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003656 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003657 /* string PIO write */
3658 ret = pio_copy_data(vcpu);
Gleb Natapov1871c602010-02-10 14:21:32 +02003659 if (ret == X86EMUL_PROPAGATE_FAULT)
Izik Eidus0f346072008-12-29 01:42:20 +02003660 return 1;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003661 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003662 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003663 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003664 ret = 1;
3665 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003666 }
3667 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003668
3669 return ret;
3670}
3671EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3672
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003673static void bounce_off(void *info)
3674{
3675 /* nothing */
3676}
3677
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003678static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3679 void *data)
3680{
3681 struct cpufreq_freqs *freq = data;
3682 struct kvm *kvm;
3683 struct kvm_vcpu *vcpu;
3684 int i, send_ipi = 0;
3685
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003686 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3687 return 0;
3688 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3689 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003690 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003691
3692 spin_lock(&kvm_lock);
3693 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003694 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003695 if (vcpu->cpu != freq->cpu)
3696 continue;
3697 if (!kvm_request_guest_time_update(vcpu))
3698 continue;
3699 if (vcpu->cpu != smp_processor_id())
3700 send_ipi++;
3701 }
3702 }
3703 spin_unlock(&kvm_lock);
3704
3705 if (freq->old < freq->new && send_ipi) {
3706 /*
3707 * We upscale the frequency. Must make the guest
3708 * doesn't see old kvmclock values while running with
3709 * the new frequency, otherwise we risk the guest sees
3710 * time go backwards.
3711 *
3712 * In case we update the frequency for another cpu
3713 * (which might be in guest context) send an interrupt
3714 * to kick the cpu out of guest context. Next time
3715 * guest context is entered kvmclock will be updated,
3716 * so the guest will not see stale values.
3717 */
3718 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3719 }
3720 return 0;
3721}
3722
3723static struct notifier_block kvmclock_cpufreq_notifier_block = {
3724 .notifier_call = kvmclock_cpufreq_notifier
3725};
3726
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003727static void kvm_timer_init(void)
3728{
3729 int cpu;
3730
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003731 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003732 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3733 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003734 for_each_online_cpu(cpu) {
3735 unsigned long khz = cpufreq_get(cpu);
3736 if (!khz)
3737 khz = tsc_khz;
3738 per_cpu(cpu_tsc_khz, cpu) = khz;
3739 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003740 } else {
3741 for_each_possible_cpu(cpu)
3742 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003743 }
3744}
3745
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003746int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003747{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003748 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003749 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3750
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003751 if (kvm_x86_ops) {
3752 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003753 r = -EEXIST;
3754 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003755 }
3756
3757 if (!ops->cpu_has_kvm_support()) {
3758 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003759 r = -EOPNOTSUPP;
3760 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003761 }
3762 if (ops->disabled_by_bios()) {
3763 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003764 r = -EOPNOTSUPP;
3765 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003766 }
3767
Avi Kivity97db56c2008-01-13 13:23:56 +02003768 r = kvm_mmu_module_init();
3769 if (r)
3770 goto out;
3771
3772 kvm_init_msr_list();
3773
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003774 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003775 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003776 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3777 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003778 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003779
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003780 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003781
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003782 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003783
3784out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003785 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003786}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003787
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003788void kvm_arch_exit(void)
3789{
Jan Kiszka888d2562009-04-17 19:24:58 +02003790 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3791 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3792 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003793 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003794 kvm_mmu_module_exit();
3795}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003796
Hollis Blanchard8776e512007-10-31 17:24:24 -05003797int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3798{
3799 ++vcpu->stat.halt_exits;
3800 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003801 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003802 return 1;
3803 } else {
3804 vcpu->run->exit_reason = KVM_EXIT_HLT;
3805 return 0;
3806 }
3807}
3808EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3809
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003810static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3811 unsigned long a1)
3812{
3813 if (is_long_mode(vcpu))
3814 return a0;
3815 else
3816 return a0 | ((gpa_t)a1 << 32);
3817}
3818
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003819int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
3820{
3821 u64 param, ingpa, outgpa, ret;
3822 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
3823 bool fast, longmode;
3824 int cs_db, cs_l;
3825
3826 /*
3827 * hypercall generates UD from non zero cpl and real mode
3828 * per HYPER-V spec
3829 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003830 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003831 kvm_queue_exception(vcpu, UD_VECTOR);
3832 return 0;
3833 }
3834
3835 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3836 longmode = is_long_mode(vcpu) && cs_l == 1;
3837
3838 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02003839 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
3840 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
3841 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
3842 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
3843 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
3844 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003845 }
3846#ifdef CONFIG_X86_64
3847 else {
3848 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
3849 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
3850 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
3851 }
3852#endif
3853
3854 code = param & 0xffff;
3855 fast = (param >> 16) & 0x1;
3856 rep_cnt = (param >> 32) & 0xfff;
3857 rep_idx = (param >> 48) & 0xfff;
3858
3859 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
3860
Gleb Natapovc25bc162010-01-17 15:51:24 +02003861 switch (code) {
3862 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
3863 kvm_vcpu_on_spin(vcpu);
3864 break;
3865 default:
3866 res = HV_STATUS_INVALID_HYPERCALL_CODE;
3867 break;
3868 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003869
3870 ret = res | (((u64)rep_done & 0xfff) << 32);
3871 if (longmode) {
3872 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3873 } else {
3874 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
3875 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
3876 }
3877
3878 return 1;
3879}
3880
Hollis Blanchard8776e512007-10-31 17:24:24 -05003881int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3882{
3883 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003884 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003885
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003886 if (kvm_hv_hypercall_enabled(vcpu->kvm))
3887 return kvm_hv_hypercall(vcpu);
3888
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003889 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3890 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3891 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3892 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3893 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003894
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003895 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003896
Hollis Blanchard8776e512007-10-31 17:24:24 -05003897 if (!is_long_mode(vcpu)) {
3898 nr &= 0xFFFFFFFF;
3899 a0 &= 0xFFFFFFFF;
3900 a1 &= 0xFFFFFFFF;
3901 a2 &= 0xFFFFFFFF;
3902 a3 &= 0xFFFFFFFF;
3903 }
3904
Jan Kiszka07708c42009-08-03 18:43:28 +02003905 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3906 ret = -KVM_EPERM;
3907 goto out;
3908 }
3909
Hollis Blanchard8776e512007-10-31 17:24:24 -05003910 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003911 case KVM_HC_VAPIC_POLL_IRQ:
3912 ret = 0;
3913 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003914 case KVM_HC_MMU_OP:
3915 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3916 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003917 default:
3918 ret = -KVM_ENOSYS;
3919 break;
3920 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003921out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003922 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303923 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003924 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003925}
3926EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3927
3928int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3929{
3930 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003931 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003932
Hollis Blanchard8776e512007-10-31 17:24:24 -05003933 /*
3934 * Blow out the MMU to ensure that no other VCPU has an active mapping
3935 * to ensure that the updated hypercall appears atomically across all
3936 * VCPUs.
3937 */
3938 kvm_mmu_zap_all(vcpu->kvm);
3939
Hollis Blanchard8776e512007-10-31 17:24:24 -05003940 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003941
Takuya Yoshikawa7edcfac2010-02-01 22:11:52 +09003942 return emulator_write_emulated(rip, instruction, 3, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003943}
3944
3945static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3946{
3947 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3948}
3949
3950void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3951{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003952 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05003953
3954 kvm_x86_ops->set_gdt(vcpu, &dt);
3955}
3956
3957void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3958{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003959 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05003960
3961 kvm_x86_ops->set_idt(vcpu, &dt);
3962}
3963
3964void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3965 unsigned long *rflags)
3966{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003967 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02003968 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003969}
3970
3971unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3972{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003973 unsigned long value;
3974
Hollis Blanchard8776e512007-10-31 17:24:24 -05003975 switch (cr) {
3976 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02003977 value = kvm_read_cr0(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02003978 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003979 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003980 value = vcpu->arch.cr2;
3981 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003982 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003983 value = vcpu->arch.cr3;
3984 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003985 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02003986 value = kvm_read_cr4(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02003987 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003988 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003989 value = kvm_get_cr8(vcpu);
3990 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003991 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003992 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003993 return 0;
3994 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003995
3996 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003997}
3998
3999void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
4000 unsigned long *rflags)
4001{
4002 switch (cr) {
4003 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004004 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Jan Kiszka91586a32009-10-05 13:07:21 +02004005 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004006 break;
4007 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004008 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004009 break;
4010 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004011 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004012 break;
4013 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004014 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004015 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004016 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004017 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004018 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004019 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004020 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004021 }
4022}
4023
Dan Kenigsberg07716712007-11-21 17:10:04 +02004024static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4025{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004026 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4027 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004028
4029 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4030 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004031 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004032 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004033 if (ej->function == e->function) {
4034 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4035 return j;
4036 }
4037 }
4038 return 0; /* silence gcc, even though control never reaches here */
4039}
4040
4041/* find an entry with matching function, matching index (if needed), and that
4042 * should be read next (if it's stateful) */
4043static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4044 u32 function, u32 index)
4045{
4046 if (e->function != function)
4047 return 0;
4048 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4049 return 0;
4050 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004051 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004052 return 0;
4053 return 1;
4054}
4055
Alexander Grafd8017472008-11-25 20:17:11 +01004056struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4057 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004058{
4059 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004060 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004061
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004062 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004063 struct kvm_cpuid_entry2 *e;
4064
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004065 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004066 if (is_matching_cpuid_entry(e, function, index)) {
4067 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4068 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004069 best = e;
4070 break;
4071 }
4072 /*
4073 * Both basic or both extended?
4074 */
4075 if (((e->function ^ function) & 0x80000000) == 0)
4076 if (!best || e->function > best->function)
4077 best = e;
4078 }
Alexander Grafd8017472008-11-25 20:17:11 +01004079 return best;
4080}
Sheng Yang0e851882009-12-18 16:48:46 +08004081EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004082
Dong, Eddie82725b22009-03-30 16:21:08 +08004083int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4084{
4085 struct kvm_cpuid_entry2 *best;
4086
4087 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4088 if (best)
4089 return best->eax & 0xff;
4090 return 36;
4091}
4092
Alexander Grafd8017472008-11-25 20:17:11 +01004093void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4094{
4095 u32 function, index;
4096 struct kvm_cpuid_entry2 *best;
4097
4098 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4099 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4100 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4101 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4102 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4103 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4104 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004105 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004106 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4107 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4108 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4109 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004110 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004111 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004112 trace_kvm_cpuid(function,
4113 kvm_register_read(vcpu, VCPU_REGS_RAX),
4114 kvm_register_read(vcpu, VCPU_REGS_RBX),
4115 kvm_register_read(vcpu, VCPU_REGS_RCX),
4116 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004117}
4118EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004119
4120/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004121 * Check if userspace requested an interrupt window, and that the
4122 * interrupt window is open.
4123 *
4124 * No need to exit to userspace if we already have an interrupt queued.
4125 */
Avi Kivity851ba692009-08-24 11:10:17 +03004126static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004127{
Gleb Natapov80618232009-04-21 17:44:56 +03004128 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004129 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004130 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004131}
4132
Avi Kivity851ba692009-08-24 11:10:17 +03004133static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004134{
Avi Kivity851ba692009-08-24 11:10:17 +03004135 struct kvm_run *kvm_run = vcpu->run;
4136
Jan Kiszka91586a32009-10-05 13:07:21 +02004137 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004138 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004139 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004140 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004141 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004142 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004143 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004144 kvm_arch_interrupt_allowed(vcpu) &&
4145 !kvm_cpu_has_interrupt(vcpu) &&
4146 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004147}
4148
Avi Kivityb93463a2007-10-25 16:52:32 +02004149static void vapic_enter(struct kvm_vcpu *vcpu)
4150{
4151 struct kvm_lapic *apic = vcpu->arch.apic;
4152 struct page *page;
4153
4154 if (!apic || !apic->vapic_addr)
4155 return;
4156
4157 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004158
4159 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004160}
4161
4162static void vapic_exit(struct kvm_vcpu *vcpu)
4163{
4164 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004165 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004166
4167 if (!apic || !apic->vapic_addr)
4168 return;
4169
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004170 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004171 kvm_release_page_dirty(apic->vapic_page);
4172 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004173 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004174}
4175
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004176static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4177{
4178 int max_irr, tpr;
4179
4180 if (!kvm_x86_ops->update_cr8_intercept)
4181 return;
4182
Avi Kivity88c808f2009-08-17 22:49:40 +03004183 if (!vcpu->arch.apic)
4184 return;
4185
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004186 if (!vcpu->arch.apic->vapic_addr)
4187 max_irr = kvm_lapic_find_highest_irr(vcpu);
4188 else
4189 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004190
4191 if (max_irr != -1)
4192 max_irr >>= 4;
4193
4194 tpr = kvm_lapic_get_cr8(vcpu);
4195
4196 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4197}
4198
Avi Kivity851ba692009-08-24 11:10:17 +03004199static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004200{
4201 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004202 if (vcpu->arch.exception.pending) {
4203 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4204 vcpu->arch.exception.has_error_code,
4205 vcpu->arch.exception.error_code);
4206 return;
4207 }
4208
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004209 if (vcpu->arch.nmi_injected) {
4210 kvm_x86_ops->set_nmi(vcpu);
4211 return;
4212 }
4213
4214 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004215 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004216 return;
4217 }
4218
4219 /* try to inject new event if pending */
4220 if (vcpu->arch.nmi_pending) {
4221 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4222 vcpu->arch.nmi_pending = false;
4223 vcpu->arch.nmi_injected = true;
4224 kvm_x86_ops->set_nmi(vcpu);
4225 }
4226 } else if (kvm_cpu_has_interrupt(vcpu)) {
4227 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004228 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4229 false);
4230 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004231 }
4232 }
4233}
4234
Avi Kivity851ba692009-08-24 11:10:17 +03004235static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004236{
4237 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004238 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004239 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004240
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004241 if (vcpu->requests)
4242 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4243 kvm_mmu_unload(vcpu);
4244
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004245 r = kvm_mmu_reload(vcpu);
4246 if (unlikely(r))
4247 goto out;
4248
Avi Kivity2f52d582008-01-16 12:49:30 +02004249 if (vcpu->requests) {
4250 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004251 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004252 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4253 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004254 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4255 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004256 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4257 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004258 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4259 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004260 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004261 r = 0;
4262 goto out;
4263 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004264 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004265 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004266 r = 0;
4267 goto out;
4268 }
Avi Kivity02daab22009-12-30 12:40:26 +02004269 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4270 vcpu->fpu_active = 0;
4271 kvm_x86_ops->fpu_deactivate(vcpu);
4272 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004273 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004274
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004275 preempt_disable();
4276
4277 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004278 if (vcpu->fpu_active)
4279 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004280
4281 local_irq_disable();
4282
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004283 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4284 smp_mb__after_clear_bit();
4285
Marcelo Tosattid7690172008-09-08 15:23:48 -03004286 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004287 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c1428012008-01-15 18:27:32 +02004288 local_irq_enable();
4289 preempt_enable();
4290 r = 1;
4291 goto out;
4292 }
4293
Avi Kivity851ba692009-08-24 11:10:17 +03004294 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004295
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004296 /* enable NMI/IRQ window open exits if needed */
4297 if (vcpu->arch.nmi_pending)
4298 kvm_x86_ops->enable_nmi_window(vcpu);
4299 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4300 kvm_x86_ops->enable_irq_window(vcpu);
4301
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004302 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004303 update_cr8_intercept(vcpu);
4304 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004305 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004306
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004307 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004308
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004309 kvm_guest_enter();
4310
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004311 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004312 set_debugreg(0, 7);
4313 set_debugreg(vcpu->arch.eff_db[0], 0);
4314 set_debugreg(vcpu->arch.eff_db[1], 1);
4315 set_debugreg(vcpu->arch.eff_db[2], 2);
4316 set_debugreg(vcpu->arch.eff_db[3], 3);
4317 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004318
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004319 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004320 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004321
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004322 /*
4323 * If the guest has used debug registers, at least dr7
4324 * will be disabled while returning to the host.
4325 * If we don't have active breakpoints in the host, we don't
4326 * care about the messed up debug address registers. But if
4327 * we have some of them active, restore the old state.
4328 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004329 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004330 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004331
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004332 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004333 local_irq_enable();
4334
4335 ++vcpu->stat.exits;
4336
4337 /*
4338 * We must have an instruction between local_irq_enable() and
4339 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4340 * the interrupt shadow. The stat.exits increment will do nicely.
4341 * But we need to prevent reordering, hence this barrier():
4342 */
4343 barrier();
4344
4345 kvm_guest_exit();
4346
4347 preempt_enable();
4348
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004349 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004350
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004351 /*
4352 * Profile KVM exit RIPs:
4353 */
4354 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004355 unsigned long rip = kvm_rip_read(vcpu);
4356 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004357 }
4358
Avi Kivity298101d2007-11-25 13:41:11 +02004359
Avi Kivityb93463a2007-10-25 16:52:32 +02004360 kvm_lapic_sync_from_vapic(vcpu);
4361
Avi Kivity851ba692009-08-24 11:10:17 +03004362 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004363out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004364 return r;
4365}
4366
Gleb Natapov09cec752009-03-23 15:11:44 +02004367
Avi Kivity851ba692009-08-24 11:10:17 +03004368static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004369{
4370 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004371 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004372
4373 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004374 pr_debug("vcpu %d received sipi with vector # %x\n",
4375 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004376 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004377 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004378 if (r)
4379 return r;
4380 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004381 }
4382
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004383 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004384 vapic_enter(vcpu);
4385
4386 r = 1;
4387 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004388 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004389 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004390 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004391 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004392 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004393 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004394 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004395 {
4396 switch(vcpu->arch.mp_state) {
4397 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004398 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004399 KVM_MP_STATE_RUNNABLE;
4400 case KVM_MP_STATE_RUNNABLE:
4401 break;
4402 case KVM_MP_STATE_SIPI_RECEIVED:
4403 default:
4404 r = -EINTR;
4405 break;
4406 }
4407 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004408 }
4409
Gleb Natapov09cec752009-03-23 15:11:44 +02004410 if (r <= 0)
4411 break;
4412
4413 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4414 if (kvm_cpu_has_pending_timer(vcpu))
4415 kvm_inject_pending_timer_irqs(vcpu);
4416
Avi Kivity851ba692009-08-24 11:10:17 +03004417 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004418 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004419 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004420 ++vcpu->stat.request_irq_exits;
4421 }
4422 if (signal_pending(current)) {
4423 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004424 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004425 ++vcpu->stat.signal_exits;
4426 }
4427 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004428 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004429 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004430 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004431 }
4432 }
4433
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004434 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Avi Kivity851ba692009-08-24 11:10:17 +03004435 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004436
Avi Kivityb93463a2007-10-25 16:52:32 +02004437 vapic_exit(vcpu);
4438
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004439 return r;
4440}
4441
4442int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4443{
4444 int r;
4445 sigset_t sigsaved;
4446
4447 vcpu_load(vcpu);
4448
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004449 if (vcpu->sigset_active)
4450 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4451
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004452 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4453 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004454 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004455 r = -EAGAIN;
4456 goto out;
4457 }
4458
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004459 /* re-sync apic's tpr */
4460 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004461 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004462
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004463 if (vcpu->arch.pio.cur_count) {
Gleb Natapov7567cae2010-03-09 12:01:10 +02004464 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004465 r = complete_pio(vcpu);
Gleb Natapov7567cae2010-03-09 12:01:10 +02004466 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004467 if (r)
4468 goto out;
4469 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004470 if (vcpu->mmio_needed) {
4471 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4472 vcpu->mmio_read_completed = 1;
4473 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004474
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004475 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity851ba692009-08-24 11:10:17 +03004476 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004477 EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004478 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004479 if (r == EMULATE_DO_MMIO) {
4480 /*
4481 * Read-modify-write. Back to userspace.
4482 */
4483 r = 0;
4484 goto out;
4485 }
4486 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004487 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4488 kvm_register_write(vcpu, VCPU_REGS_RAX,
4489 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004490
Avi Kivity851ba692009-08-24 11:10:17 +03004491 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004492
4493out:
4494 if (vcpu->sigset_active)
4495 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4496
4497 vcpu_put(vcpu);
4498 return r;
4499}
4500
4501int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4502{
4503 vcpu_load(vcpu);
4504
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004505 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4506 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4507 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4508 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4509 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4510 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4511 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4512 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004513#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004514 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4515 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4516 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4517 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4518 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4519 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4520 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4521 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004522#endif
4523
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004524 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004525 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004526
4527 vcpu_put(vcpu);
4528
4529 return 0;
4530}
4531
4532int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4533{
4534 vcpu_load(vcpu);
4535
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004536 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4537 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4538 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4539 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4540 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4541 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4542 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4543 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004544#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004545 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4546 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4547 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4548 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4549 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4550 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4551 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4552 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004553#endif
4554
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004555 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004556 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004557
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004558 vcpu->arch.exception.pending = false;
4559
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004560 vcpu_put(vcpu);
4561
4562 return 0;
4563}
4564
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004565void kvm_get_segment(struct kvm_vcpu *vcpu,
4566 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004567{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004568 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004569}
4570
4571void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4572{
4573 struct kvm_segment cs;
4574
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004575 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004576 *db = cs.db;
4577 *l = cs.l;
4578}
4579EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4580
4581int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4582 struct kvm_sregs *sregs)
4583{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004584 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004585
4586 vcpu_load(vcpu);
4587
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004588 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4589 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4590 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4591 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4592 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4593 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004594
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004595 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4596 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004597
4598 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004599 sregs->idt.limit = dt.size;
4600 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004601 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004602 sregs->gdt.limit = dt.size;
4603 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004604
Avi Kivity4d4ec082009-12-29 18:07:30 +02004605 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004606 sregs->cr2 = vcpu->arch.cr2;
4607 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004608 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004609 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02004610 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004611 sregs->apic_base = kvm_get_apic_base(vcpu);
4612
Gleb Natapov923c61b2009-05-11 13:35:48 +03004613 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004614
Gleb Natapov36752c92009-05-11 13:35:53 +03004615 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004616 set_bit(vcpu->arch.interrupt.nr,
4617 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004618
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004619 vcpu_put(vcpu);
4620
4621 return 0;
4622}
4623
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004624int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4625 struct kvm_mp_state *mp_state)
4626{
4627 vcpu_load(vcpu);
4628 mp_state->mp_state = vcpu->arch.mp_state;
4629 vcpu_put(vcpu);
4630 return 0;
4631}
4632
4633int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4634 struct kvm_mp_state *mp_state)
4635{
4636 vcpu_load(vcpu);
4637 vcpu->arch.mp_state = mp_state->mp_state;
4638 vcpu_put(vcpu);
4639 return 0;
4640}
4641
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004642static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004643 struct kvm_segment *var, int seg)
4644{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004645 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004646}
4647
Izik Eidus37817f22008-03-24 23:14:53 +02004648static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4649 struct kvm_segment *kvm_desct)
4650{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004651 kvm_desct->base = get_desc_base(seg_desc);
4652 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004653 if (seg_desc->g) {
4654 kvm_desct->limit <<= 12;
4655 kvm_desct->limit |= 0xfff;
4656 }
Izik Eidus37817f22008-03-24 23:14:53 +02004657 kvm_desct->selector = selector;
4658 kvm_desct->type = seg_desc->type;
4659 kvm_desct->present = seg_desc->p;
4660 kvm_desct->dpl = seg_desc->dpl;
4661 kvm_desct->db = seg_desc->d;
4662 kvm_desct->s = seg_desc->s;
4663 kvm_desct->l = seg_desc->l;
4664 kvm_desct->g = seg_desc->g;
4665 kvm_desct->avl = seg_desc->avl;
4666 if (!selector)
4667 kvm_desct->unusable = 1;
4668 else
4669 kvm_desct->unusable = 0;
4670 kvm_desct->padding = 0;
4671}
4672
Amit Shahb8222ad2008-10-22 16:39:47 +05304673static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4674 u16 selector,
Gleb Natapov89a27f42010-02-16 10:51:48 +02004675 struct desc_ptr *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004676{
4677 if (selector & 1 << 2) {
4678 struct kvm_segment kvm_seg;
4679
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004680 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004681
4682 if (kvm_seg.unusable)
Gleb Natapov89a27f42010-02-16 10:51:48 +02004683 dtable->size = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004684 else
Gleb Natapov89a27f42010-02-16 10:51:48 +02004685 dtable->size = kvm_seg.limit;
4686 dtable->address = kvm_seg.base;
Izik Eidus37817f22008-03-24 23:14:53 +02004687 }
4688 else
4689 kvm_x86_ops->get_gdt(vcpu, dtable);
4690}
4691
4692/* allowed just for 8 bytes segments */
4693static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4694 struct desc_struct *seg_desc)
4695{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004696 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004697 u16 index = selector >> 3;
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004698 int ret;
4699 u32 err;
4700 gva_t addr;
Izik Eidus37817f22008-03-24 23:14:53 +02004701
Amit Shahb8222ad2008-10-22 16:39:47 +05304702 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004703
Gleb Natapov89a27f42010-02-16 10:51:48 +02004704 if (dtable.size < index * 8 + 7) {
Izik Eidus37817f22008-03-24 23:14:53 +02004705 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
Takuya Yoshikawac125c602010-02-01 22:11:04 +09004706 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004707 }
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004708 addr = dtable.base + index * 8;
4709 ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc),
4710 vcpu, &err);
4711 if (ret == X86EMUL_PROPAGATE_FAULT)
4712 kvm_inject_page_fault(vcpu, addr, err);
4713
4714 return ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004715}
4716
4717/* allowed just for 8 bytes segments */
4718static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4719 struct desc_struct *seg_desc)
4720{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004721 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004722 u16 index = selector >> 3;
4723
Amit Shahb8222ad2008-10-22 16:39:47 +05304724 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004725
Gleb Natapov89a27f42010-02-16 10:51:48 +02004726 if (dtable.size < index * 8 + 7)
Izik Eidus37817f22008-03-24 23:14:53 +02004727 return 1;
Gleb Natapov89a27f42010-02-16 10:51:48 +02004728 return kvm_write_guest_virt(dtable.address + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004729}
4730
Gleb Natapov1871c602010-02-10 14:21:32 +02004731static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu,
4732 struct desc_struct *seg_desc)
4733{
4734 u32 base_addr = get_desc_base(seg_desc);
4735
4736 return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL);
4737}
4738
4739static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004740 struct desc_struct *seg_desc)
4741{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004742 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004743
Gleb Natapov1871c602010-02-10 14:21:32 +02004744 return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004745}
4746
Izik Eidus37817f22008-03-24 23:14:53 +02004747static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4748{
4749 struct kvm_segment kvm_seg;
4750
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004751 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004752 return kvm_seg.selector;
4753}
4754
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004755static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004756{
4757 struct kvm_segment segvar = {
4758 .base = selector << 4,
4759 .limit = 0xffff,
4760 .selector = selector,
4761 .type = 3,
4762 .present = 1,
4763 .dpl = 3,
4764 .db = 0,
4765 .s = 1,
4766 .l = 0,
4767 .g = 0,
4768 .avl = 0,
4769 .unusable = 0,
4770 };
4771 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
Gleb Natapovc6975182010-02-18 12:15:01 +02004772 return X86EMUL_CONTINUE;
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004773}
4774
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004775static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4776{
4777 return (seg != VCPU_SREG_LDTR) &&
4778 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004779 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004780}
4781
Gleb Natapovc6975182010-02-18 12:15:01 +02004782int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004783{
4784 struct kvm_segment kvm_seg;
Gleb Natapove01c2422010-01-25 12:01:04 +02004785 struct desc_struct seg_desc;
Gleb Natapovc6975182010-02-18 12:15:01 +02004786 u8 dpl, rpl, cpl;
4787 unsigned err_vec = GP_VECTOR;
4788 u32 err_code = 0;
4789 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
4790 int ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004791
Avi Kivity3eeb3282010-01-21 15:31:48 +02004792 if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004793 return kvm_load_realmode_segment(vcpu, selector, seg);
Gleb Natapove01c2422010-01-25 12:01:04 +02004794
Gleb Natapovc6975182010-02-18 12:15:01 +02004795 /* NULL selector is not valid for TR, CS and SS */
4796 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
4797 && null_selector)
4798 goto exception;
4799
4800 /* TR should be in GDT only */
4801 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
4802 goto exception;
4803
4804 ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc);
4805 if (ret)
4806 return ret;
4807
Gleb Natapove01c2422010-01-25 12:01:04 +02004808 seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg);
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004809
Gleb Natapovc6975182010-02-18 12:15:01 +02004810 if (null_selector) { /* for NULL selector skip all following checks */
4811 kvm_seg.unusable = 1;
4812 goto load;
4813 }
Izik Eidus37817f22008-03-24 23:14:53 +02004814
Gleb Natapovc6975182010-02-18 12:15:01 +02004815 err_code = selector & 0xfffc;
4816 err_vec = GP_VECTOR;
Izik Eidus37817f22008-03-24 23:14:53 +02004817
Gleb Natapovc6975182010-02-18 12:15:01 +02004818 /* can't load system descriptor into segment selecor */
4819 if (seg <= VCPU_SREG_GS && !kvm_seg.s)
4820 goto exception;
4821
4822 if (!kvm_seg.present) {
4823 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
4824 goto exception;
4825 }
4826
4827 rpl = selector & 3;
4828 dpl = kvm_seg.dpl;
4829 cpl = kvm_x86_ops->get_cpl(vcpu);
4830
4831 switch (seg) {
4832 case VCPU_SREG_SS:
4833 /*
4834 * segment is not a writable data segment or segment
4835 * selector's RPL != CPL or segment selector's RPL != CPL
4836 */
4837 if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl)
4838 goto exception;
4839 break;
4840 case VCPU_SREG_CS:
4841 if (!(kvm_seg.type & 8))
4842 goto exception;
4843
4844 if (kvm_seg.type & 4) {
4845 /* conforming */
4846 if (dpl > cpl)
4847 goto exception;
4848 } else {
4849 /* nonconforming */
4850 if (rpl > cpl || dpl != cpl)
4851 goto exception;
4852 }
4853 /* CS(RPL) <- CPL */
4854 selector = (selector & 0xfffc) | cpl;
4855 break;
4856 case VCPU_SREG_TR:
4857 if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9))
4858 goto exception;
4859 break;
4860 case VCPU_SREG_LDTR:
4861 if (kvm_seg.s || kvm_seg.type != 2)
4862 goto exception;
4863 break;
4864 default: /* DS, ES, FS, or GS */
4865 /*
4866 * segment is not a data or readable code segment or
4867 * ((segment is a data or nonconforming code segment)
4868 * and (both RPL and CPL > DPL))
4869 */
4870 if ((kvm_seg.type & 0xa) == 0x8 ||
4871 (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl)))
4872 goto exception;
4873 break;
4874 }
4875
4876 if (!kvm_seg.unusable && kvm_seg.s) {
Gleb Natapove01c2422010-01-25 12:01:04 +02004877 /* mark segment as accessed */
Gleb Natapovc6975182010-02-18 12:15:01 +02004878 kvm_seg.type |= 1;
Gleb Natapove01c2422010-01-25 12:01:04 +02004879 seg_desc.type |= 1;
4880 save_guest_segment_descriptor(vcpu, selector, &seg_desc);
4881 }
Gleb Natapovc6975182010-02-18 12:15:01 +02004882load:
4883 kvm_set_segment(vcpu, &kvm_seg, seg);
4884 return X86EMUL_CONTINUE;
4885exception:
4886 kvm_queue_exception_e(vcpu, err_vec, err_code);
4887 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004888}
4889
4890static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4891 struct tss_segment_32 *tss)
4892{
4893 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004894 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004895 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004896 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4897 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4898 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4899 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4900 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4901 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4902 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4903 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004904 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4905 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4906 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4907 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4908 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4909 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4910 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004911}
4912
Gleb Natapovc6975182010-02-18 12:15:01 +02004913static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg)
4914{
4915 struct kvm_segment kvm_seg;
4916 kvm_get_segment(vcpu, &kvm_seg, seg);
4917 kvm_seg.selector = sel;
4918 kvm_set_segment(vcpu, &kvm_seg, seg);
4919}
4920
Izik Eidus37817f22008-03-24 23:14:53 +02004921static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4922 struct tss_segment_32 *tss)
4923{
4924 kvm_set_cr3(vcpu, tss->cr3);
4925
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004926 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004927 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004928
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004929 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4930 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4931 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4932 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4933 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4934 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4935 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4936 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004937
Gleb Natapovc6975182010-02-18 12:15:01 +02004938 /*
4939 * SDM says that segment selectors are loaded before segment
4940 * descriptors
4941 */
4942 kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR);
4943 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
4944 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
4945 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
4946 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
4947 kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS);
4948 kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS);
4949
4950 /*
4951 * Now load segment descriptors. If fault happenes at this stage
4952 * it is handled in a context of new task
4953 */
4954 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004955 return 1;
4956
Gleb Natapovc6975182010-02-18 12:15:01 +02004957 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004958 return 1;
4959
Gleb Natapovc6975182010-02-18 12:15:01 +02004960 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004961 return 1;
4962
Gleb Natapovc6975182010-02-18 12:15:01 +02004963 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004964 return 1;
4965
Gleb Natapovc6975182010-02-18 12:15:01 +02004966 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004967 return 1;
4968
Gleb Natapovc6975182010-02-18 12:15:01 +02004969 if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004970 return 1;
4971
Gleb Natapovc6975182010-02-18 12:15:01 +02004972 if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004973 return 1;
4974 return 0;
4975}
4976
4977static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4978 struct tss_segment_16 *tss)
4979{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004980 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004981 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004982 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4983 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4984 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4985 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4986 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4987 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4988 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4989 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004990
4991 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4992 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4993 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4994 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4995 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004996}
4997
4998static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4999 struct tss_segment_16 *tss)
5000{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005001 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02005002 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005003 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
5004 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
5005 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
5006 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
5007 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
5008 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
5009 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
5010 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02005011
Gleb Natapovc6975182010-02-18 12:15:01 +02005012 /*
5013 * SDM says that segment selectors are loaded before segment
5014 * descriptors
5015 */
5016 kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR);
5017 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5018 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5019 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5020 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5021
5022 /*
5023 * Now load segment descriptors. If fault happenes at this stage
5024 * it is handled in a context of new task
5025 */
5026 if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005027 return 1;
5028
Gleb Natapovc6975182010-02-18 12:15:01 +02005029 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005030 return 1;
5031
Gleb Natapovc6975182010-02-18 12:15:01 +02005032 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005033 return 1;
5034
Gleb Natapovc6975182010-02-18 12:15:01 +02005035 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005036 return 1;
5037
Gleb Natapovc6975182010-02-18 12:15:01 +02005038 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005039 return 1;
5040 return 0;
5041}
5042
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005043static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005044 u16 old_tss_sel, u32 old_tss_base,
5045 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02005046{
5047 struct tss_segment_16 tss_segment_16;
5048 int ret = 0;
5049
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005050 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5051 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005052 goto out;
5053
5054 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02005055
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005056 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5057 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005058 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005059
Gleb Natapov1871c602010-02-10 14:21:32 +02005060 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005061 &tss_segment_16, sizeof tss_segment_16))
5062 goto out;
5063
Gleb Natapovb237ac32009-03-30 16:03:24 +03005064 if (old_tss_sel != 0xffff) {
5065 tss_segment_16.prev_task_link = old_tss_sel;
5066
5067 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005068 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005069 &tss_segment_16.prev_task_link,
5070 sizeof tss_segment_16.prev_task_link))
5071 goto out;
5072 }
5073
Izik Eidus37817f22008-03-24 23:14:53 +02005074 if (load_state_from_tss16(vcpu, &tss_segment_16))
5075 goto out;
5076
5077 ret = 1;
5078out:
5079 return ret;
5080}
5081
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005082static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005083 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02005084 struct desc_struct *nseg_desc)
5085{
5086 struct tss_segment_32 tss_segment_32;
5087 int ret = 0;
5088
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005089 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5090 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005091 goto out;
5092
5093 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02005094
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005095 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5096 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005097 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005098
Gleb Natapov1871c602010-02-10 14:21:32 +02005099 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005100 &tss_segment_32, sizeof tss_segment_32))
5101 goto out;
5102
Gleb Natapovb237ac32009-03-30 16:03:24 +03005103 if (old_tss_sel != 0xffff) {
5104 tss_segment_32.prev_task_link = old_tss_sel;
5105
5106 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005107 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005108 &tss_segment_32.prev_task_link,
5109 sizeof tss_segment_32.prev_task_link))
5110 goto out;
5111 }
5112
Izik Eidus37817f22008-03-24 23:14:53 +02005113 if (load_state_from_tss32(vcpu, &tss_segment_32))
5114 goto out;
5115
5116 ret = 1;
5117out:
5118 return ret;
5119}
5120
5121int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
5122{
5123 struct kvm_segment tr_seg;
5124 struct desc_struct cseg_desc;
5125 struct desc_struct nseg_desc;
5126 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005127 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
5128 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005129 u32 desc_limit;
Izik Eidus37817f22008-03-24 23:14:53 +02005130
Gleb Natapov1871c602010-02-10 14:21:32 +02005131 old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02005132
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005133 /* FIXME: Handle errors. Failure to read either TSS or their
5134 * descriptors should generate a pagefault.
5135 */
Izik Eidus37817f22008-03-24 23:14:53 +02005136 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
5137 goto out;
5138
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005139 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02005140 goto out;
5141
Izik Eidus37817f22008-03-24 23:14:53 +02005142 if (reason != TASK_SWITCH_IRET) {
5143 int cpl;
5144
5145 cpl = kvm_x86_ops->get_cpl(vcpu);
5146 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
5147 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
5148 return 1;
5149 }
5150 }
5151
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005152 desc_limit = get_desc_limit(&nseg_desc);
5153 if (!nseg_desc.p ||
5154 ((desc_limit < 0x67 && (nseg_desc.type & 8)) ||
5155 desc_limit < 0x2b)) {
Izik Eidus37817f22008-03-24 23:14:53 +02005156 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
5157 return 1;
5158 }
5159
5160 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005161 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005162 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005163 }
5164
5165 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005166 u32 eflags = kvm_get_rflags(vcpu);
5167 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005168 }
5169
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005170 /* set back link to prev task only if NT bit is set in eflags
5171 note that old_tss_sel is not used afetr this point */
5172 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
5173 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02005174
5175 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03005176 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
5177 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005178 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03005179 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
5180 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005181
5182 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005183 u32 eflags = kvm_get_rflags(vcpu);
5184 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005185 }
5186
5187 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005188 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02005189 save_guest_segment_descriptor(vcpu, tss_selector,
5190 &nseg_desc);
5191 }
5192
Avi Kivity4d4ec082009-12-29 18:07:30 +02005193 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS);
Izik Eidus37817f22008-03-24 23:14:53 +02005194 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
5195 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005196 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02005197out:
Izik Eidus37817f22008-03-24 23:14:53 +02005198 return ret;
5199}
5200EXPORT_SYMBOL_GPL(kvm_task_switch);
5201
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005202int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5203 struct kvm_sregs *sregs)
5204{
5205 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03005206 int pending_vec, max_bits;
Gleb Natapov89a27f42010-02-16 10:51:48 +02005207 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005208
5209 vcpu_load(vcpu);
5210
Gleb Natapov89a27f42010-02-16 10:51:48 +02005211 dt.size = sregs->idt.limit;
5212 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005213 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005214 dt.size = sregs->gdt.limit;
5215 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005216 kvm_x86_ops->set_gdt(vcpu, &dt);
5217
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005218 vcpu->arch.cr2 = sregs->cr2;
5219 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02005220 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005221
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005222 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005223
Avi Kivityf6801df2010-01-21 15:31:50 +02005224 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005225 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005226 kvm_set_apic_base(vcpu, sregs->apic_base);
5227
Avi Kivity4d4ec082009-12-29 18:07:30 +02005228 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005229 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00005230 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005231
Avi Kivityfc78f512009-12-07 12:16:48 +02005232 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005233 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005234 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005235 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005236 mmu_reset_needed = 1;
5237 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005238
5239 if (mmu_reset_needed)
5240 kvm_mmu_reset_context(vcpu);
5241
Gleb Natapov923c61b2009-05-11 13:35:48 +03005242 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5243 pending_vec = find_first_bit(
5244 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5245 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005246 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03005247 pr_debug("Set back pending irq %d\n", pending_vec);
5248 if (irqchip_in_kernel(vcpu->kvm))
5249 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005250 }
5251
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005252 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5253 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5254 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5255 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5256 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5257 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005258
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005259 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5260 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005261
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03005262 update_cr8_intercept(vcpu);
5263
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005264 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005265 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005266 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02005267 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005268 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5269
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005270 vcpu_put(vcpu);
5271
5272 return 0;
5273}
5274
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005275int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5276 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005277{
Jan Kiszka355be0b2009-10-03 00:31:21 +02005278 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005279 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005280
5281 vcpu_load(vcpu);
5282
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005283 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5284 r = -EBUSY;
5285 if (vcpu->arch.exception.pending)
5286 goto unlock_out;
5287 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5288 kvm_queue_exception(vcpu, DB_VECTOR);
5289 else
5290 kvm_queue_exception(vcpu, BP_VECTOR);
5291 }
5292
Jan Kiszka91586a32009-10-05 13:07:21 +02005293 /*
5294 * Read rflags as long as potentially injected trace flags are still
5295 * filtered out.
5296 */
5297 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02005298
5299 vcpu->guest_debug = dbg->control;
5300 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5301 vcpu->guest_debug = 0;
5302
5303 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005304 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5305 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5306 vcpu->arch.switch_db_regs =
5307 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5308 } else {
5309 for (i = 0; i < KVM_NR_DB_REGS; i++)
5310 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5311 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5312 }
5313
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005314 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
5315 vcpu->arch.singlestep_cs =
5316 get_segment_selector(vcpu, VCPU_SREG_CS);
5317 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
5318 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005319
Jan Kiszka91586a32009-10-05 13:07:21 +02005320 /*
5321 * Trigger an rflags update that will inject or remove the trace
5322 * flags.
5323 */
5324 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005325
Jan Kiszka355be0b2009-10-03 00:31:21 +02005326 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5327
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005328 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005329
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005330unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005331 vcpu_put(vcpu);
5332
5333 return r;
5334}
5335
5336/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005337 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5338 * we have asm/x86/processor.h
5339 */
5340struct fxsave {
5341 u16 cwd;
5342 u16 swd;
5343 u16 twd;
5344 u16 fop;
5345 u64 rip;
5346 u64 rdp;
5347 u32 mxcsr;
5348 u32 mxcsr_mask;
5349 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5350#ifdef CONFIG_X86_64
5351 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5352#else
5353 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5354#endif
5355};
5356
Zhang Xiantao8b006792007-11-16 13:05:55 +08005357/*
5358 * Translate a guest virtual address to a guest physical address.
5359 */
5360int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5361 struct kvm_translation *tr)
5362{
5363 unsigned long vaddr = tr->linear_address;
5364 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005365 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005366
5367 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005368 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005369 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005370 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005371 tr->physical_address = gpa;
5372 tr->valid = gpa != UNMAPPED_GVA;
5373 tr->writeable = 1;
5374 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005375 vcpu_put(vcpu);
5376
5377 return 0;
5378}
5379
Hollis Blanchardd0752062007-10-31 17:24:25 -05005380int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5381{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005382 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005383
5384 vcpu_load(vcpu);
5385
5386 memcpy(fpu->fpr, fxsave->st_space, 128);
5387 fpu->fcw = fxsave->cwd;
5388 fpu->fsw = fxsave->swd;
5389 fpu->ftwx = fxsave->twd;
5390 fpu->last_opcode = fxsave->fop;
5391 fpu->last_ip = fxsave->rip;
5392 fpu->last_dp = fxsave->rdp;
5393 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5394
5395 vcpu_put(vcpu);
5396
5397 return 0;
5398}
5399
5400int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5401{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005402 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005403
5404 vcpu_load(vcpu);
5405
5406 memcpy(fxsave->st_space, fpu->fpr, 128);
5407 fxsave->cwd = fpu->fcw;
5408 fxsave->swd = fpu->fsw;
5409 fxsave->twd = fpu->ftwx;
5410 fxsave->fop = fpu->last_opcode;
5411 fxsave->rip = fpu->last_ip;
5412 fxsave->rdp = fpu->last_dp;
5413 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5414
5415 vcpu_put(vcpu);
5416
5417 return 0;
5418}
5419
5420void fx_init(struct kvm_vcpu *vcpu)
5421{
5422 unsigned after_mxcsr_mask;
5423
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005424 /*
5425 * Touch the fpu the first time in non atomic context as if
5426 * this is the first fpu instruction the exception handler
5427 * will fire before the instruction returns and it'll have to
5428 * allocate ram with GFP_KERNEL.
5429 */
5430 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005431 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005432
Hollis Blanchardd0752062007-10-31 17:24:25 -05005433 /* Initialize guest FPU by resetting ours and saving into guest's */
5434 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005435 kvm_fx_save(&vcpu->arch.host_fx_image);
5436 kvm_fx_finit();
5437 kvm_fx_save(&vcpu->arch.guest_fx_image);
5438 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005439 preempt_enable();
5440
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005441 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005442 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005443 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5444 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005445 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5446}
5447EXPORT_SYMBOL_GPL(fx_init);
5448
5449void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5450{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005451 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005452 return;
5453
5454 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005455 kvm_fx_save(&vcpu->arch.host_fx_image);
5456 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Avi Kivity0c048512010-01-21 15:31:52 +02005457 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005458}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005459
5460void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5461{
5462 if (!vcpu->guest_fpu_loaded)
5463 return;
5464
5465 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005466 kvm_fx_save(&vcpu->arch.guest_fx_image);
5467 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005468 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005469 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Avi Kivity0c048512010-01-21 15:31:52 +02005470 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005471}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005472
5473void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5474{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005475 if (vcpu->arch.time_page) {
5476 kvm_release_page_dirty(vcpu->arch.time_page);
5477 vcpu->arch.time_page = NULL;
5478 }
5479
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005480 kvm_x86_ops->vcpu_free(vcpu);
5481}
5482
5483struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5484 unsigned int id)
5485{
Avi Kivity26e52152007-11-20 15:30:24 +02005486 return kvm_x86_ops->vcpu_create(kvm, id);
5487}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005488
Avi Kivity26e52152007-11-20 15:30:24 +02005489int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5490{
5491 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005492
5493 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005494 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005495
Sheng Yang0bed3b52008-10-09 16:01:54 +08005496 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005497 vcpu_load(vcpu);
5498 r = kvm_arch_vcpu_reset(vcpu);
5499 if (r == 0)
5500 r = kvm_mmu_setup(vcpu);
5501 vcpu_put(vcpu);
5502 if (r < 0)
5503 goto free_vcpu;
5504
Avi Kivity26e52152007-11-20 15:30:24 +02005505 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005506free_vcpu:
5507 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005508 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005509}
5510
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005511void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005512{
5513 vcpu_load(vcpu);
5514 kvm_mmu_unload(vcpu);
5515 vcpu_put(vcpu);
5516
5517 kvm_x86_ops->vcpu_free(vcpu);
5518}
5519
5520int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5521{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005522 vcpu->arch.nmi_pending = false;
5523 vcpu->arch.nmi_injected = false;
5524
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005525 vcpu->arch.switch_db_regs = 0;
5526 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5527 vcpu->arch.dr6 = DR6_FIXED_1;
5528 vcpu->arch.dr7 = DR7_FIXED_1;
5529
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005530 return kvm_x86_ops->vcpu_reset(vcpu);
5531}
5532
Alexander Graf10474ae2009-09-15 11:37:46 +02005533int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005534{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005535 /*
5536 * Since this may be called from a hotplug notifcation,
5537 * we can't get the CPU frequency directly.
5538 */
5539 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5540 int cpu = raw_smp_processor_id();
5541 per_cpu(cpu_tsc_khz, cpu) = 0;
5542 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005543
5544 kvm_shared_msr_cpu_online();
5545
Alexander Graf10474ae2009-09-15 11:37:46 +02005546 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005547}
5548
5549void kvm_arch_hardware_disable(void *garbage)
5550{
5551 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005552 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005553}
5554
5555int kvm_arch_hardware_setup(void)
5556{
5557 return kvm_x86_ops->hardware_setup();
5558}
5559
5560void kvm_arch_hardware_unsetup(void)
5561{
5562 kvm_x86_ops->hardware_unsetup();
5563}
5564
5565void kvm_arch_check_processor_compat(void *rtn)
5566{
5567 kvm_x86_ops->check_processor_compatibility(rtn);
5568}
5569
5570int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5571{
5572 struct page *page;
5573 struct kvm *kvm;
5574 int r;
5575
5576 BUG_ON(vcpu->kvm == NULL);
5577 kvm = vcpu->kvm;
5578
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005579 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005580 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005581 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005582 else
Avi Kivitya4535292008-04-13 17:54:35 +03005583 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005584
5585 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5586 if (!page) {
5587 r = -ENOMEM;
5588 goto fail;
5589 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005590 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005591
5592 r = kvm_mmu_create(vcpu);
5593 if (r < 0)
5594 goto fail_free_pio_data;
5595
5596 if (irqchip_in_kernel(kvm)) {
5597 r = kvm_create_lapic(vcpu);
5598 if (r < 0)
5599 goto fail_mmu_destroy;
5600 }
5601
Huang Ying890ca9a2009-05-11 16:48:15 +08005602 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5603 GFP_KERNEL);
5604 if (!vcpu->arch.mce_banks) {
5605 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005606 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005607 }
5608 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5609
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005610 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005611fail_free_lapic:
5612 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005613fail_mmu_destroy:
5614 kvm_mmu_destroy(vcpu);
5615fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005616 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005617fail:
5618 return r;
5619}
5620
5621void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5622{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005623 int idx;
5624
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005625 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005626 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005627 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005628 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005629 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005630 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005631}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005632
5633struct kvm *kvm_arch_create_vm(void)
5634{
5635 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5636
5637 if (!kvm)
5638 return ERR_PTR(-ENOMEM);
5639
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005640 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5641 if (!kvm->arch.aliases) {
5642 kfree(kvm);
5643 return ERR_PTR(-ENOMEM);
5644 }
5645
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005646 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005647 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005648
Sheng Yang5550af42008-10-15 20:15:06 +08005649 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5650 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5651
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005652 rdtscll(kvm->arch.vm_init_tsc);
5653
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005654 return kvm;
5655}
5656
5657static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5658{
5659 vcpu_load(vcpu);
5660 kvm_mmu_unload(vcpu);
5661 vcpu_put(vcpu);
5662}
5663
5664static void kvm_free_vcpus(struct kvm *kvm)
5665{
5666 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005667 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005668
5669 /*
5670 * Unpin any mmu pages first.
5671 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005672 kvm_for_each_vcpu(i, vcpu, kvm)
5673 kvm_unload_vcpu_mmu(vcpu);
5674 kvm_for_each_vcpu(i, vcpu, kvm)
5675 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005676
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005677 mutex_lock(&kvm->lock);
5678 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5679 kvm->vcpus[i] = NULL;
5680
5681 atomic_set(&kvm->online_vcpus, 0);
5682 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005683}
5684
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005685void kvm_arch_sync_events(struct kvm *kvm)
5686{
Sheng Yangba4cef32009-01-06 10:03:03 +08005687 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005688}
5689
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005690void kvm_arch_destroy_vm(struct kvm *kvm)
5691{
Sheng Yang6eb55812008-10-31 12:37:41 +08005692 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005693 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005694 kfree(kvm->arch.vpic);
5695 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005696 kvm_free_vcpus(kvm);
5697 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005698 if (kvm->arch.apic_access_page)
5699 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005700 if (kvm->arch.ept_identity_pagetable)
5701 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosatti64749202010-01-19 12:45:23 -02005702 cleanup_srcu_struct(&kvm->srcu);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005703 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005704 kfree(kvm);
5705}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005706
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005707int kvm_arch_prepare_memory_region(struct kvm *kvm,
5708 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005709 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005710 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005711 int user_alloc)
5712{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005713 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005714
5715 /*To keep backward compatibility with older userspace,
5716 *x86 needs to hanlde !user_alloc case.
5717 */
5718 if (!user_alloc) {
5719 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005720 unsigned long userspace_addr;
5721
Izik Eidus72dc67a2008-02-10 18:04:15 +02005722 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005723 userspace_addr = do_mmap(NULL, 0,
5724 npages * PAGE_SIZE,
5725 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005726 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005727 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005728 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005729
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005730 if (IS_ERR((void *)userspace_addr))
5731 return PTR_ERR((void *)userspace_addr);
5732
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005733 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005734 }
5735 }
5736
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005737
5738 return 0;
5739}
5740
5741void kvm_arch_commit_memory_region(struct kvm *kvm,
5742 struct kvm_userspace_memory_region *mem,
5743 struct kvm_memory_slot old,
5744 int user_alloc)
5745{
5746
5747 int npages = mem->memory_size >> PAGE_SHIFT;
5748
5749 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5750 int ret;
5751
5752 down_write(&current->mm->mmap_sem);
5753 ret = do_munmap(current->mm, old.userspace_addr,
5754 old.npages * PAGE_SIZE);
5755 up_write(&current->mm->mmap_sem);
5756 if (ret < 0)
5757 printk(KERN_WARNING
5758 "kvm_vm_ioctl_set_memory_region: "
5759 "failed to munmap memory\n");
5760 }
5761
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005762 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005763 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005764 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5765 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5766 }
5767
5768 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005769 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005770}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005771
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005772void kvm_arch_flush_shadow(struct kvm *kvm)
5773{
5774 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005775 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005776}
5777
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005778int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5779{
Avi Kivitya4535292008-04-13 17:54:35 +03005780 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005781 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5782 || vcpu->arch.nmi_pending ||
5783 (kvm_arch_interrupt_allowed(vcpu) &&
5784 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005785}
Zhang Xiantao57361992007-12-17 14:21:40 +08005786
Zhang Xiantao57361992007-12-17 14:21:40 +08005787void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5788{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005789 int me;
5790 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005791
5792 if (waitqueue_active(&vcpu->wq)) {
5793 wake_up_interruptible(&vcpu->wq);
5794 ++vcpu->stat.halt_wakeup;
5795 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005796
5797 me = get_cpu();
5798 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5799 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5800 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005801 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005802}
Gleb Natapov78646122009-03-23 12:12:11 +02005803
5804int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5805{
5806 return kvm_x86_ops->interrupt_allowed(vcpu);
5807}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005808
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005809unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5810{
5811 unsigned long rflags;
5812
5813 rflags = kvm_x86_ops->get_rflags(vcpu);
5814 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5815 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5816 return rflags;
5817}
5818EXPORT_SYMBOL_GPL(kvm_get_rflags);
5819
5820void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5821{
5822 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5823 vcpu->arch.singlestep_cs ==
5824 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5825 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5826 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5827 kvm_x86_ops->set_rflags(vcpu, rflags);
5828}
5829EXPORT_SYMBOL_GPL(kvm_set_rflags);
5830
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005831EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5832EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5833EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5834EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5835EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005836EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005837EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005838EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005839EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005840EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005841EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);