blob: 0572c90f0c84b6243925f89fd993a23add970f39 [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>
Carsten Otte043405e2007-10-10 17:16:19 +020040
41#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080042#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020043#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080044#include <asm/mtrr.h>
Carsten Otte043405e2007-10-10 17:16:19 +020045
Carsten Otte313a3dc2007-10-11 19:16:52 +020046#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010047#define CR0_RESERVED_BITS \
48 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
49 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
50 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
51#define CR4_RESERVED_BITS \
52 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
53 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
54 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
55 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
56
57#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Joerg Roedel50a37eb2008-01-31 14:57:38 +010058/* EFER defaults:
59 * - enable syscall per default because its emulated by KVM
60 * - enable LME and LMA per default on 64 bit KVM
61 */
62#ifdef CONFIG_X86_64
63static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
64#else
65static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
66#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020067
Avi Kivityba1389b2007-11-18 16:24:12 +020068#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
69#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050070
Avi Kivity674eea02008-02-11 18:37:23 +020071static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
72 struct kvm_cpuid_entry2 __user *entries);
Alexander Grafd8017472008-11-25 20:17:11 +010073struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
74 u32 function, u32 index);
Avi Kivity674eea02008-02-11 18:37:23 +020075
Zhang Xiantao97896d02007-11-14 20:09:30 +080076struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030077EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080078
Hollis Blanchard417bc302007-10-31 17:24:23 -050079struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020080 { "pf_fixed", VCPU_STAT(pf_fixed) },
81 { "pf_guest", VCPU_STAT(pf_guest) },
82 { "tlb_flush", VCPU_STAT(tlb_flush) },
83 { "invlpg", VCPU_STAT(invlpg) },
84 { "exits", VCPU_STAT(exits) },
85 { "io_exits", VCPU_STAT(io_exits) },
86 { "mmio_exits", VCPU_STAT(mmio_exits) },
87 { "signal_exits", VCPU_STAT(signal_exits) },
88 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +080089 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020090 { "halt_exits", VCPU_STAT(halt_exits) },
91 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +053092 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020093 { "request_irq", VCPU_STAT(request_irq_exits) },
94 { "irq_exits", VCPU_STAT(irq_exits) },
95 { "host_state_reload", VCPU_STAT(host_state_reload) },
96 { "efer_reload", VCPU_STAT(efer_reload) },
97 { "fpu_reload", VCPU_STAT(fpu_reload) },
98 { "insn_emulation", VCPU_STAT(insn_emulation) },
99 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300100 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200101 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200102 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
103 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
104 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
105 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
106 { "mmu_flooded", VM_STAT(mmu_flooded) },
107 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200108 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300109 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200110 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300111 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500112 { NULL }
113};
114
Carsten Otte5fb76f92007-10-29 16:08:51 +0100115unsigned long segment_base(u16 selector)
116{
117 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200118 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100119 unsigned long table_base;
120 unsigned long v;
121
122 if (selector == 0)
123 return 0;
124
125 asm("sgdt %0" : "=m"(gdt));
126 table_base = gdt.base;
127
128 if (selector & 4) { /* from ldt */
129 u16 ldt_selector;
130
131 asm("sldt %0" : "=g"(ldt_selector));
132 table_base = segment_base(ldt_selector);
133 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200134 d = (struct desc_struct *)(table_base + (selector & ~7));
135 v = d->base0 | ((unsigned long)d->base1 << 16) |
136 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100137#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200138 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
139 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100140#endif
141 return v;
142}
143EXPORT_SYMBOL_GPL(segment_base);
144
Carsten Otte6866b832007-10-29 16:09:10 +0100145u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
146{
147 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800148 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100149 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800150 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100151}
152EXPORT_SYMBOL_GPL(kvm_get_apic_base);
153
154void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
155{
156 /* TODO: reserve bits check */
157 if (irqchip_in_kernel(vcpu->kvm))
158 kvm_lapic_set_base(vcpu, data);
159 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800160 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100161}
162EXPORT_SYMBOL_GPL(kvm_set_apic_base);
163
Avi Kivity298101d2007-11-25 13:41:11 +0200164void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
165{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800166 WARN_ON(vcpu->arch.exception.pending);
167 vcpu->arch.exception.pending = true;
168 vcpu->arch.exception.has_error_code = false;
169 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200170}
171EXPORT_SYMBOL_GPL(kvm_queue_exception);
172
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200173void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
174 u32 error_code)
175{
176 ++vcpu->stat.pf_guest;
Alexander Grafd8017472008-11-25 20:17:11 +0100177
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100178 if (vcpu->arch.exception.pending) {
179 if (vcpu->arch.exception.nr == PF_VECTOR) {
180 printk(KERN_DEBUG "kvm: inject_page_fault:"
181 " double fault 0x%lx\n", addr);
182 vcpu->arch.exception.nr = DF_VECTOR;
183 vcpu->arch.exception.error_code = 0;
184 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
185 /* triple fault -> shutdown */
186 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
187 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200188 return;
189 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800190 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200191 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
192}
193
Sheng Yang3419ffc2008-05-15 09:52:48 +0800194void kvm_inject_nmi(struct kvm_vcpu *vcpu)
195{
196 vcpu->arch.nmi_pending = 1;
197}
198EXPORT_SYMBOL_GPL(kvm_inject_nmi);
199
Avi Kivity298101d2007-11-25 13:41:11 +0200200void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
201{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800202 WARN_ON(vcpu->arch.exception.pending);
203 vcpu->arch.exception.pending = true;
204 vcpu->arch.exception.has_error_code = true;
205 vcpu->arch.exception.nr = nr;
206 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200207}
208EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
209
210static void __queue_exception(struct kvm_vcpu *vcpu)
211{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800212 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
213 vcpu->arch.exception.has_error_code,
214 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200215}
216
Carsten Ottea03490e2007-10-29 16:09:35 +0100217/*
218 * Load the pae pdptrs. Return true is they are all valid.
219 */
220int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
221{
222 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
223 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
224 int i;
225 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800226 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100227
Carsten Ottea03490e2007-10-29 16:09:35 +0100228 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
229 offset * sizeof(u64), sizeof(pdpte));
230 if (ret < 0) {
231 ret = 0;
232 goto out;
233 }
234 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Dong, Eddie20c466b2009-03-31 23:03:45 +0800235 if (is_present_pte(pdpte[i]) &&
236 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100237 ret = 0;
238 goto out;
239 }
240 }
241 ret = 1;
242
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100244out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100245
246 return ret;
247}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100248EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100249
Avi Kivityd835dfe2007-11-21 02:57:59 +0200250static bool pdptrs_changed(struct kvm_vcpu *vcpu)
251{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800252 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200253 bool changed = true;
254 int r;
255
256 if (is_long_mode(vcpu) || !is_pae(vcpu))
257 return false;
258
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800259 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200260 if (r < 0)
261 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800262 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200263out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200264
265 return changed;
266}
267
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200268void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100269{
270 if (cr0 & CR0_RESERVED_BITS) {
271 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800272 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200273 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100274 return;
275 }
276
277 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
278 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200279 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100280 return;
281 }
282
283 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
284 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
285 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200286 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100287 return;
288 }
289
290 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
291#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800292 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100293 int cs_db, cs_l;
294
295 if (!is_pae(vcpu)) {
296 printk(KERN_DEBUG "set_cr0: #GP, start paging "
297 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200298 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100299 return;
300 }
301 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
302 if (cs_l) {
303 printk(KERN_DEBUG "set_cr0: #GP, start paging "
304 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200305 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100306 return;
307
308 }
309 } else
310#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800311 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100312 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
313 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200314 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100315 return;
316 }
317
318 }
319
320 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800321 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100322
Carsten Ottea03490e2007-10-29 16:09:35 +0100323 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100324 return;
325}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200326EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100327
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200328void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100329{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200330 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -0400331 KVMTRACE_1D(LMSW, vcpu,
332 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
333 handler);
Carsten Ottea03490e2007-10-29 16:09:35 +0100334}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200335EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100336
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200337void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100338{
Avi Kivitya2edf572009-05-24 22:19:00 +0300339 unsigned long old_cr4 = vcpu->arch.cr4;
340 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
341
Carsten Ottea03490e2007-10-29 16:09:35 +0100342 if (cr4 & CR4_RESERVED_BITS) {
343 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200344 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100345 return;
346 }
347
348 if (is_long_mode(vcpu)) {
349 if (!(cr4 & X86_CR4_PAE)) {
350 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
351 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200352 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100353 return;
354 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300355 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
356 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800357 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100358 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200359 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100360 return;
361 }
362
363 if (cr4 & X86_CR4_VMXE) {
364 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200365 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100366 return;
367 }
368 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200370 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100371 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100372}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200373EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100374
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200375void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800377 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300378 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200379 kvm_mmu_flush_tlb(vcpu);
380 return;
381 }
382
Carsten Ottea03490e2007-10-29 16:09:35 +0100383 if (is_long_mode(vcpu)) {
384 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
385 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200386 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100387 return;
388 }
389 } else {
390 if (is_pae(vcpu)) {
391 if (cr3 & CR3_PAE_RESERVED_BITS) {
392 printk(KERN_DEBUG
393 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200394 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100395 return;
396 }
397 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
398 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
399 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200400 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100401 return;
402 }
403 }
404 /*
405 * We don't check reserved bits in nonpae mode, because
406 * this isn't enforced, and VMware depends on this.
407 */
408 }
409
Carsten Ottea03490e2007-10-29 16:09:35 +0100410 /*
411 * Does the new cr3 value map to physical memory? (Note, we
412 * catch an invalid cr3 even in real-mode, because it would
413 * cause trouble later on when we turn on paging anyway.)
414 *
415 * A real CPU would silently accept an invalid cr3 and would
416 * attempt to use it - with largely undefined (and often hard
417 * to debug) behavior on the guest side.
418 */
419 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200420 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100421 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800422 vcpu->arch.cr3 = cr3;
423 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100424 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100425}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200426EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100427
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200428void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100429{
430 if (cr8 & CR8_RESERVED_BITS) {
431 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200432 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100433 return;
434 }
435 if (irqchip_in_kernel(vcpu->kvm))
436 kvm_lapic_set_tpr(vcpu, cr8);
437 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800438 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100439}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200440EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100441
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200442unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100443{
444 if (irqchip_in_kernel(vcpu->kvm))
445 return kvm_lapic_get_cr8(vcpu);
446 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800447 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100448}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200449EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100450
Alexander Grafd8017472008-11-25 20:17:11 +0100451static inline u32 bit(int bitno)
452{
453 return 1 << (bitno & 31);
454}
455
Carsten Otte043405e2007-10-10 17:16:19 +0200456/*
457 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
458 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
459 *
460 * This list is modified at module load time to reflect the
461 * capabilities of the host cpu.
462 */
463static u32 msrs_to_save[] = {
464 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
465 MSR_K6_STAR,
466#ifdef CONFIG_X86_64
467 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
468#endif
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200469 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Alexander Grafb286d5d2008-11-25 20:17:05 +0100470 MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200471};
472
473static unsigned num_msrs_to_save;
474
475static u32 emulated_msrs[] = {
476 MSR_IA32_MISC_ENABLE,
477};
478
Carsten Otte15c4a642007-10-30 18:44:17 +0100479static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
480{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100481 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100482 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
483 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200484 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100485 return;
486 }
487
488 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800489 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100490 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200491 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100492 return;
493 }
494
Alexander Graf1b2fd702009-02-02 16:23:51 +0100495 if (efer & EFER_FFXSR) {
496 struct kvm_cpuid_entry2 *feat;
497
498 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
499 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
500 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
501 kvm_inject_gp(vcpu, 0);
502 return;
503 }
504 }
505
Alexander Grafd8017472008-11-25 20:17:11 +0100506 if (efer & EFER_SVME) {
507 struct kvm_cpuid_entry2 *feat;
508
509 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
510 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
511 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
512 kvm_inject_gp(vcpu, 0);
513 return;
514 }
515 }
516
Carsten Otte15c4a642007-10-30 18:44:17 +0100517 kvm_x86_ops->set_efer(vcpu, efer);
518
519 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800520 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100521
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800522 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300523
524 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
525 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100526}
527
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100528void kvm_enable_efer_bits(u64 mask)
529{
530 efer_reserved_bits &= ~mask;
531}
532EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
533
534
Carsten Otte15c4a642007-10-30 18:44:17 +0100535/*
536 * Writes msr value into into the appropriate "register".
537 * Returns 0 on success, non-0 otherwise.
538 * Assumes vcpu_load() was already called.
539 */
540int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
541{
542 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
543}
544
Carsten Otte313a3dc2007-10-11 19:16:52 +0200545/*
546 * Adapt set_msr() to msr_io()'s calling convention
547 */
548static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
549{
550 return kvm_set_msr(vcpu, index, *data);
551}
552
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200553static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
554{
555 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200556 struct pvclock_wall_clock wc;
557 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200558
559 if (!wall_clock)
560 return;
561
562 version++;
563
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200564 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
565
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200566 /*
567 * The guest calculates current wall clock time by adding
568 * system time (updated by kvm_write_guest_time below) to the
569 * wall clock specified here. guest system time equals host
570 * system time for us, thus we must fill in host boot time here.
571 */
572 now = current_kernel_time();
573 ktime_get_ts(&sys);
574 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
575
576 wc.sec = boot.tv_sec;
577 wc.nsec = boot.tv_nsec;
578 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200579
580 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
581
582 version++;
583 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200584}
585
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200586static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
587{
588 uint32_t quotient, remainder;
589
590 /* Don't try to replace with do_div(), this one calculates
591 * "(dividend << 32) / divisor" */
592 __asm__ ( "divl %4"
593 : "=a" (quotient), "=d" (remainder)
594 : "0" (0), "1" (dividend), "r" (divisor) );
595 return quotient;
596}
597
598static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
599{
600 uint64_t nsecs = 1000000000LL;
601 int32_t shift = 0;
602 uint64_t tps64;
603 uint32_t tps32;
604
605 tps64 = tsc_khz * 1000LL;
606 while (tps64 > nsecs*2) {
607 tps64 >>= 1;
608 shift--;
609 }
610
611 tps32 = (uint32_t)tps64;
612 while (tps32 <= (uint32_t)nsecs) {
613 tps32 <<= 1;
614 shift++;
615 }
616
617 hv_clock->tsc_shift = shift;
618 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
619
620 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700621 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200622 hv_clock->tsc_to_system_mul);
623}
624
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100625static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
626
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200627static void kvm_write_guest_time(struct kvm_vcpu *v)
628{
629 struct timespec ts;
630 unsigned long flags;
631 struct kvm_vcpu_arch *vcpu = &v->arch;
632 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300633 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200634
635 if ((!vcpu->time_page))
636 return;
637
Avi Kivity463656c2009-04-12 15:49:07 +0300638 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
639 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
640 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
641 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200642 }
Avi Kivity463656c2009-04-12 15:49:07 +0300643 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200644
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200645 /* Keep irq disabled to prevent changes to the clock */
646 local_irq_save(flags);
647 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
648 &vcpu->hv_clock.tsc_timestamp);
649 ktime_get_ts(&ts);
650 local_irq_restore(flags);
651
652 /* With all the info we got, fill in the values */
653
654 vcpu->hv_clock.system_time = ts.tv_nsec +
655 (NSEC_PER_SEC * (u64)ts.tv_sec);
656 /*
657 * The interface expects us to write an even number signaling that the
658 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200659 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200660 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200661 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200662
663 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
664
665 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200666 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200667
668 kunmap_atomic(shared_kaddr, KM_USER0);
669
670 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
671}
672
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100673static int kvm_request_guest_time_update(struct kvm_vcpu *v)
674{
675 struct kvm_vcpu_arch *vcpu = &v->arch;
676
677 if (!vcpu->time_page)
678 return 0;
679 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
680 return 1;
681}
682
Avi Kivity9ba075a2008-05-26 20:06:35 +0300683static bool msr_mtrr_valid(unsigned msr)
684{
685 switch (msr) {
686 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
687 case MSR_MTRRfix64K_00000:
688 case MSR_MTRRfix16K_80000:
689 case MSR_MTRRfix16K_A0000:
690 case MSR_MTRRfix4K_C0000:
691 case MSR_MTRRfix4K_C8000:
692 case MSR_MTRRfix4K_D0000:
693 case MSR_MTRRfix4K_D8000:
694 case MSR_MTRRfix4K_E0000:
695 case MSR_MTRRfix4K_E8000:
696 case MSR_MTRRfix4K_F0000:
697 case MSR_MTRRfix4K_F8000:
698 case MSR_MTRRdefType:
699 case MSR_IA32_CR_PAT:
700 return true;
701 case 0x2f8:
702 return true;
703 }
704 return false;
705}
706
707static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
708{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800709 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
710
Avi Kivity9ba075a2008-05-26 20:06:35 +0300711 if (!msr_mtrr_valid(msr))
712 return 1;
713
Sheng Yang0bed3b52008-10-09 16:01:54 +0800714 if (msr == MSR_MTRRdefType) {
715 vcpu->arch.mtrr_state.def_type = data;
716 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
717 } else if (msr == MSR_MTRRfix64K_00000)
718 p[0] = data;
719 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
720 p[1 + msr - MSR_MTRRfix16K_80000] = data;
721 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
722 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
723 else if (msr == MSR_IA32_CR_PAT)
724 vcpu->arch.pat = data;
725 else { /* Variable MTRRs */
726 int idx, is_mtrr_mask;
727 u64 *pt;
728
729 idx = (msr - 0x200) / 2;
730 is_mtrr_mask = msr - 0x200 - 2 * idx;
731 if (!is_mtrr_mask)
732 pt =
733 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
734 else
735 pt =
736 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
737 *pt = data;
738 }
739
740 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300741 return 0;
742}
Carsten Otte15c4a642007-10-30 18:44:17 +0100743
744int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
745{
746 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100747 case MSR_EFER:
748 set_efer(vcpu, data);
749 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100750 case MSR_IA32_MC0_STATUS:
751 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800752 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100753 break;
754 case MSR_IA32_MCG_STATUS:
755 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800756 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100757 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100758 case MSR_IA32_MCG_CTL:
759 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800760 __func__, data);
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100761 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200762 case MSR_IA32_DEBUGCTLMSR:
763 if (!data) {
764 /* We support the non-activated case already */
765 break;
766 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
767 /* Values other than LBR and BTF are vendor-specific,
768 thus reserved and should throw a #GP */
769 return 1;
770 }
771 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
772 __func__, data);
773 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100774 case MSR_IA32_UCODE_REV:
775 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200776 case MSR_VM_HSAVE_PA:
Carsten Otte15c4a642007-10-30 18:44:17 +0100777 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300778 case 0x200 ... 0x2ff:
779 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100780 case MSR_IA32_APICBASE:
781 kvm_set_apic_base(vcpu, data);
782 break;
783 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800784 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100785 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200786 case MSR_KVM_WALL_CLOCK:
787 vcpu->kvm->arch.wall_clock = data;
788 kvm_write_wall_clock(vcpu->kvm, data);
789 break;
790 case MSR_KVM_SYSTEM_TIME: {
791 if (vcpu->arch.time_page) {
792 kvm_release_page_dirty(vcpu->arch.time_page);
793 vcpu->arch.time_page = NULL;
794 }
795
796 vcpu->arch.time = data;
797
798 /* we verify if the enable bit is set... */
799 if (!(data & 1))
800 break;
801
802 /* ...but clean it before doing the actual write */
803 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
804
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200805 vcpu->arch.time_page =
806 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200807
808 if (is_error_page(vcpu->arch.time_page)) {
809 kvm_release_page_clean(vcpu->arch.time_page);
810 vcpu->arch.time_page = NULL;
811 }
812
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100813 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200814 break;
815 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100816 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200817 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100818 return 1;
819 }
820 return 0;
821}
822EXPORT_SYMBOL_GPL(kvm_set_msr_common);
823
824
825/*
826 * Reads an msr value (of 'msr_index') into 'pdata'.
827 * Returns 0 on success, non-0 otherwise.
828 * Assumes vcpu_load() was already called.
829 */
830int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
831{
832 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
833}
834
Avi Kivity9ba075a2008-05-26 20:06:35 +0300835static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
836{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800837 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
838
Avi Kivity9ba075a2008-05-26 20:06:35 +0300839 if (!msr_mtrr_valid(msr))
840 return 1;
841
Sheng Yang0bed3b52008-10-09 16:01:54 +0800842 if (msr == MSR_MTRRdefType)
843 *pdata = vcpu->arch.mtrr_state.def_type +
844 (vcpu->arch.mtrr_state.enabled << 10);
845 else if (msr == MSR_MTRRfix64K_00000)
846 *pdata = p[0];
847 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
848 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
849 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
850 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
851 else if (msr == MSR_IA32_CR_PAT)
852 *pdata = vcpu->arch.pat;
853 else { /* Variable MTRRs */
854 int idx, is_mtrr_mask;
855 u64 *pt;
856
857 idx = (msr - 0x200) / 2;
858 is_mtrr_mask = msr - 0x200 - 2 * idx;
859 if (!is_mtrr_mask)
860 pt =
861 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
862 else
863 pt =
864 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
865 *pdata = *pt;
866 }
867
Avi Kivity9ba075a2008-05-26 20:06:35 +0300868 return 0;
869}
870
Carsten Otte15c4a642007-10-30 18:44:17 +0100871int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
872{
873 u64 data;
874
875 switch (msr) {
876 case 0xc0010010: /* SYSCFG */
877 case 0xc0010015: /* HWCR */
878 case MSR_IA32_PLATFORM_ID:
879 case MSR_IA32_P5_MC_ADDR:
880 case MSR_IA32_P5_MC_TYPE:
881 case MSR_IA32_MC0_CTL:
882 case MSR_IA32_MCG_STATUS:
883 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100884 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100885 case MSR_IA32_MC0_MISC:
886 case MSR_IA32_MC0_MISC+4:
887 case MSR_IA32_MC0_MISC+8:
888 case MSR_IA32_MC0_MISC+12:
889 case MSR_IA32_MC0_MISC+16:
Joerg Roedela89c1ad2008-08-29 11:52:07 +0200890 case MSR_IA32_MC0_MISC+20:
Carsten Otte15c4a642007-10-30 18:44:17 +0100891 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100892 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200893 case MSR_IA32_DEBUGCTLMSR:
894 case MSR_IA32_LASTBRANCHFROMIP:
895 case MSR_IA32_LASTBRANCHTOIP:
896 case MSR_IA32_LASTINTFROMIP:
897 case MSR_IA32_LASTINTTOIP:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200898 case MSR_VM_HSAVE_PA:
Amit Shah7fe29e02009-03-20 12:39:00 +0530899 case MSR_P6_EVNTSEL0:
900 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +0530901 case MSR_K7_EVNTSEL0:
Carsten Otte15c4a642007-10-30 18:44:17 +0100902 data = 0;
903 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300904 case MSR_MTRRcap:
905 data = 0x500 | KVM_NR_VAR_MTRR;
906 break;
907 case 0x200 ... 0x2ff:
908 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +0100909 case 0xcd: /* fsb frequency */
910 data = 3;
911 break;
912 case MSR_IA32_APICBASE:
913 data = kvm_get_apic_base(vcpu);
914 break;
915 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800916 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100917 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +0100918 case MSR_IA32_PERF_STATUS:
919 /* TSC increment by tick */
920 data = 1000ULL;
921 /* CPU multiplier */
922 data |= (((uint64_t)4ULL) << 40);
923 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100924 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800925 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100926 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200927 case MSR_KVM_WALL_CLOCK:
928 data = vcpu->kvm->arch.wall_clock;
929 break;
930 case MSR_KVM_SYSTEM_TIME:
931 data = vcpu->arch.time;
932 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100933 default:
934 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
935 return 1;
936 }
937 *pdata = data;
938 return 0;
939}
940EXPORT_SYMBOL_GPL(kvm_get_msr_common);
941
Carsten Otte313a3dc2007-10-11 19:16:52 +0200942/*
943 * Read or write a bunch of msrs. All parameters are kernel addresses.
944 *
945 * @return number of msrs set successfully.
946 */
947static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
948 struct kvm_msr_entry *entries,
949 int (*do_msr)(struct kvm_vcpu *vcpu,
950 unsigned index, u64 *data))
951{
952 int i;
953
954 vcpu_load(vcpu);
955
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300956 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200957 for (i = 0; i < msrs->nmsrs; ++i)
958 if (do_msr(vcpu, entries[i].index, &entries[i].data))
959 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300960 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200961
962 vcpu_put(vcpu);
963
964 return i;
965}
966
967/*
968 * Read or write a bunch of msrs. Parameters are user addresses.
969 *
970 * @return number of msrs set successfully.
971 */
972static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
973 int (*do_msr)(struct kvm_vcpu *vcpu,
974 unsigned index, u64 *data),
975 int writeback)
976{
977 struct kvm_msrs msrs;
978 struct kvm_msr_entry *entries;
979 int r, n;
980 unsigned size;
981
982 r = -EFAULT;
983 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
984 goto out;
985
986 r = -E2BIG;
987 if (msrs.nmsrs >= MAX_IO_MSRS)
988 goto out;
989
990 r = -ENOMEM;
991 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
992 entries = vmalloc(size);
993 if (!entries)
994 goto out;
995
996 r = -EFAULT;
997 if (copy_from_user(entries, user_msrs->entries, size))
998 goto out_free;
999
1000 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1001 if (r < 0)
1002 goto out_free;
1003
1004 r = -EFAULT;
1005 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1006 goto out_free;
1007
1008 r = n;
1009
1010out_free:
1011 vfree(entries);
1012out:
1013 return r;
1014}
1015
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001016int kvm_dev_ioctl_check_extension(long ext)
1017{
1018 int r;
1019
1020 switch (ext) {
1021 case KVM_CAP_IRQCHIP:
1022 case KVM_CAP_HLT:
1023 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001024 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001025 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001026 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001027 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001028 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001029 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001030 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001031 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001032 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001033 case KVM_CAP_ASSIGN_DEV_IRQ:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001034 r = 1;
1035 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001036 case KVM_CAP_COALESCED_MMIO:
1037 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1038 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001039 case KVM_CAP_VAPIC:
1040 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1041 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001042 case KVM_CAP_NR_VCPUS:
1043 r = KVM_MAX_VCPUS;
1044 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001045 case KVM_CAP_NR_MEMSLOTS:
1046 r = KVM_MEMORY_SLOTS;
1047 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001048 case KVM_CAP_PV_MMU:
1049 r = !tdp_enabled;
1050 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001051 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001052 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001053 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001054 default:
1055 r = 0;
1056 break;
1057 }
1058 return r;
1059
1060}
1061
Carsten Otte043405e2007-10-10 17:16:19 +02001062long kvm_arch_dev_ioctl(struct file *filp,
1063 unsigned int ioctl, unsigned long arg)
1064{
1065 void __user *argp = (void __user *)arg;
1066 long r;
1067
1068 switch (ioctl) {
1069 case KVM_GET_MSR_INDEX_LIST: {
1070 struct kvm_msr_list __user *user_msr_list = argp;
1071 struct kvm_msr_list msr_list;
1072 unsigned n;
1073
1074 r = -EFAULT;
1075 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1076 goto out;
1077 n = msr_list.nmsrs;
1078 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1079 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1080 goto out;
1081 r = -E2BIG;
1082 if (n < num_msrs_to_save)
1083 goto out;
1084 r = -EFAULT;
1085 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1086 num_msrs_to_save * sizeof(u32)))
1087 goto out;
1088 if (copy_to_user(user_msr_list->indices
1089 + num_msrs_to_save * sizeof(u32),
1090 &emulated_msrs,
1091 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1092 goto out;
1093 r = 0;
1094 break;
1095 }
Avi Kivity674eea02008-02-11 18:37:23 +02001096 case KVM_GET_SUPPORTED_CPUID: {
1097 struct kvm_cpuid2 __user *cpuid_arg = argp;
1098 struct kvm_cpuid2 cpuid;
1099
1100 r = -EFAULT;
1101 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1102 goto out;
1103 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001104 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001105 if (r)
1106 goto out;
1107
1108 r = -EFAULT;
1109 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1110 goto out;
1111 r = 0;
1112 break;
1113 }
Carsten Otte043405e2007-10-10 17:16:19 +02001114 default:
1115 r = -EINVAL;
1116 }
1117out:
1118 return r;
1119}
1120
Carsten Otte313a3dc2007-10-11 19:16:52 +02001121void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1122{
1123 kvm_x86_ops->vcpu_load(vcpu, cpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001124 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001125}
1126
1127void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1128{
1129 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001130 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001131}
1132
Dan Kenigsberg07716712007-11-21 17:10:04 +02001133static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001134{
Avi Kivitye286e862009-05-03 18:50:55 +03001135 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001136
Avi Kivitye286e862009-05-03 18:50:55 +03001137 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001138 return efer & EFER_NX;
1139}
1140
1141static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1142{
1143 int i;
1144 struct kvm_cpuid_entry2 *e, *entry;
1145
Carsten Otte313a3dc2007-10-11 19:16:52 +02001146 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001147 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1148 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001149 if (e->function == 0x80000001) {
1150 entry = e;
1151 break;
1152 }
1153 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001154 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001155 entry->edx &= ~(1 << 20);
1156 printk(KERN_INFO "kvm: guest NX capability removed\n");
1157 }
1158}
1159
Dan Kenigsberg07716712007-11-21 17:10:04 +02001160/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001161static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1162 struct kvm_cpuid *cpuid,
1163 struct kvm_cpuid_entry __user *entries)
1164{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001165 int r, i;
1166 struct kvm_cpuid_entry *cpuid_entries;
1167
1168 r = -E2BIG;
1169 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1170 goto out;
1171 r = -ENOMEM;
1172 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1173 if (!cpuid_entries)
1174 goto out;
1175 r = -EFAULT;
1176 if (copy_from_user(cpuid_entries, entries,
1177 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1178 goto out_free;
1179 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001180 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1181 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1182 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1183 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1184 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1185 vcpu->arch.cpuid_entries[i].index = 0;
1186 vcpu->arch.cpuid_entries[i].flags = 0;
1187 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1188 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1189 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001190 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001191 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001192 cpuid_fix_nx_cap(vcpu);
1193 r = 0;
1194
1195out_free:
1196 vfree(cpuid_entries);
1197out:
1198 return r;
1199}
1200
1201static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001202 struct kvm_cpuid2 *cpuid,
1203 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001204{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001205 int r;
1206
1207 r = -E2BIG;
1208 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1209 goto out;
1210 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001211 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001212 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001213 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001214 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001215 return 0;
1216
1217out:
1218 return r;
1219}
1220
Dan Kenigsberg07716712007-11-21 17:10:04 +02001221static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001222 struct kvm_cpuid2 *cpuid,
1223 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001224{
1225 int r;
1226
1227 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001228 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001229 goto out;
1230 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001231 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001232 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001233 goto out;
1234 return 0;
1235
1236out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001237 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001238 return r;
1239}
1240
Dan Kenigsberg07716712007-11-21 17:10:04 +02001241static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001242 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001243{
1244 entry->function = function;
1245 entry->index = index;
1246 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001247 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001248 entry->flags = 0;
1249}
1250
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001251#define F(x) bit(X86_FEATURE_##x)
1252
Dan Kenigsberg07716712007-11-21 17:10:04 +02001253static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1254 u32 index, int *nent, int maxnent)
1255{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001256 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001257#ifdef CONFIG_X86_64
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001258 unsigned f_lm = F(LM);
1259#else
1260 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001261#endif
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001262
1263 /* cpuid 1.edx */
1264 const u32 kvm_supported_word0_x86_features =
1265 F(FPU) | F(VME) | F(DE) | F(PSE) |
1266 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1267 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1268 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1269 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1270 0 /* Reserved, DS, ACPI */ | F(MMX) |
1271 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1272 0 /* HTT, TM, Reserved, PBE */;
1273 /* cpuid 0x80000001.edx */
1274 const u32 kvm_supported_word1_x86_features =
1275 F(FPU) | F(VME) | F(DE) | F(PSE) |
1276 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1277 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1278 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1279 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1280 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1281 F(FXSR) | F(FXSR_OPT) | 0 /* GBPAGES */ | 0 /* RDTSCP */ |
1282 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1283 /* cpuid 1.ecx */
1284 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001285 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1286 0 /* DS-CPL, VMX, SMX, EST */ |
1287 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1288 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1289 0 /* Reserved, DCA */ | F(XMM4_1) |
1290 F(XMM4_2) | 0 /* x2APIC */ | F(MOVBE) | F(POPCNT) |
1291 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001292 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001293 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001294 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1295 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1296 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1297 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001298
Amit Shah19355472009-01-14 16:56:00 +00001299 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001300 get_cpu();
1301 do_cpuid_1_ent(entry, function, index);
1302 ++*nent;
1303
1304 switch (function) {
1305 case 0:
1306 entry->eax = min(entry->eax, (u32)0xb);
1307 break;
1308 case 1:
1309 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001310 entry->ecx &= kvm_supported_word4_x86_features;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001311 break;
1312 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1313 * may return different values. This forces us to get_cpu() before
1314 * issuing the first command, and also to emulate this annoying behavior
1315 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1316 case 2: {
1317 int t, times = entry->eax & 0xff;
1318
1319 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001320 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001321 for (t = 1; t < times && *nent < maxnent; ++t) {
1322 do_cpuid_1_ent(&entry[t], function, 0);
1323 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1324 ++*nent;
1325 }
1326 break;
1327 }
1328 /* function 4 and 0xb have additional index. */
1329 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001330 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001331
1332 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1333 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001334 for (i = 1; *nent < maxnent; ++i) {
1335 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001336 if (!cache_type)
1337 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001338 do_cpuid_1_ent(&entry[i], function, i);
1339 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001340 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1341 ++*nent;
1342 }
1343 break;
1344 }
1345 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001346 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001347
1348 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1349 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001350 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001351 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001352 if (!level_type)
1353 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001354 do_cpuid_1_ent(&entry[i], function, i);
1355 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001356 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1357 ++*nent;
1358 }
1359 break;
1360 }
1361 case 0x80000000:
1362 entry->eax = min(entry->eax, 0x8000001a);
1363 break;
1364 case 0x80000001:
1365 entry->edx &= kvm_supported_word1_x86_features;
1366 entry->ecx &= kvm_supported_word6_x86_features;
1367 break;
1368 }
1369 put_cpu();
1370}
1371
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001372#undef F
1373
Avi Kivity674eea02008-02-11 18:37:23 +02001374static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001375 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001376{
1377 struct kvm_cpuid_entry2 *cpuid_entries;
1378 int limit, nent = 0, r = -E2BIG;
1379 u32 func;
1380
1381 if (cpuid->nent < 1)
1382 goto out;
1383 r = -ENOMEM;
1384 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1385 if (!cpuid_entries)
1386 goto out;
1387
1388 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1389 limit = cpuid_entries[0].eax;
1390 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1391 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001392 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001393 r = -E2BIG;
1394 if (nent >= cpuid->nent)
1395 goto out_free;
1396
1397 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1398 limit = cpuid_entries[nent - 1].eax;
1399 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1400 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001401 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001402 r = -EFAULT;
1403 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001404 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001405 goto out_free;
1406 cpuid->nent = nent;
1407 r = 0;
1408
1409out_free:
1410 vfree(cpuid_entries);
1411out:
1412 return r;
1413}
1414
Carsten Otte313a3dc2007-10-11 19:16:52 +02001415static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1416 struct kvm_lapic_state *s)
1417{
1418 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001419 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001420 vcpu_put(vcpu);
1421
1422 return 0;
1423}
1424
1425static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1426 struct kvm_lapic_state *s)
1427{
1428 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001429 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001430 kvm_apic_post_state_restore(vcpu);
1431 vcpu_put(vcpu);
1432
1433 return 0;
1434}
1435
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001436static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1437 struct kvm_interrupt *irq)
1438{
1439 if (irq->irq < 0 || irq->irq >= 256)
1440 return -EINVAL;
1441 if (irqchip_in_kernel(vcpu->kvm))
1442 return -ENXIO;
1443 vcpu_load(vcpu);
1444
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001445 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001446
1447 vcpu_put(vcpu);
1448
1449 return 0;
1450}
1451
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001452static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1453{
1454 vcpu_load(vcpu);
1455 kvm_inject_nmi(vcpu);
1456 vcpu_put(vcpu);
1457
1458 return 0;
1459}
1460
Avi Kivityb209749f2007-10-22 16:50:39 +02001461static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1462 struct kvm_tpr_access_ctl *tac)
1463{
1464 if (tac->flags)
1465 return -EINVAL;
1466 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1467 return 0;
1468}
1469
Carsten Otte313a3dc2007-10-11 19:16:52 +02001470long kvm_arch_vcpu_ioctl(struct file *filp,
1471 unsigned int ioctl, unsigned long arg)
1472{
1473 struct kvm_vcpu *vcpu = filp->private_data;
1474 void __user *argp = (void __user *)arg;
1475 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001476 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001477
1478 switch (ioctl) {
1479 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001480 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001481
Dave Hansenb772ff32008-08-11 10:01:47 -07001482 r = -ENOMEM;
1483 if (!lapic)
1484 goto out;
1485 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001486 if (r)
1487 goto out;
1488 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001489 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001490 goto out;
1491 r = 0;
1492 break;
1493 }
1494 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001495 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1496 r = -ENOMEM;
1497 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001498 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001499 r = -EFAULT;
1500 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1501 goto out;
1502 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001503 if (r)
1504 goto out;
1505 r = 0;
1506 break;
1507 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001508 case KVM_INTERRUPT: {
1509 struct kvm_interrupt irq;
1510
1511 r = -EFAULT;
1512 if (copy_from_user(&irq, argp, sizeof irq))
1513 goto out;
1514 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1515 if (r)
1516 goto out;
1517 r = 0;
1518 break;
1519 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001520 case KVM_NMI: {
1521 r = kvm_vcpu_ioctl_nmi(vcpu);
1522 if (r)
1523 goto out;
1524 r = 0;
1525 break;
1526 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001527 case KVM_SET_CPUID: {
1528 struct kvm_cpuid __user *cpuid_arg = argp;
1529 struct kvm_cpuid cpuid;
1530
1531 r = -EFAULT;
1532 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1533 goto out;
1534 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1535 if (r)
1536 goto out;
1537 break;
1538 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001539 case KVM_SET_CPUID2: {
1540 struct kvm_cpuid2 __user *cpuid_arg = argp;
1541 struct kvm_cpuid2 cpuid;
1542
1543 r = -EFAULT;
1544 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1545 goto out;
1546 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001547 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001548 if (r)
1549 goto out;
1550 break;
1551 }
1552 case KVM_GET_CPUID2: {
1553 struct kvm_cpuid2 __user *cpuid_arg = argp;
1554 struct kvm_cpuid2 cpuid;
1555
1556 r = -EFAULT;
1557 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1558 goto out;
1559 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001560 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001561 if (r)
1562 goto out;
1563 r = -EFAULT;
1564 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1565 goto out;
1566 r = 0;
1567 break;
1568 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001569 case KVM_GET_MSRS:
1570 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1571 break;
1572 case KVM_SET_MSRS:
1573 r = msr_io(vcpu, argp, do_set_msr, 0);
1574 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001575 case KVM_TPR_ACCESS_REPORTING: {
1576 struct kvm_tpr_access_ctl tac;
1577
1578 r = -EFAULT;
1579 if (copy_from_user(&tac, argp, sizeof tac))
1580 goto out;
1581 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1582 if (r)
1583 goto out;
1584 r = -EFAULT;
1585 if (copy_to_user(argp, &tac, sizeof tac))
1586 goto out;
1587 r = 0;
1588 break;
1589 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001590 case KVM_SET_VAPIC_ADDR: {
1591 struct kvm_vapic_addr va;
1592
1593 r = -EINVAL;
1594 if (!irqchip_in_kernel(vcpu->kvm))
1595 goto out;
1596 r = -EFAULT;
1597 if (copy_from_user(&va, argp, sizeof va))
1598 goto out;
1599 r = 0;
1600 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1601 break;
1602 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001603 default:
1604 r = -EINVAL;
1605 }
1606out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08001607 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001608 return r;
1609}
1610
Carsten Otte1fe779f2007-10-29 16:08:35 +01001611static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1612{
1613 int ret;
1614
1615 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1616 return -1;
1617 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1618 return ret;
1619}
1620
1621static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1622 u32 kvm_nr_mmu_pages)
1623{
1624 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1625 return -EINVAL;
1626
Izik Eidus72dc67a2008-02-10 18:04:15 +02001627 down_write(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001628 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001629
1630 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001631 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001632
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001633 spin_unlock(&kvm->mmu_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001634 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001635 return 0;
1636}
1637
1638static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1639{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001640 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001641}
1642
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001643gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1644{
1645 int i;
1646 struct kvm_mem_alias *alias;
1647
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001648 for (i = 0; i < kvm->arch.naliases; ++i) {
1649 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001650 if (gfn >= alias->base_gfn
1651 && gfn < alias->base_gfn + alias->npages)
1652 return alias->target_gfn + gfn - alias->base_gfn;
1653 }
1654 return gfn;
1655}
1656
Carsten Otte1fe779f2007-10-29 16:08:35 +01001657/*
1658 * Set a new alias region. Aliases map a portion of physical memory into
1659 * another portion. This is useful for memory windows, for example the PC
1660 * VGA region.
1661 */
1662static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1663 struct kvm_memory_alias *alias)
1664{
1665 int r, n;
1666 struct kvm_mem_alias *p;
1667
1668 r = -EINVAL;
1669 /* General sanity checks */
1670 if (alias->memory_size & (PAGE_SIZE - 1))
1671 goto out;
1672 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1673 goto out;
1674 if (alias->slot >= KVM_ALIAS_SLOTS)
1675 goto out;
1676 if (alias->guest_phys_addr + alias->memory_size
1677 < alias->guest_phys_addr)
1678 goto out;
1679 if (alias->target_phys_addr + alias->memory_size
1680 < alias->target_phys_addr)
1681 goto out;
1682
Izik Eidus72dc67a2008-02-10 18:04:15 +02001683 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001684 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001685
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001686 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001687 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1688 p->npages = alias->memory_size >> PAGE_SHIFT;
1689 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1690
1691 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001692 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001693 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001694 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001695
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001696 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001697 kvm_mmu_zap_all(kvm);
1698
Izik Eidus72dc67a2008-02-10 18:04:15 +02001699 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001700
1701 return 0;
1702
1703out:
1704 return r;
1705}
1706
1707static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1708{
1709 int r;
1710
1711 r = 0;
1712 switch (chip->chip_id) {
1713 case KVM_IRQCHIP_PIC_MASTER:
1714 memcpy(&chip->chip.pic,
1715 &pic_irqchip(kvm)->pics[0],
1716 sizeof(struct kvm_pic_state));
1717 break;
1718 case KVM_IRQCHIP_PIC_SLAVE:
1719 memcpy(&chip->chip.pic,
1720 &pic_irqchip(kvm)->pics[1],
1721 sizeof(struct kvm_pic_state));
1722 break;
1723 case KVM_IRQCHIP_IOAPIC:
1724 memcpy(&chip->chip.ioapic,
1725 ioapic_irqchip(kvm),
1726 sizeof(struct kvm_ioapic_state));
1727 break;
1728 default:
1729 r = -EINVAL;
1730 break;
1731 }
1732 return r;
1733}
1734
1735static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1736{
1737 int r;
1738
1739 r = 0;
1740 switch (chip->chip_id) {
1741 case KVM_IRQCHIP_PIC_MASTER:
1742 memcpy(&pic_irqchip(kvm)->pics[0],
1743 &chip->chip.pic,
1744 sizeof(struct kvm_pic_state));
1745 break;
1746 case KVM_IRQCHIP_PIC_SLAVE:
1747 memcpy(&pic_irqchip(kvm)->pics[1],
1748 &chip->chip.pic,
1749 sizeof(struct kvm_pic_state));
1750 break;
1751 case KVM_IRQCHIP_IOAPIC:
1752 memcpy(ioapic_irqchip(kvm),
1753 &chip->chip.ioapic,
1754 sizeof(struct kvm_ioapic_state));
1755 break;
1756 default:
1757 r = -EINVAL;
1758 break;
1759 }
1760 kvm_pic_update_irq(pic_irqchip(kvm));
1761 return r;
1762}
1763
Sheng Yange0f63cb2008-03-04 00:50:59 +08001764static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1765{
1766 int r = 0;
1767
1768 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1769 return r;
1770}
1771
1772static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1773{
1774 int r = 0;
1775
1776 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1777 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1778 return r;
1779}
1780
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001781static int kvm_vm_ioctl_reinject(struct kvm *kvm,
1782 struct kvm_reinject_control *control)
1783{
1784 if (!kvm->arch.vpit)
1785 return -ENXIO;
1786 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
1787 return 0;
1788}
1789
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001790/*
1791 * Get (and clear) the dirty memory log for a memory slot.
1792 */
1793int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1794 struct kvm_dirty_log *log)
1795{
1796 int r;
1797 int n;
1798 struct kvm_memory_slot *memslot;
1799 int is_dirty = 0;
1800
Izik Eidus72dc67a2008-02-10 18:04:15 +02001801 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001802
1803 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1804 if (r)
1805 goto out;
1806
1807 /* If nothing is dirty, don't bother messing with page tables. */
1808 if (is_dirty) {
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001809 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001810 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001811 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001812 kvm_flush_remote_tlbs(kvm);
1813 memslot = &kvm->memslots[log->slot];
1814 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1815 memset(memslot->dirty_bitmap, 0, n);
1816 }
1817 r = 0;
1818out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001819 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001820 return r;
1821}
1822
Carsten Otte1fe779f2007-10-29 16:08:35 +01001823long kvm_arch_vm_ioctl(struct file *filp,
1824 unsigned int ioctl, unsigned long arg)
1825{
1826 struct kvm *kvm = filp->private_data;
1827 void __user *argp = (void __user *)arg;
1828 int r = -EINVAL;
Dave Hansenf0d66272008-08-11 10:01:45 -07001829 /*
1830 * This union makes it completely explicit to gcc-3.x
1831 * that these two variables' stack usage should be
1832 * combined, not added together.
1833 */
1834 union {
1835 struct kvm_pit_state ps;
1836 struct kvm_memory_alias alias;
1837 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001838
1839 switch (ioctl) {
1840 case KVM_SET_TSS_ADDR:
1841 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1842 if (r < 0)
1843 goto out;
1844 break;
1845 case KVM_SET_MEMORY_REGION: {
1846 struct kvm_memory_region kvm_mem;
1847 struct kvm_userspace_memory_region kvm_userspace_mem;
1848
1849 r = -EFAULT;
1850 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1851 goto out;
1852 kvm_userspace_mem.slot = kvm_mem.slot;
1853 kvm_userspace_mem.flags = kvm_mem.flags;
1854 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1855 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1856 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1857 if (r)
1858 goto out;
1859 break;
1860 }
1861 case KVM_SET_NR_MMU_PAGES:
1862 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1863 if (r)
1864 goto out;
1865 break;
1866 case KVM_GET_NR_MMU_PAGES:
1867 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1868 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07001869 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001870 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001871 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01001872 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001873 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001874 if (r)
1875 goto out;
1876 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001877 case KVM_CREATE_IRQCHIP:
1878 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001879 kvm->arch.vpic = kvm_create_pic(kvm);
1880 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001881 r = kvm_ioapic_init(kvm);
1882 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001883 kfree(kvm->arch.vpic);
1884 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001885 goto out;
1886 }
1887 } else
1888 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +02001889 r = kvm_setup_default_irq_routing(kvm);
1890 if (r) {
1891 kfree(kvm->arch.vpic);
1892 kfree(kvm->arch.vioapic);
1893 goto out;
1894 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01001895 break;
Sheng Yang78376992008-01-28 05:10:22 +08001896 case KVM_CREATE_PIT:
Avi Kivity269e05e2009-01-05 15:21:42 +02001897 mutex_lock(&kvm->lock);
1898 r = -EEXIST;
1899 if (kvm->arch.vpit)
1900 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08001901 r = -ENOMEM;
1902 kvm->arch.vpit = kvm_create_pit(kvm);
1903 if (kvm->arch.vpit)
1904 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02001905 create_pit_unlock:
1906 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08001907 break;
Gleb Natapov49256632009-02-04 17:28:14 +02001908 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001909 case KVM_IRQ_LINE: {
1910 struct kvm_irq_level irq_event;
1911
1912 r = -EFAULT;
1913 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1914 goto out;
1915 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02001916 __s32 status;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001917 mutex_lock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +02001918 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1919 irq_event.irq, irq_event.level);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001920 mutex_unlock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +02001921 if (ioctl == KVM_IRQ_LINE_STATUS) {
1922 irq_event.status = status;
1923 if (copy_to_user(argp, &irq_event,
1924 sizeof irq_event))
1925 goto out;
1926 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01001927 r = 0;
1928 }
1929 break;
1930 }
1931 case KVM_GET_IRQCHIP: {
1932 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001933 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001934
Dave Hansenf0d66272008-08-11 10:01:45 -07001935 r = -ENOMEM;
1936 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001937 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001938 r = -EFAULT;
1939 if (copy_from_user(chip, argp, sizeof *chip))
1940 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001941 r = -ENXIO;
1942 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07001943 goto get_irqchip_out;
1944 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
1945 if (r)
1946 goto get_irqchip_out;
1947 r = -EFAULT;
1948 if (copy_to_user(argp, chip, sizeof *chip))
1949 goto get_irqchip_out;
1950 r = 0;
1951 get_irqchip_out:
1952 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001953 if (r)
1954 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001955 break;
1956 }
1957 case KVM_SET_IRQCHIP: {
1958 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001959 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001960
Dave Hansenf0d66272008-08-11 10:01:45 -07001961 r = -ENOMEM;
1962 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001963 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001964 r = -EFAULT;
1965 if (copy_from_user(chip, argp, sizeof *chip))
1966 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001967 r = -ENXIO;
1968 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07001969 goto set_irqchip_out;
1970 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
1971 if (r)
1972 goto set_irqchip_out;
1973 r = 0;
1974 set_irqchip_out:
1975 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001976 if (r)
1977 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001978 break;
1979 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08001980 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08001981 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001982 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001983 goto out;
1984 r = -ENXIO;
1985 if (!kvm->arch.vpit)
1986 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001987 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08001988 if (r)
1989 goto out;
1990 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001991 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001992 goto out;
1993 r = 0;
1994 break;
1995 }
1996 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08001997 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001998 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001999 goto out;
2000 r = -ENXIO;
2001 if (!kvm->arch.vpit)
2002 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002003 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002004 if (r)
2005 goto out;
2006 r = 0;
2007 break;
2008 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002009 case KVM_REINJECT_CONTROL: {
2010 struct kvm_reinject_control control;
2011 r = -EFAULT;
2012 if (copy_from_user(&control, argp, sizeof(control)))
2013 goto out;
2014 r = kvm_vm_ioctl_reinject(kvm, &control);
2015 if (r)
2016 goto out;
2017 r = 0;
2018 break;
2019 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002020 default:
2021 ;
2022 }
2023out:
2024 return r;
2025}
2026
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002027static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002028{
2029 u32 dummy[2];
2030 unsigned i, j;
2031
2032 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2033 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2034 continue;
2035 if (j < i)
2036 msrs_to_save[j] = msrs_to_save[i];
2037 j++;
2038 }
2039 num_msrs_to_save = j;
2040}
2041
Carsten Ottebbd9b642007-10-30 18:44:21 +01002042/*
2043 * Only apic need an MMIO device hook, so shortcut now..
2044 */
2045static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002046 gpa_t addr, int len,
2047 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002048{
2049 struct kvm_io_device *dev;
2050
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002051 if (vcpu->arch.apic) {
2052 dev = &vcpu->arch.apic->dev;
Laurent Vivier92760492008-05-30 16:05:53 +02002053 if (dev->in_range(dev, addr, len, is_write))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002054 return dev;
2055 }
2056 return NULL;
2057}
2058
2059
2060static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002061 gpa_t addr, int len,
2062 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002063{
2064 struct kvm_io_device *dev;
2065
Laurent Vivier92760492008-05-30 16:05:53 +02002066 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002067 if (dev == NULL)
Laurent Vivier92760492008-05-30 16:05:53 +02002068 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
2069 is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002070 return dev;
2071}
2072
Hannes Edercded19f2009-02-21 02:19:13 +01002073static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2074 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002075{
2076 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002077 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002078
2079 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002080 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002081 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02002082 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002083 int ret;
2084
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002085 if (gpa == UNMAPPED_GVA) {
2086 r = X86EMUL_PROPAGATE_FAULT;
2087 goto out;
2088 }
Izik Eidus77c20022008-12-29 01:42:19 +02002089 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002090 if (ret < 0) {
2091 r = X86EMUL_UNHANDLEABLE;
2092 goto out;
2093 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002094
Izik Eidus77c20022008-12-29 01:42:19 +02002095 bytes -= toread;
2096 data += toread;
2097 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002098 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002099out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002100 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002101}
Izik Eidus77c20022008-12-29 01:42:19 +02002102
Hannes Edercded19f2009-02-21 02:19:13 +01002103static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2104 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02002105{
2106 void *data = val;
2107 int r = X86EMUL_CONTINUE;
2108
2109 while (bytes) {
2110 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2111 unsigned offset = addr & (PAGE_SIZE-1);
2112 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2113 int ret;
2114
2115 if (gpa == UNMAPPED_GVA) {
2116 r = X86EMUL_PROPAGATE_FAULT;
2117 goto out;
2118 }
2119 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2120 if (ret < 0) {
2121 r = X86EMUL_UNHANDLEABLE;
2122 goto out;
2123 }
2124
2125 bytes -= towrite;
2126 data += towrite;
2127 addr += towrite;
2128 }
2129out:
2130 return r;
2131}
2132
Carsten Ottebbd9b642007-10-30 18:44:21 +01002133
Carsten Ottebbd9b642007-10-30 18:44:21 +01002134static int emulator_read_emulated(unsigned long addr,
2135 void *val,
2136 unsigned int bytes,
2137 struct kvm_vcpu *vcpu)
2138{
2139 struct kvm_io_device *mmio_dev;
2140 gpa_t gpa;
2141
2142 if (vcpu->mmio_read_completed) {
2143 memcpy(val, vcpu->mmio_data, bytes);
2144 vcpu->mmio_read_completed = 0;
2145 return X86EMUL_CONTINUE;
2146 }
2147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002149
2150 /* For APIC access vmexit */
2151 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2152 goto mmio;
2153
Izik Eidus77c20022008-12-29 01:42:19 +02002154 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2155 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002156 return X86EMUL_CONTINUE;
2157 if (gpa == UNMAPPED_GVA)
2158 return X86EMUL_PROPAGATE_FAULT;
2159
2160mmio:
2161 /*
2162 * Is this MMIO handled locally?
2163 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002164 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002165 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002166 if (mmio_dev) {
2167 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002168 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002169 return X86EMUL_CONTINUE;
2170 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002171 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002172
2173 vcpu->mmio_needed = 1;
2174 vcpu->mmio_phys_addr = gpa;
2175 vcpu->mmio_size = bytes;
2176 vcpu->mmio_is_write = 0;
2177
2178 return X86EMUL_UNHANDLEABLE;
2179}
2180
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002181int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002182 const void *val, int bytes)
2183{
2184 int ret;
2185
2186 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2187 if (ret < 0)
2188 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002189 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002190 return 1;
2191}
2192
Carsten Ottebbd9b642007-10-30 18:44:21 +01002193static int emulator_write_emulated_onepage(unsigned long addr,
2194 const void *val,
2195 unsigned int bytes,
2196 struct kvm_vcpu *vcpu)
2197{
2198 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002199 gpa_t gpa;
2200
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002201 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002202
2203 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002204 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002205 return X86EMUL_PROPAGATE_FAULT;
2206 }
2207
2208 /* For APIC access vmexit */
2209 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2210 goto mmio;
2211
2212 if (emulator_write_phys(vcpu, gpa, val, bytes))
2213 return X86EMUL_CONTINUE;
2214
2215mmio:
2216 /*
2217 * Is this MMIO handled locally?
2218 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002219 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002220 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002221 if (mmio_dev) {
2222 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002223 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002224 return X86EMUL_CONTINUE;
2225 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002226 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002227
2228 vcpu->mmio_needed = 1;
2229 vcpu->mmio_phys_addr = gpa;
2230 vcpu->mmio_size = bytes;
2231 vcpu->mmio_is_write = 1;
2232 memcpy(vcpu->mmio_data, val, bytes);
2233
2234 return X86EMUL_CONTINUE;
2235}
2236
2237int emulator_write_emulated(unsigned long addr,
2238 const void *val,
2239 unsigned int bytes,
2240 struct kvm_vcpu *vcpu)
2241{
2242 /* Crossing a page boundary? */
2243 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2244 int rc, now;
2245
2246 now = -addr & ~PAGE_MASK;
2247 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2248 if (rc != X86EMUL_CONTINUE)
2249 return rc;
2250 addr += now;
2251 val += now;
2252 bytes -= now;
2253 }
2254 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2255}
2256EXPORT_SYMBOL_GPL(emulator_write_emulated);
2257
2258static int emulator_cmpxchg_emulated(unsigned long addr,
2259 const void *old,
2260 const void *new,
2261 unsigned int bytes,
2262 struct kvm_vcpu *vcpu)
2263{
Marcin Slusarz9f51e242009-08-09 21:54:00 +02002264 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002265#ifndef CONFIG_X86_64
2266 /* guests cmpxchg8b have to be emulated atomically */
2267 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002268 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002269 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002270 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002271 u64 val;
2272
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002273 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2274
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002275 if (gpa == UNMAPPED_GVA ||
2276 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2277 goto emul_write;
2278
2279 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2280 goto emul_write;
2281
2282 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002283
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002284 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002285
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002286 kaddr = kmap_atomic(page, KM_USER0);
2287 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2288 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002289 kvm_release_page_dirty(page);
2290 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002291emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002292#endif
2293
Carsten Ottebbd9b642007-10-30 18:44:21 +01002294 return emulator_write_emulated(addr, new, bytes, vcpu);
2295}
2296
2297static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2298{
2299 return kvm_x86_ops->get_segment_base(vcpu, seg);
2300}
2301
2302int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2303{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002304 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002305 return X86EMUL_CONTINUE;
2306}
2307
2308int emulate_clts(struct kvm_vcpu *vcpu)
2309{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002310 KVMTRACE_0D(CLTS, vcpu, handler);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002311 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002312 return X86EMUL_CONTINUE;
2313}
2314
2315int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2316{
2317 struct kvm_vcpu *vcpu = ctxt->vcpu;
2318
2319 switch (dr) {
2320 case 0 ... 3:
2321 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2322 return X86EMUL_CONTINUE;
2323 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002324 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002325 return X86EMUL_UNHANDLEABLE;
2326 }
2327}
2328
2329int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2330{
2331 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2332 int exception;
2333
2334 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2335 if (exception) {
2336 /* FIXME: better handling */
2337 return X86EMUL_UNHANDLEABLE;
2338 }
2339 return X86EMUL_CONTINUE;
2340}
2341
2342void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2343{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002344 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002345 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002346 unsigned long rip_linear;
2347
Avi Kivityf76c7102008-06-13 22:45:42 +03002348 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002349 return;
2350
Glauber Costa25be4602008-06-10 10:46:53 -03002351 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2352
Izik Eidus77c20022008-12-29 01:42:19 +02002353 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002354
2355 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2356 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002357}
2358EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2359
Harvey Harrison14af3f32008-02-19 10:25:50 -08002360static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02002361 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002362 .read_emulated = emulator_read_emulated,
2363 .write_emulated = emulator_write_emulated,
2364 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2365};
2366
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002367static void cache_all_regs(struct kvm_vcpu *vcpu)
2368{
2369 kvm_register_read(vcpu, VCPU_REGS_RAX);
2370 kvm_register_read(vcpu, VCPU_REGS_RSP);
2371 kvm_register_read(vcpu, VCPU_REGS_RIP);
2372 vcpu->arch.regs_dirty = ~0;
2373}
2374
Carsten Ottebbd9b642007-10-30 18:44:21 +01002375int emulate_instruction(struct kvm_vcpu *vcpu,
2376 struct kvm_run *run,
2377 unsigned long cr2,
2378 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002379 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002380{
Glauber Costa310b5d32009-05-12 16:21:06 -04002381 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08002382 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002383
Avi Kivity26eef702008-07-03 14:59:22 +03002384 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002386 /*
2387 * TODO: fix x86_emulate.c to use guest_read/write_register
2388 * instead of direct ->regs accesses, can save hundred cycles
2389 * on Intel for instructions that don't read/change RSP, for
2390 * for example.
2391 */
2392 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002393
2394 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002395 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002396
Sheng Yang571008d2008-01-02 14:49:22 +08002397 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002398 int cs_db, cs_l;
2399 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2400
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002401 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2402 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2403 vcpu->arch.emulate_ctxt.mode =
2404 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002405 ? X86EMUL_MODE_REAL : cs_l
2406 ? X86EMUL_MODE_PROT64 : cs_db
2407 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2408
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002409 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002410
2411 /* Reject the instructions other than VMCALL/VMMCALL when
2412 * try to emulate invalid opcode */
2413 c = &vcpu->arch.emulate_ctxt.decode;
2414 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2415 (!(c->twobyte && c->b == 0x01 &&
2416 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2417 c->modrm_mod == 3 && c->modrm_rm == 1)))
2418 return EMULATE_FAIL;
2419
Avi Kivityf2b57562007-11-18 15:17:51 +02002420 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002421 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002422 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002423 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2424 return EMULATE_DONE;
2425 return EMULATE_FAIL;
2426 }
2427 }
2428
Gleb Natapovba8afb62009-04-12 13:36:57 +03002429 if (emulation_type & EMULTYPE_SKIP) {
2430 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
2431 return EMULATE_DONE;
2432 }
2433
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002434 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04002435 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
2436
2437 if (r == 0)
2438 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002439
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002440 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002441 return EMULATE_DO_MMIO;
2442
2443 if ((r || vcpu->mmio_is_write) && run) {
2444 run->exit_reason = KVM_EXIT_MMIO;
2445 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2446 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2447 run->mmio.len = vcpu->mmio_size;
2448 run->mmio.is_write = vcpu->mmio_is_write;
2449 }
2450
2451 if (r) {
2452 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2453 return EMULATE_DONE;
2454 if (!vcpu->mmio_needed) {
2455 kvm_report_emulation_failure(vcpu, "mmio");
2456 return EMULATE_FAIL;
2457 }
2458 return EMULATE_DO_MMIO;
2459 }
2460
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002461 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002462
2463 if (vcpu->mmio_is_write) {
2464 vcpu->mmio_needed = 0;
2465 return EMULATE_DO_MMIO;
2466 }
2467
2468 return EMULATE_DONE;
2469}
2470EXPORT_SYMBOL_GPL(emulate_instruction);
2471
Carsten Ottede7d7892007-10-30 18:44:25 +01002472static int pio_copy_data(struct kvm_vcpu *vcpu)
2473{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002474 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02002475 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01002476 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02002477 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002478
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002479 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2480 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02002481 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01002482 else
Izik Eidus0f346072008-12-29 01:42:20 +02002483 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
2484 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002485}
2486
2487int complete_pio(struct kvm_vcpu *vcpu)
2488{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002489 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002490 long delta;
2491 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002492 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002493
2494 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002495 if (io->in) {
2496 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2497 memcpy(&val, vcpu->arch.pio_data, io->size);
2498 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2499 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002500 } else {
2501 if (io->in) {
2502 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002503 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002504 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002505 }
2506
2507 delta = 1;
2508 if (io->rep) {
2509 delta *= io->cur_count;
2510 /*
2511 * The size of the register should really depend on
2512 * current address size.
2513 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002514 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2515 val -= delta;
2516 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002517 }
2518 if (io->down)
2519 delta = -delta;
2520 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002521 if (io->in) {
2522 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2523 val += delta;
2524 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2525 } else {
2526 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2527 val += delta;
2528 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2529 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002530 }
2531
Carsten Ottede7d7892007-10-30 18:44:25 +01002532 io->count -= io->cur_count;
2533 io->cur_count = 0;
2534
2535 return 0;
2536}
2537
2538static void kernel_pio(struct kvm_io_device *pio_dev,
2539 struct kvm_vcpu *vcpu,
2540 void *pd)
2541{
2542 /* TODO: String I/O for in kernel device */
2543
2544 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002545 if (vcpu->arch.pio.in)
2546 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2547 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002548 pd);
2549 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002550 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2551 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002552 pd);
2553 mutex_unlock(&vcpu->kvm->lock);
2554}
2555
2556static void pio_string_write(struct kvm_io_device *pio_dev,
2557 struct kvm_vcpu *vcpu)
2558{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002559 struct kvm_pio_request *io = &vcpu->arch.pio;
2560 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002561 int i;
2562
2563 mutex_lock(&vcpu->kvm->lock);
2564 for (i = 0; i < io->cur_count; i++) {
2565 kvm_iodevice_write(pio_dev, io->port,
2566 io->size,
2567 pd);
2568 pd += io->size;
2569 }
2570 mutex_unlock(&vcpu->kvm->lock);
2571}
2572
2573static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002574 gpa_t addr, int len,
2575 int is_write)
Carsten Ottede7d7892007-10-30 18:44:25 +01002576{
Laurent Vivier92760492008-05-30 16:05:53 +02002577 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
Carsten Ottede7d7892007-10-30 18:44:25 +01002578}
2579
2580int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2581 int size, unsigned port)
2582{
2583 struct kvm_io_device *pio_dev;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002584 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002585
2586 vcpu->run->exit_reason = KVM_EXIT_IO;
2587 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002588 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002589 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002590 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2591 vcpu->run->io.port = vcpu->arch.pio.port = port;
2592 vcpu->arch.pio.in = in;
2593 vcpu->arch.pio.string = 0;
2594 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002595 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002596
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002597 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2598 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2599 handler);
2600 else
2601 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2602 handler);
2603
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002604 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2605 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002606
Laurent Vivier92760492008-05-30 16:05:53 +02002607 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
Carsten Ottede7d7892007-10-30 18:44:25 +01002608 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002609 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002610 complete_pio(vcpu);
2611 return 1;
2612 }
2613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2616
2617int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2618 int size, unsigned long count, int down,
2619 gva_t address, int rep, unsigned port)
2620{
2621 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02002622 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002623 struct kvm_io_device *pio_dev;
2624
2625 vcpu->run->exit_reason = KVM_EXIT_IO;
2626 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002627 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002628 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002629 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2630 vcpu->run->io.port = vcpu->arch.pio.port = port;
2631 vcpu->arch.pio.in = in;
2632 vcpu->arch.pio.string = 1;
2633 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002634 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002635
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002636 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2637 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2638 handler);
2639 else
2640 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2641 handler);
2642
Carsten Ottede7d7892007-10-30 18:44:25 +01002643 if (!count) {
2644 kvm_x86_ops->skip_emulated_instruction(vcpu);
2645 return 1;
2646 }
2647
2648 if (!down)
2649 in_page = PAGE_SIZE - offset_in_page(address);
2650 else
2651 in_page = offset_in_page(address) + size;
2652 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02002653 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01002654 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002655 if (down) {
2656 /*
2657 * String I/O in reverse. Yuck. Kill the guest, fix later.
2658 */
2659 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002660 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002661 return 1;
2662 }
2663 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002664 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002665
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002666 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002667 kvm_x86_ops->skip_emulated_instruction(vcpu);
2668
Izik Eidus0f346072008-12-29 01:42:20 +02002669 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01002670
Laurent Vivier92760492008-05-30 16:05:53 +02002671 pio_dev = vcpu_find_pio_dev(vcpu, port,
2672 vcpu->arch.pio.cur_count,
2673 !vcpu->arch.pio.in);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002674 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002675 /* string PIO write */
2676 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02002677 if (ret == X86EMUL_PROPAGATE_FAULT) {
2678 kvm_inject_gp(vcpu, 0);
2679 return 1;
2680 }
2681 if (ret == 0 && pio_dev) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002682 pio_string_write(pio_dev, vcpu);
2683 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002684 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002685 ret = 1;
2686 }
2687 } else if (pio_dev)
2688 pr_unimpl(vcpu, "no string pio read support yet, "
2689 "port %x size %d count %ld\n",
2690 port, size, count);
2691
2692 return ret;
2693}
2694EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2695
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002696static void bounce_off(void *info)
2697{
2698 /* nothing */
2699}
2700
2701static unsigned int ref_freq;
2702static unsigned long tsc_khz_ref;
2703
2704static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
2705 void *data)
2706{
2707 struct cpufreq_freqs *freq = data;
2708 struct kvm *kvm;
2709 struct kvm_vcpu *vcpu;
2710 int i, send_ipi = 0;
2711
2712 if (!ref_freq)
2713 ref_freq = freq->old;
2714
2715 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
2716 return 0;
2717 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
2718 return 0;
2719 per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
2720
2721 spin_lock(&kvm_lock);
2722 list_for_each_entry(kvm, &vm_list, vm_list) {
2723 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2724 vcpu = kvm->vcpus[i];
2725 if (!vcpu)
2726 continue;
2727 if (vcpu->cpu != freq->cpu)
2728 continue;
2729 if (!kvm_request_guest_time_update(vcpu))
2730 continue;
2731 if (vcpu->cpu != smp_processor_id())
2732 send_ipi++;
2733 }
2734 }
2735 spin_unlock(&kvm_lock);
2736
2737 if (freq->old < freq->new && send_ipi) {
2738 /*
2739 * We upscale the frequency. Must make the guest
2740 * doesn't see old kvmclock values while running with
2741 * the new frequency, otherwise we risk the guest sees
2742 * time go backwards.
2743 *
2744 * In case we update the frequency for another cpu
2745 * (which might be in guest context) send an interrupt
2746 * to kick the cpu out of guest context. Next time
2747 * guest context is entered kvmclock will be updated,
2748 * so the guest will not see stale values.
2749 */
2750 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
2751 }
2752 return 0;
2753}
2754
2755static struct notifier_block kvmclock_cpufreq_notifier_block = {
2756 .notifier_call = kvmclock_cpufreq_notifier
2757};
2758
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002759int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002760{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002761 int r, cpu;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002762 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2763
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002764 if (kvm_x86_ops) {
2765 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002766 r = -EEXIST;
2767 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002768 }
2769
2770 if (!ops->cpu_has_kvm_support()) {
2771 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002772 r = -EOPNOTSUPP;
2773 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002774 }
2775 if (ops->disabled_by_bios()) {
2776 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002777 r = -EOPNOTSUPP;
2778 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002779 }
2780
Avi Kivity97db56c2008-01-13 13:23:56 +02002781 r = kvm_mmu_module_init();
2782 if (r)
2783 goto out;
2784
2785 kvm_init_msr_list();
2786
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002787 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002788 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08002789 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2790 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002791 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002792
2793 for_each_possible_cpu(cpu)
2794 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
2795 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
2796 tsc_khz_ref = tsc_khz;
2797 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
2798 CPUFREQ_TRANSITION_NOTIFIER);
2799 }
2800
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002801 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002802
2803out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002804 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002805}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002806
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002807void kvm_arch_exit(void)
2808{
Jan Kiszka888d2562009-04-17 19:24:58 +02002809 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
2810 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
2811 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002812 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002813 kvm_mmu_module_exit();
2814}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002815
Hollis Blanchard8776e512007-10-31 17:24:24 -05002816int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2817{
2818 ++vcpu->stat.halt_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002819 KVMTRACE_0D(HLT, vcpu, handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002820 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03002821 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002822 return 1;
2823 } else {
2824 vcpu->run->exit_reason = KVM_EXIT_HLT;
2825 return 0;
2826 }
2827}
2828EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2829
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002830static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2831 unsigned long a1)
2832{
2833 if (is_long_mode(vcpu))
2834 return a0;
2835 else
2836 return a0 | ((gpa_t)a1 << 32);
2837}
2838
Hollis Blanchard8776e512007-10-31 17:24:24 -05002839int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2840{
2841 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002842 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002843
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002844 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
2845 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
2846 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
2847 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
2848 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002849
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002850 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2851
Hollis Blanchard8776e512007-10-31 17:24:24 -05002852 if (!is_long_mode(vcpu)) {
2853 nr &= 0xFFFFFFFF;
2854 a0 &= 0xFFFFFFFF;
2855 a1 &= 0xFFFFFFFF;
2856 a2 &= 0xFFFFFFFF;
2857 a3 &= 0xFFFFFFFF;
2858 }
2859
2860 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002861 case KVM_HC_VAPIC_POLL_IRQ:
2862 ret = 0;
2863 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002864 case KVM_HC_MMU_OP:
2865 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2866 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002867 default:
2868 ret = -KVM_ENOSYS;
2869 break;
2870 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002871 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05302872 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002873 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002874}
2875EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2876
2877int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2878{
2879 char instruction[3];
2880 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002881 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002882
Hollis Blanchard8776e512007-10-31 17:24:24 -05002883
2884 /*
2885 * Blow out the MMU to ensure that no other VCPU has an active mapping
2886 * to ensure that the updated hypercall appears atomically across all
2887 * VCPUs.
2888 */
2889 kvm_mmu_zap_all(vcpu->kvm);
2890
Hollis Blanchard8776e512007-10-31 17:24:24 -05002891 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002892 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002893 != X86EMUL_CONTINUE)
2894 ret = -EFAULT;
2895
Hollis Blanchard8776e512007-10-31 17:24:24 -05002896 return ret;
2897}
2898
2899static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2900{
2901 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2902}
2903
2904void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2905{
2906 struct descriptor_table dt = { limit, base };
2907
2908 kvm_x86_ops->set_gdt(vcpu, &dt);
2909}
2910
2911void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2912{
2913 struct descriptor_table dt = { limit, base };
2914
2915 kvm_x86_ops->set_idt(vcpu, &dt);
2916}
2917
2918void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2919 unsigned long *rflags)
2920{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002921 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002922 *rflags = kvm_x86_ops->get_rflags(vcpu);
2923}
2924
2925unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2926{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002927 unsigned long value;
2928
Hollis Blanchard8776e512007-10-31 17:24:24 -05002929 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2930 switch (cr) {
2931 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002932 value = vcpu->arch.cr0;
2933 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002934 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002935 value = vcpu->arch.cr2;
2936 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002937 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002938 value = vcpu->arch.cr3;
2939 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002940 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002941 value = vcpu->arch.cr4;
2942 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002943 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002944 value = kvm_get_cr8(vcpu);
2945 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002946 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002947 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002948 return 0;
2949 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02002950 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2951 (u32)((u64)value >> 32), handler);
2952
2953 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002954}
2955
2956void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2957 unsigned long *rflags)
2958{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002959 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2960 (u32)((u64)val >> 32), handler);
2961
Hollis Blanchard8776e512007-10-31 17:24:24 -05002962 switch (cr) {
2963 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002964 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002965 *rflags = kvm_x86_ops->get_rflags(vcpu);
2966 break;
2967 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002968 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002969 break;
2970 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002971 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002972 break;
2973 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002974 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002975 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002976 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002977 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002978 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002979 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002980 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002981 }
2982}
2983
Dan Kenigsberg07716712007-11-21 17:10:04 +02002984static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2985{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002986 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2987 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002988
2989 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2990 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08002991 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002992 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002993 if (ej->function == e->function) {
2994 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2995 return j;
2996 }
2997 }
2998 return 0; /* silence gcc, even though control never reaches here */
2999}
3000
3001/* find an entry with matching function, matching index (if needed), and that
3002 * should be read next (if it's stateful) */
3003static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3004 u32 function, u32 index)
3005{
3006 if (e->function != function)
3007 return 0;
3008 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3009 return 0;
3010 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00003011 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02003012 return 0;
3013 return 1;
3014}
3015
Alexander Grafd8017472008-11-25 20:17:11 +01003016struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3017 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003018{
3019 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01003020 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003021
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003022 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01003023 struct kvm_cpuid_entry2 *e;
3024
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003025 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003026 if (is_matching_cpuid_entry(e, function, index)) {
3027 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3028 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003029 best = e;
3030 break;
3031 }
3032 /*
3033 * Both basic or both extended?
3034 */
3035 if (((e->function ^ function) & 0x80000000) == 0)
3036 if (!best || e->function > best->function)
3037 best = e;
3038 }
Alexander Grafd8017472008-11-25 20:17:11 +01003039 return best;
3040}
3041
Dong, Eddie82725b22009-03-30 16:21:08 +08003042int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3043{
3044 struct kvm_cpuid_entry2 *best;
3045
3046 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3047 if (best)
3048 return best->eax & 0xff;
3049 return 36;
3050}
3051
Alexander Grafd8017472008-11-25 20:17:11 +01003052void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3053{
3054 u32 function, index;
3055 struct kvm_cpuid_entry2 *best;
3056
3057 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3058 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3059 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3060 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3061 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3062 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3063 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003064 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003065 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3066 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3067 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3068 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003069 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003070 kvm_x86_ops->skip_emulated_instruction(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003071 KVMTRACE_5D(CPUID, vcpu, function,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003072 (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
3073 (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
3074 (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
3075 (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003076}
3077EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003078
3079/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003080 * Check if userspace requested an interrupt window, and that the
3081 * interrupt window is open.
3082 *
3083 * No need to exit to userspace if we already have an interrupt queued.
3084 */
3085static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
3086 struct kvm_run *kvm_run)
3087{
Gleb Natapov80618232009-04-21 17:44:56 +03003088 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003089 kvm_run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03003090 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003091}
3092
3093static void post_kvm_run_save(struct kvm_vcpu *vcpu,
3094 struct kvm_run *kvm_run)
3095{
3096 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003097 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003098 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01003099 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003100 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01003101 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003102 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03003103 kvm_arch_interrupt_allowed(vcpu) &&
3104 !kvm_cpu_has_interrupt(vcpu) &&
3105 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003106}
3107
Avi Kivityb93463a2007-10-25 16:52:32 +02003108static void vapic_enter(struct kvm_vcpu *vcpu)
3109{
3110 struct kvm_lapic *apic = vcpu->arch.apic;
3111 struct page *page;
3112
3113 if (!apic || !apic->vapic_addr)
3114 return;
3115
3116 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003117
3118 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003119}
3120
3121static void vapic_exit(struct kvm_vcpu *vcpu)
3122{
3123 struct kvm_lapic *apic = vcpu->arch.apic;
3124
3125 if (!apic || !apic->vapic_addr)
3126 return;
3127
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003128 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003129 kvm_release_page_dirty(apic->vapic_page);
3130 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003131 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003132}
3133
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003134static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3135{
3136 int max_irr, tpr;
3137
3138 if (!kvm_x86_ops->update_cr8_intercept)
3139 return;
3140
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003141 if (!vcpu->arch.apic->vapic_addr)
3142 max_irr = kvm_lapic_find_highest_irr(vcpu);
3143 else
3144 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003145
3146 if (max_irr != -1)
3147 max_irr >>= 4;
3148
3149 tpr = kvm_lapic_get_cr8(vcpu);
3150
3151 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3152}
3153
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003154static void inject_pending_irq(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003155{
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003156 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3157 kvm_x86_ops->set_interrupt_shadow(vcpu, 0);
3158
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003159 /* try to reinject previous events if any */
3160 if (vcpu->arch.nmi_injected) {
3161 kvm_x86_ops->set_nmi(vcpu);
3162 return;
3163 }
3164
3165 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003166 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003167 return;
3168 }
3169
3170 /* try to inject new event if pending */
3171 if (vcpu->arch.nmi_pending) {
3172 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3173 vcpu->arch.nmi_pending = false;
3174 vcpu->arch.nmi_injected = true;
3175 kvm_x86_ops->set_nmi(vcpu);
3176 }
3177 } else if (kvm_cpu_has_interrupt(vcpu)) {
3178 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003179 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3180 false);
3181 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003182 }
3183 }
3184}
3185
Marcelo Tosattid7690172008-09-08 15:23:48 -03003186static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003187{
3188 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003189 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
3190 kvm_run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003191
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003192 if (vcpu->requests)
3193 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3194 kvm_mmu_unload(vcpu);
3195
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003196 r = kvm_mmu_reload(vcpu);
3197 if (unlikely(r))
3198 goto out;
3199
Avi Kivity2f52d582008-01-16 12:49:30 +02003200 if (vcpu->requests) {
3201 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003202 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003203 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3204 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03003205 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3206 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003207 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3208 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003209 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3210 &vcpu->requests)) {
3211 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
3212 r = 0;
3213 goto out;
3214 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003215 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3216 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
3217 r = 0;
3218 goto out;
3219 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003220 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003221
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003222 preempt_disable();
3223
3224 kvm_x86_ops->prepare_guest_switch(vcpu);
3225 kvm_load_guest_fpu(vcpu);
3226
3227 local_irq_disable();
3228
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003229 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3230 smp_mb__after_clear_bit();
3231
Marcelo Tosattid7690172008-09-08 15:23:48 -03003232 if (vcpu->requests || need_resched() || signal_pending(current)) {
Avi Kivity6c1428012008-01-15 18:27:32 +02003233 local_irq_enable();
3234 preempt_enable();
3235 r = 1;
3236 goto out;
3237 }
3238
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003239 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02003240 __queue_exception(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02003241 else
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003242 inject_pending_irq(vcpu, kvm_run);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003243
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003244 /* enable NMI/IRQ window open exits if needed */
3245 if (vcpu->arch.nmi_pending)
3246 kvm_x86_ops->enable_nmi_window(vcpu);
3247 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3248 kvm_x86_ops->enable_irq_window(vcpu);
3249
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003250 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003251 update_cr8_intercept(vcpu);
3252 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003253 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003254
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003255 up_read(&vcpu->kvm->slots_lock);
3256
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003257 kvm_guest_enter();
3258
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003259 get_debugreg(vcpu->arch.host_dr6, 6);
3260 get_debugreg(vcpu->arch.host_dr7, 7);
3261 if (unlikely(vcpu->arch.switch_db_regs)) {
3262 get_debugreg(vcpu->arch.host_db[0], 0);
3263 get_debugreg(vcpu->arch.host_db[1], 1);
3264 get_debugreg(vcpu->arch.host_db[2], 2);
3265 get_debugreg(vcpu->arch.host_db[3], 3);
3266
3267 set_debugreg(0, 7);
3268 set_debugreg(vcpu->arch.eff_db[0], 0);
3269 set_debugreg(vcpu->arch.eff_db[1], 1);
3270 set_debugreg(vcpu->arch.eff_db[2], 2);
3271 set_debugreg(vcpu->arch.eff_db[3], 3);
3272 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003273
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003274 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003275 kvm_x86_ops->run(vcpu, kvm_run);
3276
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003277 if (unlikely(vcpu->arch.switch_db_regs)) {
3278 set_debugreg(0, 7);
3279 set_debugreg(vcpu->arch.host_db[0], 0);
3280 set_debugreg(vcpu->arch.host_db[1], 1);
3281 set_debugreg(vcpu->arch.host_db[2], 2);
3282 set_debugreg(vcpu->arch.host_db[3], 3);
3283 }
3284 set_debugreg(vcpu->arch.host_dr6, 6);
3285 set_debugreg(vcpu->arch.host_dr7, 7);
3286
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003287 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003288 local_irq_enable();
3289
3290 ++vcpu->stat.exits;
3291
3292 /*
3293 * We must have an instruction between local_irq_enable() and
3294 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3295 * the interrupt shadow. The stat.exits increment will do nicely.
3296 * But we need to prevent reordering, hence this barrier():
3297 */
3298 barrier();
3299
3300 kvm_guest_exit();
3301
3302 preempt_enable();
3303
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003304 down_read(&vcpu->kvm->slots_lock);
3305
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003306 /*
3307 * Profile KVM exit RIPs:
3308 */
3309 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003310 unsigned long rip = kvm_rip_read(vcpu);
3311 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003312 }
3313
Avi Kivity298101d2007-11-25 13:41:11 +02003314
Avi Kivityb93463a2007-10-25 16:52:32 +02003315 kvm_lapic_sync_from_vapic(vcpu);
3316
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003317 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003318out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003319 return r;
3320}
3321
Gleb Natapov09cec752009-03-23 15:11:44 +02003322
Marcelo Tosattid7690172008-09-08 15:23:48 -03003323static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3324{
3325 int r;
3326
3327 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003328 pr_debug("vcpu %d received sipi with vector # %x\n",
3329 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003330 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003331 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003332 if (r)
3333 return r;
3334 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003335 }
3336
Marcelo Tosattid7690172008-09-08 15:23:48 -03003337 down_read(&vcpu->kvm->slots_lock);
3338 vapic_enter(vcpu);
3339
3340 r = 1;
3341 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003342 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003343 r = vcpu_enter_guest(vcpu, kvm_run);
3344 else {
3345 up_read(&vcpu->kvm->slots_lock);
3346 kvm_vcpu_block(vcpu);
3347 down_read(&vcpu->kvm->slots_lock);
3348 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02003349 {
3350 switch(vcpu->arch.mp_state) {
3351 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003352 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02003353 KVM_MP_STATE_RUNNABLE;
3354 case KVM_MP_STATE_RUNNABLE:
3355 break;
3356 case KVM_MP_STATE_SIPI_RECEIVED:
3357 default:
3358 r = -EINTR;
3359 break;
3360 }
3361 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03003362 }
3363
Gleb Natapov09cec752009-03-23 15:11:44 +02003364 if (r <= 0)
3365 break;
3366
3367 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
3368 if (kvm_cpu_has_pending_timer(vcpu))
3369 kvm_inject_pending_timer_irqs(vcpu);
3370
3371 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
3372 r = -EINTR;
3373 kvm_run->exit_reason = KVM_EXIT_INTR;
3374 ++vcpu->stat.request_irq_exits;
3375 }
3376 if (signal_pending(current)) {
3377 r = -EINTR;
3378 kvm_run->exit_reason = KVM_EXIT_INTR;
3379 ++vcpu->stat.signal_exits;
3380 }
3381 if (need_resched()) {
3382 up_read(&vcpu->kvm->slots_lock);
3383 kvm_resched(vcpu);
3384 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003385 }
3386 }
3387
3388 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003389 post_kvm_run_save(vcpu, kvm_run);
3390
Avi Kivityb93463a2007-10-25 16:52:32 +02003391 vapic_exit(vcpu);
3392
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003393 return r;
3394}
3395
3396int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3397{
3398 int r;
3399 sigset_t sigsaved;
3400
3401 vcpu_load(vcpu);
3402
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003403 if (vcpu->sigset_active)
3404 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3405
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003406 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3407 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003408 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003409 r = -EAGAIN;
3410 goto out;
3411 }
3412
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003413 /* re-sync apic's tpr */
3414 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003415 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003416
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003417 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003418 r = complete_pio(vcpu);
3419 if (r)
3420 goto out;
3421 }
3422#if CONFIG_HAS_IOMEM
3423 if (vcpu->mmio_needed) {
3424 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3425 vcpu->mmio_read_completed = 1;
3426 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003427
3428 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003429 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003430 vcpu->arch.mmio_fault_cr2, 0,
3431 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003432 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003433 if (r == EMULATE_DO_MMIO) {
3434 /*
3435 * Read-modify-write. Back to userspace.
3436 */
3437 r = 0;
3438 goto out;
3439 }
3440 }
3441#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003442 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3443 kvm_register_write(vcpu, VCPU_REGS_RAX,
3444 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003445
3446 r = __vcpu_run(vcpu, kvm_run);
3447
3448out:
3449 if (vcpu->sigset_active)
3450 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3451
3452 vcpu_put(vcpu);
3453 return r;
3454}
3455
3456int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3457{
3458 vcpu_load(vcpu);
3459
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003460 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3461 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3462 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3463 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3464 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3465 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3466 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3467 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003468#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003469 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3470 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3471 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3472 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3473 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3474 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3475 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3476 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003477#endif
3478
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003479 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003480 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3481
3482 /*
3483 * Don't leak debug flags in case they were set for guest debugging
3484 */
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003485 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003486 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3487
3488 vcpu_put(vcpu);
3489
3490 return 0;
3491}
3492
3493int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3494{
3495 vcpu_load(vcpu);
3496
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003497 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3498 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3499 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3500 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3501 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3502 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3503 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3504 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003505#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003506 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3507 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3508 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3509 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3510 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3511 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3512 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3513 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3514
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003515#endif
3516
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003517 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003518 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3519
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003520
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003521 vcpu->arch.exception.pending = false;
3522
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003523 vcpu_put(vcpu);
3524
3525 return 0;
3526}
3527
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003528void kvm_get_segment(struct kvm_vcpu *vcpu,
3529 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003530{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003531 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003532}
3533
3534void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3535{
3536 struct kvm_segment cs;
3537
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003538 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003539 *db = cs.db;
3540 *l = cs.l;
3541}
3542EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3543
3544int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3545 struct kvm_sregs *sregs)
3546{
3547 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003548
3549 vcpu_load(vcpu);
3550
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003551 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3552 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3553 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3554 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3555 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3556 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003557
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003558 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3559 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003560
3561 kvm_x86_ops->get_idt(vcpu, &dt);
3562 sregs->idt.limit = dt.limit;
3563 sregs->idt.base = dt.base;
3564 kvm_x86_ops->get_gdt(vcpu, &dt);
3565 sregs->gdt.limit = dt.limit;
3566 sregs->gdt.base = dt.base;
3567
3568 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003569 sregs->cr0 = vcpu->arch.cr0;
3570 sregs->cr2 = vcpu->arch.cr2;
3571 sregs->cr3 = vcpu->arch.cr3;
3572 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003573 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003574 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003575 sregs->apic_base = kvm_get_apic_base(vcpu);
3576
Gleb Natapov923c61b2009-05-11 13:35:48 +03003577 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003578
Gleb Natapov36752c92009-05-11 13:35:53 +03003579 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03003580 set_bit(vcpu->arch.interrupt.nr,
3581 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03003582
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003583 vcpu_put(vcpu);
3584
3585 return 0;
3586}
3587
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003588int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3589 struct kvm_mp_state *mp_state)
3590{
3591 vcpu_load(vcpu);
3592 mp_state->mp_state = vcpu->arch.mp_state;
3593 vcpu_put(vcpu);
3594 return 0;
3595}
3596
3597int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3598 struct kvm_mp_state *mp_state)
3599{
3600 vcpu_load(vcpu);
3601 vcpu->arch.mp_state = mp_state->mp_state;
3602 vcpu_put(vcpu);
3603 return 0;
3604}
3605
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003606static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003607 struct kvm_segment *var, int seg)
3608{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003609 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003610}
3611
Izik Eidus37817f22008-03-24 23:14:53 +02003612static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3613 struct kvm_segment *kvm_desct)
3614{
3615 kvm_desct->base = seg_desc->base0;
3616 kvm_desct->base |= seg_desc->base1 << 16;
3617 kvm_desct->base |= seg_desc->base2 << 24;
3618 kvm_desct->limit = seg_desc->limit0;
3619 kvm_desct->limit |= seg_desc->limit << 16;
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003620 if (seg_desc->g) {
3621 kvm_desct->limit <<= 12;
3622 kvm_desct->limit |= 0xfff;
3623 }
Izik Eidus37817f22008-03-24 23:14:53 +02003624 kvm_desct->selector = selector;
3625 kvm_desct->type = seg_desc->type;
3626 kvm_desct->present = seg_desc->p;
3627 kvm_desct->dpl = seg_desc->dpl;
3628 kvm_desct->db = seg_desc->d;
3629 kvm_desct->s = seg_desc->s;
3630 kvm_desct->l = seg_desc->l;
3631 kvm_desct->g = seg_desc->g;
3632 kvm_desct->avl = seg_desc->avl;
3633 if (!selector)
3634 kvm_desct->unusable = 1;
3635 else
3636 kvm_desct->unusable = 0;
3637 kvm_desct->padding = 0;
3638}
3639
Amit Shahb8222ad2008-10-22 16:39:47 +05303640static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
3641 u16 selector,
3642 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02003643{
3644 if (selector & 1 << 2) {
3645 struct kvm_segment kvm_seg;
3646
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003647 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003648
3649 if (kvm_seg.unusable)
3650 dtable->limit = 0;
3651 else
3652 dtable->limit = kvm_seg.limit;
3653 dtable->base = kvm_seg.base;
3654 }
3655 else
3656 kvm_x86_ops->get_gdt(vcpu, dtable);
3657}
3658
3659/* allowed just for 8 bytes segments */
3660static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3661 struct desc_struct *seg_desc)
3662{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003663 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003664 struct descriptor_table dtable;
3665 u16 index = selector >> 3;
3666
Amit Shahb8222ad2008-10-22 16:39:47 +05303667 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003668
3669 if (dtable.limit < index * 8 + 7) {
3670 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3671 return 1;
3672 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003673 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3674 gpa += index * 8;
3675 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003676}
3677
3678/* allowed just for 8 bytes segments */
3679static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3680 struct desc_struct *seg_desc)
3681{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003682 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003683 struct descriptor_table dtable;
3684 u16 index = selector >> 3;
3685
Amit Shahb8222ad2008-10-22 16:39:47 +05303686 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003687
3688 if (dtable.limit < index * 8 + 7)
3689 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003690 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3691 gpa += index * 8;
3692 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003693}
3694
3695static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3696 struct desc_struct *seg_desc)
3697{
3698 u32 base_addr;
3699
3700 base_addr = seg_desc->base0;
3701 base_addr |= (seg_desc->base1 << 16);
3702 base_addr |= (seg_desc->base2 << 24);
3703
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003704 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02003705}
3706
Izik Eidus37817f22008-03-24 23:14:53 +02003707static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3708{
3709 struct kvm_segment kvm_seg;
3710
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003711 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003712 return kvm_seg.selector;
3713}
3714
3715static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3716 u16 selector,
3717 struct kvm_segment *kvm_seg)
3718{
3719 struct desc_struct seg_desc;
3720
3721 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3722 return 1;
3723 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3724 return 0;
3725}
3726
Harvey Harrison2259e3a2008-08-22 13:29:17 -07003727static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003728{
3729 struct kvm_segment segvar = {
3730 .base = selector << 4,
3731 .limit = 0xffff,
3732 .selector = selector,
3733 .type = 3,
3734 .present = 1,
3735 .dpl = 3,
3736 .db = 0,
3737 .s = 1,
3738 .l = 0,
3739 .g = 0,
3740 .avl = 0,
3741 .unusable = 0,
3742 };
3743 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
3744 return 0;
3745}
3746
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003747int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3748 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02003749{
3750 struct kvm_segment kvm_seg;
3751
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003752 if (!(vcpu->arch.cr0 & X86_CR0_PE))
3753 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003754 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3755 return 1;
3756 kvm_seg.type |= type_bits;
3757
3758 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3759 seg != VCPU_SREG_LDTR)
3760 if (!kvm_seg.s)
3761 kvm_seg.unusable = 1;
3762
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003763 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003764 return 0;
3765}
3766
3767static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3768 struct tss_segment_32 *tss)
3769{
3770 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003771 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003772 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003773 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3774 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3775 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3776 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3777 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3778 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3779 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3780 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003781 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3782 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3783 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3784 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3785 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3786 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3787 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003788}
3789
3790static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3791 struct tss_segment_32 *tss)
3792{
3793 kvm_set_cr3(vcpu, tss->cr3);
3794
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003795 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02003796 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3797
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003798 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
3799 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
3800 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
3801 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
3802 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
3803 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
3804 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
3805 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02003806
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003807 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003808 return 1;
3809
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003810 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003811 return 1;
3812
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003813 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003814 return 1;
3815
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003816 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003817 return 1;
3818
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003819 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003820 return 1;
3821
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003822 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02003823 return 1;
3824
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003825 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02003826 return 1;
3827 return 0;
3828}
3829
3830static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3831 struct tss_segment_16 *tss)
3832{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003833 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003834 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003835 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3836 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3837 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3838 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3839 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3840 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3841 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
3842 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003843
3844 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3845 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3846 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3847 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3848 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3849 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3850}
3851
3852static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3853 struct tss_segment_16 *tss)
3854{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003855 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02003856 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003857 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
3858 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
3859 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
3860 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
3861 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
3862 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
3863 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
3864 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02003865
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003866 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003867 return 1;
3868
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003869 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003870 return 1;
3871
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003872 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003873 return 1;
3874
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003875 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003876 return 1;
3877
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003878 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003879 return 1;
3880 return 0;
3881}
3882
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003883static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03003884 u16 old_tss_sel, u32 old_tss_base,
3885 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02003886{
3887 struct tss_segment_16 tss_segment_16;
3888 int ret = 0;
3889
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003890 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3891 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003892 goto out;
3893
3894 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02003895
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003896 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3897 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003898 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003899
3900 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3901 &tss_segment_16, sizeof tss_segment_16))
3902 goto out;
3903
Gleb Natapovb237ac32009-03-30 16:03:24 +03003904 if (old_tss_sel != 0xffff) {
3905 tss_segment_16.prev_task_link = old_tss_sel;
3906
3907 if (kvm_write_guest(vcpu->kvm,
3908 get_tss_base_addr(vcpu, nseg_desc),
3909 &tss_segment_16.prev_task_link,
3910 sizeof tss_segment_16.prev_task_link))
3911 goto out;
3912 }
3913
Izik Eidus37817f22008-03-24 23:14:53 +02003914 if (load_state_from_tss16(vcpu, &tss_segment_16))
3915 goto out;
3916
3917 ret = 1;
3918out:
3919 return ret;
3920}
3921
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003922static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03003923 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003924 struct desc_struct *nseg_desc)
3925{
3926 struct tss_segment_32 tss_segment_32;
3927 int ret = 0;
3928
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003929 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3930 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003931 goto out;
3932
3933 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02003934
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003935 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3936 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003937 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003938
3939 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3940 &tss_segment_32, sizeof tss_segment_32))
3941 goto out;
3942
Gleb Natapovb237ac32009-03-30 16:03:24 +03003943 if (old_tss_sel != 0xffff) {
3944 tss_segment_32.prev_task_link = old_tss_sel;
3945
3946 if (kvm_write_guest(vcpu->kvm,
3947 get_tss_base_addr(vcpu, nseg_desc),
3948 &tss_segment_32.prev_task_link,
3949 sizeof tss_segment_32.prev_task_link))
3950 goto out;
3951 }
3952
Izik Eidus37817f22008-03-24 23:14:53 +02003953 if (load_state_from_tss32(vcpu, &tss_segment_32))
3954 goto out;
3955
3956 ret = 1;
3957out:
3958 return ret;
3959}
3960
3961int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3962{
3963 struct kvm_segment tr_seg;
3964 struct desc_struct cseg_desc;
3965 struct desc_struct nseg_desc;
3966 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003967 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
3968 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003969
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003970 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02003971
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003972 /* FIXME: Handle errors. Failure to read either TSS or their
3973 * descriptors should generate a pagefault.
3974 */
Izik Eidus37817f22008-03-24 23:14:53 +02003975 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3976 goto out;
3977
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003978 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02003979 goto out;
3980
Izik Eidus37817f22008-03-24 23:14:53 +02003981 if (reason != TASK_SWITCH_IRET) {
3982 int cpl;
3983
3984 cpl = kvm_x86_ops->get_cpl(vcpu);
3985 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3986 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3987 return 1;
3988 }
3989 }
3990
3991 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3992 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3993 return 1;
3994 }
3995
3996 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003997 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003998 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02003999 }
4000
4001 if (reason == TASK_SWITCH_IRET) {
4002 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4003 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
4004 }
4005
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004006 /* set back link to prev task only if NT bit is set in eflags
4007 note that old_tss_sel is not used afetr this point */
4008 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4009 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02004010
Gleb Natapovb237ac32009-03-30 16:03:24 +03004011 /* set back link to prev task only if NT bit is set in eflags
4012 note that old_tss_sel is not used afetr this point */
4013 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4014 old_tss_sel = 0xffff;
4015
Izik Eidus37817f22008-03-24 23:14:53 +02004016 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03004017 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4018 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004019 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03004020 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4021 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004022
4023 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
4024 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4025 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
4026 }
4027
4028 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004029 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02004030 save_guest_segment_descriptor(vcpu, tss_selector,
4031 &nseg_desc);
4032 }
4033
4034 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4035 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4036 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004037 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004038out:
Izik Eidus37817f22008-03-24 23:14:53 +02004039 return ret;
4040}
4041EXPORT_SYMBOL_GPL(kvm_task_switch);
4042
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004043int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4044 struct kvm_sregs *sregs)
4045{
4046 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004047 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004048 struct descriptor_table dt;
4049
4050 vcpu_load(vcpu);
4051
4052 dt.limit = sregs->idt.limit;
4053 dt.base = sregs->idt.base;
4054 kvm_x86_ops->set_idt(vcpu, &dt);
4055 dt.limit = sregs->gdt.limit;
4056 dt.base = sregs->gdt.base;
4057 kvm_x86_ops->set_gdt(vcpu, &dt);
4058
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004059 vcpu->arch.cr2 = sregs->cr2;
4060 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Marcelo Tosatti59839df2009-04-16 08:30:44 -03004061
4062 down_read(&vcpu->kvm->slots_lock);
4063 if (gfn_to_memslot(vcpu->kvm, sregs->cr3 >> PAGE_SHIFT))
4064 vcpu->arch.cr3 = sregs->cr3;
4065 else
4066 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
4067 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004068
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004069 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004070
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004071 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004072 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004073 kvm_set_apic_base(vcpu, sregs->apic_base);
4074
4075 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4076
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004077 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004078 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004079 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004080
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004081 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004082 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4083 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004084 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004085
4086 if (mmu_reset_needed)
4087 kvm_mmu_reset_context(vcpu);
4088
Gleb Natapov923c61b2009-05-11 13:35:48 +03004089 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4090 pending_vec = find_first_bit(
4091 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4092 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004093 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004094 pr_debug("Set back pending irq %d\n", pending_vec);
4095 if (irqchip_in_kernel(vcpu->kvm))
4096 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004097 }
4098
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004099 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4100 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4101 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4102 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4103 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4104 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004105
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004106 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4107 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004108
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004109 /* Older userspace won't unhalt the vcpu on reset. */
4110 if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 &&
4111 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4112 !(vcpu->arch.cr0 & X86_CR0_PE))
4113 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4114
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004115 vcpu_put(vcpu);
4116
4117 return 0;
4118}
4119
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004120int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4121 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004122{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004123 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004124
4125 vcpu_load(vcpu);
4126
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004127 if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) ==
4128 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) {
4129 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4130 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4131 vcpu->arch.switch_db_regs =
4132 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4133 } else {
4134 for (i = 0; i < KVM_NR_DB_REGS; i++)
4135 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4136 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4137 }
4138
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004139 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
4140
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004141 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4142 kvm_queue_exception(vcpu, DB_VECTOR);
4143 else if (dbg->control & KVM_GUESTDBG_INJECT_BP)
4144 kvm_queue_exception(vcpu, BP_VECTOR);
4145
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004146 vcpu_put(vcpu);
4147
4148 return r;
4149}
4150
4151/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05004152 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4153 * we have asm/x86/processor.h
4154 */
4155struct fxsave {
4156 u16 cwd;
4157 u16 swd;
4158 u16 twd;
4159 u16 fop;
4160 u64 rip;
4161 u64 rdp;
4162 u32 mxcsr;
4163 u32 mxcsr_mask;
4164 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4165#ifdef CONFIG_X86_64
4166 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4167#else
4168 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4169#endif
4170};
4171
Zhang Xiantao8b006792007-11-16 13:05:55 +08004172/*
4173 * Translate a guest virtual address to a guest physical address.
4174 */
4175int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4176 struct kvm_translation *tr)
4177{
4178 unsigned long vaddr = tr->linear_address;
4179 gpa_t gpa;
4180
4181 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004182 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004183 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004184 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004185 tr->physical_address = gpa;
4186 tr->valid = gpa != UNMAPPED_GVA;
4187 tr->writeable = 1;
4188 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004189 vcpu_put(vcpu);
4190
4191 return 0;
4192}
4193
Hollis Blanchardd0752062007-10-31 17:24:25 -05004194int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4195{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004196 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004197
4198 vcpu_load(vcpu);
4199
4200 memcpy(fpu->fpr, fxsave->st_space, 128);
4201 fpu->fcw = fxsave->cwd;
4202 fpu->fsw = fxsave->swd;
4203 fpu->ftwx = fxsave->twd;
4204 fpu->last_opcode = fxsave->fop;
4205 fpu->last_ip = fxsave->rip;
4206 fpu->last_dp = fxsave->rdp;
4207 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4208
4209 vcpu_put(vcpu);
4210
4211 return 0;
4212}
4213
4214int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4215{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004216 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004217
4218 vcpu_load(vcpu);
4219
4220 memcpy(fxsave->st_space, fpu->fpr, 128);
4221 fxsave->cwd = fpu->fcw;
4222 fxsave->swd = fpu->fsw;
4223 fxsave->twd = fpu->ftwx;
4224 fxsave->fop = fpu->last_opcode;
4225 fxsave->rip = fpu->last_ip;
4226 fxsave->rdp = fpu->last_dp;
4227 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4228
4229 vcpu_put(vcpu);
4230
4231 return 0;
4232}
4233
4234void fx_init(struct kvm_vcpu *vcpu)
4235{
4236 unsigned after_mxcsr_mask;
4237
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004238 /*
4239 * Touch the fpu the first time in non atomic context as if
4240 * this is the first fpu instruction the exception handler
4241 * will fire before the instruction returns and it'll have to
4242 * allocate ram with GFP_KERNEL.
4243 */
4244 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004245 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004246
Hollis Blanchardd0752062007-10-31 17:24:25 -05004247 /* Initialize guest FPU by resetting ours and saving into guest's */
4248 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004249 kvm_fx_save(&vcpu->arch.host_fx_image);
4250 kvm_fx_finit();
4251 kvm_fx_save(&vcpu->arch.guest_fx_image);
4252 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004253 preempt_enable();
4254
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004255 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004256 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004257 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4258 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004259 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4260}
4261EXPORT_SYMBOL_GPL(fx_init);
4262
4263void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4264{
4265 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4266 return;
4267
4268 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004269 kvm_fx_save(&vcpu->arch.host_fx_image);
4270 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004271}
4272EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4273
4274void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4275{
4276 if (!vcpu->guest_fpu_loaded)
4277 return;
4278
4279 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004280 kvm_fx_save(&vcpu->arch.guest_fx_image);
4281 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004282 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004283}
4284EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004285
4286void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4287{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01004288 if (vcpu->arch.time_page) {
4289 kvm_release_page_dirty(vcpu->arch.time_page);
4290 vcpu->arch.time_page = NULL;
4291 }
4292
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004293 kvm_x86_ops->vcpu_free(vcpu);
4294}
4295
4296struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4297 unsigned int id)
4298{
Avi Kivity26e52152007-11-20 15:30:24 +02004299 return kvm_x86_ops->vcpu_create(kvm, id);
4300}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004301
Avi Kivity26e52152007-11-20 15:30:24 +02004302int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4303{
4304 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004305
4306 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004307 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004308
Sheng Yang0bed3b52008-10-09 16:01:54 +08004309 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004310 vcpu_load(vcpu);
4311 r = kvm_arch_vcpu_reset(vcpu);
4312 if (r == 0)
4313 r = kvm_mmu_setup(vcpu);
4314 vcpu_put(vcpu);
4315 if (r < 0)
4316 goto free_vcpu;
4317
Avi Kivity26e52152007-11-20 15:30:24 +02004318 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004319free_vcpu:
4320 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004321 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004322}
4323
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004324void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004325{
4326 vcpu_load(vcpu);
4327 kvm_mmu_unload(vcpu);
4328 vcpu_put(vcpu);
4329
4330 kvm_x86_ops->vcpu_free(vcpu);
4331}
4332
4333int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4334{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004335 vcpu->arch.nmi_pending = false;
4336 vcpu->arch.nmi_injected = false;
4337
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004338 vcpu->arch.switch_db_regs = 0;
4339 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4340 vcpu->arch.dr6 = DR6_FIXED_1;
4341 vcpu->arch.dr7 = DR7_FIXED_1;
4342
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004343 return kvm_x86_ops->vcpu_reset(vcpu);
4344}
4345
4346void kvm_arch_hardware_enable(void *garbage)
4347{
4348 kvm_x86_ops->hardware_enable(garbage);
4349}
4350
4351void kvm_arch_hardware_disable(void *garbage)
4352{
4353 kvm_x86_ops->hardware_disable(garbage);
4354}
4355
4356int kvm_arch_hardware_setup(void)
4357{
4358 return kvm_x86_ops->hardware_setup();
4359}
4360
4361void kvm_arch_hardware_unsetup(void)
4362{
4363 kvm_x86_ops->hardware_unsetup();
4364}
4365
4366void kvm_arch_check_processor_compat(void *rtn)
4367{
4368 kvm_x86_ops->check_processor_compatibility(rtn);
4369}
4370
4371int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4372{
4373 struct page *page;
4374 struct kvm *kvm;
4375 int r;
4376
4377 BUG_ON(vcpu->kvm == NULL);
4378 kvm = vcpu->kvm;
4379
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004380 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004381 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Avi Kivitya4535292008-04-13 17:54:35 +03004382 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004383 else
Avi Kivitya4535292008-04-13 17:54:35 +03004384 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004385
4386 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4387 if (!page) {
4388 r = -ENOMEM;
4389 goto fail;
4390 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004391 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004392
4393 r = kvm_mmu_create(vcpu);
4394 if (r < 0)
4395 goto fail_free_pio_data;
4396
4397 if (irqchip_in_kernel(kvm)) {
4398 r = kvm_create_lapic(vcpu);
4399 if (r < 0)
4400 goto fail_mmu_destroy;
4401 }
4402
4403 return 0;
4404
4405fail_mmu_destroy:
4406 kvm_mmu_destroy(vcpu);
4407fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004408 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004409fail:
4410 return r;
4411}
4412
4413void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4414{
4415 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004416 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004417 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004418 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004419 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004420}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004421
4422struct kvm *kvm_arch_create_vm(void)
4423{
4424 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4425
4426 if (!kvm)
4427 return ERR_PTR(-ENOMEM);
4428
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004429 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004430 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004431
Sheng Yang5550af42008-10-15 20:15:06 +08004432 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4433 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
4434
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01004435 rdtscll(kvm->arch.vm_init_tsc);
4436
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004437 return kvm;
4438}
4439
4440static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4441{
4442 vcpu_load(vcpu);
4443 kvm_mmu_unload(vcpu);
4444 vcpu_put(vcpu);
4445}
4446
4447static void kvm_free_vcpus(struct kvm *kvm)
4448{
4449 unsigned int i;
4450
4451 /*
4452 * Unpin any mmu pages first.
4453 */
4454 for (i = 0; i < KVM_MAX_VCPUS; ++i)
4455 if (kvm->vcpus[i])
4456 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
4457 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
4458 if (kvm->vcpus[i]) {
4459 kvm_arch_vcpu_free(kvm->vcpus[i]);
4460 kvm->vcpus[i] = NULL;
4461 }
4462 }
4463
4464}
4465
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004466void kvm_arch_sync_events(struct kvm *kvm)
4467{
Sheng Yangba4cef32009-01-06 10:03:03 +08004468 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004469}
4470
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004471void kvm_arch_destroy_vm(struct kvm *kvm)
4472{
Sheng Yang6eb55812008-10-31 12:37:41 +08004473 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08004474 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08004475 kfree(kvm->arch.vpic);
4476 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004477 kvm_free_vcpus(kvm);
4478 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02004479 if (kvm->arch.apic_access_page)
4480 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004481 if (kvm->arch.ept_identity_pagetable)
4482 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004483 kfree(kvm);
4484}
Zhang Xiantao0de10342007-11-20 16:25:04 +08004485
4486int kvm_arch_set_memory_region(struct kvm *kvm,
4487 struct kvm_userspace_memory_region *mem,
4488 struct kvm_memory_slot old,
4489 int user_alloc)
4490{
4491 int npages = mem->memory_size >> PAGE_SHIFT;
4492 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4493
4494 /*To keep backward compatibility with older userspace,
4495 *x86 needs to hanlde !user_alloc case.
4496 */
4497 if (!user_alloc) {
4498 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004499 unsigned long userspace_addr;
4500
Izik Eidus72dc67a2008-02-10 18:04:15 +02004501 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004502 userspace_addr = do_mmap(NULL, 0,
4503 npages * PAGE_SIZE,
4504 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03004505 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004506 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004507 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004508
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004509 if (IS_ERR((void *)userspace_addr))
4510 return PTR_ERR((void *)userspace_addr);
4511
4512 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4513 spin_lock(&kvm->mmu_lock);
4514 memslot->userspace_addr = userspace_addr;
4515 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004516 } else {
4517 if (!old.user_alloc && old.rmap) {
4518 int ret;
4519
Izik Eidus72dc67a2008-02-10 18:04:15 +02004520 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004521 ret = do_munmap(current->mm, old.userspace_addr,
4522 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004523 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004524 if (ret < 0)
4525 printk(KERN_WARNING
4526 "kvm_vm_ioctl_set_memory_region: "
4527 "failed to munmap memory\n");
4528 }
4529 }
4530 }
4531
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004532 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004533 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004534 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4535 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4536 }
4537
4538 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004539 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004540 kvm_flush_remote_tlbs(kvm);
4541
4542 return 0;
4543}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004544
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004545void kvm_arch_flush_shadow(struct kvm *kvm)
4546{
4547 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004548 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004549}
4550
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004551int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4552{
Avi Kivitya4535292008-04-13 17:54:35 +03004553 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Jan Kiszka0496fbb2008-09-26 09:30:53 +02004554 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
4555 || vcpu->arch.nmi_pending;
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004556}
Zhang Xiantao57361992007-12-17 14:21:40 +08004557
Zhang Xiantao57361992007-12-17 14:21:40 +08004558void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4559{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004560 int me;
4561 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08004562
4563 if (waitqueue_active(&vcpu->wq)) {
4564 wake_up_interruptible(&vcpu->wq);
4565 ++vcpu->stat.halt_wakeup;
4566 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004567
4568 me = get_cpu();
4569 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
4570 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
4571 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004572 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004573}
Gleb Natapov78646122009-03-23 12:12:11 +02004574
4575int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
4576{
4577 return kvm_x86_ops->interrupt_allowed(vcpu);
4578}