blob: 21b9b6aa3e88512667a41dfaffb20ce2ed201da7 [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010034#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080035#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050036#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010037#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030038#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#include <linux/cpufreq.h>
Avi Kivity18863bd2009-09-07 11:12:18 +030040#include <linux/user-return-notifier.h>
Marcelo Tosattia983fb22009-12-23 14:35:23 -020041#include <linux/srcu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080043#include <linux/perf_event.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030044#include <trace/events/kvm.h>
45#undef TRACE_INCLUDE_FILE
Marcelo Tosatti229456f2009-06-17 09:22:14 -030046#define CREATE_TRACE_POINTS
47#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020048
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020049#include <asm/debugreg.h>
Carsten Otte043405e2007-10-10 17:16:19 +020050#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080051#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020052#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080053#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080054#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020055
Carsten Otte313a3dc2007-10-11 19:16:52 +020056#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010057#define CR0_RESERVED_BITS \
58 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
59 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
60 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
61#define CR4_RESERVED_BITS \
62 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
63 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
64 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
65 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
66
67#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080068
69#define KVM_MAX_MCE_BANKS 32
70#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
71
Joerg Roedel50a37eb2008-01-31 14:57:38 +010072/* EFER defaults:
73 * - enable syscall per default because its emulated by KVM
74 * - enable LME and LMA per default on 64 bit KVM
75 */
76#ifdef CONFIG_X86_64
77static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
78#else
79static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
80#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020081
Avi Kivityba1389b2007-11-18 16:24:12 +020082#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
83#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050084
Gleb Natapovcb142eb2009-08-09 15:17:40 +030085static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020086static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
87 struct kvm_cpuid_entry2 __user *entries);
88
Zhang Xiantao97896d02007-11-14 20:09:30 +080089struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030090EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080091
Andre Przywaraed85c062009-06-25 12:36:49 +020092int ignore_msrs = 0;
93module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
94
Avi Kivity18863bd2009-09-07 11:12:18 +030095#define KVM_NR_SHARED_MSRS 16
96
97struct kvm_shared_msrs_global {
98 int nr;
Sheng Yang2bf78fa2009-12-18 16:48:44 +080099 u32 msrs[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300100};
101
102struct kvm_shared_msrs {
103 struct user_return_notifier urn;
104 bool registered;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800105 struct kvm_shared_msr_values {
106 u64 host;
107 u64 curr;
108 } values[KVM_NR_SHARED_MSRS];
Avi Kivity18863bd2009-09-07 11:12:18 +0300109};
110
111static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
112static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
113
Hollis Blanchard417bc302007-10-31 17:24:23 -0500114struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200115 { "pf_fixed", VCPU_STAT(pf_fixed) },
116 { "pf_guest", VCPU_STAT(pf_guest) },
117 { "tlb_flush", VCPU_STAT(tlb_flush) },
118 { "invlpg", VCPU_STAT(invlpg) },
119 { "exits", VCPU_STAT(exits) },
120 { "io_exits", VCPU_STAT(io_exits) },
121 { "mmio_exits", VCPU_STAT(mmio_exits) },
122 { "signal_exits", VCPU_STAT(signal_exits) },
123 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800124 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200125 { "halt_exits", VCPU_STAT(halt_exits) },
126 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530127 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200128 { "request_irq", VCPU_STAT(request_irq_exits) },
129 { "irq_exits", VCPU_STAT(irq_exits) },
130 { "host_state_reload", VCPU_STAT(host_state_reload) },
131 { "efer_reload", VCPU_STAT(efer_reload) },
132 { "fpu_reload", VCPU_STAT(fpu_reload) },
133 { "insn_emulation", VCPU_STAT(insn_emulation) },
134 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300135 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200136 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200137 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
138 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
139 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
140 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
141 { "mmu_flooded", VM_STAT(mmu_flooded) },
142 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200143 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300144 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200145 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300146 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500147 { NULL }
148};
149
Avi Kivity18863bd2009-09-07 11:12:18 +0300150static void kvm_on_user_return(struct user_return_notifier *urn)
151{
152 unsigned slot;
Avi Kivity18863bd2009-09-07 11:12:18 +0300153 struct kvm_shared_msrs *locals
154 = container_of(urn, struct kvm_shared_msrs, urn);
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800155 struct kvm_shared_msr_values *values;
Avi Kivity18863bd2009-09-07 11:12:18 +0300156
157 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800158 values = &locals->values[slot];
159 if (values->host != values->curr) {
160 wrmsrl(shared_msrs_global.msrs[slot], values->host);
161 values->curr = values->host;
Avi Kivity18863bd2009-09-07 11:12:18 +0300162 }
163 }
164 locals->registered = false;
165 user_return_notifier_unregister(urn);
166}
167
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800168static void shared_msr_update(unsigned slot, u32 msr)
Avi Kivity18863bd2009-09-07 11:12:18 +0300169{
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800170 struct kvm_shared_msrs *smsr;
Avi Kivity18863bd2009-09-07 11:12:18 +0300171 u64 value;
172
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800173 smsr = &__get_cpu_var(shared_msrs);
174 /* only read, and nobody should modify it at this time,
175 * so don't need lock */
176 if (slot >= shared_msrs_global.nr) {
177 printk(KERN_ERR "kvm: invalid MSR slot!");
178 return;
179 }
180 rdmsrl_safe(msr, &value);
181 smsr->values[slot].host = value;
182 smsr->values[slot].curr = value;
183}
184
185void kvm_define_shared_msr(unsigned slot, u32 msr)
186{
Avi Kivity18863bd2009-09-07 11:12:18 +0300187 if (slot >= shared_msrs_global.nr)
188 shared_msrs_global.nr = slot + 1;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800189 shared_msrs_global.msrs[slot] = msr;
190 /* we need ensured the shared_msr_global have been updated */
191 smp_wmb();
Avi Kivity18863bd2009-09-07 11:12:18 +0300192}
193EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
194
195static void kvm_shared_msr_cpu_online(void)
196{
197 unsigned i;
Avi Kivity18863bd2009-09-07 11:12:18 +0300198
199 for (i = 0; i < shared_msrs_global.nr; ++i)
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800200 shared_msr_update(i, shared_msrs_global.msrs[i]);
Avi Kivity18863bd2009-09-07 11:12:18 +0300201}
202
Avi Kivityd5696722009-12-02 12:28:47 +0200203void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300204{
205 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
206
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800207 if (((value ^ smsr->values[slot].curr) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300208 return;
Sheng Yang2bf78fa2009-12-18 16:48:44 +0800209 smsr->values[slot].curr = value;
210 wrmsrl(shared_msrs_global.msrs[slot], value);
Avi Kivity18863bd2009-09-07 11:12:18 +0300211 if (!smsr->registered) {
212 smsr->urn.on_user_return = kvm_on_user_return;
213 user_return_notifier_register(&smsr->urn);
214 smsr->registered = true;
215 }
216}
217EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
218
Avi Kivity3548bab2009-11-28 14:18:47 +0200219static void drop_user_return_notifiers(void *ignore)
220{
221 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
222
223 if (smsr->registered)
224 kvm_on_user_return(&smsr->urn);
225}
226
Carsten Otte5fb76f92007-10-29 16:08:51 +0100227unsigned long segment_base(u16 selector)
228{
229 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200230 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100231 unsigned long table_base;
232 unsigned long v;
233
234 if (selector == 0)
235 return 0;
236
Akinobu Mitab792c342009-07-19 00:00:01 +0900237 kvm_get_gdt(&gdt);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100238 table_base = gdt.base;
239
240 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900241 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100242
Carsten Otte5fb76f92007-10-29 16:08:51 +0100243 table_base = segment_base(ldt_selector);
244 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200245 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900246 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100247#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200248 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
249 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100250#endif
251 return v;
252}
253EXPORT_SYMBOL_GPL(segment_base);
254
Carsten Otte6866b832007-10-29 16:09:10 +0100255u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
256{
257 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800258 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100259 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800260 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100261}
262EXPORT_SYMBOL_GPL(kvm_get_apic_base);
263
264void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
265{
266 /* TODO: reserve bits check */
267 if (irqchip_in_kernel(vcpu->kvm))
268 kvm_lapic_set_base(vcpu, data);
269 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800270 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100271}
272EXPORT_SYMBOL_GPL(kvm_set_apic_base);
273
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200274#define EXCPT_BENIGN 0
275#define EXCPT_CONTRIBUTORY 1
276#define EXCPT_PF 2
277
278static int exception_class(int vector)
279{
280 switch (vector) {
281 case PF_VECTOR:
282 return EXCPT_PF;
283 case DE_VECTOR:
284 case TS_VECTOR:
285 case NP_VECTOR:
286 case SS_VECTOR:
287 case GP_VECTOR:
288 return EXCPT_CONTRIBUTORY;
289 default:
290 break;
291 }
292 return EXCPT_BENIGN;
293}
294
295static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
296 unsigned nr, bool has_error, u32 error_code)
297{
298 u32 prev_nr;
299 int class1, class2;
300
301 if (!vcpu->arch.exception.pending) {
302 queue:
303 vcpu->arch.exception.pending = true;
304 vcpu->arch.exception.has_error_code = has_error;
305 vcpu->arch.exception.nr = nr;
306 vcpu->arch.exception.error_code = error_code;
307 return;
308 }
309
310 /* to check exception */
311 prev_nr = vcpu->arch.exception.nr;
312 if (prev_nr == DF_VECTOR) {
313 /* triple fault -> shutdown */
314 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
315 return;
316 }
317 class1 = exception_class(prev_nr);
318 class2 = exception_class(nr);
319 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
320 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
321 /* generate double fault per SDM Table 5-5 */
322 vcpu->arch.exception.pending = true;
323 vcpu->arch.exception.has_error_code = true;
324 vcpu->arch.exception.nr = DF_VECTOR;
325 vcpu->arch.exception.error_code = 0;
326 } else
327 /* replace previous exception with a new one in a hope
328 that instruction re-execution will regenerate lost
329 exception */
330 goto queue;
331}
332
Avi Kivity298101d2007-11-25 13:41:11 +0200333void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
334{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200335 kvm_multiple_exception(vcpu, nr, false, 0);
Avi Kivity298101d2007-11-25 13:41:11 +0200336}
337EXPORT_SYMBOL_GPL(kvm_queue_exception);
338
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200339void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
340 u32 error_code)
341{
342 ++vcpu->stat.pf_guest;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200344 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
345}
346
Sheng Yang3419ffc2008-05-15 09:52:48 +0800347void kvm_inject_nmi(struct kvm_vcpu *vcpu)
348{
349 vcpu->arch.nmi_pending = 1;
350}
351EXPORT_SYMBOL_GPL(kvm_inject_nmi);
352
Avi Kivity298101d2007-11-25 13:41:11 +0200353void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
354{
Eddie Dong3fd28fc2009-11-19 17:54:07 +0200355 kvm_multiple_exception(vcpu, nr, true, error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200356}
357EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
358
Carsten Ottea03490e2007-10-29 16:09:35 +0100359/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300360 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
361 * a #GP and return false.
362 */
363bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200364{
Avi Kivity0a79b002009-09-01 12:03:25 +0300365 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
366 return true;
367 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
368 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100369}
Avi Kivity0a79b002009-09-01 12:03:25 +0300370EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100371
372/*
373 * Load the pae pdptrs. Return true is they are all valid.
374 */
375int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
376{
377 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
378 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
379 int i;
380 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800381 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100382
Carsten Ottea03490e2007-10-29 16:09:35 +0100383 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
384 offset * sizeof(u64), sizeof(pdpte));
385 if (ret < 0) {
386 ret = 0;
387 goto out;
388 }
389 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300390 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800391 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100392 ret = 0;
393 goto out;
394 }
395 }
396 ret = 1;
397
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800398 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300399 __set_bit(VCPU_EXREG_PDPTR,
400 (unsigned long *)&vcpu->arch.regs_avail);
401 __set_bit(VCPU_EXREG_PDPTR,
402 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100403out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100404
405 return ret;
406}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100407EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100408
Avi Kivityd835dfe2007-11-21 02:57:59 +0200409static bool pdptrs_changed(struct kvm_vcpu *vcpu)
410{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800411 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200412 bool changed = true;
413 int r;
414
415 if (is_long_mode(vcpu) || !is_pae(vcpu))
416 return false;
417
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300418 if (!test_bit(VCPU_EXREG_PDPTR,
419 (unsigned long *)&vcpu->arch.regs_avail))
420 return true;
421
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800422 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200423 if (r < 0)
424 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800425 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200426out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200427
428 return changed;
429}
430
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200431void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100432{
Avi Kivityf9a48e62010-01-06 19:10:22 +0200433 cr0 |= X86_CR0_ET;
434
Gleb Natapovab344822010-01-21 15:28:46 +0200435#ifdef CONFIG_X86_64
436 if (cr0 & 0xffffffff00000000UL) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100437 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Avi Kivity4d4ec082009-12-29 18:07:30 +0200438 cr0, kvm_read_cr0(vcpu));
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200439 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100440 return;
441 }
Gleb Natapovab344822010-01-21 15:28:46 +0200442#endif
443
444 cr0 &= ~CR0_RESERVED_BITS;
Carsten Ottea03490e2007-10-29 16:09:35 +0100445
446 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
447 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200448 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100449 return;
450 }
451
452 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
453 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
454 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200455 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100456 return;
457 }
458
459 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
460#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200461 if ((vcpu->arch.efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100462 int cs_db, cs_l;
463
464 if (!is_pae(vcpu)) {
465 printk(KERN_DEBUG "set_cr0: #GP, start paging "
466 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200467 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100468 return;
469 }
470 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
471 if (cs_l) {
472 printk(KERN_DEBUG "set_cr0: #GP, start paging "
473 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200474 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100475 return;
476
477 }
478 } else
479#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800480 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100481 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
482 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200483 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100484 return;
485 }
486
487 }
488
489 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800490 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100491
Carsten Ottea03490e2007-10-29 16:09:35 +0100492 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100493 return;
494}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200495EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100496
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200497void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100498{
Avi Kivity4d4ec082009-12-29 18:07:30 +0200499 kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100500}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200501EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100502
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200503void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100504{
Avi Kivityfc78f512009-12-07 12:16:48 +0200505 unsigned long old_cr4 = kvm_read_cr4(vcpu);
Avi Kivitya2edf572009-05-24 22:19:00 +0300506 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
507
Carsten Ottea03490e2007-10-29 16:09:35 +0100508 if (cr4 & CR4_RESERVED_BITS) {
509 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200510 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100511 return;
512 }
513
514 if (is_long_mode(vcpu)) {
515 if (!(cr4 & X86_CR4_PAE)) {
516 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
517 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200518 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100519 return;
520 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300521 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
522 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800523 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100524 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200525 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100526 return;
527 }
528
529 if (cr4 & X86_CR4_VMXE) {
530 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200531 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100532 return;
533 }
534 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800535 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200536 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100537 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100538}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200539EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100540
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200541void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100542{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800543 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300544 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200545 kvm_mmu_flush_tlb(vcpu);
546 return;
547 }
548
Carsten Ottea03490e2007-10-29 16:09:35 +0100549 if (is_long_mode(vcpu)) {
550 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
551 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200552 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100553 return;
554 }
555 } else {
556 if (is_pae(vcpu)) {
557 if (cr3 & CR3_PAE_RESERVED_BITS) {
558 printk(KERN_DEBUG
559 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200560 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100561 return;
562 }
563 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
564 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
565 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200566 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100567 return;
568 }
569 }
570 /*
571 * We don't check reserved bits in nonpae mode, because
572 * this isn't enforced, and VMware depends on this.
573 */
574 }
575
Carsten Ottea03490e2007-10-29 16:09:35 +0100576 /*
577 * Does the new cr3 value map to physical memory? (Note, we
578 * catch an invalid cr3 even in real-mode, because it would
579 * cause trouble later on when we turn on paging anyway.)
580 *
581 * A real CPU would silently accept an invalid cr3 and would
582 * attempt to use it - with largely undefined (and often hard
583 * to debug) behavior on the guest side.
584 */
585 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200586 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100587 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800588 vcpu->arch.cr3 = cr3;
589 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100590 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100591}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200592EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100593
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200594void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100595{
596 if (cr8 & CR8_RESERVED_BITS) {
597 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200598 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100599 return;
600 }
601 if (irqchip_in_kernel(vcpu->kvm))
602 kvm_lapic_set_tpr(vcpu, cr8);
603 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800604 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100605}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200606EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100607
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200608unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100609{
610 if (irqchip_in_kernel(vcpu->kvm))
611 return kvm_lapic_get_cr8(vcpu);
612 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800613 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100614}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200615EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100616
Alexander Grafd8017472008-11-25 20:17:11 +0100617static inline u32 bit(int bitno)
618{
619 return 1 << (bitno & 31);
620}
621
Carsten Otte043405e2007-10-10 17:16:19 +0200622/*
623 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
624 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
625 *
626 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400627 * capabilities of the host cpu. This capabilities test skips MSRs that are
628 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200629 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400630
Gleb Natapov10388a02010-01-17 15:51:23 +0200631#define KVM_SAVE_MSRS_BEGIN 5
Carsten Otte043405e2007-10-10 17:16:19 +0200632static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400633 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Gleb Natapov55cd8e52010-01-17 15:51:22 +0200634 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
Gleb Natapov10388a02010-01-17 15:51:23 +0200635 HV_X64_MSR_APIC_ASSIST_PAGE,
Carsten Otte043405e2007-10-10 17:16:19 +0200636 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
637 MSR_K6_STAR,
638#ifdef CONFIG_X86_64
639 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
640#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400641 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200642};
643
644static unsigned num_msrs_to_save;
645
646static u32 emulated_msrs[] = {
647 MSR_IA32_MISC_ENABLE,
648};
649
Carsten Otte15c4a642007-10-30 18:44:17 +0100650static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
651{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100652 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100653 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
654 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200655 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100656 return;
657 }
658
659 if (is_paging(vcpu)
Avi Kivityf6801df2010-01-21 15:31:50 +0200660 && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100661 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200662 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100663 return;
664 }
665
Alexander Graf1b2fd702009-02-02 16:23:51 +0100666 if (efer & EFER_FFXSR) {
667 struct kvm_cpuid_entry2 *feat;
668
669 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
670 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
671 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
672 kvm_inject_gp(vcpu, 0);
673 return;
674 }
675 }
676
Alexander Grafd8017472008-11-25 20:17:11 +0100677 if (efer & EFER_SVME) {
678 struct kvm_cpuid_entry2 *feat;
679
680 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
681 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
682 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
683 kvm_inject_gp(vcpu, 0);
684 return;
685 }
686 }
687
Carsten Otte15c4a642007-10-30 18:44:17 +0100688 kvm_x86_ops->set_efer(vcpu, efer);
689
690 efer &= ~EFER_LMA;
Avi Kivityf6801df2010-01-21 15:31:50 +0200691 efer |= vcpu->arch.efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100692
Avi Kivityf6801df2010-01-21 15:31:50 +0200693 vcpu->arch.efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300694
695 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
696 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100697}
698
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100699void kvm_enable_efer_bits(u64 mask)
700{
701 efer_reserved_bits &= ~mask;
702}
703EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
704
705
Carsten Otte15c4a642007-10-30 18:44:17 +0100706/*
707 * Writes msr value into into the appropriate "register".
708 * Returns 0 on success, non-0 otherwise.
709 * Assumes vcpu_load() was already called.
710 */
711int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
712{
713 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
714}
715
Carsten Otte313a3dc2007-10-11 19:16:52 +0200716/*
717 * Adapt set_msr() to msr_io()'s calling convention
718 */
719static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
720{
721 return kvm_set_msr(vcpu, index, *data);
722}
723
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200724static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
725{
726 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200727 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800728 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200729
730 if (!wall_clock)
731 return;
732
733 version++;
734
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200735 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
736
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200737 /*
738 * The guest calculates current wall clock time by adding
739 * system time (updated by kvm_write_guest_time below) to the
740 * wall clock specified here. guest system time equals host
741 * system time for us, thus we must fill in host boot time here.
742 */
Jason Wang923de3c2010-01-27 19:13:49 +0800743 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200744
745 wc.sec = boot.tv_sec;
746 wc.nsec = boot.tv_nsec;
747 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200748
749 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
750
751 version++;
752 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200753}
754
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200755static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
756{
757 uint32_t quotient, remainder;
758
759 /* Don't try to replace with do_div(), this one calculates
760 * "(dividend << 32) / divisor" */
761 __asm__ ( "divl %4"
762 : "=a" (quotient), "=d" (remainder)
763 : "0" (0), "1" (dividend), "r" (divisor) );
764 return quotient;
765}
766
767static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
768{
769 uint64_t nsecs = 1000000000LL;
770 int32_t shift = 0;
771 uint64_t tps64;
772 uint32_t tps32;
773
774 tps64 = tsc_khz * 1000LL;
775 while (tps64 > nsecs*2) {
776 tps64 >>= 1;
777 shift--;
778 }
779
780 tps32 = (uint32_t)tps64;
781 while (tps32 <= (uint32_t)nsecs) {
782 tps32 <<= 1;
783 shift++;
784 }
785
786 hv_clock->tsc_shift = shift;
787 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
788
789 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700790 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200791 hv_clock->tsc_to_system_mul);
792}
793
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100794static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
795
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200796static void kvm_write_guest_time(struct kvm_vcpu *v)
797{
798 struct timespec ts;
799 unsigned long flags;
800 struct kvm_vcpu_arch *vcpu = &v->arch;
801 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300802 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200803
804 if ((!vcpu->time_page))
805 return;
806
Avi Kivity463656c2009-04-12 15:49:07 +0300807 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
808 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
809 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
810 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200811 }
Avi Kivity463656c2009-04-12 15:49:07 +0300812 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200813
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200814 /* Keep irq disabled to prevent changes to the clock */
815 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530816 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200817 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800818 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200819 local_irq_restore(flags);
820
821 /* With all the info we got, fill in the values */
822
823 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400824 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
825
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200826 /*
827 * The interface expects us to write an even number signaling that the
828 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200829 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200830 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200831 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200832
833 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
834
835 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200836 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200837
838 kunmap_atomic(shared_kaddr, KM_USER0);
839
840 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
841}
842
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100843static int kvm_request_guest_time_update(struct kvm_vcpu *v)
844{
845 struct kvm_vcpu_arch *vcpu = &v->arch;
846
847 if (!vcpu->time_page)
848 return 0;
849 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
850 return 1;
851}
852
Avi Kivity9ba075a2008-05-26 20:06:35 +0300853static bool msr_mtrr_valid(unsigned msr)
854{
855 switch (msr) {
856 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
857 case MSR_MTRRfix64K_00000:
858 case MSR_MTRRfix16K_80000:
859 case MSR_MTRRfix16K_A0000:
860 case MSR_MTRRfix4K_C0000:
861 case MSR_MTRRfix4K_C8000:
862 case MSR_MTRRfix4K_D0000:
863 case MSR_MTRRfix4K_D8000:
864 case MSR_MTRRfix4K_E0000:
865 case MSR_MTRRfix4K_E8000:
866 case MSR_MTRRfix4K_F0000:
867 case MSR_MTRRfix4K_F8000:
868 case MSR_MTRRdefType:
869 case MSR_IA32_CR_PAT:
870 return true;
871 case 0x2f8:
872 return true;
873 }
874 return false;
875}
876
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300877static bool valid_pat_type(unsigned t)
878{
879 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
880}
881
882static bool valid_mtrr_type(unsigned t)
883{
884 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
885}
886
887static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
888{
889 int i;
890
891 if (!msr_mtrr_valid(msr))
892 return false;
893
894 if (msr == MSR_IA32_CR_PAT) {
895 for (i = 0; i < 8; i++)
896 if (!valid_pat_type((data >> (i * 8)) & 0xff))
897 return false;
898 return true;
899 } else if (msr == MSR_MTRRdefType) {
900 if (data & ~0xcff)
901 return false;
902 return valid_mtrr_type(data & 0xff);
903 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
904 for (i = 0; i < 8 ; i++)
905 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
906 return false;
907 return true;
908 }
909
910 /* variable MTRRs */
911 return valid_mtrr_type(data & 0xff);
912}
913
Avi Kivity9ba075a2008-05-26 20:06:35 +0300914static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
915{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800916 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
917
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300918 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300919 return 1;
920
Sheng Yang0bed3b52008-10-09 16:01:54 +0800921 if (msr == MSR_MTRRdefType) {
922 vcpu->arch.mtrr_state.def_type = data;
923 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
924 } else if (msr == MSR_MTRRfix64K_00000)
925 p[0] = data;
926 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
927 p[1 + msr - MSR_MTRRfix16K_80000] = data;
928 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
929 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
930 else if (msr == MSR_IA32_CR_PAT)
931 vcpu->arch.pat = data;
932 else { /* Variable MTRRs */
933 int idx, is_mtrr_mask;
934 u64 *pt;
935
936 idx = (msr - 0x200) / 2;
937 is_mtrr_mask = msr - 0x200 - 2 * idx;
938 if (!is_mtrr_mask)
939 pt =
940 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
941 else
942 pt =
943 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
944 *pt = data;
945 }
946
947 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300948 return 0;
949}
Carsten Otte15c4a642007-10-30 18:44:17 +0100950
Huang Ying890ca9a2009-05-11 16:48:15 +0800951static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
952{
953 u64 mcg_cap = vcpu->arch.mcg_cap;
954 unsigned bank_num = mcg_cap & 0xff;
955
956 switch (msr) {
957 case MSR_IA32_MCG_STATUS:
958 vcpu->arch.mcg_status = data;
959 break;
960 case MSR_IA32_MCG_CTL:
961 if (!(mcg_cap & MCG_CTL_P))
962 return 1;
963 if (data != 0 && data != ~(u64)0)
964 return -1;
965 vcpu->arch.mcg_ctl = data;
966 break;
967 default:
968 if (msr >= MSR_IA32_MC0_CTL &&
969 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
970 u32 offset = msr - MSR_IA32_MC0_CTL;
971 /* only 0 or all 1s can be written to IA32_MCi_CTL */
972 if ((offset & 0x3) == 0 &&
973 data != 0 && data != ~(u64)0)
974 return -1;
975 vcpu->arch.mce_banks[offset] = data;
976 break;
977 }
978 return 1;
979 }
980 return 0;
981}
982
Ed Swierkffde22a2009-10-15 15:21:43 -0700983static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
984{
985 struct kvm *kvm = vcpu->kvm;
986 int lm = is_long_mode(vcpu);
987 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
988 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
989 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
990 : kvm->arch.xen_hvm_config.blob_size_32;
991 u32 page_num = data & ~PAGE_MASK;
992 u64 page_addr = data & PAGE_MASK;
993 u8 *page;
994 int r;
995
996 r = -E2BIG;
997 if (page_num >= blob_size)
998 goto out;
999 r = -ENOMEM;
1000 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1001 if (!page)
1002 goto out;
1003 r = -EFAULT;
1004 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
1005 goto out_free;
1006 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1007 goto out_free;
1008 r = 0;
1009out_free:
1010 kfree(page);
1011out:
1012 return r;
1013}
1014
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001015static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1016{
1017 return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1018}
1019
1020static bool kvm_hv_msr_partition_wide(u32 msr)
1021{
1022 bool r = false;
1023 switch (msr) {
1024 case HV_X64_MSR_GUEST_OS_ID:
1025 case HV_X64_MSR_HYPERCALL:
1026 r = true;
1027 break;
1028 }
1029
1030 return r;
1031}
1032
1033static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1034{
1035 struct kvm *kvm = vcpu->kvm;
1036
1037 switch (msr) {
1038 case HV_X64_MSR_GUEST_OS_ID:
1039 kvm->arch.hv_guest_os_id = data;
1040 /* setting guest os id to zero disables hypercall page */
1041 if (!kvm->arch.hv_guest_os_id)
1042 kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1043 break;
1044 case HV_X64_MSR_HYPERCALL: {
1045 u64 gfn;
1046 unsigned long addr;
1047 u8 instructions[4];
1048
1049 /* if guest os id is not set hypercall should remain disabled */
1050 if (!kvm->arch.hv_guest_os_id)
1051 break;
1052 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1053 kvm->arch.hv_hypercall = data;
1054 break;
1055 }
1056 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1057 addr = gfn_to_hva(kvm, gfn);
1058 if (kvm_is_error_hva(addr))
1059 return 1;
1060 kvm_x86_ops->patch_hypercall(vcpu, instructions);
1061 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1062 if (copy_to_user((void __user *)addr, instructions, 4))
1063 return 1;
1064 kvm->arch.hv_hypercall = data;
1065 break;
1066 }
1067 default:
1068 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1069 "data 0x%llx\n", msr, data);
1070 return 1;
1071 }
1072 return 0;
1073}
1074
1075static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1076{
Gleb Natapov10388a02010-01-17 15:51:23 +02001077 switch (msr) {
1078 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1079 unsigned long addr;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001080
Gleb Natapov10388a02010-01-17 15:51:23 +02001081 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1082 vcpu->arch.hv_vapic = data;
1083 break;
1084 }
1085 addr = gfn_to_hva(vcpu->kvm, data >>
1086 HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
1087 if (kvm_is_error_hva(addr))
1088 return 1;
1089 if (clear_user((void __user *)addr, PAGE_SIZE))
1090 return 1;
1091 vcpu->arch.hv_vapic = data;
1092 break;
1093 }
1094 case HV_X64_MSR_EOI:
1095 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1096 case HV_X64_MSR_ICR:
1097 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1098 case HV_X64_MSR_TPR:
1099 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1100 default:
1101 pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
1102 "data 0x%llx\n", msr, data);
1103 return 1;
1104 }
1105
1106 return 0;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001107}
1108
Carsten Otte15c4a642007-10-30 18:44:17 +01001109int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1110{
1111 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001112 case MSR_EFER:
1113 set_efer(vcpu, data);
1114 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +02001115 case MSR_K7_HWCR:
1116 data &= ~(u64)0x40; /* ignore flush filter disable */
1117 if (data != 0) {
1118 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1119 data);
1120 return 1;
1121 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001122 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001123 case MSR_FAM10H_MMIO_CONF_BASE:
1124 if (data != 0) {
1125 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1126 "0x%llx\n", data);
1127 return 1;
1128 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001129 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +02001130 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +01001131 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001132 case MSR_IA32_DEBUGCTLMSR:
1133 if (!data) {
1134 /* We support the non-activated case already */
1135 break;
1136 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1137 /* Values other than LBR and BTF are vendor-specific,
1138 thus reserved and should throw a #GP */
1139 return 1;
1140 }
1141 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1142 __func__, data);
1143 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001144 case MSR_IA32_UCODE_REV:
1145 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001146 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +02001147 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +01001148 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001149 case 0x200 ... 0x2ff:
1150 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001151 case MSR_IA32_APICBASE:
1152 kvm_set_apic_base(vcpu, data);
1153 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001154 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1155 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001156 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001157 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001158 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001159 case MSR_KVM_WALL_CLOCK:
1160 vcpu->kvm->arch.wall_clock = data;
1161 kvm_write_wall_clock(vcpu->kvm, data);
1162 break;
1163 case MSR_KVM_SYSTEM_TIME: {
1164 if (vcpu->arch.time_page) {
1165 kvm_release_page_dirty(vcpu->arch.time_page);
1166 vcpu->arch.time_page = NULL;
1167 }
1168
1169 vcpu->arch.time = data;
1170
1171 /* we verify if the enable bit is set... */
1172 if (!(data & 1))
1173 break;
1174
1175 /* ...but clean it before doing the actual write */
1176 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1177
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001178 vcpu->arch.time_page =
1179 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001180
1181 if (is_error_page(vcpu->arch.time_page)) {
1182 kvm_release_page_clean(vcpu->arch.time_page);
1183 vcpu->arch.time_page = NULL;
1184 }
1185
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001186 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001187 break;
1188 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001189 case MSR_IA32_MCG_CTL:
1190 case MSR_IA32_MCG_STATUS:
1191 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1192 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001193
1194 /* Performance counters are not protected by a CPUID bit,
1195 * so we should check all of them in the generic path for the sake of
1196 * cross vendor migration.
1197 * Writing a zero into the event select MSRs disables them,
1198 * which we perfectly emulate ;-). Any other value should be at least
1199 * reported, some guests depend on them.
1200 */
1201 case MSR_P6_EVNTSEL0:
1202 case MSR_P6_EVNTSEL1:
1203 case MSR_K7_EVNTSEL0:
1204 case MSR_K7_EVNTSEL1:
1205 case MSR_K7_EVNTSEL2:
1206 case MSR_K7_EVNTSEL3:
1207 if (data != 0)
1208 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1209 "0x%x data 0x%llx\n", msr, data);
1210 break;
1211 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1212 * so we ignore writes to make it happy.
1213 */
1214 case MSR_P6_PERFCTR0:
1215 case MSR_P6_PERFCTR1:
1216 case MSR_K7_PERFCTR0:
1217 case MSR_K7_PERFCTR1:
1218 case MSR_K7_PERFCTR2:
1219 case MSR_K7_PERFCTR3:
1220 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1221 "0x%x data 0x%llx\n", msr, data);
1222 break;
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001223 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1224 if (kvm_hv_msr_partition_wide(msr)) {
1225 int r;
1226 mutex_lock(&vcpu->kvm->lock);
1227 r = set_msr_hyperv_pw(vcpu, msr, data);
1228 mutex_unlock(&vcpu->kvm->lock);
1229 return r;
1230 } else
1231 return set_msr_hyperv(vcpu, msr, data);
1232 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001233 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001234 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1235 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001236 if (!ignore_msrs) {
1237 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1238 msr, data);
1239 return 1;
1240 } else {
1241 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1242 msr, data);
1243 break;
1244 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001245 }
1246 return 0;
1247}
1248EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1249
1250
1251/*
1252 * Reads an msr value (of 'msr_index') into 'pdata'.
1253 * Returns 0 on success, non-0 otherwise.
1254 * Assumes vcpu_load() was already called.
1255 */
1256int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1257{
1258 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1259}
1260
Avi Kivity9ba075a2008-05-26 20:06:35 +03001261static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1262{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001263 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1264
Avi Kivity9ba075a2008-05-26 20:06:35 +03001265 if (!msr_mtrr_valid(msr))
1266 return 1;
1267
Sheng Yang0bed3b52008-10-09 16:01:54 +08001268 if (msr == MSR_MTRRdefType)
1269 *pdata = vcpu->arch.mtrr_state.def_type +
1270 (vcpu->arch.mtrr_state.enabled << 10);
1271 else if (msr == MSR_MTRRfix64K_00000)
1272 *pdata = p[0];
1273 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1274 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1275 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1276 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1277 else if (msr == MSR_IA32_CR_PAT)
1278 *pdata = vcpu->arch.pat;
1279 else { /* Variable MTRRs */
1280 int idx, is_mtrr_mask;
1281 u64 *pt;
1282
1283 idx = (msr - 0x200) / 2;
1284 is_mtrr_mask = msr - 0x200 - 2 * idx;
1285 if (!is_mtrr_mask)
1286 pt =
1287 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1288 else
1289 pt =
1290 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1291 *pdata = *pt;
1292 }
1293
Avi Kivity9ba075a2008-05-26 20:06:35 +03001294 return 0;
1295}
1296
Huang Ying890ca9a2009-05-11 16:48:15 +08001297static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1298{
1299 u64 data;
1300 u64 mcg_cap = vcpu->arch.mcg_cap;
1301 unsigned bank_num = mcg_cap & 0xff;
1302
1303 switch (msr) {
1304 case MSR_IA32_P5_MC_ADDR:
1305 case MSR_IA32_P5_MC_TYPE:
1306 data = 0;
1307 break;
1308 case MSR_IA32_MCG_CAP:
1309 data = vcpu->arch.mcg_cap;
1310 break;
1311 case MSR_IA32_MCG_CTL:
1312 if (!(mcg_cap & MCG_CTL_P))
1313 return 1;
1314 data = vcpu->arch.mcg_ctl;
1315 break;
1316 case MSR_IA32_MCG_STATUS:
1317 data = vcpu->arch.mcg_status;
1318 break;
1319 default:
1320 if (msr >= MSR_IA32_MC0_CTL &&
1321 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1322 u32 offset = msr - MSR_IA32_MC0_CTL;
1323 data = vcpu->arch.mce_banks[offset];
1324 break;
1325 }
1326 return 1;
1327 }
1328 *pdata = data;
1329 return 0;
1330}
1331
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001332static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1333{
1334 u64 data = 0;
1335 struct kvm *kvm = vcpu->kvm;
1336
1337 switch (msr) {
1338 case HV_X64_MSR_GUEST_OS_ID:
1339 data = kvm->arch.hv_guest_os_id;
1340 break;
1341 case HV_X64_MSR_HYPERCALL:
1342 data = kvm->arch.hv_hypercall;
1343 break;
1344 default:
1345 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1346 return 1;
1347 }
1348
1349 *pdata = data;
1350 return 0;
1351}
1352
1353static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1354{
1355 u64 data = 0;
1356
1357 switch (msr) {
1358 case HV_X64_MSR_VP_INDEX: {
1359 int r;
1360 struct kvm_vcpu *v;
1361 kvm_for_each_vcpu(r, v, vcpu->kvm)
1362 if (v == vcpu)
1363 data = r;
1364 break;
1365 }
Gleb Natapov10388a02010-01-17 15:51:23 +02001366 case HV_X64_MSR_EOI:
1367 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1368 case HV_X64_MSR_ICR:
1369 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1370 case HV_X64_MSR_TPR:
1371 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001372 default:
1373 pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1374 return 1;
1375 }
1376 *pdata = data;
1377 return 0;
1378}
1379
Carsten Otte15c4a642007-10-30 18:44:17 +01001380int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1381{
1382 u64 data;
1383
1384 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001385 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001386 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001387 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001388 case MSR_IA32_DEBUGCTLMSR:
1389 case MSR_IA32_LASTBRANCHFROMIP:
1390 case MSR_IA32_LASTBRANCHTOIP:
1391 case MSR_IA32_LASTINTFROMIP:
1392 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301393 case MSR_K8_SYSCFG:
1394 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001395 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301396 case MSR_P6_PERFCTR0:
1397 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301398 case MSR_P6_EVNTSEL0:
1399 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301400 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301401 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001402 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001403 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001404 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001405 data = 0;
1406 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001407 case MSR_MTRRcap:
1408 data = 0x500 | KVM_NR_VAR_MTRR;
1409 break;
1410 case 0x200 ... 0x2ff:
1411 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001412 case 0xcd: /* fsb frequency */
1413 data = 3;
1414 break;
1415 case MSR_IA32_APICBASE:
1416 data = kvm_get_apic_base(vcpu);
1417 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001418 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1419 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1420 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001421 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001422 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001423 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001424 case MSR_IA32_PERF_STATUS:
1425 /* TSC increment by tick */
1426 data = 1000ULL;
1427 /* CPU multiplier */
1428 data |= (((uint64_t)4ULL) << 40);
1429 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001430 case MSR_EFER:
Avi Kivityf6801df2010-01-21 15:31:50 +02001431 data = vcpu->arch.efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001432 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001433 case MSR_KVM_WALL_CLOCK:
1434 data = vcpu->kvm->arch.wall_clock;
1435 break;
1436 case MSR_KVM_SYSTEM_TIME:
1437 data = vcpu->arch.time;
1438 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001439 case MSR_IA32_P5_MC_ADDR:
1440 case MSR_IA32_P5_MC_TYPE:
1441 case MSR_IA32_MCG_CAP:
1442 case MSR_IA32_MCG_CTL:
1443 case MSR_IA32_MCG_STATUS:
1444 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1445 return get_msr_mce(vcpu, msr, pdata);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001446 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
1447 if (kvm_hv_msr_partition_wide(msr)) {
1448 int r;
1449 mutex_lock(&vcpu->kvm->lock);
1450 r = get_msr_hyperv_pw(vcpu, msr, pdata);
1451 mutex_unlock(&vcpu->kvm->lock);
1452 return r;
1453 } else
1454 return get_msr_hyperv(vcpu, msr, pdata);
1455 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001456 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001457 if (!ignore_msrs) {
1458 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1459 return 1;
1460 } else {
1461 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1462 data = 0;
1463 }
1464 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001465 }
1466 *pdata = data;
1467 return 0;
1468}
1469EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1470
Carsten Otte313a3dc2007-10-11 19:16:52 +02001471/*
1472 * Read or write a bunch of msrs. All parameters are kernel addresses.
1473 *
1474 * @return number of msrs set successfully.
1475 */
1476static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1477 struct kvm_msr_entry *entries,
1478 int (*do_msr)(struct kvm_vcpu *vcpu,
1479 unsigned index, u64 *data))
1480{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001481 int i, idx;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001482
1483 vcpu_load(vcpu);
1484
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001485 idx = srcu_read_lock(&vcpu->kvm->srcu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001486 for (i = 0; i < msrs->nmsrs; ++i)
1487 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1488 break;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02001489 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001490
1491 vcpu_put(vcpu);
1492
1493 return i;
1494}
1495
1496/*
1497 * Read or write a bunch of msrs. Parameters are user addresses.
1498 *
1499 * @return number of msrs set successfully.
1500 */
1501static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1502 int (*do_msr)(struct kvm_vcpu *vcpu,
1503 unsigned index, u64 *data),
1504 int writeback)
1505{
1506 struct kvm_msrs msrs;
1507 struct kvm_msr_entry *entries;
1508 int r, n;
1509 unsigned size;
1510
1511 r = -EFAULT;
1512 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1513 goto out;
1514
1515 r = -E2BIG;
1516 if (msrs.nmsrs >= MAX_IO_MSRS)
1517 goto out;
1518
1519 r = -ENOMEM;
1520 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1521 entries = vmalloc(size);
1522 if (!entries)
1523 goto out;
1524
1525 r = -EFAULT;
1526 if (copy_from_user(entries, user_msrs->entries, size))
1527 goto out_free;
1528
1529 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1530 if (r < 0)
1531 goto out_free;
1532
1533 r = -EFAULT;
1534 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1535 goto out_free;
1536
1537 r = n;
1538
1539out_free:
1540 vfree(entries);
1541out:
1542 return r;
1543}
1544
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001545int kvm_dev_ioctl_check_extension(long ext)
1546{
1547 int r;
1548
1549 switch (ext) {
1550 case KVM_CAP_IRQCHIP:
1551 case KVM_CAP_HLT:
1552 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001553 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001554 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001555 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001556 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001557 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001558 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001559 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001560 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001561 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001562 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001563 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001564 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001565 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001566 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001567 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001568 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001569 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001570 case KVM_CAP_VCPU_EVENTS:
Gleb Natapov55cd8e52010-01-17 15:51:22 +02001571 case KVM_CAP_HYPERV:
Gleb Natapov10388a02010-01-17 15:51:23 +02001572 case KVM_CAP_HYPERV_VAPIC:
Gleb Natapovc25bc162010-01-17 15:51:24 +02001573 case KVM_CAP_HYPERV_SPIN:
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +08001574 case KVM_CAP_PCI_SEGMENT:
Jan Kiszkad2be1652010-02-23 17:47:57 +01001575 case KVM_CAP_X86_ROBUST_SINGLESTEP:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001576 r = 1;
1577 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001578 case KVM_CAP_COALESCED_MMIO:
1579 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1580 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001581 case KVM_CAP_VAPIC:
1582 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1583 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001584 case KVM_CAP_NR_VCPUS:
1585 r = KVM_MAX_VCPUS;
1586 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001587 case KVM_CAP_NR_MEMSLOTS:
1588 r = KVM_MEMORY_SLOTS;
1589 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001590 case KVM_CAP_PV_MMU: /* obsolete */
1591 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001592 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001593 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001594 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001595 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001596 case KVM_CAP_MCE:
1597 r = KVM_MAX_MCE_BANKS;
1598 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001599 default:
1600 r = 0;
1601 break;
1602 }
1603 return r;
1604
1605}
1606
Carsten Otte043405e2007-10-10 17:16:19 +02001607long kvm_arch_dev_ioctl(struct file *filp,
1608 unsigned int ioctl, unsigned long arg)
1609{
1610 void __user *argp = (void __user *)arg;
1611 long r;
1612
1613 switch (ioctl) {
1614 case KVM_GET_MSR_INDEX_LIST: {
1615 struct kvm_msr_list __user *user_msr_list = argp;
1616 struct kvm_msr_list msr_list;
1617 unsigned n;
1618
1619 r = -EFAULT;
1620 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1621 goto out;
1622 n = msr_list.nmsrs;
1623 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1624 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1625 goto out;
1626 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001627 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001628 goto out;
1629 r = -EFAULT;
1630 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1631 num_msrs_to_save * sizeof(u32)))
1632 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001633 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001634 &emulated_msrs,
1635 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1636 goto out;
1637 r = 0;
1638 break;
1639 }
Avi Kivity674eea02008-02-11 18:37:23 +02001640 case KVM_GET_SUPPORTED_CPUID: {
1641 struct kvm_cpuid2 __user *cpuid_arg = argp;
1642 struct kvm_cpuid2 cpuid;
1643
1644 r = -EFAULT;
1645 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1646 goto out;
1647 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001648 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001649 if (r)
1650 goto out;
1651
1652 r = -EFAULT;
1653 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1654 goto out;
1655 r = 0;
1656 break;
1657 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001658 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1659 u64 mce_cap;
1660
1661 mce_cap = KVM_MCE_CAP_SUPPORTED;
1662 r = -EFAULT;
1663 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1664 goto out;
1665 r = 0;
1666 break;
1667 }
Carsten Otte043405e2007-10-10 17:16:19 +02001668 default:
1669 r = -EINVAL;
1670 }
1671out:
1672 return r;
1673}
1674
Carsten Otte313a3dc2007-10-11 19:16:52 +02001675void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1676{
1677 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001678 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1679 unsigned long khz = cpufreq_quick_get(cpu);
1680 if (!khz)
1681 khz = tsc_khz;
1682 per_cpu(cpu_tsc_khz, cpu) = khz;
1683 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001684 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001685}
1686
1687void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1688{
Amit Shah9327fd12007-11-15 18:38:46 +02001689 kvm_put_guest_fpu(vcpu);
Avi Kivity02daab22009-12-30 12:40:26 +02001690 kvm_x86_ops->vcpu_put(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001691}
1692
Dan Kenigsberg07716712007-11-21 17:10:04 +02001693static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001694{
Avi Kivitye286e862009-05-03 18:50:55 +03001695 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001696
Avi Kivitye286e862009-05-03 18:50:55 +03001697 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001698 return efer & EFER_NX;
1699}
1700
1701static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1702{
1703 int i;
1704 struct kvm_cpuid_entry2 *e, *entry;
1705
Carsten Otte313a3dc2007-10-11 19:16:52 +02001706 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001707 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1708 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001709 if (e->function == 0x80000001) {
1710 entry = e;
1711 break;
1712 }
1713 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001714 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001715 entry->edx &= ~(1 << 20);
1716 printk(KERN_INFO "kvm: guest NX capability removed\n");
1717 }
1718}
1719
Dan Kenigsberg07716712007-11-21 17:10:04 +02001720/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001721static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1722 struct kvm_cpuid *cpuid,
1723 struct kvm_cpuid_entry __user *entries)
1724{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001725 int r, i;
1726 struct kvm_cpuid_entry *cpuid_entries;
1727
1728 r = -E2BIG;
1729 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1730 goto out;
1731 r = -ENOMEM;
1732 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1733 if (!cpuid_entries)
1734 goto out;
1735 r = -EFAULT;
1736 if (copy_from_user(cpuid_entries, entries,
1737 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1738 goto out_free;
1739 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001740 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1741 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1742 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1743 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1744 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1745 vcpu->arch.cpuid_entries[i].index = 0;
1746 vcpu->arch.cpuid_entries[i].flags = 0;
1747 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1748 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1749 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001750 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001751 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001752 cpuid_fix_nx_cap(vcpu);
1753 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001754 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001755 kvm_x86_ops->cpuid_update(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001756
1757out_free:
1758 vfree(cpuid_entries);
1759out:
1760 return r;
1761}
1762
1763static int kvm_vcpu_ioctl_set_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{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001767 int r;
1768
1769 r = -E2BIG;
1770 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1771 goto out;
1772 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001773 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001774 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001775 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001776 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001777 kvm_apic_set_version(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08001778 kvm_x86_ops->cpuid_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001779 return 0;
1780
1781out:
1782 return r;
1783}
1784
Dan Kenigsberg07716712007-11-21 17:10:04 +02001785static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001786 struct kvm_cpuid2 *cpuid,
1787 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001788{
1789 int r;
1790
1791 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001792 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001793 goto out;
1794 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001795 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001796 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001797 goto out;
1798 return 0;
1799
1800out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001801 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001802 return r;
1803}
1804
Dan Kenigsberg07716712007-11-21 17:10:04 +02001805static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001806 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001807{
1808 entry->function = function;
1809 entry->index = index;
1810 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001811 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001812 entry->flags = 0;
1813}
1814
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001815#define F(x) bit(X86_FEATURE_##x)
1816
Dan Kenigsberg07716712007-11-21 17:10:04 +02001817static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1818 u32 index, int *nent, int maxnent)
1819{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001820 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001821#ifdef CONFIG_X86_64
Sheng Yang17cc3932010-01-05 19:02:27 +08001822 unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
1823 ? F(GBPAGES) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001824 unsigned f_lm = F(LM);
1825#else
Sheng Yang17cc3932010-01-05 19:02:27 +08001826 unsigned f_gbpages = 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001827 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001828#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001829 unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001830
1831 /* cpuid 1.edx */
1832 const u32 kvm_supported_word0_x86_features =
1833 F(FPU) | F(VME) | F(DE) | F(PSE) |
1834 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1835 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1836 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1837 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1838 0 /* Reserved, DS, ACPI */ | F(MMX) |
1839 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1840 0 /* HTT, TM, Reserved, PBE */;
1841 /* cpuid 0x80000001.edx */
1842 const u32 kvm_supported_word1_x86_features =
1843 F(FPU) | F(VME) | F(DE) | F(PSE) |
1844 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1845 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1846 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1847 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1848 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001849 F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001850 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1851 /* cpuid 1.ecx */
1852 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001853 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1854 0 /* DS-CPL, VMX, SMX, EST */ |
1855 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1856 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1857 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001858 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001859 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001860 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001861 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001862 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1863 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1864 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1865 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001866
Amit Shah19355472009-01-14 16:56:00 +00001867 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001868 get_cpu();
1869 do_cpuid_1_ent(entry, function, index);
1870 ++*nent;
1871
1872 switch (function) {
1873 case 0:
1874 entry->eax = min(entry->eax, (u32)0xb);
1875 break;
1876 case 1:
1877 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001878 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d2009-07-12 16:10:55 +03001879 /* we support x2apic emulation even if host does not support
1880 * it since we emulate x2apic in software */
1881 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001882 break;
1883 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1884 * may return different values. This forces us to get_cpu() before
1885 * issuing the first command, and also to emulate this annoying behavior
1886 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1887 case 2: {
1888 int t, times = entry->eax & 0xff;
1889
1890 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001891 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001892 for (t = 1; t < times && *nent < maxnent; ++t) {
1893 do_cpuid_1_ent(&entry[t], function, 0);
1894 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1895 ++*nent;
1896 }
1897 break;
1898 }
1899 /* function 4 and 0xb have additional index. */
1900 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001901 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001902
1903 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1904 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001905 for (i = 1; *nent < maxnent; ++i) {
1906 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001907 if (!cache_type)
1908 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001909 do_cpuid_1_ent(&entry[i], function, i);
1910 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001911 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1912 ++*nent;
1913 }
1914 break;
1915 }
1916 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001917 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001918
1919 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1920 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001921 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001922 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001923 if (!level_type)
1924 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001925 do_cpuid_1_ent(&entry[i], function, i);
1926 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001927 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1928 ++*nent;
1929 }
1930 break;
1931 }
1932 case 0x80000000:
1933 entry->eax = min(entry->eax, 0x8000001a);
1934 break;
1935 case 0x80000001:
1936 entry->edx &= kvm_supported_word1_x86_features;
1937 entry->ecx &= kvm_supported_word6_x86_features;
1938 break;
1939 }
1940 put_cpu();
1941}
1942
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001943#undef F
1944
Avi Kivity674eea02008-02-11 18:37:23 +02001945static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001946 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001947{
1948 struct kvm_cpuid_entry2 *cpuid_entries;
1949 int limit, nent = 0, r = -E2BIG;
1950 u32 func;
1951
1952 if (cpuid->nent < 1)
1953 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001954 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1955 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001956 r = -ENOMEM;
1957 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1958 if (!cpuid_entries)
1959 goto out;
1960
1961 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1962 limit = cpuid_entries[0].eax;
1963 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1964 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001965 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001966 r = -E2BIG;
1967 if (nent >= cpuid->nent)
1968 goto out_free;
1969
1970 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1971 limit = cpuid_entries[nent - 1].eax;
1972 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1973 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001974 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001975 r = -E2BIG;
1976 if (nent >= cpuid->nent)
1977 goto out_free;
1978
Dan Kenigsberg07716712007-11-21 17:10:04 +02001979 r = -EFAULT;
1980 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001981 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001982 goto out_free;
1983 cpuid->nent = nent;
1984 r = 0;
1985
1986out_free:
1987 vfree(cpuid_entries);
1988out:
1989 return r;
1990}
1991
Carsten Otte313a3dc2007-10-11 19:16:52 +02001992static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1993 struct kvm_lapic_state *s)
1994{
1995 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001996 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001997 vcpu_put(vcpu);
1998
1999 return 0;
2000}
2001
2002static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
2003 struct kvm_lapic_state *s)
2004{
2005 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002006 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002007 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03002008 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002009 vcpu_put(vcpu);
2010
2011 return 0;
2012}
2013
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002014static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2015 struct kvm_interrupt *irq)
2016{
2017 if (irq->irq < 0 || irq->irq >= 256)
2018 return -EINVAL;
2019 if (irqchip_in_kernel(vcpu->kvm))
2020 return -ENXIO;
2021 vcpu_load(vcpu);
2022
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002023 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002024
2025 vcpu_put(vcpu);
2026
2027 return 0;
2028}
2029
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002030static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
2031{
2032 vcpu_load(vcpu);
2033 kvm_inject_nmi(vcpu);
2034 vcpu_put(vcpu);
2035
2036 return 0;
2037}
2038
Avi Kivityb209749f2007-10-22 16:50:39 +02002039static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
2040 struct kvm_tpr_access_ctl *tac)
2041{
2042 if (tac->flags)
2043 return -EINVAL;
2044 vcpu->arch.tpr_access_reporting = !!tac->enabled;
2045 return 0;
2046}
2047
Huang Ying890ca9a2009-05-11 16:48:15 +08002048static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
2049 u64 mcg_cap)
2050{
2051 int r;
2052 unsigned bank_num = mcg_cap & 0xff, bank;
2053
2054 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02002055 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08002056 goto out;
2057 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
2058 goto out;
2059 r = 0;
2060 vcpu->arch.mcg_cap = mcg_cap;
2061 /* Init IA32_MCG_CTL to all 1s */
2062 if (mcg_cap & MCG_CTL_P)
2063 vcpu->arch.mcg_ctl = ~(u64)0;
2064 /* Init IA32_MCi_CTL to all 1s */
2065 for (bank = 0; bank < bank_num; bank++)
2066 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
2067out:
2068 return r;
2069}
2070
2071static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
2072 struct kvm_x86_mce *mce)
2073{
2074 u64 mcg_cap = vcpu->arch.mcg_cap;
2075 unsigned bank_num = mcg_cap & 0xff;
2076 u64 *banks = vcpu->arch.mce_banks;
2077
2078 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
2079 return -EINVAL;
2080 /*
2081 * if IA32_MCG_CTL is not all 1s, the uncorrected error
2082 * reporting is disabled
2083 */
2084 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2085 vcpu->arch.mcg_ctl != ~(u64)0)
2086 return 0;
2087 banks += 4 * mce->bank;
2088 /*
2089 * if IA32_MCi_CTL is not all 1s, the uncorrected error
2090 * reporting is disabled for the bank
2091 */
2092 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
2093 return 0;
2094 if (mce->status & MCI_STATUS_UC) {
2095 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
Avi Kivityfc78f512009-12-07 12:16:48 +02002096 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
Huang Ying890ca9a2009-05-11 16:48:15 +08002097 printk(KERN_DEBUG "kvm: set_mce: "
2098 "injects mce exception while "
2099 "previous one is in progress!\n");
2100 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
2101 return 0;
2102 }
2103 if (banks[1] & MCI_STATUS_VAL)
2104 mce->status |= MCI_STATUS_OVER;
2105 banks[2] = mce->addr;
2106 banks[3] = mce->misc;
2107 vcpu->arch.mcg_status = mce->mcg_status;
2108 banks[1] = mce->status;
2109 kvm_queue_exception(vcpu, MC_VECTOR);
2110 } else if (!(banks[1] & MCI_STATUS_VAL)
2111 || !(banks[1] & MCI_STATUS_UC)) {
2112 if (banks[1] & MCI_STATUS_VAL)
2113 mce->status |= MCI_STATUS_OVER;
2114 banks[2] = mce->addr;
2115 banks[3] = mce->misc;
2116 banks[1] = mce->status;
2117 } else
2118 banks[1] |= MCI_STATUS_OVER;
2119 return 0;
2120}
2121
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002122static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
2123 struct kvm_vcpu_events *events)
2124{
2125 vcpu_load(vcpu);
2126
2127 events->exception.injected = vcpu->arch.exception.pending;
2128 events->exception.nr = vcpu->arch.exception.nr;
2129 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
2130 events->exception.error_code = vcpu->arch.exception.error_code;
2131
2132 events->interrupt.injected = vcpu->arch.interrupt.pending;
2133 events->interrupt.nr = vcpu->arch.interrupt.nr;
2134 events->interrupt.soft = vcpu->arch.interrupt.soft;
2135
2136 events->nmi.injected = vcpu->arch.nmi_injected;
2137 events->nmi.pending = vcpu->arch.nmi_pending;
2138 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
2139
2140 events->sipi_vector = vcpu->arch.sipi_vector;
2141
Jan Kiszkadab4b912009-12-06 18:24:15 +01002142 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
2143 | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002144
2145 vcpu_put(vcpu);
2146}
2147
2148static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
2149 struct kvm_vcpu_events *events)
2150{
Jan Kiszkadab4b912009-12-06 18:24:15 +01002151 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
2152 | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002153 return -EINVAL;
2154
2155 vcpu_load(vcpu);
2156
2157 vcpu->arch.exception.pending = events->exception.injected;
2158 vcpu->arch.exception.nr = events->exception.nr;
2159 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
2160 vcpu->arch.exception.error_code = events->exception.error_code;
2161
2162 vcpu->arch.interrupt.pending = events->interrupt.injected;
2163 vcpu->arch.interrupt.nr = events->interrupt.nr;
2164 vcpu->arch.interrupt.soft = events->interrupt.soft;
2165 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
2166 kvm_pic_clear_isr_ack(vcpu->kvm);
2167
2168 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01002169 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
2170 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002171 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
2172
Jan Kiszkadab4b912009-12-06 18:24:15 +01002173 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
2174 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002175
2176 vcpu_put(vcpu);
2177
2178 return 0;
2179}
2180
Carsten Otte313a3dc2007-10-11 19:16:52 +02002181long kvm_arch_vcpu_ioctl(struct file *filp,
2182 unsigned int ioctl, unsigned long arg)
2183{
2184 struct kvm_vcpu *vcpu = filp->private_data;
2185 void __user *argp = (void __user *)arg;
2186 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07002187 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02002188
2189 switch (ioctl) {
2190 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002191 r = -EINVAL;
2192 if (!vcpu->arch.apic)
2193 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002194 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002195
Dave Hansenb772ff32008-08-11 10:01:47 -07002196 r = -ENOMEM;
2197 if (!lapic)
2198 goto out;
2199 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002200 if (r)
2201 goto out;
2202 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07002203 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02002204 goto out;
2205 r = 0;
2206 break;
2207 }
2208 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02002209 r = -EINVAL;
2210 if (!vcpu->arch.apic)
2211 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002212 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2213 r = -ENOMEM;
2214 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02002215 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07002216 r = -EFAULT;
2217 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2218 goto out;
2219 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002220 if (r)
2221 goto out;
2222 r = 0;
2223 break;
2224 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08002225 case KVM_INTERRUPT: {
2226 struct kvm_interrupt irq;
2227
2228 r = -EFAULT;
2229 if (copy_from_user(&irq, argp, sizeof irq))
2230 goto out;
2231 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2232 if (r)
2233 goto out;
2234 r = 0;
2235 break;
2236 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002237 case KVM_NMI: {
2238 r = kvm_vcpu_ioctl_nmi(vcpu);
2239 if (r)
2240 goto out;
2241 r = 0;
2242 break;
2243 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002244 case KVM_SET_CPUID: {
2245 struct kvm_cpuid __user *cpuid_arg = argp;
2246 struct kvm_cpuid cpuid;
2247
2248 r = -EFAULT;
2249 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2250 goto out;
2251 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2252 if (r)
2253 goto out;
2254 break;
2255 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002256 case KVM_SET_CPUID2: {
2257 struct kvm_cpuid2 __user *cpuid_arg = argp;
2258 struct kvm_cpuid2 cpuid;
2259
2260 r = -EFAULT;
2261 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2262 goto out;
2263 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002264 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002265 if (r)
2266 goto out;
2267 break;
2268 }
2269 case KVM_GET_CPUID2: {
2270 struct kvm_cpuid2 __user *cpuid_arg = argp;
2271 struct kvm_cpuid2 cpuid;
2272
2273 r = -EFAULT;
2274 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2275 goto out;
2276 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002277 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002278 if (r)
2279 goto out;
2280 r = -EFAULT;
2281 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2282 goto out;
2283 r = 0;
2284 break;
2285 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002286 case KVM_GET_MSRS:
2287 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2288 break;
2289 case KVM_SET_MSRS:
2290 r = msr_io(vcpu, argp, do_set_msr, 0);
2291 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002292 case KVM_TPR_ACCESS_REPORTING: {
2293 struct kvm_tpr_access_ctl tac;
2294
2295 r = -EFAULT;
2296 if (copy_from_user(&tac, argp, sizeof tac))
2297 goto out;
2298 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2299 if (r)
2300 goto out;
2301 r = -EFAULT;
2302 if (copy_to_user(argp, &tac, sizeof tac))
2303 goto out;
2304 r = 0;
2305 break;
2306 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002307 case KVM_SET_VAPIC_ADDR: {
2308 struct kvm_vapic_addr va;
2309
2310 r = -EINVAL;
2311 if (!irqchip_in_kernel(vcpu->kvm))
2312 goto out;
2313 r = -EFAULT;
2314 if (copy_from_user(&va, argp, sizeof va))
2315 goto out;
2316 r = 0;
2317 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2318 break;
2319 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002320 case KVM_X86_SETUP_MCE: {
2321 u64 mcg_cap;
2322
2323 r = -EFAULT;
2324 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2325 goto out;
2326 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2327 break;
2328 }
2329 case KVM_X86_SET_MCE: {
2330 struct kvm_x86_mce mce;
2331
2332 r = -EFAULT;
2333 if (copy_from_user(&mce, argp, sizeof mce))
2334 goto out;
2335 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2336 break;
2337 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002338 case KVM_GET_VCPU_EVENTS: {
2339 struct kvm_vcpu_events events;
2340
2341 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2342
2343 r = -EFAULT;
2344 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2345 break;
2346 r = 0;
2347 break;
2348 }
2349 case KVM_SET_VCPU_EVENTS: {
2350 struct kvm_vcpu_events events;
2351
2352 r = -EFAULT;
2353 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2354 break;
2355
2356 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2357 break;
2358 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002359 default:
2360 r = -EINVAL;
2361 }
2362out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002363 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002364 return r;
2365}
2366
Carsten Otte1fe779f2007-10-29 16:08:35 +01002367static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2368{
2369 int ret;
2370
2371 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2372 return -1;
2373 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2374 return ret;
2375}
2376
Sheng Yangb927a3c2009-07-21 10:42:48 +08002377static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2378 u64 ident_addr)
2379{
2380 kvm->arch.ept_identity_map_addr = ident_addr;
2381 return 0;
2382}
2383
Carsten Otte1fe779f2007-10-29 16:08:35 +01002384static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2385 u32 kvm_nr_mmu_pages)
2386{
2387 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2388 return -EINVAL;
2389
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002390 mutex_lock(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002391 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002392
2393 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002394 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002395
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002396 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002397 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002398 return 0;
2399}
2400
2401static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2402{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002403 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002404}
2405
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002406gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
2407{
2408 int i;
2409 struct kvm_mem_alias *alias;
2410 struct kvm_mem_aliases *aliases;
2411
2412 aliases = rcu_dereference(kvm->arch.aliases);
2413
2414 for (i = 0; i < aliases->naliases; ++i) {
2415 alias = &aliases->aliases[i];
2416 if (alias->flags & KVM_ALIAS_INVALID)
2417 continue;
2418 if (gfn >= alias->base_gfn
2419 && gfn < alias->base_gfn + alias->npages)
2420 return alias->target_gfn + gfn - alias->base_gfn;
2421 }
2422 return gfn;
2423}
2424
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002425gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2426{
2427 int i;
2428 struct kvm_mem_alias *alias;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002429 struct kvm_mem_aliases *aliases;
2430
2431 aliases = rcu_dereference(kvm->arch.aliases);
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002432
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002433 for (i = 0; i < aliases->naliases; ++i) {
2434 alias = &aliases->aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002435 if (gfn >= alias->base_gfn
2436 && gfn < alias->base_gfn + alias->npages)
2437 return alias->target_gfn + gfn - alias->base_gfn;
2438 }
2439 return gfn;
2440}
2441
Carsten Otte1fe779f2007-10-29 16:08:35 +01002442/*
2443 * Set a new alias region. Aliases map a portion of physical memory into
2444 * another portion. This is useful for memory windows, for example the PC
2445 * VGA region.
2446 */
2447static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2448 struct kvm_memory_alias *alias)
2449{
2450 int r, n;
2451 struct kvm_mem_alias *p;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002452 struct kvm_mem_aliases *aliases, *old_aliases;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002453
2454 r = -EINVAL;
2455 /* General sanity checks */
2456 if (alias->memory_size & (PAGE_SIZE - 1))
2457 goto out;
2458 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2459 goto out;
2460 if (alias->slot >= KVM_ALIAS_SLOTS)
2461 goto out;
2462 if (alias->guest_phys_addr + alias->memory_size
2463 < alias->guest_phys_addr)
2464 goto out;
2465 if (alias->target_phys_addr + alias->memory_size
2466 < alias->target_phys_addr)
2467 goto out;
2468
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002469 r = -ENOMEM;
2470 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2471 if (!aliases)
2472 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002473
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002474 mutex_lock(&kvm->slots_lock);
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002475
2476 /* invalidate any gfn reference in case of deletion/shrinking */
2477 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
2478 aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
2479 old_aliases = kvm->arch.aliases;
2480 rcu_assign_pointer(kvm->arch.aliases, aliases);
2481 synchronize_srcu_expedited(&kvm->srcu);
2482 kvm_mmu_zap_all(kvm);
2483 kfree(old_aliases);
2484
2485 r = -ENOMEM;
2486 aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
2487 if (!aliases)
2488 goto out_unlock;
2489
2490 memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002491
2492 p = &aliases->aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002493 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2494 p->npages = alias->memory_size >> PAGE_SHIFT;
2495 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002496 p->flags &= ~(KVM_ALIAS_INVALID);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002497
2498 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002499 if (aliases->aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002500 break;
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02002501 aliases->naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002502
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002503 old_aliases = kvm->arch.aliases;
2504 rcu_assign_pointer(kvm->arch.aliases, aliases);
2505 synchronize_srcu_expedited(&kvm->srcu);
2506 kfree(old_aliases);
2507 r = 0;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002508
Marcelo Tosattia983fb22009-12-23 14:35:23 -02002509out_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002510 mutex_unlock(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002511out:
2512 return r;
2513}
2514
2515static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2516{
2517 int r;
2518
2519 r = 0;
2520 switch (chip->chip_id) {
2521 case KVM_IRQCHIP_PIC_MASTER:
2522 memcpy(&chip->chip.pic,
2523 &pic_irqchip(kvm)->pics[0],
2524 sizeof(struct kvm_pic_state));
2525 break;
2526 case KVM_IRQCHIP_PIC_SLAVE:
2527 memcpy(&chip->chip.pic,
2528 &pic_irqchip(kvm)->pics[1],
2529 sizeof(struct kvm_pic_state));
2530 break;
2531 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002532 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002533 break;
2534 default:
2535 r = -EINVAL;
2536 break;
2537 }
2538 return r;
2539}
2540
2541static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2542{
2543 int r;
2544
2545 r = 0;
2546 switch (chip->chip_id) {
2547 case KVM_IRQCHIP_PIC_MASTER:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002548 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002549 memcpy(&pic_irqchip(kvm)->pics[0],
2550 &chip->chip.pic,
2551 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002552 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002553 break;
2554 case KVM_IRQCHIP_PIC_SLAVE:
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002555 raw_spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002556 memcpy(&pic_irqchip(kvm)->pics[1],
2557 &chip->chip.pic,
2558 sizeof(struct kvm_pic_state));
Thomas Gleixnerfa8273e2010-02-17 14:00:41 +00002559 raw_spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002560 break;
2561 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002562 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002563 break;
2564 default:
2565 r = -EINVAL;
2566 break;
2567 }
2568 kvm_pic_update_irq(pic_irqchip(kvm));
2569 return r;
2570}
2571
Sheng Yange0f63cb2008-03-04 00:50:59 +08002572static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2573{
2574 int r = 0;
2575
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002576 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002577 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002578 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002579 return r;
2580}
2581
2582static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2583{
2584 int r = 0;
2585
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002586 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002587 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002588 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2589 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2590 return r;
2591}
2592
2593static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2594{
2595 int r = 0;
2596
2597 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2598 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2599 sizeof(ps->channels));
2600 ps->flags = kvm->arch.vpit->pit_state.flags;
2601 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2602 return r;
2603}
2604
2605static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2606{
2607 int r = 0, start = 0;
2608 u32 prev_legacy, cur_legacy;
2609 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2610 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2611 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2612 if (!prev_legacy && cur_legacy)
2613 start = 1;
2614 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2615 sizeof(kvm->arch.vpit->pit_state.channels));
2616 kvm->arch.vpit->pit_state.flags = ps->flags;
2617 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002618 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002619 return r;
2620}
2621
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002622static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2623 struct kvm_reinject_control *control)
2624{
2625 if (!kvm->arch.vpit)
2626 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002627 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002628 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002629 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002630 return 0;
2631}
2632
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002633/*
2634 * Get (and clear) the dirty memory log for a memory slot.
2635 */
2636int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2637 struct kvm_dirty_log *log)
2638{
Marcelo Tosattib050b012009-12-23 14:35:22 -02002639 int r, n, i;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002640 struct kvm_memory_slot *memslot;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002641 unsigned long is_dirty = 0;
2642 unsigned long *dirty_bitmap = NULL;
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002643
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002644 mutex_lock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002645
Marcelo Tosattib050b012009-12-23 14:35:22 -02002646 r = -EINVAL;
2647 if (log->slot >= KVM_MEMORY_SLOTS)
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002648 goto out;
2649
Marcelo Tosattib050b012009-12-23 14:35:22 -02002650 memslot = &kvm->memslots->memslots[log->slot];
2651 r = -ENOENT;
2652 if (!memslot->dirty_bitmap)
2653 goto out;
2654
2655 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2656
2657 r = -ENOMEM;
2658 dirty_bitmap = vmalloc(n);
2659 if (!dirty_bitmap)
2660 goto out;
2661 memset(dirty_bitmap, 0, n);
2662
2663 for (i = 0; !is_dirty && i < n/sizeof(long); i++)
2664 is_dirty = memslot->dirty_bitmap[i];
2665
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002666 /* If nothing is dirty, don't bother messing with page tables. */
2667 if (is_dirty) {
Marcelo Tosattib050b012009-12-23 14:35:22 -02002668 struct kvm_memslots *slots, *old_slots;
2669
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002670 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002671 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002672 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattib050b012009-12-23 14:35:22 -02002673
2674 slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
2675 if (!slots)
2676 goto out_free;
2677
2678 memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
2679 slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
2680
2681 old_slots = kvm->memslots;
2682 rcu_assign_pointer(kvm->memslots, slots);
2683 synchronize_srcu_expedited(&kvm->srcu);
2684 dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
2685 kfree(old_slots);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002686 }
Marcelo Tosattib050b012009-12-23 14:35:22 -02002687
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002688 r = 0;
Marcelo Tosattib050b012009-12-23 14:35:22 -02002689 if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
2690 r = -EFAULT;
2691out_free:
2692 vfree(dirty_bitmap);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002693out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002694 mutex_unlock(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002695 return r;
2696}
2697
Carsten Otte1fe779f2007-10-29 16:08:35 +01002698long kvm_arch_vm_ioctl(struct file *filp,
2699 unsigned int ioctl, unsigned long arg)
2700{
2701 struct kvm *kvm = filp->private_data;
2702 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002703 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002704 /*
2705 * This union makes it completely explicit to gcc-3.x
2706 * that these two variables' stack usage should be
2707 * combined, not added together.
2708 */
2709 union {
2710 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002711 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002712 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002713 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002714 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002715
2716 switch (ioctl) {
2717 case KVM_SET_TSS_ADDR:
2718 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2719 if (r < 0)
2720 goto out;
2721 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002722 case KVM_SET_IDENTITY_MAP_ADDR: {
2723 u64 ident_addr;
2724
2725 r = -EFAULT;
2726 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2727 goto out;
2728 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2729 if (r < 0)
2730 goto out;
2731 break;
2732 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002733 case KVM_SET_MEMORY_REGION: {
2734 struct kvm_memory_region kvm_mem;
2735 struct kvm_userspace_memory_region kvm_userspace_mem;
2736
2737 r = -EFAULT;
2738 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2739 goto out;
2740 kvm_userspace_mem.slot = kvm_mem.slot;
2741 kvm_userspace_mem.flags = kvm_mem.flags;
2742 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2743 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2744 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2745 if (r)
2746 goto out;
2747 break;
2748 }
2749 case KVM_SET_NR_MMU_PAGES:
2750 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2751 if (r)
2752 goto out;
2753 break;
2754 case KVM_GET_NR_MMU_PAGES:
2755 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2756 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002757 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002758 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002759 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002760 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002761 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002762 if (r)
2763 goto out;
2764 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002765 case KVM_CREATE_IRQCHIP: {
2766 struct kvm_pic *vpic;
2767
2768 mutex_lock(&kvm->lock);
2769 r = -EEXIST;
2770 if (kvm->arch.vpic)
2771 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002772 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002773 vpic = kvm_create_pic(kvm);
2774 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002775 r = kvm_ioapic_init(kvm);
2776 if (r) {
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002777 kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
2778 &vpic->dev);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002779 kfree(vpic);
2780 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002781 }
2782 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002783 goto create_irqchip_unlock;
2784 smp_wmb();
2785 kvm->arch.vpic = vpic;
2786 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002787 r = kvm_setup_default_irq_routing(kvm);
2788 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002789 mutex_lock(&kvm->irq_lock);
Wei Yongjun72bb2fc2010-02-09 10:33:03 +08002790 kvm_ioapic_destroy(kvm);
2791 kvm_destroy_pic(kvm);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002792 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002793 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002794 create_irqchip_unlock:
2795 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002796 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002797 }
Sheng Yang78376992008-01-28 05:10:22 +08002798 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002799 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2800 goto create_pit;
2801 case KVM_CREATE_PIT2:
2802 r = -EFAULT;
2803 if (copy_from_user(&u.pit_config, argp,
2804 sizeof(struct kvm_pit_config)))
2805 goto out;
2806 create_pit:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002807 mutex_lock(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002808 r = -EEXIST;
2809 if (kvm->arch.vpit)
2810 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002811 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002812 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002813 if (kvm->arch.vpit)
2814 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002815 create_pit_unlock:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002816 mutex_unlock(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002817 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002818 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002819 case KVM_IRQ_LINE: {
2820 struct kvm_irq_level irq_event;
2821
2822 r = -EFAULT;
2823 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2824 goto out;
2825 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002826 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002827 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2828 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002829 if (ioctl == KVM_IRQ_LINE_STATUS) {
2830 irq_event.status = status;
2831 if (copy_to_user(argp, &irq_event,
2832 sizeof irq_event))
2833 goto out;
2834 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002835 r = 0;
2836 }
2837 break;
2838 }
2839 case KVM_GET_IRQCHIP: {
2840 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002841 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002842
Dave Hansenf0d66272008-08-11 10:01:45 -07002843 r = -ENOMEM;
2844 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002845 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002846 r = -EFAULT;
2847 if (copy_from_user(chip, argp, sizeof *chip))
2848 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002849 r = -ENXIO;
2850 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002851 goto get_irqchip_out;
2852 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2853 if (r)
2854 goto get_irqchip_out;
2855 r = -EFAULT;
2856 if (copy_to_user(argp, chip, sizeof *chip))
2857 goto get_irqchip_out;
2858 r = 0;
2859 get_irqchip_out:
2860 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002861 if (r)
2862 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002863 break;
2864 }
2865 case KVM_SET_IRQCHIP: {
2866 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002867 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002868
Dave Hansenf0d66272008-08-11 10:01:45 -07002869 r = -ENOMEM;
2870 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002871 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002872 r = -EFAULT;
2873 if (copy_from_user(chip, argp, sizeof *chip))
2874 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002875 r = -ENXIO;
2876 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002877 goto set_irqchip_out;
2878 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2879 if (r)
2880 goto set_irqchip_out;
2881 r = 0;
2882 set_irqchip_out:
2883 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002884 if (r)
2885 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002886 break;
2887 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002888 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002889 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002890 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002891 goto out;
2892 r = -ENXIO;
2893 if (!kvm->arch.vpit)
2894 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002895 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002896 if (r)
2897 goto out;
2898 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002899 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002900 goto out;
2901 r = 0;
2902 break;
2903 }
2904 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002905 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002906 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002907 goto out;
2908 r = -ENXIO;
2909 if (!kvm->arch.vpit)
2910 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002911 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002912 if (r)
2913 goto out;
2914 r = 0;
2915 break;
2916 }
Beth Kone9f42752009-07-07 11:50:38 -04002917 case KVM_GET_PIT2: {
2918 r = -ENXIO;
2919 if (!kvm->arch.vpit)
2920 goto out;
2921 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2922 if (r)
2923 goto out;
2924 r = -EFAULT;
2925 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2926 goto out;
2927 r = 0;
2928 break;
2929 }
2930 case KVM_SET_PIT2: {
2931 r = -EFAULT;
2932 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2933 goto out;
2934 r = -ENXIO;
2935 if (!kvm->arch.vpit)
2936 goto out;
2937 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2938 if (r)
2939 goto out;
2940 r = 0;
2941 break;
2942 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002943 case KVM_REINJECT_CONTROL: {
2944 struct kvm_reinject_control control;
2945 r = -EFAULT;
2946 if (copy_from_user(&control, argp, sizeof(control)))
2947 goto out;
2948 r = kvm_vm_ioctl_reinject(kvm, &control);
2949 if (r)
2950 goto out;
2951 r = 0;
2952 break;
2953 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002954 case KVM_XEN_HVM_CONFIG: {
2955 r = -EFAULT;
2956 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2957 sizeof(struct kvm_xen_hvm_config)))
2958 goto out;
2959 r = -EINVAL;
2960 if (kvm->arch.xen_hvm_config.flags)
2961 goto out;
2962 r = 0;
2963 break;
2964 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002965 case KVM_SET_CLOCK: {
2966 struct timespec now;
2967 struct kvm_clock_data user_ns;
2968 u64 now_ns;
2969 s64 delta;
2970
2971 r = -EFAULT;
2972 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2973 goto out;
2974
2975 r = -EINVAL;
2976 if (user_ns.flags)
2977 goto out;
2978
2979 r = 0;
2980 ktime_get_ts(&now);
2981 now_ns = timespec_to_ns(&now);
2982 delta = user_ns.clock - now_ns;
2983 kvm->arch.kvmclock_offset = delta;
2984 break;
2985 }
2986 case KVM_GET_CLOCK: {
2987 struct timespec now;
2988 struct kvm_clock_data user_ns;
2989 u64 now_ns;
2990
2991 ktime_get_ts(&now);
2992 now_ns = timespec_to_ns(&now);
2993 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2994 user_ns.flags = 0;
2995
2996 r = -EFAULT;
2997 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2998 goto out;
2999 r = 0;
3000 break;
3001 }
3002
Carsten Otte1fe779f2007-10-29 16:08:35 +01003003 default:
3004 ;
3005 }
3006out:
3007 return r;
3008}
3009
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08003010static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02003011{
3012 u32 dummy[2];
3013 unsigned i, j;
3014
Glauber Costae3267cb2009-10-06 13:24:50 -04003015 /* skip the first msrs in the list. KVM-specific */
3016 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02003017 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3018 continue;
3019 if (j < i)
3020 msrs_to_save[j] = msrs_to_save[i];
3021 j++;
3022 }
3023 num_msrs_to_save = j;
3024}
3025
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003026static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
3027 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003028{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003029 if (vcpu->arch.apic &&
3030 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
3031 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003032
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003033 return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003034}
3035
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003036static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003037{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003038 if (vcpu->arch.apic &&
3039 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
3040 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003041
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003042 return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003043}
3044
Gleb Natapov1871c602010-02-10 14:21:32 +02003045gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3046{
3047 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3048 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3049}
3050
3051 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3052{
3053 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3054 access |= PFERR_FETCH_MASK;
3055 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3056}
3057
3058gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3059{
3060 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3061 access |= PFERR_WRITE_MASK;
3062 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
3063}
3064
3065/* uses this to access any guest's mapped memory without checking CPL */
3066gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
3067{
3068 return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
3069}
3070
3071static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
3072 struct kvm_vcpu *vcpu, u32 access,
3073 u32 *error)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003074{
3075 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003076 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003077
3078 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003079 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003080 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02003081 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003082 int ret;
3083
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003084 if (gpa == UNMAPPED_GVA) {
3085 r = X86EMUL_PROPAGATE_FAULT;
3086 goto out;
3087 }
Izik Eidus77c20022008-12-29 01:42:19 +02003088 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003089 if (ret < 0) {
3090 r = X86EMUL_UNHANDLEABLE;
3091 goto out;
3092 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003093
Izik Eidus77c20022008-12-29 01:42:19 +02003094 bytes -= toread;
3095 data += toread;
3096 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003097 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003098out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003099 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003100}
Izik Eidus77c20022008-12-29 01:42:19 +02003101
Gleb Natapov1871c602010-02-10 14:21:32 +02003102/* used for instruction fetching */
3103static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
3104 struct kvm_vcpu *vcpu, u32 *error)
3105{
3106 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3107 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3108 access | PFERR_FETCH_MASK, error);
3109}
3110
3111static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
3112 struct kvm_vcpu *vcpu, u32 *error)
3113{
3114 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
3115 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3116 error);
3117}
3118
3119static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
3120 struct kvm_vcpu *vcpu, u32 *error)
3121{
3122 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
3123}
3124
Hannes Edercded19f2009-02-21 02:19:13 +01003125static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
Gleb Natapov1871c602010-02-10 14:21:32 +02003126 struct kvm_vcpu *vcpu, u32 *error)
Izik Eidus77c20022008-12-29 01:42:19 +02003127{
3128 void *data = val;
3129 int r = X86EMUL_CONTINUE;
3130
3131 while (bytes) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003132 gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error);
Izik Eidus77c20022008-12-29 01:42:19 +02003133 unsigned offset = addr & (PAGE_SIZE-1);
3134 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
3135 int ret;
3136
3137 if (gpa == UNMAPPED_GVA) {
3138 r = X86EMUL_PROPAGATE_FAULT;
3139 goto out;
3140 }
3141 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
3142 if (ret < 0) {
3143 r = X86EMUL_UNHANDLEABLE;
3144 goto out;
3145 }
3146
3147 bytes -= towrite;
3148 data += towrite;
3149 addr += towrite;
3150 }
3151out:
3152 return r;
3153}
3154
Carsten Ottebbd9b642007-10-30 18:44:21 +01003155
Carsten Ottebbd9b642007-10-30 18:44:21 +01003156static int emulator_read_emulated(unsigned long addr,
3157 void *val,
3158 unsigned int bytes,
3159 struct kvm_vcpu *vcpu)
3160{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003161 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003162 u32 error_code;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003163
3164 if (vcpu->mmio_read_completed) {
3165 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03003166 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
3167 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003168 vcpu->mmio_read_completed = 0;
3169 return X86EMUL_CONTINUE;
3170 }
3171
Gleb Natapov1871c602010-02-10 14:21:32 +02003172 gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
3173
3174 if (gpa == UNMAPPED_GVA) {
3175 kvm_inject_page_fault(vcpu, addr, error_code);
3176 return X86EMUL_PROPAGATE_FAULT;
3177 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01003178
3179 /* For APIC access vmexit */
3180 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3181 goto mmio;
3182
Gleb Natapov1871c602010-02-10 14:21:32 +02003183 if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
Izik Eidus77c20022008-12-29 01:42:19 +02003184 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003185 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003186
3187mmio:
3188 /*
3189 * Is this MMIO handled locally?
3190 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03003191 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
3192 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003193 return X86EMUL_CONTINUE;
3194 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03003195
3196 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003197
3198 vcpu->mmio_needed = 1;
3199 vcpu->mmio_phys_addr = gpa;
3200 vcpu->mmio_size = bytes;
3201 vcpu->mmio_is_write = 0;
3202
3203 return X86EMUL_UNHANDLEABLE;
3204}
3205
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003206int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02003207 const void *val, int bytes)
3208{
3209 int ret;
3210
3211 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
3212 if (ret < 0)
3213 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02003214 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02003215 return 1;
3216}
3217
Carsten Ottebbd9b642007-10-30 18:44:21 +01003218static int emulator_write_emulated_onepage(unsigned long addr,
3219 const void *val,
3220 unsigned int bytes,
3221 struct kvm_vcpu *vcpu)
3222{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003223 gpa_t gpa;
Gleb Natapov1871c602010-02-10 14:21:32 +02003224 u32 error_code;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003225
Gleb Natapov1871c602010-02-10 14:21:32 +02003226 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003227
3228 if (gpa == UNMAPPED_GVA) {
Gleb Natapov1871c602010-02-10 14:21:32 +02003229 kvm_inject_page_fault(vcpu, addr, error_code);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003230 return X86EMUL_PROPAGATE_FAULT;
3231 }
3232
3233 /* For APIC access vmexit */
3234 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3235 goto mmio;
3236
3237 if (emulator_write_phys(vcpu, gpa, val, bytes))
3238 return X86EMUL_CONTINUE;
3239
3240mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03003241 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003242 /*
3243 * Is this MMIO handled locally?
3244 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003245 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01003246 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003247
3248 vcpu->mmio_needed = 1;
3249 vcpu->mmio_phys_addr = gpa;
3250 vcpu->mmio_size = bytes;
3251 vcpu->mmio_is_write = 1;
3252 memcpy(vcpu->mmio_data, val, bytes);
3253
3254 return X86EMUL_CONTINUE;
3255}
3256
3257int emulator_write_emulated(unsigned long addr,
3258 const void *val,
3259 unsigned int bytes,
3260 struct kvm_vcpu *vcpu)
3261{
3262 /* Crossing a page boundary? */
3263 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
3264 int rc, now;
3265
3266 now = -addr & ~PAGE_MASK;
3267 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
3268 if (rc != X86EMUL_CONTINUE)
3269 return rc;
3270 addr += now;
3271 val += now;
3272 bytes -= now;
3273 }
3274 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
3275}
3276EXPORT_SYMBOL_GPL(emulator_write_emulated);
3277
3278static int emulator_cmpxchg_emulated(unsigned long addr,
3279 const void *old,
3280 const void *new,
3281 unsigned int bytes,
3282 struct kvm_vcpu *vcpu)
3283{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02003284 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003285#ifndef CONFIG_X86_64
3286 /* guests cmpxchg8b have to be emulated atomically */
3287 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003288 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003289 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003290 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003291 u64 val;
3292
Gleb Natapov1871c602010-02-10 14:21:32 +02003293 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003294
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003295 if (gpa == UNMAPPED_GVA ||
3296 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
3297 goto emul_write;
3298
3299 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3300 goto emul_write;
3301
3302 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003303
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003304 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003305
Andrew Mortonc0b49b02008-02-04 22:27:18 -08003306 kaddr = kmap_atomic(page, KM_USER0);
3307 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
3308 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003309 kvm_release_page_dirty(page);
3310 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003311emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05003312#endif
3313
Carsten Ottebbd9b642007-10-30 18:44:21 +01003314 return emulator_write_emulated(addr, new, bytes, vcpu);
3315}
3316
3317static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3318{
3319 return kvm_x86_ops->get_segment_base(vcpu, seg);
3320}
3321
3322int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3323{
Marcelo Tosattia7052892008-09-23 13:18:35 -03003324 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003325 return X86EMUL_CONTINUE;
3326}
3327
3328int emulate_clts(struct kvm_vcpu *vcpu)
3329{
Avi Kivity4d4ec082009-12-29 18:07:30 +02003330 kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity6b52d182010-01-21 15:31:47 +02003331 kvm_x86_ops->fpu_activate(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003332 return X86EMUL_CONTINUE;
3333}
3334
3335int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3336{
Jan Kiszkac76de352010-01-20 18:20:20 +01003337 return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003338}
3339
3340int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3341{
3342 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003343
Jan Kiszkac76de352010-01-20 18:20:20 +01003344 return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003345}
3346
3347void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3348{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003349 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003350 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003351 unsigned long rip_linear;
3352
Avi Kivityf76c7102008-06-13 22:45:42 +03003353 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003354 return;
3355
Glauber Costa25be4602008-06-10 10:46:53 -03003356 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3357
Gleb Natapov1871c602010-02-10 14:21:32 +02003358 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003359
3360 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3361 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003362}
3363EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3364
Harvey Harrison14af3f32008-02-19 10:25:50 -08003365static struct x86_emulate_ops emulate_ops = {
Gleb Natapov1871c602010-02-10 14:21:32 +02003366 .read_std = kvm_read_guest_virt_system,
3367 .fetch = kvm_fetch_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003368 .read_emulated = emulator_read_emulated,
3369 .write_emulated = emulator_write_emulated,
3370 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3371};
3372
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003373static void cache_all_regs(struct kvm_vcpu *vcpu)
3374{
3375 kvm_register_read(vcpu, VCPU_REGS_RAX);
3376 kvm_register_read(vcpu, VCPU_REGS_RSP);
3377 kvm_register_read(vcpu, VCPU_REGS_RIP);
3378 vcpu->arch.regs_dirty = ~0;
3379}
3380
Carsten Ottebbd9b642007-10-30 18:44:21 +01003381int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003382 unsigned long cr2,
3383 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003384 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003385{
Glauber Costa310b5d32009-05-12 16:21:06 -04003386 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003387 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003388 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003389
Avi Kivity26eef702008-07-03 14:59:22 +03003390 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003391 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003392 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003393 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003394 * instead of direct ->regs accesses, can save hundred cycles
3395 * on Intel for instructions that don't read/change RSP, for
3396 * for example.
3397 */
3398 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003399
3400 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003401 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003402
Sheng Yang571008d2008-01-02 14:49:22 +08003403 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003404 int cs_db, cs_l;
3405 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3406
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003407 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka91586a32009-10-05 13:07:21 +02003408 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003409 vcpu->arch.emulate_ctxt.mode =
Gleb Natapova0044752010-02-10 14:21:31 +02003410 (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003411 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Gleb Natapova0044752010-02-10 14:21:31 +02003412 ? X86EMUL_MODE_VM86 : cs_l
Carsten Ottebbd9b642007-10-30 18:44:21 +01003413 ? X86EMUL_MODE_PROT64 : cs_db
3414 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3415
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003416 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003417
Andre Przywara0cb57622009-06-17 15:50:31 +02003418 /* Only allow emulation of specific instructions on #UD
3419 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003420 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003421 if (emulation_type & EMULTYPE_TRAP_UD) {
3422 if (!c->twobyte)
3423 return EMULATE_FAIL;
3424 switch (c->b) {
3425 case 0x01: /* VMMCALL */
3426 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3427 return EMULATE_FAIL;
3428 break;
3429 case 0x34: /* sysenter */
3430 case 0x35: /* sysexit */
3431 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3432 return EMULATE_FAIL;
3433 break;
3434 case 0x05: /* syscall */
3435 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3436 return EMULATE_FAIL;
3437 break;
3438 default:
3439 return EMULATE_FAIL;
3440 }
3441
3442 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3443 return EMULATE_FAIL;
3444 }
Sheng Yang571008d2008-01-02 14:49:22 +08003445
Avi Kivityf2b57562007-11-18 15:17:51 +02003446 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003447 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003448 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003449 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3450 return EMULATE_DONE;
3451 return EMULATE_FAIL;
3452 }
3453 }
3454
Gleb Natapovba8afb62009-04-12 13:36:57 +03003455 if (emulation_type & EMULTYPE_SKIP) {
3456 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3457 return EMULATE_DONE;
3458 }
3459
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003460 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003461 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3462
3463 if (r == 0)
3464 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003465
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003466 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003467 return EMULATE_DO_MMIO;
3468
3469 if ((r || vcpu->mmio_is_write) && run) {
3470 run->exit_reason = KVM_EXIT_MMIO;
3471 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3472 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3473 run->mmio.len = vcpu->mmio_size;
3474 run->mmio.is_write = vcpu->mmio_is_write;
3475 }
3476
3477 if (r) {
3478 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3479 return EMULATE_DONE;
3480 if (!vcpu->mmio_needed) {
3481 kvm_report_emulation_failure(vcpu, "mmio");
3482 return EMULATE_FAIL;
3483 }
3484 return EMULATE_DO_MMIO;
3485 }
3486
Jan Kiszka91586a32009-10-05 13:07:21 +02003487 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003488
3489 if (vcpu->mmio_is_write) {
3490 vcpu->mmio_needed = 0;
3491 return EMULATE_DO_MMIO;
3492 }
3493
3494 return EMULATE_DONE;
3495}
3496EXPORT_SYMBOL_GPL(emulate_instruction);
3497
Carsten Ottede7d7892007-10-30 18:44:25 +01003498static int pio_copy_data(struct kvm_vcpu *vcpu)
3499{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003500 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003501 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003502 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003503 int ret;
Gleb Natapov1871c602010-02-10 14:21:32 +02003504 u32 error_code;
Carsten Ottede7d7892007-10-30 18:44:25 +01003505
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003506 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3507 if (vcpu->arch.pio.in)
Gleb Natapov1871c602010-02-10 14:21:32 +02003508 ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code);
Carsten Ottede7d7892007-10-30 18:44:25 +01003509 else
Gleb Natapov1871c602010-02-10 14:21:32 +02003510 ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code);
3511
3512 if (ret == X86EMUL_PROPAGATE_FAULT)
3513 kvm_inject_page_fault(vcpu, q, error_code);
3514
Izik Eidus0f346072008-12-29 01:42:20 +02003515 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003516}
3517
3518int complete_pio(struct kvm_vcpu *vcpu)
3519{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003520 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003521 long delta;
3522 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003523 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003524
3525 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003526 if (io->in) {
3527 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3528 memcpy(&val, vcpu->arch.pio_data, io->size);
3529 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3530 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003531 } else {
3532 if (io->in) {
3533 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003534 if (r)
Gleb Natapov1871c602010-02-10 14:21:32 +02003535 goto out;
Carsten Ottede7d7892007-10-30 18:44:25 +01003536 }
3537
3538 delta = 1;
3539 if (io->rep) {
3540 delta *= io->cur_count;
3541 /*
3542 * The size of the register should really depend on
3543 * current address size.
3544 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003545 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3546 val -= delta;
3547 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003548 }
3549 if (io->down)
3550 delta = -delta;
3551 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003552 if (io->in) {
3553 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3554 val += delta;
3555 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3556 } else {
3557 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3558 val += delta;
3559 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3560 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003561 }
Gleb Natapov1871c602010-02-10 14:21:32 +02003562out:
Carsten Ottede7d7892007-10-30 18:44:25 +01003563 io->count -= io->cur_count;
3564 io->cur_count = 0;
3565
3566 return 0;
3567}
3568
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003569static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003570{
3571 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003572 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003573
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003574 if (vcpu->arch.pio.in)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003575 r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003576 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003577 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003578 r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
3579 vcpu->arch.pio.port, vcpu->arch.pio.size,
3580 pd);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003581 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003582}
3583
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003584static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003585{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003586 struct kvm_pio_request *io = &vcpu->arch.pio;
3587 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003588 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003589
Carsten Ottede7d7892007-10-30 18:44:25 +01003590 for (i = 0; i < io->cur_count; i++) {
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02003591 if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003592 io->port, io->size, pd)) {
3593 r = -EOPNOTSUPP;
3594 break;
3595 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003596 pd += io->size;
3597 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003598 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003599}
3600
Avi Kivity851ba692009-08-24 11:10:17 +03003601int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003602{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003603 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003604
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003605 trace_kvm_pio(!in, port, size, 1);
3606
Carsten Ottede7d7892007-10-30 18:44:25 +01003607 vcpu->run->exit_reason = KVM_EXIT_IO;
3608 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003609 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003610 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003611 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3612 vcpu->run->io.port = vcpu->arch.pio.port = port;
3613 vcpu->arch.pio.in = in;
3614 vcpu->arch.pio.string = 0;
3615 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003616 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003617
Takuya Yoshikawa1976d2d2010-02-05 17:52:46 +09003618 if (!vcpu->arch.pio.in) {
3619 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3620 memcpy(vcpu->arch.pio_data, &val, 4);
3621 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003622
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003623 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003624 complete_pio(vcpu);
3625 return 1;
3626 }
3627 return 0;
3628}
3629EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3630
Avi Kivity851ba692009-08-24 11:10:17 +03003631int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003632 int size, unsigned long count, int down,
3633 gva_t address, int rep, unsigned port)
3634{
3635 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003636 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003637
Gleb Natapovf850e2e2010-02-10 14:21:33 +02003638 trace_kvm_pio(!in, port, size, count);
3639
Carsten Ottede7d7892007-10-30 18:44:25 +01003640 vcpu->run->exit_reason = KVM_EXIT_IO;
3641 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003642 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003643 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003644 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3645 vcpu->run->io.port = vcpu->arch.pio.port = port;
3646 vcpu->arch.pio.in = in;
3647 vcpu->arch.pio.string = 1;
3648 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003649 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003650
3651 if (!count) {
3652 kvm_x86_ops->skip_emulated_instruction(vcpu);
3653 return 1;
3654 }
3655
3656 if (!down)
3657 in_page = PAGE_SIZE - offset_in_page(address);
3658 else
3659 in_page = offset_in_page(address) + size;
3660 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003661 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003662 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003663 if (down) {
3664 /*
3665 * String I/O in reverse. Yuck. Kill the guest, fix later.
3666 */
3667 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003668 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003669 return 1;
3670 }
3671 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003672 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003673
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003674 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003675 kvm_x86_ops->skip_emulated_instruction(vcpu);
3676
Izik Eidus0f346072008-12-29 01:42:20 +02003677 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003678
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003679 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003680 /* string PIO write */
3681 ret = pio_copy_data(vcpu);
Gleb Natapov1871c602010-02-10 14:21:32 +02003682 if (ret == X86EMUL_PROPAGATE_FAULT)
Izik Eidus0f346072008-12-29 01:42:20 +02003683 return 1;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003684 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003685 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003686 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003687 ret = 1;
3688 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003689 }
3690 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003691
3692 return ret;
3693}
3694EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3695
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003696static void bounce_off(void *info)
3697{
3698 /* nothing */
3699}
3700
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003701static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3702 void *data)
3703{
3704 struct cpufreq_freqs *freq = data;
3705 struct kvm *kvm;
3706 struct kvm_vcpu *vcpu;
3707 int i, send_ipi = 0;
3708
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003709 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3710 return 0;
3711 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3712 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003713 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003714
3715 spin_lock(&kvm_lock);
3716 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003717 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003718 if (vcpu->cpu != freq->cpu)
3719 continue;
3720 if (!kvm_request_guest_time_update(vcpu))
3721 continue;
3722 if (vcpu->cpu != smp_processor_id())
3723 send_ipi++;
3724 }
3725 }
3726 spin_unlock(&kvm_lock);
3727
3728 if (freq->old < freq->new && send_ipi) {
3729 /*
3730 * We upscale the frequency. Must make the guest
3731 * doesn't see old kvmclock values while running with
3732 * the new frequency, otherwise we risk the guest sees
3733 * time go backwards.
3734 *
3735 * In case we update the frequency for another cpu
3736 * (which might be in guest context) send an interrupt
3737 * to kick the cpu out of guest context. Next time
3738 * guest context is entered kvmclock will be updated,
3739 * so the guest will not see stale values.
3740 */
3741 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3742 }
3743 return 0;
3744}
3745
3746static struct notifier_block kvmclock_cpufreq_notifier_block = {
3747 .notifier_call = kvmclock_cpufreq_notifier
3748};
3749
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003750static void kvm_timer_init(void)
3751{
3752 int cpu;
3753
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003754 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003755 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3756 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003757 for_each_online_cpu(cpu) {
3758 unsigned long khz = cpufreq_get(cpu);
3759 if (!khz)
3760 khz = tsc_khz;
3761 per_cpu(cpu_tsc_khz, cpu) = khz;
3762 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003763 } else {
3764 for_each_possible_cpu(cpu)
3765 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003766 }
3767}
3768
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003769static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
3770
3771static int kvm_is_in_guest(void)
3772{
3773 return percpu_read(current_vcpu) != NULL;
3774}
3775
3776static int kvm_is_user_mode(void)
3777{
3778 int user_mode = 3;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003779
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003780 if (percpu_read(current_vcpu))
3781 user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003782
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003783 return user_mode != 0;
3784}
3785
3786static unsigned long kvm_get_guest_ip(void)
3787{
3788 unsigned long ip = 0;
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003789
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003790 if (percpu_read(current_vcpu))
3791 ip = kvm_rip_read(percpu_read(current_vcpu));
Zhang, Yanmindcf46b92010-04-20 10:13:58 +08003792
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003793 return ip;
3794}
3795
3796static struct perf_guest_info_callbacks kvm_guest_cbs = {
3797 .is_in_guest = kvm_is_in_guest,
3798 .is_user_mode = kvm_is_user_mode,
3799 .get_guest_ip = kvm_get_guest_ip,
3800};
3801
3802void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
3803{
3804 percpu_write(current_vcpu, vcpu);
3805}
3806EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
3807
3808void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
3809{
3810 percpu_write(current_vcpu, NULL);
3811}
3812EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
3813
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003814int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003815{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003816 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003817 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3818
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003819 if (kvm_x86_ops) {
3820 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003821 r = -EEXIST;
3822 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003823 }
3824
3825 if (!ops->cpu_has_kvm_support()) {
3826 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003827 r = -EOPNOTSUPP;
3828 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003829 }
3830 if (ops->disabled_by_bios()) {
3831 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003832 r = -EOPNOTSUPP;
3833 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003834 }
3835
Avi Kivity97db56c2008-01-13 13:23:56 +02003836 r = kvm_mmu_module_init();
3837 if (r)
3838 goto out;
3839
3840 kvm_init_msr_list();
3841
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003842 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003843 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003844 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3845 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003846 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003847
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003848 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003849
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003850 perf_register_guest_info_callbacks(&kvm_guest_cbs);
3851
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003852 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003853
3854out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003855 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003856}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003857
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003858void kvm_arch_exit(void)
3859{
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003860 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
3861
Jan Kiszka888d2562009-04-17 19:24:58 +02003862 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3863 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3864 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003865 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003866 kvm_mmu_module_exit();
3867}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003868
Hollis Blanchard8776e512007-10-31 17:24:24 -05003869int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3870{
3871 ++vcpu->stat.halt_exits;
3872 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003873 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003874 return 1;
3875 } else {
3876 vcpu->run->exit_reason = KVM_EXIT_HLT;
3877 return 0;
3878 }
3879}
3880EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3881
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003882static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3883 unsigned long a1)
3884{
3885 if (is_long_mode(vcpu))
3886 return a0;
3887 else
3888 return a0 | ((gpa_t)a1 << 32);
3889}
3890
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003891int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
3892{
3893 u64 param, ingpa, outgpa, ret;
3894 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
3895 bool fast, longmode;
3896 int cs_db, cs_l;
3897
3898 /*
3899 * hypercall generates UD from non zero cpl and real mode
3900 * per HYPER-V spec
3901 */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003902 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003903 kvm_queue_exception(vcpu, UD_VECTOR);
3904 return 0;
3905 }
3906
3907 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3908 longmode = is_long_mode(vcpu) && cs_l == 1;
3909
3910 if (!longmode) {
Gleb Natapovccd46932010-01-19 15:06:38 +02003911 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
3912 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
3913 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
3914 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
3915 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
3916 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003917 }
3918#ifdef CONFIG_X86_64
3919 else {
3920 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
3921 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
3922 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
3923 }
3924#endif
3925
3926 code = param & 0xffff;
3927 fast = (param >> 16) & 0x1;
3928 rep_cnt = (param >> 32) & 0xfff;
3929 rep_idx = (param >> 48) & 0xfff;
3930
3931 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
3932
Gleb Natapovc25bc162010-01-17 15:51:24 +02003933 switch (code) {
3934 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
3935 kvm_vcpu_on_spin(vcpu);
3936 break;
3937 default:
3938 res = HV_STATUS_INVALID_HYPERCALL_CODE;
3939 break;
3940 }
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003941
3942 ret = res | (((u64)rep_done & 0xfff) << 32);
3943 if (longmode) {
3944 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3945 } else {
3946 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
3947 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
3948 }
3949
3950 return 1;
3951}
3952
Hollis Blanchard8776e512007-10-31 17:24:24 -05003953int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3954{
3955 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003956 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003957
Gleb Natapov55cd8e52010-01-17 15:51:22 +02003958 if (kvm_hv_hypercall_enabled(vcpu->kvm))
3959 return kvm_hv_hypercall(vcpu);
3960
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003961 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3962 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3963 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3964 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3965 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003966
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003967 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003968
Hollis Blanchard8776e512007-10-31 17:24:24 -05003969 if (!is_long_mode(vcpu)) {
3970 nr &= 0xFFFFFFFF;
3971 a0 &= 0xFFFFFFFF;
3972 a1 &= 0xFFFFFFFF;
3973 a2 &= 0xFFFFFFFF;
3974 a3 &= 0xFFFFFFFF;
3975 }
3976
Jan Kiszka07708c42009-08-03 18:43:28 +02003977 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3978 ret = -KVM_EPERM;
3979 goto out;
3980 }
3981
Hollis Blanchard8776e512007-10-31 17:24:24 -05003982 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003983 case KVM_HC_VAPIC_POLL_IRQ:
3984 ret = 0;
3985 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003986 case KVM_HC_MMU_OP:
3987 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3988 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003989 default:
3990 ret = -KVM_ENOSYS;
3991 break;
3992 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003993out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003994 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303995 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003996 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003997}
3998EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3999
4000int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
4001{
4002 char instruction[3];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004003 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004004
Hollis Blanchard8776e512007-10-31 17:24:24 -05004005 /*
4006 * Blow out the MMU to ensure that no other VCPU has an active mapping
4007 * to ensure that the updated hypercall appears atomically across all
4008 * VCPUs.
4009 */
4010 kvm_mmu_zap_all(vcpu->kvm);
4011
Hollis Blanchard8776e512007-10-31 17:24:24 -05004012 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004013
Takuya Yoshikawa7edcfac2010-02-01 22:11:52 +09004014 return emulator_write_emulated(rip, instruction, 3, vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004015}
4016
4017static u64 mk_cr_64(u64 curr_cr, u32 new_val)
4018{
4019 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4020}
4021
4022void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4023{
4024 struct descriptor_table dt = { limit, base };
4025
4026 kvm_x86_ops->set_gdt(vcpu, &dt);
4027}
4028
4029void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
4030{
4031 struct descriptor_table dt = { limit, base };
4032
4033 kvm_x86_ops->set_idt(vcpu, &dt);
4034}
4035
4036void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
4037 unsigned long *rflags)
4038{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004039 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02004040 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004041}
4042
4043unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
4044{
Joerg Roedel54e445c2008-04-30 17:56:02 +02004045 unsigned long value;
4046
Hollis Blanchard8776e512007-10-31 17:24:24 -05004047 switch (cr) {
4048 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004049 value = kvm_read_cr0(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004050 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004051 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004052 value = vcpu->arch.cr2;
4053 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004054 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004055 value = vcpu->arch.cr3;
4056 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004057 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004058 value = kvm_read_cr4(vcpu);
Joerg Roedel54e445c2008-04-30 17:56:02 +02004059 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004060 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02004061 value = kvm_get_cr8(vcpu);
4062 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004063 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004064 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004065 return 0;
4066 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02004067
4068 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004069}
4070
4071void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
4072 unsigned long *rflags)
4073{
4074 switch (cr) {
4075 case 0:
Avi Kivity4d4ec082009-12-29 18:07:30 +02004076 kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
Jan Kiszka91586a32009-10-05 13:07:21 +02004077 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004078 break;
4079 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004080 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004081 break;
4082 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004083 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004084 break;
4085 case 4:
Avi Kivityfc78f512009-12-07 12:16:48 +02004086 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004087 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004088 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004089 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01004090 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004091 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004092 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004093 }
4094}
4095
Dan Kenigsberg07716712007-11-21 17:10:04 +02004096static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
4097{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004098 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
4099 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02004100
4101 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
4102 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08004103 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004104 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004105 if (ej->function == e->function) {
4106 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
4107 return j;
4108 }
4109 }
4110 return 0; /* silence gcc, even though control never reaches here */
4111}
4112
4113/* find an entry with matching function, matching index (if needed), and that
4114 * should be read next (if it's stateful) */
4115static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
4116 u32 function, u32 index)
4117{
4118 if (e->function != function)
4119 return 0;
4120 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
4121 return 0;
4122 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00004123 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02004124 return 0;
4125 return 1;
4126}
4127
Alexander Grafd8017472008-11-25 20:17:11 +01004128struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
4129 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05004130{
4131 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01004132 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05004133
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004134 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01004135 struct kvm_cpuid_entry2 *e;
4136
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004137 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02004138 if (is_matching_cpuid_entry(e, function, index)) {
4139 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
4140 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004141 best = e;
4142 break;
4143 }
4144 /*
4145 * Both basic or both extended?
4146 */
4147 if (((e->function ^ function) & 0x80000000) == 0)
4148 if (!best || e->function > best->function)
4149 best = e;
4150 }
Alexander Grafd8017472008-11-25 20:17:11 +01004151 return best;
4152}
Sheng Yang0e851882009-12-18 16:48:46 +08004153EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
Alexander Grafd8017472008-11-25 20:17:11 +01004154
Dong, Eddie82725b22009-03-30 16:21:08 +08004155int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
4156{
4157 struct kvm_cpuid_entry2 *best;
4158
4159 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
4160 if (best)
4161 return best->eax & 0xff;
4162 return 36;
4163}
4164
Alexander Grafd8017472008-11-25 20:17:11 +01004165void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
4166{
4167 u32 function, index;
4168 struct kvm_cpuid_entry2 *best;
4169
4170 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
4171 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4172 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
4173 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
4174 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
4175 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
4176 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004177 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004178 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
4179 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
4180 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
4181 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05004182 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05004183 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004184 trace_kvm_cpuid(function,
4185 kvm_register_read(vcpu, VCPU_REGS_RAX),
4186 kvm_register_read(vcpu, VCPU_REGS_RBX),
4187 kvm_register_read(vcpu, VCPU_REGS_RCX),
4188 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05004189}
4190EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004191
4192/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004193 * Check if userspace requested an interrupt window, and that the
4194 * interrupt window is open.
4195 *
4196 * No need to exit to userspace if we already have an interrupt queued.
4197 */
Avi Kivity851ba692009-08-24 11:10:17 +03004198static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004199{
Gleb Natapov80618232009-04-21 17:44:56 +03004200 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004201 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03004202 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004203}
4204
Avi Kivity851ba692009-08-24 11:10:17 +03004205static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004206{
Avi Kivity851ba692009-08-24 11:10:17 +03004207 struct kvm_run *kvm_run = vcpu->run;
4208
Jan Kiszka91586a32009-10-05 13:07:21 +02004209 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004210 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004211 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01004212 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004213 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01004214 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004215 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03004216 kvm_arch_interrupt_allowed(vcpu) &&
4217 !kvm_cpu_has_interrupt(vcpu) &&
4218 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004219}
4220
Avi Kivityb93463a2007-10-25 16:52:32 +02004221static void vapic_enter(struct kvm_vcpu *vcpu)
4222{
4223 struct kvm_lapic *apic = vcpu->arch.apic;
4224 struct page *page;
4225
4226 if (!apic || !apic->vapic_addr)
4227 return;
4228
4229 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004230
4231 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02004232}
4233
4234static void vapic_exit(struct kvm_vcpu *vcpu)
4235{
4236 struct kvm_lapic *apic = vcpu->arch.apic;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004237 int idx;
Avi Kivityb93463a2007-10-25 16:52:32 +02004238
4239 if (!apic || !apic->vapic_addr)
4240 return;
4241
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004242 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004243 kvm_release_page_dirty(apic->vapic_page);
4244 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004245 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivityb93463a2007-10-25 16:52:32 +02004246}
4247
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004248static void update_cr8_intercept(struct kvm_vcpu *vcpu)
4249{
4250 int max_irr, tpr;
4251
4252 if (!kvm_x86_ops->update_cr8_intercept)
4253 return;
4254
Avi Kivity88c808f2009-08-17 22:49:40 +03004255 if (!vcpu->arch.apic)
4256 return;
4257
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004258 if (!vcpu->arch.apic->vapic_addr)
4259 max_irr = kvm_lapic_find_highest_irr(vcpu);
4260 else
4261 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004262
4263 if (max_irr != -1)
4264 max_irr >>= 4;
4265
4266 tpr = kvm_lapic_get_cr8(vcpu);
4267
4268 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
4269}
4270
Avi Kivity851ba692009-08-24 11:10:17 +03004271static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004272{
4273 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03004274 if (vcpu->arch.exception.pending) {
4275 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
4276 vcpu->arch.exception.has_error_code,
4277 vcpu->arch.exception.error_code);
4278 return;
4279 }
4280
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004281 if (vcpu->arch.nmi_injected) {
4282 kvm_x86_ops->set_nmi(vcpu);
4283 return;
4284 }
4285
4286 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004287 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004288 return;
4289 }
4290
4291 /* try to inject new event if pending */
4292 if (vcpu->arch.nmi_pending) {
4293 if (kvm_x86_ops->nmi_allowed(vcpu)) {
4294 vcpu->arch.nmi_pending = false;
4295 vcpu->arch.nmi_injected = true;
4296 kvm_x86_ops->set_nmi(vcpu);
4297 }
4298 } else if (kvm_cpu_has_interrupt(vcpu)) {
4299 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004300 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
4301 false);
4302 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004303 }
4304 }
4305}
4306
Avi Kivity851ba692009-08-24 11:10:17 +03004307static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004308{
4309 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004310 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004311 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004312
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05004313 if (vcpu->requests)
4314 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
4315 kvm_mmu_unload(vcpu);
4316
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004317 r = kvm_mmu_reload(vcpu);
4318 if (unlikely(r))
4319 goto out;
4320
Avi Kivity2f52d582008-01-16 12:49:30 +02004321 if (vcpu->requests) {
4322 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03004323 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01004324 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
4325 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03004326 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
4327 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03004328 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
4329 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02004330 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
4331 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004332 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02004333 r = 0;
4334 goto out;
4335 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004336 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004337 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01004338 r = 0;
4339 goto out;
4340 }
Avi Kivity02daab22009-12-30 12:40:26 +02004341 if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
4342 vcpu->fpu_active = 0;
4343 kvm_x86_ops->fpu_deactivate(vcpu);
4344 }
Avi Kivity2f52d582008-01-16 12:49:30 +02004345 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004346
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004347 preempt_disable();
4348
4349 kvm_x86_ops->prepare_guest_switch(vcpu);
Avi Kivity2608d7a2010-01-21 15:31:45 +02004350 if (vcpu->fpu_active)
4351 kvm_load_guest_fpu(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004352
4353 local_irq_disable();
4354
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004355 clear_bit(KVM_REQ_KICK, &vcpu->requests);
4356 smp_mb__after_clear_bit();
4357
Marcelo Tosattid7690172008-09-08 15:23:48 -03004358 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03004359 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c142802008-01-15 18:27:32 +02004360 local_irq_enable();
4361 preempt_enable();
4362 r = 1;
4363 goto out;
4364 }
4365
Avi Kivity851ba692009-08-24 11:10:17 +03004366 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004367
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03004368 /* enable NMI/IRQ window open exits if needed */
4369 if (vcpu->arch.nmi_pending)
4370 kvm_x86_ops->enable_nmi_window(vcpu);
4371 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
4372 kvm_x86_ops->enable_irq_window(vcpu);
4373
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004374 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03004375 update_cr8_intercept(vcpu);
4376 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004377 }
Avi Kivityb93463a2007-10-25 16:52:32 +02004378
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004379 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004380
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004381 kvm_guest_enter();
4382
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004383 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004384 set_debugreg(0, 7);
4385 set_debugreg(vcpu->arch.eff_db[0], 0);
4386 set_debugreg(vcpu->arch.eff_db[1], 1);
4387 set_debugreg(vcpu->arch.eff_db[2], 2);
4388 set_debugreg(vcpu->arch.eff_db[3], 3);
4389 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004390
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004391 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03004392 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004393
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004394 /*
4395 * If the guest has used debug registers, at least dr7
4396 * will be disabled while returning to the host.
4397 * If we don't have active breakpoints in the host, we don't
4398 * care about the messed up debug address registers. But if
4399 * we have some of them active, restore the old state.
4400 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01004401 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004402 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004403
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004404 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004405 local_irq_enable();
4406
4407 ++vcpu->stat.exits;
4408
4409 /*
4410 * We must have an instruction between local_irq_enable() and
4411 * kvm_guest_exit(), so the timer interrupt isn't delayed by
4412 * the interrupt shadow. The stat.exits increment will do nicely.
4413 * But we need to prevent reordering, hence this barrier():
4414 */
4415 barrier();
4416
4417 kvm_guest_exit();
4418
4419 preempt_enable();
4420
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004421 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004422
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004423 /*
4424 * Profile KVM exit RIPs:
4425 */
4426 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004427 unsigned long rip = kvm_rip_read(vcpu);
4428 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004429 }
4430
Avi Kivity298101d2007-11-25 13:41:11 +02004431
Avi Kivityb93463a2007-10-25 16:52:32 +02004432 kvm_lapic_sync_from_vapic(vcpu);
4433
Avi Kivity851ba692009-08-24 11:10:17 +03004434 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004435out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004436 return r;
4437}
4438
Gleb Natapov09cec752009-03-23 15:11:44 +02004439
Avi Kivity851ba692009-08-24 11:10:17 +03004440static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03004441{
4442 int r;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004443 struct kvm *kvm = vcpu->kvm;
Marcelo Tosattid7690172008-09-08 15:23:48 -03004444
4445 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02004446 pr_debug("vcpu %d received sipi with vector # %x\n",
4447 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004448 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02004449 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004450 if (r)
4451 return r;
4452 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004453 }
4454
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004455 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004456 vapic_enter(vcpu);
4457
4458 r = 1;
4459 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03004460 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03004461 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004462 else {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004463 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004464 kvm_vcpu_block(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004465 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004466 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02004467 {
4468 switch(vcpu->arch.mp_state) {
4469 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004470 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004471 KVM_MP_STATE_RUNNABLE;
4472 case KVM_MP_STATE_RUNNABLE:
4473 break;
4474 case KVM_MP_STATE_SIPI_RECEIVED:
4475 default:
4476 r = -EINTR;
4477 break;
4478 }
4479 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004480 }
4481
Gleb Natapov09cec752009-03-23 15:11:44 +02004482 if (r <= 0)
4483 break;
4484
4485 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4486 if (kvm_cpu_has_pending_timer(vcpu))
4487 kvm_inject_pending_timer_irqs(vcpu);
4488
Avi Kivity851ba692009-08-24 11:10:17 +03004489 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004490 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004491 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004492 ++vcpu->stat.request_irq_exits;
4493 }
4494 if (signal_pending(current)) {
4495 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004496 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004497 ++vcpu->stat.signal_exits;
4498 }
4499 if (need_resched()) {
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004500 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Gleb Natapov09cec752009-03-23 15:11:44 +02004501 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004502 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004503 }
4504 }
4505
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004506 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
Avi Kivity851ba692009-08-24 11:10:17 +03004507 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004508
Avi Kivityb93463a2007-10-25 16:52:32 +02004509 vapic_exit(vcpu);
4510
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004511 return r;
4512}
4513
4514int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4515{
4516 int r;
4517 sigset_t sigsaved;
4518
4519 vcpu_load(vcpu);
4520
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004521 if (vcpu->sigset_active)
4522 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4523
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004524 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4525 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004526 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004527 r = -EAGAIN;
4528 goto out;
4529 }
4530
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004531 /* re-sync apic's tpr */
4532 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004533 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004534
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004535 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004536 r = complete_pio(vcpu);
4537 if (r)
4538 goto out;
4539 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004540 if (vcpu->mmio_needed) {
4541 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4542 vcpu->mmio_read_completed = 1;
4543 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004544
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004545 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity851ba692009-08-24 11:10:17 +03004546 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004547 EMULTYPE_NO_DECODE);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02004548 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004549 if (r == EMULATE_DO_MMIO) {
4550 /*
4551 * Read-modify-write. Back to userspace.
4552 */
4553 r = 0;
4554 goto out;
4555 }
4556 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004557 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4558 kvm_register_write(vcpu, VCPU_REGS_RAX,
4559 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004560
Avi Kivity851ba692009-08-24 11:10:17 +03004561 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004562
4563out:
4564 if (vcpu->sigset_active)
4565 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4566
4567 vcpu_put(vcpu);
4568 return r;
4569}
4570
4571int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4572{
4573 vcpu_load(vcpu);
4574
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004575 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4576 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4577 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4578 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4579 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4580 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4581 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4582 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004583#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004584 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4585 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4586 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4587 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4588 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4589 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4590 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4591 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004592#endif
4593
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004594 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004595 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004596
4597 vcpu_put(vcpu);
4598
4599 return 0;
4600}
4601
4602int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4603{
4604 vcpu_load(vcpu);
4605
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004606 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4607 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4608 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4609 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4610 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4611 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4612 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4613 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004614#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004615 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4616 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4617 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4618 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4619 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4620 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4621 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4622 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004623#endif
4624
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004625 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004626 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004627
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004628 vcpu->arch.exception.pending = false;
4629
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004630 vcpu_put(vcpu);
4631
4632 return 0;
4633}
4634
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004635void kvm_get_segment(struct kvm_vcpu *vcpu,
4636 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004637{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004638 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004639}
4640
4641void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4642{
4643 struct kvm_segment cs;
4644
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004645 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004646 *db = cs.db;
4647 *l = cs.l;
4648}
4649EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4650
4651int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4652 struct kvm_sregs *sregs)
4653{
4654 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004655
4656 vcpu_load(vcpu);
4657
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004658 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4659 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4660 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4661 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4662 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4663 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004664
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004665 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4666 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004667
4668 kvm_x86_ops->get_idt(vcpu, &dt);
4669 sregs->idt.limit = dt.limit;
4670 sregs->idt.base = dt.base;
4671 kvm_x86_ops->get_gdt(vcpu, &dt);
4672 sregs->gdt.limit = dt.limit;
4673 sregs->gdt.base = dt.base;
4674
Avi Kivity4d4ec082009-12-29 18:07:30 +02004675 sregs->cr0 = kvm_read_cr0(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004676 sregs->cr2 = vcpu->arch.cr2;
4677 sregs->cr3 = vcpu->arch.cr3;
Avi Kivityfc78f512009-12-07 12:16:48 +02004678 sregs->cr4 = kvm_read_cr4(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004679 sregs->cr8 = kvm_get_cr8(vcpu);
Avi Kivityf6801df2010-01-21 15:31:50 +02004680 sregs->efer = vcpu->arch.efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004681 sregs->apic_base = kvm_get_apic_base(vcpu);
4682
Gleb Natapov923c61b2009-05-11 13:35:48 +03004683 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004684
Gleb Natapov36752c92009-05-11 13:35:53 +03004685 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004686 set_bit(vcpu->arch.interrupt.nr,
4687 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004688
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004689 vcpu_put(vcpu);
4690
4691 return 0;
4692}
4693
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004694int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4695 struct kvm_mp_state *mp_state)
4696{
4697 vcpu_load(vcpu);
4698 mp_state->mp_state = vcpu->arch.mp_state;
4699 vcpu_put(vcpu);
4700 return 0;
4701}
4702
4703int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4704 struct kvm_mp_state *mp_state)
4705{
4706 vcpu_load(vcpu);
4707 vcpu->arch.mp_state = mp_state->mp_state;
4708 vcpu_put(vcpu);
4709 return 0;
4710}
4711
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004712static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004713 struct kvm_segment *var, int seg)
4714{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004715 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004716}
4717
Izik Eidus37817f22008-03-24 23:14:53 +02004718static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4719 struct kvm_segment *kvm_desct)
4720{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004721 kvm_desct->base = get_desc_base(seg_desc);
4722 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004723 if (seg_desc->g) {
4724 kvm_desct->limit <<= 12;
4725 kvm_desct->limit |= 0xfff;
4726 }
Izik Eidus37817f22008-03-24 23:14:53 +02004727 kvm_desct->selector = selector;
4728 kvm_desct->type = seg_desc->type;
4729 kvm_desct->present = seg_desc->p;
4730 kvm_desct->dpl = seg_desc->dpl;
4731 kvm_desct->db = seg_desc->d;
4732 kvm_desct->s = seg_desc->s;
4733 kvm_desct->l = seg_desc->l;
4734 kvm_desct->g = seg_desc->g;
4735 kvm_desct->avl = seg_desc->avl;
4736 if (!selector)
4737 kvm_desct->unusable = 1;
4738 else
4739 kvm_desct->unusable = 0;
4740 kvm_desct->padding = 0;
4741}
4742
Amit Shahb8222ad2008-10-22 16:39:47 +05304743static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4744 u16 selector,
4745 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004746{
4747 if (selector & 1 << 2) {
4748 struct kvm_segment kvm_seg;
4749
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004750 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004751
4752 if (kvm_seg.unusable)
4753 dtable->limit = 0;
4754 else
4755 dtable->limit = kvm_seg.limit;
4756 dtable->base = kvm_seg.base;
4757 }
4758 else
4759 kvm_x86_ops->get_gdt(vcpu, dtable);
4760}
4761
4762/* allowed just for 8 bytes segments */
4763static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4764 struct desc_struct *seg_desc)
4765{
4766 struct descriptor_table dtable;
4767 u16 index = selector >> 3;
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004768 int ret;
4769 u32 err;
4770 gva_t addr;
Izik Eidus37817f22008-03-24 23:14:53 +02004771
Amit Shahb8222ad2008-10-22 16:39:47 +05304772 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004773
4774 if (dtable.limit < index * 8 + 7) {
4775 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
Takuya Yoshikawac125c602010-02-01 22:11:04 +09004776 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004777 }
Takuya Yoshikawa6f550482010-02-18 12:15:00 +02004778 addr = dtable.base + index * 8;
4779 ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc),
4780 vcpu, &err);
4781 if (ret == X86EMUL_PROPAGATE_FAULT)
4782 kvm_inject_page_fault(vcpu, addr, err);
4783
4784 return ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004785}
4786
4787/* allowed just for 8 bytes segments */
4788static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4789 struct desc_struct *seg_desc)
4790{
4791 struct descriptor_table dtable;
4792 u16 index = selector >> 3;
4793
Amit Shahb8222ad2008-10-22 16:39:47 +05304794 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004795
4796 if (dtable.limit < index * 8 + 7)
4797 return 1;
Gleb Natapov1871c602010-02-10 14:21:32 +02004798 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004799}
4800
Gleb Natapov1871c602010-02-10 14:21:32 +02004801static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu,
4802 struct desc_struct *seg_desc)
4803{
4804 u32 base_addr = get_desc_base(seg_desc);
4805
4806 return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL);
4807}
4808
4809static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004810 struct desc_struct *seg_desc)
4811{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004812 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004813
Gleb Natapov1871c602010-02-10 14:21:32 +02004814 return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02004815}
4816
Izik Eidus37817f22008-03-24 23:14:53 +02004817static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4818{
4819 struct kvm_segment kvm_seg;
4820
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004821 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004822 return kvm_seg.selector;
4823}
4824
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004825static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004826{
4827 struct kvm_segment segvar = {
4828 .base = selector << 4,
4829 .limit = 0xffff,
4830 .selector = selector,
4831 .type = 3,
4832 .present = 1,
4833 .dpl = 3,
4834 .db = 0,
4835 .s = 1,
4836 .l = 0,
4837 .g = 0,
4838 .avl = 0,
4839 .unusable = 0,
4840 };
4841 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
Gleb Natapovc6975182010-02-18 12:15:01 +02004842 return X86EMUL_CONTINUE;
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004843}
4844
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004845static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4846{
4847 return (seg != VCPU_SREG_LDTR) &&
4848 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004849 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004850}
4851
Gleb Natapovc6975182010-02-18 12:15:01 +02004852int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004853{
4854 struct kvm_segment kvm_seg;
Gleb Natapove01c2422010-01-25 12:01:04 +02004855 struct desc_struct seg_desc;
Gleb Natapovc6975182010-02-18 12:15:01 +02004856 u8 dpl, rpl, cpl;
4857 unsigned err_vec = GP_VECTOR;
4858 u32 err_code = 0;
4859 bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */
4860 int ret;
Izik Eidus37817f22008-03-24 23:14:53 +02004861
Avi Kivity3eeb3282010-01-21 15:31:48 +02004862 if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004863 return kvm_load_realmode_segment(vcpu, selector, seg);
Gleb Natapove01c2422010-01-25 12:01:04 +02004864
Gleb Natapovc6975182010-02-18 12:15:01 +02004865 /* NULL selector is not valid for TR, CS and SS */
4866 if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR)
4867 && null_selector)
4868 goto exception;
4869
4870 /* TR should be in GDT only */
4871 if (seg == VCPU_SREG_TR && (selector & (1 << 2)))
4872 goto exception;
4873
4874 ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc);
4875 if (ret)
4876 return ret;
4877
Gleb Natapove01c2422010-01-25 12:01:04 +02004878 seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg);
Marcelo Tosatticb84b552009-11-11 17:29:49 -02004879
Gleb Natapovc6975182010-02-18 12:15:01 +02004880 if (null_selector) { /* for NULL selector skip all following checks */
4881 kvm_seg.unusable = 1;
4882 goto load;
4883 }
Izik Eidus37817f22008-03-24 23:14:53 +02004884
Gleb Natapovc6975182010-02-18 12:15:01 +02004885 err_code = selector & 0xfffc;
4886 err_vec = GP_VECTOR;
Izik Eidus37817f22008-03-24 23:14:53 +02004887
Gleb Natapovc6975182010-02-18 12:15:01 +02004888 /* can't load system descriptor into segment selecor */
4889 if (seg <= VCPU_SREG_GS && !kvm_seg.s)
4890 goto exception;
4891
4892 if (!kvm_seg.present) {
4893 err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR;
4894 goto exception;
4895 }
4896
4897 rpl = selector & 3;
4898 dpl = kvm_seg.dpl;
4899 cpl = kvm_x86_ops->get_cpl(vcpu);
4900
4901 switch (seg) {
4902 case VCPU_SREG_SS:
4903 /*
4904 * segment is not a writable data segment or segment
4905 * selector's RPL != CPL or segment selector's RPL != CPL
4906 */
4907 if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl)
4908 goto exception;
4909 break;
4910 case VCPU_SREG_CS:
4911 if (!(kvm_seg.type & 8))
4912 goto exception;
4913
4914 if (kvm_seg.type & 4) {
4915 /* conforming */
4916 if (dpl > cpl)
4917 goto exception;
4918 } else {
4919 /* nonconforming */
4920 if (rpl > cpl || dpl != cpl)
4921 goto exception;
4922 }
4923 /* CS(RPL) <- CPL */
4924 selector = (selector & 0xfffc) | cpl;
4925 break;
4926 case VCPU_SREG_TR:
4927 if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9))
4928 goto exception;
4929 break;
4930 case VCPU_SREG_LDTR:
4931 if (kvm_seg.s || kvm_seg.type != 2)
4932 goto exception;
4933 break;
4934 default: /* DS, ES, FS, or GS */
4935 /*
4936 * segment is not a data or readable code segment or
4937 * ((segment is a data or nonconforming code segment)
4938 * and (both RPL and CPL > DPL))
4939 */
4940 if ((kvm_seg.type & 0xa) == 0x8 ||
4941 (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl)))
4942 goto exception;
4943 break;
4944 }
4945
4946 if (!kvm_seg.unusable && kvm_seg.s) {
Gleb Natapove01c2422010-01-25 12:01:04 +02004947 /* mark segment as accessed */
Gleb Natapovc6975182010-02-18 12:15:01 +02004948 kvm_seg.type |= 1;
Gleb Natapove01c2422010-01-25 12:01:04 +02004949 seg_desc.type |= 1;
4950 save_guest_segment_descriptor(vcpu, selector, &seg_desc);
4951 }
Gleb Natapovc6975182010-02-18 12:15:01 +02004952load:
4953 kvm_set_segment(vcpu, &kvm_seg, seg);
4954 return X86EMUL_CONTINUE;
4955exception:
4956 kvm_queue_exception_e(vcpu, err_vec, err_code);
4957 return X86EMUL_PROPAGATE_FAULT;
Izik Eidus37817f22008-03-24 23:14:53 +02004958}
4959
4960static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4961 struct tss_segment_32 *tss)
4962{
4963 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004964 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004965 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004966 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4967 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4968 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4969 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4970 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4971 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4972 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4973 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004974 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4975 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4976 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4977 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4978 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4979 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4980 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004981}
4982
Gleb Natapovc6975182010-02-18 12:15:01 +02004983static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg)
4984{
4985 struct kvm_segment kvm_seg;
4986 kvm_get_segment(vcpu, &kvm_seg, seg);
4987 kvm_seg.selector = sel;
4988 kvm_set_segment(vcpu, &kvm_seg, seg);
4989}
4990
Izik Eidus37817f22008-03-24 23:14:53 +02004991static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4992 struct tss_segment_32 *tss)
4993{
4994 kvm_set_cr3(vcpu, tss->cr3);
4995
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004996 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004997 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004998
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004999 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
5000 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
5001 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
5002 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
5003 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
5004 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
5005 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
5006 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02005007
Gleb Natapovc6975182010-02-18 12:15:01 +02005008 /*
5009 * SDM says that segment selectors are loaded before segment
5010 * descriptors
5011 */
5012 kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR);
5013 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5014 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5015 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5016 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5017 kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS);
5018 kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS);
5019
5020 /*
5021 * Now load segment descriptors. If fault happenes at this stage
5022 * it is handled in a context of new task
5023 */
5024 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005025 return 1;
5026
Gleb Natapovc6975182010-02-18 12:15:01 +02005027 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005028 return 1;
5029
Gleb Natapovc6975182010-02-18 12:15:01 +02005030 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005031 return 1;
5032
Gleb Natapovc6975182010-02-18 12:15:01 +02005033 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005034 return 1;
5035
Gleb Natapovc6975182010-02-18 12:15:01 +02005036 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005037 return 1;
5038
Gleb Natapovc6975182010-02-18 12:15:01 +02005039 if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02005040 return 1;
5041
Gleb Natapovc6975182010-02-18 12:15:01 +02005042 if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02005043 return 1;
5044 return 0;
5045}
5046
5047static void save_state_to_tss16(struct kvm_vcpu *vcpu,
5048 struct tss_segment_16 *tss)
5049{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005050 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02005051 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005052 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
5053 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
5054 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
5055 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
5056 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
5057 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
5058 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
5059 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02005060
5061 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
5062 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
5063 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
5064 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
5065 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02005066}
5067
5068static int load_state_from_tss16(struct kvm_vcpu *vcpu,
5069 struct tss_segment_16 *tss)
5070{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005071 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02005072 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03005073 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
5074 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
5075 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
5076 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
5077 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
5078 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
5079 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
5080 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02005081
Gleb Natapovc6975182010-02-18 12:15:01 +02005082 /*
5083 * SDM says that segment selectors are loaded before segment
5084 * descriptors
5085 */
5086 kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR);
5087 kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES);
5088 kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS);
5089 kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS);
5090 kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS);
5091
5092 /*
5093 * Now load segment descriptors. If fault happenes at this stage
5094 * it is handled in a context of new task
5095 */
5096 if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02005097 return 1;
5098
Gleb Natapovc6975182010-02-18 12:15:01 +02005099 if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02005100 return 1;
5101
Gleb Natapovc6975182010-02-18 12:15:01 +02005102 if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02005103 return 1;
5104
Gleb Natapovc6975182010-02-18 12:15:01 +02005105 if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02005106 return 1;
5107
Gleb Natapovc6975182010-02-18 12:15:01 +02005108 if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02005109 return 1;
5110 return 0;
5111}
5112
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005113static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005114 u16 old_tss_sel, u32 old_tss_base,
5115 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02005116{
5117 struct tss_segment_16 tss_segment_16;
5118 int ret = 0;
5119
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005120 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5121 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005122 goto out;
5123
5124 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02005125
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005126 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
5127 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02005128 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005129
Gleb Natapov1871c602010-02-10 14:21:32 +02005130 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005131 &tss_segment_16, sizeof tss_segment_16))
5132 goto out;
5133
Gleb Natapovb237ac32009-03-30 16:03:24 +03005134 if (old_tss_sel != 0xffff) {
5135 tss_segment_16.prev_task_link = old_tss_sel;
5136
5137 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005138 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005139 &tss_segment_16.prev_task_link,
5140 sizeof tss_segment_16.prev_task_link))
5141 goto out;
5142 }
5143
Izik Eidus37817f22008-03-24 23:14:53 +02005144 if (load_state_from_tss16(vcpu, &tss_segment_16))
5145 goto out;
5146
5147 ret = 1;
5148out:
5149 return ret;
5150}
5151
Harvey Harrison8b2cf732008-04-27 12:14:13 -07005152static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03005153 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02005154 struct desc_struct *nseg_desc)
5155{
5156 struct tss_segment_32 tss_segment_32;
5157 int ret = 0;
5158
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005159 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5160 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005161 goto out;
5162
5163 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02005164
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005165 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
5166 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02005167 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005168
Gleb Natapov1871c602010-02-10 14:21:32 +02005169 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc),
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005170 &tss_segment_32, sizeof tss_segment_32))
5171 goto out;
5172
Gleb Natapovb237ac32009-03-30 16:03:24 +03005173 if (old_tss_sel != 0xffff) {
5174 tss_segment_32.prev_task_link = old_tss_sel;
5175
5176 if (kvm_write_guest(vcpu->kvm,
Gleb Natapov1871c602010-02-10 14:21:32 +02005177 get_tss_base_addr_write(vcpu, nseg_desc),
Gleb Natapovb237ac32009-03-30 16:03:24 +03005178 &tss_segment_32.prev_task_link,
5179 sizeof tss_segment_32.prev_task_link))
5180 goto out;
5181 }
5182
Izik Eidus37817f22008-03-24 23:14:53 +02005183 if (load_state_from_tss32(vcpu, &tss_segment_32))
5184 goto out;
5185
5186 ret = 1;
5187out:
5188 return ret;
5189}
5190
5191int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
5192{
5193 struct kvm_segment tr_seg;
5194 struct desc_struct cseg_desc;
5195 struct desc_struct nseg_desc;
5196 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005197 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
5198 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02005199
Gleb Natapov1871c602010-02-10 14:21:32 +02005200 old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL);
Izik Eidus37817f22008-03-24 23:14:53 +02005201
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005202 /* FIXME: Handle errors. Failure to read either TSS or their
5203 * descriptors should generate a pagefault.
5204 */
Izik Eidus37817f22008-03-24 23:14:53 +02005205 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
5206 goto out;
5207
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03005208 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02005209 goto out;
5210
Izik Eidus37817f22008-03-24 23:14:53 +02005211 if (reason != TASK_SWITCH_IRET) {
5212 int cpl;
5213
5214 cpl = kvm_x86_ops->get_cpl(vcpu);
5215 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
5216 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
5217 return 1;
5218 }
5219 }
5220
Akinobu Mita46a359e2009-07-18 23:58:32 +09005221 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
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;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005273 struct descriptor_table dt;
5274
5275 vcpu_load(vcpu);
5276
5277 dt.limit = sregs->idt.limit;
5278 dt.base = sregs->idt.base;
5279 kvm_x86_ops->set_idt(vcpu, &dt);
5280 dt.limit = sregs->gdt.limit;
5281 dt.base = sregs->gdt.base;
5282 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 Tosatti7c93be42009-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 Tosatti7c93be42009-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 Kiszka94fe45d2009-10-18 13:24:44 +02005380 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
5381 vcpu->arch.singlestep_cs =
5382 get_segment_selector(vcpu, VCPU_SREG_CS);
5383 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
5384 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005385
Jan Kiszka91586a32009-10-05 13:07:21 +02005386 /*
5387 * Trigger an rflags update that will inject or remove the trace
5388 * flags.
5389 */
5390 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005391
Jan Kiszka355be0b2009-10-03 00:31:21 +02005392 kvm_x86_ops->set_guest_debug(vcpu, dbg);
5393
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005394 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005395
Jan Kiszka4f926bf22009-10-30 12:46:59 +01005396unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05005397 vcpu_put(vcpu);
5398
5399 return r;
5400}
5401
5402/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05005403 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
5404 * we have asm/x86/processor.h
5405 */
5406struct fxsave {
5407 u16 cwd;
5408 u16 swd;
5409 u16 twd;
5410 u16 fop;
5411 u64 rip;
5412 u64 rdp;
5413 u32 mxcsr;
5414 u32 mxcsr_mask;
5415 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
5416#ifdef CONFIG_X86_64
5417 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
5418#else
5419 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
5420#endif
5421};
5422
Zhang Xiantao8b006792007-11-16 13:05:55 +08005423/*
5424 * Translate a guest virtual address to a guest physical address.
5425 */
5426int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
5427 struct kvm_translation *tr)
5428{
5429 unsigned long vaddr = tr->linear_address;
5430 gpa_t gpa;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005431 int idx;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005432
5433 vcpu_load(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005434 idx = srcu_read_lock(&vcpu->kvm->srcu);
Gleb Natapov1871c602010-02-10 14:21:32 +02005435 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005436 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantao8b006792007-11-16 13:05:55 +08005437 tr->physical_address = gpa;
5438 tr->valid = gpa != UNMAPPED_GVA;
5439 tr->writeable = 1;
5440 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08005441 vcpu_put(vcpu);
5442
5443 return 0;
5444}
5445
Hollis Blanchardd0752062007-10-31 17:24:25 -05005446int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5447{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005448 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005449
5450 vcpu_load(vcpu);
5451
5452 memcpy(fpu->fpr, fxsave->st_space, 128);
5453 fpu->fcw = fxsave->cwd;
5454 fpu->fsw = fxsave->swd;
5455 fpu->ftwx = fxsave->twd;
5456 fpu->last_opcode = fxsave->fop;
5457 fpu->last_ip = fxsave->rip;
5458 fpu->last_dp = fxsave->rdp;
5459 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
5460
5461 vcpu_put(vcpu);
5462
5463 return 0;
5464}
5465
5466int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
5467{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005468 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005469
5470 vcpu_load(vcpu);
5471
5472 memcpy(fxsave->st_space, fpu->fpr, 128);
5473 fxsave->cwd = fpu->fcw;
5474 fxsave->swd = fpu->fsw;
5475 fxsave->twd = fpu->ftwx;
5476 fxsave->fop = fpu->last_opcode;
5477 fxsave->rip = fpu->last_ip;
5478 fxsave->rdp = fpu->last_dp;
5479 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
5480
5481 vcpu_put(vcpu);
5482
5483 return 0;
5484}
5485
5486void fx_init(struct kvm_vcpu *vcpu)
5487{
5488 unsigned after_mxcsr_mask;
5489
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005490 /*
5491 * Touch the fpu the first time in non atomic context as if
5492 * this is the first fpu instruction the exception handler
5493 * will fire before the instruction returns and it'll have to
5494 * allocate ram with GFP_KERNEL.
5495 */
5496 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005497 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02005498
Hollis Blanchardd0752062007-10-31 17:24:25 -05005499 /* Initialize guest FPU by resetting ours and saving into guest's */
5500 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005501 kvm_fx_save(&vcpu->arch.host_fx_image);
5502 kvm_fx_finit();
5503 kvm_fx_save(&vcpu->arch.guest_fx_image);
5504 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005505 preempt_enable();
5506
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005507 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05005508 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005509 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
5510 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05005511 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
5512}
5513EXPORT_SYMBOL_GPL(fx_init);
5514
5515void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
5516{
Avi Kivity2608d7a2010-01-21 15:31:45 +02005517 if (vcpu->guest_fpu_loaded)
Hollis Blanchardd0752062007-10-31 17:24:25 -05005518 return;
5519
5520 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005521 kvm_fx_save(&vcpu->arch.host_fx_image);
5522 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Avi Kivity0c048512010-01-21 15:31:52 +02005523 trace_kvm_fpu(1);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005524}
Hollis Blanchardd0752062007-10-31 17:24:25 -05005525
5526void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
5527{
5528 if (!vcpu->guest_fpu_loaded)
5529 return;
5530
5531 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03005532 kvm_fx_save(&vcpu->arch.guest_fx_image);
5533 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02005534 ++vcpu->stat.fpu_reload;
Avi Kivity02daab22009-12-30 12:40:26 +02005535 set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
Avi Kivity0c048512010-01-21 15:31:52 +02005536 trace_kvm_fpu(0);
Hollis Blanchardd0752062007-10-31 17:24:25 -05005537}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005538
5539void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
5540{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01005541 if (vcpu->arch.time_page) {
5542 kvm_release_page_dirty(vcpu->arch.time_page);
5543 vcpu->arch.time_page = NULL;
5544 }
5545
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005546 kvm_x86_ops->vcpu_free(vcpu);
5547}
5548
5549struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5550 unsigned int id)
5551{
Avi Kivity26e52152007-11-20 15:30:24 +02005552 return kvm_x86_ops->vcpu_create(kvm, id);
5553}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005554
Avi Kivity26e52152007-11-20 15:30:24 +02005555int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5556{
5557 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005558
5559 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005560 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005561
Sheng Yang0bed3b52008-10-09 16:01:54 +08005562 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005563 vcpu_load(vcpu);
5564 r = kvm_arch_vcpu_reset(vcpu);
5565 if (r == 0)
5566 r = kvm_mmu_setup(vcpu);
5567 vcpu_put(vcpu);
5568 if (r < 0)
5569 goto free_vcpu;
5570
Avi Kivity26e52152007-11-20 15:30:24 +02005571 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005572free_vcpu:
5573 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02005574 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005575}
5576
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06005577void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005578{
5579 vcpu_load(vcpu);
5580 kvm_mmu_unload(vcpu);
5581 vcpu_put(vcpu);
5582
5583 kvm_x86_ops->vcpu_free(vcpu);
5584}
5585
5586int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5587{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02005588 vcpu->arch.nmi_pending = false;
5589 vcpu->arch.nmi_injected = false;
5590
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005591 vcpu->arch.switch_db_regs = 0;
5592 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5593 vcpu->arch.dr6 = DR6_FIXED_1;
5594 vcpu->arch.dr7 = DR7_FIXED_1;
5595
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005596 return kvm_x86_ops->vcpu_reset(vcpu);
5597}
5598
Alexander Graf10474ae2009-09-15 11:37:46 +02005599int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005600{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005601 /*
5602 * Since this may be called from a hotplug notifcation,
5603 * we can't get the CPU frequency directly.
5604 */
5605 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5606 int cpu = raw_smp_processor_id();
5607 per_cpu(cpu_tsc_khz, cpu) = 0;
5608 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005609
5610 kvm_shared_msr_cpu_online();
5611
Alexander Graf10474ae2009-09-15 11:37:46 +02005612 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005613}
5614
5615void kvm_arch_hardware_disable(void *garbage)
5616{
5617 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005618 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005619}
5620
5621int kvm_arch_hardware_setup(void)
5622{
5623 return kvm_x86_ops->hardware_setup();
5624}
5625
5626void kvm_arch_hardware_unsetup(void)
5627{
5628 kvm_x86_ops->hardware_unsetup();
5629}
5630
5631void kvm_arch_check_processor_compat(void *rtn)
5632{
5633 kvm_x86_ops->check_processor_compatibility(rtn);
5634}
5635
5636int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5637{
5638 struct page *page;
5639 struct kvm *kvm;
5640 int r;
5641
5642 BUG_ON(vcpu->kvm == NULL);
5643 kvm = vcpu->kvm;
5644
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005645 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005646 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005647 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005648 else
Avi Kivitya4535292008-04-13 17:54:35 +03005649 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005650
5651 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5652 if (!page) {
5653 r = -ENOMEM;
5654 goto fail;
5655 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005656 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005657
5658 r = kvm_mmu_create(vcpu);
5659 if (r < 0)
5660 goto fail_free_pio_data;
5661
5662 if (irqchip_in_kernel(kvm)) {
5663 r = kvm_create_lapic(vcpu);
5664 if (r < 0)
5665 goto fail_mmu_destroy;
5666 }
5667
Huang Ying890ca9a2009-05-11 16:48:15 +08005668 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5669 GFP_KERNEL);
5670 if (!vcpu->arch.mce_banks) {
5671 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005672 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005673 }
5674 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5675
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005676 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005677fail_free_lapic:
5678 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005679fail_mmu_destroy:
5680 kvm_mmu_destroy(vcpu);
5681fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005682 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005683fail:
5684 return r;
5685}
5686
5687void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5688{
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005689 int idx;
5690
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005691 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005692 kvm_free_lapic(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005693 idx = srcu_read_lock(&vcpu->kvm->srcu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005694 kvm_mmu_destroy(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -02005695 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005696 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005697}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005698
5699struct kvm *kvm_arch_create_vm(void)
5700{
5701 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5702
5703 if (!kvm)
5704 return ERR_PTR(-ENOMEM);
5705
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005706 kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
5707 if (!kvm->arch.aliases) {
5708 kfree(kvm);
5709 return ERR_PTR(-ENOMEM);
5710 }
5711
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005712 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005713 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005714
Sheng Yang5550af42008-10-15 20:15:06 +08005715 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5716 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5717
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005718 rdtscll(kvm->arch.vm_init_tsc);
5719
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005720 return kvm;
5721}
5722
5723static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5724{
5725 vcpu_load(vcpu);
5726 kvm_mmu_unload(vcpu);
5727 vcpu_put(vcpu);
5728}
5729
5730static void kvm_free_vcpus(struct kvm *kvm)
5731{
5732 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005733 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005734
5735 /*
5736 * Unpin any mmu pages first.
5737 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005738 kvm_for_each_vcpu(i, vcpu, kvm)
5739 kvm_unload_vcpu_mmu(vcpu);
5740 kvm_for_each_vcpu(i, vcpu, kvm)
5741 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005742
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005743 mutex_lock(&kvm->lock);
5744 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5745 kvm->vcpus[i] = NULL;
5746
5747 atomic_set(&kvm->online_vcpus, 0);
5748 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005749}
5750
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005751void kvm_arch_sync_events(struct kvm *kvm)
5752{
Sheng Yangba4cef32009-01-06 10:03:03 +08005753 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005754}
5755
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005756void kvm_arch_destroy_vm(struct kvm *kvm)
5757{
Sheng Yang6eb55812008-10-31 12:37:41 +08005758 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005759 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005760 kfree(kvm->arch.vpic);
5761 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005762 kvm_free_vcpus(kvm);
5763 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005764 if (kvm->arch.apic_access_page)
5765 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005766 if (kvm->arch.ept_identity_pagetable)
5767 put_page(kvm->arch.ept_identity_pagetable);
Marcelo Tosatti64749202010-01-19 12:45:23 -02005768 cleanup_srcu_struct(&kvm->srcu);
Marcelo Tosattifef9cce2009-12-23 14:35:17 -02005769 kfree(kvm->arch.aliases);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005770 kfree(kvm);
5771}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005772
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005773int kvm_arch_prepare_memory_region(struct kvm *kvm,
5774 struct kvm_memory_slot *memslot,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005775 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005776 struct kvm_userspace_memory_region *mem,
Zhang Xiantao0de10342007-11-20 16:25:04 +08005777 int user_alloc)
5778{
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005779 int npages = memslot->npages;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005780
5781 /*To keep backward compatibility with older userspace,
5782 *x86 needs to hanlde !user_alloc case.
5783 */
5784 if (!user_alloc) {
5785 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005786 unsigned long userspace_addr;
5787
Izik Eidus72dc67a2008-02-10 18:04:15 +02005788 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005789 userspace_addr = do_mmap(NULL, 0,
5790 npages * PAGE_SIZE,
5791 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005792 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005793 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005794 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005795
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005796 if (IS_ERR((void *)userspace_addr))
5797 return PTR_ERR((void *)userspace_addr);
5798
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005799 memslot->userspace_addr = userspace_addr;
Zhang Xiantao0de10342007-11-20 16:25:04 +08005800 }
5801 }
5802
Marcelo Tosattif7784b82009-12-23 14:35:18 -02005803
5804 return 0;
5805}
5806
5807void kvm_arch_commit_memory_region(struct kvm *kvm,
5808 struct kvm_userspace_memory_region *mem,
5809 struct kvm_memory_slot old,
5810 int user_alloc)
5811{
5812
5813 int npages = mem->memory_size >> PAGE_SHIFT;
5814
5815 if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
5816 int ret;
5817
5818 down_write(&current->mm->mmap_sem);
5819 ret = do_munmap(current->mm, old.userspace_addr,
5820 old.npages * PAGE_SIZE);
5821 up_write(&current->mm->mmap_sem);
5822 if (ret < 0)
5823 printk(KERN_WARNING
5824 "kvm_vm_ioctl_set_memory_region: "
5825 "failed to munmap memory\n");
5826 }
5827
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005828 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005829 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005830 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5831 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5832 }
5833
5834 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005835 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005836}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005837
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005838void kvm_arch_flush_shadow(struct kvm *kvm)
5839{
5840 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005841 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005842}
5843
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005844int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5845{
Avi Kivitya4535292008-04-13 17:54:35 +03005846 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005847 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5848 || vcpu->arch.nmi_pending ||
5849 (kvm_arch_interrupt_allowed(vcpu) &&
5850 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005851}
Zhang Xiantao57361992007-12-17 14:21:40 +08005852
Zhang Xiantao57361992007-12-17 14:21:40 +08005853void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5854{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005855 int me;
5856 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005857
5858 if (waitqueue_active(&vcpu->wq)) {
5859 wake_up_interruptible(&vcpu->wq);
5860 ++vcpu->stat.halt_wakeup;
5861 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005862
5863 me = get_cpu();
5864 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5865 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5866 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005867 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005868}
Gleb Natapov78646122009-03-23 12:12:11 +02005869
5870int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5871{
5872 return kvm_x86_ops->interrupt_allowed(vcpu);
5873}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005874
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005875unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5876{
5877 unsigned long rflags;
5878
5879 rflags = kvm_x86_ops->get_rflags(vcpu);
5880 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5881 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5882 return rflags;
5883}
5884EXPORT_SYMBOL_GPL(kvm_get_rflags);
5885
5886void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5887{
5888 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5889 vcpu->arch.singlestep_cs ==
5890 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5891 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5892 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5893 kvm_x86_ops->set_rflags(vcpu, rflags);
5894}
5895EXPORT_SYMBOL_GPL(kvm_set_rflags);
5896
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005897EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5898EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5899EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5900EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5901EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005902EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005903EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005904EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005905EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005906EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005907EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);