blob: 81b7af5558f939a77f64fbf23784d0837e5205fd [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 */
Joerg Roedel82494022010-02-24 18:59:16 +01001093 data &= ~(u64)0x100; /* ignore ignne emulation enable */
Andre Przywara8f1589d2009-06-24 12:44:33 +02001094 if (data != 0) {
1095 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1096 data);
1097 return 1;
1098 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001099 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001100 case MSR_FAM10H_MMIO_CONF_BASE:
1101 if (data != 0) {
1102 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1103 "0x%llx\n", data);
1104 return 1;
1105 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001106 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001107 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001108 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001109 case MSR_IA32_DEBUGCTLMSR:
1110 if (!data) {
1111 /* We support the non-activated case already */
1112 break;
1113 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1114 /* Values other than LBR and BTF are vendor-specific,
1115 thus reserved and should throw a #GP */
1116 return 1;
1117 }
1118 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1119 __func__, data);
1120 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001121 case MSR_IA32_UCODE_REV:
1122 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001123 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001124 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001125 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001126 case 0x200 ... 0x2ff:
1127 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001128 case MSR_IA32_APICBASE:
1129 kvm_set_apic_base(vcpu, data);
1130 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001131 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1132 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001133 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001134 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001135 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001136 case MSR_KVM_WALL_CLOCK:
1137 vcpu->kvm->arch.wall_clock = data;
1138 kvm_write_wall_clock(vcpu->kvm, data);
1139 break;
1140 case MSR_KVM_SYSTEM_TIME: {
1141 if (vcpu->arch.time_page) {
1142 kvm_release_page_dirty(vcpu->arch.time_page);
1143 vcpu->arch.time_page = NULL;
1144 }
1145
1146 vcpu->arch.time = data;
1147
1148 /* we verify if the enable bit is set... */
1149 if (!(data & 1))
1150 break;
1151
1152 /* ...but clean it before doing the actual write */
1153 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1154
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001155 vcpu->arch.time_page =
1156 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001157
1158 if (is_error_page(vcpu->arch.time_page)) {
1159 kvm_release_page_clean(vcpu->arch.time_page);
1160 vcpu->arch.time_page = NULL;
1161 }
1162
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001163 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001164 break;
1165 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001166 case MSR_IA32_MCG_CTL:
1167 case MSR_IA32_MCG_STATUS:
1168 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1169 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001170
1171 /* Performance counters are not protected by a CPUID bit,
1172 * so we should check all of them in the generic path for the sake of
1173 * cross vendor migration.
1174 * Writing a zero into the event select MSRs disables them,
1175 * which we perfectly emulate ;-). Any other value should be at least
1176 * reported, some guests depend on them.
1177 */
1178 case MSR_P6_EVNTSEL0:
1179 case MSR_P6_EVNTSEL1:
1180 case MSR_K7_EVNTSEL0:
1181 case MSR_K7_EVNTSEL1:
1182 case MSR_K7_EVNTSEL2:
1183 case MSR_K7_EVNTSEL3:
1184 if (data != 0)
1185 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1186 "0x%x data 0x%llx\n", msr, data);
1187 break;
1188 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1189 * so we ignore writes to make it happy.
1190 */
1191 case MSR_P6_PERFCTR0:
1192 case MSR_P6_PERFCTR1:
1193 case MSR_K7_PERFCTR0:
1194 case MSR_K7_PERFCTR1:
1195 case MSR_K7_PERFCTR2:
1196 case MSR_K7_PERFCTR3:
1197 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1198 "0x%x data 0x%llx\n", msr, data);
1199 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001200 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1201 if (kvm_hv_msr_partition_wide(msr)) {
1202 int r;
1203 mutex_lock(&vcpu->kvm->lock);
1204 r = set_msr_hyperv_pw(vcpu, msr, data);
1205 mutex_unlock(&vcpu->kvm->lock);
1206 return r;
1207 } else
1208 return set_msr_hyperv(vcpu, msr, data);
1209 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001210 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001211 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1212 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001213 if (!ignore_msrs) {
1214 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1215 msr, data);
1216 return 1;
1217 } else {
1218 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1219 msr, data);
1220 break;
1221 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001222 }
1223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1226
1227
1228/*
1229 * Reads an msr value (of 'msr_index') into 'pdata'.
1230 * Returns 0 on success, non-0 otherwise.
1231 * Assumes vcpu_load() was already called.
1232 */
1233int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1234{
1235 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1236}
1237
Avi Kivity9ba075a2008-05-26 20:06:35 +03001238static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1239{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001240 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1241
Avi Kivity9ba075a2008-05-26 20:06:35 +03001242 if (!msr_mtrr_valid(msr))
1243 return 1;
1244
Sheng Yang0bed3b52008-10-09 16:01:54 +08001245 if (msr == MSR_MTRRdefType)
1246 *pdata = vcpu->arch.mtrr_state.def_type +
1247 (vcpu->arch.mtrr_state.enabled << 10);
1248 else if (msr == MSR_MTRRfix64K_00000)
1249 *pdata = p[0];
1250 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1251 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1252 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1253 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1254 else if (msr == MSR_IA32_CR_PAT)
1255 *pdata = vcpu->arch.pat;
1256 else { /* Variable MTRRs */
1257 int idx, is_mtrr_mask;
1258 u64 *pt;
1259
1260 idx = (msr - 0x200) / 2;
1261 is_mtrr_mask = msr - 0x200 - 2 * idx;
1262 if (!is_mtrr_mask)
1263 pt =
1264 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1265 else
1266 pt =
1267 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1268 *pdata = *pt;
1269 }
1270
Avi Kivity9ba075a2008-05-26 20:06:35 +03001271 return 0;
1272}
1273
Huang Ying890ca9a2009-05-11 16:48:15 +08001274static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1275{
1276 u64 data;
1277 u64 mcg_cap = vcpu->arch.mcg_cap;
1278 unsigned bank_num = mcg_cap & 0xff;
1279
1280 switch (msr) {
1281 case MSR_IA32_P5_MC_ADDR:
1282 case MSR_IA32_P5_MC_TYPE:
1283 data = 0;
1284 break;
1285 case MSR_IA32_MCG_CAP:
1286 data = vcpu->arch.mcg_cap;
1287 break;
1288 case MSR_IA32_MCG_CTL:
1289 if (!(mcg_cap & MCG_CTL_P))
1290 return 1;
1291 data = vcpu->arch.mcg_ctl;
1292 break;
1293 case MSR_IA32_MCG_STATUS:
1294 data = vcpu->arch.mcg_status;
1295 break;
1296 default:
1297 if (msr >= MSR_IA32_MC0_CTL &&
1298 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1299 u32 offset = msr - MSR_IA32_MC0_CTL;
1300 data = vcpu->arch.mce_banks[offset];
1301 break;
1302 }
1303 return 1;
1304 }
1305 *pdata = data;
1306 return 0;
1307}
1308
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001309static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1310{
1311 u64 data = 0;
1312 struct kvm *kvm = vcpu->kvm;
1313
1314 switch (msr) {
1315 case HV_X64_MSR_GUEST_OS_ID:
1316 data = kvm->arch.hv_guest_os_id;
1317 break;
1318 case HV_X64_MSR_HYPERCALL:
1319 data = kvm->arch.hv_hypercall;
1320 break;
1321 default:
1322 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1323 return 1;
1324 }
1325
1326 *pdata = data;
1327 return 0;
1328}
1329
1330static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1331{
1332 u64 data = 0;
1333
1334 switch (msr) {
1335 case HV_X64_MSR_VP_INDEX: {
1336 int r;
1337 struct kvm_vcpu *v;
1338 kvm_for_each_vcpu(r, v, vcpu->kvm)
1339 if (v == vcpu)
1340 data = r;
1341 break;
1342 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001343 case HV_X64_MSR_EOI:
1344 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1345 case HV_X64_MSR_ICR:
1346 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1347 case HV_X64_MSR_TPR:
1348 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001349 default:
1350 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1351 return 1;
1352 }
1353 *pdata = data;
1354 return 0;
1355}
1356
Carsten Otte15c4a642007-10-30 18:44:17 +01001357int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1358{
1359 u64 data;
1360
1361 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001362 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001363 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001364 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001365 case MSR_IA32_DEBUGCTLMSR:
1366 case MSR_IA32_LASTBRANCHFROMIP:
1367 case MSR_IA32_LASTBRANCHTOIP:
1368 case MSR_IA32_LASTINTFROMIP:
1369 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301370 case MSR_K8_SYSCFG:
1371 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001372 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301373 case MSR_P6_PERFCTR0:
1374 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301375 case MSR_P6_EVNTSEL0:
1376 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301377 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301378 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001379 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001380 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001381 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001382 data = 0;
1383 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001384 case MSR_MTRRcap:
1385 data = 0x500 | KVM_NR_VAR_MTRR;
1386 break;
1387 case 0x200 ... 0x2ff:
1388 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001389 case 0xcd: /* fsb frequency */
1390 data = 3;
1391 break;
1392 case MSR_IA32_APICBASE:
1393 data = kvm_get_apic_base(vcpu);
1394 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001395 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1396 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1397 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001398 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001399 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001400 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001401 case MSR_IA32_PERF_STATUS:
1402 /* TSC increment by tick */
1403 data = 1000ULL;
1404 /* CPU multiplier */
1405 data |= (((uint64_t)4ULL) << 40);
1406 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001407 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001408 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001409 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001410 case MSR_KVM_WALL_CLOCK:
1411 data = vcpu->kvm->arch.wall_clock;
1412 break;
1413 case MSR_KVM_SYSTEM_TIME:
1414 data = vcpu->arch.time;
1415 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001416 case MSR_IA32_P5_MC_ADDR:
1417 case MSR_IA32_P5_MC_TYPE:
1418 case MSR_IA32_MCG_CAP:
1419 case MSR_IA32_MCG_CTL:
1420 case MSR_IA32_MCG_STATUS:
1421 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1422 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001423 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1424 if (kvm_hv_msr_partition_wide(msr)) {
1425 int r;
1426 mutex_lock(&vcpu->kvm->lock);
1427 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1428 mutex_unlock(&vcpu->kvm->lock);
1429 return r;
1430 } else
1431 return get_msr_hyperv(vcpu, msr, pdata);
1432 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001433 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001434 if (!ignore_msrs) {
1435 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1436 return 1;
1437 } else {
1438 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1439 data = 0;
1440 }
1441 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001442 }
1443 *pdata = data;
1444 return 0;
1445}
1446EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1447
Carsten Otte313a3dc2007-10-11 19:16:52 +02001448/*
1449 * Read or write a bunch of msrs. All parameters are kernel addresses.
1450 *
1451 * @return number of msrs set successfully.
1452 */
1453static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1454 struct kvm_msr_entry *entries,
1455 int (*do_msr)(struct kvm_vcpu *vcpu,
1456 unsigned index, u64 *data))
1457{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001458 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001459
1460 vcpu_load(vcpu);
1461
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001462 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001463 for (i = 0; i < msrs->nmsrs; ++i)
1464 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1465 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001466 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001467
1468 vcpu_put(vcpu);
1469
1470 return i;
1471}
1472
1473/*
1474 * Read or write a bunch of msrs. Parameters are user addresses.
1475 *
1476 * @return number of msrs set successfully.
1477 */
1478static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1479 int (*do_msr)(struct kvm_vcpu *vcpu,
1480 unsigned index, u64 *data),
1481 int writeback)
1482{
1483 struct kvm_msrs msrs;
1484 struct kvm_msr_entry *entries;
1485 int r, n;
1486 unsigned size;
1487
1488 r = -EFAULT;
1489 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1490 goto out;
1491
1492 r = -E2BIG;
1493 if (msrs.nmsrs >= MAX_IO_MSRS)
1494 goto out;
1495
1496 r = -ENOMEM;
1497 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1498 entries = vmalloc(size);
1499 if (!entries)
1500 goto out;
1501
1502 r = -EFAULT;
1503 if (copy_from_user(entries, user_msrs->entries, size))
1504 goto out_free;
1505
1506 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1507 if (r < 0)
1508 goto out_free;
1509
1510 r = -EFAULT;
1511 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1512 goto out_free;
1513
1514 r = n;
1515
1516out_free:
1517 vfree(entries);
1518out:
1519 return r;
1520}
1521
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001522int kvm_dev_ioctl_check_extension(long ext)
1523{
1524 int r;
1525
1526 switch (ext) {
1527 case KVM_CAP_IRQCHIP:
1528 case KVM_CAP_HLT:
1529 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001530 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001531 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001532 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001533 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001534 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001535 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001536 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001537 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001538 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001539 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001540 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001541 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001542 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001543 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001544 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001545 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001546 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001547 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001548 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001549 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001550 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001551 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01001552 case KVM_CAP_DEBUGREGS:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001553 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001554 r = 1;
1555 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001556 case KVM_CAP_COALESCED_MMIO:
1557 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1558 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001559 case KVM_CAP_VAPIC:
1560 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1561 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001562 case KVM_CAP_NR_VCPUS:
1563 r = KVM_MAX_VCPUS;
1564 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001565 case KVM_CAP_NR_MEMSLOTS:
1566 r = KVM_MEMORY_SLOTS;
1567 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001568 case KVM_CAP_PV_MMU: /* obsolete */
1569 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001570 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001571 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001572 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001573 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001574 case KVM_CAP_MCE:
1575 r = KVM_MAX_MCE_BANKS;
1576 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001577 default:
1578 r = 0;
1579 break;
1580 }
1581 return r;
1582
1583}
1584
Carsten Otte043405e2007-10-10 17:16:19 +02001585long kvm_arch_dev_ioctl(struct file *filp,
1586 unsigned int ioctl, unsigned long arg)
1587{
1588 void __user *argp = (void __user *)arg;
1589 long r;
1590
1591 switch (ioctl) {
1592 case KVM_GET_MSR_INDEX_LIST: {
1593 struct kvm_msr_list __user *user_msr_list = argp;
1594 struct kvm_msr_list msr_list;
1595 unsigned n;
1596
1597 r = -EFAULT;
1598 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1599 goto out;
1600 n = msr_list.nmsrs;
1601 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1602 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1603 goto out;
1604 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001605 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001606 goto out;
1607 r = -EFAULT;
1608 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1609 num_msrs_to_save * sizeof(u32)))
1610 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001611 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001612 &emulated_msrs,
1613 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1614 goto out;
1615 r = 0;
1616 break;
1617 }
Avi Kivity674eea02008-02-11 18:37:23 +02001618 case KVM_GET_SUPPORTED_CPUID: {
1619 struct kvm_cpuid2 __user *cpuid_arg = argp;
1620 struct kvm_cpuid2 cpuid;
1621
1622 r = -EFAULT;
1623 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1624 goto out;
1625 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001626 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001627 if (r)
1628 goto out;
1629
1630 r = -EFAULT;
1631 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1632 goto out;
1633 r = 0;
1634 break;
1635 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001636 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1637 u64 mce_cap;
1638
1639 mce_cap = KVM_MCE_CAP_SUPPORTED;
1640 r = -EFAULT;
1641 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1642 goto out;
1643 r = 0;
1644 break;
1645 }
Carsten Otte043405e2007-10-10 17:16:19 +02001646 default:
1647 r = -EINVAL;
1648 }
1649out:
1650 return r;
1651}
1652
Carsten Otte313a3dc2007-10-11 19:16:52 +02001653void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1654{
1655 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001656 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1657 unsigned long khz = cpufreq_quick_get(cpu);
1658 if (!khz)
1659 khz = tsc_khz;
1660 per_cpu(cpu_tsc_khz, cpu) = khz;
1661 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001662 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001663}
1664
1665void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1666{
Amit Shah9327fd12007-11-15 18:38:46 +02001667 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001668 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001669}
1670
Dan Kenigsberg07716712007-11-21 17:10:04 +02001671static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001672{
Avi Kivitye286e862009-05-03 18:50:55 +03001673 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001674
Avi Kivitye286e862009-05-03 18:50:55 +03001675 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001676 return efer & EFER_NX;
1677}
1678
1679static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1680{
1681 int i;
1682 struct kvm_cpuid_entry2 *e, *entry;
1683
Carsten Otte313a3dc2007-10-11 19:16:52 +02001684 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001685 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1686 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001687 if (e->function == 0x80000001) {
1688 entry = e;
1689 break;
1690 }
1691 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001692 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001693 entry->edx &= ~(1 << 20);
1694 printk(KERN_INFO "kvm: guest NX capability removed\n");
1695 }
1696}
1697
Dan Kenigsberg07716712007-11-21 17:10:04 +02001698/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001699static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1700 struct kvm_cpuid *cpuid,
1701 struct kvm_cpuid_entry __user *entries)
1702{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001703 int r, i;
1704 struct kvm_cpuid_entry *cpuid_entries;
1705
1706 r = -E2BIG;
1707 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1708 goto out;
1709 r = -ENOMEM;
1710 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1711 if (!cpuid_entries)
1712 goto out;
1713 r = -EFAULT;
1714 if (copy_from_user(cpuid_entries, entries,
1715 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1716 goto out_free;
1717 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001718 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1719 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1720 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1721 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1722 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1723 vcpu->arch.cpuid_entries[i].index = 0;
1724 vcpu->arch.cpuid_entries[i].flags = 0;
1725 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1726 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1727 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001728 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001729 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001730 cpuid_fix_nx_cap(vcpu);
1731 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001732 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001733 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001734
1735out_free:
1736 vfree(cpuid_entries);
1737out:
1738 return r;
1739}
1740
1741static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001742 struct kvm_cpuid2 *cpuid,
1743 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001744{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001745 int r;
1746
1747 r = -E2BIG;
1748 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1749 goto out;
1750 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001751 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001752 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001753 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001754 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001755 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001756 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001757 return 0;
1758
1759out:
1760 return r;
1761}
1762
Dan Kenigsberg07716712007-11-21 17:10:04 +02001763static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001764 struct kvm_cpuid2 *cpuid,
1765 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001766{
1767 int r;
1768
1769 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001771 goto out;
1772 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001773 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001774 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001775 goto out;
1776 return 0;
1777
1778out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001779 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001780 return r;
1781}
1782
Dan Kenigsberg07716712007-11-21 17:10:04 +02001783static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001784 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001785{
1786 entry->function = function;
1787 entry->index = index;
1788 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001789 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001790 entry->flags = 0;
1791}
1792
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001793#define F(x) bit(X86_FEATURE_##x)
1794
Dan Kenigsberg07716712007-11-21 17:10:04 +02001795static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1796 u32 index, int *nent, int maxnent)
1797{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001798 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001799#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001800 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1801 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001802 unsigned f_lm = F(LM);
1803#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001804 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001805 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001806#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001807 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001808
1809 /* cpuid 1.edx */
1810 const u32 kvm_supported_word0_x86_features =
1811 F(FPU) | F(VME) | F(DE) | F(PSE) |
1812 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1813 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1814 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1815 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1816 0 /* Reserved, DS, ACPI */ | F(MMX) |
1817 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1818 0 /* HTT, TM, Reserved, PBE */;
1819 /* cpuid 0x80000001.edx */
1820 const u32 kvm_supported_word1_x86_features =
1821 F(FPU) | F(VME) | F(DE) | F(PSE) |
1822 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1823 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1824 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1825 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1826 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001827 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001828 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1829 /* cpuid 1.ecx */
1830 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001831 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1832 0 /* DS-CPL, VMX, SMX, EST */ |
1833 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1834 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1835 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001836 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001837 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001838 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001839 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001840 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1841 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1842 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1843 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001844
Amit Shah19355472009-01-14 16:56:00 +00001845 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001846 get_cpu();
1847 do_cpuid_1_ent(entry, function, index);
1848 ++*nent;
1849
1850 switch (function) {
1851 case 0:
1852 entry->eax = min(entry->eax, (u32)0xb);
1853 break;
1854 case 1:
1855 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001856 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001857 /* we support x2apic emulation even if host does not support
1858 * it since we emulate x2apic in software */
1859 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001860 break;
1861 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1862 * may return different values. This forces us to get_cpu() before
1863 * issuing the first command, and also to emulate this annoying behavior
1864 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1865 case 2: {
1866 int t, times = entry->eax & 0xff;
1867
1868 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001869 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001870 for (t = 1; t < times && *nent < maxnent; ++t) {
1871 do_cpuid_1_ent(&entry[t], function, 0);
1872 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1873 ++*nent;
1874 }
1875 break;
1876 }
1877 /* function 4 and 0xb have additional index. */
1878 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001879 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001880
1881 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1882 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001883 for (i = 1; *nent < maxnent; ++i) {
1884 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001885 if (!cache_type)
1886 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001887 do_cpuid_1_ent(&entry[i], function, i);
1888 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001889 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1890 ++*nent;
1891 }
1892 break;
1893 }
1894 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001895 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001896
1897 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1898 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001899 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001900 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001901 if (!level_type)
1902 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001903 do_cpuid_1_ent(&entry[i], function, i);
1904 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001905 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1906 ++*nent;
1907 }
1908 break;
1909 }
1910 case 0x80000000:
1911 entry->eax = min(entry->eax, 0x8000001a);
1912 break;
1913 case 0x80000001:
1914 entry->edx &= kvm_supported_word1_x86_features;
1915 entry->ecx &= kvm_supported_word6_x86_features;
1916 break;
1917 }
1918 put_cpu();
1919}
1920
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001921#undef F
1922
Avi Kivity674eea02008-02-11 18:37:23 +02001923static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001924 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001925{
1926 struct kvm_cpuid_entry2 *cpuid_entries;
1927 int limit, nent = 0, r = -E2BIG;
1928 u32 func;
1929
1930 if (cpuid->nent < 1)
1931 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001932 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1933 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001934 r = -ENOMEM;
1935 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1936 if (!cpuid_entries)
1937 goto out;
1938
1939 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1940 limit = cpuid_entries[0].eax;
1941 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1942 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001943 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001944 r = -E2BIG;
1945 if (nent >= cpuid->nent)
1946 goto out_free;
1947
1948 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1949 limit = cpuid_entries[nent - 1].eax;
1950 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1951 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001952 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001953 r = -E2BIG;
1954 if (nent >= cpuid->nent)
1955 goto out_free;
1956
Dan Kenigsberg07716712007-11-21 17:10:04 +02001957 r = -EFAULT;
1958 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001959 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001960 goto out_free;
1961 cpuid->nent = nent;
1962 r = 0;
1963
1964out_free:
1965 vfree(cpuid_entries);
1966out:
1967 return r;
1968}
1969
Carsten Otte313a3dc2007-10-11 19:16:52 +02001970static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1971 struct kvm_lapic_state *s)
1972{
1973 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001974 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001975 vcpu_put(vcpu);
1976
1977 return 0;
1978}
1979
1980static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1981 struct kvm_lapic_state *s)
1982{
1983 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001984 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001985 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03001986 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001987 vcpu_put(vcpu);
1988
1989 return 0;
1990}
1991
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001992static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1993 struct kvm_interrupt *irq)
1994{
1995 if (irq->irq < 0 || irq->irq >= 256)
1996 return -EINVAL;
1997 if (irqchip_in_kernel(vcpu->kvm))
1998 return -ENXIO;
1999 vcpu_load(vcpu);
2000
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002001 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002002
2003 vcpu_put(vcpu);
2004
2005 return 0;
2006}
2007
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002008static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2009{
2010 vcpu_load(vcpu);
2011 kvm_inject_nmi(vcpu);
2012 vcpu_put(vcpu);
2013
2014 return 0;
2015}
2016
Avi Kivityb209749f2007-10-22 16:50:39 +02002017static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2018 struct kvm_tpr_access_ctl *tac)
2019{
2020 if (tac->flags)
2021 return -EINVAL;
2022 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2023 return 0;
2024}
2025
Huang Ying890ca9a2009-05-11 16:48:15 +08002026static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2027 u64 mcg_cap)
2028{
2029 int r;
2030 unsigned bank_num = mcg_cap & 0xff, bank;
2031
2032 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002033 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002034 goto out;
2035 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2036 goto out;
2037 r = 0;
2038 vcpu->arch.mcg_cap = mcg_cap;
2039 /* Init IA32_MCG_CTL to all 1s */
2040 if (mcg_cap & MCG_CTL_P)
2041 vcpu->arch.mcg_ctl = ~(u64)0;
2042 /* Init IA32_MCi_CTL to all 1s */
2043 for (bank = 0; bank < bank_num; bank++)
2044 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2045out:
2046 return r;
2047}
2048
2049static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2050 struct kvm_x86_mce *mce)
2051{
2052 u64 mcg_cap = vcpu->arch.mcg_cap;
2053 unsigned bank_num = mcg_cap & 0xff;
2054 u64 *banks = vcpu->arch.mce_banks;
2055
2056 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2057 return -EINVAL;
2058 /*
2059 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2060 * reporting is disabled
2061 */
2062 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2063 vcpu->arch.mcg_ctl != ~(u64)0)
2064 return 0;
2065 banks += 4 * mce->bank;
2066 /*
2067 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2068 * reporting is disabled for the bank
2069 */
2070 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2071 return 0;
2072 if (mce->status & MCI_STATUS_UC) {
2073 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002074 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002075 printk(KERN_DEBUG "kvm: set_mce: "
2076 "injects mce exception while "
2077 "previous one is in progress!\n");
2078 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2079 return 0;
2080 }
2081 if (banks[1] & MCI_STATUS_VAL)
2082 mce->status |= MCI_STATUS_OVER;
2083 banks[2] = mce->addr;
2084 banks[3] = mce->misc;
2085 vcpu->arch.mcg_status = mce->mcg_status;
2086 banks[1] = mce->status;
2087 kvm_queue_exception(vcpu, MC_VECTOR);
2088 } else if (!(banks[1] & MCI_STATUS_VAL)
2089 || !(banks[1] & MCI_STATUS_UC)) {
2090 if (banks[1] & MCI_STATUS_VAL)
2091 mce->status |= MCI_STATUS_OVER;
2092 banks[2] = mce->addr;
2093 banks[3] = mce->misc;
2094 banks[1] = mce->status;
2095 } else
2096 banks[1] |= MCI_STATUS_OVER;
2097 return 0;
2098}
2099
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002100static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2101 struct kvm_vcpu_events *events)
2102{
2103 vcpu_load(vcpu);
2104
Jan Kiszka03b82a32010-02-15 10:45:41 +01002105 events->exception.injected =
2106 vcpu->arch.exception.pending &&
2107 !kvm_exception_is_soft(vcpu->arch.exception.nr);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002108 events->exception.nr = vcpu->arch.exception.nr;
2109 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2110 events->exception.error_code = vcpu->arch.exception.error_code;
2111
Jan Kiszka03b82a32010-02-15 10:45:41 +01002112 events->interrupt.injected =
2113 vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002114 events->interrupt.nr = vcpu->arch.interrupt.nr;
Jan Kiszka03b82a32010-02-15 10:45:41 +01002115 events->interrupt.soft = 0;
Jan Kiszka48005f62010-02-19 19:38:07 +01002116 events->interrupt.shadow =
2117 kvm_x86_ops->get_interrupt_shadow(vcpu,
2118 KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002119
2120 events->nmi.injected = vcpu->arch.nmi_injected;
2121 events->nmi.pending = vcpu->arch.nmi_pending;
2122 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2123
2124 events->sipi_vector = vcpu->arch.sipi_vector;
2125
Jan Kiszkadab4b912009-12-06 18:24:15 +01002126 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002127 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2128 | KVM_VCPUEVENT_VALID_SHADOW);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002129
2130 vcpu_put(vcpu);
2131}
2132
2133static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2134 struct kvm_vcpu_events *events)
2135{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002136 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
Jan Kiszka48005f62010-02-19 19:38:07 +01002137 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
2138 | KVM_VCPUEVENT_VALID_SHADOW))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002139 return -EINVAL;
2140
2141 vcpu_load(vcpu);
2142
2143 vcpu->arch.exception.pending = events->exception.injected;
2144 vcpu->arch.exception.nr = events->exception.nr;
2145 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2146 vcpu->arch.exception.error_code = events->exception.error_code;
2147
2148 vcpu->arch.interrupt.pending = events->interrupt.injected;
2149 vcpu->arch.interrupt.nr = events->interrupt.nr;
2150 vcpu->arch.interrupt.soft = events->interrupt.soft;
2151 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2152 kvm_pic_clear_isr_ack(vcpu->kvm);
Jan Kiszka48005f62010-02-19 19:38:07 +01002153 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
2154 kvm_x86_ops->set_interrupt_shadow(vcpu,
2155 events->interrupt.shadow);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002156
2157 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002158 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2159 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002160 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2161
Jan Kiszkadab4b912009-12-06 18:24:15 +01002162 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2163 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002164
2165 vcpu_put(vcpu);
2166
2167 return 0;
2168}
2169
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002170static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
2171 struct kvm_debugregs *dbgregs)
2172{
2173 vcpu_load(vcpu);
2174
2175 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
2176 dbgregs->dr6 = vcpu->arch.dr6;
2177 dbgregs->dr7 = vcpu->arch.dr7;
2178 dbgregs->flags = 0;
2179
2180 vcpu_put(vcpu);
2181}
2182
2183static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
2184 struct kvm_debugregs *dbgregs)
2185{
2186 if (dbgregs->flags)
2187 return -EINVAL;
2188
2189 vcpu_load(vcpu);
2190
2191 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
2192 vcpu->arch.dr6 = dbgregs->dr6;
2193 vcpu->arch.dr7 = dbgregs->dr7;
2194
2195 vcpu_put(vcpu);
2196
2197 return 0;
2198}
2199
Carsten Otte313a3dc2007-10-11 19:16:52 +02002200long kvm_arch_vcpu_ioctl(struct file *filp,
2201 unsigned int ioctl, unsigned long arg)
2202{
2203 struct kvm_vcpu *vcpu = filp->private_data;
2204 void __user *argp = (void __user *)arg;
2205 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002206 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002207
2208 switch (ioctl) {
2209 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002210 r = -EINVAL;
2211 if (!vcpu->arch.apic)
2212 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002213 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002214
Dave Hansenb772ff32008-08-11 10:01:47 -07002215 r = -ENOMEM;
2216 if (!lapic)
2217 goto out;
2218 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002219 if (r)
2220 goto out;
2221 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002222 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002223 goto out;
2224 r = 0;
2225 break;
2226 }
2227 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002228 r = -EINVAL;
2229 if (!vcpu->arch.apic)
2230 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002231 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2232 r = -ENOMEM;
2233 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002234 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002235 r = -EFAULT;
2236 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2237 goto out;
2238 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002239 if (r)
2240 goto out;
2241 r = 0;
2242 break;
2243 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002244 case KVM_INTERRUPT: {
2245 struct kvm_interrupt irq;
2246
2247 r = -EFAULT;
2248 if (copy_from_user(&irq, argp, sizeof irq))
2249 goto out;
2250 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2251 if (r)
2252 goto out;
2253 r = 0;
2254 break;
2255 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002256 case KVM_NMI: {
2257 r = kvm_vcpu_ioctl_nmi(vcpu);
2258 if (r)
2259 goto out;
2260 r = 0;
2261 break;
2262 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002263 case KVM_SET_CPUID: {
2264 struct kvm_cpuid __user *cpuid_arg = argp;
2265 struct kvm_cpuid cpuid;
2266
2267 r = -EFAULT;
2268 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2269 goto out;
2270 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2271 if (r)
2272 goto out;
2273 break;
2274 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002275 case KVM_SET_CPUID2: {
2276 struct kvm_cpuid2 __user *cpuid_arg = argp;
2277 struct kvm_cpuid2 cpuid;
2278
2279 r = -EFAULT;
2280 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2281 goto out;
2282 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002283 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002284 if (r)
2285 goto out;
2286 break;
2287 }
2288 case KVM_GET_CPUID2: {
2289 struct kvm_cpuid2 __user *cpuid_arg = argp;
2290 struct kvm_cpuid2 cpuid;
2291
2292 r = -EFAULT;
2293 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2294 goto out;
2295 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002296 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002297 if (r)
2298 goto out;
2299 r = -EFAULT;
2300 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2301 goto out;
2302 r = 0;
2303 break;
2304 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002305 case KVM_GET_MSRS:
2306 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2307 break;
2308 case KVM_SET_MSRS:
2309 r = msr_io(vcpu, argp, do_set_msr, 0);
2310 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002311 case KVM_TPR_ACCESS_REPORTING: {
2312 struct kvm_tpr_access_ctl tac;
2313
2314 r = -EFAULT;
2315 if (copy_from_user(&tac, argp, sizeof tac))
2316 goto out;
2317 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2318 if (r)
2319 goto out;
2320 r = -EFAULT;
2321 if (copy_to_user(argp, &tac, sizeof tac))
2322 goto out;
2323 r = 0;
2324 break;
2325 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002326 case KVM_SET_VAPIC_ADDR: {
2327 struct kvm_vapic_addr va;
2328
2329 r = -EINVAL;
2330 if (!irqchip_in_kernel(vcpu->kvm))
2331 goto out;
2332 r = -EFAULT;
2333 if (copy_from_user(&va, argp, sizeof va))
2334 goto out;
2335 r = 0;
2336 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2337 break;
2338 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002339 case KVM_X86_SETUP_MCE: {
2340 u64 mcg_cap;
2341
2342 r = -EFAULT;
2343 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2344 goto out;
2345 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2346 break;
2347 }
2348 case KVM_X86_SET_MCE: {
2349 struct kvm_x86_mce mce;
2350
2351 r = -EFAULT;
2352 if (copy_from_user(&mce, argp, sizeof mce))
2353 goto out;
2354 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2355 break;
2356 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002357 case KVM_GET_VCPU_EVENTS: {
2358 struct kvm_vcpu_events events;
2359
2360 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2361
2362 r = -EFAULT;
2363 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2364 break;
2365 r = 0;
2366 break;
2367 }
2368 case KVM_SET_VCPU_EVENTS: {
2369 struct kvm_vcpu_events events;
2370
2371 r = -EFAULT;
2372 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2373 break;
2374
2375 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2376 break;
2377 }
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01002378 case KVM_GET_DEBUGREGS: {
2379 struct kvm_debugregs dbgregs;
2380
2381 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2382
2383 r = -EFAULT;
2384 if (copy_to_user(argp, &dbgregs,
2385 sizeof(struct kvm_debugregs)))
2386 break;
2387 r = 0;
2388 break;
2389 }
2390 case KVM_SET_DEBUGREGS: {
2391 struct kvm_debugregs dbgregs;
2392
2393 r = -EFAULT;
2394 if (copy_from_user(&dbgregs, argp,
2395 sizeof(struct kvm_debugregs)))
2396 break;
2397
2398 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2399 break;
2400 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002401 default:
2402 r = -EINVAL;
2403 }
2404out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002405 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002406 return r;
2407}
2408
Carsten Otte1fe779f2007-10-29 16:08:35 +01002409static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2410{
2411 int ret;
2412
2413 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2414 return -1;
2415 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2416 return ret;
2417}
2418
Sheng Yangb927a3c2009-07-21 10:42:48 +08002419static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2420 u64 ident_addr)
2421{
2422 kvm->arch.ept_identity_map_addr = ident_addr;
2423 return 0;
2424}
2425
Carsten Otte1fe779f2007-10-29 16:08:35 +01002426static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2427 u32 kvm_nr_mmu_pages)
2428{
2429 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2430 return -EINVAL;
2431
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002432 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002433 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002434
2435 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002436 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002437
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002438 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002439 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002440 return 0;
2441}
2442
2443static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2444{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002445 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002446}
2447
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002448gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2449{
2450 int i;
2451 struct kvm_mem_alias *alias;
2452 struct kvm_mem_aliases *aliases;
2453
2454 aliases = rcu_dereference(kvm->arch.aliases);
2455
2456 for (i = 0; i < aliases->naliases; ++i) {
2457 alias = &aliases->aliases[i];
2458 if (alias->flags & KVM_ALIAS_INVALID)
2459 continue;
2460 if (gfn >= alias->base_gfn
2461 && gfn < alias->base_gfn + alias->npages)
2462 return alias->target_gfn + gfn - alias->base_gfn;
2463 }
2464 return gfn;
2465}
2466
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002467gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2468{
2469 int i;
2470 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002471 struct kvm_mem_aliases *aliases;
2472
2473 aliases = rcu_dereference(kvm->arch.aliases);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002474
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002475 for (i = 0; i < aliases->naliases; ++i) {
2476 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002477 if (gfn >= alias->base_gfn
2478 && gfn < alias->base_gfn + alias->npages)
2479 return alias->target_gfn + gfn - alias->base_gfn;
2480 }
2481 return gfn;
2482}
2483
Carsten Otte1fe779f2007-10-29 16:08:35 +01002484/*
2485 * Set a new alias region. Aliases map a portion of physical memory into
2486 * another portion. This is useful for memory windows, for example the PC
2487 * VGA region.
2488 */
2489static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2490 struct kvm_memory_alias *alias)
2491{
2492 int r, n;
2493 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002494 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002495
2496 r = -EINVAL;
2497 /* General sanity checks */
2498 if (alias->memory_size & (PAGE_SIZE - 1))
2499 goto out;
2500 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2501 goto out;
2502 if (alias->slot >= KVM_ALIAS_SLOTS)
2503 goto out;
2504 if (alias->guest_phys_addr + alias->memory_size
2505 < alias->guest_phys_addr)
2506 goto out;
2507 if (alias->target_phys_addr + alias->memory_size
2508 < alias->target_phys_addr)
2509 goto out;
2510
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002511 r = -ENOMEM;
2512 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2513 if (!aliases)
2514 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002515
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002516 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002517
2518 /* invalidate any gfn reference in case of deletion/shrinking */
2519 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2520 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2521 old_aliases = kvm->arch.aliases;
2522 rcu_assign_pointer(kvm->arch.aliases, aliases);
2523 synchronize_srcu_expedited(&kvm->srcu);
2524 kvm_mmu_zap_all(kvm);
2525 kfree(old_aliases);
2526
2527 r = -ENOMEM;
2528 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2529 if (!aliases)
2530 goto out_unlock;
2531
2532 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002533
2534 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002535 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2536 p->npages = alias->memory_size >> PAGE_SHIFT;
2537 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002538 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002539
2540 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002541 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002542 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002543 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002544
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002545 old_aliases = kvm->arch.aliases;
2546 rcu_assign_pointer(kvm->arch.aliases, aliases);
2547 synchronize_srcu_expedited(&kvm->srcu);
2548 kfree(old_aliases);
2549 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002550
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002551out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002552 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002553out:
2554 return r;
2555}
2556
2557static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2558{
2559 int r;
2560
2561 r = 0;
2562 switch (chip->chip_id) {
2563 case KVM_IRQCHIP_PIC_MASTER:
2564 memcpy(&chip->chip.pic,
2565 &pic_irqchip(kvm)->pics[0],
2566 sizeof(struct kvm_pic_state));
2567 break;
2568 case KVM_IRQCHIP_PIC_SLAVE:
2569 memcpy(&chip->chip.pic,
2570 &pic_irqchip(kvm)->pics[1],
2571 sizeof(struct kvm_pic_state));
2572 break;
2573 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002574 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002575 break;
2576 default:
2577 r = -EINVAL;
2578 break;
2579 }
2580 return r;
2581}
2582
2583static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2584{
2585 int r;
2586
2587 r = 0;
2588 switch (chip->chip_id) {
2589 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002590 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002591 memcpy(&pic_irqchip(kvm)->pics[0],
2592 &chip->chip.pic,
2593 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002594 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002595 break;
2596 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002597 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002598 memcpy(&pic_irqchip(kvm)->pics[1],
2599 &chip->chip.pic,
2600 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002601 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002602 break;
2603 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002604 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002605 break;
2606 default:
2607 r = -EINVAL;
2608 break;
2609 }
2610 kvm_pic_update_irq(pic_irqchip(kvm));
2611 return r;
2612}
2613
Sheng Yange0f63cb2008-03-04 00:50:59 +08002614static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2615{
2616 int r = 0;
2617
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002618 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002619 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002620 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002621 return r;
2622}
2623
2624static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2625{
2626 int r = 0;
2627
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002628 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002629 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002630 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2631 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2632 return r;
2633}
2634
2635static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2636{
2637 int r = 0;
2638
2639 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2640 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2641 sizeof(ps->channels));
2642 ps->flags = kvm->arch.vpit->pit_state.flags;
2643 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2644 return r;
2645}
2646
2647static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2648{
2649 int r = 0, start = 0;
2650 u32 prev_legacy, cur_legacy;
2651 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2652 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2653 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2654 if (!prev_legacy && cur_legacy)
2655 start = 1;
2656 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2657 sizeof(kvm->arch.vpit->pit_state.channels));
2658 kvm->arch.vpit->pit_state.flags = ps->flags;
2659 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002660 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002661 return r;
2662}
2663
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002664static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2665 struct kvm_reinject_control *control)
2666{
2667 if (!kvm->arch.vpit)
2668 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002669 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002670 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002671 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002672 return 0;
2673}
2674
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002675/*
2676 * Get (and clear) the dirty memory log for a memory slot.
2677 */
2678int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2679 struct kvm_dirty_log *log)
2680{
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002681 int r, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002682 struct kvm_memory_slot *memslot;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002683 unsigned long n;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002684 unsigned long is_dirty = 0;
2685 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002686
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002687 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002688
Marcelo Tosattib050b012009-12-23 14:35:22 -02002689 r = -EINVAL;
2690 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002691 goto out;
2692
Marcelo Tosattib050b012009-12-23 14:35:22 -02002693 memslot = &kvm->memslots->memslots[log->slot];
2694 r = -ENOENT;
2695 if (!memslot->dirty_bitmap)
2696 goto out;
2697
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09002698 n = kvm_dirty_bitmap_bytes(memslot);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002699
2700 r = -ENOMEM;
2701 dirty_bitmap = vmalloc(n);
2702 if (!dirty_bitmap)
2703 goto out;
2704 memset(dirty_bitmap, 0, n);
2705
2706 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2707 is_dirty = memslot->dirty_bitmap[i];
2708
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002709 /* If nothing is dirty, don't bother messing with page tables. */
2710 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002711 struct kvm_memslots *slots, *old_slots;
2712
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002713 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002714 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002715 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002716
2717 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2718 if (!slots)
2719 goto out_free;
2720
2721 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2722 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2723
2724 old_slots = kvm->memslots;
2725 rcu_assign_pointer(kvm->memslots, slots);
2726 synchronize_srcu_expedited(&kvm->srcu);
2727 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2728 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002729 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002730
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002731 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002732 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2733 r = -EFAULT;
2734out_free:
2735 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002736out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002737 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002738 return r;
2739}
2740
Carsten Otte1fe779f2007-10-29 16:08:35 +01002741long kvm_arch_vm_ioctl(struct file *filp,
2742 unsigned int ioctl, unsigned long arg)
2743{
2744 struct kvm *kvm = filp->private_data;
2745 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002746 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002747 /*
2748 * This union makes it completely explicit to gcc-3.x
2749 * that these two variables' stack usage should be
2750 * combined, not added together.
2751 */
2752 union {
2753 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002754 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002755 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002756 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002757 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002758
2759 switch (ioctl) {
2760 case KVM_SET_TSS_ADDR:
2761 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2762 if (r < 0)
2763 goto out;
2764 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002765 case KVM_SET_IDENTITY_MAP_ADDR: {
2766 u64 ident_addr;
2767
2768 r = -EFAULT;
2769 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2770 goto out;
2771 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2772 if (r < 0)
2773 goto out;
2774 break;
2775 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002776 case KVM_SET_MEMORY_REGION: {
2777 struct kvm_memory_region kvm_mem;
2778 struct kvm_userspace_memory_region kvm_userspace_mem;
2779
2780 r = -EFAULT;
2781 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2782 goto out;
2783 kvm_userspace_mem.slot = kvm_mem.slot;
2784 kvm_userspace_mem.flags = kvm_mem.flags;
2785 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2786 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2787 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2788 if (r)
2789 goto out;
2790 break;
2791 }
2792 case KVM_SET_NR_MMU_PAGES:
2793 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2794 if (r)
2795 goto out;
2796 break;
2797 case KVM_GET_NR_MMU_PAGES:
2798 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2799 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002800 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002801 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002802 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002803 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002804 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002805 if (r)
2806 goto out;
2807 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002808 case KVM_CREATE_IRQCHIP: {
2809 struct kvm_pic *vpic;
2810
2811 mutex_lock(&kvm->lock);
2812 r = -EEXIST;
2813 if (kvm->arch.vpic)
2814 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002815 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002816 vpic = kvm_create_pic(kvm);
2817 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002818 r = kvm_ioapic_init(kvm);
2819 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002820 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2821 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002822 kfree(vpic);
2823 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002824 }
2825 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002826 goto create_irqchip_unlock;
2827 smp_wmb();
2828 kvm->arch.vpic = vpic;
2829 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002830 r = kvm_setup_default_irq_routing(kvm);
2831 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002832 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002833 kvm_ioapic_destroy(kvm);
2834 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002835 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002836 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002837 create_irqchip_unlock:
2838 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002839 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002840 }
Sheng Yang78376992008-01-28 05:10:22 +08002841 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002842 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2843 goto create_pit;
2844 case KVM_CREATE_PIT2:
2845 r = -EFAULT;
2846 if (copy_from_user(&u.pit_config, argp,
2847 sizeof(struct kvm_pit_config)))
2848 goto out;
2849 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002850 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002851 r = -EEXIST;
2852 if (kvm->arch.vpit)
2853 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002854 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002855 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002856 if (kvm->arch.vpit)
2857 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002858 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002859 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002860 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002861 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002862 case KVM_IRQ_LINE: {
2863 struct kvm_irq_level irq_event;
2864
2865 r = -EFAULT;
2866 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2867 goto out;
2868 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002869 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002870 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2871 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002872 if (ioctl == KVM_IRQ_LINE_STATUS) {
2873 irq_event.status = status;
2874 if (copy_to_user(argp, &irq_event,
2875 sizeof irq_event))
2876 goto out;
2877 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002878 r = 0;
2879 }
2880 break;
2881 }
2882 case KVM_GET_IRQCHIP: {
2883 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002884 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002885
Dave Hansenf0d66272008-08-11 10:01:45 -07002886 r = -ENOMEM;
2887 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002888 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002889 r = -EFAULT;
2890 if (copy_from_user(chip, argp, sizeof *chip))
2891 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002892 r = -ENXIO;
2893 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002894 goto get_irqchip_out;
2895 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2896 if (r)
2897 goto get_irqchip_out;
2898 r = -EFAULT;
2899 if (copy_to_user(argp, chip, sizeof *chip))
2900 goto get_irqchip_out;
2901 r = 0;
2902 get_irqchip_out:
2903 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002904 if (r)
2905 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002906 break;
2907 }
2908 case KVM_SET_IRQCHIP: {
2909 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002910 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002911
Dave Hansenf0d66272008-08-11 10:01:45 -07002912 r = -ENOMEM;
2913 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002914 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002915 r = -EFAULT;
2916 if (copy_from_user(chip, argp, sizeof *chip))
2917 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002918 r = -ENXIO;
2919 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002920 goto set_irqchip_out;
2921 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2922 if (r)
2923 goto set_irqchip_out;
2924 r = 0;
2925 set_irqchip_out:
2926 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002927 if (r)
2928 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002929 break;
2930 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002931 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002932 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002933 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002934 goto out;
2935 r = -ENXIO;
2936 if (!kvm->arch.vpit)
2937 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002938 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002939 if (r)
2940 goto out;
2941 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002942 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002943 goto out;
2944 r = 0;
2945 break;
2946 }
2947 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002948 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002949 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002950 goto out;
2951 r = -ENXIO;
2952 if (!kvm->arch.vpit)
2953 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002954 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002955 if (r)
2956 goto out;
2957 r = 0;
2958 break;
2959 }
Beth Kone9f42752009-07-07 11:50:38 -04002960 case KVM_GET_PIT2: {
2961 r = -ENXIO;
2962 if (!kvm->arch.vpit)
2963 goto out;
2964 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2965 if (r)
2966 goto out;
2967 r = -EFAULT;
2968 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2969 goto out;
2970 r = 0;
2971 break;
2972 }
2973 case KVM_SET_PIT2: {
2974 r = -EFAULT;
2975 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2976 goto out;
2977 r = -ENXIO;
2978 if (!kvm->arch.vpit)
2979 goto out;
2980 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2981 if (r)
2982 goto out;
2983 r = 0;
2984 break;
2985 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002986 case KVM_REINJECT_CONTROL: {
2987 struct kvm_reinject_control control;
2988 r = -EFAULT;
2989 if (copy_from_user(&control, argp, sizeof(control)))
2990 goto out;
2991 r = kvm_vm_ioctl_reinject(kvm, &control);
2992 if (r)
2993 goto out;
2994 r = 0;
2995 break;
2996 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002997 case KVM_XEN_HVM_CONFIG: {
2998 r = -EFAULT;
2999 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
3000 sizeof(struct kvm_xen_hvm_config)))
3001 goto out;
3002 r = -EINVAL;
3003 if (kvm->arch.xen_hvm_config.flags)
3004 goto out;
3005 r = 0;
3006 break;
3007 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04003008 case KVM_SET_CLOCK: {
3009 struct timespec now;
3010 struct kvm_clock_data user_ns;
3011 u64 now_ns;
3012 s64 delta;
3013
3014 r = -EFAULT;
3015 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
3016 goto out;
3017
3018 r = -EINVAL;
3019 if (user_ns.flags)
3020 goto out;
3021
3022 r = 0;
3023 ktime_get_ts(&now);
3024 now_ns = timespec_to_ns(&now);
3025 delta = user_ns.clock - now_ns;
3026 kvm->arch.kvmclock_offset = delta;
3027 break;
3028 }
3029 case KVM_GET_CLOCK: {
3030 struct timespec now;
3031 struct kvm_clock_data user_ns;
3032 u64 now_ns;
3033
3034 ktime_get_ts(&now);
3035 now_ns = timespec_to_ns(&now);
3036 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
3037 user_ns.flags = 0;
3038
3039 r = -EFAULT;
3040 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
3041 goto out;
3042 r = 0;
3043 break;
3044 }
3045
Carsten Otte1fe779f2007-10-29 16:08:35 +01003046 default:
3047 ;
3048 }
3049out:
3050 return r;
3051}
3052
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003053static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003054{
3055 u32 dummy[2];
3056 unsigned i, j;
3057
Glauber Costae3267cb2009-10-06 13:24:50 -04003058 /* skip the first msrs in the list. KVM-specific */
3059 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003060 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3061 continue;
3062 if (j < i)
3063 msrs_to_save[j] = msrs_to_save[i];
3064 j++;
3065 }
3066 num_msrs_to_save = j;
3067}
3068
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003069static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3070 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003071{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003072 if (vcpu->arch.apic &&
3073 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3074 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003075
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003076 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003077}
3078
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003079static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003080{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003081 if (vcpu->arch.apic &&
3082 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3083 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003084
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003085 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003086}
3087
Gleb Natapov1871c602010-02-10 14:21:32 +02003088gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3089{
3090 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3091 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3092}
3093
3094 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3095{
3096 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3097 access |= PFERR_FETCH_MASK;
3098 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3099}
3100
3101gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3102{
3103 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3104 access |= PFERR_WRITE_MASK;
3105 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3106}
3107
3108/* uses this to access any guest's mapped memory without checking CPL */
3109gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3110{
3111 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3112}
3113
3114static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3115 struct kvm_vcpu *vcpu, u32 access,
3116 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003117{
3118 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003119 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003120
3121 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003122 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003123 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003124 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003125 int ret;
3126
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003127 if (gpa == UNMAPPED_GVA) {
3128 r = X86EMUL_PROPAGATE_FAULT;
3129 goto out;
3130 }
Izik Eidus77c20022008-12-29 01:42:19 +02003131 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003132 if (ret < 0) {
3133 r = X86EMUL_UNHANDLEABLE;
3134 goto out;
3135 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003136
Izik Eidus77c20022008-12-29 01:42:19 +02003137 bytes -= toread;
3138 data += toread;
3139 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003140 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003141out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003142 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003143}
Izik Eidus77c20022008-12-29 01:42:19 +02003144
Gleb Natapov1871c602010-02-10 14:21:32 +02003145/* used for instruction fetching */
3146static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3147 struct kvm_vcpu *vcpu, u32 *error)
3148{
3149 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3150 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3151 access | PFERR_FETCH_MASK, error);
3152}
3153
3154static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3155 struct kvm_vcpu *vcpu, u32 *error)
3156{
3157 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3158 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3159 error);
3160}
3161
3162static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3163 struct kvm_vcpu *vcpu, u32 *error)
3164{
3165 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3166}
3167
Hannes Edercded19f2009-02-21 02:19:13 +01003168static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +02003169 struct kvm_vcpu *vcpu, u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003170{
3171 void *data = val;
3172 int r = X86EMUL_CONTINUE;
3173
3174 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003175 gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003176 unsigned offset = addr & (PAGE_SIZE-1);
3177 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3178 int ret;
3179
3180 if (gpa == UNMAPPED_GVA) {
3181 r = X86EMUL_PROPAGATE_FAULT;
3182 goto out;
3183 }
3184 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3185 if (ret < 0) {
3186 r = X86EMUL_UNHANDLEABLE;
3187 goto out;
3188 }
3189
3190 bytes -= towrite;
3191 data += towrite;
3192 addr += towrite;
3193 }
3194out:
3195 return r;
3196}
3197
Carsten Ottebbd9b642007-10-30 18:44:21 +01003198
Carsten Ottebbd9b642007-10-30 18:44:21 +01003199static int emulator_read_emulated(unsigned long addr,
3200 void *val,
3201 unsigned int bytes,
3202 struct kvm_vcpu *vcpu)
3203{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003204 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003205 u32 error_code;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003206
3207 if (vcpu->mmio_read_completed) {
3208 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003209 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3210 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003211 vcpu->mmio_read_completed = 0;
3212 return X86EMUL_CONTINUE;
3213 }
3214
Gleb Natapov1871c602010-02-10 14:21:32 +02003215 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
3216
3217 if (gpa == UNMAPPED_GVA) {
3218 kvm_inject_page_fault(vcpu, addr, error_code);
3219 return X86EMUL_PROPAGATE_FAULT;
3220 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003221
3222 /* For APIC access vmexit */
3223 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3224 goto mmio;
3225
Gleb Natapov1871c602010-02-10 14:21:32 +02003226 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003227 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003228 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003229
3230mmio:
3231 /*
3232 * Is this MMIO handled locally?
3233 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003234 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3235 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003236 return X86EMUL_CONTINUE;
3237 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003238
3239 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003240
3241 vcpu->mmio_needed = 1;
3242 vcpu->mmio_phys_addr = gpa;
3243 vcpu->mmio_size = bytes;
3244 vcpu->mmio_is_write = 0;
3245
3246 return X86EMUL_UNHANDLEABLE;
3247}
3248
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003249int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003250 const void *val, int bytes)
3251{
3252 int ret;
3253
3254 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3255 if (ret < 0)
3256 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003257 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003258 return 1;
3259}
3260
Carsten Ottebbd9b642007-10-30 18:44:21 +01003261static int emulator_write_emulated_onepage(unsigned long addr,
3262 const void *val,
3263 unsigned int bytes,
3264 struct kvm_vcpu *vcpu)
3265{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003266 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003267 u32 error_code;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003268
Gleb Natapov1871c602010-02-10 14:21:32 +02003269 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003270
3271 if (gpa == UNMAPPED_GVA) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003272 kvm_inject_page_fault(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003273 return X86EMUL_PROPAGATE_FAULT;
3274 }
3275
3276 /* For APIC access vmexit */
3277 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3278 goto mmio;
3279
3280 if (emulator_write_phys(vcpu, gpa, val, bytes))
3281 return X86EMUL_CONTINUE;
3282
3283mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003284 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003285 /*
3286 * Is this MMIO handled locally?
3287 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003288 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003289 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003290
3291 vcpu->mmio_needed = 1;
3292 vcpu->mmio_phys_addr = gpa;
3293 vcpu->mmio_size = bytes;
3294 vcpu->mmio_is_write = 1;
3295 memcpy(vcpu->mmio_data, val, bytes);
3296
3297 return X86EMUL_CONTINUE;
3298}
3299
3300int emulator_write_emulated(unsigned long addr,
3301 const void *val,
3302 unsigned int bytes,
3303 struct kvm_vcpu *vcpu)
3304{
3305 /* Crossing a page boundary? */
3306 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3307 int rc, now;
3308
3309 now = -addr & ~PAGE_MASK;
3310 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
3311 if (rc != X86EMUL_CONTINUE)
3312 return rc;
3313 addr += now;
3314 val += now;
3315 bytes -= now;
3316 }
3317 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
3318}
3319EXPORT_SYMBOL_GPL(emulator_write_emulated);
3320
3321static int emulator_cmpxchg_emulated(unsigned long addr,
3322 const void *old,
3323 const void *new,
3324 unsigned int bytes,
3325 struct kvm_vcpu *vcpu)
3326{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02003327 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003328#ifndef CONFIG_X86_64
3329 /* guests cmpxchg8b have to be emulated atomically */
3330 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003331 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003332 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003333 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003334 u64 val;
3335
Gleb Natapov1871c602010-02-10 14:21:32 +02003336 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003337
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003338 if (gpa == UNMAPPED_GVA ||
3339 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3340 goto emul_write;
3341
3342 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3343 goto emul_write;
3344
3345 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003346
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003347 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003348
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003349 kaddr = kmap_atomic(page, KM_USER0);
3350 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
3351 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003352 kvm_release_page_dirty(page);
3353 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003354emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003355#endif
3356
Carsten Ottebbd9b642007-10-30 18:44:21 +01003357 return emulator_write_emulated(addr, new, bytes, vcpu);
3358}
3359
3360static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3361{
3362 return kvm_x86_ops->get_segment_base(vcpu, seg);
3363}
3364
3365int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3366{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003367 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003368 return X86EMUL_CONTINUE;
3369}
3370
3371int emulate_clts(struct kvm_vcpu *vcpu)
3372{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003373 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003374 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003375 return X86EMUL_CONTINUE;
3376}
3377
3378int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3379{
Jan Kiszkac76de352010-01-20 18:20:20 +01003380 return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003381}
3382
3383int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3384{
3385 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003386
Jan Kiszkac76de352010-01-20 18:20:20 +01003387 return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003388}
3389
3390void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3391{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003392 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003393 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003394 unsigned long rip_linear;
3395
Avi Kivityf76c7102008-06-13 22:45:42 +03003396 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003397 return;
3398
Glauber Costa25be4602008-06-10 10:46:53 -03003399 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3400
Gleb Natapov1871c602010-02-10 14:21:32 +02003401 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003402
3403 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3404 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003405}
3406EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3407
Harvey Harrison14af3f32008-02-19 10:25:50 -08003408static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003409 .read_std = kvm_read_guest_virt_system,
3410 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003411 .read_emulated = emulator_read_emulated,
3412 .write_emulated = emulator_write_emulated,
3413 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3414};
3415
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003416static void cache_all_regs(struct kvm_vcpu *vcpu)
3417{
3418 kvm_register_read(vcpu, VCPU_REGS_RAX);
3419 kvm_register_read(vcpu, VCPU_REGS_RSP);
3420 kvm_register_read(vcpu, VCPU_REGS_RIP);
3421 vcpu->arch.regs_dirty = ~0;
3422}
3423
Carsten Ottebbd9b642007-10-30 18:44:21 +01003424int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003425 unsigned long cr2,
3426 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003427 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003428{
Glauber Costa310b5d32009-05-12 16:21:06 -04003429 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003430 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003431 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003432
Avi Kivity26eef702008-07-03 14:59:22 +03003433 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003434 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003435 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003436 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003437 * instead of direct ->regs accesses, can save hundred cycles
3438 * on Intel for instructions that don't read/change RSP, for
3439 * for example.
3440 */
3441 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003442
3443 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003444 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003445
Sheng Yang571008d2008-01-02 14:49:22 +08003446 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003447 int cs_db, cs_l;
3448 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3449
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003450 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka83bf0002010-02-23 17:47:59 +01003451 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003452 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003453 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003454 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003455 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003456 ? X86EMUL_MODE_PROT64 : cs_db
3457 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3458
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003459 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003460
Andre Przywara0cb57622009-06-17 15:50:31 +02003461 /* Only allow emulation of specific instructions on #UD
3462 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003463 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003464 if (emulation_type & EMULTYPE_TRAP_UD) {
3465 if (!c->twobyte)
3466 return EMULATE_FAIL;
3467 switch (c->b) {
3468 case 0x01: /* VMMCALL */
3469 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3470 return EMULATE_FAIL;
3471 break;
3472 case 0x34: /* sysenter */
3473 case 0x35: /* sysexit */
3474 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3475 return EMULATE_FAIL;
3476 break;
3477 case 0x05: /* syscall */
3478 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3479 return EMULATE_FAIL;
3480 break;
3481 default:
3482 return EMULATE_FAIL;
3483 }
3484
3485 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3486 return EMULATE_FAIL;
3487 }
Sheng Yang571008d2008-01-02 14:49:22 +08003488
Avi Kivityf2b57562007-11-18 15:17:51 +02003489 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003490 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003491 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003492 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3493 return EMULATE_DONE;
3494 return EMULATE_FAIL;
3495 }
3496 }
3497
Gleb Natapovba8afb62009-04-12 13:36:57 +03003498 if (emulation_type & EMULTYPE_SKIP) {
3499 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3500 return EMULATE_DONE;
3501 }
3502
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003503 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003504 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3505
3506 if (r == 0)
3507 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003508
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003509 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003510 return EMULATE_DO_MMIO;
3511
Gleb Natapov112592d2010-02-21 15:00:47 +02003512 if (r || vcpu->mmio_is_write) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003513 run->exit_reason = KVM_EXIT_MMIO;
3514 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3515 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3516 run->mmio.len = vcpu->mmio_size;
3517 run->mmio.is_write = vcpu->mmio_is_write;
3518 }
3519
3520 if (r) {
3521 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3522 return EMULATE_DONE;
3523 if (!vcpu->mmio_needed) {
3524 kvm_report_emulation_failure(vcpu, "mmio");
3525 return EMULATE_FAIL;
3526 }
3527 return EMULATE_DO_MMIO;
3528 }
3529
Jan Kiszka83bf0002010-02-23 17:47:59 +01003530 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003531
3532 if (vcpu->mmio_is_write) {
3533 vcpu->mmio_needed = 0;
3534 return EMULATE_DO_MMIO;
3535 }
3536
3537 return EMULATE_DONE;
3538}
3539EXPORT_SYMBOL_GPL(emulate_instruction);
3540
Carsten Ottede7d7892007-10-30 18:44:25 +01003541static int pio_copy_data(struct kvm_vcpu *vcpu)
3542{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003543 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003544 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003545 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003546 int ret;
Gleb Natapov1871c602010-02-10 14:21:32 +02003547 u32 error_code;
Carsten Ottede7d7892007-10-30 18:44:25 +01003548
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003549 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3550 if (vcpu->arch.pio.in)
Gleb Natapov1871c602010-02-10 14:21:32 +02003551 ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code);
Carsten Ottede7d7892007-10-30 18:44:25 +01003552 else
Gleb Natapov1871c602010-02-10 14:21:32 +02003553 ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code);
3554
3555 if (ret == X86EMUL_PROPAGATE_FAULT)
3556 kvm_inject_page_fault(vcpu, q, error_code);
3557
Izik Eidus0f346072008-12-29 01:42:20 +02003558 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003559}
3560
3561int complete_pio(struct kvm_vcpu *vcpu)
3562{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003563 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003564 long delta;
3565 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003566 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003567
3568 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003569 if (io->in) {
3570 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3571 memcpy(&val, vcpu->arch.pio_data, io->size);
3572 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3573 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003574 } else {
3575 if (io->in) {
3576 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003577 if (r)
Gleb Natapov1871c602010-02-10 14:21:32 +02003578 goto out;
Carsten Ottede7d7892007-10-30 18:44:25 +01003579 }
3580
3581 delta = 1;
3582 if (io->rep) {
3583 delta *= io->cur_count;
3584 /*
3585 * The size of the register should really depend on
3586 * current address size.
3587 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003588 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3589 val -= delta;
3590 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003591 }
3592 if (io->down)
3593 delta = -delta;
3594 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003595 if (io->in) {
3596 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3597 val += delta;
3598 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3599 } else {
3600 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3601 val += delta;
3602 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3603 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003604 }
Gleb Natapov1871c602010-02-10 14:21:32 +02003605out:
Carsten Ottede7d7892007-10-30 18:44:25 +01003606 io->count -= io->cur_count;
3607 io->cur_count = 0;
3608
3609 return 0;
3610}
3611
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003612static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003613{
3614 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003615 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003616
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003617 if (vcpu->arch.pio.in)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003618 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003619 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003620 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003621 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3622 vcpu->arch.pio.port, vcpu->arch.pio.size,
3623 pd);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003624 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003625}
3626
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003627static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003628{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003629 struct kvm_pio_request *io = &vcpu->arch.pio;
3630 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003631 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003632
Carsten Ottede7d7892007-10-30 18:44:25 +01003633 for (i = 0; i < io->cur_count; i++) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003634 if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003635 io->port, io->size, pd)) {
3636 r = -EOPNOTSUPP;
3637 break;
3638 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003639 pd += io->size;
3640 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003641 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003642}
3643
Avi Kivity851ba692009-08-24 11:10:17 +03003644int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003645{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003646 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003647
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003648 trace_kvm_pio(!in, port, size, 1);
3649
Carsten Ottede7d7892007-10-30 18:44:25 +01003650 vcpu->run->exit_reason = KVM_EXIT_IO;
3651 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003652 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003653 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003654 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3655 vcpu->run->io.port = vcpu->arch.pio.port = port;
3656 vcpu->arch.pio.in = in;
3657 vcpu->arch.pio.string = 0;
3658 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003659 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003660
Takuya Yoshikawa1976d2d2010-02-05 17:52:46 +09003661 if (!vcpu->arch.pio.in) {
3662 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3663 memcpy(vcpu->arch.pio_data, &val, 4);
3664 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003665
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003666 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003667 complete_pio(vcpu);
3668 return 1;
3669 }
3670 return 0;
3671}
3672EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3673
Avi Kivity851ba692009-08-24 11:10:17 +03003674int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003675 int size, unsigned long count, int down,
3676 gva_t address, int rep, unsigned port)
3677{
3678 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003679 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003680
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003681 trace_kvm_pio(!in, port, size, count);
3682
Carsten Ottede7d7892007-10-30 18:44:25 +01003683 vcpu->run->exit_reason = KVM_EXIT_IO;
3684 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003685 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003686 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003687 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3688 vcpu->run->io.port = vcpu->arch.pio.port = port;
3689 vcpu->arch.pio.in = in;
3690 vcpu->arch.pio.string = 1;
3691 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003692 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003693
3694 if (!count) {
3695 kvm_x86_ops->skip_emulated_instruction(vcpu);
3696 return 1;
3697 }
3698
3699 if (!down)
3700 in_page = PAGE_SIZE - offset_in_page(address);
3701 else
3702 in_page = offset_in_page(address) + size;
3703 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003704 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003705 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003706 if (down) {
3707 /*
3708 * String I/O in reverse. Yuck. Kill the guest, fix later.
3709 */
3710 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003711 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003712 return 1;
3713 }
3714 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003715 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003716
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003717 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003718 kvm_x86_ops->skip_emulated_instruction(vcpu);
3719
Izik Eidus0f346072008-12-29 01:42:20 +02003720 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003721
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003722 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003723 /* string PIO write */
3724 ret = pio_copy_data(vcpu);
Gleb Natapov1871c602010-02-10 14:21:32 +02003725 if (ret == X86EMUL_PROPAGATE_FAULT)
Izik Eidus0f346072008-12-29 01:42:20 +02003726 return 1;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003727 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003728 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003729 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003730 ret = 1;
3731 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003732 }
3733 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003734
3735 return ret;
3736}
3737EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3738
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003739static void bounce_off(void *info)
3740{
3741 /* nothing */
3742}
3743
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003744static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3745 void *data)
3746{
3747 struct cpufreq_freqs *freq = data;
3748 struct kvm *kvm;
3749 struct kvm_vcpu *vcpu;
3750 int i, send_ipi = 0;
3751
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003752 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3753 return 0;
3754 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3755 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003756 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003757
3758 spin_lock(&kvm_lock);
3759 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003760 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003761 if (vcpu->cpu != freq->cpu)
3762 continue;
3763 if (!kvm_request_guest_time_update(vcpu))
3764 continue;
3765 if (vcpu->cpu != smp_processor_id())
3766 send_ipi++;
3767 }
3768 }
3769 spin_unlock(&kvm_lock);
3770
3771 if (freq->old < freq->new && send_ipi) {
3772 /*
3773 * We upscale the frequency. Must make the guest
3774 * doesn't see old kvmclock values while running with
3775 * the new frequency, otherwise we risk the guest sees
3776 * time go backwards.
3777 *
3778 * In case we update the frequency for another cpu
3779 * (which might be in guest context) send an interrupt
3780 * to kick the cpu out of guest context. Next time
3781 * guest context is entered kvmclock will be updated,
3782 * so the guest will not see stale values.
3783 */
3784 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3785 }
3786 return 0;
3787}
3788
3789static struct notifier_block kvmclock_cpufreq_notifier_block = {
3790 .notifier_call = kvmclock_cpufreq_notifier
3791};
3792
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003793static void kvm_timer_init(void)
3794{
3795 int cpu;
3796
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003797 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003798 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3799 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003800 for_each_online_cpu(cpu) {
3801 unsigned long khz = cpufreq_get(cpu);
3802 if (!khz)
3803 khz = tsc_khz;
3804 per_cpu(cpu_tsc_khz, cpu) = khz;
3805 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003806 } else {
3807 for_each_possible_cpu(cpu)
3808 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003809 }
3810}
3811
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003812int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003813{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003814 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003815 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3816
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003817 if (kvm_x86_ops) {
3818 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003819 r = -EEXIST;
3820 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003821 }
3822
3823 if (!ops->cpu_has_kvm_support()) {
3824 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003825 r = -EOPNOTSUPP;
3826 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003827 }
3828 if (ops->disabled_by_bios()) {
3829 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003830 r = -EOPNOTSUPP;
3831 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003832 }
3833
Avi Kivity97db56c2008-01-13 13:23:56 +02003834 r = kvm_mmu_module_init();
3835 if (r)
3836 goto out;
3837
3838 kvm_init_msr_list();
3839
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003840 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003841 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003842 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3843 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003844 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003845
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003846 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003847
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003848 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003849
3850out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003851 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003852}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003853
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003854void kvm_arch_exit(void)
3855{
Jan Kiszka888d2562009-04-17 19:24:58 +02003856 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3857 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3858 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003859 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003860 kvm_mmu_module_exit();
3861}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003862
Hollis Blanchard8776e512007-10-31 17:24:24 -05003863int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3864{
3865 ++vcpu->stat.halt_exits;
3866 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003867 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003868 return 1;
3869 } else {
3870 vcpu->run->exit_reason = KVM_EXIT_HLT;
3871 return 0;
3872 }
3873}
3874EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3875
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003876static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3877 unsigned long a1)
3878{
3879 if (is_long_mode(vcpu))
3880 return a0;
3881 else
3882 return a0 | ((gpa_t)a1 << 32);
3883}
3884
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003885int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
3886{
3887 u64 param, ingpa, outgpa, ret;
3888 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
3889 bool fast, longmode;
3890 int cs_db, cs_l;
3891
3892 /*
3893 * hypercall generates UD from non zero cpl and real mode
3894 * per HYPER-V spec
3895 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003896 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003897 kvm_queue_exception(vcpu, UD_VECTOR);
3898 return 0;
3899 }
3900
3901 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3902 longmode = is_long_mode(vcpu) && cs_l == 1;
3903
3904 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02003905 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
3906 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
3907 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
3908 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
3909 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
3910 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003911 }
3912#ifdef CONFIG_X86_64
3913 else {
3914 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
3915 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
3916 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
3917 }
3918#endif
3919
3920 code = param & 0xffff;
3921 fast = (param >> 16) & 0x1;
3922 rep_cnt = (param >> 32) & 0xfff;
3923 rep_idx = (param >> 48) & 0xfff;
3924
3925 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
3926
Gleb Natapovc25bc162010-01-17 15:51:24 +02003927 switch (code) {
3928 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
3929 kvm_vcpu_on_spin(vcpu);
3930 break;
3931 default:
3932 res = HV_STATUS_INVALID_HYPERCALL_CODE;
3933 break;
3934 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003935
3936 ret = res | (((u64)rep_done & 0xfff) << 32);
3937 if (longmode) {
3938 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3939 } else {
3940 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
3941 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
3942 }
3943
3944 return 1;
3945}
3946
Hollis Blanchard8776e512007-10-31 17:24:24 -05003947int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3948{
3949 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003950 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003951
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003952 if (kvm_hv_hypercall_enabled(vcpu->kvm))
3953 return kvm_hv_hypercall(vcpu);
3954
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003955 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3956 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3957 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3958 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3959 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003960
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003961 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003962
Hollis Blanchard8776e512007-10-31 17:24:24 -05003963 if (!is_long_mode(vcpu)) {
3964 nr &= 0xFFFFFFFF;
3965 a0 &= 0xFFFFFFFF;
3966 a1 &= 0xFFFFFFFF;
3967 a2 &= 0xFFFFFFFF;
3968 a3 &= 0xFFFFFFFF;
3969 }
3970
Jan Kiszka07708c42009-08-03 18:43:28 +02003971 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3972 ret = -KVM_EPERM;
3973 goto out;
3974 }
3975
Hollis Blanchard8776e512007-10-31 17:24:24 -05003976 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003977 case KVM_HC_VAPIC_POLL_IRQ:
3978 ret = 0;
3979 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003980 case KVM_HC_MMU_OP:
3981 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3982 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003983 default:
3984 ret = -KVM_ENOSYS;
3985 break;
3986 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003987out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003988 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303989 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003990 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003991}
3992EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3993
3994int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3995{
3996 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003997 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003998
Hollis Blanchard8776e512007-10-31 17:24:24 -05003999 /*
4000 * Blow out the MMU to ensure that no other VCPU has an active mapping
4001 * to ensure that the updated hypercall appears atomically across all
4002 * VCPUs.
4003 */
4004 kvm_mmu_zap_all(vcpu->kvm);
4005
Hollis Blanchard8776e512007-10-31 17:24:24 -05004006 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004007
Takuya Yoshikawa7edcfac2010-02-01 22:11:52 +09004008 return emulator_write_emulated(rip, instruction, 3, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004009}
4010
4011static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4012{
4013 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4014}
4015
4016void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4017{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004018 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004019
4020 kvm_x86_ops->set_gdt(vcpu, &dt);
4021}
4022
4023void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4024{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004025 struct desc_ptr dt = { limit, base };
Hollis Blanchard8776e512007-10-31 17:24:24 -05004026
4027 kvm_x86_ops->set_idt(vcpu, &dt);
4028}
4029
4030void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
4031 unsigned long *rflags)
4032{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004033 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02004034 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004035}
4036
4037unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
4038{
Joerg Roedel54e445c2008-04-30 17:56:02 +02004039 unsigned long value;
4040
Hollis Blanchard8776e512007-10-31 17:24:24 -05004041 switch (cr) {
4042 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004043 value = kvm_read_cr0(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004044 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004045 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004046 value = vcpu->arch.cr2;
4047 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004048 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004049 value = vcpu->arch.cr3;
4050 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004051 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004052 value = kvm_read_cr4(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004053 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004054 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004055 value = kvm_get_cr8(vcpu);
4056 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004057 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004058 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004059 return 0;
4060 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02004061
4062 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004063}
4064
4065void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
4066 unsigned long *rflags)
4067{
4068 switch (cr) {
4069 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004070 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Jan Kiszka91586a32009-10-05 13:07:21 +02004071 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004072 break;
4073 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004074 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004075 break;
4076 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004077 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004078 break;
4079 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004080 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004081 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004082 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004083 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004084 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004085 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004086 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004087 }
4088}
4089
Dan Kenigsberg07716712007-11-21 17:10:04 +02004090static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4091{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004092 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4093 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004094
4095 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4096 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004097 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004098 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004099 if (ej->function == e->function) {
4100 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4101 return j;
4102 }
4103 }
4104 return 0; /* silence gcc, even though control never reaches here */
4105}
4106
4107/* find an entry with matching function, matching index (if needed), and that
4108 * should be read next (if it's stateful) */
4109static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4110 u32 function, u32 index)
4111{
4112 if (e->function != function)
4113 return 0;
4114 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4115 return 0;
4116 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004117 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004118 return 0;
4119 return 1;
4120}
4121
Alexander Grafd8017472008-11-25 20:17:11 +01004122struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4123 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004124{
4125 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004126 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004127
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004128 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004129 struct kvm_cpuid_entry2 *e;
4130
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004131 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004132 if (is_matching_cpuid_entry(e, function, index)) {
4133 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4134 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004135 best = e;
4136 break;
4137 }
4138 /*
4139 * Both basic or both extended?
4140 */
4141 if (((e->function ^ function) & 0x80000000) == 0)
4142 if (!best || e->function > best->function)
4143 best = e;
4144 }
Alexander Grafd8017472008-11-25 20:17:11 +01004145 return best;
4146}
Sheng Yang0e851882009-12-18 16:48:46 +08004147EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004148
Dong, Eddie82725b22009-03-30 16:21:08 +08004149int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4150{
4151 struct kvm_cpuid_entry2 *best;
4152
4153 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4154 if (best)
4155 return best->eax & 0xff;
4156 return 36;
4157}
4158
Alexander Grafd8017472008-11-25 20:17:11 +01004159void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4160{
4161 u32 function, index;
4162 struct kvm_cpuid_entry2 *best;
4163
4164 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4165 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4166 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4167 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4168 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4169 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4170 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004171 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004172 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4173 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4174 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4175 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004176 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004177 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004178 trace_kvm_cpuid(function,
4179 kvm_register_read(vcpu, VCPU_REGS_RAX),
4180 kvm_register_read(vcpu, VCPU_REGS_RBX),
4181 kvm_register_read(vcpu, VCPU_REGS_RCX),
4182 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004183}
4184EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004185
4186/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004187 * Check if userspace requested an interrupt window, and that the
4188 * interrupt window is open.
4189 *
4190 * No need to exit to userspace if we already have an interrupt queued.
4191 */
Avi Kivity851ba692009-08-24 11:10:17 +03004192static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004193{
Gleb Natapov80618232009-04-21 17:44:56 +03004194 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004195 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004196 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004197}
4198
Avi Kivity851ba692009-08-24 11:10:17 +03004199static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004200{
Avi Kivity851ba692009-08-24 11:10:17 +03004201 struct kvm_run *kvm_run = vcpu->run;
4202
Jan Kiszka91586a32009-10-05 13:07:21 +02004203 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004204 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004205 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004206 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004207 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004208 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004209 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004210 kvm_arch_interrupt_allowed(vcpu) &&
4211 !kvm_cpu_has_interrupt(vcpu) &&
4212 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004213}
4214
Avi Kivityb93463a2007-10-25 16:52:32 +02004215static void vapic_enter(struct kvm_vcpu *vcpu)
4216{
4217 struct kvm_lapic *apic = vcpu->arch.apic;
4218 struct page *page;
4219
4220 if (!apic || !apic->vapic_addr)
4221 return;
4222
4223 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004224
4225 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004226}
4227
4228static void vapic_exit(struct kvm_vcpu *vcpu)
4229{
4230 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004231 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004232
4233 if (!apic || !apic->vapic_addr)
4234 return;
4235
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004236 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004237 kvm_release_page_dirty(apic->vapic_page);
4238 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004239 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004240}
4241
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004242static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4243{
4244 int max_irr, tpr;
4245
4246 if (!kvm_x86_ops->update_cr8_intercept)
4247 return;
4248
Avi Kivity88c808f2009-08-17 22:49:40 +03004249 if (!vcpu->arch.apic)
4250 return;
4251
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004252 if (!vcpu->arch.apic->vapic_addr)
4253 max_irr = kvm_lapic_find_highest_irr(vcpu);
4254 else
4255 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004256
4257 if (max_irr != -1)
4258 max_irr >>= 4;
4259
4260 tpr = kvm_lapic_get_cr8(vcpu);
4261
4262 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4263}
4264
Avi Kivity851ba692009-08-24 11:10:17 +03004265static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004266{
4267 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004268 if (vcpu->arch.exception.pending) {
4269 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4270 vcpu->arch.exception.has_error_code,
4271 vcpu->arch.exception.error_code);
4272 return;
4273 }
4274
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004275 if (vcpu->arch.nmi_injected) {
4276 kvm_x86_ops->set_nmi(vcpu);
4277 return;
4278 }
4279
4280 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004281 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004282 return;
4283 }
4284
4285 /* try to inject new event if pending */
4286 if (vcpu->arch.nmi_pending) {
4287 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4288 vcpu->arch.nmi_pending = false;
4289 vcpu->arch.nmi_injected = true;
4290 kvm_x86_ops->set_nmi(vcpu);
4291 }
4292 } else if (kvm_cpu_has_interrupt(vcpu)) {
4293 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004294 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4295 false);
4296 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004297 }
4298 }
4299}
4300
Avi Kivity851ba692009-08-24 11:10:17 +03004301static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004302{
4303 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004304 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004305 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004306
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004307 if (vcpu->requests)
4308 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4309 kvm_mmu_unload(vcpu);
4310
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004311 r = kvm_mmu_reload(vcpu);
4312 if (unlikely(r))
4313 goto out;
4314
Avi Kivity2f52d582008-01-16 12:49:30 +02004315 if (vcpu->requests) {
4316 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004317 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004318 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4319 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004320 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4321 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004322 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4323 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004324 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4325 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004326 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004327 r = 0;
4328 goto out;
4329 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004330 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004331 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004332 r = 0;
4333 goto out;
4334 }
Avi Kivity02daab22009-12-30 12:40:26 +02004335 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4336 vcpu->fpu_active = 0;
4337 kvm_x86_ops->fpu_deactivate(vcpu);
4338 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004339 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004340
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004341 preempt_disable();
4342
4343 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004344 if (vcpu->fpu_active)
4345 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004346
4347 local_irq_disable();
4348
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004349 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4350 smp_mb__after_clear_bit();
4351
Marcelo Tosattid7690172008-09-08 15:23:48 -03004352 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004353 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c1428012008-01-15 18:27:32 +02004354 local_irq_enable();
4355 preempt_enable();
4356 r = 1;
4357 goto out;
4358 }
4359
Avi Kivity851ba692009-08-24 11:10:17 +03004360 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004361
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004362 /* enable NMI/IRQ window open exits if needed */
4363 if (vcpu->arch.nmi_pending)
4364 kvm_x86_ops->enable_nmi_window(vcpu);
4365 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4366 kvm_x86_ops->enable_irq_window(vcpu);
4367
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004368 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004369 update_cr8_intercept(vcpu);
4370 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004371 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004372
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004373 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004374
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004375 kvm_guest_enter();
4376
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004377 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004378 set_debugreg(0, 7);
4379 set_debugreg(vcpu->arch.eff_db[0], 0);
4380 set_debugreg(vcpu->arch.eff_db[1], 1);
4381 set_debugreg(vcpu->arch.eff_db[2], 2);
4382 set_debugreg(vcpu->arch.eff_db[3], 3);
4383 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004384
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004385 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004386 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004387
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004388 /*
4389 * If the guest has used debug registers, at least dr7
4390 * will be disabled while returning to the host.
4391 * If we don't have active breakpoints in the host, we don't
4392 * care about the messed up debug address registers. But if
4393 * we have some of them active, restore the old state.
4394 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004395 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004396 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004397
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004398 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004399 local_irq_enable();
4400
4401 ++vcpu->stat.exits;
4402
4403 /*
4404 * We must have an instruction between local_irq_enable() and
4405 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4406 * the interrupt shadow. The stat.exits increment will do nicely.
4407 * But we need to prevent reordering, hence this barrier():
4408 */
4409 barrier();
4410
4411 kvm_guest_exit();
4412
4413 preempt_enable();
4414
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004415 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004416
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004417 /*
4418 * Profile KVM exit RIPs:
4419 */
4420 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004421 unsigned long rip = kvm_rip_read(vcpu);
4422 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004423 }
4424
Avi Kivity298101d2007-11-25 13:41:11 +02004425
Avi Kivityb93463a2007-10-25 16:52:32 +02004426 kvm_lapic_sync_from_vapic(vcpu);
4427
Avi Kivity851ba692009-08-24 11:10:17 +03004428 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004429out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004430 return r;
4431}
4432
Gleb Natapov09cec752009-03-23 15:11:44 +02004433
Avi Kivity851ba692009-08-24 11:10:17 +03004434static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004435{
4436 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004437 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004438
4439 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004440 pr_debug("vcpu %d received sipi with vector # %x\n",
4441 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004442 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004443 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004444 if (r)
4445 return r;
4446 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004447 }
4448
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004449 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004450 vapic_enter(vcpu);
4451
4452 r = 1;
4453 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004454 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004455 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004456 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004457 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004458 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004459 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004460 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004461 {
4462 switch(vcpu->arch.mp_state) {
4463 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004464 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004465 KVM_MP_STATE_RUNNABLE;
4466 case KVM_MP_STATE_RUNNABLE:
4467 break;
4468 case KVM_MP_STATE_SIPI_RECEIVED:
4469 default:
4470 r = -EINTR;
4471 break;
4472 }
4473 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004474 }
4475
Gleb Natapov09cec752009-03-23 15:11:44 +02004476 if (r <= 0)
4477 break;
4478
4479 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4480 if (kvm_cpu_has_pending_timer(vcpu))
4481 kvm_inject_pending_timer_irqs(vcpu);
4482
Avi Kivity851ba692009-08-24 11:10:17 +03004483 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004484 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004485 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004486 ++vcpu->stat.request_irq_exits;
4487 }
4488 if (signal_pending(current)) {
4489 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004490 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004491 ++vcpu->stat.signal_exits;
4492 }
4493 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004494 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004495 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004496 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004497 }
4498 }
4499
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004500 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Avi Kivity851ba692009-08-24 11:10:17 +03004501 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004502
Avi Kivityb93463a2007-10-25 16:52:32 +02004503 vapic_exit(vcpu);
4504
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004505 return r;
4506}
4507
4508int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4509{
4510 int r;
4511 sigset_t sigsaved;
4512
4513 vcpu_load(vcpu);
4514
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004515 if (vcpu->sigset_active)
4516 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4517
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004518 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4519 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004520 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004521 r = -EAGAIN;
4522 goto out;
4523 }
4524
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004525 /* re-sync apic's tpr */
4526 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004527 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004528
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004529 if (vcpu->arch.pio.cur_count) {
Gleb Natapov7567cae2010-03-09 12:01:10 +02004530 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004531 r = complete_pio(vcpu);
Gleb Natapov7567cae2010-03-09 12:01:10 +02004532 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004533 if (r)
4534 goto out;
4535 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004536 if (vcpu->mmio_needed) {
4537 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4538 vcpu->mmio_read_completed = 1;
4539 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004540
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004541 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity851ba692009-08-24 11:10:17 +03004542 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004543 EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004544 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004545 if (r == EMULATE_DO_MMIO) {
4546 /*
4547 * Read-modify-write. Back to userspace.
4548 */
4549 r = 0;
4550 goto out;
4551 }
4552 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004553 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4554 kvm_register_write(vcpu, VCPU_REGS_RAX,
4555 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004556
Avi Kivity851ba692009-08-24 11:10:17 +03004557 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004558
4559out:
4560 if (vcpu->sigset_active)
4561 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4562
4563 vcpu_put(vcpu);
4564 return r;
4565}
4566
4567int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4568{
4569 vcpu_load(vcpu);
4570
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004571 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4572 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4573 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4574 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4575 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4576 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4577 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4578 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004579#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004580 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4581 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4582 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4583 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4584 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4585 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4586 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4587 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004588#endif
4589
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004590 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004591 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004592
4593 vcpu_put(vcpu);
4594
4595 return 0;
4596}
4597
4598int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4599{
4600 vcpu_load(vcpu);
4601
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004602 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4603 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4604 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4605 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4606 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4607 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4608 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4609 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004610#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004611 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4612 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4613 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4614 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4615 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4616 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4617 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4618 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004619#endif
4620
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004621 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004622 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004623
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004624 vcpu->arch.exception.pending = false;
4625
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004626 vcpu_put(vcpu);
4627
4628 return 0;
4629}
4630
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004631void kvm_get_segment(struct kvm_vcpu *vcpu,
4632 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004633{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004634 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004635}
4636
4637void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4638{
4639 struct kvm_segment cs;
4640
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004641 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004642 *db = cs.db;
4643 *l = cs.l;
4644}
4645EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4646
4647int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4648 struct kvm_sregs *sregs)
4649{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004650 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004651
4652 vcpu_load(vcpu);
4653
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004654 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4655 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4656 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4657 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4658 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4659 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004660
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004661 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4662 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004663
4664 kvm_x86_ops->get_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004665 sregs->idt.limit = dt.size;
4666 sregs->idt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004667 kvm_x86_ops->get_gdt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02004668 sregs->gdt.limit = dt.size;
4669 sregs->gdt.base = dt.address;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004670
Avi Kivity4d4ec082009-12-29 18:07:30 +02004671 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004672 sregs->cr2 = vcpu->arch.cr2;
4673 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004674 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004675 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02004676 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004677 sregs->apic_base = kvm_get_apic_base(vcpu);
4678
Gleb Natapov923c61b2009-05-11 13:35:48 +03004679 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004680
Gleb Natapov36752c92009-05-11 13:35:53 +03004681 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004682 set_bit(vcpu->arch.interrupt.nr,
4683 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004684
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004685 vcpu_put(vcpu);
4686
4687 return 0;
4688}
4689
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004690int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4691 struct kvm_mp_state *mp_state)
4692{
4693 vcpu_load(vcpu);
4694 mp_state->mp_state = vcpu->arch.mp_state;
4695 vcpu_put(vcpu);
4696 return 0;
4697}
4698
4699int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4700 struct kvm_mp_state *mp_state)
4701{
4702 vcpu_load(vcpu);
4703 vcpu->arch.mp_state = mp_state->mp_state;
4704 vcpu_put(vcpu);
4705 return 0;
4706}
4707
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004708static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004709 struct kvm_segment *var, int seg)
4710{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004711 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004712}
4713
Izik Eidus37817f22008-03-24 23:14:53 +02004714static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4715 struct kvm_segment *kvm_desct)
4716{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004717 kvm_desct->base = get_desc_base(seg_desc);
4718 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004719 if (seg_desc->g) {
4720 kvm_desct->limit <<= 12;
4721 kvm_desct->limit |= 0xfff;
4722 }
Izik Eidus37817f22008-03-24 23:14:53 +02004723 kvm_desct->selector = selector;
4724 kvm_desct->type = seg_desc->type;
4725 kvm_desct->present = seg_desc->p;
4726 kvm_desct->dpl = seg_desc->dpl;
4727 kvm_desct->db = seg_desc->d;
4728 kvm_desct->s = seg_desc->s;
4729 kvm_desct->l = seg_desc->l;
4730 kvm_desct->g = seg_desc->g;
4731 kvm_desct->avl = seg_desc->avl;
4732 if (!selector)
4733 kvm_desct->unusable = 1;
4734 else
4735 kvm_desct->unusable = 0;
4736 kvm_desct->padding = 0;
4737}
4738
Amit Shahb8222ad2008-10-22 16:39:47 +05304739static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4740 u16 selector,
Gleb Natapov89a27f42010-02-16 10:51:48 +02004741 struct desc_ptr *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004742{
4743 if (selector & 1 << 2) {
4744 struct kvm_segment kvm_seg;
4745
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004746 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004747
4748 if (kvm_seg.unusable)
Gleb Natapov89a27f42010-02-16 10:51:48 +02004749 dtable->size = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004750 else
Gleb Natapov89a27f42010-02-16 10:51:48 +02004751 dtable->size = kvm_seg.limit;
4752 dtable->address = kvm_seg.base;
Izik Eidus37817f22008-03-24 23:14:53 +02004753 }
4754 else
4755 kvm_x86_ops->get_gdt(vcpu, dtable);
4756}
4757
4758/* allowed just for 8 bytes segments */
4759static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4760 struct desc_struct *seg_desc)
4761{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004762 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004763 u16 index = selector >> 3;
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004764 int ret;
4765 u32 err;
4766 gva_t addr;
Izik Eidus37817f22008-03-24 23:14:53 +02004767
Amit Shahb8222ad2008-10-22 16:39:47 +05304768 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004769
Gleb Natapov89a27f42010-02-16 10:51:48 +02004770 if (dtable.size < index * 8 + 7) {
Izik Eidus37817f22008-03-24 23:14:53 +02004771 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
Takuya Yoshikawac125c602010-02-01 22:11:04 +09004772 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004773 }
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004774 addr = dtable.base + index * 8;
4775 ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc),
4776 vcpu, &err);
4777 if (ret == X86EMUL_PROPAGATE_FAULT)
4778 kvm_inject_page_fault(vcpu, addr, err);
4779
4780 return ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004781}
4782
4783/* allowed just for 8 bytes segments */
4784static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4785 struct desc_struct *seg_desc)
4786{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004787 struct desc_ptr dtable;
Izik Eidus37817f22008-03-24 23:14:53 +02004788 u16 index = selector >> 3;
4789
Amit Shahb8222ad2008-10-22 16:39:47 +05304790 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004791
Gleb Natapov89a27f42010-02-16 10:51:48 +02004792 if (dtable.size < index * 8 + 7)
Izik Eidus37817f22008-03-24 23:14:53 +02004793 return 1;
Gleb Natapov89a27f42010-02-16 10:51:48 +02004794 return kvm_write_guest_virt(dtable.address + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004795}
4796
Gleb Natapov1871c602010-02-10 14:21:32 +02004797static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu,
4798 struct desc_struct *seg_desc)
4799{
4800 u32 base_addr = get_desc_base(seg_desc);
4801
4802 return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL);
4803}
4804
4805static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004806 struct desc_struct *seg_desc)
4807{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004808 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004809
Gleb Natapov1871c602010-02-10 14:21:32 +02004810 return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004811}
4812
Izik Eidus37817f22008-03-24 23:14:53 +02004813static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4814{
4815 struct kvm_segment kvm_seg;
4816
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004817 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004818 return kvm_seg.selector;
4819}
4820
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004821static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004822{
4823 struct kvm_segment segvar = {
4824 .base = selector << 4,
4825 .limit = 0xffff,
4826 .selector = selector,
4827 .type = 3,
4828 .present = 1,
4829 .dpl = 3,
4830 .db = 0,
4831 .s = 1,
4832 .l = 0,
4833 .g = 0,
4834 .avl = 0,
4835 .unusable = 0,
4836 };
4837 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
Gleb Natapovc6975182010-02-18 12:15:01 +02004838 return X86EMUL_CONTINUE;
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004839}
4840
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004841static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4842{
4843 return (seg != VCPU_SREG_LDTR) &&
4844 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004845 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004846}
4847
Gleb Natapovc6975182010-02-18 12:15:01 +02004848int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004849{
4850 struct kvm_segment kvm_seg;
Gleb Natapove01c2422010-01-25 12:01:04 +02004851 struct desc_struct seg_desc;
Gleb Natapovc6975182010-02-18 12:15:01 +02004852 u8 dpl, rpl, cpl;
4853 unsigned err_vec = GP_VECTOR;
4854 u32 err_code = 0;
4855 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
4856 int ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004857
Avi Kivity3eeb3282010-01-21 15:31:48 +02004858 if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004859 return kvm_load_realmode_segment(vcpu, selector, seg);
Gleb Natapove01c2422010-01-25 12:01:04 +02004860
Gleb Natapovc6975182010-02-18 12:15:01 +02004861 /* NULL selector is not valid for TR, CS and SS */
4862 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
4863 && null_selector)
4864 goto exception;
4865
4866 /* TR should be in GDT only */
4867 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
4868 goto exception;
4869
4870 ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc);
4871 if (ret)
4872 return ret;
4873
Gleb Natapove01c2422010-01-25 12:01:04 +02004874 seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg);
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004875
Gleb Natapovc6975182010-02-18 12:15:01 +02004876 if (null_selector) { /* for NULL selector skip all following checks */
4877 kvm_seg.unusable = 1;
4878 goto load;
4879 }
Izik Eidus37817f22008-03-24 23:14:53 +02004880
Gleb Natapovc6975182010-02-18 12:15:01 +02004881 err_code = selector & 0xfffc;
4882 err_vec = GP_VECTOR;
Izik Eidus37817f22008-03-24 23:14:53 +02004883
Gleb Natapovc6975182010-02-18 12:15:01 +02004884 /* can't load system descriptor into segment selecor */
4885 if (seg <= VCPU_SREG_GS && !kvm_seg.s)
4886 goto exception;
4887
4888 if (!kvm_seg.present) {
4889 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
4890 goto exception;
4891 }
4892
4893 rpl = selector & 3;
4894 dpl = kvm_seg.dpl;
4895 cpl = kvm_x86_ops->get_cpl(vcpu);
4896
4897 switch (seg) {
4898 case VCPU_SREG_SS:
4899 /*
4900 * segment is not a writable data segment or segment
4901 * selector's RPL != CPL or segment selector's RPL != CPL
4902 */
4903 if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl)
4904 goto exception;
4905 break;
4906 case VCPU_SREG_CS:
4907 if (!(kvm_seg.type & 8))
4908 goto exception;
4909
4910 if (kvm_seg.type & 4) {
4911 /* conforming */
4912 if (dpl > cpl)
4913 goto exception;
4914 } else {
4915 /* nonconforming */
4916 if (rpl > cpl || dpl != cpl)
4917 goto exception;
4918 }
4919 /* CS(RPL) <- CPL */
4920 selector = (selector & 0xfffc) | cpl;
4921 break;
4922 case VCPU_SREG_TR:
4923 if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9))
4924 goto exception;
4925 break;
4926 case VCPU_SREG_LDTR:
4927 if (kvm_seg.s || kvm_seg.type != 2)
4928 goto exception;
4929 break;
4930 default: /* DS, ES, FS, or GS */
4931 /*
4932 * segment is not a data or readable code segment or
4933 * ((segment is a data or nonconforming code segment)
4934 * and (both RPL and CPL > DPL))
4935 */
4936 if ((kvm_seg.type & 0xa) == 0x8 ||
4937 (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl)))
4938 goto exception;
4939 break;
4940 }
4941
4942 if (!kvm_seg.unusable && kvm_seg.s) {
Gleb Natapove01c2422010-01-25 12:01:04 +02004943 /* mark segment as accessed */
Gleb Natapovc6975182010-02-18 12:15:01 +02004944 kvm_seg.type |= 1;
Gleb Natapove01c2422010-01-25 12:01:04 +02004945 seg_desc.type |= 1;
4946 save_guest_segment_descriptor(vcpu, selector, &seg_desc);
4947 }
Gleb Natapovc6975182010-02-18 12:15:01 +02004948load:
4949 kvm_set_segment(vcpu, &kvm_seg, seg);
4950 return X86EMUL_CONTINUE;
4951exception:
4952 kvm_queue_exception_e(vcpu, err_vec, err_code);
4953 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004954}
4955
4956static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4957 struct tss_segment_32 *tss)
4958{
4959 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004960 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004961 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004962 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4963 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4964 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4965 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4966 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4967 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4968 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4969 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004970 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4971 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4972 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4973 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4974 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4975 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4976 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004977}
4978
Gleb Natapovc6975182010-02-18 12:15:01 +02004979static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg)
4980{
4981 struct kvm_segment kvm_seg;
4982 kvm_get_segment(vcpu, &kvm_seg, seg);
4983 kvm_seg.selector = sel;
4984 kvm_set_segment(vcpu, &kvm_seg, seg);
4985}
4986
Izik Eidus37817f22008-03-24 23:14:53 +02004987static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4988 struct tss_segment_32 *tss)
4989{
4990 kvm_set_cr3(vcpu, tss->cr3);
4991
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004992 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004993 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004994
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004995 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4996 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4997 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4998 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4999 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
5000 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
5001 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
5002 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02005003
Gleb Natapovc6975182010-02-18 12:15:01 +02005004 /*
5005 * SDM says that segment selectors are loaded before segment
5006 * descriptors
5007 */
5008 kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR);
5009 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5010 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5011 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5012 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5013 kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS);
5014 kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS);
5015
5016 /*
5017 * Now load segment descriptors. If fault happenes at this stage
5018 * it is handled in a context of new task
5019 */
5020 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005021 return 1;
5022
Gleb Natapovc6975182010-02-18 12:15:01 +02005023 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005024 return 1;
5025
Gleb Natapovc6975182010-02-18 12:15:01 +02005026 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
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->ss, VCPU_SREG_SS))
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->ds, VCPU_SREG_DS))
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->fs, VCPU_SREG_FS))
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->gs, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02005039 return 1;
5040 return 0;
5041}
5042
5043static void save_state_to_tss16(struct kvm_vcpu *vcpu,
5044 struct tss_segment_16 *tss)
5045{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005046 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02005047 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005048 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
5049 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
5050 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
5051 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
5052 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
5053 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
5054 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
5055 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02005056
5057 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
5058 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
5059 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
5060 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
5061 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02005062}
5063
5064static int load_state_from_tss16(struct kvm_vcpu *vcpu,
5065 struct tss_segment_16 *tss)
5066{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005067 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02005068 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005069 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
5070 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
5071 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
5072 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
5073 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
5074 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
5075 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
5076 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02005077
Gleb Natapovc6975182010-02-18 12:15:01 +02005078 /*
5079 * SDM says that segment selectors are loaded before segment
5080 * descriptors
5081 */
5082 kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR);
5083 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5084 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5085 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5086 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5087
5088 /*
5089 * Now load segment descriptors. If fault happenes at this stage
5090 * it is handled in a context of new task
5091 */
5092 if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005093 return 1;
5094
Gleb Natapovc6975182010-02-18 12:15:01 +02005095 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005096 return 1;
5097
Gleb Natapovc6975182010-02-18 12:15:01 +02005098 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005099 return 1;
5100
Gleb Natapovc6975182010-02-18 12:15:01 +02005101 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005102 return 1;
5103
Gleb Natapovc6975182010-02-18 12:15:01 +02005104 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005105 return 1;
5106 return 0;
5107}
5108
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005109static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005110 u16 old_tss_sel, u32 old_tss_base,
5111 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02005112{
5113 struct tss_segment_16 tss_segment_16;
5114 int ret = 0;
5115
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005116 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5117 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005118 goto out;
5119
5120 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02005121
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005122 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5123 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005124 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005125
Gleb Natapov1871c602010-02-10 14:21:32 +02005126 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005127 &tss_segment_16, sizeof tss_segment_16))
5128 goto out;
5129
Gleb Natapovb237ac32009-03-30 16:03:24 +03005130 if (old_tss_sel != 0xffff) {
5131 tss_segment_16.prev_task_link = old_tss_sel;
5132
5133 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005134 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005135 &tss_segment_16.prev_task_link,
5136 sizeof tss_segment_16.prev_task_link))
5137 goto out;
5138 }
5139
Izik Eidus37817f22008-03-24 23:14:53 +02005140 if (load_state_from_tss16(vcpu, &tss_segment_16))
5141 goto out;
5142
5143 ret = 1;
5144out:
5145 return ret;
5146}
5147
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005148static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005149 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02005150 struct desc_struct *nseg_desc)
5151{
5152 struct tss_segment_32 tss_segment_32;
5153 int ret = 0;
5154
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005155 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5156 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005157 goto out;
5158
5159 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02005160
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005161 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5162 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005163 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005164
Gleb Natapov1871c602010-02-10 14:21:32 +02005165 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005166 &tss_segment_32, sizeof tss_segment_32))
5167 goto out;
5168
Gleb Natapovb237ac32009-03-30 16:03:24 +03005169 if (old_tss_sel != 0xffff) {
5170 tss_segment_32.prev_task_link = old_tss_sel;
5171
5172 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005173 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005174 &tss_segment_32.prev_task_link,
5175 sizeof tss_segment_32.prev_task_link))
5176 goto out;
5177 }
5178
Izik Eidus37817f22008-03-24 23:14:53 +02005179 if (load_state_from_tss32(vcpu, &tss_segment_32))
5180 goto out;
5181
5182 ret = 1;
5183out:
5184 return ret;
5185}
5186
5187int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
5188{
5189 struct kvm_segment tr_seg;
5190 struct desc_struct cseg_desc;
5191 struct desc_struct nseg_desc;
5192 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005193 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
5194 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005195 u32 desc_limit;
Izik Eidus37817f22008-03-24 23:14:53 +02005196
Gleb Natapov1871c602010-02-10 14:21:32 +02005197 old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02005198
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005199 /* FIXME: Handle errors. Failure to read either TSS or their
5200 * descriptors should generate a pagefault.
5201 */
Izik Eidus37817f22008-03-24 23:14:53 +02005202 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
5203 goto out;
5204
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005205 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02005206 goto out;
5207
Izik Eidus37817f22008-03-24 23:14:53 +02005208 if (reason != TASK_SWITCH_IRET) {
5209 int cpl;
5210
5211 cpl = kvm_x86_ops->get_cpl(vcpu);
5212 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
5213 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
5214 return 1;
5215 }
5216 }
5217
Jan Kiszkae8861cfe2010-04-14 16:57:11 +02005218 desc_limit = get_desc_limit(&nseg_desc);
5219 if (!nseg_desc.p ||
5220 ((desc_limit < 0x67 && (nseg_desc.type & 8)) ||
5221 desc_limit < 0x2b)) {
Izik Eidus37817f22008-03-24 23:14:53 +02005222 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
5223 return 1;
5224 }
5225
5226 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005227 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005228 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005229 }
5230
5231 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005232 u32 eflags = kvm_get_rflags(vcpu);
5233 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005234 }
5235
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005236 /* set back link to prev task only if NT bit is set in eflags
5237 note that old_tss_sel is not used afetr this point */
5238 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
5239 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02005240
5241 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03005242 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
5243 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005244 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03005245 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
5246 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02005247
5248 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02005249 u32 eflags = kvm_get_rflags(vcpu);
5250 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02005251 }
5252
5253 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03005254 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02005255 save_guest_segment_descriptor(vcpu, tss_selector,
5256 &nseg_desc);
5257 }
5258
Avi Kivity4d4ec082009-12-29 18:07:30 +02005259 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS);
Izik Eidus37817f22008-03-24 23:14:53 +02005260 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
5261 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005262 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02005263out:
Izik Eidus37817f22008-03-24 23:14:53 +02005264 return ret;
5265}
5266EXPORT_SYMBOL_GPL(kvm_task_switch);
5267
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005268int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
5269 struct kvm_sregs *sregs)
5270{
5271 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03005272 int pending_vec, max_bits;
Gleb Natapov89a27f42010-02-16 10:51:48 +02005273 struct desc_ptr dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005274
5275 vcpu_load(vcpu);
5276
Gleb Natapov89a27f42010-02-16 10:51:48 +02005277 dt.size = sregs->idt.limit;
5278 dt.address = sregs->idt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005279 kvm_x86_ops->set_idt(vcpu, &dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02005280 dt.size = sregs->gdt.limit;
5281 dt.address = sregs->gdt.base;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005282 kvm_x86_ops->set_gdt(vcpu, &dt);
5283
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005284 vcpu->arch.cr2 = sregs->cr2;
5285 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02005286 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005287
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02005288 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005289
Avi Kivityf6801df2010-01-21 15:31:50 +02005290 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005291 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005292 kvm_set_apic_base(vcpu, sregs->apic_base);
5293
Avi Kivity4d4ec082009-12-29 18:07:30 +02005294 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005295 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00005296 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005297
Avi Kivityfc78f512009-12-07 12:16:48 +02005298 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005299 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005300 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005301 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005302 mmu_reset_needed = 1;
5303 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005304
5305 if (mmu_reset_needed)
5306 kvm_mmu_reset_context(vcpu);
5307
Gleb Natapov923c61b2009-05-11 13:35:48 +03005308 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
5309 pending_vec = find_first_bit(
5310 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
5311 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005312 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03005313 pr_debug("Set back pending irq %d\n", pending_vec);
5314 if (irqchip_in_kernel(vcpu->kvm))
5315 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005316 }
5317
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005318 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
5319 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
5320 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
5321 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
5322 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
5323 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005324
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02005325 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
5326 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005327
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03005328 update_cr8_intercept(vcpu);
5329
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005330 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005331 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005332 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
Avi Kivity3eeb3282010-01-21 15:31:48 +02005333 !is_protmode(vcpu))
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03005334 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5335
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005336 vcpu_put(vcpu);
5337
5338 return 0;
5339}
5340
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005341int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
5342 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005343{
Jan Kiszka355be0b2009-10-03 00:31:21 +02005344 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005345 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005346
5347 vcpu_load(vcpu);
5348
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005349 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
5350 r = -EBUSY;
5351 if (vcpu->arch.exception.pending)
5352 goto unlock_out;
5353 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
5354 kvm_queue_exception(vcpu, DB_VECTOR);
5355 else
5356 kvm_queue_exception(vcpu, BP_VECTOR);
5357 }
5358
Jan Kiszka91586a32009-10-05 13:07:21 +02005359 /*
5360 * Read rflags as long as potentially injected trace flags are still
5361 * filtered out.
5362 */
5363 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02005364
5365 vcpu->guest_debug = dbg->control;
5366 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
5367 vcpu->guest_debug = 0;
5368
5369 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01005370 for (i = 0; i < KVM_NR_DB_REGS; ++i)
5371 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
5372 vcpu->arch.switch_db_regs =
5373 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
5374 } else {
5375 for (i = 0; i < KVM_NR_DB_REGS; i++)
5376 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
5377 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
5378 }
5379
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005380 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5381 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
5382 get_segment_base(vcpu, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005383
Jan Kiszka91586a32009-10-05 13:07:21 +02005384 /*
5385 * Trigger an rflags update that will inject or remove the trace
5386 * flags.
5387 */
5388 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005389
Jan Kiszka355be0b2009-10-03 00:31:21 +02005390 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5391
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005392 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005393
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005394unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005395 vcpu_put(vcpu);
5396
5397 return r;
5398}
5399
5400/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005401 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5402 * we have asm/x86/processor.h
5403 */
5404struct fxsave {
5405 u16 cwd;
5406 u16 swd;
5407 u16 twd;
5408 u16 fop;
5409 u64 rip;
5410 u64 rdp;
5411 u32 mxcsr;
5412 u32 mxcsr_mask;
5413 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5414#ifdef CONFIG_X86_64
5415 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5416#else
5417 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5418#endif
5419};
5420
Zhang Xiantao8b006792007-11-16 13:05:55 +08005421/*
5422 * Translate a guest virtual address to a guest physical address.
5423 */
5424int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5425 struct kvm_translation *tr)
5426{
5427 unsigned long vaddr = tr->linear_address;
5428 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005429 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005430
5431 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005432 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005433 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005434 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005435 tr->physical_address = gpa;
5436 tr->valid = gpa != UNMAPPED_GVA;
5437 tr->writeable = 1;
5438 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005439 vcpu_put(vcpu);
5440
5441 return 0;
5442}
5443
Hollis Blanchardd0752062007-10-31 17:24:25 -05005444int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5445{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005446 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005447
5448 vcpu_load(vcpu);
5449
5450 memcpy(fpu->fpr, fxsave->st_space, 128);
5451 fpu->fcw = fxsave->cwd;
5452 fpu->fsw = fxsave->swd;
5453 fpu->ftwx = fxsave->twd;
5454 fpu->last_opcode = fxsave->fop;
5455 fpu->last_ip = fxsave->rip;
5456 fpu->last_dp = fxsave->rdp;
5457 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5458
5459 vcpu_put(vcpu);
5460
5461 return 0;
5462}
5463
5464int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5465{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005466 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005467
5468 vcpu_load(vcpu);
5469
5470 memcpy(fxsave->st_space, fpu->fpr, 128);
5471 fxsave->cwd = fpu->fcw;
5472 fxsave->swd = fpu->fsw;
5473 fxsave->twd = fpu->ftwx;
5474 fxsave->fop = fpu->last_opcode;
5475 fxsave->rip = fpu->last_ip;
5476 fxsave->rdp = fpu->last_dp;
5477 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5478
5479 vcpu_put(vcpu);
5480
5481 return 0;
5482}
5483
5484void fx_init(struct kvm_vcpu *vcpu)
5485{
5486 unsigned after_mxcsr_mask;
5487
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005488 /*
5489 * Touch the fpu the first time in non atomic context as if
5490 * this is the first fpu instruction the exception handler
5491 * will fire before the instruction returns and it'll have to
5492 * allocate ram with GFP_KERNEL.
5493 */
5494 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005495 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005496
Hollis Blanchardd0752062007-10-31 17:24:25 -05005497 /* Initialize guest FPU by resetting ours and saving into guest's */
5498 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005499 kvm_fx_save(&vcpu->arch.host_fx_image);
5500 kvm_fx_finit();
5501 kvm_fx_save(&vcpu->arch.guest_fx_image);
5502 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005503 preempt_enable();
5504
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005505 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005506 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005507 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5508 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005509 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5510}
5511EXPORT_SYMBOL_GPL(fx_init);
5512
5513void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5514{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005515 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005516 return;
5517
5518 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005519 kvm_fx_save(&vcpu->arch.host_fx_image);
5520 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Avi Kivity0c048512010-01-21 15:31:52 +02005521 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005522}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005523
5524void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5525{
5526 if (!vcpu->guest_fpu_loaded)
5527 return;
5528
5529 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005530 kvm_fx_save(&vcpu->arch.guest_fx_image);
5531 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005532 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005533 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Avi Kivity0c048512010-01-21 15:31:52 +02005534 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005535}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005536
5537void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5538{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005539 if (vcpu->arch.time_page) {
5540 kvm_release_page_dirty(vcpu->arch.time_page);
5541 vcpu->arch.time_page = NULL;
5542 }
5543
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005544 kvm_x86_ops->vcpu_free(vcpu);
5545}
5546
5547struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5548 unsigned int id)
5549{
Avi Kivity26e52152007-11-20 15:30:24 +02005550 return kvm_x86_ops->vcpu_create(kvm, id);
5551}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005552
Avi Kivity26e52152007-11-20 15:30:24 +02005553int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5554{
5555 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005556
5557 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005558 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005559
Sheng Yang0bed3b52008-10-09 16:01:54 +08005560 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005561 vcpu_load(vcpu);
5562 r = kvm_arch_vcpu_reset(vcpu);
5563 if (r == 0)
5564 r = kvm_mmu_setup(vcpu);
5565 vcpu_put(vcpu);
5566 if (r < 0)
5567 goto free_vcpu;
5568
Avi Kivity26e52152007-11-20 15:30:24 +02005569 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005570free_vcpu:
5571 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005572 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005573}
5574
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005575void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005576{
5577 vcpu_load(vcpu);
5578 kvm_mmu_unload(vcpu);
5579 vcpu_put(vcpu);
5580
5581 kvm_x86_ops->vcpu_free(vcpu);
5582}
5583
5584int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5585{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005586 vcpu->arch.nmi_pending = false;
5587 vcpu->arch.nmi_injected = false;
5588
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005589 vcpu->arch.switch_db_regs = 0;
5590 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5591 vcpu->arch.dr6 = DR6_FIXED_1;
5592 vcpu->arch.dr7 = DR7_FIXED_1;
5593
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005594 return kvm_x86_ops->vcpu_reset(vcpu);
5595}
5596
Alexander Graf10474ae2009-09-15 11:37:46 +02005597int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005598{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005599 /*
5600 * Since this may be called from a hotplug notifcation,
5601 * we can't get the CPU frequency directly.
5602 */
5603 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5604 int cpu = raw_smp_processor_id();
5605 per_cpu(cpu_tsc_khz, cpu) = 0;
5606 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005607
5608 kvm_shared_msr_cpu_online();
5609
Alexander Graf10474ae2009-09-15 11:37:46 +02005610 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005611}
5612
5613void kvm_arch_hardware_disable(void *garbage)
5614{
5615 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005616 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005617}
5618
5619int kvm_arch_hardware_setup(void)
5620{
5621 return kvm_x86_ops->hardware_setup();
5622}
5623
5624void kvm_arch_hardware_unsetup(void)
5625{
5626 kvm_x86_ops->hardware_unsetup();
5627}
5628
5629void kvm_arch_check_processor_compat(void *rtn)
5630{
5631 kvm_x86_ops->check_processor_compatibility(rtn);
5632}
5633
5634int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5635{
5636 struct page *page;
5637 struct kvm *kvm;
5638 int r;
5639
5640 BUG_ON(vcpu->kvm == NULL);
5641 kvm = vcpu->kvm;
5642
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005643 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005644 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005645 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005646 else
Avi Kivitya4535292008-04-13 17:54:35 +03005647 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005648
5649 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5650 if (!page) {
5651 r = -ENOMEM;
5652 goto fail;
5653 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005654 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005655
5656 r = kvm_mmu_create(vcpu);
5657 if (r < 0)
5658 goto fail_free_pio_data;
5659
5660 if (irqchip_in_kernel(kvm)) {
5661 r = kvm_create_lapic(vcpu);
5662 if (r < 0)
5663 goto fail_mmu_destroy;
5664 }
5665
Huang Ying890ca9a2009-05-11 16:48:15 +08005666 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5667 GFP_KERNEL);
5668 if (!vcpu->arch.mce_banks) {
5669 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005670 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005671 }
5672 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5673
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005674 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005675fail_free_lapic:
5676 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005677fail_mmu_destroy:
5678 kvm_mmu_destroy(vcpu);
5679fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005680 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005681fail:
5682 return r;
5683}
5684
5685void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5686{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005687 int idx;
5688
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005689 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005690 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005691 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005692 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005693 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005694 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005695}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005696
5697struct kvm *kvm_arch_create_vm(void)
5698{
5699 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5700
5701 if (!kvm)
5702 return ERR_PTR(-ENOMEM);
5703
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005704 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5705 if (!kvm->arch.aliases) {
5706 kfree(kvm);
5707 return ERR_PTR(-ENOMEM);
5708 }
5709
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005710 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005711 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005712
Sheng Yang5550af42008-10-15 20:15:06 +08005713 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5714 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5715
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005716 rdtscll(kvm->arch.vm_init_tsc);
5717
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005718 return kvm;
5719}
5720
5721static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5722{
5723 vcpu_load(vcpu);
5724 kvm_mmu_unload(vcpu);
5725 vcpu_put(vcpu);
5726}
5727
5728static void kvm_free_vcpus(struct kvm *kvm)
5729{
5730 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005731 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005732
5733 /*
5734 * Unpin any mmu pages first.
5735 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005736 kvm_for_each_vcpu(i, vcpu, kvm)
5737 kvm_unload_vcpu_mmu(vcpu);
5738 kvm_for_each_vcpu(i, vcpu, kvm)
5739 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005740
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005741 mutex_lock(&kvm->lock);
5742 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5743 kvm->vcpus[i] = NULL;
5744
5745 atomic_set(&kvm->online_vcpus, 0);
5746 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005747}
5748
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005749void kvm_arch_sync_events(struct kvm *kvm)
5750{
Sheng Yangba4cef32009-01-06 10:03:03 +08005751 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005752}
5753
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005754void kvm_arch_destroy_vm(struct kvm *kvm)
5755{
Sheng Yang6eb55812008-10-31 12:37:41 +08005756 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005757 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005758 kfree(kvm->arch.vpic);
5759 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005760 kvm_free_vcpus(kvm);
5761 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005762 if (kvm->arch.apic_access_page)
5763 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005764 if (kvm->arch.ept_identity_pagetable)
5765 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosatti64749202010-01-19 12:45:23 -02005766 cleanup_srcu_struct(&kvm->srcu);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005767 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005768 kfree(kvm);
5769}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005770
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005771int kvm_arch_prepare_memory_region(struct kvm *kvm,
5772 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005773 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005774 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005775 int user_alloc)
5776{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005777 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005778
5779 /*To keep backward compatibility with older userspace,
5780 *x86 needs to hanlde !user_alloc case.
5781 */
5782 if (!user_alloc) {
5783 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005784 unsigned long userspace_addr;
5785
Izik Eidus72dc67a2008-02-10 18:04:15 +02005786 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005787 userspace_addr = do_mmap(NULL, 0,
5788 npages * PAGE_SIZE,
5789 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005790 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005791 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005792 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005793
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005794 if (IS_ERR((void *)userspace_addr))
5795 return PTR_ERR((void *)userspace_addr);
5796
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005797 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005798 }
5799 }
5800
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005801
5802 return 0;
5803}
5804
5805void kvm_arch_commit_memory_region(struct kvm *kvm,
5806 struct kvm_userspace_memory_region *mem,
5807 struct kvm_memory_slot old,
5808 int user_alloc)
5809{
5810
5811 int npages = mem->memory_size >> PAGE_SHIFT;
5812
5813 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5814 int ret;
5815
5816 down_write(&current->mm->mmap_sem);
5817 ret = do_munmap(current->mm, old.userspace_addr,
5818 old.npages * PAGE_SIZE);
5819 up_write(&current->mm->mmap_sem);
5820 if (ret < 0)
5821 printk(KERN_WARNING
5822 "kvm_vm_ioctl_set_memory_region: "
5823 "failed to munmap memory\n");
5824 }
5825
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005826 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005827 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005828 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5829 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5830 }
5831
5832 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005833 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005834}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005835
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005836void kvm_arch_flush_shadow(struct kvm *kvm)
5837{
5838 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005839 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005840}
5841
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005842int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5843{
Avi Kivitya4535292008-04-13 17:54:35 +03005844 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005845 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5846 || vcpu->arch.nmi_pending ||
5847 (kvm_arch_interrupt_allowed(vcpu) &&
5848 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005849}
Zhang Xiantao57361992007-12-17 14:21:40 +08005850
Zhang Xiantao57361992007-12-17 14:21:40 +08005851void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5852{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005853 int me;
5854 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005855
5856 if (waitqueue_active(&vcpu->wq)) {
5857 wake_up_interruptible(&vcpu->wq);
5858 ++vcpu->stat.halt_wakeup;
5859 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005860
5861 me = get_cpu();
5862 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5863 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5864 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005865 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005866}
Gleb Natapov78646122009-03-23 12:12:11 +02005867
5868int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5869{
5870 return kvm_x86_ops->interrupt_allowed(vcpu);
5871}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005872
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005873bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
5874{
5875 unsigned long current_rip = kvm_rip_read(vcpu) +
5876 get_segment_base(vcpu, VCPU_SREG_CS);
5877
5878 return current_rip == linear_rip;
5879}
5880EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
5881
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005882unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5883{
5884 unsigned long rflags;
5885
5886 rflags = kvm_x86_ops->get_rflags(vcpu);
5887 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Jan Kiszkac310bac2010-02-23 17:47:58 +01005888 rflags &= ~X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005889 return rflags;
5890}
5891EXPORT_SYMBOL_GPL(kvm_get_rflags);
5892
5893void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5894{
5895 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
Jan Kiszkaf92653e2010-02-23 17:47:55 +01005896 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
Jan Kiszkac310bac2010-02-23 17:47:58 +01005897 rflags |= X86_EFLAGS_TF;
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005898 kvm_x86_ops->set_rflags(vcpu, rflags);
5899}
5900EXPORT_SYMBOL_GPL(kvm_set_rflags);
5901
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005902EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5903EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5904EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5905EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5906EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005907EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005908EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005909EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005910EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005911EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005912EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
Joerg Roedel2e554e82010-02-24 18:59:14 +01005913EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);