blob: a1e1bc9d412dadca87e4a7be6c7903ab0768a8a8 [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
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020046#include <asm/debugreg.h>
Carsten Otte043405e2007-10-10 17:16:19 +020047#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080048#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020049#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080050#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080051#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020052
Carsten Otte313a3dc2007-10-11 19:16:52 +020053#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010054#define CR0_RESERVED_BITS \
55 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
56 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
57 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
58#define CR4_RESERVED_BITS \
59 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
60 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
61 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
62 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
63
64#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080065
66#define KVM_MAX_MCE_BANKS 32
67#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
68
Joerg Roedel50a37eb2008-01-31 14:57:38 +010069/* EFER defaults:
70 * - enable syscall per default because its emulated by KVM
71 * - enable LME and LMA per default on 64 bit KVM
72 */
73#ifdef CONFIG_X86_64
74static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
75#else
76static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
77#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020078
Avi Kivityba1389b2007-11-18 16:24:12 +020079#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
80#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050081
Gleb Natapovcb142eb2009-08-09 15:17:40 +030082static void update_cr8_intercept(struct kvm_vcpu *vcpu);
Avi Kivity674eea02008-02-11 18:37:23 +020083static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
84 struct kvm_cpuid_entry2 __user *entries);
85
Zhang Xiantao97896d02007-11-14 20:09:30 +080086struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030087EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080088
Andre Przywaraed85c062009-06-25 12:36:49 +020089int ignore_msrs = 0;
90module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
91
Avi Kivity18863bd2009-09-07 11:12:18 +030092#define KVM_NR_SHARED_MSRS 16
93
94struct kvm_shared_msrs_global {
95 int nr;
96 struct kvm_shared_msr {
97 u32 msr;
98 u64 value;
99 } msrs[KVM_NR_SHARED_MSRS];
100};
101
102struct kvm_shared_msrs {
103 struct user_return_notifier urn;
104 bool registered;
105 u64 current_value[KVM_NR_SHARED_MSRS];
106};
107
108static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
109static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
110
Hollis Blanchard417bc302007-10-31 17:24:23 -0500111struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +0200112 { "pf_fixed", VCPU_STAT(pf_fixed) },
113 { "pf_guest", VCPU_STAT(pf_guest) },
114 { "tlb_flush", VCPU_STAT(tlb_flush) },
115 { "invlpg", VCPU_STAT(invlpg) },
116 { "exits", VCPU_STAT(exits) },
117 { "io_exits", VCPU_STAT(io_exits) },
118 { "mmio_exits", VCPU_STAT(mmio_exits) },
119 { "signal_exits", VCPU_STAT(signal_exits) },
120 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +0800121 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200122 { "halt_exits", VCPU_STAT(halt_exits) },
123 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530124 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200125 { "request_irq", VCPU_STAT(request_irq_exits) },
126 { "irq_exits", VCPU_STAT(irq_exits) },
127 { "host_state_reload", VCPU_STAT(host_state_reload) },
128 { "efer_reload", VCPU_STAT(efer_reload) },
129 { "fpu_reload", VCPU_STAT(fpu_reload) },
130 { "insn_emulation", VCPU_STAT(insn_emulation) },
131 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300132 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200133 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200134 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
135 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
136 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
137 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
138 { "mmu_flooded", VM_STAT(mmu_flooded) },
139 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200140 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300141 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200142 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300143 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500144 { NULL }
145};
146
Avi Kivity18863bd2009-09-07 11:12:18 +0300147static void kvm_on_user_return(struct user_return_notifier *urn)
148{
149 unsigned slot;
150 struct kvm_shared_msr *global;
151 struct kvm_shared_msrs *locals
152 = container_of(urn, struct kvm_shared_msrs, urn);
153
154 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
155 global = &shared_msrs_global.msrs[slot];
156 if (global->value != locals->current_value[slot]) {
157 wrmsrl(global->msr, global->value);
158 locals->current_value[slot] = global->value;
159 }
160 }
161 locals->registered = false;
162 user_return_notifier_unregister(urn);
163}
164
165void kvm_define_shared_msr(unsigned slot, u32 msr)
166{
167 int cpu;
168 u64 value;
169
170 if (slot >= shared_msrs_global.nr)
171 shared_msrs_global.nr = slot + 1;
172 shared_msrs_global.msrs[slot].msr = msr;
173 rdmsrl_safe(msr, &value);
174 shared_msrs_global.msrs[slot].value = value;
175 for_each_online_cpu(cpu)
176 per_cpu(shared_msrs, cpu).current_value[slot] = value;
177}
178EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
179
180static void kvm_shared_msr_cpu_online(void)
181{
182 unsigned i;
183 struct kvm_shared_msrs *locals = &__get_cpu_var(shared_msrs);
184
185 for (i = 0; i < shared_msrs_global.nr; ++i)
186 locals->current_value[i] = shared_msrs_global.msrs[i].value;
187}
188
Avi Kivityd5696722009-12-02 12:28:47 +0200189void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
Avi Kivity18863bd2009-09-07 11:12:18 +0300190{
191 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
192
Avi Kivityd5696722009-12-02 12:28:47 +0200193 if (((value ^ smsr->current_value[slot]) & mask) == 0)
Avi Kivity18863bd2009-09-07 11:12:18 +0300194 return;
195 smsr->current_value[slot] = value;
196 wrmsrl(shared_msrs_global.msrs[slot].msr, value);
197 if (!smsr->registered) {
198 smsr->urn.on_user_return = kvm_on_user_return;
199 user_return_notifier_register(&smsr->urn);
200 smsr->registered = true;
201 }
202}
203EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
204
Avi Kivity3548bab2009-11-28 14:18:47 +0200205static void drop_user_return_notifiers(void *ignore)
206{
207 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
208
209 if (smsr->registered)
210 kvm_on_user_return(&smsr->urn);
211}
212
Carsten Otte5fb76f92007-10-29 16:08:51 +0100213unsigned long segment_base(u16 selector)
214{
215 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200216 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100217 unsigned long table_base;
218 unsigned long v;
219
220 if (selector == 0)
221 return 0;
222
Akinobu Mitab792c342009-07-19 00:00:01 +0900223 kvm_get_gdt(&gdt);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100224 table_base = gdt.base;
225
226 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900227 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100228
Carsten Otte5fb76f92007-10-29 16:08:51 +0100229 table_base = segment_base(ldt_selector);
230 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200231 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900232 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100233#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200234 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
235 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100236#endif
237 return v;
238}
239EXPORT_SYMBOL_GPL(segment_base);
240
Carsten Otte6866b832007-10-29 16:09:10 +0100241u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
242{
243 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800244 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100245 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800246 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100247}
248EXPORT_SYMBOL_GPL(kvm_get_apic_base);
249
250void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
251{
252 /* TODO: reserve bits check */
253 if (irqchip_in_kernel(vcpu->kvm))
254 kvm_lapic_set_base(vcpu, data);
255 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800256 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100257}
258EXPORT_SYMBOL_GPL(kvm_set_apic_base);
259
Avi Kivity298101d2007-11-25 13:41:11 +0200260void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
261{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800262 WARN_ON(vcpu->arch.exception.pending);
263 vcpu->arch.exception.pending = true;
264 vcpu->arch.exception.has_error_code = false;
265 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200266}
267EXPORT_SYMBOL_GPL(kvm_queue_exception);
268
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200269void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
270 u32 error_code)
271{
272 ++vcpu->stat.pf_guest;
Alexander Grafd8017472008-11-25 20:17:11 +0100273
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100274 if (vcpu->arch.exception.pending) {
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300275 switch(vcpu->arch.exception.nr) {
276 case DF_VECTOR:
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100277 /* triple fault -> shutdown */
278 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300279 return;
280 case PF_VECTOR:
281 vcpu->arch.exception.nr = DF_VECTOR;
282 vcpu->arch.exception.error_code = 0;
283 return;
284 default:
285 /* replace previous exception with a new one in a hope
286 that instruction re-execution will regenerate lost
287 exception */
288 vcpu->arch.exception.pending = false;
289 break;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100290 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200291 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800292 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200293 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
294}
295
Sheng Yang3419ffc2008-05-15 09:52:48 +0800296void kvm_inject_nmi(struct kvm_vcpu *vcpu)
297{
298 vcpu->arch.nmi_pending = 1;
299}
300EXPORT_SYMBOL_GPL(kvm_inject_nmi);
301
Avi Kivity298101d2007-11-25 13:41:11 +0200302void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
303{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800304 WARN_ON(vcpu->arch.exception.pending);
305 vcpu->arch.exception.pending = true;
306 vcpu->arch.exception.has_error_code = true;
307 vcpu->arch.exception.nr = nr;
308 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200309}
310EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
311
Carsten Ottea03490e2007-10-29 16:09:35 +0100312/*
Avi Kivity0a79b002009-09-01 12:03:25 +0300313 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
314 * a #GP and return false.
315 */
316bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
Carsten Otte043405e2007-10-10 17:16:19 +0200317{
Avi Kivity0a79b002009-09-01 12:03:25 +0300318 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
319 return true;
320 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
321 return false;
Carsten Ottea03490e2007-10-29 16:09:35 +0100322}
Avi Kivity0a79b002009-09-01 12:03:25 +0300323EXPORT_SYMBOL_GPL(kvm_require_cpl);
Carsten Ottea03490e2007-10-29 16:09:35 +0100324
325/*
326 * Load the pae pdptrs. Return true is they are all valid.
327 */
328int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
329{
330 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
331 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
332 int i;
333 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100335
Carsten Ottea03490e2007-10-29 16:09:35 +0100336 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
337 offset * sizeof(u64), sizeof(pdpte));
338 if (ret < 0) {
339 ret = 0;
340 goto out;
341 }
342 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300343 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800344 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100345 ret = 0;
346 goto out;
347 }
348 }
349 ret = 1;
350
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300352 __set_bit(VCPU_EXREG_PDPTR,
353 (unsigned long *)&vcpu->arch.regs_avail);
354 __set_bit(VCPU_EXREG_PDPTR,
355 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100356out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100357
358 return ret;
359}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100360EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100361
Avi Kivityd835dfe2007-11-21 02:57:59 +0200362static bool pdptrs_changed(struct kvm_vcpu *vcpu)
363{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800364 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200365 bool changed = true;
366 int r;
367
368 if (is_long_mode(vcpu) || !is_pae(vcpu))
369 return false;
370
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300371 if (!test_bit(VCPU_EXREG_PDPTR,
372 (unsigned long *)&vcpu->arch.regs_avail))
373 return true;
374
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800375 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200376 if (r < 0)
377 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800378 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200379out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200380
381 return changed;
382}
383
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200384void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100385{
386 if (cr0 & CR0_RESERVED_BITS) {
387 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800388 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200389 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100390 return;
391 }
392
393 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
394 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200395 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100396 return;
397 }
398
399 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
400 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
401 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200402 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100403 return;
404 }
405
406 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
407#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800408 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100409 int cs_db, cs_l;
410
411 if (!is_pae(vcpu)) {
412 printk(KERN_DEBUG "set_cr0: #GP, start paging "
413 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200414 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100415 return;
416 }
417 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
418 if (cs_l) {
419 printk(KERN_DEBUG "set_cr0: #GP, start paging "
420 "in long mode while CS.L == 1\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 } else
426#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800427 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100428 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
429 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200430 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100431 return;
432 }
433
434 }
435
436 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800437 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100438
Carsten Ottea03490e2007-10-29 16:09:35 +0100439 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100440 return;
441}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200442EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100443
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200444void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100445{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200446 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100447}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200448EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100449
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200450void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100451{
Avi Kivitya2edf572009-05-24 22:19:00 +0300452 unsigned long old_cr4 = vcpu->arch.cr4;
453 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
454
Carsten Ottea03490e2007-10-29 16:09:35 +0100455 if (cr4 & CR4_RESERVED_BITS) {
456 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200457 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100458 return;
459 }
460
461 if (is_long_mode(vcpu)) {
462 if (!(cr4 & X86_CR4_PAE)) {
463 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
464 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200465 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100466 return;
467 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300468 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
469 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800470 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100471 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200472 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100473 return;
474 }
475
476 if (cr4 & X86_CR4_VMXE) {
477 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200478 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100479 return;
480 }
481 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800482 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200483 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100484 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100485}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200486EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100487
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200488void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100489{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800490 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300491 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200492 kvm_mmu_flush_tlb(vcpu);
493 return;
494 }
495
Carsten Ottea03490e2007-10-29 16:09:35 +0100496 if (is_long_mode(vcpu)) {
497 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
498 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200499 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100500 return;
501 }
502 } else {
503 if (is_pae(vcpu)) {
504 if (cr3 & CR3_PAE_RESERVED_BITS) {
505 printk(KERN_DEBUG
506 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200507 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100508 return;
509 }
510 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
511 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
512 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200513 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100514 return;
515 }
516 }
517 /*
518 * We don't check reserved bits in nonpae mode, because
519 * this isn't enforced, and VMware depends on this.
520 */
521 }
522
Carsten Ottea03490e2007-10-29 16:09:35 +0100523 /*
524 * Does the new cr3 value map to physical memory? (Note, we
525 * catch an invalid cr3 even in real-mode, because it would
526 * cause trouble later on when we turn on paging anyway.)
527 *
528 * A real CPU would silently accept an invalid cr3 and would
529 * attempt to use it - with largely undefined (and often hard
530 * to debug) behavior on the guest side.
531 */
532 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200533 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100534 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800535 vcpu->arch.cr3 = cr3;
536 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100537 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100538}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200539EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100540
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200541void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100542{
543 if (cr8 & CR8_RESERVED_BITS) {
544 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200545 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100546 return;
547 }
548 if (irqchip_in_kernel(vcpu->kvm))
549 kvm_lapic_set_tpr(vcpu, cr8);
550 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800551 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100552}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200553EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100554
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200555unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100556{
557 if (irqchip_in_kernel(vcpu->kvm))
558 return kvm_lapic_get_cr8(vcpu);
559 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800560 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100561}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200562EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100563
Alexander Grafd8017472008-11-25 20:17:11 +0100564static inline u32 bit(int bitno)
565{
566 return 1 << (bitno & 31);
567}
568
Carsten Otte043405e2007-10-10 17:16:19 +0200569/*
570 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
571 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
572 *
573 * This list is modified at module load time to reflect the
Glauber Costae3267cb2009-10-06 13:24:50 -0400574 * capabilities of the host cpu. This capabilities test skips MSRs that are
575 * kvm-specific. Those are put in the beginning of the list.
Carsten Otte043405e2007-10-10 17:16:19 +0200576 */
Glauber Costae3267cb2009-10-06 13:24:50 -0400577
578#define KVM_SAVE_MSRS_BEGIN 2
Carsten Otte043405e2007-10-10 17:16:19 +0200579static u32 msrs_to_save[] = {
Glauber Costae3267cb2009-10-06 13:24:50 -0400580 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Carsten Otte043405e2007-10-10 17:16:19 +0200581 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
582 MSR_K6_STAR,
583#ifdef CONFIG_X86_64
584 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
585#endif
Glauber Costae3267cb2009-10-06 13:24:50 -0400586 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200587};
588
589static unsigned num_msrs_to_save;
590
591static u32 emulated_msrs[] = {
592 MSR_IA32_MISC_ENABLE,
593};
594
Carsten Otte15c4a642007-10-30 18:44:17 +0100595static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
596{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100597 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100598 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
599 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200600 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100601 return;
602 }
603
604 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800605 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100606 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200607 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100608 return;
609 }
610
Alexander Graf1b2fd702009-02-02 16:23:51 +0100611 if (efer & EFER_FFXSR) {
612 struct kvm_cpuid_entry2 *feat;
613
614 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
615 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
616 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
617 kvm_inject_gp(vcpu, 0);
618 return;
619 }
620 }
621
Alexander Grafd8017472008-11-25 20:17:11 +0100622 if (efer & EFER_SVME) {
623 struct kvm_cpuid_entry2 *feat;
624
625 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
626 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
627 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
628 kvm_inject_gp(vcpu, 0);
629 return;
630 }
631 }
632
Carsten Otte15c4a642007-10-30 18:44:17 +0100633 kvm_x86_ops->set_efer(vcpu, efer);
634
635 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800636 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100637
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800638 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300639
640 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
641 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100642}
643
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100644void kvm_enable_efer_bits(u64 mask)
645{
646 efer_reserved_bits &= ~mask;
647}
648EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
649
650
Carsten Otte15c4a642007-10-30 18:44:17 +0100651/*
652 * Writes msr value into into the appropriate "register".
653 * Returns 0 on success, non-0 otherwise.
654 * Assumes vcpu_load() was already called.
655 */
656int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
657{
658 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
659}
660
Carsten Otte313a3dc2007-10-11 19:16:52 +0200661/*
662 * Adapt set_msr() to msr_io()'s calling convention
663 */
664static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
665{
666 return kvm_set_msr(vcpu, index, *data);
667}
668
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200669static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
670{
671 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200672 struct pvclock_wall_clock wc;
Jason Wang923de3c2010-01-27 19:13:49 +0800673 struct timespec boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200674
675 if (!wall_clock)
676 return;
677
678 version++;
679
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200680 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
681
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200682 /*
683 * The guest calculates current wall clock time by adding
684 * system time (updated by kvm_write_guest_time below) to the
685 * wall clock specified here. guest system time equals host
686 * system time for us, thus we must fill in host boot time here.
687 */
Jason Wang923de3c2010-01-27 19:13:49 +0800688 getboottime(&boot);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200689
690 wc.sec = boot.tv_sec;
691 wc.nsec = boot.tv_nsec;
692 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200693
694 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
695
696 version++;
697 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200698}
699
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200700static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
701{
702 uint32_t quotient, remainder;
703
704 /* Don't try to replace with do_div(), this one calculates
705 * "(dividend << 32) / divisor" */
706 __asm__ ( "divl %4"
707 : "=a" (quotient), "=d" (remainder)
708 : "0" (0), "1" (dividend), "r" (divisor) );
709 return quotient;
710}
711
712static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
713{
714 uint64_t nsecs = 1000000000LL;
715 int32_t shift = 0;
716 uint64_t tps64;
717 uint32_t tps32;
718
719 tps64 = tsc_khz * 1000LL;
720 while (tps64 > nsecs*2) {
721 tps64 >>= 1;
722 shift--;
723 }
724
725 tps32 = (uint32_t)tps64;
726 while (tps32 <= (uint32_t)nsecs) {
727 tps32 <<= 1;
728 shift++;
729 }
730
731 hv_clock->tsc_shift = shift;
732 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
733
734 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700735 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200736 hv_clock->tsc_to_system_mul);
737}
738
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100739static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
740
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200741static void kvm_write_guest_time(struct kvm_vcpu *v)
742{
743 struct timespec ts;
744 unsigned long flags;
745 struct kvm_vcpu_arch *vcpu = &v->arch;
746 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300747 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200748
749 if ((!vcpu->time_page))
750 return;
751
Avi Kivity463656c2009-04-12 15:49:07 +0300752 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
753 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
754 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
755 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200756 }
Avi Kivity463656c2009-04-12 15:49:07 +0300757 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200758
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200759 /* Keep irq disabled to prevent changes to the clock */
760 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530761 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200762 ktime_get_ts(&ts);
Jason Wang923de3c2010-01-27 19:13:49 +0800763 monotonic_to_bootbased(&ts);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200764 local_irq_restore(flags);
765
766 /* With all the info we got, fill in the values */
767
768 vcpu->hv_clock.system_time = ts.tv_nsec +
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400769 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
770
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200771 /*
772 * The interface expects us to write an even number signaling that the
773 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200774 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200775 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200776 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200777
778 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
779
780 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200781 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200782
783 kunmap_atomic(shared_kaddr, KM_USER0);
784
785 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
786}
787
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100788static int kvm_request_guest_time_update(struct kvm_vcpu *v)
789{
790 struct kvm_vcpu_arch *vcpu = &v->arch;
791
792 if (!vcpu->time_page)
793 return 0;
794 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
795 return 1;
796}
797
Avi Kivity9ba075a2008-05-26 20:06:35 +0300798static bool msr_mtrr_valid(unsigned msr)
799{
800 switch (msr) {
801 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
802 case MSR_MTRRfix64K_00000:
803 case MSR_MTRRfix16K_80000:
804 case MSR_MTRRfix16K_A0000:
805 case MSR_MTRRfix4K_C0000:
806 case MSR_MTRRfix4K_C8000:
807 case MSR_MTRRfix4K_D0000:
808 case MSR_MTRRfix4K_D8000:
809 case MSR_MTRRfix4K_E0000:
810 case MSR_MTRRfix4K_E8000:
811 case MSR_MTRRfix4K_F0000:
812 case MSR_MTRRfix4K_F8000:
813 case MSR_MTRRdefType:
814 case MSR_IA32_CR_PAT:
815 return true;
816 case 0x2f8:
817 return true;
818 }
819 return false;
820}
821
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300822static bool valid_pat_type(unsigned t)
823{
824 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
825}
826
827static bool valid_mtrr_type(unsigned t)
828{
829 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
830}
831
832static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
833{
834 int i;
835
836 if (!msr_mtrr_valid(msr))
837 return false;
838
839 if (msr == MSR_IA32_CR_PAT) {
840 for (i = 0; i < 8; i++)
841 if (!valid_pat_type((data >> (i * 8)) & 0xff))
842 return false;
843 return true;
844 } else if (msr == MSR_MTRRdefType) {
845 if (data & ~0xcff)
846 return false;
847 return valid_mtrr_type(data & 0xff);
848 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
849 for (i = 0; i < 8 ; i++)
850 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
851 return false;
852 return true;
853 }
854
855 /* variable MTRRs */
856 return valid_mtrr_type(data & 0xff);
857}
858
Avi Kivity9ba075a2008-05-26 20:06:35 +0300859static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
860{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800861 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
862
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300863 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300864 return 1;
865
Sheng Yang0bed3b52008-10-09 16:01:54 +0800866 if (msr == MSR_MTRRdefType) {
867 vcpu->arch.mtrr_state.def_type = data;
868 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
869 } else if (msr == MSR_MTRRfix64K_00000)
870 p[0] = data;
871 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
872 p[1 + msr - MSR_MTRRfix16K_80000] = data;
873 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
874 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
875 else if (msr == MSR_IA32_CR_PAT)
876 vcpu->arch.pat = data;
877 else { /* Variable MTRRs */
878 int idx, is_mtrr_mask;
879 u64 *pt;
880
881 idx = (msr - 0x200) / 2;
882 is_mtrr_mask = msr - 0x200 - 2 * idx;
883 if (!is_mtrr_mask)
884 pt =
885 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
886 else
887 pt =
888 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
889 *pt = data;
890 }
891
892 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300893 return 0;
894}
Carsten Otte15c4a642007-10-30 18:44:17 +0100895
Huang Ying890ca9a2009-05-11 16:48:15 +0800896static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
897{
898 u64 mcg_cap = vcpu->arch.mcg_cap;
899 unsigned bank_num = mcg_cap & 0xff;
900
901 switch (msr) {
902 case MSR_IA32_MCG_STATUS:
903 vcpu->arch.mcg_status = data;
904 break;
905 case MSR_IA32_MCG_CTL:
906 if (!(mcg_cap & MCG_CTL_P))
907 return 1;
908 if (data != 0 && data != ~(u64)0)
909 return -1;
910 vcpu->arch.mcg_ctl = data;
911 break;
912 default:
913 if (msr >= MSR_IA32_MC0_CTL &&
914 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
915 u32 offset = msr - MSR_IA32_MC0_CTL;
916 /* only 0 or all 1s can be written to IA32_MCi_CTL */
917 if ((offset & 0x3) == 0 &&
918 data != 0 && data != ~(u64)0)
919 return -1;
920 vcpu->arch.mce_banks[offset] = data;
921 break;
922 }
923 return 1;
924 }
925 return 0;
926}
927
Ed Swierkffde22a2009-10-15 15:21:43 -0700928static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
929{
930 struct kvm *kvm = vcpu->kvm;
931 int lm = is_long_mode(vcpu);
932 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
933 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
934 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
935 : kvm->arch.xen_hvm_config.blob_size_32;
936 u32 page_num = data & ~PAGE_MASK;
937 u64 page_addr = data & PAGE_MASK;
938 u8 *page;
939 int r;
940
941 r = -E2BIG;
942 if (page_num >= blob_size)
943 goto out;
944 r = -ENOMEM;
945 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
946 if (!page)
947 goto out;
948 r = -EFAULT;
949 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
950 goto out_free;
951 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
952 goto out_free;
953 r = 0;
954out_free:
955 kfree(page);
956out:
957 return r;
958}
959
Carsten Otte15c4a642007-10-30 18:44:17 +0100960int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
961{
962 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100963 case MSR_EFER:
964 set_efer(vcpu, data);
965 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +0200966 case MSR_K7_HWCR:
967 data &= ~(u64)0x40; /* ignore flush filter disable */
968 if (data != 0) {
969 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
970 data);
971 return 1;
972 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100973 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +0200974 case MSR_FAM10H_MMIO_CONF_BASE:
975 if (data != 0) {
976 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
977 "0x%llx\n", data);
978 return 1;
979 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100980 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +0200981 case MSR_AMD64_NB_CFG:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100982 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200983 case MSR_IA32_DEBUGCTLMSR:
984 if (!data) {
985 /* We support the non-activated case already */
986 break;
987 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
988 /* Values other than LBR and BTF are vendor-specific,
989 thus reserved and should throw a #GP */
990 return 1;
991 }
992 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
993 __func__, data);
994 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100995 case MSR_IA32_UCODE_REV:
996 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200997 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +0200998 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +0100999 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001000 case 0x200 ... 0x2ff:
1001 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001002 case MSR_IA32_APICBASE:
1003 kvm_set_apic_base(vcpu, data);
1004 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001005 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1006 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +01001007 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001008 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +01001009 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001010 case MSR_KVM_WALL_CLOCK:
1011 vcpu->kvm->arch.wall_clock = data;
1012 kvm_write_wall_clock(vcpu->kvm, data);
1013 break;
1014 case MSR_KVM_SYSTEM_TIME: {
1015 if (vcpu->arch.time_page) {
1016 kvm_release_page_dirty(vcpu->arch.time_page);
1017 vcpu->arch.time_page = NULL;
1018 }
1019
1020 vcpu->arch.time = data;
1021
1022 /* we verify if the enable bit is set... */
1023 if (!(data & 1))
1024 break;
1025
1026 /* ...but clean it before doing the actual write */
1027 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001029 vcpu->arch.time_page =
1030 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001031
1032 if (is_error_page(vcpu->arch.time_page)) {
1033 kvm_release_page_clean(vcpu->arch.time_page);
1034 vcpu->arch.time_page = NULL;
1035 }
1036
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001037 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001038 break;
1039 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001040 case MSR_IA32_MCG_CTL:
1041 case MSR_IA32_MCG_STATUS:
1042 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1043 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +02001044
1045 /* Performance counters are not protected by a CPUID bit,
1046 * so we should check all of them in the generic path for the sake of
1047 * cross vendor migration.
1048 * Writing a zero into the event select MSRs disables them,
1049 * which we perfectly emulate ;-). Any other value should be at least
1050 * reported, some guests depend on them.
1051 */
1052 case MSR_P6_EVNTSEL0:
1053 case MSR_P6_EVNTSEL1:
1054 case MSR_K7_EVNTSEL0:
1055 case MSR_K7_EVNTSEL1:
1056 case MSR_K7_EVNTSEL2:
1057 case MSR_K7_EVNTSEL3:
1058 if (data != 0)
1059 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1060 "0x%x data 0x%llx\n", msr, data);
1061 break;
1062 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1063 * so we ignore writes to make it happy.
1064 */
1065 case MSR_P6_PERFCTR0:
1066 case MSR_P6_PERFCTR1:
1067 case MSR_K7_PERFCTR0:
1068 case MSR_K7_PERFCTR1:
1069 case MSR_K7_PERFCTR2:
1070 case MSR_K7_PERFCTR3:
1071 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1072 "0x%x data 0x%llx\n", msr, data);
1073 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001074 default:
Ed Swierkffde22a2009-10-15 15:21:43 -07001075 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1076 return xen_hvm_config(vcpu, data);
Andre Przywaraed85c062009-06-25 12:36:49 +02001077 if (!ignore_msrs) {
1078 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1079 msr, data);
1080 return 1;
1081 } else {
1082 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1083 msr, data);
1084 break;
1085 }
Carsten Otte15c4a642007-10-30 18:44:17 +01001086 }
1087 return 0;
1088}
1089EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1090
1091
1092/*
1093 * Reads an msr value (of 'msr_index') into 'pdata'.
1094 * Returns 0 on success, non-0 otherwise.
1095 * Assumes vcpu_load() was already called.
1096 */
1097int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1098{
1099 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1100}
1101
Avi Kivity9ba075a2008-05-26 20:06:35 +03001102static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1103{
Sheng Yang0bed3b52008-10-09 16:01:54 +08001104 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1105
Avi Kivity9ba075a2008-05-26 20:06:35 +03001106 if (!msr_mtrr_valid(msr))
1107 return 1;
1108
Sheng Yang0bed3b52008-10-09 16:01:54 +08001109 if (msr == MSR_MTRRdefType)
1110 *pdata = vcpu->arch.mtrr_state.def_type +
1111 (vcpu->arch.mtrr_state.enabled << 10);
1112 else if (msr == MSR_MTRRfix64K_00000)
1113 *pdata = p[0];
1114 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1115 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1116 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1117 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1118 else if (msr == MSR_IA32_CR_PAT)
1119 *pdata = vcpu->arch.pat;
1120 else { /* Variable MTRRs */
1121 int idx, is_mtrr_mask;
1122 u64 *pt;
1123
1124 idx = (msr - 0x200) / 2;
1125 is_mtrr_mask = msr - 0x200 - 2 * idx;
1126 if (!is_mtrr_mask)
1127 pt =
1128 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1129 else
1130 pt =
1131 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1132 *pdata = *pt;
1133 }
1134
Avi Kivity9ba075a2008-05-26 20:06:35 +03001135 return 0;
1136}
1137
Huang Ying890ca9a2009-05-11 16:48:15 +08001138static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1139{
1140 u64 data;
1141 u64 mcg_cap = vcpu->arch.mcg_cap;
1142 unsigned bank_num = mcg_cap & 0xff;
1143
1144 switch (msr) {
1145 case MSR_IA32_P5_MC_ADDR:
1146 case MSR_IA32_P5_MC_TYPE:
1147 data = 0;
1148 break;
1149 case MSR_IA32_MCG_CAP:
1150 data = vcpu->arch.mcg_cap;
1151 break;
1152 case MSR_IA32_MCG_CTL:
1153 if (!(mcg_cap & MCG_CTL_P))
1154 return 1;
1155 data = vcpu->arch.mcg_ctl;
1156 break;
1157 case MSR_IA32_MCG_STATUS:
1158 data = vcpu->arch.mcg_status;
1159 break;
1160 default:
1161 if (msr >= MSR_IA32_MC0_CTL &&
1162 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1163 u32 offset = msr - MSR_IA32_MC0_CTL;
1164 data = vcpu->arch.mce_banks[offset];
1165 break;
1166 }
1167 return 1;
1168 }
1169 *pdata = data;
1170 return 0;
1171}
1172
Carsten Otte15c4a642007-10-30 18:44:17 +01001173int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1174{
1175 u64 data;
1176
1177 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001178 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001179 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001180 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001181 case MSR_IA32_DEBUGCTLMSR:
1182 case MSR_IA32_LASTBRANCHFROMIP:
1183 case MSR_IA32_LASTBRANCHTOIP:
1184 case MSR_IA32_LASTINTFROMIP:
1185 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301186 case MSR_K8_SYSCFG:
1187 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001188 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301189 case MSR_P6_PERFCTR0:
1190 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301191 case MSR_P6_EVNTSEL0:
1192 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301193 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301194 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001195 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001196 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001197 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001198 data = 0;
1199 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001200 case MSR_MTRRcap:
1201 data = 0x500 | KVM_NR_VAR_MTRR;
1202 break;
1203 case 0x200 ... 0x2ff:
1204 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001205 case 0xcd: /* fsb frequency */
1206 data = 3;
1207 break;
1208 case MSR_IA32_APICBASE:
1209 data = kvm_get_apic_base(vcpu);
1210 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001211 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1212 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1213 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001214 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001215 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001216 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001217 case MSR_IA32_PERF_STATUS:
1218 /* TSC increment by tick */
1219 data = 1000ULL;
1220 /* CPU multiplier */
1221 data |= (((uint64_t)4ULL) << 40);
1222 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001223 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001224 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001225 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001226 case MSR_KVM_WALL_CLOCK:
1227 data = vcpu->kvm->arch.wall_clock;
1228 break;
1229 case MSR_KVM_SYSTEM_TIME:
1230 data = vcpu->arch.time;
1231 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001232 case MSR_IA32_P5_MC_ADDR:
1233 case MSR_IA32_P5_MC_TYPE:
1234 case MSR_IA32_MCG_CAP:
1235 case MSR_IA32_MCG_CTL:
1236 case MSR_IA32_MCG_STATUS:
1237 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1238 return get_msr_mce(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001239 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001240 if (!ignore_msrs) {
1241 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1242 return 1;
1243 } else {
1244 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1245 data = 0;
1246 }
1247 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001248 }
1249 *pdata = data;
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1253
Carsten Otte313a3dc2007-10-11 19:16:52 +02001254/*
1255 * Read or write a bunch of msrs. All parameters are kernel addresses.
1256 *
1257 * @return number of msrs set successfully.
1258 */
1259static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1260 struct kvm_msr_entry *entries,
1261 int (*do_msr)(struct kvm_vcpu *vcpu,
1262 unsigned index, u64 *data))
1263{
1264 int i;
1265
1266 vcpu_load(vcpu);
1267
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001268 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001269 for (i = 0; i < msrs->nmsrs; ++i)
1270 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1271 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001272 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001273
1274 vcpu_put(vcpu);
1275
1276 return i;
1277}
1278
1279/*
1280 * Read or write a bunch of msrs. Parameters are user addresses.
1281 *
1282 * @return number of msrs set successfully.
1283 */
1284static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1285 int (*do_msr)(struct kvm_vcpu *vcpu,
1286 unsigned index, u64 *data),
1287 int writeback)
1288{
1289 struct kvm_msrs msrs;
1290 struct kvm_msr_entry *entries;
1291 int r, n;
1292 unsigned size;
1293
1294 r = -EFAULT;
1295 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1296 goto out;
1297
1298 r = -E2BIG;
1299 if (msrs.nmsrs >= MAX_IO_MSRS)
1300 goto out;
1301
1302 r = -ENOMEM;
1303 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1304 entries = vmalloc(size);
1305 if (!entries)
1306 goto out;
1307
1308 r = -EFAULT;
1309 if (copy_from_user(entries, user_msrs->entries, size))
1310 goto out_free;
1311
1312 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1313 if (r < 0)
1314 goto out_free;
1315
1316 r = -EFAULT;
1317 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1318 goto out_free;
1319
1320 r = n;
1321
1322out_free:
1323 vfree(entries);
1324out:
1325 return r;
1326}
1327
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001328int kvm_dev_ioctl_check_extension(long ext)
1329{
1330 int r;
1331
1332 switch (ext) {
1333 case KVM_CAP_IRQCHIP:
1334 case KVM_CAP_HLT:
1335 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001336 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001337 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001338 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001339 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001340 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001341 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001342 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001343 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001344 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001345 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001346 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001347 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001348 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001349 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001350 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Ed Swierkffde22a2009-10-15 15:21:43 -07001351 case KVM_CAP_XEN_HVM:
Glauber Costaafbcf7a2009-10-16 15:28:36 -04001352 case KVM_CAP_ADJUST_CLOCK:
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001353 case KVM_CAP_VCPU_EVENTS:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001354 r = 1;
1355 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001356 case KVM_CAP_COALESCED_MMIO:
1357 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1358 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001359 case KVM_CAP_VAPIC:
1360 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1361 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001362 case KVM_CAP_NR_VCPUS:
1363 r = KVM_MAX_VCPUS;
1364 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001365 case KVM_CAP_NR_MEMSLOTS:
1366 r = KVM_MEMORY_SLOTS;
1367 break;
Marcelo Tosattia68a6a72009-10-01 19:28:39 -03001368 case KVM_CAP_PV_MMU: /* obsolete */
1369 r = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001370 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001371 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001372 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001373 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001374 case KVM_CAP_MCE:
1375 r = KVM_MAX_MCE_BANKS;
1376 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001377 default:
1378 r = 0;
1379 break;
1380 }
1381 return r;
1382
1383}
1384
Carsten Otte043405e2007-10-10 17:16:19 +02001385long kvm_arch_dev_ioctl(struct file *filp,
1386 unsigned int ioctl, unsigned long arg)
1387{
1388 void __user *argp = (void __user *)arg;
1389 long r;
1390
1391 switch (ioctl) {
1392 case KVM_GET_MSR_INDEX_LIST: {
1393 struct kvm_msr_list __user *user_msr_list = argp;
1394 struct kvm_msr_list msr_list;
1395 unsigned n;
1396
1397 r = -EFAULT;
1398 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1399 goto out;
1400 n = msr_list.nmsrs;
1401 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1402 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1403 goto out;
1404 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001405 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001406 goto out;
1407 r = -EFAULT;
1408 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1409 num_msrs_to_save * sizeof(u32)))
1410 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001411 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001412 &emulated_msrs,
1413 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1414 goto out;
1415 r = 0;
1416 break;
1417 }
Avi Kivity674eea02008-02-11 18:37:23 +02001418 case KVM_GET_SUPPORTED_CPUID: {
1419 struct kvm_cpuid2 __user *cpuid_arg = argp;
1420 struct kvm_cpuid2 cpuid;
1421
1422 r = -EFAULT;
1423 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1424 goto out;
1425 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001426 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001427 if (r)
1428 goto out;
1429
1430 r = -EFAULT;
1431 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1432 goto out;
1433 r = 0;
1434 break;
1435 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001436 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1437 u64 mce_cap;
1438
1439 mce_cap = KVM_MCE_CAP_SUPPORTED;
1440 r = -EFAULT;
1441 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1442 goto out;
1443 r = 0;
1444 break;
1445 }
Carsten Otte043405e2007-10-10 17:16:19 +02001446 default:
1447 r = -EINVAL;
1448 }
1449out:
1450 return r;
1451}
1452
Carsten Otte313a3dc2007-10-11 19:16:52 +02001453void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1454{
1455 kvm_x86_ops->vcpu_load(vcpu, cpu);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10001456 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1457 unsigned long khz = cpufreq_quick_get(cpu);
1458 if (!khz)
1459 khz = tsc_khz;
1460 per_cpu(cpu_tsc_khz, cpu) = khz;
1461 }
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001462 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001463}
1464
1465void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1466{
1467 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001468 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001469}
1470
Dan Kenigsberg07716712007-11-21 17:10:04 +02001471static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001472{
Avi Kivitye286e862009-05-03 18:50:55 +03001473 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001474
Avi Kivitye286e862009-05-03 18:50:55 +03001475 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001476 return efer & EFER_NX;
1477}
1478
1479static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1480{
1481 int i;
1482 struct kvm_cpuid_entry2 *e, *entry;
1483
Carsten Otte313a3dc2007-10-11 19:16:52 +02001484 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001485 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1486 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001487 if (e->function == 0x80000001) {
1488 entry = e;
1489 break;
1490 }
1491 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001492 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001493 entry->edx &= ~(1 << 20);
1494 printk(KERN_INFO "kvm: guest NX capability removed\n");
1495 }
1496}
1497
Dan Kenigsberg07716712007-11-21 17:10:04 +02001498/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001499static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1500 struct kvm_cpuid *cpuid,
1501 struct kvm_cpuid_entry __user *entries)
1502{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001503 int r, i;
1504 struct kvm_cpuid_entry *cpuid_entries;
1505
1506 r = -E2BIG;
1507 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1508 goto out;
1509 r = -ENOMEM;
1510 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1511 if (!cpuid_entries)
1512 goto out;
1513 r = -EFAULT;
1514 if (copy_from_user(cpuid_entries, entries,
1515 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1516 goto out_free;
1517 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001518 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1519 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1520 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1521 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1522 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1523 vcpu->arch.cpuid_entries[i].index = 0;
1524 vcpu->arch.cpuid_entries[i].flags = 0;
1525 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1526 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1527 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001528 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001529 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001530 cpuid_fix_nx_cap(vcpu);
1531 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001532 kvm_apic_set_version(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001533
1534out_free:
1535 vfree(cpuid_entries);
1536out:
1537 return r;
1538}
1539
1540static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001541 struct kvm_cpuid2 *cpuid,
1542 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001543{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001544 int r;
1545
1546 r = -E2BIG;
1547 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1548 goto out;
1549 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001550 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001551 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001552 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001553 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001554 kvm_apic_set_version(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001555 return 0;
1556
1557out:
1558 return r;
1559}
1560
Dan Kenigsberg07716712007-11-21 17:10:04 +02001561static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001562 struct kvm_cpuid2 *cpuid,
1563 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001564{
1565 int r;
1566
1567 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001568 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001569 goto out;
1570 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001571 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001572 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001573 goto out;
1574 return 0;
1575
1576out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001577 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001578 return r;
1579}
1580
Dan Kenigsberg07716712007-11-21 17:10:04 +02001581static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001582 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001583{
1584 entry->function = function;
1585 entry->index = index;
1586 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001587 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001588 entry->flags = 0;
1589}
1590
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001591#define F(x) bit(X86_FEATURE_##x)
1592
Dan Kenigsberg07716712007-11-21 17:10:04 +02001593static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1594 u32 index, int *nent, int maxnent)
1595{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001596 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Joerg Roedel344f4142009-07-27 16:30:48 +02001597 unsigned f_gbpages = kvm_x86_ops->gb_page_enable() ? F(GBPAGES) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001598#ifdef CONFIG_X86_64
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001599 unsigned f_lm = F(LM);
1600#else
1601 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001602#endif
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001603
1604 /* cpuid 1.edx */
1605 const u32 kvm_supported_word0_x86_features =
1606 F(FPU) | F(VME) | F(DE) | F(PSE) |
1607 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1608 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1609 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1610 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1611 0 /* Reserved, DS, ACPI */ | F(MMX) |
1612 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1613 0 /* HTT, TM, Reserved, PBE */;
1614 /* cpuid 0x80000001.edx */
1615 const u32 kvm_supported_word1_x86_features =
1616 F(FPU) | F(VME) | F(DE) | F(PSE) |
1617 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1618 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1619 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1620 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1621 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Joerg Roedel344f4142009-07-27 16:30:48 +02001622 F(FXSR) | F(FXSR_OPT) | f_gbpages | 0 /* RDTSCP */ |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001623 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1624 /* cpuid 1.ecx */
1625 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001626 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1627 0 /* DS-CPL, VMX, SMX, EST */ |
1628 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1629 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1630 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001631 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001632 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001633 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001634 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001635 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1636 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1637 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1638 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001639
Amit Shah19355472009-01-14 16:56:00 +00001640 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001641 get_cpu();
1642 do_cpuid_1_ent(entry, function, index);
1643 ++*nent;
1644
1645 switch (function) {
1646 case 0:
1647 entry->eax = min(entry->eax, (u32)0xb);
1648 break;
1649 case 1:
1650 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001651 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001652 /* we support x2apic emulation even if host does not support
1653 * it since we emulate x2apic in software */
1654 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001655 break;
1656 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1657 * may return different values. This forces us to get_cpu() before
1658 * issuing the first command, and also to emulate this annoying behavior
1659 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1660 case 2: {
1661 int t, times = entry->eax & 0xff;
1662
1663 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001664 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001665 for (t = 1; t < times && *nent < maxnent; ++t) {
1666 do_cpuid_1_ent(&entry[t], function, 0);
1667 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1668 ++*nent;
1669 }
1670 break;
1671 }
1672 /* function 4 and 0xb have additional index. */
1673 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001674 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001675
1676 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1677 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001678 for (i = 1; *nent < maxnent; ++i) {
1679 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001680 if (!cache_type)
1681 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001682 do_cpuid_1_ent(&entry[i], function, i);
1683 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001684 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1685 ++*nent;
1686 }
1687 break;
1688 }
1689 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001690 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001691
1692 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1693 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001694 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001695 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001696 if (!level_type)
1697 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001698 do_cpuid_1_ent(&entry[i], function, i);
1699 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001700 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1701 ++*nent;
1702 }
1703 break;
1704 }
1705 case 0x80000000:
1706 entry->eax = min(entry->eax, 0x8000001a);
1707 break;
1708 case 0x80000001:
1709 entry->edx &= kvm_supported_word1_x86_features;
1710 entry->ecx &= kvm_supported_word6_x86_features;
1711 break;
1712 }
1713 put_cpu();
1714}
1715
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001716#undef F
1717
Avi Kivity674eea02008-02-11 18:37:23 +02001718static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001719 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001720{
1721 struct kvm_cpuid_entry2 *cpuid_entries;
1722 int limit, nent = 0, r = -E2BIG;
1723 u32 func;
1724
1725 if (cpuid->nent < 1)
1726 goto out;
Avi Kivity6a544352009-10-04 16:45:13 +02001727 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1728 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001729 r = -ENOMEM;
1730 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1731 if (!cpuid_entries)
1732 goto out;
1733
1734 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1735 limit = cpuid_entries[0].eax;
1736 for (func = 1; 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);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001739 r = -E2BIG;
1740 if (nent >= cpuid->nent)
1741 goto out_free;
1742
1743 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1744 limit = cpuid_entries[nent - 1].eax;
1745 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1746 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001747 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001748 r = -E2BIG;
1749 if (nent >= cpuid->nent)
1750 goto out_free;
1751
Dan Kenigsberg07716712007-11-21 17:10:04 +02001752 r = -EFAULT;
1753 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001754 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001755 goto out_free;
1756 cpuid->nent = nent;
1757 r = 0;
1758
1759out_free:
1760 vfree(cpuid_entries);
1761out:
1762 return r;
1763}
1764
Carsten Otte313a3dc2007-10-11 19:16:52 +02001765static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1766 struct kvm_lapic_state *s)
1767{
1768 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001769 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001770 vcpu_put(vcpu);
1771
1772 return 0;
1773}
1774
1775static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1776 struct kvm_lapic_state *s)
1777{
1778 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001779 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001780 kvm_apic_post_state_restore(vcpu);
Gleb Natapovcb142eb2009-08-09 15:17:40 +03001781 update_cr8_intercept(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001782 vcpu_put(vcpu);
1783
1784 return 0;
1785}
1786
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001787static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1788 struct kvm_interrupt *irq)
1789{
1790 if (irq->irq < 0 || irq->irq >= 256)
1791 return -EINVAL;
1792 if (irqchip_in_kernel(vcpu->kvm))
1793 return -ENXIO;
1794 vcpu_load(vcpu);
1795
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001796 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001797
1798 vcpu_put(vcpu);
1799
1800 return 0;
1801}
1802
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001803static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1804{
1805 vcpu_load(vcpu);
1806 kvm_inject_nmi(vcpu);
1807 vcpu_put(vcpu);
1808
1809 return 0;
1810}
1811
Avi Kivityb209749f2007-10-22 16:50:39 +02001812static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1813 struct kvm_tpr_access_ctl *tac)
1814{
1815 if (tac->flags)
1816 return -EINVAL;
1817 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1818 return 0;
1819}
1820
Huang Ying890ca9a2009-05-11 16:48:15 +08001821static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1822 u64 mcg_cap)
1823{
1824 int r;
1825 unsigned bank_num = mcg_cap & 0xff, bank;
1826
1827 r = -EINVAL;
Jan Kiszkaa9e38c3e2009-10-23 09:37:00 +02001828 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
Huang Ying890ca9a2009-05-11 16:48:15 +08001829 goto out;
1830 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1831 goto out;
1832 r = 0;
1833 vcpu->arch.mcg_cap = mcg_cap;
1834 /* Init IA32_MCG_CTL to all 1s */
1835 if (mcg_cap & MCG_CTL_P)
1836 vcpu->arch.mcg_ctl = ~(u64)0;
1837 /* Init IA32_MCi_CTL to all 1s */
1838 for (bank = 0; bank < bank_num; bank++)
1839 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1840out:
1841 return r;
1842}
1843
1844static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1845 struct kvm_x86_mce *mce)
1846{
1847 u64 mcg_cap = vcpu->arch.mcg_cap;
1848 unsigned bank_num = mcg_cap & 0xff;
1849 u64 *banks = vcpu->arch.mce_banks;
1850
1851 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1852 return -EINVAL;
1853 /*
1854 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1855 * reporting is disabled
1856 */
1857 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1858 vcpu->arch.mcg_ctl != ~(u64)0)
1859 return 0;
1860 banks += 4 * mce->bank;
1861 /*
1862 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1863 * reporting is disabled for the bank
1864 */
1865 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1866 return 0;
1867 if (mce->status & MCI_STATUS_UC) {
1868 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1869 !(vcpu->arch.cr4 & X86_CR4_MCE)) {
1870 printk(KERN_DEBUG "kvm: set_mce: "
1871 "injects mce exception while "
1872 "previous one is in progress!\n");
1873 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1874 return 0;
1875 }
1876 if (banks[1] & MCI_STATUS_VAL)
1877 mce->status |= MCI_STATUS_OVER;
1878 banks[2] = mce->addr;
1879 banks[3] = mce->misc;
1880 vcpu->arch.mcg_status = mce->mcg_status;
1881 banks[1] = mce->status;
1882 kvm_queue_exception(vcpu, MC_VECTOR);
1883 } else if (!(banks[1] & MCI_STATUS_VAL)
1884 || !(banks[1] & MCI_STATUS_UC)) {
1885 if (banks[1] & MCI_STATUS_VAL)
1886 mce->status |= MCI_STATUS_OVER;
1887 banks[2] = mce->addr;
1888 banks[3] = mce->misc;
1889 banks[1] = mce->status;
1890 } else
1891 banks[1] |= MCI_STATUS_OVER;
1892 return 0;
1893}
1894
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001895static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
1896 struct kvm_vcpu_events *events)
1897{
1898 vcpu_load(vcpu);
1899
1900 events->exception.injected = vcpu->arch.exception.pending;
1901 events->exception.nr = vcpu->arch.exception.nr;
1902 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
1903 events->exception.error_code = vcpu->arch.exception.error_code;
1904
1905 events->interrupt.injected = vcpu->arch.interrupt.pending;
1906 events->interrupt.nr = vcpu->arch.interrupt.nr;
1907 events->interrupt.soft = vcpu->arch.interrupt.soft;
1908
1909 events->nmi.injected = vcpu->arch.nmi_injected;
1910 events->nmi.pending = vcpu->arch.nmi_pending;
1911 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
1912
1913 events->sipi_vector = vcpu->arch.sipi_vector;
1914
Jan Kiszkadab4b912009-12-06 18:24:15 +01001915 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
1916 | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001917
1918 vcpu_put(vcpu);
1919}
1920
1921static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
1922 struct kvm_vcpu_events *events)
1923{
Jan Kiszkadab4b912009-12-06 18:24:15 +01001924 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
1925 | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001926 return -EINVAL;
1927
1928 vcpu_load(vcpu);
1929
1930 vcpu->arch.exception.pending = events->exception.injected;
1931 vcpu->arch.exception.nr = events->exception.nr;
1932 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
1933 vcpu->arch.exception.error_code = events->exception.error_code;
1934
1935 vcpu->arch.interrupt.pending = events->interrupt.injected;
1936 vcpu->arch.interrupt.nr = events->interrupt.nr;
1937 vcpu->arch.interrupt.soft = events->interrupt.soft;
1938 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
1939 kvm_pic_clear_isr_ack(vcpu->kvm);
1940
1941 vcpu->arch.nmi_injected = events->nmi.injected;
Jan Kiszkadab4b912009-12-06 18:24:15 +01001942 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
1943 vcpu->arch.nmi_pending = events->nmi.pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001944 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
1945
Jan Kiszkadab4b912009-12-06 18:24:15 +01001946 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
1947 vcpu->arch.sipi_vector = events->sipi_vector;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01001948
1949 vcpu_put(vcpu);
1950
1951 return 0;
1952}
1953
Carsten Otte313a3dc2007-10-11 19:16:52 +02001954long kvm_arch_vcpu_ioctl(struct file *filp,
1955 unsigned int ioctl, unsigned long arg)
1956{
1957 struct kvm_vcpu *vcpu = filp->private_data;
1958 void __user *argp = (void __user *)arg;
1959 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001960 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001961
1962 switch (ioctl) {
1963 case KVM_GET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02001964 r = -EINVAL;
1965 if (!vcpu->arch.apic)
1966 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001967 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001968
Dave Hansenb772ff32008-08-11 10:01:47 -07001969 r = -ENOMEM;
1970 if (!lapic)
1971 goto out;
1972 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001973 if (r)
1974 goto out;
1975 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001976 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001977 goto out;
1978 r = 0;
1979 break;
1980 }
1981 case KVM_SET_LAPIC: {
Marcelo Tosatti2204ae32009-10-29 13:44:16 -02001982 r = -EINVAL;
1983 if (!vcpu->arch.apic)
1984 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001985 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1986 r = -ENOMEM;
1987 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001988 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001989 r = -EFAULT;
1990 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1991 goto out;
1992 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001993 if (r)
1994 goto out;
1995 r = 0;
1996 break;
1997 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001998 case KVM_INTERRUPT: {
1999 struct kvm_interrupt irq;
2000
2001 r = -EFAULT;
2002 if (copy_from_user(&irq, argp, sizeof irq))
2003 goto out;
2004 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2005 if (r)
2006 goto out;
2007 r = 0;
2008 break;
2009 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002010 case KVM_NMI: {
2011 r = kvm_vcpu_ioctl_nmi(vcpu);
2012 if (r)
2013 goto out;
2014 r = 0;
2015 break;
2016 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002017 case KVM_SET_CPUID: {
2018 struct kvm_cpuid __user *cpuid_arg = argp;
2019 struct kvm_cpuid cpuid;
2020
2021 r = -EFAULT;
2022 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2023 goto out;
2024 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2025 if (r)
2026 goto out;
2027 break;
2028 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02002029 case KVM_SET_CPUID2: {
2030 struct kvm_cpuid2 __user *cpuid_arg = argp;
2031 struct kvm_cpuid2 cpuid;
2032
2033 r = -EFAULT;
2034 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2035 goto out;
2036 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002037 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002038 if (r)
2039 goto out;
2040 break;
2041 }
2042 case KVM_GET_CPUID2: {
2043 struct kvm_cpuid2 __user *cpuid_arg = argp;
2044 struct kvm_cpuid2 cpuid;
2045
2046 r = -EFAULT;
2047 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2048 goto out;
2049 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00002050 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02002051 if (r)
2052 goto out;
2053 r = -EFAULT;
2054 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2055 goto out;
2056 r = 0;
2057 break;
2058 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002059 case KVM_GET_MSRS:
2060 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2061 break;
2062 case KVM_SET_MSRS:
2063 r = msr_io(vcpu, argp, do_set_msr, 0);
2064 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02002065 case KVM_TPR_ACCESS_REPORTING: {
2066 struct kvm_tpr_access_ctl tac;
2067
2068 r = -EFAULT;
2069 if (copy_from_user(&tac, argp, sizeof tac))
2070 goto out;
2071 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2072 if (r)
2073 goto out;
2074 r = -EFAULT;
2075 if (copy_to_user(argp, &tac, sizeof tac))
2076 goto out;
2077 r = 0;
2078 break;
2079 };
Avi Kivityb93463a2007-10-25 16:52:32 +02002080 case KVM_SET_VAPIC_ADDR: {
2081 struct kvm_vapic_addr va;
2082
2083 r = -EINVAL;
2084 if (!irqchip_in_kernel(vcpu->kvm))
2085 goto out;
2086 r = -EFAULT;
2087 if (copy_from_user(&va, argp, sizeof va))
2088 goto out;
2089 r = 0;
2090 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2091 break;
2092 }
Huang Ying890ca9a2009-05-11 16:48:15 +08002093 case KVM_X86_SETUP_MCE: {
2094 u64 mcg_cap;
2095
2096 r = -EFAULT;
2097 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2098 goto out;
2099 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2100 break;
2101 }
2102 case KVM_X86_SET_MCE: {
2103 struct kvm_x86_mce mce;
2104
2105 r = -EFAULT;
2106 if (copy_from_user(&mce, argp, sizeof mce))
2107 goto out;
2108 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2109 break;
2110 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002111 case KVM_GET_VCPU_EVENTS: {
2112 struct kvm_vcpu_events events;
2113
2114 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2115
2116 r = -EFAULT;
2117 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2118 break;
2119 r = 0;
2120 break;
2121 }
2122 case KVM_SET_VCPU_EVENTS: {
2123 struct kvm_vcpu_events events;
2124
2125 r = -EFAULT;
2126 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2127 break;
2128
2129 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2130 break;
2131 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02002132 default:
2133 r = -EINVAL;
2134 }
2135out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08002136 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02002137 return r;
2138}
2139
Carsten Otte1fe779f2007-10-29 16:08:35 +01002140static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2141{
2142 int ret;
2143
2144 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2145 return -1;
2146 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2147 return ret;
2148}
2149
Sheng Yangb927a3c2009-07-21 10:42:48 +08002150static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2151 u64 ident_addr)
2152{
2153 kvm->arch.ept_identity_map_addr = ident_addr;
2154 return 0;
2155}
2156
Carsten Otte1fe779f2007-10-29 16:08:35 +01002157static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2158 u32 kvm_nr_mmu_pages)
2159{
2160 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2161 return -EINVAL;
2162
Izik Eidus72dc67a2008-02-10 18:04:15 +02002163 down_write(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002164 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002165
2166 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002167 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002168
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002169 spin_unlock(&kvm->mmu_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002170 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002171 return 0;
2172}
2173
2174static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2175{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002176 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002177}
2178
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002179gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2180{
2181 int i;
2182 struct kvm_mem_alias *alias;
2183
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002184 for (i = 0; i < kvm->arch.naliases; ++i) {
2185 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08002186 if (gfn >= alias->base_gfn
2187 && gfn < alias->base_gfn + alias->npages)
2188 return alias->target_gfn + gfn - alias->base_gfn;
2189 }
2190 return gfn;
2191}
2192
Carsten Otte1fe779f2007-10-29 16:08:35 +01002193/*
2194 * Set a new alias region. Aliases map a portion of physical memory into
2195 * another portion. This is useful for memory windows, for example the PC
2196 * VGA region.
2197 */
2198static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2199 struct kvm_memory_alias *alias)
2200{
2201 int r, n;
2202 struct kvm_mem_alias *p;
2203
2204 r = -EINVAL;
2205 /* General sanity checks */
2206 if (alias->memory_size & (PAGE_SIZE - 1))
2207 goto out;
2208 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2209 goto out;
2210 if (alias->slot >= KVM_ALIAS_SLOTS)
2211 goto out;
2212 if (alias->guest_phys_addr + alias->memory_size
2213 < alias->guest_phys_addr)
2214 goto out;
2215 if (alias->target_phys_addr + alias->memory_size
2216 < alias->target_phys_addr)
2217 goto out;
2218
Izik Eidus72dc67a2008-02-10 18:04:15 +02002219 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02002220 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002221
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002222 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01002223 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2224 p->npages = alias->memory_size >> PAGE_SHIFT;
2225 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
2226
2227 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002228 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002229 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08002230 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002231
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02002232 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002233 kvm_mmu_zap_all(kvm);
2234
Izik Eidus72dc67a2008-02-10 18:04:15 +02002235 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002236
2237 return 0;
2238
2239out:
2240 return r;
2241}
2242
2243static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2244{
2245 int r;
2246
2247 r = 0;
2248 switch (chip->chip_id) {
2249 case KVM_IRQCHIP_PIC_MASTER:
2250 memcpy(&chip->chip.pic,
2251 &pic_irqchip(kvm)->pics[0],
2252 sizeof(struct kvm_pic_state));
2253 break;
2254 case KVM_IRQCHIP_PIC_SLAVE:
2255 memcpy(&chip->chip.pic,
2256 &pic_irqchip(kvm)->pics[1],
2257 sizeof(struct kvm_pic_state));
2258 break;
2259 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002260 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002261 break;
2262 default:
2263 r = -EINVAL;
2264 break;
2265 }
2266 return r;
2267}
2268
2269static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2270{
2271 int r;
2272
2273 r = 0;
2274 switch (chip->chip_id) {
2275 case KVM_IRQCHIP_PIC_MASTER:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002276 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002277 memcpy(&pic_irqchip(kvm)->pics[0],
2278 &chip->chip.pic,
2279 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002280 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002281 break;
2282 case KVM_IRQCHIP_PIC_SLAVE:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002283 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002284 memcpy(&pic_irqchip(kvm)->pics[1],
2285 &chip->chip.pic,
2286 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002287 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002288 break;
2289 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +03002290 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002291 break;
2292 default:
2293 r = -EINVAL;
2294 break;
2295 }
2296 kvm_pic_update_irq(pic_irqchip(kvm));
2297 return r;
2298}
2299
Sheng Yange0f63cb2008-03-04 00:50:59 +08002300static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2301{
2302 int r = 0;
2303
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002304 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002305 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002306 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002307 return r;
2308}
2309
2310static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2311{
2312 int r = 0;
2313
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002314 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002315 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002316 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2317 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2318 return r;
2319}
2320
2321static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2322{
2323 int r = 0;
2324
2325 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2326 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2327 sizeof(ps->channels));
2328 ps->flags = kvm->arch.vpit->pit_state.flags;
2329 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2330 return r;
2331}
2332
2333static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2334{
2335 int r = 0, start = 0;
2336 u32 prev_legacy, cur_legacy;
2337 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2338 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2339 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2340 if (!prev_legacy && cur_legacy)
2341 start = 1;
2342 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2343 sizeof(kvm->arch.vpit->pit_state.channels));
2344 kvm->arch.vpit->pit_state.flags = ps->flags;
2345 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002346 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002347 return r;
2348}
2349
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002350static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2351 struct kvm_reinject_control *control)
2352{
2353 if (!kvm->arch.vpit)
2354 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002355 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002356 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002357 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002358 return 0;
2359}
2360
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002361/*
2362 * Get (and clear) the dirty memory log for a memory slot.
2363 */
2364int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2365 struct kvm_dirty_log *log)
2366{
2367 int r;
2368 int n;
2369 struct kvm_memory_slot *memslot;
2370 int is_dirty = 0;
2371
Izik Eidus72dc67a2008-02-10 18:04:15 +02002372 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002373
2374 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2375 if (r)
2376 goto out;
2377
2378 /* If nothing is dirty, don't bother messing with page tables. */
2379 if (is_dirty) {
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002380 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002381 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002382 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002383 memslot = &kvm->memslots[log->slot];
2384 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2385 memset(memslot->dirty_bitmap, 0, n);
2386 }
2387 r = 0;
2388out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002389 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002390 return r;
2391}
2392
Carsten Otte1fe779f2007-10-29 16:08:35 +01002393long kvm_arch_vm_ioctl(struct file *filp,
2394 unsigned int ioctl, unsigned long arg)
2395{
2396 struct kvm *kvm = filp->private_data;
2397 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +03002398 int r = -ENOTTY;
Dave Hansenf0d66272008-08-11 10:01:45 -07002399 /*
2400 * This union makes it completely explicit to gcc-3.x
2401 * that these two variables' stack usage should be
2402 * combined, not added together.
2403 */
2404 union {
2405 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002406 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002407 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002408 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002409 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002410
2411 switch (ioctl) {
2412 case KVM_SET_TSS_ADDR:
2413 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2414 if (r < 0)
2415 goto out;
2416 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002417 case KVM_SET_IDENTITY_MAP_ADDR: {
2418 u64 ident_addr;
2419
2420 r = -EFAULT;
2421 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2422 goto out;
2423 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2424 if (r < 0)
2425 goto out;
2426 break;
2427 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002428 case KVM_SET_MEMORY_REGION: {
2429 struct kvm_memory_region kvm_mem;
2430 struct kvm_userspace_memory_region kvm_userspace_mem;
2431
2432 r = -EFAULT;
2433 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2434 goto out;
2435 kvm_userspace_mem.slot = kvm_mem.slot;
2436 kvm_userspace_mem.flags = kvm_mem.flags;
2437 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2438 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2439 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2440 if (r)
2441 goto out;
2442 break;
2443 }
2444 case KVM_SET_NR_MMU_PAGES:
2445 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2446 if (r)
2447 goto out;
2448 break;
2449 case KVM_GET_NR_MMU_PAGES:
2450 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2451 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002452 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002453 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002454 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002455 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002456 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002457 if (r)
2458 goto out;
2459 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002460 case KVM_CREATE_IRQCHIP: {
2461 struct kvm_pic *vpic;
2462
2463 mutex_lock(&kvm->lock);
2464 r = -EEXIST;
2465 if (kvm->arch.vpic)
2466 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002467 r = -ENOMEM;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002468 vpic = kvm_create_pic(kvm);
2469 if (vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002470 r = kvm_ioapic_init(kvm);
2471 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002472 kfree(vpic);
2473 goto create_irqchip_unlock;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002474 }
2475 } else
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002476 goto create_irqchip_unlock;
2477 smp_wmb();
2478 kvm->arch.vpic = vpic;
2479 smp_wmb();
Avi Kivity399ec802008-11-19 13:58:46 +02002480 r = kvm_setup_default_irq_routing(kvm);
2481 if (r) {
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002482 mutex_lock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002483 kfree(kvm->arch.vpic);
2484 kfree(kvm->arch.vioapic);
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002485 kvm->arch.vpic = NULL;
2486 kvm->arch.vioapic = NULL;
2487 mutex_unlock(&kvm->irq_lock);
Avi Kivity399ec802008-11-19 13:58:46 +02002488 }
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002489 create_irqchip_unlock:
2490 mutex_unlock(&kvm->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002491 break;
Marcelo Tosatti3ddea122009-10-29 13:44:15 -02002492 }
Sheng Yang78376992008-01-28 05:10:22 +08002493 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002494 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2495 goto create_pit;
2496 case KVM_CREATE_PIT2:
2497 r = -EFAULT;
2498 if (copy_from_user(&u.pit_config, argp,
2499 sizeof(struct kvm_pit_config)))
2500 goto out;
2501 create_pit:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002502 down_write(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002503 r = -EEXIST;
2504 if (kvm->arch.vpit)
2505 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002506 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002507 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002508 if (kvm->arch.vpit)
2509 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002510 create_pit_unlock:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002511 up_write(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002512 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002513 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002514 case KVM_IRQ_LINE: {
2515 struct kvm_irq_level irq_event;
2516
2517 r = -EFAULT;
2518 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2519 goto out;
2520 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002521 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +02002522 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2523 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +02002524 if (ioctl == KVM_IRQ_LINE_STATUS) {
2525 irq_event.status = status;
2526 if (copy_to_user(argp, &irq_event,
2527 sizeof irq_event))
2528 goto out;
2529 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002530 r = 0;
2531 }
2532 break;
2533 }
2534 case KVM_GET_IRQCHIP: {
2535 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002536 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002537
Dave Hansenf0d66272008-08-11 10:01:45 -07002538 r = -ENOMEM;
2539 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002540 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002541 r = -EFAULT;
2542 if (copy_from_user(chip, argp, sizeof *chip))
2543 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002544 r = -ENXIO;
2545 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002546 goto get_irqchip_out;
2547 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2548 if (r)
2549 goto get_irqchip_out;
2550 r = -EFAULT;
2551 if (copy_to_user(argp, chip, sizeof *chip))
2552 goto get_irqchip_out;
2553 r = 0;
2554 get_irqchip_out:
2555 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002556 if (r)
2557 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002558 break;
2559 }
2560 case KVM_SET_IRQCHIP: {
2561 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002562 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002563
Dave Hansenf0d66272008-08-11 10:01:45 -07002564 r = -ENOMEM;
2565 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002566 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002567 r = -EFAULT;
2568 if (copy_from_user(chip, argp, sizeof *chip))
2569 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002570 r = -ENXIO;
2571 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002572 goto set_irqchip_out;
2573 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2574 if (r)
2575 goto set_irqchip_out;
2576 r = 0;
2577 set_irqchip_out:
2578 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002579 if (r)
2580 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002581 break;
2582 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002583 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002584 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002585 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002586 goto out;
2587 r = -ENXIO;
2588 if (!kvm->arch.vpit)
2589 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002590 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002591 if (r)
2592 goto out;
2593 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002594 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002595 goto out;
2596 r = 0;
2597 break;
2598 }
2599 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002600 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002601 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002602 goto out;
2603 r = -ENXIO;
2604 if (!kvm->arch.vpit)
2605 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002606 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002607 if (r)
2608 goto out;
2609 r = 0;
2610 break;
2611 }
Beth Kone9f42752009-07-07 11:50:38 -04002612 case KVM_GET_PIT2: {
2613 r = -ENXIO;
2614 if (!kvm->arch.vpit)
2615 goto out;
2616 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2617 if (r)
2618 goto out;
2619 r = -EFAULT;
2620 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2621 goto out;
2622 r = 0;
2623 break;
2624 }
2625 case KVM_SET_PIT2: {
2626 r = -EFAULT;
2627 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2628 goto out;
2629 r = -ENXIO;
2630 if (!kvm->arch.vpit)
2631 goto out;
2632 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2633 if (r)
2634 goto out;
2635 r = 0;
2636 break;
2637 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002638 case KVM_REINJECT_CONTROL: {
2639 struct kvm_reinject_control control;
2640 r = -EFAULT;
2641 if (copy_from_user(&control, argp, sizeof(control)))
2642 goto out;
2643 r = kvm_vm_ioctl_reinject(kvm, &control);
2644 if (r)
2645 goto out;
2646 r = 0;
2647 break;
2648 }
Ed Swierkffde22a2009-10-15 15:21:43 -07002649 case KVM_XEN_HVM_CONFIG: {
2650 r = -EFAULT;
2651 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2652 sizeof(struct kvm_xen_hvm_config)))
2653 goto out;
2654 r = -EINVAL;
2655 if (kvm->arch.xen_hvm_config.flags)
2656 goto out;
2657 r = 0;
2658 break;
2659 }
Glauber Costaafbcf7a2009-10-16 15:28:36 -04002660 case KVM_SET_CLOCK: {
2661 struct timespec now;
2662 struct kvm_clock_data user_ns;
2663 u64 now_ns;
2664 s64 delta;
2665
2666 r = -EFAULT;
2667 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2668 goto out;
2669
2670 r = -EINVAL;
2671 if (user_ns.flags)
2672 goto out;
2673
2674 r = 0;
2675 ktime_get_ts(&now);
2676 now_ns = timespec_to_ns(&now);
2677 delta = user_ns.clock - now_ns;
2678 kvm->arch.kvmclock_offset = delta;
2679 break;
2680 }
2681 case KVM_GET_CLOCK: {
2682 struct timespec now;
2683 struct kvm_clock_data user_ns;
2684 u64 now_ns;
2685
2686 ktime_get_ts(&now);
2687 now_ns = timespec_to_ns(&now);
2688 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2689 user_ns.flags = 0;
2690
2691 r = -EFAULT;
2692 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2693 goto out;
2694 r = 0;
2695 break;
2696 }
2697
Carsten Otte1fe779f2007-10-29 16:08:35 +01002698 default:
2699 ;
2700 }
2701out:
2702 return r;
2703}
2704
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002705static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002706{
2707 u32 dummy[2];
2708 unsigned i, j;
2709
Glauber Costae3267cb2009-10-06 13:24:50 -04002710 /* skip the first msrs in the list. KVM-specific */
2711 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
Carsten Otte043405e2007-10-10 17:16:19 +02002712 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2713 continue;
2714 if (j < i)
2715 msrs_to_save[j] = msrs_to_save[i];
2716 j++;
2717 }
2718 num_msrs_to_save = j;
2719}
2720
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002721static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2722 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002723{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002724 if (vcpu->arch.apic &&
2725 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2726 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002727
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002728 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002729}
2730
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002731static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002732{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002733 if (vcpu->arch.apic &&
2734 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2735 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002736
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002737 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002738}
2739
Hannes Edercded19f2009-02-21 02:19:13 +01002740static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2741 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002742{
2743 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002744 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002745
2746 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002747 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002748 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02002749 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002750 int ret;
2751
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002752 if (gpa == UNMAPPED_GVA) {
2753 r = X86EMUL_PROPAGATE_FAULT;
2754 goto out;
2755 }
Izik Eidus77c20022008-12-29 01:42:19 +02002756 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002757 if (ret < 0) {
2758 r = X86EMUL_UNHANDLEABLE;
2759 goto out;
2760 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002761
Izik Eidus77c20022008-12-29 01:42:19 +02002762 bytes -= toread;
2763 data += toread;
2764 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002765 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002766out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002767 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002768}
Izik Eidus77c20022008-12-29 01:42:19 +02002769
Hannes Edercded19f2009-02-21 02:19:13 +01002770static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2771 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02002772{
2773 void *data = val;
2774 int r = X86EMUL_CONTINUE;
2775
2776 while (bytes) {
2777 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2778 unsigned offset = addr & (PAGE_SIZE-1);
2779 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2780 int ret;
2781
2782 if (gpa == UNMAPPED_GVA) {
2783 r = X86EMUL_PROPAGATE_FAULT;
2784 goto out;
2785 }
2786 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2787 if (ret < 0) {
2788 r = X86EMUL_UNHANDLEABLE;
2789 goto out;
2790 }
2791
2792 bytes -= towrite;
2793 data += towrite;
2794 addr += towrite;
2795 }
2796out:
2797 return r;
2798}
2799
Carsten Ottebbd9b642007-10-30 18:44:21 +01002800
Carsten Ottebbd9b642007-10-30 18:44:21 +01002801static int emulator_read_emulated(unsigned long addr,
2802 void *val,
2803 unsigned int bytes,
2804 struct kvm_vcpu *vcpu)
2805{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002806 gpa_t gpa;
2807
2808 if (vcpu->mmio_read_completed) {
2809 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03002810 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
2811 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002812 vcpu->mmio_read_completed = 0;
2813 return X86EMUL_CONTINUE;
2814 }
2815
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002816 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002817
2818 /* For APIC access vmexit */
2819 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2820 goto mmio;
2821
Izik Eidus77c20022008-12-29 01:42:19 +02002822 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2823 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002824 return X86EMUL_CONTINUE;
2825 if (gpa == UNMAPPED_GVA)
2826 return X86EMUL_PROPAGATE_FAULT;
2827
2828mmio:
2829 /*
2830 * Is this MMIO handled locally?
2831 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03002832 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
2833 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002834 return X86EMUL_CONTINUE;
2835 }
Avi Kivityaec51dc2009-07-01 16:01:02 +03002836
2837 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002838
2839 vcpu->mmio_needed = 1;
2840 vcpu->mmio_phys_addr = gpa;
2841 vcpu->mmio_size = bytes;
2842 vcpu->mmio_is_write = 0;
2843
2844 return X86EMUL_UNHANDLEABLE;
2845}
2846
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002847int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002848 const void *val, int bytes)
2849{
2850 int ret;
2851
2852 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2853 if (ret < 0)
2854 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002855 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002856 return 1;
2857}
2858
Carsten Ottebbd9b642007-10-30 18:44:21 +01002859static int emulator_write_emulated_onepage(unsigned long addr,
2860 const void *val,
2861 unsigned int bytes,
2862 struct kvm_vcpu *vcpu)
2863{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002864 gpa_t gpa;
2865
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002866 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002867
2868 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002869 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002870 return X86EMUL_PROPAGATE_FAULT;
2871 }
2872
2873 /* For APIC access vmexit */
2874 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2875 goto mmio;
2876
2877 if (emulator_write_phys(vcpu, gpa, val, bytes))
2878 return X86EMUL_CONTINUE;
2879
2880mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03002881 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002882 /*
2883 * Is this MMIO handled locally?
2884 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002885 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002886 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002887
2888 vcpu->mmio_needed = 1;
2889 vcpu->mmio_phys_addr = gpa;
2890 vcpu->mmio_size = bytes;
2891 vcpu->mmio_is_write = 1;
2892 memcpy(vcpu->mmio_data, val, bytes);
2893
2894 return X86EMUL_CONTINUE;
2895}
2896
2897int emulator_write_emulated(unsigned long addr,
2898 const void *val,
2899 unsigned int bytes,
2900 struct kvm_vcpu *vcpu)
2901{
2902 /* Crossing a page boundary? */
2903 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2904 int rc, now;
2905
2906 now = -addr & ~PAGE_MASK;
2907 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2908 if (rc != X86EMUL_CONTINUE)
2909 return rc;
2910 addr += now;
2911 val += now;
2912 bytes -= now;
2913 }
2914 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2915}
2916EXPORT_SYMBOL_GPL(emulator_write_emulated);
2917
2918static int emulator_cmpxchg_emulated(unsigned long addr,
2919 const void *old,
2920 const void *new,
2921 unsigned int bytes,
2922 struct kvm_vcpu *vcpu)
2923{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02002924 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002925#ifndef CONFIG_X86_64
2926 /* guests cmpxchg8b have to be emulated atomically */
2927 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002928 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002929 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002930 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002931 u64 val;
2932
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002933 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2934
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002935 if (gpa == UNMAPPED_GVA ||
2936 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2937 goto emul_write;
2938
2939 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2940 goto emul_write;
2941
2942 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002943
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002944 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002945
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002946 kaddr = kmap_atomic(page, KM_USER0);
2947 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2948 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002949 kvm_release_page_dirty(page);
2950 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002951emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002952#endif
2953
Carsten Ottebbd9b642007-10-30 18:44:21 +01002954 return emulator_write_emulated(addr, new, bytes, vcpu);
2955}
2956
2957static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2958{
2959 return kvm_x86_ops->get_segment_base(vcpu, seg);
2960}
2961
2962int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2963{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002964 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002965 return X86EMUL_CONTINUE;
2966}
2967
2968int emulate_clts(struct kvm_vcpu *vcpu)
2969{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002970 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002971 return X86EMUL_CONTINUE;
2972}
2973
2974int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2975{
2976 struct kvm_vcpu *vcpu = ctxt->vcpu;
2977
2978 switch (dr) {
2979 case 0 ... 3:
2980 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2981 return X86EMUL_CONTINUE;
2982 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002983 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002984 return X86EMUL_UNHANDLEABLE;
2985 }
2986}
2987
2988int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2989{
2990 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2991 int exception;
2992
2993 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2994 if (exception) {
2995 /* FIXME: better handling */
2996 return X86EMUL_UNHANDLEABLE;
2997 }
2998 return X86EMUL_CONTINUE;
2999}
3000
3001void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3002{
Carsten Ottebbd9b642007-10-30 18:44:21 +01003003 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003004 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003005 unsigned long rip_linear;
3006
Avi Kivityf76c7102008-06-13 22:45:42 +03003007 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01003008 return;
3009
Glauber Costa25be4602008-06-10 10:46:53 -03003010 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3011
Izik Eidus77c20022008-12-29 01:42:19 +02003012 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003013
3014 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3015 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003016}
3017EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3018
Harvey Harrison14af3f32008-02-19 10:25:50 -08003019static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02003020 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003021 .read_emulated = emulator_read_emulated,
3022 .write_emulated = emulator_write_emulated,
3023 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3024};
3025
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003026static void cache_all_regs(struct kvm_vcpu *vcpu)
3027{
3028 kvm_register_read(vcpu, VCPU_REGS_RAX);
3029 kvm_register_read(vcpu, VCPU_REGS_RSP);
3030 kvm_register_read(vcpu, VCPU_REGS_RIP);
3031 vcpu->arch.regs_dirty = ~0;
3032}
3033
Carsten Ottebbd9b642007-10-30 18:44:21 +01003034int emulate_instruction(struct kvm_vcpu *vcpu,
Carsten Ottebbd9b642007-10-30 18:44:21 +01003035 unsigned long cr2,
3036 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08003037 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003038{
Glauber Costa310b5d32009-05-12 16:21:06 -04003039 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08003040 struct decode_cache *c;
Avi Kivity851ba692009-08-24 11:10:17 +03003041 struct kvm_run *run = vcpu->run;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003042
Avi Kivity26eef702008-07-03 14:59:22 +03003043 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003044 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003045 /*
Avi Kivity56e82312009-08-12 15:04:37 +03003046 * TODO: fix emulate.c to use guest_read/write_register
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003047 * instead of direct ->regs accesses, can save hundred cycles
3048 * on Intel for instructions that don't read/change RSP, for
3049 * for example.
3050 */
3051 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003052
3053 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003054 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003055
Sheng Yang571008d2008-01-02 14:49:22 +08003056 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01003057 int cs_db, cs_l;
3058 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3059
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003060 vcpu->arch.emulate_ctxt.vcpu = vcpu;
Jan Kiszka91586a32009-10-05 13:07:21 +02003061 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003062 vcpu->arch.emulate_ctxt.mode =
3063 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003064 ? X86EMUL_MODE_REAL : cs_l
3065 ? X86EMUL_MODE_PROT64 : cs_db
3066 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3067
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003068 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08003069
Andre Przywara0cb57622009-06-17 15:50:31 +02003070 /* Only allow emulation of specific instructions on #UD
3071 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08003072 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02003073 if (emulation_type & EMULTYPE_TRAP_UD) {
3074 if (!c->twobyte)
3075 return EMULATE_FAIL;
3076 switch (c->b) {
3077 case 0x01: /* VMMCALL */
3078 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3079 return EMULATE_FAIL;
3080 break;
3081 case 0x34: /* sysenter */
3082 case 0x35: /* sysexit */
3083 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3084 return EMULATE_FAIL;
3085 break;
3086 case 0x05: /* syscall */
3087 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3088 return EMULATE_FAIL;
3089 break;
3090 default:
3091 return EMULATE_FAIL;
3092 }
3093
3094 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3095 return EMULATE_FAIL;
3096 }
Sheng Yang571008d2008-01-02 14:49:22 +08003097
Avi Kivityf2b57562007-11-18 15:17:51 +02003098 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003099 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02003100 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01003101 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3102 return EMULATE_DONE;
3103 return EMULATE_FAIL;
3104 }
3105 }
3106
Gleb Natapovba8afb62009-04-12 13:36:57 +03003107 if (emulation_type & EMULTYPE_SKIP) {
3108 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3109 return EMULATE_DONE;
3110 }
3111
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003112 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04003113 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3114
3115 if (r == 0)
3116 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003117
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003118 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01003119 return EMULATE_DO_MMIO;
3120
3121 if ((r || vcpu->mmio_is_write) && run) {
3122 run->exit_reason = KVM_EXIT_MMIO;
3123 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3124 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3125 run->mmio.len = vcpu->mmio_size;
3126 run->mmio.is_write = vcpu->mmio_is_write;
3127 }
3128
3129 if (r) {
3130 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3131 return EMULATE_DONE;
3132 if (!vcpu->mmio_needed) {
3133 kvm_report_emulation_failure(vcpu, "mmio");
3134 return EMULATE_FAIL;
3135 }
3136 return EMULATE_DO_MMIO;
3137 }
3138
Jan Kiszka91586a32009-10-05 13:07:21 +02003139 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01003140
3141 if (vcpu->mmio_is_write) {
3142 vcpu->mmio_needed = 0;
3143 return EMULATE_DO_MMIO;
3144 }
3145
3146 return EMULATE_DONE;
3147}
3148EXPORT_SYMBOL_GPL(emulate_instruction);
3149
Carsten Ottede7d7892007-10-30 18:44:25 +01003150static int pio_copy_data(struct kvm_vcpu *vcpu)
3151{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003152 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02003153 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01003154 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02003155 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003156
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003157 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3158 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02003159 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01003160 else
Izik Eidus0f346072008-12-29 01:42:20 +02003161 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
3162 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01003163}
3164
3165int complete_pio(struct kvm_vcpu *vcpu)
3166{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003167 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01003168 long delta;
3169 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003170 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003171
3172 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003173 if (io->in) {
3174 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3175 memcpy(&val, vcpu->arch.pio_data, io->size);
3176 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3177 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003178 } else {
3179 if (io->in) {
3180 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003181 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01003182 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003183 }
3184
3185 delta = 1;
3186 if (io->rep) {
3187 delta *= io->cur_count;
3188 /*
3189 * The size of the register should really depend on
3190 * current address size.
3191 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003192 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3193 val -= delta;
3194 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01003195 }
3196 if (io->down)
3197 delta = -delta;
3198 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003199 if (io->in) {
3200 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3201 val += delta;
3202 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3203 } else {
3204 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3205 val += delta;
3206 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3207 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003208 }
3209
Carsten Ottede7d7892007-10-30 18:44:25 +01003210 io->count -= io->cur_count;
3211 io->cur_count = 0;
3212
3213 return 0;
3214}
3215
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003216static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01003217{
3218 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003219 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003220
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003221 if (vcpu->arch.pio.in)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003222 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3223 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01003224 else
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003225 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3226 vcpu->arch.pio.size, pd);
3227 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003228}
3229
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003230static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01003231{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003232 struct kvm_pio_request *io = &vcpu->arch.pio;
3233 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003234 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003235
Carsten Ottede7d7892007-10-30 18:44:25 +01003236 for (i = 0; i < io->cur_count; i++) {
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003237 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
3238 io->port, io->size, pd)) {
3239 r = -EOPNOTSUPP;
3240 break;
3241 }
Carsten Ottede7d7892007-10-30 18:44:25 +01003242 pd += io->size;
3243 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003244 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01003245}
3246
Avi Kivity851ba692009-08-24 11:10:17 +03003247int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
Carsten Ottede7d7892007-10-30 18:44:25 +01003248{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003249 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01003250
3251 vcpu->run->exit_reason = KVM_EXIT_IO;
3252 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003253 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003254 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003255 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3256 vcpu->run->io.port = vcpu->arch.pio.port = port;
3257 vcpu->arch.pio.in = in;
3258 vcpu->arch.pio.string = 0;
3259 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003260 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003261
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003262 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3263 size, 1);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003264
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003265 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3266 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01003267
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003268 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003269 complete_pio(vcpu);
3270 return 1;
3271 }
3272 return 0;
3273}
3274EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3275
Avi Kivity851ba692009-08-24 11:10:17 +03003276int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
Carsten Ottede7d7892007-10-30 18:44:25 +01003277 int size, unsigned long count, int down,
3278 gva_t address, int rep, unsigned port)
3279{
3280 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02003281 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01003282
3283 vcpu->run->exit_reason = KVM_EXIT_IO;
3284 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003285 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003286 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003287 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3288 vcpu->run->io.port = vcpu->arch.pio.port = port;
3289 vcpu->arch.pio.in = in;
3290 vcpu->arch.pio.string = 1;
3291 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003292 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003293
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003294 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3295 size, count);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003296
Carsten Ottede7d7892007-10-30 18:44:25 +01003297 if (!count) {
3298 kvm_x86_ops->skip_emulated_instruction(vcpu);
3299 return 1;
3300 }
3301
3302 if (!down)
3303 in_page = PAGE_SIZE - offset_in_page(address);
3304 else
3305 in_page = offset_in_page(address) + size;
3306 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003307 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003308 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003309 if (down) {
3310 /*
3311 * String I/O in reverse. Yuck. Kill the guest, fix later.
3312 */
3313 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003314 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003315 return 1;
3316 }
3317 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003318 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003319
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003320 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003321 kvm_x86_ops->skip_emulated_instruction(vcpu);
3322
Izik Eidus0f346072008-12-29 01:42:20 +02003323 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003324
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003325 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003326 /* string PIO write */
3327 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02003328 if (ret == X86EMUL_PROPAGATE_FAULT) {
3329 kvm_inject_gp(vcpu, 0);
3330 return 1;
3331 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003332 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003333 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003334 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003335 ret = 1;
3336 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003337 }
3338 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003339
3340 return ret;
3341}
3342EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3343
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003344static void bounce_off(void *info)
3345{
3346 /* nothing */
3347}
3348
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003349static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3350 void *data)
3351{
3352 struct cpufreq_freqs *freq = data;
3353 struct kvm *kvm;
3354 struct kvm_vcpu *vcpu;
3355 int i, send_ipi = 0;
3356
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003357 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3358 return 0;
3359 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3360 return 0;
Zachary Amsden0cca7902009-09-29 11:38:35 -10003361 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003362
3363 spin_lock(&kvm_lock);
3364 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003365 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003366 if (vcpu->cpu != freq->cpu)
3367 continue;
3368 if (!kvm_request_guest_time_update(vcpu))
3369 continue;
3370 if (vcpu->cpu != smp_processor_id())
3371 send_ipi++;
3372 }
3373 }
3374 spin_unlock(&kvm_lock);
3375
3376 if (freq->old < freq->new && send_ipi) {
3377 /*
3378 * We upscale the frequency. Must make the guest
3379 * doesn't see old kvmclock values while running with
3380 * the new frequency, otherwise we risk the guest sees
3381 * time go backwards.
3382 *
3383 * In case we update the frequency for another cpu
3384 * (which might be in guest context) send an interrupt
3385 * to kick the cpu out of guest context. Next time
3386 * guest context is entered kvmclock will be updated,
3387 * so the guest will not see stale values.
3388 */
3389 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3390 }
3391 return 0;
3392}
3393
3394static struct notifier_block kvmclock_cpufreq_notifier_block = {
3395 .notifier_call = kvmclock_cpufreq_notifier
3396};
3397
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003398static void kvm_timer_init(void)
3399{
3400 int cpu;
3401
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003402 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003403 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3404 CPUFREQ_TRANSITION_NOTIFIER);
Zachary Amsden6b7d7e72009-10-09 16:26:08 -10003405 for_each_online_cpu(cpu) {
3406 unsigned long khz = cpufreq_get(cpu);
3407 if (!khz)
3408 khz = tsc_khz;
3409 per_cpu(cpu_tsc_khz, cpu) = khz;
3410 }
Zachary Amsden0cca7902009-09-29 11:38:35 -10003411 } else {
3412 for_each_possible_cpu(cpu)
3413 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003414 }
3415}
3416
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003417int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003418{
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003419 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003420 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3421
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003422 if (kvm_x86_ops) {
3423 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003424 r = -EEXIST;
3425 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003426 }
3427
3428 if (!ops->cpu_has_kvm_support()) {
3429 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003430 r = -EOPNOTSUPP;
3431 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003432 }
3433 if (ops->disabled_by_bios()) {
3434 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003435 r = -EOPNOTSUPP;
3436 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003437 }
3438
Avi Kivity97db56c2008-01-13 13:23:56 +02003439 r = kvm_mmu_module_init();
3440 if (r)
3441 goto out;
3442
3443 kvm_init_msr_list();
3444
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003445 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003446 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003447 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3448 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003449 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003450
Zachary Amsdenb820cc02009-09-29 11:38:34 -10003451 kvm_timer_init();
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003452
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003453 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003454
3455out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003456 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003457}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003458
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003459void kvm_arch_exit(void)
3460{
Jan Kiszka888d2562009-04-17 19:24:58 +02003461 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3462 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3463 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003464 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003465 kvm_mmu_module_exit();
3466}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003467
Hollis Blanchard8776e512007-10-31 17:24:24 -05003468int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3469{
3470 ++vcpu->stat.halt_exits;
3471 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003472 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003473 return 1;
3474 } else {
3475 vcpu->run->exit_reason = KVM_EXIT_HLT;
3476 return 0;
3477 }
3478}
3479EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3480
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003481static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3482 unsigned long a1)
3483{
3484 if (is_long_mode(vcpu))
3485 return a0;
3486 else
3487 return a0 | ((gpa_t)a1 << 32);
3488}
3489
Hollis Blanchard8776e512007-10-31 17:24:24 -05003490int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3491{
3492 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003493 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003494
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003495 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3496 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3497 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3498 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3499 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003500
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003501 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003502
Hollis Blanchard8776e512007-10-31 17:24:24 -05003503 if (!is_long_mode(vcpu)) {
3504 nr &= 0xFFFFFFFF;
3505 a0 &= 0xFFFFFFFF;
3506 a1 &= 0xFFFFFFFF;
3507 a2 &= 0xFFFFFFFF;
3508 a3 &= 0xFFFFFFFF;
3509 }
3510
Jan Kiszka07708c42009-08-03 18:43:28 +02003511 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3512 ret = -KVM_EPERM;
3513 goto out;
3514 }
3515
Hollis Blanchard8776e512007-10-31 17:24:24 -05003516 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003517 case KVM_HC_VAPIC_POLL_IRQ:
3518 ret = 0;
3519 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003520 case KVM_HC_MMU_OP:
3521 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3522 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003523 default:
3524 ret = -KVM_ENOSYS;
3525 break;
3526 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003527out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003528 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303529 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003530 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003531}
3532EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3533
3534int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3535{
3536 char instruction[3];
3537 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003538 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003539
Hollis Blanchard8776e512007-10-31 17:24:24 -05003540
3541 /*
3542 * Blow out the MMU to ensure that no other VCPU has an active mapping
3543 * to ensure that the updated hypercall appears atomically across all
3544 * VCPUs.
3545 */
3546 kvm_mmu_zap_all(vcpu->kvm);
3547
Hollis Blanchard8776e512007-10-31 17:24:24 -05003548 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003549 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003550 != X86EMUL_CONTINUE)
3551 ret = -EFAULT;
3552
Hollis Blanchard8776e512007-10-31 17:24:24 -05003553 return ret;
3554}
3555
3556static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3557{
3558 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3559}
3560
3561void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3562{
3563 struct descriptor_table dt = { limit, base };
3564
3565 kvm_x86_ops->set_gdt(vcpu, &dt);
3566}
3567
3568void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3569{
3570 struct descriptor_table dt = { limit, base };
3571
3572 kvm_x86_ops->set_idt(vcpu, &dt);
3573}
3574
3575void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3576 unsigned long *rflags)
3577{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003578 kvm_lmsw(vcpu, msw);
Jan Kiszka91586a32009-10-05 13:07:21 +02003579 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003580}
3581
3582unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3583{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003584 unsigned long value;
3585
Hollis Blanchard8776e512007-10-31 17:24:24 -05003586 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3587 switch (cr) {
3588 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003589 value = vcpu->arch.cr0;
3590 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003591 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003592 value = vcpu->arch.cr2;
3593 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003594 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003595 value = vcpu->arch.cr3;
3596 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003597 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003598 value = vcpu->arch.cr4;
3599 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003600 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003601 value = kvm_get_cr8(vcpu);
3602 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003603 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003604 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003605 return 0;
3606 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003607
3608 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003609}
3610
3611void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3612 unsigned long *rflags)
3613{
3614 switch (cr) {
3615 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003616 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Jan Kiszka91586a32009-10-05 13:07:21 +02003617 *rflags = kvm_get_rflags(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003618 break;
3619 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003620 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003621 break;
3622 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003623 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003624 break;
3625 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003626 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003627 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003628 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003629 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003630 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003631 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003632 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003633 }
3634}
3635
Dan Kenigsberg07716712007-11-21 17:10:04 +02003636static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003638 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3639 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02003640
3641 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3642 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08003643 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003644 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003645 if (ej->function == e->function) {
3646 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3647 return j;
3648 }
3649 }
3650 return 0; /* silence gcc, even though control never reaches here */
3651}
3652
3653/* find an entry with matching function, matching index (if needed), and that
3654 * should be read next (if it's stateful) */
3655static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3656 u32 function, u32 index)
3657{
3658 if (e->function != function)
3659 return 0;
3660 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3661 return 0;
3662 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00003663 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02003664 return 0;
3665 return 1;
3666}
3667
Alexander Grafd8017472008-11-25 20:17:11 +01003668struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3669 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003670{
3671 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01003672 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003673
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003674 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01003675 struct kvm_cpuid_entry2 *e;
3676
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003677 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003678 if (is_matching_cpuid_entry(e, function, index)) {
3679 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3680 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003681 best = e;
3682 break;
3683 }
3684 /*
3685 * Both basic or both extended?
3686 */
3687 if (((e->function ^ function) & 0x80000000) == 0)
3688 if (!best || e->function > best->function)
3689 best = e;
3690 }
Alexander Grafd8017472008-11-25 20:17:11 +01003691 return best;
3692}
3693
Dong, Eddie82725b22009-03-30 16:21:08 +08003694int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3695{
3696 struct kvm_cpuid_entry2 *best;
3697
3698 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3699 if (best)
3700 return best->eax & 0xff;
3701 return 36;
3702}
3703
Alexander Grafd8017472008-11-25 20:17:11 +01003704void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3705{
3706 u32 function, index;
3707 struct kvm_cpuid_entry2 *best;
3708
3709 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3710 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3711 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3712 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3713 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3714 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3715 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003716 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003717 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3718 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3719 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3720 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003721 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003722 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003723 trace_kvm_cpuid(function,
3724 kvm_register_read(vcpu, VCPU_REGS_RAX),
3725 kvm_register_read(vcpu, VCPU_REGS_RBX),
3726 kvm_register_read(vcpu, VCPU_REGS_RCX),
3727 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003728}
3729EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003730
3731/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003732 * Check if userspace requested an interrupt window, and that the
3733 * interrupt window is open.
3734 *
3735 * No need to exit to userspace if we already have an interrupt queued.
3736 */
Avi Kivity851ba692009-08-24 11:10:17 +03003737static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003738{
Gleb Natapov80618232009-04-21 17:44:56 +03003739 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003740 vcpu->run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03003741 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003742}
3743
Avi Kivity851ba692009-08-24 11:10:17 +03003744static void post_kvm_run_save(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003745{
Avi Kivity851ba692009-08-24 11:10:17 +03003746 struct kvm_run *kvm_run = vcpu->run;
3747
Jan Kiszka91586a32009-10-05 13:07:21 +02003748 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003749 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003750 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01003751 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003752 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01003753 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003754 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03003755 kvm_arch_interrupt_allowed(vcpu) &&
3756 !kvm_cpu_has_interrupt(vcpu) &&
3757 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003758}
3759
Avi Kivityb93463a2007-10-25 16:52:32 +02003760static void vapic_enter(struct kvm_vcpu *vcpu)
3761{
3762 struct kvm_lapic *apic = vcpu->arch.apic;
3763 struct page *page;
3764
3765 if (!apic || !apic->vapic_addr)
3766 return;
3767
3768 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003769
3770 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003771}
3772
3773static void vapic_exit(struct kvm_vcpu *vcpu)
3774{
3775 struct kvm_lapic *apic = vcpu->arch.apic;
3776
3777 if (!apic || !apic->vapic_addr)
3778 return;
3779
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003780 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003781 kvm_release_page_dirty(apic->vapic_page);
3782 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003783 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003784}
3785
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003786static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3787{
3788 int max_irr, tpr;
3789
3790 if (!kvm_x86_ops->update_cr8_intercept)
3791 return;
3792
Avi Kivity88c808f2009-08-17 22:49:40 +03003793 if (!vcpu->arch.apic)
3794 return;
3795
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003796 if (!vcpu->arch.apic->vapic_addr)
3797 max_irr = kvm_lapic_find_highest_irr(vcpu);
3798 else
3799 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003800
3801 if (max_irr != -1)
3802 max_irr >>= 4;
3803
3804 tpr = kvm_lapic_get_cr8(vcpu);
3805
3806 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3807}
3808
Avi Kivity851ba692009-08-24 11:10:17 +03003809static void inject_pending_event(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003810{
3811 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03003812 if (vcpu->arch.exception.pending) {
3813 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
3814 vcpu->arch.exception.has_error_code,
3815 vcpu->arch.exception.error_code);
3816 return;
3817 }
3818
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003819 if (vcpu->arch.nmi_injected) {
3820 kvm_x86_ops->set_nmi(vcpu);
3821 return;
3822 }
3823
3824 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003825 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003826 return;
3827 }
3828
3829 /* try to inject new event if pending */
3830 if (vcpu->arch.nmi_pending) {
3831 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3832 vcpu->arch.nmi_pending = false;
3833 vcpu->arch.nmi_injected = true;
3834 kvm_x86_ops->set_nmi(vcpu);
3835 }
3836 } else if (kvm_cpu_has_interrupt(vcpu)) {
3837 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003838 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3839 false);
3840 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003841 }
3842 }
3843}
3844
Avi Kivity851ba692009-08-24 11:10:17 +03003845static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003846{
3847 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003848 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003849 vcpu->run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003850
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003851 if (vcpu->requests)
3852 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3853 kvm_mmu_unload(vcpu);
3854
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003855 r = kvm_mmu_reload(vcpu);
3856 if (unlikely(r))
3857 goto out;
3858
Avi Kivity2f52d582008-01-16 12:49:30 +02003859 if (vcpu->requests) {
3860 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003861 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003862 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3863 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03003864 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3865 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003866 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3867 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003868 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3869 &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003870 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
Avi Kivityb93463a2007-10-25 16:52:32 +02003871 r = 0;
3872 goto out;
3873 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003874 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003875 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003876 r = 0;
3877 goto out;
3878 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003879 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003880
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003881 preempt_disable();
3882
3883 kvm_x86_ops->prepare_guest_switch(vcpu);
3884 kvm_load_guest_fpu(vcpu);
3885
3886 local_irq_disable();
3887
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003888 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3889 smp_mb__after_clear_bit();
3890
Marcelo Tosattid7690172008-09-08 15:23:48 -03003891 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03003892 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c142802008-01-15 18:27:32 +02003893 local_irq_enable();
3894 preempt_enable();
3895 r = 1;
3896 goto out;
3897 }
3898
Avi Kivity851ba692009-08-24 11:10:17 +03003899 inject_pending_event(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003900
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003901 /* enable NMI/IRQ window open exits if needed */
3902 if (vcpu->arch.nmi_pending)
3903 kvm_x86_ops->enable_nmi_window(vcpu);
3904 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3905 kvm_x86_ops->enable_irq_window(vcpu);
3906
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003907 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003908 update_cr8_intercept(vcpu);
3909 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003910 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003911
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003912 up_read(&vcpu->kvm->slots_lock);
3913
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003914 kvm_guest_enter();
3915
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003916 if (unlikely(vcpu->arch.switch_db_regs)) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003917 set_debugreg(0, 7);
3918 set_debugreg(vcpu->arch.eff_db[0], 0);
3919 set_debugreg(vcpu->arch.eff_db[1], 1);
3920 set_debugreg(vcpu->arch.eff_db[2], 2);
3921 set_debugreg(vcpu->arch.eff_db[3], 3);
3922 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003923
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003924 trace_kvm_entry(vcpu->vcpu_id);
Avi Kivity851ba692009-08-24 11:10:17 +03003925 kvm_x86_ops->run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003926
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02003927 /*
3928 * If the guest has used debug registers, at least dr7
3929 * will be disabled while returning to the host.
3930 * If we don't have active breakpoints in the host, we don't
3931 * care about the messed up debug address registers. But if
3932 * we have some of them active, restore the old state.
3933 */
Frederic Weisbecker59d8eb52009-11-10 11:03:12 +01003934 if (hw_breakpoint_active())
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02003935 hw_breakpoint_restore();
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003936
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003937 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003938 local_irq_enable();
3939
3940 ++vcpu->stat.exits;
3941
3942 /*
3943 * We must have an instruction between local_irq_enable() and
3944 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3945 * the interrupt shadow. The stat.exits increment will do nicely.
3946 * But we need to prevent reordering, hence this barrier():
3947 */
3948 barrier();
3949
3950 kvm_guest_exit();
3951
3952 preempt_enable();
3953
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003954 down_read(&vcpu->kvm->slots_lock);
3955
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003956 /*
3957 * Profile KVM exit RIPs:
3958 */
3959 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003960 unsigned long rip = kvm_rip_read(vcpu);
3961 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003962 }
3963
Avi Kivity298101d2007-11-25 13:41:11 +02003964
Avi Kivityb93463a2007-10-25 16:52:32 +02003965 kvm_lapic_sync_from_vapic(vcpu);
3966
Avi Kivity851ba692009-08-24 11:10:17 +03003967 r = kvm_x86_ops->handle_exit(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003968out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003969 return r;
3970}
3971
Gleb Natapov09cec752009-03-23 15:11:44 +02003972
Avi Kivity851ba692009-08-24 11:10:17 +03003973static int __vcpu_run(struct kvm_vcpu *vcpu)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003974{
3975 int r;
3976
3977 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003978 pr_debug("vcpu %d received sipi with vector # %x\n",
3979 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003980 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003981 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003982 if (r)
3983 return r;
3984 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003985 }
3986
Marcelo Tosattid7690172008-09-08 15:23:48 -03003987 down_read(&vcpu->kvm->slots_lock);
3988 vapic_enter(vcpu);
3989
3990 r = 1;
3991 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003992 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Avi Kivity851ba692009-08-24 11:10:17 +03003993 r = vcpu_enter_guest(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003994 else {
3995 up_read(&vcpu->kvm->slots_lock);
3996 kvm_vcpu_block(vcpu);
3997 down_read(&vcpu->kvm->slots_lock);
3998 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02003999 {
4000 switch(vcpu->arch.mp_state) {
4001 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03004002 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02004003 KVM_MP_STATE_RUNNABLE;
4004 case KVM_MP_STATE_RUNNABLE:
4005 break;
4006 case KVM_MP_STATE_SIPI_RECEIVED:
4007 default:
4008 r = -EINTR;
4009 break;
4010 }
4011 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03004012 }
4013
Gleb Natapov09cec752009-03-23 15:11:44 +02004014 if (r <= 0)
4015 break;
4016
4017 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4018 if (kvm_cpu_has_pending_timer(vcpu))
4019 kvm_inject_pending_timer_irqs(vcpu);
4020
Avi Kivity851ba692009-08-24 11:10:17 +03004021 if (dm_request_for_irq_injection(vcpu)) {
Gleb Natapov09cec752009-03-23 15:11:44 +02004022 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004023 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004024 ++vcpu->stat.request_irq_exits;
4025 }
4026 if (signal_pending(current)) {
4027 r = -EINTR;
Avi Kivity851ba692009-08-24 11:10:17 +03004028 vcpu->run->exit_reason = KVM_EXIT_INTR;
Gleb Natapov09cec752009-03-23 15:11:44 +02004029 ++vcpu->stat.signal_exits;
4030 }
4031 if (need_resched()) {
4032 up_read(&vcpu->kvm->slots_lock);
4033 kvm_resched(vcpu);
4034 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004035 }
4036 }
4037
4038 up_read(&vcpu->kvm->slots_lock);
Avi Kivity851ba692009-08-24 11:10:17 +03004039 post_kvm_run_save(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004040
Avi Kivityb93463a2007-10-25 16:52:32 +02004041 vapic_exit(vcpu);
4042
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004043 return r;
4044}
4045
4046int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4047{
4048 int r;
4049 sigset_t sigsaved;
4050
4051 vcpu_load(vcpu);
4052
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004053 if (vcpu->sigset_active)
4054 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4055
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004056 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4057 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03004058 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03004059 r = -EAGAIN;
4060 goto out;
4061 }
4062
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004063 /* re-sync apic's tpr */
4064 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004065 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004066
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004067 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004068 r = complete_pio(vcpu);
4069 if (r)
4070 goto out;
4071 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004072 if (vcpu->mmio_needed) {
4073 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4074 vcpu->mmio_read_completed = 1;
4075 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004076
4077 down_read(&vcpu->kvm->slots_lock);
Avi Kivity851ba692009-08-24 11:10:17 +03004078 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
Sheng Yang571008d2008-01-02 14:49:22 +08004079 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004080 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004081 if (r == EMULATE_DO_MMIO) {
4082 /*
4083 * Read-modify-write. Back to userspace.
4084 */
4085 r = 0;
4086 goto out;
4087 }
4088 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004089 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4090 kvm_register_write(vcpu, VCPU_REGS_RAX,
4091 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004092
Avi Kivity851ba692009-08-24 11:10:17 +03004093 r = __vcpu_run(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004094
4095out:
4096 if (vcpu->sigset_active)
4097 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4098
4099 vcpu_put(vcpu);
4100 return r;
4101}
4102
4103int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4104{
4105 vcpu_load(vcpu);
4106
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004107 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4108 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4109 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4110 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4111 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4112 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4113 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4114 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004115#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004116 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4117 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4118 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4119 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4120 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4121 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4122 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4123 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004124#endif
4125
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004126 regs->rip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004127 regs->rflags = kvm_get_rflags(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004128
4129 vcpu_put(vcpu);
4130
4131 return 0;
4132}
4133
4134int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4135{
4136 vcpu_load(vcpu);
4137
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004138 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4139 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4140 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4141 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4142 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4143 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4144 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4145 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004146#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004147 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4148 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4149 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4150 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4151 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4152 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4153 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4154 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004155#endif
4156
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004157 kvm_rip_write(vcpu, regs->rip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004158 kvm_set_rflags(vcpu, regs->rflags);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004159
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02004160 vcpu->arch.exception.pending = false;
4161
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004162 vcpu_put(vcpu);
4163
4164 return 0;
4165}
4166
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004167void kvm_get_segment(struct kvm_vcpu *vcpu,
4168 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004169{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004170 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004171}
4172
4173void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4174{
4175 struct kvm_segment cs;
4176
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004177 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004178 *db = cs.db;
4179 *l = cs.l;
4180}
4181EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4182
4183int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4184 struct kvm_sregs *sregs)
4185{
4186 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004187
4188 vcpu_load(vcpu);
4189
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004190 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4191 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4192 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4193 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4194 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4195 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004196
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004197 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4198 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004199
4200 kvm_x86_ops->get_idt(vcpu, &dt);
4201 sregs->idt.limit = dt.limit;
4202 sregs->idt.base = dt.base;
4203 kvm_x86_ops->get_gdt(vcpu, &dt);
4204 sregs->gdt.limit = dt.limit;
4205 sregs->gdt.base = dt.base;
4206
4207 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004208 sregs->cr0 = vcpu->arch.cr0;
4209 sregs->cr2 = vcpu->arch.cr2;
4210 sregs->cr3 = vcpu->arch.cr3;
4211 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004212 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004213 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004214 sregs->apic_base = kvm_get_apic_base(vcpu);
4215
Gleb Natapov923c61b2009-05-11 13:35:48 +03004216 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004217
Gleb Natapov36752c92009-05-11 13:35:53 +03004218 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03004219 set_bit(vcpu->arch.interrupt.nr,
4220 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03004221
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004222 vcpu_put(vcpu);
4223
4224 return 0;
4225}
4226
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03004227int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4228 struct kvm_mp_state *mp_state)
4229{
4230 vcpu_load(vcpu);
4231 mp_state->mp_state = vcpu->arch.mp_state;
4232 vcpu_put(vcpu);
4233 return 0;
4234}
4235
4236int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4237 struct kvm_mp_state *mp_state)
4238{
4239 vcpu_load(vcpu);
4240 vcpu->arch.mp_state = mp_state->mp_state;
4241 vcpu_put(vcpu);
4242 return 0;
4243}
4244
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004245static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004246 struct kvm_segment *var, int seg)
4247{
Harvey Harrison14af3f32008-02-19 10:25:50 -08004248 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004249}
4250
Izik Eidus37817f22008-03-24 23:14:53 +02004251static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4252 struct kvm_segment *kvm_desct)
4253{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004254 kvm_desct->base = get_desc_base(seg_desc);
4255 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03004256 if (seg_desc->g) {
4257 kvm_desct->limit <<= 12;
4258 kvm_desct->limit |= 0xfff;
4259 }
Izik Eidus37817f22008-03-24 23:14:53 +02004260 kvm_desct->selector = selector;
4261 kvm_desct->type = seg_desc->type;
4262 kvm_desct->present = seg_desc->p;
4263 kvm_desct->dpl = seg_desc->dpl;
4264 kvm_desct->db = seg_desc->d;
4265 kvm_desct->s = seg_desc->s;
4266 kvm_desct->l = seg_desc->l;
4267 kvm_desct->g = seg_desc->g;
4268 kvm_desct->avl = seg_desc->avl;
4269 if (!selector)
4270 kvm_desct->unusable = 1;
4271 else
4272 kvm_desct->unusable = 0;
4273 kvm_desct->padding = 0;
4274}
4275
Amit Shahb8222ad2008-10-22 16:39:47 +05304276static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4277 u16 selector,
4278 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004279{
4280 if (selector & 1 << 2) {
4281 struct kvm_segment kvm_seg;
4282
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004283 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004284
4285 if (kvm_seg.unusable)
4286 dtable->limit = 0;
4287 else
4288 dtable->limit = kvm_seg.limit;
4289 dtable->base = kvm_seg.base;
4290 }
4291 else
4292 kvm_x86_ops->get_gdt(vcpu, dtable);
4293}
4294
4295/* allowed just for 8 bytes segments */
4296static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4297 struct desc_struct *seg_desc)
4298{
4299 struct descriptor_table dtable;
4300 u16 index = selector >> 3;
4301
Amit Shahb8222ad2008-10-22 16:39:47 +05304302 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004303
4304 if (dtable.limit < index * 8 + 7) {
4305 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4306 return 1;
4307 }
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004308 return kvm_read_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004309}
4310
4311/* allowed just for 8 bytes segments */
4312static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4313 struct desc_struct *seg_desc)
4314{
4315 struct descriptor_table dtable;
4316 u16 index = selector >> 3;
4317
Amit Shahb8222ad2008-10-22 16:39:47 +05304318 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004319
4320 if (dtable.limit < index * 8 + 7)
4321 return 1;
Mikhail Ershovd9048d32009-08-19 14:08:07 +04004322 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004323}
4324
Gleb Natapovabb39112009-10-25 17:42:02 +02004325static gpa_t get_tss_base_addr(struct kvm_vcpu *vcpu,
Izik Eidus37817f22008-03-24 23:14:53 +02004326 struct desc_struct *seg_desc)
4327{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004328 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004329
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004330 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02004331}
4332
Izik Eidus37817f22008-03-24 23:14:53 +02004333static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4334{
4335 struct kvm_segment kvm_seg;
4336
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004337 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004338 return kvm_seg.selector;
4339}
4340
4341static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4342 u16 selector,
4343 struct kvm_segment *kvm_seg)
4344{
4345 struct desc_struct seg_desc;
4346
4347 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4348 return 1;
4349 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4350 return 0;
4351}
4352
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004353static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004354{
4355 struct kvm_segment segvar = {
4356 .base = selector << 4,
4357 .limit = 0xffff,
4358 .selector = selector,
4359 .type = 3,
4360 .present = 1,
4361 .dpl = 3,
4362 .db = 0,
4363 .s = 1,
4364 .l = 0,
4365 .g = 0,
4366 .avl = 0,
4367 .unusable = 0,
4368 };
4369 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4370 return 0;
4371}
4372
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004373static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4374{
4375 return (seg != VCPU_SREG_LDTR) &&
4376 (seg != VCPU_SREG_TR) &&
Jan Kiszka91586a32009-10-05 13:07:21 +02004377 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004378}
4379
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004380int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4381 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004382{
4383 struct kvm_segment kvm_seg;
4384
Anthony Liguoric0c7c042009-08-11 15:57:59 -05004385 if (is_vm86_segment(vcpu, seg) || !(vcpu->arch.cr0 & X86_CR0_PE))
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004386 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004387 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4388 return 1;
4389 kvm_seg.type |= type_bits;
4390
4391 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4392 seg != VCPU_SREG_LDTR)
4393 if (!kvm_seg.s)
4394 kvm_seg.unusable = 1;
4395
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004396 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004397 return 0;
4398}
4399
4400static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4401 struct tss_segment_32 *tss)
4402{
4403 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004404 tss->eip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004405 tss->eflags = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004406 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4407 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4408 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4409 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4410 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4411 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4412 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4413 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004414 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4415 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4416 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4417 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4418 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4419 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4420 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004421}
4422
4423static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4424 struct tss_segment_32 *tss)
4425{
4426 kvm_set_cr3(vcpu, tss->cr3);
4427
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004428 kvm_rip_write(vcpu, tss->eip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004429 kvm_set_rflags(vcpu, tss->eflags | 2);
Izik Eidus37817f22008-03-24 23:14:53 +02004430
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004431 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4432 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4433 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4434 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4435 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4436 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4437 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4438 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004439
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004440 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004441 return 1;
4442
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004443 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004444 return 1;
4445
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004446 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004447 return 1;
4448
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004449 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004450 return 1;
4451
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004452 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004453 return 1;
4454
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004455 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004456 return 1;
4457
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004458 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004459 return 1;
4460 return 0;
4461}
4462
4463static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4464 struct tss_segment_16 *tss)
4465{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004466 tss->ip = kvm_rip_read(vcpu);
Jan Kiszka91586a32009-10-05 13:07:21 +02004467 tss->flag = kvm_get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004468 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4469 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4470 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4471 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4472 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4473 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4474 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4475 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004476
4477 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4478 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4479 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4480 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4481 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004482}
4483
4484static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4485 struct tss_segment_16 *tss)
4486{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004487 kvm_rip_write(vcpu, tss->ip);
Jan Kiszka91586a32009-10-05 13:07:21 +02004488 kvm_set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004489 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4490 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4491 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4492 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4493 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4494 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4495 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4496 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02004497
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004498 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004499 return 1;
4500
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004501 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004502 return 1;
4503
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004504 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004505 return 1;
4506
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004507 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004508 return 1;
4509
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004510 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004511 return 1;
4512 return 0;
4513}
4514
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004515static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004516 u16 old_tss_sel, u32 old_tss_base,
4517 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02004518{
4519 struct tss_segment_16 tss_segment_16;
4520 int ret = 0;
4521
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004522 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4523 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004524 goto out;
4525
4526 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02004527
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004528 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4529 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004530 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004531
4532 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4533 &tss_segment_16, sizeof tss_segment_16))
4534 goto out;
4535
Gleb Natapovb237ac32009-03-30 16:03:24 +03004536 if (old_tss_sel != 0xffff) {
4537 tss_segment_16.prev_task_link = old_tss_sel;
4538
4539 if (kvm_write_guest(vcpu->kvm,
4540 get_tss_base_addr(vcpu, nseg_desc),
4541 &tss_segment_16.prev_task_link,
4542 sizeof tss_segment_16.prev_task_link))
4543 goto out;
4544 }
4545
Izik Eidus37817f22008-03-24 23:14:53 +02004546 if (load_state_from_tss16(vcpu, &tss_segment_16))
4547 goto out;
4548
4549 ret = 1;
4550out:
4551 return ret;
4552}
4553
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004554static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004555 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02004556 struct desc_struct *nseg_desc)
4557{
4558 struct tss_segment_32 tss_segment_32;
4559 int ret = 0;
4560
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004561 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4562 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004563 goto out;
4564
4565 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02004566
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004567 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4568 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004569 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004570
4571 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4572 &tss_segment_32, sizeof tss_segment_32))
4573 goto out;
4574
Gleb Natapovb237ac32009-03-30 16:03:24 +03004575 if (old_tss_sel != 0xffff) {
4576 tss_segment_32.prev_task_link = old_tss_sel;
4577
4578 if (kvm_write_guest(vcpu->kvm,
4579 get_tss_base_addr(vcpu, nseg_desc),
4580 &tss_segment_32.prev_task_link,
4581 sizeof tss_segment_32.prev_task_link))
4582 goto out;
4583 }
4584
Izik Eidus37817f22008-03-24 23:14:53 +02004585 if (load_state_from_tss32(vcpu, &tss_segment_32))
4586 goto out;
4587
4588 ret = 1;
4589out:
4590 return ret;
4591}
4592
4593int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4594{
4595 struct kvm_segment tr_seg;
4596 struct desc_struct cseg_desc;
4597 struct desc_struct nseg_desc;
4598 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004599 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4600 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004601
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004602 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02004603
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004604 /* FIXME: Handle errors. Failure to read either TSS or their
4605 * descriptors should generate a pagefault.
4606 */
Izik Eidus37817f22008-03-24 23:14:53 +02004607 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4608 goto out;
4609
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004610 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02004611 goto out;
4612
Izik Eidus37817f22008-03-24 23:14:53 +02004613 if (reason != TASK_SWITCH_IRET) {
4614 int cpl;
4615
4616 cpl = kvm_x86_ops->get_cpl(vcpu);
4617 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4618 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4619 return 1;
4620 }
4621 }
4622
Akinobu Mita46a359e2009-07-18 23:58:32 +09004623 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
Izik Eidus37817f22008-03-24 23:14:53 +02004624 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4625 return 1;
4626 }
4627
4628 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004629 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004630 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004631 }
4632
4633 if (reason == TASK_SWITCH_IRET) {
Jan Kiszka91586a32009-10-05 13:07:21 +02004634 u32 eflags = kvm_get_rflags(vcpu);
4635 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02004636 }
4637
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004638 /* set back link to prev task only if NT bit is set in eflags
4639 note that old_tss_sel is not used afetr this point */
4640 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4641 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02004642
4643 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03004644 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4645 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004646 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03004647 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4648 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004649
4650 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
Jan Kiszka91586a32009-10-05 13:07:21 +02004651 u32 eflags = kvm_get_rflags(vcpu);
4652 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
Izik Eidus37817f22008-03-24 23:14:53 +02004653 }
4654
4655 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004656 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02004657 save_guest_segment_descriptor(vcpu, tss_selector,
4658 &nseg_desc);
4659 }
4660
4661 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4662 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4663 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004664 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004665out:
Izik Eidus37817f22008-03-24 23:14:53 +02004666 return ret;
4667}
4668EXPORT_SYMBOL_GPL(kvm_task_switch);
4669
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004670int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4671 struct kvm_sregs *sregs)
4672{
4673 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004674 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004675 struct descriptor_table dt;
4676
4677 vcpu_load(vcpu);
4678
4679 dt.limit = sregs->idt.limit;
4680 dt.base = sregs->idt.base;
4681 kvm_x86_ops->set_idt(vcpu, &dt);
4682 dt.limit = sregs->gdt.limit;
4683 dt.base = sregs->gdt.base;
4684 kvm_x86_ops->set_gdt(vcpu, &dt);
4685
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004686 vcpu->arch.cr2 = sregs->cr2;
4687 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02004688 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004689
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004690 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004691
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004692 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004693 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004694 kvm_set_apic_base(vcpu, sregs->apic_base);
4695
4696 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4697
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004698 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004699 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004700 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004701
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004702 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004703 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004704 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004705 load_pdptrs(vcpu, vcpu->arch.cr3);
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004706 mmu_reset_needed = 1;
4707 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004708
4709 if (mmu_reset_needed)
4710 kvm_mmu_reset_context(vcpu);
4711
Gleb Natapov923c61b2009-05-11 13:35:48 +03004712 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4713 pending_vec = find_first_bit(
4714 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4715 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004716 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004717 pr_debug("Set back pending irq %d\n", pending_vec);
4718 if (irqchip_in_kernel(vcpu->kvm))
4719 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004720 }
4721
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004722 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4723 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4724 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4725 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4726 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4727 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004728
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004729 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4730 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004731
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03004732 update_cr8_intercept(vcpu);
4733
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004734 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004735 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004736 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4737 !(vcpu->arch.cr0 & X86_CR0_PE))
4738 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4739
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004740 vcpu_put(vcpu);
4741
4742 return 0;
4743}
4744
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004745int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4746 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004747{
Jan Kiszka355be0b2009-10-03 00:31:21 +02004748 unsigned long rflags;
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004749 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004750
4751 vcpu_load(vcpu);
4752
Jan Kiszka4f926bf22009-10-30 12:46:59 +01004753 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
4754 r = -EBUSY;
4755 if (vcpu->arch.exception.pending)
4756 goto unlock_out;
4757 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4758 kvm_queue_exception(vcpu, DB_VECTOR);
4759 else
4760 kvm_queue_exception(vcpu, BP_VECTOR);
4761 }
4762
Jan Kiszka91586a32009-10-05 13:07:21 +02004763 /*
4764 * Read rflags as long as potentially injected trace flags are still
4765 * filtered out.
4766 */
4767 rflags = kvm_get_rflags(vcpu);
Jan Kiszka355be0b2009-10-03 00:31:21 +02004768
4769 vcpu->guest_debug = dbg->control;
4770 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
4771 vcpu->guest_debug = 0;
4772
4773 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004774 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4775 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4776 vcpu->arch.switch_db_regs =
4777 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4778 } else {
4779 for (i = 0; i < KVM_NR_DB_REGS; i++)
4780 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4781 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4782 }
4783
Jan Kiszka94fe45d2009-10-18 13:24:44 +02004784 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
4785 vcpu->arch.singlestep_cs =
4786 get_segment_selector(vcpu, VCPU_SREG_CS);
4787 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
4788 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004789
Jan Kiszka91586a32009-10-05 13:07:21 +02004790 /*
4791 * Trigger an rflags update that will inject or remove the trace
4792 * flags.
4793 */
4794 kvm_set_rflags(vcpu, rflags);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004795
Jan Kiszka355be0b2009-10-03 00:31:21 +02004796 kvm_x86_ops->set_guest_debug(vcpu, dbg);
4797
Jan Kiszka4f926bf22009-10-30 12:46:59 +01004798 r = 0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004799
Jan Kiszka4f926bf22009-10-30 12:46:59 +01004800unlock_out:
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004801 vcpu_put(vcpu);
4802
4803 return r;
4804}
4805
4806/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05004807 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4808 * we have asm/x86/processor.h
4809 */
4810struct fxsave {
4811 u16 cwd;
4812 u16 swd;
4813 u16 twd;
4814 u16 fop;
4815 u64 rip;
4816 u64 rdp;
4817 u32 mxcsr;
4818 u32 mxcsr_mask;
4819 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4820#ifdef CONFIG_X86_64
4821 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4822#else
4823 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4824#endif
4825};
4826
Zhang Xiantao8b006792007-11-16 13:05:55 +08004827/*
4828 * Translate a guest virtual address to a guest physical address.
4829 */
4830int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4831 struct kvm_translation *tr)
4832{
4833 unsigned long vaddr = tr->linear_address;
4834 gpa_t gpa;
4835
4836 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004837 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004838 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004839 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004840 tr->physical_address = gpa;
4841 tr->valid = gpa != UNMAPPED_GVA;
4842 tr->writeable = 1;
4843 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004844 vcpu_put(vcpu);
4845
4846 return 0;
4847}
4848
Hollis Blanchardd0752062007-10-31 17:24:25 -05004849int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4850{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004851 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004852
4853 vcpu_load(vcpu);
4854
4855 memcpy(fpu->fpr, fxsave->st_space, 128);
4856 fpu->fcw = fxsave->cwd;
4857 fpu->fsw = fxsave->swd;
4858 fpu->ftwx = fxsave->twd;
4859 fpu->last_opcode = fxsave->fop;
4860 fpu->last_ip = fxsave->rip;
4861 fpu->last_dp = fxsave->rdp;
4862 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4863
4864 vcpu_put(vcpu);
4865
4866 return 0;
4867}
4868
4869int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4870{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004871 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004872
4873 vcpu_load(vcpu);
4874
4875 memcpy(fxsave->st_space, fpu->fpr, 128);
4876 fxsave->cwd = fpu->fcw;
4877 fxsave->swd = fpu->fsw;
4878 fxsave->twd = fpu->ftwx;
4879 fxsave->fop = fpu->last_opcode;
4880 fxsave->rip = fpu->last_ip;
4881 fxsave->rdp = fpu->last_dp;
4882 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4883
4884 vcpu_put(vcpu);
4885
4886 return 0;
4887}
4888
4889void fx_init(struct kvm_vcpu *vcpu)
4890{
4891 unsigned after_mxcsr_mask;
4892
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004893 /*
4894 * Touch the fpu the first time in non atomic context as if
4895 * this is the first fpu instruction the exception handler
4896 * will fire before the instruction returns and it'll have to
4897 * allocate ram with GFP_KERNEL.
4898 */
4899 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004900 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004901
Hollis Blanchardd0752062007-10-31 17:24:25 -05004902 /* Initialize guest FPU by resetting ours and saving into guest's */
4903 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004904 kvm_fx_save(&vcpu->arch.host_fx_image);
4905 kvm_fx_finit();
4906 kvm_fx_save(&vcpu->arch.guest_fx_image);
4907 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004908 preempt_enable();
4909
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004910 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004911 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004912 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4913 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004914 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4915}
4916EXPORT_SYMBOL_GPL(fx_init);
4917
4918void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4919{
4920 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4921 return;
4922
4923 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004924 kvm_fx_save(&vcpu->arch.host_fx_image);
4925 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004926}
4927EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4928
4929void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4930{
4931 if (!vcpu->guest_fpu_loaded)
4932 return;
4933
4934 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004935 kvm_fx_save(&vcpu->arch.guest_fx_image);
4936 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004937 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004938}
4939EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004940
4941void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4942{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01004943 if (vcpu->arch.time_page) {
4944 kvm_release_page_dirty(vcpu->arch.time_page);
4945 vcpu->arch.time_page = NULL;
4946 }
4947
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004948 kvm_x86_ops->vcpu_free(vcpu);
4949}
4950
4951struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4952 unsigned int id)
4953{
Avi Kivity26e52152007-11-20 15:30:24 +02004954 return kvm_x86_ops->vcpu_create(kvm, id);
4955}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004956
Avi Kivity26e52152007-11-20 15:30:24 +02004957int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4958{
4959 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004960
4961 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004962 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004963
Sheng Yang0bed3b52008-10-09 16:01:54 +08004964 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004965 vcpu_load(vcpu);
4966 r = kvm_arch_vcpu_reset(vcpu);
4967 if (r == 0)
4968 r = kvm_mmu_setup(vcpu);
4969 vcpu_put(vcpu);
4970 if (r < 0)
4971 goto free_vcpu;
4972
Avi Kivity26e52152007-11-20 15:30:24 +02004973 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004974free_vcpu:
4975 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004976 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004977}
4978
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004979void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004980{
4981 vcpu_load(vcpu);
4982 kvm_mmu_unload(vcpu);
4983 vcpu_put(vcpu);
4984
4985 kvm_x86_ops->vcpu_free(vcpu);
4986}
4987
4988int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4989{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004990 vcpu->arch.nmi_pending = false;
4991 vcpu->arch.nmi_injected = false;
4992
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004993 vcpu->arch.switch_db_regs = 0;
4994 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4995 vcpu->arch.dr6 = DR6_FIXED_1;
4996 vcpu->arch.dr7 = DR7_FIXED_1;
4997
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004998 return kvm_x86_ops->vcpu_reset(vcpu);
4999}
5000
Alexander Graf10474ae2009-09-15 11:37:46 +02005001int kvm_arch_hardware_enable(void *garbage)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005002{
Zachary Amsden0cca7902009-09-29 11:38:35 -10005003 /*
5004 * Since this may be called from a hotplug notifcation,
5005 * we can't get the CPU frequency directly.
5006 */
5007 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5008 int cpu = raw_smp_processor_id();
5009 per_cpu(cpu_tsc_khz, cpu) = 0;
5010 }
Avi Kivity18863bd2009-09-07 11:12:18 +03005011
5012 kvm_shared_msr_cpu_online();
5013
Alexander Graf10474ae2009-09-15 11:37:46 +02005014 return kvm_x86_ops->hardware_enable(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005015}
5016
5017void kvm_arch_hardware_disable(void *garbage)
5018{
5019 kvm_x86_ops->hardware_disable(garbage);
Avi Kivity3548bab2009-11-28 14:18:47 +02005020 drop_user_return_notifiers(garbage);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005021}
5022
5023int kvm_arch_hardware_setup(void)
5024{
5025 return kvm_x86_ops->hardware_setup();
5026}
5027
5028void kvm_arch_hardware_unsetup(void)
5029{
5030 kvm_x86_ops->hardware_unsetup();
5031}
5032
5033void kvm_arch_check_processor_compat(void *rtn)
5034{
5035 kvm_x86_ops->check_processor_compatibility(rtn);
5036}
5037
5038int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5039{
5040 struct page *page;
5041 struct kvm *kvm;
5042 int r;
5043
5044 BUG_ON(vcpu->kvm == NULL);
5045 kvm = vcpu->kvm;
5046
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005047 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03005048 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03005049 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005050 else
Avi Kivitya4535292008-04-13 17:54:35 +03005051 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005052
5053 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5054 if (!page) {
5055 r = -ENOMEM;
5056 goto fail;
5057 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005058 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005059
5060 r = kvm_mmu_create(vcpu);
5061 if (r < 0)
5062 goto fail_free_pio_data;
5063
5064 if (irqchip_in_kernel(kvm)) {
5065 r = kvm_create_lapic(vcpu);
5066 if (r < 0)
5067 goto fail_mmu_destroy;
5068 }
5069
Huang Ying890ca9a2009-05-11 16:48:15 +08005070 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5071 GFP_KERNEL);
5072 if (!vcpu->arch.mce_banks) {
5073 r = -ENOMEM;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005074 goto fail_free_lapic;
Huang Ying890ca9a2009-05-11 16:48:15 +08005075 }
5076 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5077
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005078 return 0;
Wei Yongjun443c39b2010-01-22 14:21:29 +08005079fail_free_lapic:
5080 kvm_free_lapic(vcpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005081fail_mmu_destroy:
5082 kvm_mmu_destroy(vcpu);
5083fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005084 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005085fail:
5086 return r;
5087}
5088
5089void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5090{
Wei Yongjun36cb93f2010-01-22 14:18:47 +08005091 kfree(vcpu->arch.mce_banks);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005092 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005093 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005094 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03005095 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005096 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08005097}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005098
5099struct kvm *kvm_arch_create_vm(void)
5100{
5101 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5102
5103 if (!kvm)
5104 return ERR_PTR(-ENOMEM);
5105
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005106 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03005107 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005108
Sheng Yang5550af42008-10-15 20:15:06 +08005109 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5110 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5111
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01005112 rdtscll(kvm->arch.vm_init_tsc);
5113
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005114 return kvm;
5115}
5116
5117static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5118{
5119 vcpu_load(vcpu);
5120 kvm_mmu_unload(vcpu);
5121 vcpu_put(vcpu);
5122}
5123
5124static void kvm_free_vcpus(struct kvm *kvm)
5125{
5126 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005127 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005128
5129 /*
5130 * Unpin any mmu pages first.
5131 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005132 kvm_for_each_vcpu(i, vcpu, kvm)
5133 kvm_unload_vcpu_mmu(vcpu);
5134 kvm_for_each_vcpu(i, vcpu, kvm)
5135 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005136
Gleb Natapov988a2ca2009-06-09 15:56:29 +03005137 mutex_lock(&kvm->lock);
5138 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5139 kvm->vcpus[i] = NULL;
5140
5141 atomic_set(&kvm->online_vcpus, 0);
5142 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005143}
5144
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005145void kvm_arch_sync_events(struct kvm *kvm)
5146{
Sheng Yangba4cef32009-01-06 10:03:03 +08005147 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08005148}
5149
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005150void kvm_arch_destroy_vm(struct kvm *kvm)
5151{
Sheng Yang6eb55812008-10-31 12:37:41 +08005152 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08005153 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08005154 kfree(kvm->arch.vpic);
5155 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005156 kvm_free_vcpus(kvm);
5157 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02005158 if (kvm->arch.apic_access_page)
5159 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005160 if (kvm->arch.ept_identity_pagetable)
5161 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08005162 kfree(kvm);
5163}
Zhang Xiantao0de10342007-11-20 16:25:04 +08005164
5165int kvm_arch_set_memory_region(struct kvm *kvm,
5166 struct kvm_userspace_memory_region *mem,
5167 struct kvm_memory_slot old,
5168 int user_alloc)
5169{
5170 int npages = mem->memory_size >> PAGE_SHIFT;
5171 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
5172
5173 /*To keep backward compatibility with older userspace,
5174 *x86 needs to hanlde !user_alloc case.
5175 */
5176 if (!user_alloc) {
5177 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005178 unsigned long userspace_addr;
5179
Izik Eidus72dc67a2008-02-10 18:04:15 +02005180 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005181 userspace_addr = do_mmap(NULL, 0,
5182 npages * PAGE_SIZE,
5183 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03005184 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005185 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005186 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005187
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02005188 if (IS_ERR((void *)userspace_addr))
5189 return PTR_ERR((void *)userspace_addr);
5190
5191 /* set userspace_addr atomically for kvm_hva_to_rmapp */
5192 spin_lock(&kvm->mmu_lock);
5193 memslot->userspace_addr = userspace_addr;
5194 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005195 } else {
5196 if (!old.user_alloc && old.rmap) {
5197 int ret;
5198
Izik Eidus72dc67a2008-02-10 18:04:15 +02005199 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005200 ret = do_munmap(current->mm, old.userspace_addr,
5201 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02005202 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005203 if (ret < 0)
5204 printk(KERN_WARNING
5205 "kvm_vm_ioctl_set_memory_region: "
5206 "failed to munmap memory\n");
5207 }
5208 }
5209 }
5210
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005211 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08005212 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08005213 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5214 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5215 }
5216
5217 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03005218 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08005219
5220 return 0;
5221}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005222
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005223void kvm_arch_flush_shadow(struct kvm *kvm)
5224{
5225 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03005226 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03005227}
5228
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005229int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5230{
Avi Kivitya4535292008-04-13 17:54:35 +03005231 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03005232 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5233 || vcpu->arch.nmi_pending ||
5234 (kvm_arch_interrupt_allowed(vcpu) &&
5235 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08005236}
Zhang Xiantao57361992007-12-17 14:21:40 +08005237
Zhang Xiantao57361992007-12-17 14:21:40 +08005238void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5239{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005240 int me;
5241 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08005242
5243 if (waitqueue_active(&vcpu->wq)) {
5244 wake_up_interruptible(&vcpu->wq);
5245 ++vcpu->stat.halt_wakeup;
5246 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03005247
5248 me = get_cpu();
5249 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5250 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5251 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03005252 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08005253}
Gleb Natapov78646122009-03-23 12:12:11 +02005254
5255int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5256{
5257 return kvm_x86_ops->interrupt_allowed(vcpu);
5258}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005259
Jan Kiszka94fe45d2009-10-18 13:24:44 +02005260unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5261{
5262 unsigned long rflags;
5263
5264 rflags = kvm_x86_ops->get_rflags(vcpu);
5265 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5266 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5267 return rflags;
5268}
5269EXPORT_SYMBOL_GPL(kvm_get_rflags);
5270
5271void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5272{
5273 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5274 vcpu->arch.singlestep_cs ==
5275 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5276 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5277 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5278 kvm_x86_ops->set_rflags(vcpu, rflags);
5279}
5280EXPORT_SYMBOL_GPL(kvm_set_rflags);
5281
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005282EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5283EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5284EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5285EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5286EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
Joerg Roedel0ac406d2009-10-09 16:08:27 +02005287EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02005288EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
Joerg Roedel17897f32009-10-09 16:08:29 +02005289EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
Joerg Roedel236649d2009-10-09 16:08:30 +02005290EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
Joerg Roedelec1ff792009-10-09 16:08:31 +02005291EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
Joerg Roedel532a46b2009-10-09 16:08:32 +02005292EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);