blob: 97f6f9565ac9cba10426e98b63b273967a585971 [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>
Avi Kivityaec51dc2009-07-01 16:01:02 +030041#include <trace/events/kvm.h>
42#undef TRACE_INCLUDE_FILE
Marcelo Tosatti229456f2009-06-17 09:22:14 -030043#define CREATE_TRACE_POINTS
44#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020045
46#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080047#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020048#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080049#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080050#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020051
Carsten Otte313a3dc2007-10-11 19:16:52 +020052#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010053#define CR0_RESERVED_BITS \
54 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
55 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
56 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
57#define CR4_RESERVED_BITS \
58 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
59 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
60 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
61 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
62
63#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080064
65#define KVM_MAX_MCE_BANKS 32
66#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
67
Joerg Roedel50a37eb2008-01-31 14:57:38 +010068/* EFER defaults:
69 * - enable syscall per default because its emulated by KVM
70 * - enable LME and LMA per default on 64 bit KVM
71 */
72#ifdef CONFIG_X86_64
73static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
74#else
75static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
76#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020077
Avi Kivityba1389b2007-11-18 16:24:12 +020078#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
79#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050080
Gleb Natapovcb142eb2009-08-09 15:17:40 +030081static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020082static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
83 struct kvm_cpuid_entry2 __user *entries);
84
Zhang Xiantao97896d02007-11-14 20:09:30 +080085struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030086EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080087
Andre Przywaraed85c062009-06-25 12:36:49 +020088int ignore_msrs = 0;
89module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
90
Avi Kivity18863bd2009-09-07 11:12:18 +030091#define KVM_NR_SHARED_MSRS 16
92
93struct kvm_shared_msrs_global {
94 int nr;
95 struct kvm_shared_msr {
96 u32 msr;
97 u64 value;
98 } msrs[KVM_NR_SHARED_MSRS];
99};
100
101struct kvm_shared_msrs {
102 struct user_return_notifier urn;
103 bool registered;
104 u64 current_value[KVM_NR_SHARED_MSRS];
105};
106
107static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
108static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
109
Hollis Blanchard417bc302007-10-31 17:24:23 -0500110struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200111 { "pf_fixed", VCPU_STAT(pf_fixed) },
112 { "pf_guest", VCPU_STAT(pf_guest) },
113 { "tlb_flush", VCPU_STAT(tlb_flush) },
114 { "invlpg", VCPU_STAT(invlpg) },
115 { "exits", VCPU_STAT(exits) },
116 { "io_exits", VCPU_STAT(io_exits) },
117 { "mmio_exits", VCPU_STAT(mmio_exits) },
118 { "signal_exits", VCPU_STAT(signal_exits) },
119 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800120 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200121 { "halt_exits", VCPU_STAT(halt_exits) },
122 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530123 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200124 { "request_irq", VCPU_STAT(request_irq_exits) },
125 { "irq_exits", VCPU_STAT(irq_exits) },
126 { "host_state_reload", VCPU_STAT(host_state_reload) },
127 { "efer_reload", VCPU_STAT(efer_reload) },
128 { "fpu_reload", VCPU_STAT(fpu_reload) },
129 { "insn_emulation", VCPU_STAT(insn_emulation) },
130 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300131 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200132 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200133 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
134 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
135 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
136 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
137 { "mmu_flooded", VM_STAT(mmu_flooded) },
138 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200139 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300140 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200141 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300142 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500143 { NULL }
144};
145
Avi Kivity18863bd2009-09-07 11:12:18 +0300146static void kvm_on_user_return(struct user_return_notifier *urn)
147{
148 unsigned slot;
149 struct kvm_shared_msr *global;
150 struct kvm_shared_msrs *locals
151 = container_of(urn, struct kvm_shared_msrs, urn);
152
153 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
154 global = &shared_msrs_global.msrs[slot];
155 if (global->value != locals->current_value[slot]) {
156 wrmsrl(global->msr, global->value);
157 locals->current_value[slot] = global->value;
158 }
159 }
160 locals->registered = false;
161 user_return_notifier_unregister(urn);
162}
163
164void kvm_define_shared_msr(unsigned slot, u32 msr)
165{
166 int cpu;
167 u64 value;
168
169 if (slot >= shared_msrs_global.nr)
170 shared_msrs_global.nr = slot + 1;
171 shared_msrs_global.msrs[slot].msr = msr;
172 rdmsrl_safe(msr, &value);
173 shared_msrs_global.msrs[slot].value = value;
174 for_each_online_cpu(cpu)
175 per_cpu(shared_msrs, cpu).current_value[slot] = value;
176}
177EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
178
179static void kvm_shared_msr_cpu_online(void)
180{
181 unsigned i;
182 struct kvm_shared_msrs *locals = &__get_cpu_var(shared_msrs);
183
184 for (i = 0; i < shared_msrs_global.nr; ++i)
185 locals->current_value[i] = shared_msrs_global.msrs[i].value;
186}
187
188void kvm_set_shared_msr(unsigned slot, u64 value)
189{
190 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
191
192 if (value == smsr->current_value[slot])
193 return;
194 smsr->current_value[slot] = value;
195 wrmsrl(shared_msrs_global.msrs[slot].msr, value);
196 if (!smsr->registered) {
197 smsr->urn.on_user_return = kvm_on_user_return;
198 user_return_notifier_register(&smsr->urn);
199 smsr->registered = true;
200 }
201}
202EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
203
Carsten Otte5fb76f92007-10-29 16:08:51 +0100204unsigned long segment_base(u16 selector)
205{
206 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200207 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100208 unsigned long table_base;
209 unsigned long v;
210
211 if (selector == 0)
212 return 0;
213
Akinobu Mitab792c342009-07-19 00:00:01 +0900214 kvm_get_gdt(&gdt);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100215 table_base = gdt.base;
216
217 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900218 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100219
Carsten Otte5fb76f92007-10-29 16:08:51 +0100220 table_base = segment_base(ldt_selector);
221 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200222 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900223 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100224#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200225 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
226 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100227#endif
228 return v;
229}
230EXPORT_SYMBOL_GPL(segment_base);
231
Carsten Otte6866b832007-10-29 16:09:10 +0100232u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
233{
234 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800235 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100236 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800237 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100238}
239EXPORT_SYMBOL_GPL(kvm_get_apic_base);
240
241void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
242{
243 /* TODO: reserve bits check */
244 if (irqchip_in_kernel(vcpu->kvm))
245 kvm_lapic_set_base(vcpu, data);
246 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800247 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100248}
249EXPORT_SYMBOL_GPL(kvm_set_apic_base);
250
Avi Kivity298101d2007-11-25 13:41:11 +0200251void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
252{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800253 WARN_ON(vcpu->arch.exception.pending);
254 vcpu->arch.exception.pending = true;
255 vcpu->arch.exception.has_error_code = false;
256 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200257}
258EXPORT_SYMBOL_GPL(kvm_queue_exception);
259
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200260void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
261 u32 error_code)
262{
263 ++vcpu->stat.pf_guest;
Alexander Grafd8017472008-11-25 20:17:11 +0100264
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100265 if (vcpu->arch.exception.pending) {
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300266 switch(vcpu->arch.exception.nr) {
267 case DF_VECTOR:
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100268 /* triple fault -> shutdown */
269 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300270 return;
271 case PF_VECTOR:
272 vcpu->arch.exception.nr = DF_VECTOR;
273 vcpu->arch.exception.error_code = 0;
274 return;
275 default:
276 /* replace previous exception with a new one in a hope
277 that instruction re-execution will regenerate lost
278 exception */
279 vcpu->arch.exception.pending = false;
280 break;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100281 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200282 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800283 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200284 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
285}
286
Sheng Yang3419ffc2008-05-15 09:52:48 +0800287void kvm_inject_nmi(struct kvm_vcpu *vcpu)
288{
289 vcpu->arch.nmi_pending = 1;
290}
291EXPORT_SYMBOL_GPL(kvm_inject_nmi);
292
Avi Kivity298101d2007-11-25 13:41:11 +0200293void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
294{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800295 WARN_ON(vcpu->arch.exception.pending);
296 vcpu->arch.exception.pending = true;
297 vcpu->arch.exception.has_error_code = true;
298 vcpu->arch.exception.nr = nr;
299 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200300}
301EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
302
Carsten Ottea03490e2007-10-29 16:09:35 +0100303/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300304 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
305 * a #GP and return false.
306 */
307bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200308{
Avi Kivity0a79b002009-09-01 12:03:25 +0300309 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
310 return true;
311 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
312 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100313}
Avi Kivity0a79b002009-09-01 12:03:25 +0300314EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100315
316/*
317 * Load the pae pdptrs. Return true is they are all valid.
318 */
319int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
320{
321 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
322 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
323 int i;
324 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800325 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100326
Carsten Ottea03490e2007-10-29 16:09:35 +0100327 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
328 offset * sizeof(u64), sizeof(pdpte));
329 if (ret < 0) {
330 ret = 0;
331 goto out;
332 }
333 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300334 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800335 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100336 ret = 0;
337 goto out;
338 }
339 }
340 ret = 1;
341
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800342 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300343 __set_bit(VCPU_EXREG_PDPTR,
344 (unsigned long *)&vcpu->arch.regs_avail);
345 __set_bit(VCPU_EXREG_PDPTR,
346 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100347out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100348
349 return ret;
350}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100351EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100352
Avi Kivityd835dfe2007-11-21 02:57:59 +0200353static bool pdptrs_changed(struct kvm_vcpu *vcpu)
354{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800355 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200356 bool changed = true;
357 int r;
358
359 if (is_long_mode(vcpu) || !is_pae(vcpu))
360 return false;
361
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300362 if (!test_bit(VCPU_EXREG_PDPTR,
363 (unsigned long *)&vcpu->arch.regs_avail))
364 return true;
365
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800366 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200367 if (r < 0)
368 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200370out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200371
372 return changed;
373}
374
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200375void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100376{
377 if (cr0 & CR0_RESERVED_BITS) {
378 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800379 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200380 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100381 return;
382 }
383
384 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
385 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200386 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100387 return;
388 }
389
390 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
391 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
392 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200393 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100394 return;
395 }
396
397 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
398#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800399 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100400 int cs_db, cs_l;
401
402 if (!is_pae(vcpu)) {
403 printk(KERN_DEBUG "set_cr0: #GP, start paging "
404 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200405 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100406 return;
407 }
408 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
409 if (cs_l) {
410 printk(KERN_DEBUG "set_cr0: #GP, start paging "
411 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200412 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100413 return;
414
415 }
416 } else
417#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800418 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100419 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
420 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200421 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100422 return;
423 }
424
425 }
426
427 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800428 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100429
Carsten Ottea03490e2007-10-29 16:09:35 +0100430 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100431 return;
432}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200433EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100434
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200435void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100436{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200437 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100438}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200439EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100440
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200441void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100442{
Avi Kivitya2edf572009-05-24 22:19:00 +0300443 unsigned long old_cr4 = vcpu->arch.cr4;
444 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
445
Carsten Ottea03490e2007-10-29 16:09:35 +0100446 if (cr4 & CR4_RESERVED_BITS) {
447 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\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 (is_long_mode(vcpu)) {
453 if (!(cr4 & X86_CR4_PAE)) {
454 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
455 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200456 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100457 return;
458 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300459 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
460 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800461 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100462 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200463 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100464 return;
465 }
466
467 if (cr4 & X86_CR4_VMXE) {
468 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200469 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100470 return;
471 }
472 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800473 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200474 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100475 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100476}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200477EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100478
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200479void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100480{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800481 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300482 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200483 kvm_mmu_flush_tlb(vcpu);
484 return;
485 }
486
Carsten Ottea03490e2007-10-29 16:09:35 +0100487 if (is_long_mode(vcpu)) {
488 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
489 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200490 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100491 return;
492 }
493 } else {
494 if (is_pae(vcpu)) {
495 if (cr3 & CR3_PAE_RESERVED_BITS) {
496 printk(KERN_DEBUG
497 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200498 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100499 return;
500 }
501 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
502 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
503 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200504 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100505 return;
506 }
507 }
508 /*
509 * We don't check reserved bits in nonpae mode, because
510 * this isn't enforced, and VMware depends on this.
511 */
512 }
513
Carsten Ottea03490e2007-10-29 16:09:35 +0100514 /*
515 * Does the new cr3 value map to physical memory? (Note, we
516 * catch an invalid cr3 even in real-mode, because it would
517 * cause trouble later on when we turn on paging anyway.)
518 *
519 * A real CPU would silently accept an invalid cr3 and would
520 * attempt to use it - with largely undefined (and often hard
521 * to debug) behavior on the guest side.
522 */
523 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200524 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100525 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800526 vcpu->arch.cr3 = cr3;
527 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100528 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100529}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200530EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100531
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200532void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100533{
534 if (cr8 & CR8_RESERVED_BITS) {
535 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200536 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100537 return;
538 }
539 if (irqchip_in_kernel(vcpu->kvm))
540 kvm_lapic_set_tpr(vcpu, cr8);
541 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800542 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100543}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200544EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100545
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200546unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100547{
548 if (irqchip_in_kernel(vcpu->kvm))
549 return kvm_lapic_get_cr8(vcpu);
550 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800551 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100552}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200553EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100554
Alexander Grafd8017472008-11-25 20:17:11 +0100555static inline u32 bit(int bitno)
556{
557 return 1 << (bitno & 31);
558}
559
Carsten Otte043405e2007-10-10 17:16:19 +0200560/*
561 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
562 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
563 *
564 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400565 * capabilities of the host cpu. This capabilities test skips MSRs that are
566 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200567 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400568
569#define KVM_SAVE_MSRS_BEGIN 2
Carsten Otte043405e2007-10-10 17:16:19 +0200570static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400571 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Carsten Otte043405e2007-10-10 17:16:19 +0200572 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
573 MSR_K6_STAR,
574#ifdef CONFIG_X86_64
575 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
576#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400577 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200578};
579
580static unsigned num_msrs_to_save;
581
582static u32 emulated_msrs[] = {
583 MSR_IA32_MISC_ENABLE,
584};
585
Carsten Otte15c4a642007-10-30 18:44:17 +0100586static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
587{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100588 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100589 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
590 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200591 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100592 return;
593 }
594
595 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800596 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100597 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200598 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100599 return;
600 }
601
Alexander Graf1b2fd702009-02-02 16:23:51 +0100602 if (efer & EFER_FFXSR) {
603 struct kvm_cpuid_entry2 *feat;
604
605 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
606 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
607 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
608 kvm_inject_gp(vcpu, 0);
609 return;
610 }
611 }
612
Alexander Grafd8017472008-11-25 20:17:11 +0100613 if (efer & EFER_SVME) {
614 struct kvm_cpuid_entry2 *feat;
615
616 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
617 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
618 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
619 kvm_inject_gp(vcpu, 0);
620 return;
621 }
622 }
623
Carsten Otte15c4a642007-10-30 18:44:17 +0100624 kvm_x86_ops->set_efer(vcpu, efer);
625
626 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800627 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100628
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800629 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300630
631 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
632 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100633}
634
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100635void kvm_enable_efer_bits(u64 mask)
636{
637 efer_reserved_bits &= ~mask;
638}
639EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
640
641
Carsten Otte15c4a642007-10-30 18:44:17 +0100642/*
643 * Writes msr value into into the appropriate "register".
644 * Returns 0 on success, non-0 otherwise.
645 * Assumes vcpu_load() was already called.
646 */
647int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
648{
649 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
650}
651
Carsten Otte313a3dc2007-10-11 19:16:52 +0200652/*
653 * Adapt set_msr() to msr_io()'s calling convention
654 */
655static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
656{
657 return kvm_set_msr(vcpu, index, *data);
658}
659
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200660static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
661{
662 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200663 struct pvclock_wall_clock wc;
664 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200665
666 if (!wall_clock)
667 return;
668
669 version++;
670
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200671 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
672
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200673 /*
674 * The guest calculates current wall clock time by adding
675 * system time (updated by kvm_write_guest_time below) to the
676 * wall clock specified here. guest system time equals host
677 * system time for us, thus we must fill in host boot time here.
678 */
679 now = current_kernel_time();
680 ktime_get_ts(&sys);
681 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
682
683 wc.sec = boot.tv_sec;
684 wc.nsec = boot.tv_nsec;
685 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200686
687 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
688
689 version++;
690 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200691}
692
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200693static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
694{
695 uint32_t quotient, remainder;
696
697 /* Don't try to replace with do_div(), this one calculates
698 * "(dividend << 32) / divisor" */
699 __asm__ ( "divl %4"
700 : "=a" (quotient), "=d" (remainder)
701 : "0" (0), "1" (dividend), "r" (divisor) );
702 return quotient;
703}
704
705static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
706{
707 uint64_t nsecs = 1000000000LL;
708 int32_t shift = 0;
709 uint64_t tps64;
710 uint32_t tps32;
711
712 tps64 = tsc_khz * 1000LL;
713 while (tps64 > nsecs*2) {
714 tps64 >>= 1;
715 shift--;
716 }
717
718 tps32 = (uint32_t)tps64;
719 while (tps32 <= (uint32_t)nsecs) {
720 tps32 <<= 1;
721 shift++;
722 }
723
724 hv_clock->tsc_shift = shift;
725 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
726
727 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700728 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200729 hv_clock->tsc_to_system_mul);
730}
731
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100732static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
733
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200734static void kvm_write_guest_time(struct kvm_vcpu *v)
735{
736 struct timespec ts;
737 unsigned long flags;
738 struct kvm_vcpu_arch *vcpu = &v->arch;
739 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300740 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200741
742 if ((!vcpu->time_page))
743 return;
744
Avi Kivity463656c2009-04-12 15:49:07 +0300745 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
746 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
747 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
748 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200749 }
Avi Kivity463656c2009-04-12 15:49:07 +0300750 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200751
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200752 /* Keep irq disabled to prevent changes to the clock */
753 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530754 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200755 ktime_get_ts(&ts);
756 local_irq_restore(flags);
757
758 /* With all the info we got, fill in the values */
759
760 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400761 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
762
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200763 /*
764 * The interface expects us to write an even number signaling that the
765 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200766 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200767 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200768 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200769
770 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
771
772 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200773 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200774
775 kunmap_atomic(shared_kaddr, KM_USER0);
776
777 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
778}
779
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100780static int kvm_request_guest_time_update(struct kvm_vcpu *v)
781{
782 struct kvm_vcpu_arch *vcpu = &v->arch;
783
784 if (!vcpu->time_page)
785 return 0;
786 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
787 return 1;
788}
789
Avi Kivity9ba075a2008-05-26 20:06:35 +0300790static bool msr_mtrr_valid(unsigned msr)
791{
792 switch (msr) {
793 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
794 case MSR_MTRRfix64K_00000:
795 case MSR_MTRRfix16K_80000:
796 case MSR_MTRRfix16K_A0000:
797 case MSR_MTRRfix4K_C0000:
798 case MSR_MTRRfix4K_C8000:
799 case MSR_MTRRfix4K_D0000:
800 case MSR_MTRRfix4K_D8000:
801 case MSR_MTRRfix4K_E0000:
802 case MSR_MTRRfix4K_E8000:
803 case MSR_MTRRfix4K_F0000:
804 case MSR_MTRRfix4K_F8000:
805 case MSR_MTRRdefType:
806 case MSR_IA32_CR_PAT:
807 return true;
808 case 0x2f8:
809 return true;
810 }
811 return false;
812}
813
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300814static bool valid_pat_type(unsigned t)
815{
816 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
817}
818
819static bool valid_mtrr_type(unsigned t)
820{
821 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
822}
823
824static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
825{
826 int i;
827
828 if (!msr_mtrr_valid(msr))
829 return false;
830
831 if (msr == MSR_IA32_CR_PAT) {
832 for (i = 0; i < 8; i++)
833 if (!valid_pat_type((data >> (i * 8)) & 0xff))
834 return false;
835 return true;
836 } else if (msr == MSR_MTRRdefType) {
837 if (data & ~0xcff)
838 return false;
839 return valid_mtrr_type(data & 0xff);
840 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
841 for (i = 0; i < 8 ; i++)
842 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
843 return false;
844 return true;
845 }
846
847 /* variable MTRRs */
848 return valid_mtrr_type(data & 0xff);
849}
850
Avi Kivity9ba075a2008-05-26 20:06:35 +0300851static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
852{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800853 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
854
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300855 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300856 return 1;
857
Sheng Yang0bed3b52008-10-09 16:01:54 +0800858 if (msr == MSR_MTRRdefType) {
859 vcpu->arch.mtrr_state.def_type = data;
860 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
861 } else if (msr == MSR_MTRRfix64K_00000)
862 p[0] = data;
863 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
864 p[1 + msr - MSR_MTRRfix16K_80000] = data;
865 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
866 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
867 else if (msr == MSR_IA32_CR_PAT)
868 vcpu->arch.pat = data;
869 else { /* Variable MTRRs */
870 int idx, is_mtrr_mask;
871 u64 *pt;
872
873 idx = (msr - 0x200) / 2;
874 is_mtrr_mask = msr - 0x200 - 2 * idx;
875 if (!is_mtrr_mask)
876 pt =
877 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
878 else
879 pt =
880 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
881 *pt = data;
882 }
883
884 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300885 return 0;
886}
Carsten Otte15c4a642007-10-30 18:44:17 +0100887
Huang Ying890ca9a2009-05-11 16:48:15 +0800888static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
889{
890 u64 mcg_cap = vcpu->arch.mcg_cap;
891 unsigned bank_num = mcg_cap & 0xff;
892
893 switch (msr) {
894 case MSR_IA32_MCG_STATUS:
895 vcpu->arch.mcg_status = data;
896 break;
897 case MSR_IA32_MCG_CTL:
898 if (!(mcg_cap & MCG_CTL_P))
899 return 1;
900 if (data != 0 && data != ~(u64)0)
901 return -1;
902 vcpu->arch.mcg_ctl = data;
903 break;
904 default:
905 if (msr >= MSR_IA32_MC0_CTL &&
906 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
907 u32 offset = msr - MSR_IA32_MC0_CTL;
908 /* only 0 or all 1s can be written to IA32_MCi_CTL */
909 if ((offset & 0x3) == 0 &&
910 data != 0 && data != ~(u64)0)
911 return -1;
912 vcpu->arch.mce_banks[offset] = data;
913 break;
914 }
915 return 1;
916 }
917 return 0;
918}
919
Ed Swierkffde22a2009-10-15 15:21:43 -0700920static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
921{
922 struct kvm *kvm = vcpu->kvm;
923 int lm = is_long_mode(vcpu);
924 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
925 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
926 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
927 : kvm->arch.xen_hvm_config.blob_size_32;
928 u32 page_num = data & ~PAGE_MASK;
929 u64 page_addr = data & PAGE_MASK;
930 u8 *page;
931 int r;
932
933 r = -E2BIG;
934 if (page_num >= blob_size)
935 goto out;
936 r = -ENOMEM;
937 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
938 if (!page)
939 goto out;
940 r = -EFAULT;
941 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
942 goto out_free;
943 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
944 goto out_free;
945 r = 0;
946out_free:
947 kfree(page);
948out:
949 return r;
950}
951
Carsten Otte15c4a642007-10-30 18:44:17 +0100952int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
953{
954 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100955 case MSR_EFER:
956 set_efer(vcpu, data);
957 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +0200958 case MSR_K7_HWCR:
959 data &= ~(u64)0x40; /* ignore flush filter disable */
960 if (data != 0) {
961 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
962 data);
963 return 1;
964 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100965 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +0200966 case MSR_FAM10H_MMIO_CONF_BASE:
967 if (data != 0) {
968 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
969 "0x%llx\n", data);
970 return 1;
971 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100972 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +0200973 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100974 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200975 case MSR_IA32_DEBUGCTLMSR:
976 if (!data) {
977 /* We support the non-activated case already */
978 break;
979 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
980 /* Values other than LBR and BTF are vendor-specific,
981 thus reserved and should throw a #GP */
982 return 1;
983 }
984 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
985 __func__, data);
986 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100987 case MSR_IA32_UCODE_REV:
988 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200989 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +0200990 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +0100991 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300992 case 0x200 ... 0x2ff:
993 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100994 case MSR_IA32_APICBASE:
995 kvm_set_apic_base(vcpu, data);
996 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300997 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
998 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100999 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001000 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001001 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001002 case MSR_KVM_WALL_CLOCK:
1003 vcpu->kvm->arch.wall_clock = data;
1004 kvm_write_wall_clock(vcpu->kvm, data);
1005 break;
1006 case MSR_KVM_SYSTEM_TIME: {
1007 if (vcpu->arch.time_page) {
1008 kvm_release_page_dirty(vcpu->arch.time_page);
1009 vcpu->arch.time_page = NULL;
1010 }
1011
1012 vcpu->arch.time = data;
1013
1014 /* we verify if the enable bit is set... */
1015 if (!(data & 1))
1016 break;
1017
1018 /* ...but clean it before doing the actual write */
1019 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1020
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001021 vcpu->arch.time_page =
1022 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001023
1024 if (is_error_page(vcpu->arch.time_page)) {
1025 kvm_release_page_clean(vcpu->arch.time_page);
1026 vcpu->arch.time_page = NULL;
1027 }
1028
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001029 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001030 break;
1031 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001032 case MSR_IA32_MCG_CTL:
1033 case MSR_IA32_MCG_STATUS:
1034 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1035 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001036
1037 /* Performance counters are not protected by a CPUID bit,
1038 * so we should check all of them in the generic path for the sake of
1039 * cross vendor migration.
1040 * Writing a zero into the event select MSRs disables them,
1041 * which we perfectly emulate ;-). Any other value should be at least
1042 * reported, some guests depend on them.
1043 */
1044 case MSR_P6_EVNTSEL0:
1045 case MSR_P6_EVNTSEL1:
1046 case MSR_K7_EVNTSEL0:
1047 case MSR_K7_EVNTSEL1:
1048 case MSR_K7_EVNTSEL2:
1049 case MSR_K7_EVNTSEL3:
1050 if (data != 0)
1051 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1052 "0x%x data 0x%llx\n", msr, data);
1053 break;
1054 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1055 * so we ignore writes to make it happy.
1056 */
1057 case MSR_P6_PERFCTR0:
1058 case MSR_P6_PERFCTR1:
1059 case MSR_K7_PERFCTR0:
1060 case MSR_K7_PERFCTR1:
1061 case MSR_K7_PERFCTR2:
1062 case MSR_K7_PERFCTR3:
1063 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1064 "0x%x data 0x%llx\n", msr, data);
1065 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001066 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001067 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1068 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001069 if (!ignore_msrs) {
1070 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1071 msr, data);
1072 return 1;
1073 } else {
1074 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1075 msr, data);
1076 break;
1077 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001078 }
1079 return 0;
1080}
1081EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1082
1083
1084/*
1085 * Reads an msr value (of 'msr_index') into 'pdata'.
1086 * Returns 0 on success, non-0 otherwise.
1087 * Assumes vcpu_load() was already called.
1088 */
1089int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1090{
1091 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1092}
1093
Avi Kivity9ba075a2008-05-26 20:06:35 +03001094static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1095{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001096 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1097
Avi Kivity9ba075a2008-05-26 20:06:35 +03001098 if (!msr_mtrr_valid(msr))
1099 return 1;
1100
Sheng Yang0bed3b52008-10-09 16:01:54 +08001101 if (msr == MSR_MTRRdefType)
1102 *pdata = vcpu->arch.mtrr_state.def_type +
1103 (vcpu->arch.mtrr_state.enabled << 10);
1104 else if (msr == MSR_MTRRfix64K_00000)
1105 *pdata = p[0];
1106 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1107 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1108 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1109 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1110 else if (msr == MSR_IA32_CR_PAT)
1111 *pdata = vcpu->arch.pat;
1112 else { /* Variable MTRRs */
1113 int idx, is_mtrr_mask;
1114 u64 *pt;
1115
1116 idx = (msr - 0x200) / 2;
1117 is_mtrr_mask = msr - 0x200 - 2 * idx;
1118 if (!is_mtrr_mask)
1119 pt =
1120 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1121 else
1122 pt =
1123 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1124 *pdata = *pt;
1125 }
1126
Avi Kivity9ba075a2008-05-26 20:06:35 +03001127 return 0;
1128}
1129
Huang Ying890ca9a2009-05-11 16:48:15 +08001130static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1131{
1132 u64 data;
1133 u64 mcg_cap = vcpu->arch.mcg_cap;
1134 unsigned bank_num = mcg_cap & 0xff;
1135
1136 switch (msr) {
1137 case MSR_IA32_P5_MC_ADDR:
1138 case MSR_IA32_P5_MC_TYPE:
1139 data = 0;
1140 break;
1141 case MSR_IA32_MCG_CAP:
1142 data = vcpu->arch.mcg_cap;
1143 break;
1144 case MSR_IA32_MCG_CTL:
1145 if (!(mcg_cap & MCG_CTL_P))
1146 return 1;
1147 data = vcpu->arch.mcg_ctl;
1148 break;
1149 case MSR_IA32_MCG_STATUS:
1150 data = vcpu->arch.mcg_status;
1151 break;
1152 default:
1153 if (msr >= MSR_IA32_MC0_CTL &&
1154 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1155 u32 offset = msr - MSR_IA32_MC0_CTL;
1156 data = vcpu->arch.mce_banks[offset];
1157 break;
1158 }
1159 return 1;
1160 }
1161 *pdata = data;
1162 return 0;
1163}
1164
Carsten Otte15c4a642007-10-30 18:44:17 +01001165int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1166{
1167 u64 data;
1168
1169 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001170 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001171 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001172 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001173 case MSR_IA32_DEBUGCTLMSR:
1174 case MSR_IA32_LASTBRANCHFROMIP:
1175 case MSR_IA32_LASTBRANCHTOIP:
1176 case MSR_IA32_LASTINTFROMIP:
1177 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301178 case MSR_K8_SYSCFG:
1179 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001180 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301181 case MSR_P6_PERFCTR0:
1182 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301183 case MSR_P6_EVNTSEL0:
1184 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301185 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301186 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001187 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001188 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001189 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001190 data = 0;
1191 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001192 case MSR_MTRRcap:
1193 data = 0x500 | KVM_NR_VAR_MTRR;
1194 break;
1195 case 0x200 ... 0x2ff:
1196 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001197 case 0xcd: /* fsb frequency */
1198 data = 3;
1199 break;
1200 case MSR_IA32_APICBASE:
1201 data = kvm_get_apic_base(vcpu);
1202 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001203 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1204 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1205 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001206 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001207 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001208 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001209 case MSR_IA32_PERF_STATUS:
1210 /* TSC increment by tick */
1211 data = 1000ULL;
1212 /* CPU multiplier */
1213 data |= (((uint64_t)4ULL) << 40);
1214 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001215 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001216 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001217 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001218 case MSR_KVM_WALL_CLOCK:
1219 data = vcpu->kvm->arch.wall_clock;
1220 break;
1221 case MSR_KVM_SYSTEM_TIME:
1222 data = vcpu->arch.time;
1223 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001224 case MSR_IA32_P5_MC_ADDR:
1225 case MSR_IA32_P5_MC_TYPE:
1226 case MSR_IA32_MCG_CAP:
1227 case MSR_IA32_MCG_CTL:
1228 case MSR_IA32_MCG_STATUS:
1229 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1230 return get_msr_mce(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001231 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001232 if (!ignore_msrs) {
1233 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1234 return 1;
1235 } else {
1236 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1237 data = 0;
1238 }
1239 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001240 }
1241 *pdata = data;
1242 return 0;
1243}
1244EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1245
Carsten Otte313a3dc2007-10-11 19:16:52 +02001246/*
1247 * Read or write a bunch of msrs. All parameters are kernel addresses.
1248 *
1249 * @return number of msrs set successfully.
1250 */
1251static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1252 struct kvm_msr_entry *entries,
1253 int (*do_msr)(struct kvm_vcpu *vcpu,
1254 unsigned index, u64 *data))
1255{
1256 int i;
1257
1258 vcpu_load(vcpu);
1259
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001260 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001261 for (i = 0; i < msrs->nmsrs; ++i)
1262 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1263 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001264 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001265
1266 vcpu_put(vcpu);
1267
1268 return i;
1269}
1270
1271/*
1272 * Read or write a bunch of msrs. Parameters are user addresses.
1273 *
1274 * @return number of msrs set successfully.
1275 */
1276static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1277 int (*do_msr)(struct kvm_vcpu *vcpu,
1278 unsigned index, u64 *data),
1279 int writeback)
1280{
1281 struct kvm_msrs msrs;
1282 struct kvm_msr_entry *entries;
1283 int r, n;
1284 unsigned size;
1285
1286 r = -EFAULT;
1287 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1288 goto out;
1289
1290 r = -E2BIG;
1291 if (msrs.nmsrs >= MAX_IO_MSRS)
1292 goto out;
1293
1294 r = -ENOMEM;
1295 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1296 entries = vmalloc(size);
1297 if (!entries)
1298 goto out;
1299
1300 r = -EFAULT;
1301 if (copy_from_user(entries, user_msrs->entries, size))
1302 goto out_free;
1303
1304 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1305 if (r < 0)
1306 goto out_free;
1307
1308 r = -EFAULT;
1309 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1310 goto out_free;
1311
1312 r = n;
1313
1314out_free:
1315 vfree(entries);
1316out:
1317 return r;
1318}
1319
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001320int kvm_dev_ioctl_check_extension(long ext)
1321{
1322 int r;
1323
1324 switch (ext) {
1325 case KVM_CAP_IRQCHIP:
1326 case KVM_CAP_HLT:
1327 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001328 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001329 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001330 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001331 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001332 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001333 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001334 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001335 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001336 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001337 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001338 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001339 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001340 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001341 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001342 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001343 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001344 case KVM_CAP_ADJUST_CLOCK:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001345 r = 1;
1346 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001347 case KVM_CAP_COALESCED_MMIO:
1348 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1349 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001350 case KVM_CAP_VAPIC:
1351 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1352 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001353 case KVM_CAP_NR_VCPUS:
1354 r = KVM_MAX_VCPUS;
1355 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001356 case KVM_CAP_NR_MEMSLOTS:
1357 r = KVM_MEMORY_SLOTS;
1358 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001359 case KVM_CAP_PV_MMU: /* obsolete */
1360 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001361 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001362 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001363 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001364 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001365 case KVM_CAP_MCE:
1366 r = KVM_MAX_MCE_BANKS;
1367 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001368 default:
1369 r = 0;
1370 break;
1371 }
1372 return r;
1373
1374}
1375
Carsten Otte043405e2007-10-10 17:16:19 +02001376long kvm_arch_dev_ioctl(struct file *filp,
1377 unsigned int ioctl, unsigned long arg)
1378{
1379 void __user *argp = (void __user *)arg;
1380 long r;
1381
1382 switch (ioctl) {
1383 case KVM_GET_MSR_INDEX_LIST: {
1384 struct kvm_msr_list __user *user_msr_list = argp;
1385 struct kvm_msr_list msr_list;
1386 unsigned n;
1387
1388 r = -EFAULT;
1389 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1390 goto out;
1391 n = msr_list.nmsrs;
1392 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1393 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1394 goto out;
1395 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001396 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001397 goto out;
1398 r = -EFAULT;
1399 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1400 num_msrs_to_save * sizeof(u32)))
1401 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001402 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001403 &emulated_msrs,
1404 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1405 goto out;
1406 r = 0;
1407 break;
1408 }
Avi Kivity674eea02008-02-11 18:37:23 +02001409 case KVM_GET_SUPPORTED_CPUID: {
1410 struct kvm_cpuid2 __user *cpuid_arg = argp;
1411 struct kvm_cpuid2 cpuid;
1412
1413 r = -EFAULT;
1414 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1415 goto out;
1416 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001417 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001418 if (r)
1419 goto out;
1420
1421 r = -EFAULT;
1422 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1423 goto out;
1424 r = 0;
1425 break;
1426 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001427 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1428 u64 mce_cap;
1429
1430 mce_cap = KVM_MCE_CAP_SUPPORTED;
1431 r = -EFAULT;
1432 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1433 goto out;
1434 r = 0;
1435 break;
1436 }
Carsten Otte043405e2007-10-10 17:16:19 +02001437 default:
1438 r = -EINVAL;
1439 }
1440out:
1441 return r;
1442}
1443
Carsten Otte313a3dc2007-10-11 19:16:52 +02001444void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1445{
1446 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001447 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1448 unsigned long khz = cpufreq_quick_get(cpu);
1449 if (!khz)
1450 khz = tsc_khz;
1451 per_cpu(cpu_tsc_khz, cpu) = khz;
1452 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001453 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001454}
1455
1456void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1457{
1458 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001459 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001460}
1461
Dan Kenigsberg07716712007-11-21 17:10:04 +02001462static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001463{
Avi Kivitye286e862009-05-03 18:50:55 +03001464 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001465
Avi Kivitye286e862009-05-03 18:50:55 +03001466 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001467 return efer & EFER_NX;
1468}
1469
1470static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1471{
1472 int i;
1473 struct kvm_cpuid_entry2 *e, *entry;
1474
Carsten Otte313a3dc2007-10-11 19:16:52 +02001475 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001476 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1477 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001478 if (e->function == 0x80000001) {
1479 entry = e;
1480 break;
1481 }
1482 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001483 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001484 entry->edx &= ~(1 << 20);
1485 printk(KERN_INFO "kvm: guest NX capability removed\n");
1486 }
1487}
1488
Dan Kenigsberg07716712007-11-21 17:10:04 +02001489/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001490static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1491 struct kvm_cpuid *cpuid,
1492 struct kvm_cpuid_entry __user *entries)
1493{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001494 int r, i;
1495 struct kvm_cpuid_entry *cpuid_entries;
1496
1497 r = -E2BIG;
1498 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1499 goto out;
1500 r = -ENOMEM;
1501 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1502 if (!cpuid_entries)
1503 goto out;
1504 r = -EFAULT;
1505 if (copy_from_user(cpuid_entries, entries,
1506 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1507 goto out_free;
1508 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001509 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1510 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1511 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1512 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1513 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1514 vcpu->arch.cpuid_entries[i].index = 0;
1515 vcpu->arch.cpuid_entries[i].flags = 0;
1516 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1517 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1518 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001519 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001520 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001521 cpuid_fix_nx_cap(vcpu);
1522 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001523 kvm_apic_set_version(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001524
1525out_free:
1526 vfree(cpuid_entries);
1527out:
1528 return r;
1529}
1530
1531static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001532 struct kvm_cpuid2 *cpuid,
1533 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001534{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001535 int r;
1536
1537 r = -E2BIG;
1538 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1539 goto out;
1540 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001541 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001542 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001543 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001544 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001545 kvm_apic_set_version(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001546 return 0;
1547
1548out:
1549 return r;
1550}
1551
Dan Kenigsberg07716712007-11-21 17:10:04 +02001552static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001553 struct kvm_cpuid2 *cpuid,
1554 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001555{
1556 int r;
1557
1558 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001559 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001560 goto out;
1561 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001562 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001563 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001564 goto out;
1565 return 0;
1566
1567out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001568 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001569 return r;
1570}
1571
Dan Kenigsberg07716712007-11-21 17:10:04 +02001572static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001573 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001574{
1575 entry->function = function;
1576 entry->index = index;
1577 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001578 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001579 entry->flags = 0;
1580}
1581
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001582#define F(x) bit(X86_FEATURE_##x)
1583
Dan Kenigsberg07716712007-11-21 17:10:04 +02001584static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1585 u32 index, int *nent, int maxnent)
1586{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001587 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Joerg Roedel344f4142009-07-27 16:30:48 +02001588 unsigned f_gbpages = kvm_x86_ops->gb_page_enable() ? F(GBPAGES) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001589#ifdef CONFIG_X86_64
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001590 unsigned f_lm = F(LM);
1591#else
1592 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001593#endif
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001594
1595 /* cpuid 1.edx */
1596 const u32 kvm_supported_word0_x86_features =
1597 F(FPU) | F(VME) | F(DE) | F(PSE) |
1598 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1599 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1600 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1601 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1602 0 /* Reserved, DS, ACPI */ | F(MMX) |
1603 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1604 0 /* HTT, TM, Reserved, PBE */;
1605 /* cpuid 0x80000001.edx */
1606 const u32 kvm_supported_word1_x86_features =
1607 F(FPU) | F(VME) | F(DE) | F(PSE) |
1608 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1609 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1610 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1611 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1612 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Joerg Roedel344f4142009-07-27 16:30:48 +02001613 F(FXSR) | F(FXSR_OPT) | f_gbpages | 0 /* RDTSCP */ |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001614 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1615 /* cpuid 1.ecx */
1616 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001617 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1618 0 /* DS-CPL, VMX, SMX, EST */ |
1619 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1620 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1621 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001622 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001623 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001624 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001625 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001626 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1627 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1628 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1629 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001630
Amit Shah19355472009-01-14 16:56:00 +00001631 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001632 get_cpu();
1633 do_cpuid_1_ent(entry, function, index);
1634 ++*nent;
1635
1636 switch (function) {
1637 case 0:
1638 entry->eax = min(entry->eax, (u32)0xb);
1639 break;
1640 case 1:
1641 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001642 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001643 /* we support x2apic emulation even if host does not support
1644 * it since we emulate x2apic in software */
1645 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001646 break;
1647 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1648 * may return different values. This forces us to get_cpu() before
1649 * issuing the first command, and also to emulate this annoying behavior
1650 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1651 case 2: {
1652 int t, times = entry->eax & 0xff;
1653
1654 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001655 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001656 for (t = 1; t < times && *nent < maxnent; ++t) {
1657 do_cpuid_1_ent(&entry[t], function, 0);
1658 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1659 ++*nent;
1660 }
1661 break;
1662 }
1663 /* function 4 and 0xb have additional index. */
1664 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001665 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001666
1667 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1668 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001669 for (i = 1; *nent < maxnent; ++i) {
1670 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001671 if (!cache_type)
1672 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001673 do_cpuid_1_ent(&entry[i], function, i);
1674 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001675 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1676 ++*nent;
1677 }
1678 break;
1679 }
1680 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001681 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001682
1683 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1684 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001685 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001686 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001687 if (!level_type)
1688 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001689 do_cpuid_1_ent(&entry[i], function, i);
1690 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001691 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1692 ++*nent;
1693 }
1694 break;
1695 }
1696 case 0x80000000:
1697 entry->eax = min(entry->eax, 0x8000001a);
1698 break;
1699 case 0x80000001:
1700 entry->edx &= kvm_supported_word1_x86_features;
1701 entry->ecx &= kvm_supported_word6_x86_features;
1702 break;
1703 }
1704 put_cpu();
1705}
1706
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001707#undef F
1708
Avi Kivity674eea02008-02-11 18:37:23 +02001709static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001710 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001711{
1712 struct kvm_cpuid_entry2 *cpuid_entries;
1713 int limit, nent = 0, r = -E2BIG;
1714 u32 func;
1715
1716 if (cpuid->nent < 1)
1717 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001718 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1719 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001720 r = -ENOMEM;
1721 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1722 if (!cpuid_entries)
1723 goto out;
1724
1725 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1726 limit = cpuid_entries[0].eax;
1727 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1728 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001729 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001730 r = -E2BIG;
1731 if (nent >= cpuid->nent)
1732 goto out_free;
1733
1734 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1735 limit = cpuid_entries[nent - 1].eax;
1736 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1737 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001738 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001739 r = -E2BIG;
1740 if (nent >= cpuid->nent)
1741 goto out_free;
1742
Dan Kenigsberg07716712007-11-21 17:10:04 +02001743 r = -EFAULT;
1744 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001745 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001746 goto out_free;
1747 cpuid->nent = nent;
1748 r = 0;
1749
1750out_free:
1751 vfree(cpuid_entries);
1752out:
1753 return r;
1754}
1755
Carsten Otte313a3dc2007-10-11 19:16:52 +02001756static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1757 struct kvm_lapic_state *s)
1758{
1759 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001760 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001761 vcpu_put(vcpu);
1762
1763 return 0;
1764}
1765
1766static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1767 struct kvm_lapic_state *s)
1768{
1769 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001770 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001771 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03001772 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001773 vcpu_put(vcpu);
1774
1775 return 0;
1776}
1777
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001778static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1779 struct kvm_interrupt *irq)
1780{
1781 if (irq->irq < 0 || irq->irq >= 256)
1782 return -EINVAL;
1783 if (irqchip_in_kernel(vcpu->kvm))
1784 return -ENXIO;
1785 vcpu_load(vcpu);
1786
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001787 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001788
1789 vcpu_put(vcpu);
1790
1791 return 0;
1792}
1793
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001794static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1795{
1796 vcpu_load(vcpu);
1797 kvm_inject_nmi(vcpu);
1798 vcpu_put(vcpu);
1799
1800 return 0;
1801}
1802
Avi Kivityb209749f2007-10-22 16:50:39 +02001803static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1804 struct kvm_tpr_access_ctl *tac)
1805{
1806 if (tac->flags)
1807 return -EINVAL;
1808 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1809 return 0;
1810}
1811
Huang Ying890ca9a2009-05-11 16:48:15 +08001812static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1813 u64 mcg_cap)
1814{
1815 int r;
1816 unsigned bank_num = mcg_cap & 0xff, bank;
1817
1818 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02001819 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08001820 goto out;
1821 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1822 goto out;
1823 r = 0;
1824 vcpu->arch.mcg_cap = mcg_cap;
1825 /* Init IA32_MCG_CTL to all 1s */
1826 if (mcg_cap & MCG_CTL_P)
1827 vcpu->arch.mcg_ctl = ~(u64)0;
1828 /* Init IA32_MCi_CTL to all 1s */
1829 for (bank = 0; bank < bank_num; bank++)
1830 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1831out:
1832 return r;
1833}
1834
1835static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1836 struct kvm_x86_mce *mce)
1837{
1838 u64 mcg_cap = vcpu->arch.mcg_cap;
1839 unsigned bank_num = mcg_cap & 0xff;
1840 u64 *banks = vcpu->arch.mce_banks;
1841
1842 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1843 return -EINVAL;
1844 /*
1845 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1846 * reporting is disabled
1847 */
1848 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1849 vcpu->arch.mcg_ctl != ~(u64)0)
1850 return 0;
1851 banks += 4 * mce->bank;
1852 /*
1853 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1854 * reporting is disabled for the bank
1855 */
1856 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1857 return 0;
1858 if (mce->status & MCI_STATUS_UC) {
1859 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1860 !(vcpu->arch.cr4 & X86_CR4_MCE)) {
1861 printk(KERN_DEBUG "kvm: set_mce: "
1862 "injects mce exception while "
1863 "previous one is in progress!\n");
1864 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1865 return 0;
1866 }
1867 if (banks[1] & MCI_STATUS_VAL)
1868 mce->status |= MCI_STATUS_OVER;
1869 banks[2] = mce->addr;
1870 banks[3] = mce->misc;
1871 vcpu->arch.mcg_status = mce->mcg_status;
1872 banks[1] = mce->status;
1873 kvm_queue_exception(vcpu, MC_VECTOR);
1874 } else if (!(banks[1] & MCI_STATUS_VAL)
1875 || !(banks[1] & MCI_STATUS_UC)) {
1876 if (banks[1] & MCI_STATUS_VAL)
1877 mce->status |= MCI_STATUS_OVER;
1878 banks[2] = mce->addr;
1879 banks[3] = mce->misc;
1880 banks[1] = mce->status;
1881 } else
1882 banks[1] |= MCI_STATUS_OVER;
1883 return 0;
1884}
1885
Carsten Otte313a3dc2007-10-11 19:16:52 +02001886long kvm_arch_vcpu_ioctl(struct file *filp,
1887 unsigned int ioctl, unsigned long arg)
1888{
1889 struct kvm_vcpu *vcpu = filp->private_data;
1890 void __user *argp = (void __user *)arg;
1891 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001892 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001893
1894 switch (ioctl) {
1895 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001896 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001897
Dave Hansenb772ff32008-08-11 10:01:47 -07001898 r = -ENOMEM;
1899 if (!lapic)
1900 goto out;
1901 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001902 if (r)
1903 goto out;
1904 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001905 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001906 goto out;
1907 r = 0;
1908 break;
1909 }
1910 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001911 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1912 r = -ENOMEM;
1913 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001914 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001915 r = -EFAULT;
1916 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1917 goto out;
1918 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001919 if (r)
1920 goto out;
1921 r = 0;
1922 break;
1923 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001924 case KVM_INTERRUPT: {
1925 struct kvm_interrupt irq;
1926
1927 r = -EFAULT;
1928 if (copy_from_user(&irq, argp, sizeof irq))
1929 goto out;
1930 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1931 if (r)
1932 goto out;
1933 r = 0;
1934 break;
1935 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001936 case KVM_NMI: {
1937 r = kvm_vcpu_ioctl_nmi(vcpu);
1938 if (r)
1939 goto out;
1940 r = 0;
1941 break;
1942 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001943 case KVM_SET_CPUID: {
1944 struct kvm_cpuid __user *cpuid_arg = argp;
1945 struct kvm_cpuid cpuid;
1946
1947 r = -EFAULT;
1948 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1949 goto out;
1950 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1951 if (r)
1952 goto out;
1953 break;
1954 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001955 case KVM_SET_CPUID2: {
1956 struct kvm_cpuid2 __user *cpuid_arg = argp;
1957 struct kvm_cpuid2 cpuid;
1958
1959 r = -EFAULT;
1960 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1961 goto out;
1962 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001963 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001964 if (r)
1965 goto out;
1966 break;
1967 }
1968 case KVM_GET_CPUID2: {
1969 struct kvm_cpuid2 __user *cpuid_arg = argp;
1970 struct kvm_cpuid2 cpuid;
1971
1972 r = -EFAULT;
1973 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1974 goto out;
1975 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001976 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001977 if (r)
1978 goto out;
1979 r = -EFAULT;
1980 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1981 goto out;
1982 r = 0;
1983 break;
1984 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001985 case KVM_GET_MSRS:
1986 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1987 break;
1988 case KVM_SET_MSRS:
1989 r = msr_io(vcpu, argp, do_set_msr, 0);
1990 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001991 case KVM_TPR_ACCESS_REPORTING: {
1992 struct kvm_tpr_access_ctl tac;
1993
1994 r = -EFAULT;
1995 if (copy_from_user(&tac, argp, sizeof tac))
1996 goto out;
1997 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1998 if (r)
1999 goto out;
2000 r = -EFAULT;
2001 if (copy_to_user(argp, &tac, sizeof tac))
2002 goto out;
2003 r = 0;
2004 break;
2005 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002006 case KVM_SET_VAPIC_ADDR: {
2007 struct kvm_vapic_addr va;
2008
2009 r = -EINVAL;
2010 if (!irqchip_in_kernel(vcpu->kvm))
2011 goto out;
2012 r = -EFAULT;
2013 if (copy_from_user(&va, argp, sizeof va))
2014 goto out;
2015 r = 0;
2016 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2017 break;
2018 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002019 case KVM_X86_SETUP_MCE: {
2020 u64 mcg_cap;
2021
2022 r = -EFAULT;
2023 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2024 goto out;
2025 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2026 break;
2027 }
2028 case KVM_X86_SET_MCE: {
2029 struct kvm_x86_mce mce;
2030
2031 r = -EFAULT;
2032 if (copy_from_user(&mce, argp, sizeof mce))
2033 goto out;
2034 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2035 break;
2036 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002037 default:
2038 r = -EINVAL;
2039 }
2040out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002041 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002042 return r;
2043}
2044
Carsten Otte1fe779f2007-10-29 16:08:35 +01002045static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2046{
2047 int ret;
2048
2049 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2050 return -1;
2051 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2052 return ret;
2053}
2054
Sheng Yangb927a3c2009-07-21 10:42:48 +08002055static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2056 u64 ident_addr)
2057{
2058 kvm->arch.ept_identity_map_addr = ident_addr;
2059 return 0;
2060}
2061
Carsten Otte1fe779f2007-10-29 16:08:35 +01002062static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2063 u32 kvm_nr_mmu_pages)
2064{
2065 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2066 return -EINVAL;
2067
Izik Eidus72dc67a2008-02-10 18:04:15 +02002068 down_write(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002069 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002070
2071 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002072 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002073
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002074 spin_unlock(&kvm->mmu_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002075 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002076 return 0;
2077}
2078
2079static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2080{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002081 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002082}
2083
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002084gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2085{
2086 int i;
2087 struct kvm_mem_alias *alias;
2088
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002089 for (i = 0; i < kvm->arch.naliases; ++i) {
2090 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002091 if (gfn >= alias->base_gfn
2092 && gfn < alias->base_gfn + alias->npages)
2093 return alias->target_gfn + gfn - alias->base_gfn;
2094 }
2095 return gfn;
2096}
2097
Carsten Otte1fe779f2007-10-29 16:08:35 +01002098/*
2099 * Set a new alias region. Aliases map a portion of physical memory into
2100 * another portion. This is useful for memory windows, for example the PC
2101 * VGA region.
2102 */
2103static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2104 struct kvm_memory_alias *alias)
2105{
2106 int r, n;
2107 struct kvm_mem_alias *p;
2108
2109 r = -EINVAL;
2110 /* General sanity checks */
2111 if (alias->memory_size & (PAGE_SIZE - 1))
2112 goto out;
2113 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2114 goto out;
2115 if (alias->slot >= KVM_ALIAS_SLOTS)
2116 goto out;
2117 if (alias->guest_phys_addr + alias->memory_size
2118 < alias->guest_phys_addr)
2119 goto out;
2120 if (alias->target_phys_addr + alias->memory_size
2121 < alias->target_phys_addr)
2122 goto out;
2123
Izik Eidus72dc67a2008-02-10 18:04:15 +02002124 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02002125 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002126
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002127 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002128 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2129 p->npages = alias->memory_size >> PAGE_SHIFT;
2130 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
2131
2132 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002133 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002134 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002135 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002136
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02002137 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002138 kvm_mmu_zap_all(kvm);
2139
Izik Eidus72dc67a2008-02-10 18:04:15 +02002140 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002141
2142 return 0;
2143
2144out:
2145 return r;
2146}
2147
2148static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2149{
2150 int r;
2151
2152 r = 0;
2153 switch (chip->chip_id) {
2154 case KVM_IRQCHIP_PIC_MASTER:
2155 memcpy(&chip->chip.pic,
2156 &pic_irqchip(kvm)->pics[0],
2157 sizeof(struct kvm_pic_state));
2158 break;
2159 case KVM_IRQCHIP_PIC_SLAVE:
2160 memcpy(&chip->chip.pic,
2161 &pic_irqchip(kvm)->pics[1],
2162 sizeof(struct kvm_pic_state));
2163 break;
2164 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002165 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002166 break;
2167 default:
2168 r = -EINVAL;
2169 break;
2170 }
2171 return r;
2172}
2173
2174static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2175{
2176 int r;
2177
2178 r = 0;
2179 switch (chip->chip_id) {
2180 case KVM_IRQCHIP_PIC_MASTER:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002181 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002182 memcpy(&pic_irqchip(kvm)->pics[0],
2183 &chip->chip.pic,
2184 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002185 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002186 break;
2187 case KVM_IRQCHIP_PIC_SLAVE:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002188 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002189 memcpy(&pic_irqchip(kvm)->pics[1],
2190 &chip->chip.pic,
2191 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002192 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002193 break;
2194 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002195 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002196 break;
2197 default:
2198 r = -EINVAL;
2199 break;
2200 }
2201 kvm_pic_update_irq(pic_irqchip(kvm));
2202 return r;
2203}
2204
Sheng Yange0f63cb2008-03-04 00:50:59 +08002205static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2206{
2207 int r = 0;
2208
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002209 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002210 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002211 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002212 return r;
2213}
2214
2215static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2216{
2217 int r = 0;
2218
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002219 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002220 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002221 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2222 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2223 return r;
2224}
2225
2226static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2227{
2228 int r = 0;
2229
2230 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2231 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2232 sizeof(ps->channels));
2233 ps->flags = kvm->arch.vpit->pit_state.flags;
2234 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2235 return r;
2236}
2237
2238static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2239{
2240 int r = 0, start = 0;
2241 u32 prev_legacy, cur_legacy;
2242 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2243 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2244 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2245 if (!prev_legacy && cur_legacy)
2246 start = 1;
2247 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2248 sizeof(kvm->arch.vpit->pit_state.channels));
2249 kvm->arch.vpit->pit_state.flags = ps->flags;
2250 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002251 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002252 return r;
2253}
2254
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002255static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2256 struct kvm_reinject_control *control)
2257{
2258 if (!kvm->arch.vpit)
2259 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002260 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002261 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002262 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002263 return 0;
2264}
2265
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002266/*
2267 * Get (and clear) the dirty memory log for a memory slot.
2268 */
2269int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2270 struct kvm_dirty_log *log)
2271{
2272 int r;
2273 int n;
2274 struct kvm_memory_slot *memslot;
2275 int is_dirty = 0;
2276
Izik Eidus72dc67a2008-02-10 18:04:15 +02002277 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002278
2279 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2280 if (r)
2281 goto out;
2282
2283 /* If nothing is dirty, don't bother messing with page tables. */
2284 if (is_dirty) {
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002285 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002286 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002287 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002288 memslot = &kvm->memslots[log->slot];
2289 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2290 memset(memslot->dirty_bitmap, 0, n);
2291 }
2292 r = 0;
2293out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002294 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002295 return r;
2296}
2297
Carsten Otte1fe779f2007-10-29 16:08:35 +01002298long kvm_arch_vm_ioctl(struct file *filp,
2299 unsigned int ioctl, unsigned long arg)
2300{
2301 struct kvm *kvm = filp->private_data;
2302 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002303 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002304 /*
2305 * This union makes it completely explicit to gcc-3.x
2306 * that these two variables' stack usage should be
2307 * combined, not added together.
2308 */
2309 union {
2310 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002311 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002312 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002313 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002314 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002315
2316 switch (ioctl) {
2317 case KVM_SET_TSS_ADDR:
2318 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2319 if (r < 0)
2320 goto out;
2321 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002322 case KVM_SET_IDENTITY_MAP_ADDR: {
2323 u64 ident_addr;
2324
2325 r = -EFAULT;
2326 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2327 goto out;
2328 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2329 if (r < 0)
2330 goto out;
2331 break;
2332 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002333 case KVM_SET_MEMORY_REGION: {
2334 struct kvm_memory_region kvm_mem;
2335 struct kvm_userspace_memory_region kvm_userspace_mem;
2336
2337 r = -EFAULT;
2338 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2339 goto out;
2340 kvm_userspace_mem.slot = kvm_mem.slot;
2341 kvm_userspace_mem.flags = kvm_mem.flags;
2342 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2343 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2344 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2345 if (r)
2346 goto out;
2347 break;
2348 }
2349 case KVM_SET_NR_MMU_PAGES:
2350 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2351 if (r)
2352 goto out;
2353 break;
2354 case KVM_GET_NR_MMU_PAGES:
2355 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2356 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002357 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002358 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002359 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002360 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002361 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002362 if (r)
2363 goto out;
2364 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002365 case KVM_CREATE_IRQCHIP: {
2366 struct kvm_pic *vpic;
2367
2368 mutex_lock(&kvm->lock);
2369 r = -EEXIST;
2370 if (kvm->arch.vpic)
2371 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002372 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002373 vpic = kvm_create_pic(kvm);
2374 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002375 r = kvm_ioapic_init(kvm);
2376 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002377 kfree(vpic);
2378 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002379 }
2380 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002381 goto create_irqchip_unlock;
2382 smp_wmb();
2383 kvm->arch.vpic = vpic;
2384 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002385 r = kvm_setup_default_irq_routing(kvm);
2386 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002387 mutex_lock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002388 kfree(kvm->arch.vpic);
2389 kfree(kvm->arch.vioapic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002390 kvm->arch.vpic = NULL;
2391 kvm->arch.vioapic = NULL;
2392 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002393 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002394 create_irqchip_unlock:
2395 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002396 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002397 }
Sheng Yang78376992008-01-28 05:10:22 +08002398 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002399 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2400 goto create_pit;
2401 case KVM_CREATE_PIT2:
2402 r = -EFAULT;
2403 if (copy_from_user(&u.pit_config, argp,
2404 sizeof(struct kvm_pit_config)))
2405 goto out;
2406 create_pit:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002407 down_write(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002408 r = -EEXIST;
2409 if (kvm->arch.vpit)
2410 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002411 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002412 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002413 if (kvm->arch.vpit)
2414 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002415 create_pit_unlock:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002416 up_write(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002417 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002418 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002419 case KVM_IRQ_LINE: {
2420 struct kvm_irq_level irq_event;
2421
2422 r = -EFAULT;
2423 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2424 goto out;
2425 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002426 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002427 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2428 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002429 if (ioctl == KVM_IRQ_LINE_STATUS) {
2430 irq_event.status = status;
2431 if (copy_to_user(argp, &irq_event,
2432 sizeof irq_event))
2433 goto out;
2434 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002435 r = 0;
2436 }
2437 break;
2438 }
2439 case KVM_GET_IRQCHIP: {
2440 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002441 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002442
Dave Hansenf0d66272008-08-11 10:01:45 -07002443 r = -ENOMEM;
2444 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002445 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002446 r = -EFAULT;
2447 if (copy_from_user(chip, argp, sizeof *chip))
2448 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002449 r = -ENXIO;
2450 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002451 goto get_irqchip_out;
2452 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2453 if (r)
2454 goto get_irqchip_out;
2455 r = -EFAULT;
2456 if (copy_to_user(argp, chip, sizeof *chip))
2457 goto get_irqchip_out;
2458 r = 0;
2459 get_irqchip_out:
2460 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002461 if (r)
2462 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002463 break;
2464 }
2465 case KVM_SET_IRQCHIP: {
2466 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002467 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002468
Dave Hansenf0d66272008-08-11 10:01:45 -07002469 r = -ENOMEM;
2470 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002471 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002472 r = -EFAULT;
2473 if (copy_from_user(chip, argp, sizeof *chip))
2474 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002475 r = -ENXIO;
2476 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002477 goto set_irqchip_out;
2478 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2479 if (r)
2480 goto set_irqchip_out;
2481 r = 0;
2482 set_irqchip_out:
2483 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002484 if (r)
2485 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002486 break;
2487 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002488 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002489 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002490 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002491 goto out;
2492 r = -ENXIO;
2493 if (!kvm->arch.vpit)
2494 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002495 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002496 if (r)
2497 goto out;
2498 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002499 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002500 goto out;
2501 r = 0;
2502 break;
2503 }
2504 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002505 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002506 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002507 goto out;
2508 r = -ENXIO;
2509 if (!kvm->arch.vpit)
2510 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002511 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002512 if (r)
2513 goto out;
2514 r = 0;
2515 break;
2516 }
Beth Kone9f42752009-07-07 11:50:38 -04002517 case KVM_GET_PIT2: {
2518 r = -ENXIO;
2519 if (!kvm->arch.vpit)
2520 goto out;
2521 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2522 if (r)
2523 goto out;
2524 r = -EFAULT;
2525 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2526 goto out;
2527 r = 0;
2528 break;
2529 }
2530 case KVM_SET_PIT2: {
2531 r = -EFAULT;
2532 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2533 goto out;
2534 r = -ENXIO;
2535 if (!kvm->arch.vpit)
2536 goto out;
2537 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2538 if (r)
2539 goto out;
2540 r = 0;
2541 break;
2542 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002543 case KVM_REINJECT_CONTROL: {
2544 struct kvm_reinject_control control;
2545 r = -EFAULT;
2546 if (copy_from_user(&control, argp, sizeof(control)))
2547 goto out;
2548 r = kvm_vm_ioctl_reinject(kvm, &control);
2549 if (r)
2550 goto out;
2551 r = 0;
2552 break;
2553 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002554 case KVM_XEN_HVM_CONFIG: {
2555 r = -EFAULT;
2556 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2557 sizeof(struct kvm_xen_hvm_config)))
2558 goto out;
2559 r = -EINVAL;
2560 if (kvm->arch.xen_hvm_config.flags)
2561 goto out;
2562 r = 0;
2563 break;
2564 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002565 case KVM_SET_CLOCK: {
2566 struct timespec now;
2567 struct kvm_clock_data user_ns;
2568 u64 now_ns;
2569 s64 delta;
2570
2571 r = -EFAULT;
2572 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2573 goto out;
2574
2575 r = -EINVAL;
2576 if (user_ns.flags)
2577 goto out;
2578
2579 r = 0;
2580 ktime_get_ts(&now);
2581 now_ns = timespec_to_ns(&now);
2582 delta = user_ns.clock - now_ns;
2583 kvm->arch.kvmclock_offset = delta;
2584 break;
2585 }
2586 case KVM_GET_CLOCK: {
2587 struct timespec now;
2588 struct kvm_clock_data user_ns;
2589 u64 now_ns;
2590
2591 ktime_get_ts(&now);
2592 now_ns = timespec_to_ns(&now);
2593 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2594 user_ns.flags = 0;
2595
2596 r = -EFAULT;
2597 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2598 goto out;
2599 r = 0;
2600 break;
2601 }
2602
Carsten Otte1fe779f2007-10-29 16:08:35 +01002603 default:
2604 ;
2605 }
2606out:
2607 return r;
2608}
2609
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002610static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002611{
2612 u32 dummy[2];
2613 unsigned i, j;
2614
Glauber Costae3267cb2009-10-06 13:24:50 -04002615 /* skip the first msrs in the list. KVM-specific */
2616 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02002617 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2618 continue;
2619 if (j < i)
2620 msrs_to_save[j] = msrs_to_save[i];
2621 j++;
2622 }
2623 num_msrs_to_save = j;
2624}
2625
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002626static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2627 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002628{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002629 if (vcpu->arch.apic &&
2630 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2631 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002632
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002633 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002634}
2635
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002636static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002637{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002638 if (vcpu->arch.apic &&
2639 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2640 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002641
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002642 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002643}
2644
Hannes Edercded19f2009-02-21 02:19:13 +01002645static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2646 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002647{
2648 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002649 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002650
2651 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002652 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002653 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02002654 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002655 int ret;
2656
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002657 if (gpa == UNMAPPED_GVA) {
2658 r = X86EMUL_PROPAGATE_FAULT;
2659 goto out;
2660 }
Izik Eidus77c20022008-12-29 01:42:19 +02002661 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002662 if (ret < 0) {
2663 r = X86EMUL_UNHANDLEABLE;
2664 goto out;
2665 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002666
Izik Eidus77c20022008-12-29 01:42:19 +02002667 bytes -= toread;
2668 data += toread;
2669 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002670 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002671out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002672 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002673}
Izik Eidus77c20022008-12-29 01:42:19 +02002674
Hannes Edercded19f2009-02-21 02:19:13 +01002675static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2676 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02002677{
2678 void *data = val;
2679 int r = X86EMUL_CONTINUE;
2680
2681 while (bytes) {
2682 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2683 unsigned offset = addr & (PAGE_SIZE-1);
2684 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2685 int ret;
2686
2687 if (gpa == UNMAPPED_GVA) {
2688 r = X86EMUL_PROPAGATE_FAULT;
2689 goto out;
2690 }
2691 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2692 if (ret < 0) {
2693 r = X86EMUL_UNHANDLEABLE;
2694 goto out;
2695 }
2696
2697 bytes -= towrite;
2698 data += towrite;
2699 addr += towrite;
2700 }
2701out:
2702 return r;
2703}
2704
Carsten Ottebbd9b642007-10-30 18:44:21 +01002705
Carsten Ottebbd9b642007-10-30 18:44:21 +01002706static int emulator_read_emulated(unsigned long addr,
2707 void *val,
2708 unsigned int bytes,
2709 struct kvm_vcpu *vcpu)
2710{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002711 gpa_t gpa;
2712
2713 if (vcpu->mmio_read_completed) {
2714 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03002715 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
2716 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002717 vcpu->mmio_read_completed = 0;
2718 return X86EMUL_CONTINUE;
2719 }
2720
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002721 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002722
2723 /* For APIC access vmexit */
2724 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2725 goto mmio;
2726
Izik Eidus77c20022008-12-29 01:42:19 +02002727 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2728 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002729 return X86EMUL_CONTINUE;
2730 if (gpa == UNMAPPED_GVA)
2731 return X86EMUL_PROPAGATE_FAULT;
2732
2733mmio:
2734 /*
2735 * Is this MMIO handled locally?
2736 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03002737 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
2738 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002739 return X86EMUL_CONTINUE;
2740 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03002741
2742 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002743
2744 vcpu->mmio_needed = 1;
2745 vcpu->mmio_phys_addr = gpa;
2746 vcpu->mmio_size = bytes;
2747 vcpu->mmio_is_write = 0;
2748
2749 return X86EMUL_UNHANDLEABLE;
2750}
2751
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002752int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002753 const void *val, int bytes)
2754{
2755 int ret;
2756
2757 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2758 if (ret < 0)
2759 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002760 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002761 return 1;
2762}
2763
Carsten Ottebbd9b642007-10-30 18:44:21 +01002764static int emulator_write_emulated_onepage(unsigned long addr,
2765 const void *val,
2766 unsigned int bytes,
2767 struct kvm_vcpu *vcpu)
2768{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002769 gpa_t gpa;
2770
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002771 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002772
2773 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002774 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002775 return X86EMUL_PROPAGATE_FAULT;
2776 }
2777
2778 /* For APIC access vmexit */
2779 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2780 goto mmio;
2781
2782 if (emulator_write_phys(vcpu, gpa, val, bytes))
2783 return X86EMUL_CONTINUE;
2784
2785mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03002786 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002787 /*
2788 * Is this MMIO handled locally?
2789 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002790 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002791 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002792
2793 vcpu->mmio_needed = 1;
2794 vcpu->mmio_phys_addr = gpa;
2795 vcpu->mmio_size = bytes;
2796 vcpu->mmio_is_write = 1;
2797 memcpy(vcpu->mmio_data, val, bytes);
2798
2799 return X86EMUL_CONTINUE;
2800}
2801
2802int emulator_write_emulated(unsigned long addr,
2803 const void *val,
2804 unsigned int bytes,
2805 struct kvm_vcpu *vcpu)
2806{
2807 /* Crossing a page boundary? */
2808 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2809 int rc, now;
2810
2811 now = -addr & ~PAGE_MASK;
2812 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2813 if (rc != X86EMUL_CONTINUE)
2814 return rc;
2815 addr += now;
2816 val += now;
2817 bytes -= now;
2818 }
2819 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2820}
2821EXPORT_SYMBOL_GPL(emulator_write_emulated);
2822
2823static int emulator_cmpxchg_emulated(unsigned long addr,
2824 const void *old,
2825 const void *new,
2826 unsigned int bytes,
2827 struct kvm_vcpu *vcpu)
2828{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02002829 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002830#ifndef CONFIG_X86_64
2831 /* guests cmpxchg8b have to be emulated atomically */
2832 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002833 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002834 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002835 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002836 u64 val;
2837
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002838 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2839
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002840 if (gpa == UNMAPPED_GVA ||
2841 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2842 goto emul_write;
2843
2844 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2845 goto emul_write;
2846
2847 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002848
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002849 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002850
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002851 kaddr = kmap_atomic(page, KM_USER0);
2852 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2853 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002854 kvm_release_page_dirty(page);
2855 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002856emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002857#endif
2858
Carsten Ottebbd9b642007-10-30 18:44:21 +01002859 return emulator_write_emulated(addr, new, bytes, vcpu);
2860}
2861
2862static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2863{
2864 return kvm_x86_ops->get_segment_base(vcpu, seg);
2865}
2866
2867int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2868{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002869 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002870 return X86EMUL_CONTINUE;
2871}
2872
2873int emulate_clts(struct kvm_vcpu *vcpu)
2874{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002875 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002876 return X86EMUL_CONTINUE;
2877}
2878
2879int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2880{
2881 struct kvm_vcpu *vcpu = ctxt->vcpu;
2882
2883 switch (dr) {
2884 case 0 ... 3:
2885 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2886 return X86EMUL_CONTINUE;
2887 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002888 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002889 return X86EMUL_UNHANDLEABLE;
2890 }
2891}
2892
2893int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2894{
2895 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2896 int exception;
2897
2898 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2899 if (exception) {
2900 /* FIXME: better handling */
2901 return X86EMUL_UNHANDLEABLE;
2902 }
2903 return X86EMUL_CONTINUE;
2904}
2905
2906void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2907{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002908 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002909 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002910 unsigned long rip_linear;
2911
Avi Kivityf76c7102008-06-13 22:45:42 +03002912 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002913 return;
2914
Glauber Costa25be4602008-06-10 10:46:53 -03002915 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2916
Izik Eidus77c20022008-12-29 01:42:19 +02002917 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002918
2919 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2920 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002921}
2922EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2923
Harvey Harrison14af3f32008-02-19 10:25:50 -08002924static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02002925 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002926 .read_emulated = emulator_read_emulated,
2927 .write_emulated = emulator_write_emulated,
2928 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2929};
2930
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002931static void cache_all_regs(struct kvm_vcpu *vcpu)
2932{
2933 kvm_register_read(vcpu, VCPU_REGS_RAX);
2934 kvm_register_read(vcpu, VCPU_REGS_RSP);
2935 kvm_register_read(vcpu, VCPU_REGS_RIP);
2936 vcpu->arch.regs_dirty = ~0;
2937}
2938
Carsten Ottebbd9b642007-10-30 18:44:21 +01002939int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002940 unsigned long cr2,
2941 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002942 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002943{
Glauber Costa310b5d32009-05-12 16:21:06 -04002944 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08002945 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03002946 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002947
Avi Kivity26eef702008-07-03 14:59:22 +03002948 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002949 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002950 /*
Avi Kivity56e82312009-08-12 15:04:37 +03002951 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002952 * instead of direct ->regs accesses, can save hundred cycles
2953 * on Intel for instructions that don't read/change RSP, for
2954 * for example.
2955 */
2956 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002957
2958 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002959 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002960
Sheng Yang571008d2008-01-02 14:49:22 +08002961 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002962 int cs_db, cs_l;
2963 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2964
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002965 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka91586a32009-10-05 13:07:21 +02002966 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002967 vcpu->arch.emulate_ctxt.mode =
2968 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002969 ? X86EMUL_MODE_REAL : cs_l
2970 ? X86EMUL_MODE_PROT64 : cs_db
2971 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2972
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002973 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002974
Andre Przywara0cb57622009-06-17 15:50:31 +02002975 /* Only allow emulation of specific instructions on #UD
2976 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08002977 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02002978 if (emulation_type & EMULTYPE_TRAP_UD) {
2979 if (!c->twobyte)
2980 return EMULATE_FAIL;
2981 switch (c->b) {
2982 case 0x01: /* VMMCALL */
2983 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2984 return EMULATE_FAIL;
2985 break;
2986 case 0x34: /* sysenter */
2987 case 0x35: /* sysexit */
2988 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2989 return EMULATE_FAIL;
2990 break;
2991 case 0x05: /* syscall */
2992 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2993 return EMULATE_FAIL;
2994 break;
2995 default:
2996 return EMULATE_FAIL;
2997 }
2998
2999 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3000 return EMULATE_FAIL;
3001 }
Sheng Yang571008d2008-01-02 14:49:22 +08003002
Avi Kivityf2b57562007-11-18 15:17:51 +02003003 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003004 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003005 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003006 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3007 return EMULATE_DONE;
3008 return EMULATE_FAIL;
3009 }
3010 }
3011
Gleb Natapovba8afb62009-04-12 13:36:57 +03003012 if (emulation_type & EMULTYPE_SKIP) {
3013 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3014 return EMULATE_DONE;
3015 }
3016
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003017 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003018 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3019
3020 if (r == 0)
3021 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003022
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003023 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003024 return EMULATE_DO_MMIO;
3025
3026 if ((r || vcpu->mmio_is_write) && run) {
3027 run->exit_reason = KVM_EXIT_MMIO;
3028 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3029 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3030 run->mmio.len = vcpu->mmio_size;
3031 run->mmio.is_write = vcpu->mmio_is_write;
3032 }
3033
3034 if (r) {
3035 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3036 return EMULATE_DONE;
3037 if (!vcpu->mmio_needed) {
3038 kvm_report_emulation_failure(vcpu, "mmio");
3039 return EMULATE_FAIL;
3040 }
3041 return EMULATE_DO_MMIO;
3042 }
3043
Jan Kiszka91586a32009-10-05 13:07:21 +02003044 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003045
3046 if (vcpu->mmio_is_write) {
3047 vcpu->mmio_needed = 0;
3048 return EMULATE_DO_MMIO;
3049 }
3050
3051 return EMULATE_DONE;
3052}
3053EXPORT_SYMBOL_GPL(emulate_instruction);
3054
Carsten Ottede7d7892007-10-30 18:44:25 +01003055static int pio_copy_data(struct kvm_vcpu *vcpu)
3056{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003057 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003058 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003059 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003060 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003061
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003062 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3063 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02003064 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01003065 else
Izik Eidus0f346072008-12-29 01:42:20 +02003066 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
3067 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003068}
3069
3070int complete_pio(struct kvm_vcpu *vcpu)
3071{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003072 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003073 long delta;
3074 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003075 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003076
3077 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003078 if (io->in) {
3079 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3080 memcpy(&val, vcpu->arch.pio_data, io->size);
3081 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3082 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003083 } else {
3084 if (io->in) {
3085 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003086 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01003087 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003088 }
3089
3090 delta = 1;
3091 if (io->rep) {
3092 delta *= io->cur_count;
3093 /*
3094 * The size of the register should really depend on
3095 * current address size.
3096 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003097 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3098 val -= delta;
3099 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003100 }
3101 if (io->down)
3102 delta = -delta;
3103 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003104 if (io->in) {
3105 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3106 val += delta;
3107 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3108 } else {
3109 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3110 val += delta;
3111 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3112 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003113 }
3114
Carsten Ottede7d7892007-10-30 18:44:25 +01003115 io->count -= io->cur_count;
3116 io->cur_count = 0;
3117
3118 return 0;
3119}
3120
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003121static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003122{
3123 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003124 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003125
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003126 if (vcpu->arch.pio.in)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003127 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3128 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003129 else
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003130 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3131 vcpu->arch.pio.size, pd);
3132 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003133}
3134
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003135static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003136{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003137 struct kvm_pio_request *io = &vcpu->arch.pio;
3138 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003139 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003140
Carsten Ottede7d7892007-10-30 18:44:25 +01003141 for (i = 0; i < io->cur_count; i++) {
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003142 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
3143 io->port, io->size, pd)) {
3144 r = -EOPNOTSUPP;
3145 break;
3146 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003147 pd += io->size;
3148 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003149 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003150}
3151
Avi Kivity851ba692009-08-24 11:10:17 +03003152int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003153{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003154 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003155
3156 vcpu->run->exit_reason = KVM_EXIT_IO;
3157 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003158 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003159 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003160 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3161 vcpu->run->io.port = vcpu->arch.pio.port = port;
3162 vcpu->arch.pio.in = in;
3163 vcpu->arch.pio.string = 0;
3164 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003165 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003166
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003167 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3168 size, 1);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003169
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003170 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3171 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01003172
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003173 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003174 complete_pio(vcpu);
3175 return 1;
3176 }
3177 return 0;
3178}
3179EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3180
Avi Kivity851ba692009-08-24 11:10:17 +03003181int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003182 int size, unsigned long count, int down,
3183 gva_t address, int rep, unsigned port)
3184{
3185 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003186 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003187
3188 vcpu->run->exit_reason = KVM_EXIT_IO;
3189 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003190 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003191 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003192 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3193 vcpu->run->io.port = vcpu->arch.pio.port = port;
3194 vcpu->arch.pio.in = in;
3195 vcpu->arch.pio.string = 1;
3196 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003197 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003198
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003199 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3200 size, count);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003201
Carsten Ottede7d7892007-10-30 18:44:25 +01003202 if (!count) {
3203 kvm_x86_ops->skip_emulated_instruction(vcpu);
3204 return 1;
3205 }
3206
3207 if (!down)
3208 in_page = PAGE_SIZE - offset_in_page(address);
3209 else
3210 in_page = offset_in_page(address) + size;
3211 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003212 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003213 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003214 if (down) {
3215 /*
3216 * String I/O in reverse. Yuck. Kill the guest, fix later.
3217 */
3218 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003219 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003220 return 1;
3221 }
3222 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003223 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003224
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003225 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003226 kvm_x86_ops->skip_emulated_instruction(vcpu);
3227
Izik Eidus0f346072008-12-29 01:42:20 +02003228 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003229
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003230 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003231 /* string PIO write */
3232 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02003233 if (ret == X86EMUL_PROPAGATE_FAULT) {
3234 kvm_inject_gp(vcpu, 0);
3235 return 1;
3236 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003237 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003238 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003239 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003240 ret = 1;
3241 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003242 }
3243 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003244
3245 return ret;
3246}
3247EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3248
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003249static void bounce_off(void *info)
3250{
3251 /* nothing */
3252}
3253
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003254static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3255 void *data)
3256{
3257 struct cpufreq_freqs *freq = data;
3258 struct kvm *kvm;
3259 struct kvm_vcpu *vcpu;
3260 int i, send_ipi = 0;
3261
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003262 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3263 return 0;
3264 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3265 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003266 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003267
3268 spin_lock(&kvm_lock);
3269 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003270 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003271 if (vcpu->cpu != freq->cpu)
3272 continue;
3273 if (!kvm_request_guest_time_update(vcpu))
3274 continue;
3275 if (vcpu->cpu != smp_processor_id())
3276 send_ipi++;
3277 }
3278 }
3279 spin_unlock(&kvm_lock);
3280
3281 if (freq->old < freq->new && send_ipi) {
3282 /*
3283 * We upscale the frequency. Must make the guest
3284 * doesn't see old kvmclock values while running with
3285 * the new frequency, otherwise we risk the guest sees
3286 * time go backwards.
3287 *
3288 * In case we update the frequency for another cpu
3289 * (which might be in guest context) send an interrupt
3290 * to kick the cpu out of guest context. Next time
3291 * guest context is entered kvmclock will be updated,
3292 * so the guest will not see stale values.
3293 */
3294 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3295 }
3296 return 0;
3297}
3298
3299static struct notifier_block kvmclock_cpufreq_notifier_block = {
3300 .notifier_call = kvmclock_cpufreq_notifier
3301};
3302
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003303static void kvm_timer_init(void)
3304{
3305 int cpu;
3306
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003307 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003308 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3309 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003310 for_each_online_cpu(cpu) {
3311 unsigned long khz = cpufreq_get(cpu);
3312 if (!khz)
3313 khz = tsc_khz;
3314 per_cpu(cpu_tsc_khz, cpu) = khz;
3315 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003316 } else {
3317 for_each_possible_cpu(cpu)
3318 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003319 }
3320}
3321
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003322int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003323{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003324 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003325 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3326
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003327 if (kvm_x86_ops) {
3328 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003329 r = -EEXIST;
3330 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003331 }
3332
3333 if (!ops->cpu_has_kvm_support()) {
3334 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003335 r = -EOPNOTSUPP;
3336 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003337 }
3338 if (ops->disabled_by_bios()) {
3339 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003340 r = -EOPNOTSUPP;
3341 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003342 }
3343
Avi Kivity97db56c2008-01-13 13:23:56 +02003344 r = kvm_mmu_module_init();
3345 if (r)
3346 goto out;
3347
3348 kvm_init_msr_list();
3349
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003350 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003351 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003352 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3353 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003354 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003355
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003356 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003357
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003358 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003359
3360out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003361 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003362}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003363
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003364void kvm_arch_exit(void)
3365{
Jan Kiszka888d2562009-04-17 19:24:58 +02003366 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3367 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3368 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003369 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003370 kvm_mmu_module_exit();
3371}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003372
Hollis Blanchard8776e512007-10-31 17:24:24 -05003373int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3374{
3375 ++vcpu->stat.halt_exits;
3376 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003377 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003378 return 1;
3379 } else {
3380 vcpu->run->exit_reason = KVM_EXIT_HLT;
3381 return 0;
3382 }
3383}
3384EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3385
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003386static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3387 unsigned long a1)
3388{
3389 if (is_long_mode(vcpu))
3390 return a0;
3391 else
3392 return a0 | ((gpa_t)a1 << 32);
3393}
3394
Hollis Blanchard8776e512007-10-31 17:24:24 -05003395int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3396{
3397 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003398 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003399
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003400 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3401 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3402 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3403 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3404 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003405
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003406 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003407
Hollis Blanchard8776e512007-10-31 17:24:24 -05003408 if (!is_long_mode(vcpu)) {
3409 nr &= 0xFFFFFFFF;
3410 a0 &= 0xFFFFFFFF;
3411 a1 &= 0xFFFFFFFF;
3412 a2 &= 0xFFFFFFFF;
3413 a3 &= 0xFFFFFFFF;
3414 }
3415
Jan Kiszka07708c42009-08-03 18:43:28 +02003416 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3417 ret = -KVM_EPERM;
3418 goto out;
3419 }
3420
Hollis Blanchard8776e512007-10-31 17:24:24 -05003421 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003422 case KVM_HC_VAPIC_POLL_IRQ:
3423 ret = 0;
3424 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003425 case KVM_HC_MMU_OP:
3426 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3427 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003428 default:
3429 ret = -KVM_ENOSYS;
3430 break;
3431 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003432out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003433 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303434 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003435 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003436}
3437EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3438
3439int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3440{
3441 char instruction[3];
3442 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003443 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003444
Hollis Blanchard8776e512007-10-31 17:24:24 -05003445
3446 /*
3447 * Blow out the MMU to ensure that no other VCPU has an active mapping
3448 * to ensure that the updated hypercall appears atomically across all
3449 * VCPUs.
3450 */
3451 kvm_mmu_zap_all(vcpu->kvm);
3452
Hollis Blanchard8776e512007-10-31 17:24:24 -05003453 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003454 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003455 != X86EMUL_CONTINUE)
3456 ret = -EFAULT;
3457
Hollis Blanchard8776e512007-10-31 17:24:24 -05003458 return ret;
3459}
3460
3461static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3462{
3463 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3464}
3465
3466void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3467{
3468 struct descriptor_table dt = { limit, base };
3469
3470 kvm_x86_ops->set_gdt(vcpu, &dt);
3471}
3472
3473void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3474{
3475 struct descriptor_table dt = { limit, base };
3476
3477 kvm_x86_ops->set_idt(vcpu, &dt);
3478}
3479
3480void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3481 unsigned long *rflags)
3482{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003483 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02003484 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003485}
3486
3487unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3488{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003489 unsigned long value;
3490
Hollis Blanchard8776e512007-10-31 17:24:24 -05003491 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3492 switch (cr) {
3493 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003494 value = vcpu->arch.cr0;
3495 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003496 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003497 value = vcpu->arch.cr2;
3498 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003499 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003500 value = vcpu->arch.cr3;
3501 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003502 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003503 value = vcpu->arch.cr4;
3504 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003505 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003506 value = kvm_get_cr8(vcpu);
3507 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003508 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003509 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003510 return 0;
3511 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003512
3513 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003514}
3515
3516void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3517 unsigned long *rflags)
3518{
3519 switch (cr) {
3520 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003521 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Jan Kiszka91586a32009-10-05 13:07:21 +02003522 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003523 break;
3524 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003525 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003526 break;
3527 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003528 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003529 break;
3530 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003531 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003532 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003533 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003534 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003535 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003536 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003537 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003538 }
3539}
3540
Dan Kenigsberg07716712007-11-21 17:10:04 +02003541static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3542{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003543 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3544 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02003545
3546 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3547 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08003548 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003549 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003550 if (ej->function == e->function) {
3551 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3552 return j;
3553 }
3554 }
3555 return 0; /* silence gcc, even though control never reaches here */
3556}
3557
3558/* find an entry with matching function, matching index (if needed), and that
3559 * should be read next (if it's stateful) */
3560static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3561 u32 function, u32 index)
3562{
3563 if (e->function != function)
3564 return 0;
3565 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3566 return 0;
3567 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00003568 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02003569 return 0;
3570 return 1;
3571}
3572
Alexander Grafd8017472008-11-25 20:17:11 +01003573struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3574 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003575{
3576 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01003577 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003578
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003579 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01003580 struct kvm_cpuid_entry2 *e;
3581
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003582 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003583 if (is_matching_cpuid_entry(e, function, index)) {
3584 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3585 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003586 best = e;
3587 break;
3588 }
3589 /*
3590 * Both basic or both extended?
3591 */
3592 if (((e->function ^ function) & 0x80000000) == 0)
3593 if (!best || e->function > best->function)
3594 best = e;
3595 }
Alexander Grafd8017472008-11-25 20:17:11 +01003596 return best;
3597}
3598
Dong, Eddie82725b22009-03-30 16:21:08 +08003599int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3600{
3601 struct kvm_cpuid_entry2 *best;
3602
3603 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3604 if (best)
3605 return best->eax & 0xff;
3606 return 36;
3607}
3608
Alexander Grafd8017472008-11-25 20:17:11 +01003609void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3610{
3611 u32 function, index;
3612 struct kvm_cpuid_entry2 *best;
3613
3614 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3615 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3616 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3617 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3618 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3619 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3620 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003621 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003622 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3623 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3624 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3625 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003626 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003627 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003628 trace_kvm_cpuid(function,
3629 kvm_register_read(vcpu, VCPU_REGS_RAX),
3630 kvm_register_read(vcpu, VCPU_REGS_RBX),
3631 kvm_register_read(vcpu, VCPU_REGS_RCX),
3632 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003633}
3634EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003635
3636/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003637 * Check if userspace requested an interrupt window, and that the
3638 * interrupt window is open.
3639 *
3640 * No need to exit to userspace if we already have an interrupt queued.
3641 */
Avi Kivity851ba692009-08-24 11:10:17 +03003642static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003643{
Gleb Natapov80618232009-04-21 17:44:56 +03003644 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003645 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03003646 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003647}
3648
Avi Kivity851ba692009-08-24 11:10:17 +03003649static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003650{
Avi Kivity851ba692009-08-24 11:10:17 +03003651 struct kvm_run *kvm_run = vcpu->run;
3652
Jan Kiszka91586a32009-10-05 13:07:21 +02003653 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003654 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003655 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01003656 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003657 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01003658 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003659 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03003660 kvm_arch_interrupt_allowed(vcpu) &&
3661 !kvm_cpu_has_interrupt(vcpu) &&
3662 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003663}
3664
Avi Kivityb93463a2007-10-25 16:52:32 +02003665static void vapic_enter(struct kvm_vcpu *vcpu)
3666{
3667 struct kvm_lapic *apic = vcpu->arch.apic;
3668 struct page *page;
3669
3670 if (!apic || !apic->vapic_addr)
3671 return;
3672
3673 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003674
3675 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003676}
3677
3678static void vapic_exit(struct kvm_vcpu *vcpu)
3679{
3680 struct kvm_lapic *apic = vcpu->arch.apic;
3681
3682 if (!apic || !apic->vapic_addr)
3683 return;
3684
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003685 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003686 kvm_release_page_dirty(apic->vapic_page);
3687 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003688 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003689}
3690
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003691static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3692{
3693 int max_irr, tpr;
3694
3695 if (!kvm_x86_ops->update_cr8_intercept)
3696 return;
3697
Avi Kivity88c808f2009-08-17 22:49:40 +03003698 if (!vcpu->arch.apic)
3699 return;
3700
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003701 if (!vcpu->arch.apic->vapic_addr)
3702 max_irr = kvm_lapic_find_highest_irr(vcpu);
3703 else
3704 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003705
3706 if (max_irr != -1)
3707 max_irr >>= 4;
3708
3709 tpr = kvm_lapic_get_cr8(vcpu);
3710
3711 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3712}
3713
Avi Kivity851ba692009-08-24 11:10:17 +03003714static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003715{
3716 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03003717 if (vcpu->arch.exception.pending) {
3718 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
3719 vcpu->arch.exception.has_error_code,
3720 vcpu->arch.exception.error_code);
3721 return;
3722 }
3723
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003724 if (vcpu->arch.nmi_injected) {
3725 kvm_x86_ops->set_nmi(vcpu);
3726 return;
3727 }
3728
3729 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003730 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003731 return;
3732 }
3733
3734 /* try to inject new event if pending */
3735 if (vcpu->arch.nmi_pending) {
3736 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3737 vcpu->arch.nmi_pending = false;
3738 vcpu->arch.nmi_injected = true;
3739 kvm_x86_ops->set_nmi(vcpu);
3740 }
3741 } else if (kvm_cpu_has_interrupt(vcpu)) {
3742 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003743 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3744 false);
3745 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003746 }
3747 }
3748}
3749
Avi Kivity851ba692009-08-24 11:10:17 +03003750static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003751{
3752 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003753 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003754 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003755
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003756 if (vcpu->requests)
3757 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3758 kvm_mmu_unload(vcpu);
3759
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003760 r = kvm_mmu_reload(vcpu);
3761 if (unlikely(r))
3762 goto out;
3763
Avi Kivity2f52d582008-01-16 12:49:30 +02003764 if (vcpu->requests) {
3765 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003766 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003767 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3768 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03003769 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3770 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003771 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3772 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003773 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3774 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003775 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02003776 r = 0;
3777 goto out;
3778 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003779 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003780 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003781 r = 0;
3782 goto out;
3783 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003784 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003785
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003786 preempt_disable();
3787
3788 kvm_x86_ops->prepare_guest_switch(vcpu);
3789 kvm_load_guest_fpu(vcpu);
3790
3791 local_irq_disable();
3792
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003793 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3794 smp_mb__after_clear_bit();
3795
Marcelo Tosattid7690172008-09-08 15:23:48 -03003796 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03003797 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c1428012008-01-15 18:27:32 +02003798 local_irq_enable();
3799 preempt_enable();
3800 r = 1;
3801 goto out;
3802 }
3803
Avi Kivity851ba692009-08-24 11:10:17 +03003804 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003805
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003806 /* enable NMI/IRQ window open exits if needed */
3807 if (vcpu->arch.nmi_pending)
3808 kvm_x86_ops->enable_nmi_window(vcpu);
3809 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3810 kvm_x86_ops->enable_irq_window(vcpu);
3811
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003812 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003813 update_cr8_intercept(vcpu);
3814 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003815 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003816
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003817 up_read(&vcpu->kvm->slots_lock);
3818
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003819 kvm_guest_enter();
3820
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003821 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003822 set_debugreg(0, 7);
3823 set_debugreg(vcpu->arch.eff_db[0], 0);
3824 set_debugreg(vcpu->arch.eff_db[1], 1);
3825 set_debugreg(vcpu->arch.eff_db[2], 2);
3826 set_debugreg(vcpu->arch.eff_db[3], 3);
3827 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003828
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003829 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03003830 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003831
Avi Kivity3d53c272009-09-01 12:34:07 +03003832 if (unlikely(vcpu->arch.switch_db_regs || test_thread_flag(TIF_DEBUG))) {
3833 set_debugreg(current->thread.debugreg0, 0);
3834 set_debugreg(current->thread.debugreg1, 1);
3835 set_debugreg(current->thread.debugreg2, 2);
3836 set_debugreg(current->thread.debugreg3, 3);
3837 set_debugreg(current->thread.debugreg6, 6);
3838 set_debugreg(current->thread.debugreg7, 7);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003839 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003840
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003841 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003842 local_irq_enable();
3843
3844 ++vcpu->stat.exits;
3845
3846 /*
3847 * We must have an instruction between local_irq_enable() and
3848 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3849 * the interrupt shadow. The stat.exits increment will do nicely.
3850 * But we need to prevent reordering, hence this barrier():
3851 */
3852 barrier();
3853
3854 kvm_guest_exit();
3855
3856 preempt_enable();
3857
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003858 down_read(&vcpu->kvm->slots_lock);
3859
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003860 /*
3861 * Profile KVM exit RIPs:
3862 */
3863 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003864 unsigned long rip = kvm_rip_read(vcpu);
3865 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003866 }
3867
Avi Kivity298101d2007-11-25 13:41:11 +02003868
Avi Kivityb93463a2007-10-25 16:52:32 +02003869 kvm_lapic_sync_from_vapic(vcpu);
3870
Avi Kivity851ba692009-08-24 11:10:17 +03003871 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003872out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003873 return r;
3874}
3875
Gleb Natapov09cec752009-03-23 15:11:44 +02003876
Avi Kivity851ba692009-08-24 11:10:17 +03003877static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003878{
3879 int r;
3880
3881 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003882 pr_debug("vcpu %d received sipi with vector # %x\n",
3883 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003884 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003885 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003886 if (r)
3887 return r;
3888 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003889 }
3890
Marcelo Tosattid7690172008-09-08 15:23:48 -03003891 down_read(&vcpu->kvm->slots_lock);
3892 vapic_enter(vcpu);
3893
3894 r = 1;
3895 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003896 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03003897 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003898 else {
3899 up_read(&vcpu->kvm->slots_lock);
3900 kvm_vcpu_block(vcpu);
3901 down_read(&vcpu->kvm->slots_lock);
3902 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02003903 {
3904 switch(vcpu->arch.mp_state) {
3905 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003906 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02003907 KVM_MP_STATE_RUNNABLE;
3908 case KVM_MP_STATE_RUNNABLE:
3909 break;
3910 case KVM_MP_STATE_SIPI_RECEIVED:
3911 default:
3912 r = -EINTR;
3913 break;
3914 }
3915 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03003916 }
3917
Gleb Natapov09cec752009-03-23 15:11:44 +02003918 if (r <= 0)
3919 break;
3920
3921 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
3922 if (kvm_cpu_has_pending_timer(vcpu))
3923 kvm_inject_pending_timer_irqs(vcpu);
3924
Avi Kivity851ba692009-08-24 11:10:17 +03003925 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02003926 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03003927 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02003928 ++vcpu->stat.request_irq_exits;
3929 }
3930 if (signal_pending(current)) {
3931 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03003932 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02003933 ++vcpu->stat.signal_exits;
3934 }
3935 if (need_resched()) {
3936 up_read(&vcpu->kvm->slots_lock);
3937 kvm_resched(vcpu);
3938 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003939 }
3940 }
3941
3942 up_read(&vcpu->kvm->slots_lock);
Avi Kivity851ba692009-08-24 11:10:17 +03003943 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003944
Avi Kivityb93463a2007-10-25 16:52:32 +02003945 vapic_exit(vcpu);
3946
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003947 return r;
3948}
3949
3950int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3951{
3952 int r;
3953 sigset_t sigsaved;
3954
3955 vcpu_load(vcpu);
3956
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003957 if (vcpu->sigset_active)
3958 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3959
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003960 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3961 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003962 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003963 r = -EAGAIN;
3964 goto out;
3965 }
3966
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003967 /* re-sync apic's tpr */
3968 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003969 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003970
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003971 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003972 r = complete_pio(vcpu);
3973 if (r)
3974 goto out;
3975 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003976 if (vcpu->mmio_needed) {
3977 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3978 vcpu->mmio_read_completed = 1;
3979 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003980
3981 down_read(&vcpu->kvm->slots_lock);
Avi Kivity851ba692009-08-24 11:10:17 +03003982 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08003983 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003984 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003985 if (r == EMULATE_DO_MMIO) {
3986 /*
3987 * Read-modify-write. Back to userspace.
3988 */
3989 r = 0;
3990 goto out;
3991 }
3992 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003993 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3994 kvm_register_write(vcpu, VCPU_REGS_RAX,
3995 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003996
Avi Kivity851ba692009-08-24 11:10:17 +03003997 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003998
3999out:
4000 if (vcpu->sigset_active)
4001 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4002
4003 vcpu_put(vcpu);
4004 return r;
4005}
4006
4007int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4008{
4009 vcpu_load(vcpu);
4010
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004011 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4012 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4013 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4014 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4015 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4016 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4017 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4018 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004019#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004020 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4021 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4022 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4023 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4024 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4025 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4026 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4027 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004028#endif
4029
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004030 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004031 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004032
4033 vcpu_put(vcpu);
4034
4035 return 0;
4036}
4037
4038int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4039{
4040 vcpu_load(vcpu);
4041
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004042 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4043 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4044 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4045 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4046 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4047 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4048 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4049 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004050#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004051 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4052 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4053 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4054 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4055 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4056 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4057 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4058 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004059#endif
4060
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004061 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004062 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004063
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004064 vcpu->arch.exception.pending = false;
4065
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004066 vcpu_put(vcpu);
4067
4068 return 0;
4069}
4070
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004071void kvm_get_segment(struct kvm_vcpu *vcpu,
4072 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004073{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004074 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004075}
4076
4077void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4078{
4079 struct kvm_segment cs;
4080
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004081 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004082 *db = cs.db;
4083 *l = cs.l;
4084}
4085EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4086
4087int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4088 struct kvm_sregs *sregs)
4089{
4090 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004091
4092 vcpu_load(vcpu);
4093
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004094 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4095 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4096 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4097 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4098 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4099 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004100
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004101 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4102 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004103
4104 kvm_x86_ops->get_idt(vcpu, &dt);
4105 sregs->idt.limit = dt.limit;
4106 sregs->idt.base = dt.base;
4107 kvm_x86_ops->get_gdt(vcpu, &dt);
4108 sregs->gdt.limit = dt.limit;
4109 sregs->gdt.base = dt.base;
4110
4111 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004112 sregs->cr0 = vcpu->arch.cr0;
4113 sregs->cr2 = vcpu->arch.cr2;
4114 sregs->cr3 = vcpu->arch.cr3;
4115 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004116 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004117 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004118 sregs->apic_base = kvm_get_apic_base(vcpu);
4119
Gleb Natapov923c61b2009-05-11 13:35:48 +03004120 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004121
Gleb Natapov36752c92009-05-11 13:35:53 +03004122 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004123 set_bit(vcpu->arch.interrupt.nr,
4124 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004125
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004126 vcpu_put(vcpu);
4127
4128 return 0;
4129}
4130
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004131int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4132 struct kvm_mp_state *mp_state)
4133{
4134 vcpu_load(vcpu);
4135 mp_state->mp_state = vcpu->arch.mp_state;
4136 vcpu_put(vcpu);
4137 return 0;
4138}
4139
4140int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4141 struct kvm_mp_state *mp_state)
4142{
4143 vcpu_load(vcpu);
4144 vcpu->arch.mp_state = mp_state->mp_state;
4145 vcpu_put(vcpu);
4146 return 0;
4147}
4148
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004149static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004150 struct kvm_segment *var, int seg)
4151{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004152 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004153}
4154
Izik Eidus37817f22008-03-24 23:14:53 +02004155static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4156 struct kvm_segment *kvm_desct)
4157{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004158 kvm_desct->base = get_desc_base(seg_desc);
4159 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004160 if (seg_desc->g) {
4161 kvm_desct->limit <<= 12;
4162 kvm_desct->limit |= 0xfff;
4163 }
Izik Eidus37817f22008-03-24 23:14:53 +02004164 kvm_desct->selector = selector;
4165 kvm_desct->type = seg_desc->type;
4166 kvm_desct->present = seg_desc->p;
4167 kvm_desct->dpl = seg_desc->dpl;
4168 kvm_desct->db = seg_desc->d;
4169 kvm_desct->s = seg_desc->s;
4170 kvm_desct->l = seg_desc->l;
4171 kvm_desct->g = seg_desc->g;
4172 kvm_desct->avl = seg_desc->avl;
4173 if (!selector)
4174 kvm_desct->unusable = 1;
4175 else
4176 kvm_desct->unusable = 0;
4177 kvm_desct->padding = 0;
4178}
4179
Amit Shahb8222ad2008-10-22 16:39:47 +05304180static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4181 u16 selector,
4182 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004183{
4184 if (selector & 1 << 2) {
4185 struct kvm_segment kvm_seg;
4186
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004187 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004188
4189 if (kvm_seg.unusable)
4190 dtable->limit = 0;
4191 else
4192 dtable->limit = kvm_seg.limit;
4193 dtable->base = kvm_seg.base;
4194 }
4195 else
4196 kvm_x86_ops->get_gdt(vcpu, dtable);
4197}
4198
4199/* allowed just for 8 bytes segments */
4200static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4201 struct desc_struct *seg_desc)
4202{
4203 struct descriptor_table dtable;
4204 u16 index = selector >> 3;
4205
Amit Shahb8222ad2008-10-22 16:39:47 +05304206 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004207
4208 if (dtable.limit < index * 8 + 7) {
4209 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4210 return 1;
4211 }
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004212 return kvm_read_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004213}
4214
4215/* allowed just for 8 bytes segments */
4216static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4217 struct desc_struct *seg_desc)
4218{
4219 struct descriptor_table dtable;
4220 u16 index = selector >> 3;
4221
Amit Shahb8222ad2008-10-22 16:39:47 +05304222 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004223
4224 if (dtable.limit < index * 8 + 7)
4225 return 1;
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004226 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004227}
4228
Gleb Natapovabb39112009-10-25 17:42:02 +02004229static gpa_t get_tss_base_addr(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004230 struct desc_struct *seg_desc)
4231{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004232 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004233
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004234 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02004235}
4236
Izik Eidus37817f22008-03-24 23:14:53 +02004237static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4238{
4239 struct kvm_segment kvm_seg;
4240
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004241 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004242 return kvm_seg.selector;
4243}
4244
4245static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4246 u16 selector,
4247 struct kvm_segment *kvm_seg)
4248{
4249 struct desc_struct seg_desc;
4250
4251 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4252 return 1;
4253 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4254 return 0;
4255}
4256
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004257static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004258{
4259 struct kvm_segment segvar = {
4260 .base = selector << 4,
4261 .limit = 0xffff,
4262 .selector = selector,
4263 .type = 3,
4264 .present = 1,
4265 .dpl = 3,
4266 .db = 0,
4267 .s = 1,
4268 .l = 0,
4269 .g = 0,
4270 .avl = 0,
4271 .unusable = 0,
4272 };
4273 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4274 return 0;
4275}
4276
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004277static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4278{
4279 return (seg != VCPU_SREG_LDTR) &&
4280 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004281 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004282}
4283
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004284int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4285 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004286{
4287 struct kvm_segment kvm_seg;
4288
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004289 if (is_vm86_segment(vcpu, seg) || !(vcpu->arch.cr0 & X86_CR0_PE))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004290 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004291 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4292 return 1;
4293 kvm_seg.type |= type_bits;
4294
4295 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4296 seg != VCPU_SREG_LDTR)
4297 if (!kvm_seg.s)
4298 kvm_seg.unusable = 1;
4299
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004300 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004301 return 0;
4302}
4303
4304static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4305 struct tss_segment_32 *tss)
4306{
4307 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004308 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004309 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004310 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4311 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4312 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4313 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4314 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4315 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4316 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4317 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004318 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4319 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4320 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4321 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4322 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4323 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4324 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004325}
4326
4327static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4328 struct tss_segment_32 *tss)
4329{
4330 kvm_set_cr3(vcpu, tss->cr3);
4331
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004332 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004333 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004334
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004335 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4336 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4337 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4338 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4339 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4340 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4341 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4342 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004343
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004344 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004345 return 1;
4346
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004347 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004348 return 1;
4349
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004350 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004351 return 1;
4352
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004353 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004354 return 1;
4355
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004356 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004357 return 1;
4358
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004359 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004360 return 1;
4361
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004362 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004363 return 1;
4364 return 0;
4365}
4366
4367static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4368 struct tss_segment_16 *tss)
4369{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004370 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004371 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004372 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4373 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4374 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4375 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4376 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4377 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4378 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4379 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004380
4381 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4382 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4383 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4384 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4385 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004386}
4387
4388static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4389 struct tss_segment_16 *tss)
4390{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004391 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004392 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004393 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4394 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4395 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4396 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4397 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4398 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4399 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4400 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02004401
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004402 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004403 return 1;
4404
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004405 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004406 return 1;
4407
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004408 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004409 return 1;
4410
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004411 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004412 return 1;
4413
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004414 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004415 return 1;
4416 return 0;
4417}
4418
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004419static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004420 u16 old_tss_sel, u32 old_tss_base,
4421 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02004422{
4423 struct tss_segment_16 tss_segment_16;
4424 int ret = 0;
4425
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004426 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4427 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004428 goto out;
4429
4430 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02004431
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004432 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4433 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004434 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004435
4436 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4437 &tss_segment_16, sizeof tss_segment_16))
4438 goto out;
4439
Gleb Natapovb237ac32009-03-30 16:03:24 +03004440 if (old_tss_sel != 0xffff) {
4441 tss_segment_16.prev_task_link = old_tss_sel;
4442
4443 if (kvm_write_guest(vcpu->kvm,
4444 get_tss_base_addr(vcpu, nseg_desc),
4445 &tss_segment_16.prev_task_link,
4446 sizeof tss_segment_16.prev_task_link))
4447 goto out;
4448 }
4449
Izik Eidus37817f22008-03-24 23:14:53 +02004450 if (load_state_from_tss16(vcpu, &tss_segment_16))
4451 goto out;
4452
4453 ret = 1;
4454out:
4455 return ret;
4456}
4457
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004458static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004459 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02004460 struct desc_struct *nseg_desc)
4461{
4462 struct tss_segment_32 tss_segment_32;
4463 int ret = 0;
4464
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004465 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4466 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004467 goto out;
4468
4469 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02004470
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004471 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4472 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004473 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004474
4475 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4476 &tss_segment_32, sizeof tss_segment_32))
4477 goto out;
4478
Gleb Natapovb237ac32009-03-30 16:03:24 +03004479 if (old_tss_sel != 0xffff) {
4480 tss_segment_32.prev_task_link = old_tss_sel;
4481
4482 if (kvm_write_guest(vcpu->kvm,
4483 get_tss_base_addr(vcpu, nseg_desc),
4484 &tss_segment_32.prev_task_link,
4485 sizeof tss_segment_32.prev_task_link))
4486 goto out;
4487 }
4488
Izik Eidus37817f22008-03-24 23:14:53 +02004489 if (load_state_from_tss32(vcpu, &tss_segment_32))
4490 goto out;
4491
4492 ret = 1;
4493out:
4494 return ret;
4495}
4496
4497int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4498{
4499 struct kvm_segment tr_seg;
4500 struct desc_struct cseg_desc;
4501 struct desc_struct nseg_desc;
4502 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004503 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4504 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004505
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004506 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02004507
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004508 /* FIXME: Handle errors. Failure to read either TSS or their
4509 * descriptors should generate a pagefault.
4510 */
Izik Eidus37817f22008-03-24 23:14:53 +02004511 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4512 goto out;
4513
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004514 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02004515 goto out;
4516
Izik Eidus37817f22008-03-24 23:14:53 +02004517 if (reason != TASK_SWITCH_IRET) {
4518 int cpl;
4519
4520 cpl = kvm_x86_ops->get_cpl(vcpu);
4521 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4522 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4523 return 1;
4524 }
4525 }
4526
Akinobu Mita46a359e2009-07-18 23:58:32 +09004527 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
Izik Eidus37817f22008-03-24 23:14:53 +02004528 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4529 return 1;
4530 }
4531
4532 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004533 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004534 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004535 }
4536
4537 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02004538 u32 eflags = kvm_get_rflags(vcpu);
4539 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02004540 }
4541
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004542 /* set back link to prev task only if NT bit is set in eflags
4543 note that old_tss_sel is not used afetr this point */
4544 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4545 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02004546
4547 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03004548 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4549 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004550 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03004551 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4552 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004553
4554 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02004555 u32 eflags = kvm_get_rflags(vcpu);
4556 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02004557 }
4558
4559 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004560 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02004561 save_guest_segment_descriptor(vcpu, tss_selector,
4562 &nseg_desc);
4563 }
4564
4565 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4566 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4567 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004568 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004569out:
Izik Eidus37817f22008-03-24 23:14:53 +02004570 return ret;
4571}
4572EXPORT_SYMBOL_GPL(kvm_task_switch);
4573
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004574int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4575 struct kvm_sregs *sregs)
4576{
4577 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004578 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004579 struct descriptor_table dt;
4580
4581 vcpu_load(vcpu);
4582
4583 dt.limit = sregs->idt.limit;
4584 dt.base = sregs->idt.base;
4585 kvm_x86_ops->set_idt(vcpu, &dt);
4586 dt.limit = sregs->gdt.limit;
4587 dt.base = sregs->gdt.base;
4588 kvm_x86_ops->set_gdt(vcpu, &dt);
4589
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004590 vcpu->arch.cr2 = sregs->cr2;
4591 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02004592 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004593
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004594 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004595
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004596 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004597 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004598 kvm_set_apic_base(vcpu, sregs->apic_base);
4599
4600 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4601
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004602 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004603 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004604 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004605
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004606 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004607 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004608 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004609 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004610 mmu_reset_needed = 1;
4611 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004612
4613 if (mmu_reset_needed)
4614 kvm_mmu_reset_context(vcpu);
4615
Gleb Natapov923c61b2009-05-11 13:35:48 +03004616 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4617 pending_vec = find_first_bit(
4618 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4619 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004620 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004621 pr_debug("Set back pending irq %d\n", pending_vec);
4622 if (irqchip_in_kernel(vcpu->kvm))
4623 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004624 }
4625
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004626 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4627 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4628 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4629 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4630 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4631 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004632
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004633 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4634 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004635
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03004636 update_cr8_intercept(vcpu);
4637
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004638 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004639 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004640 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4641 !(vcpu->arch.cr0 & X86_CR0_PE))
4642 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4643
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004644 vcpu_put(vcpu);
4645
4646 return 0;
4647}
4648
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004649int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4650 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004651{
Jan Kiszka355be0b2009-10-03 00:31:21 +02004652 unsigned long rflags;
Jan Kiszka355be0b2009-10-03 00:31:21 +02004653 int i;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004654
4655 vcpu_load(vcpu);
4656
Jan Kiszka91586a32009-10-05 13:07:21 +02004657 /*
4658 * Read rflags as long as potentially injected trace flags are still
4659 * filtered out.
4660 */
4661 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02004662
4663 vcpu->guest_debug = dbg->control;
4664 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
4665 vcpu->guest_debug = 0;
4666
4667 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004668 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4669 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4670 vcpu->arch.switch_db_regs =
4671 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4672 } else {
4673 for (i = 0; i < KVM_NR_DB_REGS; i++)
4674 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4675 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4676 }
4677
Jan Kiszka94fe45d2009-10-18 13:24:44 +02004678 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
4679 vcpu->arch.singlestep_cs =
4680 get_segment_selector(vcpu, VCPU_SREG_CS);
4681 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
4682 }
4683
Jan Kiszka91586a32009-10-05 13:07:21 +02004684 /*
4685 * Trigger an rflags update that will inject or remove the trace
4686 * flags.
4687 */
4688 kvm_set_rflags(vcpu, rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004689
Jan Kiszka355be0b2009-10-03 00:31:21 +02004690 kvm_x86_ops->set_guest_debug(vcpu, dbg);
4691
4692 if (vcpu->guest_debug & KVM_GUESTDBG_INJECT_DB)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004693 kvm_queue_exception(vcpu, DB_VECTOR);
Jan Kiszka355be0b2009-10-03 00:31:21 +02004694 else if (vcpu->guest_debug & KVM_GUESTDBG_INJECT_BP)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004695 kvm_queue_exception(vcpu, BP_VECTOR);
4696
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004697 vcpu_put(vcpu);
4698
Jan Kiszka355be0b2009-10-03 00:31:21 +02004699 return 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004700}
4701
4702/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05004703 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4704 * we have asm/x86/processor.h
4705 */
4706struct fxsave {
4707 u16 cwd;
4708 u16 swd;
4709 u16 twd;
4710 u16 fop;
4711 u64 rip;
4712 u64 rdp;
4713 u32 mxcsr;
4714 u32 mxcsr_mask;
4715 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4716#ifdef CONFIG_X86_64
4717 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4718#else
4719 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4720#endif
4721};
4722
Zhang Xiantao8b006792007-11-16 13:05:55 +08004723/*
4724 * Translate a guest virtual address to a guest physical address.
4725 */
4726int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4727 struct kvm_translation *tr)
4728{
4729 unsigned long vaddr = tr->linear_address;
4730 gpa_t gpa;
4731
4732 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004733 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004734 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004735 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004736 tr->physical_address = gpa;
4737 tr->valid = gpa != UNMAPPED_GVA;
4738 tr->writeable = 1;
4739 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004740 vcpu_put(vcpu);
4741
4742 return 0;
4743}
4744
Hollis Blanchardd0752062007-10-31 17:24:25 -05004745int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4746{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004747 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004748
4749 vcpu_load(vcpu);
4750
4751 memcpy(fpu->fpr, fxsave->st_space, 128);
4752 fpu->fcw = fxsave->cwd;
4753 fpu->fsw = fxsave->swd;
4754 fpu->ftwx = fxsave->twd;
4755 fpu->last_opcode = fxsave->fop;
4756 fpu->last_ip = fxsave->rip;
4757 fpu->last_dp = fxsave->rdp;
4758 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4759
4760 vcpu_put(vcpu);
4761
4762 return 0;
4763}
4764
4765int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4766{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004767 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004768
4769 vcpu_load(vcpu);
4770
4771 memcpy(fxsave->st_space, fpu->fpr, 128);
4772 fxsave->cwd = fpu->fcw;
4773 fxsave->swd = fpu->fsw;
4774 fxsave->twd = fpu->ftwx;
4775 fxsave->fop = fpu->last_opcode;
4776 fxsave->rip = fpu->last_ip;
4777 fxsave->rdp = fpu->last_dp;
4778 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4779
4780 vcpu_put(vcpu);
4781
4782 return 0;
4783}
4784
4785void fx_init(struct kvm_vcpu *vcpu)
4786{
4787 unsigned after_mxcsr_mask;
4788
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004789 /*
4790 * Touch the fpu the first time in non atomic context as if
4791 * this is the first fpu instruction the exception handler
4792 * will fire before the instruction returns and it'll have to
4793 * allocate ram with GFP_KERNEL.
4794 */
4795 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004796 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004797
Hollis Blanchardd0752062007-10-31 17:24:25 -05004798 /* Initialize guest FPU by resetting ours and saving into guest's */
4799 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004800 kvm_fx_save(&vcpu->arch.host_fx_image);
4801 kvm_fx_finit();
4802 kvm_fx_save(&vcpu->arch.guest_fx_image);
4803 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004804 preempt_enable();
4805
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004806 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004807 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004808 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4809 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004810 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4811}
4812EXPORT_SYMBOL_GPL(fx_init);
4813
4814void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4815{
4816 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4817 return;
4818
4819 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004820 kvm_fx_save(&vcpu->arch.host_fx_image);
4821 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004822}
4823EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4824
4825void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4826{
4827 if (!vcpu->guest_fpu_loaded)
4828 return;
4829
4830 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004831 kvm_fx_save(&vcpu->arch.guest_fx_image);
4832 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004833 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004834}
4835EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004836
4837void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4838{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01004839 if (vcpu->arch.time_page) {
4840 kvm_release_page_dirty(vcpu->arch.time_page);
4841 vcpu->arch.time_page = NULL;
4842 }
4843
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004844 kvm_x86_ops->vcpu_free(vcpu);
4845}
4846
4847struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4848 unsigned int id)
4849{
Avi Kivity26e52152007-11-20 15:30:24 +02004850 return kvm_x86_ops->vcpu_create(kvm, id);
4851}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004852
Avi Kivity26e52152007-11-20 15:30:24 +02004853int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4854{
4855 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004856
4857 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004858 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004859
Sheng Yang0bed3b52008-10-09 16:01:54 +08004860 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004861 vcpu_load(vcpu);
4862 r = kvm_arch_vcpu_reset(vcpu);
4863 if (r == 0)
4864 r = kvm_mmu_setup(vcpu);
4865 vcpu_put(vcpu);
4866 if (r < 0)
4867 goto free_vcpu;
4868
Avi Kivity26e52152007-11-20 15:30:24 +02004869 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004870free_vcpu:
4871 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004872 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004873}
4874
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004875void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004876{
4877 vcpu_load(vcpu);
4878 kvm_mmu_unload(vcpu);
4879 vcpu_put(vcpu);
4880
4881 kvm_x86_ops->vcpu_free(vcpu);
4882}
4883
4884int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4885{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004886 vcpu->arch.nmi_pending = false;
4887 vcpu->arch.nmi_injected = false;
4888
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004889 vcpu->arch.switch_db_regs = 0;
4890 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4891 vcpu->arch.dr6 = DR6_FIXED_1;
4892 vcpu->arch.dr7 = DR7_FIXED_1;
4893
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004894 return kvm_x86_ops->vcpu_reset(vcpu);
4895}
4896
Alexander Graf10474ae2009-09-15 11:37:46 +02004897int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004898{
Zachary Amsden0cca7902009-09-29 11:38:35 -10004899 /*
4900 * Since this may be called from a hotplug notifcation,
4901 * we can't get the CPU frequency directly.
4902 */
4903 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
4904 int cpu = raw_smp_processor_id();
4905 per_cpu(cpu_tsc_khz, cpu) = 0;
4906 }
Avi Kivity18863bd2009-09-07 11:12:18 +03004907
4908 kvm_shared_msr_cpu_online();
4909
Alexander Graf10474ae2009-09-15 11:37:46 +02004910 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004911}
4912
4913void kvm_arch_hardware_disable(void *garbage)
4914{
4915 kvm_x86_ops->hardware_disable(garbage);
4916}
4917
4918int kvm_arch_hardware_setup(void)
4919{
4920 return kvm_x86_ops->hardware_setup();
4921}
4922
4923void kvm_arch_hardware_unsetup(void)
4924{
4925 kvm_x86_ops->hardware_unsetup();
4926}
4927
4928void kvm_arch_check_processor_compat(void *rtn)
4929{
4930 kvm_x86_ops->check_processor_compatibility(rtn);
4931}
4932
4933int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4934{
4935 struct page *page;
4936 struct kvm *kvm;
4937 int r;
4938
4939 BUG_ON(vcpu->kvm == NULL);
4940 kvm = vcpu->kvm;
4941
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004942 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004943 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03004944 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004945 else
Avi Kivitya4535292008-04-13 17:54:35 +03004946 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004947
4948 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4949 if (!page) {
4950 r = -ENOMEM;
4951 goto fail;
4952 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004953 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004954
4955 r = kvm_mmu_create(vcpu);
4956 if (r < 0)
4957 goto fail_free_pio_data;
4958
4959 if (irqchip_in_kernel(kvm)) {
4960 r = kvm_create_lapic(vcpu);
4961 if (r < 0)
4962 goto fail_mmu_destroy;
4963 }
4964
Huang Ying890ca9a2009-05-11 16:48:15 +08004965 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
4966 GFP_KERNEL);
4967 if (!vcpu->arch.mce_banks) {
4968 r = -ENOMEM;
4969 goto fail_mmu_destroy;
4970 }
4971 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
4972
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004973 return 0;
4974
4975fail_mmu_destroy:
4976 kvm_mmu_destroy(vcpu);
4977fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004978 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004979fail:
4980 return r;
4981}
4982
4983void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4984{
4985 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004986 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004987 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004988 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004989 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004990}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004991
4992struct kvm *kvm_arch_create_vm(void)
4993{
4994 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4995
4996 if (!kvm)
4997 return ERR_PTR(-ENOMEM);
4998
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004999 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005000 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005001
Sheng Yang5550af42008-10-15 20:15:06 +08005002 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5003 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5004
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005005 rdtscll(kvm->arch.vm_init_tsc);
5006
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005007 return kvm;
5008}
5009
5010static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5011{
5012 vcpu_load(vcpu);
5013 kvm_mmu_unload(vcpu);
5014 vcpu_put(vcpu);
5015}
5016
5017static void kvm_free_vcpus(struct kvm *kvm)
5018{
5019 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005020 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005021
5022 /*
5023 * Unpin any mmu pages first.
5024 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005025 kvm_for_each_vcpu(i, vcpu, kvm)
5026 kvm_unload_vcpu_mmu(vcpu);
5027 kvm_for_each_vcpu(i, vcpu, kvm)
5028 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005029
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005030 mutex_lock(&kvm->lock);
5031 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5032 kvm->vcpus[i] = NULL;
5033
5034 atomic_set(&kvm->online_vcpus, 0);
5035 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005036}
5037
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005038void kvm_arch_sync_events(struct kvm *kvm)
5039{
Sheng Yangba4cef32009-01-06 10:03:03 +08005040 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005041}
5042
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005043void kvm_arch_destroy_vm(struct kvm *kvm)
5044{
Sheng Yang6eb55812008-10-31 12:37:41 +08005045 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005046 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005047 kfree(kvm->arch.vpic);
5048 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005049 kvm_free_vcpus(kvm);
5050 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005051 if (kvm->arch.apic_access_page)
5052 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005053 if (kvm->arch.ept_identity_pagetable)
5054 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005055 kfree(kvm);
5056}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005057
5058int kvm_arch_set_memory_region(struct kvm *kvm,
5059 struct kvm_userspace_memory_region *mem,
5060 struct kvm_memory_slot old,
5061 int user_alloc)
5062{
5063 int npages = mem->memory_size >> PAGE_SHIFT;
5064 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
5065
5066 /*To keep backward compatibility with older userspace,
5067 *x86 needs to hanlde !user_alloc case.
5068 */
5069 if (!user_alloc) {
5070 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005071 unsigned long userspace_addr;
5072
Izik Eidus72dc67a2008-02-10 18:04:15 +02005073 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005074 userspace_addr = do_mmap(NULL, 0,
5075 npages * PAGE_SIZE,
5076 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005077 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005078 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005079 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005080
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005081 if (IS_ERR((void *)userspace_addr))
5082 return PTR_ERR((void *)userspace_addr);
5083
5084 /* set userspace_addr atomically for kvm_hva_to_rmapp */
5085 spin_lock(&kvm->mmu_lock);
5086 memslot->userspace_addr = userspace_addr;
5087 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005088 } else {
5089 if (!old.user_alloc && old.rmap) {
5090 int ret;
5091
Izik Eidus72dc67a2008-02-10 18:04:15 +02005092 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005093 ret = do_munmap(current->mm, old.userspace_addr,
5094 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005095 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005096 if (ret < 0)
5097 printk(KERN_WARNING
5098 "kvm_vm_ioctl_set_memory_region: "
5099 "failed to munmap memory\n");
5100 }
5101 }
5102 }
5103
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005104 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005105 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005106 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5107 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5108 }
5109
5110 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005111 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005112
5113 return 0;
5114}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005115
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005116void kvm_arch_flush_shadow(struct kvm *kvm)
5117{
5118 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005119 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005120}
5121
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005122int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5123{
Avi Kivitya4535292008-04-13 17:54:35 +03005124 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005125 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5126 || vcpu->arch.nmi_pending ||
5127 (kvm_arch_interrupt_allowed(vcpu) &&
5128 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005129}
Zhang Xiantao57361992007-12-17 14:21:40 +08005130
Zhang Xiantao57361992007-12-17 14:21:40 +08005131void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5132{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005133 int me;
5134 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005135
5136 if (waitqueue_active(&vcpu->wq)) {
5137 wake_up_interruptible(&vcpu->wq);
5138 ++vcpu->stat.halt_wakeup;
5139 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005140
5141 me = get_cpu();
5142 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5143 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5144 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005145 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005146}
Gleb Natapov78646122009-03-23 12:12:11 +02005147
5148int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5149{
5150 return kvm_x86_ops->interrupt_allowed(vcpu);
5151}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005152
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005153unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5154{
5155 unsigned long rflags;
5156
5157 rflags = kvm_x86_ops->get_rflags(vcpu);
5158 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5159 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5160 return rflags;
5161}
5162EXPORT_SYMBOL_GPL(kvm_get_rflags);
5163
5164void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5165{
5166 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5167 vcpu->arch.singlestep_cs ==
5168 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5169 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5170 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5171 kvm_x86_ops->set_rflags(vcpu, rflags);
5172}
5173EXPORT_SYMBOL_GPL(kvm_set_rflags);
5174
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005175EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5176EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5177EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5178EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5179EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005180EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005181EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005182EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005183EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005184EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005185EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);