blob: 10302d3bd4157d0068b9beebdae6823bc1f96f41 [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>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030037#include <linux/intel-iommu.h>
Carsten Otte043405e2007-10-10 17:16:19 +020038
39#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080040#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020041#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080042#include <asm/mtrr.h>
Carsten Otte043405e2007-10-10 17:16:19 +020043
Carsten Otte313a3dc2007-10-11 19:16:52 +020044#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010045#define CR0_RESERVED_BITS \
46 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
47 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
48 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
49#define CR4_RESERVED_BITS \
50 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
51 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
52 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
53 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
54
55#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Joerg Roedel50a37eb2008-01-31 14:57:38 +010056/* EFER defaults:
57 * - enable syscall per default because its emulated by KVM
58 * - enable LME and LMA per default on 64 bit KVM
59 */
60#ifdef CONFIG_X86_64
61static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
62#else
63static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
64#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020065
Avi Kivityba1389b2007-11-18 16:24:12 +020066#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
67#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050068
Avi Kivity674eea02008-02-11 18:37:23 +020069static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
70 struct kvm_cpuid_entry2 __user *entries);
71
Zhang Xiantao97896d02007-11-14 20:09:30 +080072struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030073EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080074
Hollis Blanchard417bc302007-10-31 17:24:23 -050075struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020076 { "pf_fixed", VCPU_STAT(pf_fixed) },
77 { "pf_guest", VCPU_STAT(pf_guest) },
78 { "tlb_flush", VCPU_STAT(tlb_flush) },
79 { "invlpg", VCPU_STAT(invlpg) },
80 { "exits", VCPU_STAT(exits) },
81 { "io_exits", VCPU_STAT(io_exits) },
82 { "mmio_exits", VCPU_STAT(mmio_exits) },
83 { "signal_exits", VCPU_STAT(signal_exits) },
84 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +080085 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020086 { "halt_exits", VCPU_STAT(halt_exits) },
87 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +053088 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020089 { "request_irq", VCPU_STAT(request_irq_exits) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +020090 { "request_nmi", VCPU_STAT(request_nmi_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020091 { "irq_exits", VCPU_STAT(irq_exits) },
92 { "host_state_reload", VCPU_STAT(host_state_reload) },
93 { "efer_reload", VCPU_STAT(efer_reload) },
94 { "fpu_reload", VCPU_STAT(fpu_reload) },
95 { "insn_emulation", VCPU_STAT(insn_emulation) },
96 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +030097 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +020098 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +020099 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
100 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
101 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
102 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
103 { "mmu_flooded", VM_STAT(mmu_flooded) },
104 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200105 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300106 { "mmu_unsync", VM_STAT(mmu_unsync) },
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200107 { "mmu_unsync_global", VM_STAT(mmu_unsync_global) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200108 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300109 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500110 { NULL }
111};
112
Carsten Otte5fb76f92007-10-29 16:08:51 +0100113unsigned long segment_base(u16 selector)
114{
115 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200116 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100117 unsigned long table_base;
118 unsigned long v;
119
120 if (selector == 0)
121 return 0;
122
123 asm("sgdt %0" : "=m"(gdt));
124 table_base = gdt.base;
125
126 if (selector & 4) { /* from ldt */
127 u16 ldt_selector;
128
129 asm("sldt %0" : "=g"(ldt_selector));
130 table_base = segment_base(ldt_selector);
131 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200132 d = (struct desc_struct *)(table_base + (selector & ~7));
133 v = d->base0 | ((unsigned long)d->base1 << 16) |
134 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100135#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200136 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
137 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100138#endif
139 return v;
140}
141EXPORT_SYMBOL_GPL(segment_base);
142
Carsten Otte6866b832007-10-29 16:09:10 +0100143u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
144{
145 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800146 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100147 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800148 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100149}
150EXPORT_SYMBOL_GPL(kvm_get_apic_base);
151
152void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
153{
154 /* TODO: reserve bits check */
155 if (irqchip_in_kernel(vcpu->kvm))
156 kvm_lapic_set_base(vcpu, data);
157 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800158 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100159}
160EXPORT_SYMBOL_GPL(kvm_set_apic_base);
161
Avi Kivity298101d2007-11-25 13:41:11 +0200162void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
163{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800164 WARN_ON(vcpu->arch.exception.pending);
165 vcpu->arch.exception.pending = true;
166 vcpu->arch.exception.has_error_code = false;
167 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200168}
169EXPORT_SYMBOL_GPL(kvm_queue_exception);
170
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200171void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
172 u32 error_code)
173{
174 ++vcpu->stat.pf_guest;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100175 if (vcpu->arch.exception.pending) {
176 if (vcpu->arch.exception.nr == PF_VECTOR) {
177 printk(KERN_DEBUG "kvm: inject_page_fault:"
178 " double fault 0x%lx\n", addr);
179 vcpu->arch.exception.nr = DF_VECTOR;
180 vcpu->arch.exception.error_code = 0;
181 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
182 /* triple fault -> shutdown */
183 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
184 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200185 return;
186 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800187 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200188 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
189}
190
Sheng Yang3419ffc2008-05-15 09:52:48 +0800191void kvm_inject_nmi(struct kvm_vcpu *vcpu)
192{
193 vcpu->arch.nmi_pending = 1;
194}
195EXPORT_SYMBOL_GPL(kvm_inject_nmi);
196
Avi Kivity298101d2007-11-25 13:41:11 +0200197void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 WARN_ON(vcpu->arch.exception.pending);
200 vcpu->arch.exception.pending = true;
201 vcpu->arch.exception.has_error_code = true;
202 vcpu->arch.exception.nr = nr;
203 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200204}
205EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
206
207static void __queue_exception(struct kvm_vcpu *vcpu)
208{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800209 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
210 vcpu->arch.exception.has_error_code,
211 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200212}
213
Carsten Ottea03490e2007-10-29 16:09:35 +0100214/*
215 * Load the pae pdptrs. Return true is they are all valid.
216 */
217int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
218{
219 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
220 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
221 int i;
222 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800223 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100224
Carsten Ottea03490e2007-10-29 16:09:35 +0100225 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
226 offset * sizeof(u64), sizeof(pdpte));
227 if (ret < 0) {
228 ret = 0;
229 goto out;
230 }
231 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
232 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
233 ret = 0;
234 goto out;
235 }
236 }
237 ret = 1;
238
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800239 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100240out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100241
242 return ret;
243}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100244EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100245
Avi Kivityd835dfe2007-11-21 02:57:59 +0200246static bool pdptrs_changed(struct kvm_vcpu *vcpu)
247{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800248 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200249 bool changed = true;
250 int r;
251
252 if (is_long_mode(vcpu) || !is_pae(vcpu))
253 return false;
254
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800255 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200256 if (r < 0)
257 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800258 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200259out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200260
261 return changed;
262}
263
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200264void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100265{
266 if (cr0 & CR0_RESERVED_BITS) {
267 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800268 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200269 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100270 return;
271 }
272
273 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
274 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200275 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100276 return;
277 }
278
279 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
280 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
281 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200282 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100283 return;
284 }
285
286 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
287#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800288 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100289 int cs_db, cs_l;
290
291 if (!is_pae(vcpu)) {
292 printk(KERN_DEBUG "set_cr0: #GP, start paging "
293 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200294 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100295 return;
296 }
297 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
298 if (cs_l) {
299 printk(KERN_DEBUG "set_cr0: #GP, start paging "
300 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200301 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100302 return;
303
304 }
305 } else
306#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800307 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100308 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
309 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200310 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100311 return;
312 }
313
314 }
315
316 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800317 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100318
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200319 kvm_mmu_sync_global(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100320 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100321 return;
322}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200323EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100324
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200325void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100326{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200327 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -0400328 KVMTRACE_1D(LMSW, vcpu,
329 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
330 handler);
Carsten Ottea03490e2007-10-29 16:09:35 +0100331}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200332EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100333
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200334void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100335{
336 if (cr4 & CR4_RESERVED_BITS) {
337 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200338 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100339 return;
340 }
341
342 if (is_long_mode(vcpu)) {
343 if (!(cr4 & X86_CR4_PAE)) {
344 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
345 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200346 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100347 return;
348 }
349 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800350 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100351 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200352 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100353 return;
354 }
355
356 if (cr4 & X86_CR4_VMXE) {
357 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200358 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100359 return;
360 }
361 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800362 vcpu->arch.cr4 = cr4;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200363 kvm_mmu_sync_global(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100364 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100365}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200366EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100367
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200368void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100369{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800370 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300371 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200372 kvm_mmu_flush_tlb(vcpu);
373 return;
374 }
375
Carsten Ottea03490e2007-10-29 16:09:35 +0100376 if (is_long_mode(vcpu)) {
377 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
378 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200379 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100380 return;
381 }
382 } else {
383 if (is_pae(vcpu)) {
384 if (cr3 & CR3_PAE_RESERVED_BITS) {
385 printk(KERN_DEBUG
386 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200387 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100388 return;
389 }
390 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
391 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
392 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200393 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100394 return;
395 }
396 }
397 /*
398 * We don't check reserved bits in nonpae mode, because
399 * this isn't enforced, and VMware depends on this.
400 */
401 }
402
Carsten Ottea03490e2007-10-29 16:09:35 +0100403 /*
404 * Does the new cr3 value map to physical memory? (Note, we
405 * catch an invalid cr3 even in real-mode, because it would
406 * cause trouble later on when we turn on paging anyway.)
407 *
408 * A real CPU would silently accept an invalid cr3 and would
409 * attempt to use it - with largely undefined (and often hard
410 * to debug) behavior on the guest side.
411 */
412 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200413 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100414 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800415 vcpu->arch.cr3 = cr3;
416 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100417 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100418}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200419EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100420
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200421void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100422{
423 if (cr8 & CR8_RESERVED_BITS) {
424 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200425 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100426 return;
427 }
428 if (irqchip_in_kernel(vcpu->kvm))
429 kvm_lapic_set_tpr(vcpu, cr8);
430 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800431 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100432}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200433EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100434
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200435unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100436{
437 if (irqchip_in_kernel(vcpu->kvm))
438 return kvm_lapic_get_cr8(vcpu);
439 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800440 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100441}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200442EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100443
Carsten Otte043405e2007-10-10 17:16:19 +0200444/*
445 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
446 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
447 *
448 * This list is modified at module load time to reflect the
449 * capabilities of the host cpu.
450 */
451static u32 msrs_to_save[] = {
452 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
453 MSR_K6_STAR,
454#ifdef CONFIG_X86_64
455 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
456#endif
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200457 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Sheng Yang468d4722008-10-09 16:01:55 +0800458 MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT
Carsten Otte043405e2007-10-10 17:16:19 +0200459};
460
461static unsigned num_msrs_to_save;
462
463static u32 emulated_msrs[] = {
464 MSR_IA32_MISC_ENABLE,
465};
466
Carsten Otte15c4a642007-10-30 18:44:17 +0100467static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
468{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100469 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100470 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
471 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200472 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100473 return;
474 }
475
476 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800477 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100478 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200479 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100480 return;
481 }
482
483 kvm_x86_ops->set_efer(vcpu, efer);
484
485 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800486 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100487
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800488 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100489}
490
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100491void kvm_enable_efer_bits(u64 mask)
492{
493 efer_reserved_bits &= ~mask;
494}
495EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
496
497
Carsten Otte15c4a642007-10-30 18:44:17 +0100498/*
499 * Writes msr value into into the appropriate "register".
500 * Returns 0 on success, non-0 otherwise.
501 * Assumes vcpu_load() was already called.
502 */
503int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
504{
505 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
506}
507
Carsten Otte313a3dc2007-10-11 19:16:52 +0200508/*
509 * Adapt set_msr() to msr_io()'s calling convention
510 */
511static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
512{
513 return kvm_set_msr(vcpu, index, *data);
514}
515
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200516static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
517{
518 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200519 struct pvclock_wall_clock wc;
520 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200521
522 if (!wall_clock)
523 return;
524
525 version++;
526
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200527 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
528
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200529 /*
530 * The guest calculates current wall clock time by adding
531 * system time (updated by kvm_write_guest_time below) to the
532 * wall clock specified here. guest system time equals host
533 * system time for us, thus we must fill in host boot time here.
534 */
535 now = current_kernel_time();
536 ktime_get_ts(&sys);
537 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
538
539 wc.sec = boot.tv_sec;
540 wc.nsec = boot.tv_nsec;
541 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200542
543 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
544
545 version++;
546 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200547}
548
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200549static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
550{
551 uint32_t quotient, remainder;
552
553 /* Don't try to replace with do_div(), this one calculates
554 * "(dividend << 32) / divisor" */
555 __asm__ ( "divl %4"
556 : "=a" (quotient), "=d" (remainder)
557 : "0" (0), "1" (dividend), "r" (divisor) );
558 return quotient;
559}
560
561static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
562{
563 uint64_t nsecs = 1000000000LL;
564 int32_t shift = 0;
565 uint64_t tps64;
566 uint32_t tps32;
567
568 tps64 = tsc_khz * 1000LL;
569 while (tps64 > nsecs*2) {
570 tps64 >>= 1;
571 shift--;
572 }
573
574 tps32 = (uint32_t)tps64;
575 while (tps32 <= (uint32_t)nsecs) {
576 tps32 <<= 1;
577 shift++;
578 }
579
580 hv_clock->tsc_shift = shift;
581 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
582
583 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700584 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200585 hv_clock->tsc_to_system_mul);
586}
587
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200588static void kvm_write_guest_time(struct kvm_vcpu *v)
589{
590 struct timespec ts;
591 unsigned long flags;
592 struct kvm_vcpu_arch *vcpu = &v->arch;
593 void *shared_kaddr;
594
595 if ((!vcpu->time_page))
596 return;
597
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200598 if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
599 kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
600 vcpu->hv_clock_tsc_khz = tsc_khz;
601 }
602
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200603 /* Keep irq disabled to prevent changes to the clock */
604 local_irq_save(flags);
605 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
606 &vcpu->hv_clock.tsc_timestamp);
607 ktime_get_ts(&ts);
608 local_irq_restore(flags);
609
610 /* With all the info we got, fill in the values */
611
612 vcpu->hv_clock.system_time = ts.tv_nsec +
613 (NSEC_PER_SEC * (u64)ts.tv_sec);
614 /*
615 * The interface expects us to write an even number signaling that the
616 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200617 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200618 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200619 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200620
621 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
622
623 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200624 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200625
626 kunmap_atomic(shared_kaddr, KM_USER0);
627
628 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
629}
630
Avi Kivity9ba075a2008-05-26 20:06:35 +0300631static bool msr_mtrr_valid(unsigned msr)
632{
633 switch (msr) {
634 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
635 case MSR_MTRRfix64K_00000:
636 case MSR_MTRRfix16K_80000:
637 case MSR_MTRRfix16K_A0000:
638 case MSR_MTRRfix4K_C0000:
639 case MSR_MTRRfix4K_C8000:
640 case MSR_MTRRfix4K_D0000:
641 case MSR_MTRRfix4K_D8000:
642 case MSR_MTRRfix4K_E0000:
643 case MSR_MTRRfix4K_E8000:
644 case MSR_MTRRfix4K_F0000:
645 case MSR_MTRRfix4K_F8000:
646 case MSR_MTRRdefType:
647 case MSR_IA32_CR_PAT:
648 return true;
649 case 0x2f8:
650 return true;
651 }
652 return false;
653}
654
655static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
656{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800657 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
658
Avi Kivity9ba075a2008-05-26 20:06:35 +0300659 if (!msr_mtrr_valid(msr))
660 return 1;
661
Sheng Yang0bed3b52008-10-09 16:01:54 +0800662 if (msr == MSR_MTRRdefType) {
663 vcpu->arch.mtrr_state.def_type = data;
664 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
665 } else if (msr == MSR_MTRRfix64K_00000)
666 p[0] = data;
667 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
668 p[1 + msr - MSR_MTRRfix16K_80000] = data;
669 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
670 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
671 else if (msr == MSR_IA32_CR_PAT)
672 vcpu->arch.pat = data;
673 else { /* Variable MTRRs */
674 int idx, is_mtrr_mask;
675 u64 *pt;
676
677 idx = (msr - 0x200) / 2;
678 is_mtrr_mask = msr - 0x200 - 2 * idx;
679 if (!is_mtrr_mask)
680 pt =
681 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
682 else
683 pt =
684 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
685 *pt = data;
686 }
687
688 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300689 return 0;
690}
Carsten Otte15c4a642007-10-30 18:44:17 +0100691
692int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
693{
694 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100695 case MSR_EFER:
696 set_efer(vcpu, data);
697 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100698 case MSR_IA32_MC0_STATUS:
699 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800700 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100701 break;
702 case MSR_IA32_MCG_STATUS:
703 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800704 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100705 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100706 case MSR_IA32_MCG_CTL:
707 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800708 __func__, data);
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100709 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200710 case MSR_IA32_DEBUGCTLMSR:
711 if (!data) {
712 /* We support the non-activated case already */
713 break;
714 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
715 /* Values other than LBR and BTF are vendor-specific,
716 thus reserved and should throw a #GP */
717 return 1;
718 }
719 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
720 __func__, data);
721 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100722 case MSR_IA32_UCODE_REV:
723 case MSR_IA32_UCODE_WRITE:
Carsten Otte15c4a642007-10-30 18:44:17 +0100724 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300725 case 0x200 ... 0x2ff:
726 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100727 case MSR_IA32_APICBASE:
728 kvm_set_apic_base(vcpu, data);
729 break;
730 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800731 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100732 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200733 case MSR_KVM_WALL_CLOCK:
734 vcpu->kvm->arch.wall_clock = data;
735 kvm_write_wall_clock(vcpu->kvm, data);
736 break;
737 case MSR_KVM_SYSTEM_TIME: {
738 if (vcpu->arch.time_page) {
739 kvm_release_page_dirty(vcpu->arch.time_page);
740 vcpu->arch.time_page = NULL;
741 }
742
743 vcpu->arch.time = data;
744
745 /* we verify if the enable bit is set... */
746 if (!(data & 1))
747 break;
748
749 /* ...but clean it before doing the actual write */
750 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
751
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200752 vcpu->arch.time_page =
753 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200754
755 if (is_error_page(vcpu->arch.time_page)) {
756 kvm_release_page_clean(vcpu->arch.time_page);
757 vcpu->arch.time_page = NULL;
758 }
759
760 kvm_write_guest_time(vcpu);
761 break;
762 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100763 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200764 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100765 return 1;
766 }
767 return 0;
768}
769EXPORT_SYMBOL_GPL(kvm_set_msr_common);
770
771
772/*
773 * Reads an msr value (of 'msr_index') into 'pdata'.
774 * Returns 0 on success, non-0 otherwise.
775 * Assumes vcpu_load() was already called.
776 */
777int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
778{
779 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
780}
781
Avi Kivity9ba075a2008-05-26 20:06:35 +0300782static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
783{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800784 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
785
Avi Kivity9ba075a2008-05-26 20:06:35 +0300786 if (!msr_mtrr_valid(msr))
787 return 1;
788
Sheng Yang0bed3b52008-10-09 16:01:54 +0800789 if (msr == MSR_MTRRdefType)
790 *pdata = vcpu->arch.mtrr_state.def_type +
791 (vcpu->arch.mtrr_state.enabled << 10);
792 else if (msr == MSR_MTRRfix64K_00000)
793 *pdata = p[0];
794 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
795 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
796 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
797 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
798 else if (msr == MSR_IA32_CR_PAT)
799 *pdata = vcpu->arch.pat;
800 else { /* Variable MTRRs */
801 int idx, is_mtrr_mask;
802 u64 *pt;
803
804 idx = (msr - 0x200) / 2;
805 is_mtrr_mask = msr - 0x200 - 2 * idx;
806 if (!is_mtrr_mask)
807 pt =
808 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
809 else
810 pt =
811 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
812 *pdata = *pt;
813 }
814
Avi Kivity9ba075a2008-05-26 20:06:35 +0300815 return 0;
816}
817
Carsten Otte15c4a642007-10-30 18:44:17 +0100818int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
819{
820 u64 data;
821
822 switch (msr) {
823 case 0xc0010010: /* SYSCFG */
824 case 0xc0010015: /* HWCR */
825 case MSR_IA32_PLATFORM_ID:
826 case MSR_IA32_P5_MC_ADDR:
827 case MSR_IA32_P5_MC_TYPE:
828 case MSR_IA32_MC0_CTL:
829 case MSR_IA32_MCG_STATUS:
830 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100831 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100832 case MSR_IA32_MC0_MISC:
833 case MSR_IA32_MC0_MISC+4:
834 case MSR_IA32_MC0_MISC+8:
835 case MSR_IA32_MC0_MISC+12:
836 case MSR_IA32_MC0_MISC+16:
Joerg Roedela89c1ad2008-08-29 11:52:07 +0200837 case MSR_IA32_MC0_MISC+20:
Carsten Otte15c4a642007-10-30 18:44:17 +0100838 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100839 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200840 case MSR_IA32_DEBUGCTLMSR:
841 case MSR_IA32_LASTBRANCHFROMIP:
842 case MSR_IA32_LASTBRANCHTOIP:
843 case MSR_IA32_LASTINTFROMIP:
844 case MSR_IA32_LASTINTTOIP:
Carsten Otte15c4a642007-10-30 18:44:17 +0100845 data = 0;
846 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300847 case MSR_MTRRcap:
848 data = 0x500 | KVM_NR_VAR_MTRR;
849 break;
850 case 0x200 ... 0x2ff:
851 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +0100852 case 0xcd: /* fsb frequency */
853 data = 3;
854 break;
855 case MSR_IA32_APICBASE:
856 data = kvm_get_apic_base(vcpu);
857 break;
858 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800859 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100860 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +0100861 case MSR_IA32_PERF_STATUS:
862 /* TSC increment by tick */
863 data = 1000ULL;
864 /* CPU multiplier */
865 data |= (((uint64_t)4ULL) << 40);
866 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100867 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800868 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100869 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200870 case MSR_KVM_WALL_CLOCK:
871 data = vcpu->kvm->arch.wall_clock;
872 break;
873 case MSR_KVM_SYSTEM_TIME:
874 data = vcpu->arch.time;
875 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100876 default:
877 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
878 return 1;
879 }
880 *pdata = data;
881 return 0;
882}
883EXPORT_SYMBOL_GPL(kvm_get_msr_common);
884
Carsten Otte313a3dc2007-10-11 19:16:52 +0200885/*
886 * Read or write a bunch of msrs. All parameters are kernel addresses.
887 *
888 * @return number of msrs set successfully.
889 */
890static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
891 struct kvm_msr_entry *entries,
892 int (*do_msr)(struct kvm_vcpu *vcpu,
893 unsigned index, u64 *data))
894{
895 int i;
896
897 vcpu_load(vcpu);
898
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300899 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200900 for (i = 0; i < msrs->nmsrs; ++i)
901 if (do_msr(vcpu, entries[i].index, &entries[i].data))
902 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300903 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200904
905 vcpu_put(vcpu);
906
907 return i;
908}
909
910/*
911 * Read or write a bunch of msrs. Parameters are user addresses.
912 *
913 * @return number of msrs set successfully.
914 */
915static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
916 int (*do_msr)(struct kvm_vcpu *vcpu,
917 unsigned index, u64 *data),
918 int writeback)
919{
920 struct kvm_msrs msrs;
921 struct kvm_msr_entry *entries;
922 int r, n;
923 unsigned size;
924
925 r = -EFAULT;
926 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
927 goto out;
928
929 r = -E2BIG;
930 if (msrs.nmsrs >= MAX_IO_MSRS)
931 goto out;
932
933 r = -ENOMEM;
934 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
935 entries = vmalloc(size);
936 if (!entries)
937 goto out;
938
939 r = -EFAULT;
940 if (copy_from_user(entries, user_msrs->entries, size))
941 goto out_free;
942
943 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
944 if (r < 0)
945 goto out_free;
946
947 r = -EFAULT;
948 if (writeback && copy_to_user(user_msrs->entries, entries, size))
949 goto out_free;
950
951 r = n;
952
953out_free:
954 vfree(entries);
955out:
956 return r;
957}
958
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800959int kvm_dev_ioctl_check_extension(long ext)
960{
961 int r;
962
963 switch (ext) {
964 case KVM_CAP_IRQCHIP:
965 case KVM_CAP_HLT:
966 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800967 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200968 case KVM_CAP_EXT_CPUID:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200969 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +0800970 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -0500971 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -0300972 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +0300973 case KVM_CAP_SYNC_MMU:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800974 r = 1;
975 break;
Laurent Vivier542472b2008-05-30 16:05:55 +0200976 case KVM_CAP_COALESCED_MMIO:
977 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
978 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200979 case KVM_CAP_VAPIC:
980 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
981 break;
Avi Kivityf7252302008-02-20 11:53:16 +0200982 case KVM_CAP_NR_VCPUS:
983 r = KVM_MAX_VCPUS;
984 break;
Avi Kivitya988b912008-02-20 11:59:20 +0200985 case KVM_CAP_NR_MEMSLOTS:
986 r = KVM_MEMORY_SLOTS;
987 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500988 case KVM_CAP_PV_MMU:
989 r = !tdp_enabled;
990 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300991 case KVM_CAP_IOMMU:
992 r = intel_iommu_found();
993 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800994 default:
995 r = 0;
996 break;
997 }
998 return r;
999
1000}
1001
Carsten Otte043405e2007-10-10 17:16:19 +02001002long kvm_arch_dev_ioctl(struct file *filp,
1003 unsigned int ioctl, unsigned long arg)
1004{
1005 void __user *argp = (void __user *)arg;
1006 long r;
1007
1008 switch (ioctl) {
1009 case KVM_GET_MSR_INDEX_LIST: {
1010 struct kvm_msr_list __user *user_msr_list = argp;
1011 struct kvm_msr_list msr_list;
1012 unsigned n;
1013
1014 r = -EFAULT;
1015 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1016 goto out;
1017 n = msr_list.nmsrs;
1018 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1019 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1020 goto out;
1021 r = -E2BIG;
1022 if (n < num_msrs_to_save)
1023 goto out;
1024 r = -EFAULT;
1025 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1026 num_msrs_to_save * sizeof(u32)))
1027 goto out;
1028 if (copy_to_user(user_msr_list->indices
1029 + num_msrs_to_save * sizeof(u32),
1030 &emulated_msrs,
1031 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1032 goto out;
1033 r = 0;
1034 break;
1035 }
Avi Kivity674eea02008-02-11 18:37:23 +02001036 case KVM_GET_SUPPORTED_CPUID: {
1037 struct kvm_cpuid2 __user *cpuid_arg = argp;
1038 struct kvm_cpuid2 cpuid;
1039
1040 r = -EFAULT;
1041 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1042 goto out;
1043 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1044 cpuid_arg->entries);
1045 if (r)
1046 goto out;
1047
1048 r = -EFAULT;
1049 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1050 goto out;
1051 r = 0;
1052 break;
1053 }
Carsten Otte043405e2007-10-10 17:16:19 +02001054 default:
1055 r = -EINVAL;
1056 }
1057out:
1058 return r;
1059}
1060
Carsten Otte313a3dc2007-10-11 19:16:52 +02001061void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1062{
1063 kvm_x86_ops->vcpu_load(vcpu, cpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001064 kvm_write_guest_time(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001065}
1066
1067void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1068{
1069 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001070 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001071}
1072
Dan Kenigsberg07716712007-11-21 17:10:04 +02001073static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001074{
1075 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001076
1077 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001078 return efer & EFER_NX;
1079}
1080
1081static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1082{
1083 int i;
1084 struct kvm_cpuid_entry2 *e, *entry;
1085
Carsten Otte313a3dc2007-10-11 19:16:52 +02001086 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001087 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1088 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001089 if (e->function == 0x80000001) {
1090 entry = e;
1091 break;
1092 }
1093 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001094 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001095 entry->edx &= ~(1 << 20);
1096 printk(KERN_INFO "kvm: guest NX capability removed\n");
1097 }
1098}
1099
Dan Kenigsberg07716712007-11-21 17:10:04 +02001100/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001101static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1102 struct kvm_cpuid *cpuid,
1103 struct kvm_cpuid_entry __user *entries)
1104{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001105 int r, i;
1106 struct kvm_cpuid_entry *cpuid_entries;
1107
1108 r = -E2BIG;
1109 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1110 goto out;
1111 r = -ENOMEM;
1112 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1113 if (!cpuid_entries)
1114 goto out;
1115 r = -EFAULT;
1116 if (copy_from_user(cpuid_entries, entries,
1117 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1118 goto out_free;
1119 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001120 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1121 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1122 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1123 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1124 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1125 vcpu->arch.cpuid_entries[i].index = 0;
1126 vcpu->arch.cpuid_entries[i].flags = 0;
1127 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1128 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1129 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001130 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001131 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001132 cpuid_fix_nx_cap(vcpu);
1133 r = 0;
1134
1135out_free:
1136 vfree(cpuid_entries);
1137out:
1138 return r;
1139}
1140
1141static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1142 struct kvm_cpuid2 *cpuid,
1143 struct kvm_cpuid_entry2 __user *entries)
1144{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001145 int r;
1146
1147 r = -E2BIG;
1148 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1149 goto out;
1150 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001151 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001152 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001153 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001154 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001155 return 0;
1156
1157out:
1158 return r;
1159}
1160
Dan Kenigsberg07716712007-11-21 17:10:04 +02001161static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1162 struct kvm_cpuid2 *cpuid,
1163 struct kvm_cpuid_entry2 __user *entries)
1164{
1165 int r;
1166
1167 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001168 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001169 goto out;
1170 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001171 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1172 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001173 goto out;
1174 return 0;
1175
1176out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001177 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001178 return r;
1179}
1180
1181static inline u32 bit(int bitno)
1182{
1183 return 1 << (bitno & 31);
1184}
1185
1186static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1187 u32 index)
1188{
1189 entry->function = function;
1190 entry->index = index;
1191 cpuid_count(entry->function, entry->index,
1192 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1193 entry->flags = 0;
1194}
1195
1196static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1197 u32 index, int *nent, int maxnent)
1198{
1199 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1200 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1201 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1202 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1203 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1204 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1205 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1206 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1207 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1208 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1209 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1210 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1211 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1212 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1213 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1214 bit(X86_FEATURE_PGE) |
1215 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1216 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1217 bit(X86_FEATURE_SYSCALL) |
1218 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1219#ifdef CONFIG_X86_64
1220 bit(X86_FEATURE_LM) |
1221#endif
1222 bit(X86_FEATURE_MMXEXT) |
1223 bit(X86_FEATURE_3DNOWEXT) |
1224 bit(X86_FEATURE_3DNOW);
1225 const u32 kvm_supported_word3_x86_features =
1226 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1227 const u32 kvm_supported_word6_x86_features =
1228 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1229
1230 /* all func 2 cpuid_count() should be called on the same cpu */
1231 get_cpu();
1232 do_cpuid_1_ent(entry, function, index);
1233 ++*nent;
1234
1235 switch (function) {
1236 case 0:
1237 entry->eax = min(entry->eax, (u32)0xb);
1238 break;
1239 case 1:
1240 entry->edx &= kvm_supported_word0_x86_features;
1241 entry->ecx &= kvm_supported_word3_x86_features;
1242 break;
1243 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1244 * may return different values. This forces us to get_cpu() before
1245 * issuing the first command, and also to emulate this annoying behavior
1246 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1247 case 2: {
1248 int t, times = entry->eax & 0xff;
1249
1250 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001251 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001252 for (t = 1; t < times && *nent < maxnent; ++t) {
1253 do_cpuid_1_ent(&entry[t], function, 0);
1254 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1255 ++*nent;
1256 }
1257 break;
1258 }
1259 /* function 4 and 0xb have additional index. */
1260 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001261 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001262
1263 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1264 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001265 for (i = 1; *nent < maxnent; ++i) {
1266 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001267 if (!cache_type)
1268 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001269 do_cpuid_1_ent(&entry[i], function, i);
1270 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001271 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1272 ++*nent;
1273 }
1274 break;
1275 }
1276 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001277 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001278
1279 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1280 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001281 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001282 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001283 if (!level_type)
1284 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001285 do_cpuid_1_ent(&entry[i], function, i);
1286 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001287 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1288 ++*nent;
1289 }
1290 break;
1291 }
1292 case 0x80000000:
1293 entry->eax = min(entry->eax, 0x8000001a);
1294 break;
1295 case 0x80000001:
1296 entry->edx &= kvm_supported_word1_x86_features;
1297 entry->ecx &= kvm_supported_word6_x86_features;
1298 break;
1299 }
1300 put_cpu();
1301}
1302
Avi Kivity674eea02008-02-11 18:37:23 +02001303static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001304 struct kvm_cpuid_entry2 __user *entries)
1305{
1306 struct kvm_cpuid_entry2 *cpuid_entries;
1307 int limit, nent = 0, r = -E2BIG;
1308 u32 func;
1309
1310 if (cpuid->nent < 1)
1311 goto out;
1312 r = -ENOMEM;
1313 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1314 if (!cpuid_entries)
1315 goto out;
1316
1317 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1318 limit = cpuid_entries[0].eax;
1319 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1320 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1321 &nent, cpuid->nent);
1322 r = -E2BIG;
1323 if (nent >= cpuid->nent)
1324 goto out_free;
1325
1326 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1327 limit = cpuid_entries[nent - 1].eax;
1328 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1329 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1330 &nent, cpuid->nent);
1331 r = -EFAULT;
1332 if (copy_to_user(entries, cpuid_entries,
1333 nent * sizeof(struct kvm_cpuid_entry2)))
1334 goto out_free;
1335 cpuid->nent = nent;
1336 r = 0;
1337
1338out_free:
1339 vfree(cpuid_entries);
1340out:
1341 return r;
1342}
1343
Carsten Otte313a3dc2007-10-11 19:16:52 +02001344static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1345 struct kvm_lapic_state *s)
1346{
1347 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001348 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001349 vcpu_put(vcpu);
1350
1351 return 0;
1352}
1353
1354static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1355 struct kvm_lapic_state *s)
1356{
1357 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001358 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001359 kvm_apic_post_state_restore(vcpu);
1360 vcpu_put(vcpu);
1361
1362 return 0;
1363}
1364
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001365static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1366 struct kvm_interrupt *irq)
1367{
1368 if (irq->irq < 0 || irq->irq >= 256)
1369 return -EINVAL;
1370 if (irqchip_in_kernel(vcpu->kvm))
1371 return -ENXIO;
1372 vcpu_load(vcpu);
1373
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001374 set_bit(irq->irq, vcpu->arch.irq_pending);
1375 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001376
1377 vcpu_put(vcpu);
1378
1379 return 0;
1380}
1381
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001382static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1383{
1384 vcpu_load(vcpu);
1385 kvm_inject_nmi(vcpu);
1386 vcpu_put(vcpu);
1387
1388 return 0;
1389}
1390
Avi Kivityb209749f2007-10-22 16:50:39 +02001391static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1392 struct kvm_tpr_access_ctl *tac)
1393{
1394 if (tac->flags)
1395 return -EINVAL;
1396 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1397 return 0;
1398}
1399
Carsten Otte313a3dc2007-10-11 19:16:52 +02001400long kvm_arch_vcpu_ioctl(struct file *filp,
1401 unsigned int ioctl, unsigned long arg)
1402{
1403 struct kvm_vcpu *vcpu = filp->private_data;
1404 void __user *argp = (void __user *)arg;
1405 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001406 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001407
1408 switch (ioctl) {
1409 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001410 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001411
Dave Hansenb772ff32008-08-11 10:01:47 -07001412 r = -ENOMEM;
1413 if (!lapic)
1414 goto out;
1415 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001416 if (r)
1417 goto out;
1418 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001419 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001420 goto out;
1421 r = 0;
1422 break;
1423 }
1424 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001425 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1426 r = -ENOMEM;
1427 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001428 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001429 r = -EFAULT;
1430 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1431 goto out;
1432 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001433 if (r)
1434 goto out;
1435 r = 0;
1436 break;
1437 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001438 case KVM_INTERRUPT: {
1439 struct kvm_interrupt irq;
1440
1441 r = -EFAULT;
1442 if (copy_from_user(&irq, argp, sizeof irq))
1443 goto out;
1444 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1445 if (r)
1446 goto out;
1447 r = 0;
1448 break;
1449 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001450 case KVM_NMI: {
1451 r = kvm_vcpu_ioctl_nmi(vcpu);
1452 if (r)
1453 goto out;
1454 r = 0;
1455 break;
1456 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001457 case KVM_SET_CPUID: {
1458 struct kvm_cpuid __user *cpuid_arg = argp;
1459 struct kvm_cpuid cpuid;
1460
1461 r = -EFAULT;
1462 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1463 goto out;
1464 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1465 if (r)
1466 goto out;
1467 break;
1468 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001469 case KVM_SET_CPUID2: {
1470 struct kvm_cpuid2 __user *cpuid_arg = argp;
1471 struct kvm_cpuid2 cpuid;
1472
1473 r = -EFAULT;
1474 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1475 goto out;
1476 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1477 cpuid_arg->entries);
1478 if (r)
1479 goto out;
1480 break;
1481 }
1482 case KVM_GET_CPUID2: {
1483 struct kvm_cpuid2 __user *cpuid_arg = argp;
1484 struct kvm_cpuid2 cpuid;
1485
1486 r = -EFAULT;
1487 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1488 goto out;
1489 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1490 cpuid_arg->entries);
1491 if (r)
1492 goto out;
1493 r = -EFAULT;
1494 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1495 goto out;
1496 r = 0;
1497 break;
1498 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001499 case KVM_GET_MSRS:
1500 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1501 break;
1502 case KVM_SET_MSRS:
1503 r = msr_io(vcpu, argp, do_set_msr, 0);
1504 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001505 case KVM_TPR_ACCESS_REPORTING: {
1506 struct kvm_tpr_access_ctl tac;
1507
1508 r = -EFAULT;
1509 if (copy_from_user(&tac, argp, sizeof tac))
1510 goto out;
1511 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1512 if (r)
1513 goto out;
1514 r = -EFAULT;
1515 if (copy_to_user(argp, &tac, sizeof tac))
1516 goto out;
1517 r = 0;
1518 break;
1519 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001520 case KVM_SET_VAPIC_ADDR: {
1521 struct kvm_vapic_addr va;
1522
1523 r = -EINVAL;
1524 if (!irqchip_in_kernel(vcpu->kvm))
1525 goto out;
1526 r = -EFAULT;
1527 if (copy_from_user(&va, argp, sizeof va))
1528 goto out;
1529 r = 0;
1530 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1531 break;
1532 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001533 default:
1534 r = -EINVAL;
1535 }
1536out:
Dave Hansenb772ff32008-08-11 10:01:47 -07001537 if (lapic)
1538 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001539 return r;
1540}
1541
Carsten Otte1fe779f2007-10-29 16:08:35 +01001542static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1543{
1544 int ret;
1545
1546 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1547 return -1;
1548 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1549 return ret;
1550}
1551
1552static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1553 u32 kvm_nr_mmu_pages)
1554{
1555 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1556 return -EINVAL;
1557
Izik Eidus72dc67a2008-02-10 18:04:15 +02001558 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001559
1560 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001561 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001562
Izik Eidus72dc67a2008-02-10 18:04:15 +02001563 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001564 return 0;
1565}
1566
1567static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1568{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001569 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001570}
1571
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001572gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1573{
1574 int i;
1575 struct kvm_mem_alias *alias;
1576
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001577 for (i = 0; i < kvm->arch.naliases; ++i) {
1578 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001579 if (gfn >= alias->base_gfn
1580 && gfn < alias->base_gfn + alias->npages)
1581 return alias->target_gfn + gfn - alias->base_gfn;
1582 }
1583 return gfn;
1584}
1585
Carsten Otte1fe779f2007-10-29 16:08:35 +01001586/*
1587 * Set a new alias region. Aliases map a portion of physical memory into
1588 * another portion. This is useful for memory windows, for example the PC
1589 * VGA region.
1590 */
1591static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1592 struct kvm_memory_alias *alias)
1593{
1594 int r, n;
1595 struct kvm_mem_alias *p;
1596
1597 r = -EINVAL;
1598 /* General sanity checks */
1599 if (alias->memory_size & (PAGE_SIZE - 1))
1600 goto out;
1601 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1602 goto out;
1603 if (alias->slot >= KVM_ALIAS_SLOTS)
1604 goto out;
1605 if (alias->guest_phys_addr + alias->memory_size
1606 < alias->guest_phys_addr)
1607 goto out;
1608 if (alias->target_phys_addr + alias->memory_size
1609 < alias->target_phys_addr)
1610 goto out;
1611
Izik Eidus72dc67a2008-02-10 18:04:15 +02001612 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001613 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001614
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001615 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001616 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1617 p->npages = alias->memory_size >> PAGE_SHIFT;
1618 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1619
1620 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001621 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001622 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001623 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001624
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001625 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001626 kvm_mmu_zap_all(kvm);
1627
Izik Eidus72dc67a2008-02-10 18:04:15 +02001628 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001629
1630 return 0;
1631
1632out:
1633 return r;
1634}
1635
1636static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1637{
1638 int r;
1639
1640 r = 0;
1641 switch (chip->chip_id) {
1642 case KVM_IRQCHIP_PIC_MASTER:
1643 memcpy(&chip->chip.pic,
1644 &pic_irqchip(kvm)->pics[0],
1645 sizeof(struct kvm_pic_state));
1646 break;
1647 case KVM_IRQCHIP_PIC_SLAVE:
1648 memcpy(&chip->chip.pic,
1649 &pic_irqchip(kvm)->pics[1],
1650 sizeof(struct kvm_pic_state));
1651 break;
1652 case KVM_IRQCHIP_IOAPIC:
1653 memcpy(&chip->chip.ioapic,
1654 ioapic_irqchip(kvm),
1655 sizeof(struct kvm_ioapic_state));
1656 break;
1657 default:
1658 r = -EINVAL;
1659 break;
1660 }
1661 return r;
1662}
1663
1664static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1665{
1666 int r;
1667
1668 r = 0;
1669 switch (chip->chip_id) {
1670 case KVM_IRQCHIP_PIC_MASTER:
1671 memcpy(&pic_irqchip(kvm)->pics[0],
1672 &chip->chip.pic,
1673 sizeof(struct kvm_pic_state));
1674 break;
1675 case KVM_IRQCHIP_PIC_SLAVE:
1676 memcpy(&pic_irqchip(kvm)->pics[1],
1677 &chip->chip.pic,
1678 sizeof(struct kvm_pic_state));
1679 break;
1680 case KVM_IRQCHIP_IOAPIC:
1681 memcpy(ioapic_irqchip(kvm),
1682 &chip->chip.ioapic,
1683 sizeof(struct kvm_ioapic_state));
1684 break;
1685 default:
1686 r = -EINVAL;
1687 break;
1688 }
1689 kvm_pic_update_irq(pic_irqchip(kvm));
1690 return r;
1691}
1692
Sheng Yange0f63cb2008-03-04 00:50:59 +08001693static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1694{
1695 int r = 0;
1696
1697 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1698 return r;
1699}
1700
1701static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1702{
1703 int r = 0;
1704
1705 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1706 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1707 return r;
1708}
1709
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001710/*
1711 * Get (and clear) the dirty memory log for a memory slot.
1712 */
1713int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1714 struct kvm_dirty_log *log)
1715{
1716 int r;
1717 int n;
1718 struct kvm_memory_slot *memslot;
1719 int is_dirty = 0;
1720
Izik Eidus72dc67a2008-02-10 18:04:15 +02001721 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001722
1723 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1724 if (r)
1725 goto out;
1726
1727 /* If nothing is dirty, don't bother messing with page tables. */
1728 if (is_dirty) {
1729 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1730 kvm_flush_remote_tlbs(kvm);
1731 memslot = &kvm->memslots[log->slot];
1732 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1733 memset(memslot->dirty_bitmap, 0, n);
1734 }
1735 r = 0;
1736out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001737 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001738 return r;
1739}
1740
Carsten Otte1fe779f2007-10-29 16:08:35 +01001741long kvm_arch_vm_ioctl(struct file *filp,
1742 unsigned int ioctl, unsigned long arg)
1743{
1744 struct kvm *kvm = filp->private_data;
1745 void __user *argp = (void __user *)arg;
1746 int r = -EINVAL;
Dave Hansenf0d66272008-08-11 10:01:45 -07001747 /*
1748 * This union makes it completely explicit to gcc-3.x
1749 * that these two variables' stack usage should be
1750 * combined, not added together.
1751 */
1752 union {
1753 struct kvm_pit_state ps;
1754 struct kvm_memory_alias alias;
1755 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001756
1757 switch (ioctl) {
1758 case KVM_SET_TSS_ADDR:
1759 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1760 if (r < 0)
1761 goto out;
1762 break;
1763 case KVM_SET_MEMORY_REGION: {
1764 struct kvm_memory_region kvm_mem;
1765 struct kvm_userspace_memory_region kvm_userspace_mem;
1766
1767 r = -EFAULT;
1768 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1769 goto out;
1770 kvm_userspace_mem.slot = kvm_mem.slot;
1771 kvm_userspace_mem.flags = kvm_mem.flags;
1772 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1773 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1774 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1775 if (r)
1776 goto out;
1777 break;
1778 }
1779 case KVM_SET_NR_MMU_PAGES:
1780 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1781 if (r)
1782 goto out;
1783 break;
1784 case KVM_GET_NR_MMU_PAGES:
1785 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1786 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07001787 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01001788 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001789 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01001790 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001791 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001792 if (r)
1793 goto out;
1794 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001795 case KVM_CREATE_IRQCHIP:
1796 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001797 kvm->arch.vpic = kvm_create_pic(kvm);
1798 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001799 r = kvm_ioapic_init(kvm);
1800 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001801 kfree(kvm->arch.vpic);
1802 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001803 goto out;
1804 }
1805 } else
1806 goto out;
1807 break;
Sheng Yang78376992008-01-28 05:10:22 +08001808 case KVM_CREATE_PIT:
1809 r = -ENOMEM;
1810 kvm->arch.vpit = kvm_create_pit(kvm);
1811 if (kvm->arch.vpit)
1812 r = 0;
1813 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001814 case KVM_IRQ_LINE: {
1815 struct kvm_irq_level irq_event;
1816
1817 r = -EFAULT;
1818 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1819 goto out;
1820 if (irqchip_in_kernel(kvm)) {
1821 mutex_lock(&kvm->lock);
Sheng Yang5550af42008-10-15 20:15:06 +08001822 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1823 irq_event.irq, irq_event.level);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001824 mutex_unlock(&kvm->lock);
1825 r = 0;
1826 }
1827 break;
1828 }
1829 case KVM_GET_IRQCHIP: {
1830 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001831 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001832
Dave Hansenf0d66272008-08-11 10:01:45 -07001833 r = -ENOMEM;
1834 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001835 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001836 r = -EFAULT;
1837 if (copy_from_user(chip, argp, sizeof *chip))
1838 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001839 r = -ENXIO;
1840 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07001841 goto get_irqchip_out;
1842 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
1843 if (r)
1844 goto get_irqchip_out;
1845 r = -EFAULT;
1846 if (copy_to_user(argp, chip, sizeof *chip))
1847 goto get_irqchip_out;
1848 r = 0;
1849 get_irqchip_out:
1850 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001851 if (r)
1852 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001853 break;
1854 }
1855 case KVM_SET_IRQCHIP: {
1856 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07001857 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001858
Dave Hansenf0d66272008-08-11 10:01:45 -07001859 r = -ENOMEM;
1860 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001861 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001862 r = -EFAULT;
1863 if (copy_from_user(chip, argp, sizeof *chip))
1864 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001865 r = -ENXIO;
1866 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07001867 goto set_irqchip_out;
1868 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
1869 if (r)
1870 goto set_irqchip_out;
1871 r = 0;
1872 set_irqchip_out:
1873 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001874 if (r)
1875 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001876 break;
1877 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08001878 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08001879 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001880 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001881 goto out;
1882 r = -ENXIO;
1883 if (!kvm->arch.vpit)
1884 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001885 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08001886 if (r)
1887 goto out;
1888 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001889 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001890 goto out;
1891 r = 0;
1892 break;
1893 }
1894 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08001895 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07001896 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08001897 goto out;
1898 r = -ENXIO;
1899 if (!kvm->arch.vpit)
1900 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07001901 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08001902 if (r)
1903 goto out;
1904 r = 0;
1905 break;
1906 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01001907 default:
1908 ;
1909 }
1910out:
1911 return r;
1912}
1913
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001914static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001915{
1916 u32 dummy[2];
1917 unsigned i, j;
1918
1919 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1920 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1921 continue;
1922 if (j < i)
1923 msrs_to_save[j] = msrs_to_save[i];
1924 j++;
1925 }
1926 num_msrs_to_save = j;
1927}
1928
Carsten Ottebbd9b642007-10-30 18:44:21 +01001929/*
1930 * Only apic need an MMIO device hook, so shortcut now..
1931 */
1932static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02001933 gpa_t addr, int len,
1934 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001935{
1936 struct kvm_io_device *dev;
1937
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001938 if (vcpu->arch.apic) {
1939 dev = &vcpu->arch.apic->dev;
Laurent Vivier92760492008-05-30 16:05:53 +02001940 if (dev->in_range(dev, addr, len, is_write))
Carsten Ottebbd9b642007-10-30 18:44:21 +01001941 return dev;
1942 }
1943 return NULL;
1944}
1945
1946
1947static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02001948 gpa_t addr, int len,
1949 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001950{
1951 struct kvm_io_device *dev;
1952
Laurent Vivier92760492008-05-30 16:05:53 +02001953 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001954 if (dev == NULL)
Laurent Vivier92760492008-05-30 16:05:53 +02001955 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
1956 is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001957 return dev;
1958}
1959
1960int emulator_read_std(unsigned long addr,
1961 void *val,
1962 unsigned int bytes,
1963 struct kvm_vcpu *vcpu)
1964{
1965 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001966 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001967
1968 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001970 unsigned offset = addr & (PAGE_SIZE-1);
1971 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1972 int ret;
1973
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001974 if (gpa == UNMAPPED_GVA) {
1975 r = X86EMUL_PROPAGATE_FAULT;
1976 goto out;
1977 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001978 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001979 if (ret < 0) {
1980 r = X86EMUL_UNHANDLEABLE;
1981 goto out;
1982 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001983
1984 bytes -= tocopy;
1985 data += tocopy;
1986 addr += tocopy;
1987 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001988out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001989 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001990}
1991EXPORT_SYMBOL_GPL(emulator_read_std);
1992
Carsten Ottebbd9b642007-10-30 18:44:21 +01001993static int emulator_read_emulated(unsigned long addr,
1994 void *val,
1995 unsigned int bytes,
1996 struct kvm_vcpu *vcpu)
1997{
1998 struct kvm_io_device *mmio_dev;
1999 gpa_t gpa;
2000
2001 if (vcpu->mmio_read_completed) {
2002 memcpy(val, vcpu->mmio_data, bytes);
2003 vcpu->mmio_read_completed = 0;
2004 return X86EMUL_CONTINUE;
2005 }
2006
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002007 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002008
2009 /* For APIC access vmexit */
2010 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2011 goto mmio;
2012
2013 if (emulator_read_std(addr, val, bytes, vcpu)
2014 == X86EMUL_CONTINUE)
2015 return X86EMUL_CONTINUE;
2016 if (gpa == UNMAPPED_GVA)
2017 return X86EMUL_PROPAGATE_FAULT;
2018
2019mmio:
2020 /*
2021 * Is this MMIO handled locally?
2022 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002023 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002024 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002025 if (mmio_dev) {
2026 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002027 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002028 return X86EMUL_CONTINUE;
2029 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002030 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002031
2032 vcpu->mmio_needed = 1;
2033 vcpu->mmio_phys_addr = gpa;
2034 vcpu->mmio_size = bytes;
2035 vcpu->mmio_is_write = 0;
2036
2037 return X86EMUL_UNHANDLEABLE;
2038}
2039
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002040int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002041 const void *val, int bytes)
2042{
2043 int ret;
2044
2045 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2046 if (ret < 0)
2047 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002048 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002049 return 1;
2050}
2051
Carsten Ottebbd9b642007-10-30 18:44:21 +01002052static int emulator_write_emulated_onepage(unsigned long addr,
2053 const void *val,
2054 unsigned int bytes,
2055 struct kvm_vcpu *vcpu)
2056{
2057 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002058 gpa_t gpa;
2059
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002060 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002061
2062 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002063 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002064 return X86EMUL_PROPAGATE_FAULT;
2065 }
2066
2067 /* For APIC access vmexit */
2068 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2069 goto mmio;
2070
2071 if (emulator_write_phys(vcpu, gpa, val, bytes))
2072 return X86EMUL_CONTINUE;
2073
2074mmio:
2075 /*
2076 * Is this MMIO handled locally?
2077 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002078 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002079 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002080 if (mmio_dev) {
2081 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002082 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002083 return X86EMUL_CONTINUE;
2084 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002085 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002086
2087 vcpu->mmio_needed = 1;
2088 vcpu->mmio_phys_addr = gpa;
2089 vcpu->mmio_size = bytes;
2090 vcpu->mmio_is_write = 1;
2091 memcpy(vcpu->mmio_data, val, bytes);
2092
2093 return X86EMUL_CONTINUE;
2094}
2095
2096int emulator_write_emulated(unsigned long addr,
2097 const void *val,
2098 unsigned int bytes,
2099 struct kvm_vcpu *vcpu)
2100{
2101 /* Crossing a page boundary? */
2102 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2103 int rc, now;
2104
2105 now = -addr & ~PAGE_MASK;
2106 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2107 if (rc != X86EMUL_CONTINUE)
2108 return rc;
2109 addr += now;
2110 val += now;
2111 bytes -= now;
2112 }
2113 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2114}
2115EXPORT_SYMBOL_GPL(emulator_write_emulated);
2116
2117static int emulator_cmpxchg_emulated(unsigned long addr,
2118 const void *old,
2119 const void *new,
2120 unsigned int bytes,
2121 struct kvm_vcpu *vcpu)
2122{
2123 static int reported;
2124
2125 if (!reported) {
2126 reported = 1;
2127 printk(KERN_WARNING "kvm: emulating exchange as write\n");
2128 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002129#ifndef CONFIG_X86_64
2130 /* guests cmpxchg8b have to be emulated atomically */
2131 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002132 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002133 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002134 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002135 u64 val;
2136
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002137 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2138
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002139 if (gpa == UNMAPPED_GVA ||
2140 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2141 goto emul_write;
2142
2143 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2144 goto emul_write;
2145
2146 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002147
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002148 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002149
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002150 kaddr = kmap_atomic(page, KM_USER0);
2151 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2152 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002153 kvm_release_page_dirty(page);
2154 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002155emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002156#endif
2157
Carsten Ottebbd9b642007-10-30 18:44:21 +01002158 return emulator_write_emulated(addr, new, bytes, vcpu);
2159}
2160
2161static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2162{
2163 return kvm_x86_ops->get_segment_base(vcpu, seg);
2164}
2165
2166int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2167{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002168 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002169 return X86EMUL_CONTINUE;
2170}
2171
2172int emulate_clts(struct kvm_vcpu *vcpu)
2173{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002174 KVMTRACE_0D(CLTS, vcpu, handler);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002175 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002176 return X86EMUL_CONTINUE;
2177}
2178
2179int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2180{
2181 struct kvm_vcpu *vcpu = ctxt->vcpu;
2182
2183 switch (dr) {
2184 case 0 ... 3:
2185 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2186 return X86EMUL_CONTINUE;
2187 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002188 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002189 return X86EMUL_UNHANDLEABLE;
2190 }
2191}
2192
2193int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2194{
2195 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2196 int exception;
2197
2198 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2199 if (exception) {
2200 /* FIXME: better handling */
2201 return X86EMUL_UNHANDLEABLE;
2202 }
2203 return X86EMUL_CONTINUE;
2204}
2205
2206void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2207{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002208 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002209 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002210 unsigned long rip_linear;
2211
Avi Kivityf76c7102008-06-13 22:45:42 +03002212 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002213 return;
2214
Glauber Costa25be4602008-06-10 10:46:53 -03002215 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2216
Carsten Ottebbd9b642007-10-30 18:44:21 +01002217 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
2218
2219 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2220 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002221}
2222EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2223
Harvey Harrison14af3f32008-02-19 10:25:50 -08002224static struct x86_emulate_ops emulate_ops = {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002225 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002226 .read_emulated = emulator_read_emulated,
2227 .write_emulated = emulator_write_emulated,
2228 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2229};
2230
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002231static void cache_all_regs(struct kvm_vcpu *vcpu)
2232{
2233 kvm_register_read(vcpu, VCPU_REGS_RAX);
2234 kvm_register_read(vcpu, VCPU_REGS_RSP);
2235 kvm_register_read(vcpu, VCPU_REGS_RIP);
2236 vcpu->arch.regs_dirty = ~0;
2237}
2238
Carsten Ottebbd9b642007-10-30 18:44:21 +01002239int emulate_instruction(struct kvm_vcpu *vcpu,
2240 struct kvm_run *run,
2241 unsigned long cr2,
2242 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002243 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002244{
2245 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08002246 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002247
Avi Kivity26eef702008-07-03 14:59:22 +03002248 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002249 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002250 /*
2251 * TODO: fix x86_emulate.c to use guest_read/write_register
2252 * instead of direct ->regs accesses, can save hundred cycles
2253 * on Intel for instructions that don't read/change RSP, for
2254 * for example.
2255 */
2256 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002257
2258 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002259 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002260
Sheng Yang571008d2008-01-02 14:49:22 +08002261 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002262 int cs_db, cs_l;
2263 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2264
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002265 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2266 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2267 vcpu->arch.emulate_ctxt.mode =
2268 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002269 ? X86EMUL_MODE_REAL : cs_l
2270 ? X86EMUL_MODE_PROT64 : cs_db
2271 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2272
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002273 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002274
2275 /* Reject the instructions other than VMCALL/VMMCALL when
2276 * try to emulate invalid opcode */
2277 c = &vcpu->arch.emulate_ctxt.decode;
2278 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2279 (!(c->twobyte && c->b == 0x01 &&
2280 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2281 c->modrm_mod == 3 && c->modrm_rm == 1)))
2282 return EMULATE_FAIL;
2283
Avi Kivityf2b57562007-11-18 15:17:51 +02002284 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002285 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002286 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002287 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2288 return EMULATE_DONE;
2289 return EMULATE_FAIL;
2290 }
2291 }
2292
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002293 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002294
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002295 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002296 return EMULATE_DO_MMIO;
2297
2298 if ((r || vcpu->mmio_is_write) && run) {
2299 run->exit_reason = KVM_EXIT_MMIO;
2300 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2301 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2302 run->mmio.len = vcpu->mmio_size;
2303 run->mmio.is_write = vcpu->mmio_is_write;
2304 }
2305
2306 if (r) {
2307 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2308 return EMULATE_DONE;
2309 if (!vcpu->mmio_needed) {
2310 kvm_report_emulation_failure(vcpu, "mmio");
2311 return EMULATE_FAIL;
2312 }
2313 return EMULATE_DO_MMIO;
2314 }
2315
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002316 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002317
2318 if (vcpu->mmio_is_write) {
2319 vcpu->mmio_needed = 0;
2320 return EMULATE_DO_MMIO;
2321 }
2322
2323 return EMULATE_DONE;
2324}
2325EXPORT_SYMBOL_GPL(emulate_instruction);
2326
Carsten Ottede7d7892007-10-30 18:44:25 +01002327static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2328{
2329 int i;
2330
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002331 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2332 if (vcpu->arch.pio.guest_pages[i]) {
2333 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2334 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01002335 }
2336}
2337
2338static int pio_copy_data(struct kvm_vcpu *vcpu)
2339{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002340 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002341 void *q;
2342 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002343 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002344
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002345 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01002346 PAGE_KERNEL);
2347 if (!q) {
2348 free_pio_guest_pages(vcpu);
2349 return -ENOMEM;
2350 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002351 q += vcpu->arch.pio.guest_page_offset;
2352 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2353 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01002354 memcpy(q, p, bytes);
2355 else
2356 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002357 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01002358 vunmap(q);
2359 free_pio_guest_pages(vcpu);
2360 return 0;
2361}
2362
2363int complete_pio(struct kvm_vcpu *vcpu)
2364{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002365 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002366 long delta;
2367 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002368 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002369
2370 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002371 if (io->in) {
2372 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2373 memcpy(&val, vcpu->arch.pio_data, io->size);
2374 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2375 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002376 } else {
2377 if (io->in) {
2378 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002379 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002380 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002381 }
2382
2383 delta = 1;
2384 if (io->rep) {
2385 delta *= io->cur_count;
2386 /*
2387 * The size of the register should really depend on
2388 * current address size.
2389 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002390 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2391 val -= delta;
2392 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002393 }
2394 if (io->down)
2395 delta = -delta;
2396 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002397 if (io->in) {
2398 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2399 val += delta;
2400 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2401 } else {
2402 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2403 val += delta;
2404 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2405 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002406 }
2407
Carsten Ottede7d7892007-10-30 18:44:25 +01002408 io->count -= io->cur_count;
2409 io->cur_count = 0;
2410
2411 return 0;
2412}
2413
2414static void kernel_pio(struct kvm_io_device *pio_dev,
2415 struct kvm_vcpu *vcpu,
2416 void *pd)
2417{
2418 /* TODO: String I/O for in kernel device */
2419
2420 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002421 if (vcpu->arch.pio.in)
2422 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2423 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002424 pd);
2425 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002426 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2427 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002428 pd);
2429 mutex_unlock(&vcpu->kvm->lock);
2430}
2431
2432static void pio_string_write(struct kvm_io_device *pio_dev,
2433 struct kvm_vcpu *vcpu)
2434{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 struct kvm_pio_request *io = &vcpu->arch.pio;
2436 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002437 int i;
2438
2439 mutex_lock(&vcpu->kvm->lock);
2440 for (i = 0; i < io->cur_count; i++) {
2441 kvm_iodevice_write(pio_dev, io->port,
2442 io->size,
2443 pd);
2444 pd += io->size;
2445 }
2446 mutex_unlock(&vcpu->kvm->lock);
2447}
2448
2449static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002450 gpa_t addr, int len,
2451 int is_write)
Carsten Ottede7d7892007-10-30 18:44:25 +01002452{
Laurent Vivier92760492008-05-30 16:05:53 +02002453 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
Carsten Ottede7d7892007-10-30 18:44:25 +01002454}
2455
2456int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2457 int size, unsigned port)
2458{
2459 struct kvm_io_device *pio_dev;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002460 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002461
2462 vcpu->run->exit_reason = KVM_EXIT_IO;
2463 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002464 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002465 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002466 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2467 vcpu->run->io.port = vcpu->arch.pio.port = port;
2468 vcpu->arch.pio.in = in;
2469 vcpu->arch.pio.string = 0;
2470 vcpu->arch.pio.down = 0;
2471 vcpu->arch.pio.guest_page_offset = 0;
2472 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002473
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002474 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2475 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2476 handler);
2477 else
2478 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2479 handler);
2480
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002481 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2482 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002483
Laurent Vivier92760492008-05-30 16:05:53 +02002484 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
Carsten Ottede7d7892007-10-30 18:44:25 +01002485 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002486 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002487 complete_pio(vcpu);
2488 return 1;
2489 }
2490 return 0;
2491}
2492EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2493
2494int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2495 int size, unsigned long count, int down,
2496 gva_t address, int rep, unsigned port)
2497{
2498 unsigned now, in_page;
2499 int i, ret = 0;
2500 int nr_pages = 1;
2501 struct page *page;
2502 struct kvm_io_device *pio_dev;
2503
2504 vcpu->run->exit_reason = KVM_EXIT_IO;
2505 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002506 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002507 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2509 vcpu->run->io.port = vcpu->arch.pio.port = port;
2510 vcpu->arch.pio.in = in;
2511 vcpu->arch.pio.string = 1;
2512 vcpu->arch.pio.down = down;
2513 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2514 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002515
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002516 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2517 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2518 handler);
2519 else
2520 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2521 handler);
2522
Carsten Ottede7d7892007-10-30 18:44:25 +01002523 if (!count) {
2524 kvm_x86_ops->skip_emulated_instruction(vcpu);
2525 return 1;
2526 }
2527
2528 if (!down)
2529 in_page = PAGE_SIZE - offset_in_page(address);
2530 else
2531 in_page = offset_in_page(address) + size;
2532 now = min(count, (unsigned long)in_page / size);
2533 if (!now) {
2534 /*
2535 * String I/O straddles page boundary. Pin two guest pages
2536 * so that we satisfy atomicity constraints. Do just one
2537 * transaction to avoid complexity.
2538 */
2539 nr_pages = 2;
2540 now = 1;
2541 }
2542 if (down) {
2543 /*
2544 * String I/O in reverse. Yuck. Kill the guest, fix later.
2545 */
2546 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002547 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002548 return 1;
2549 }
2550 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002551 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002552
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002553 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002554 kvm_x86_ops->skip_emulated_instruction(vcpu);
2555
2556 for (i = 0; i < nr_pages; ++i) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002557 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002558 vcpu->arch.pio.guest_pages[i] = page;
Carsten Ottede7d7892007-10-30 18:44:25 +01002559 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002560 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002561 free_pio_guest_pages(vcpu);
2562 return 1;
2563 }
2564 }
2565
Laurent Vivier92760492008-05-30 16:05:53 +02002566 pio_dev = vcpu_find_pio_dev(vcpu, port,
2567 vcpu->arch.pio.cur_count,
2568 !vcpu->arch.pio.in);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002569 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002570 /* string PIO write */
2571 ret = pio_copy_data(vcpu);
2572 if (ret >= 0 && pio_dev) {
2573 pio_string_write(pio_dev, vcpu);
2574 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002575 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002576 ret = 1;
2577 }
2578 } else if (pio_dev)
2579 pr_unimpl(vcpu, "no string pio read support yet, "
2580 "port %x size %d count %ld\n",
2581 port, size, count);
2582
2583 return ret;
2584}
2585EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2586
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002587int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002588{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002589 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002590 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2591
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002592 if (kvm_x86_ops) {
2593 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002594 r = -EEXIST;
2595 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002596 }
2597
2598 if (!ops->cpu_has_kvm_support()) {
2599 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002600 r = -EOPNOTSUPP;
2601 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002602 }
2603 if (ops->disabled_by_bios()) {
2604 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002605 r = -EOPNOTSUPP;
2606 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002607 }
2608
Avi Kivity97db56c2008-01-13 13:23:56 +02002609 r = kvm_mmu_module_init();
2610 if (r)
2611 goto out;
2612
2613 kvm_init_msr_list();
2614
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002615 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002616 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08002617 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2618 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang64d4d522008-10-09 16:01:57 +08002619 PT_DIRTY_MASK, PT64_NX_MASK, 0, 0);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002620 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002621
2622out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002623 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002624}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002625
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002626void kvm_arch_exit(void)
2627{
2628 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002629 kvm_mmu_module_exit();
2630}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002631
Hollis Blanchard8776e512007-10-31 17:24:24 -05002632int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2633{
2634 ++vcpu->stat.halt_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002635 KVMTRACE_0D(HLT, vcpu, handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002636 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03002637 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002638 return 1;
2639 } else {
2640 vcpu->run->exit_reason = KVM_EXIT_HLT;
2641 return 0;
2642 }
2643}
2644EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2645
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002646static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2647 unsigned long a1)
2648{
2649 if (is_long_mode(vcpu))
2650 return a0;
2651 else
2652 return a0 | ((gpa_t)a1 << 32);
2653}
2654
Hollis Blanchard8776e512007-10-31 17:24:24 -05002655int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2656{
2657 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002658 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002659
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002660 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
2661 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
2662 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
2663 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
2664 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002665
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002666 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2667
Hollis Blanchard8776e512007-10-31 17:24:24 -05002668 if (!is_long_mode(vcpu)) {
2669 nr &= 0xFFFFFFFF;
2670 a0 &= 0xFFFFFFFF;
2671 a1 &= 0xFFFFFFFF;
2672 a2 &= 0xFFFFFFFF;
2673 a3 &= 0xFFFFFFFF;
2674 }
2675
2676 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002677 case KVM_HC_VAPIC_POLL_IRQ:
2678 ret = 0;
2679 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002680 case KVM_HC_MMU_OP:
2681 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2682 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002683 default:
2684 ret = -KVM_ENOSYS;
2685 break;
2686 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002687 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05302688 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002689 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002690}
2691EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2692
2693int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2694{
2695 char instruction[3];
2696 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002697 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002698
Hollis Blanchard8776e512007-10-31 17:24:24 -05002699
2700 /*
2701 * Blow out the MMU to ensure that no other VCPU has an active mapping
2702 * to ensure that the updated hypercall appears atomically across all
2703 * VCPUs.
2704 */
2705 kvm_mmu_zap_all(vcpu->kvm);
2706
Hollis Blanchard8776e512007-10-31 17:24:24 -05002707 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002708 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002709 != X86EMUL_CONTINUE)
2710 ret = -EFAULT;
2711
Hollis Blanchard8776e512007-10-31 17:24:24 -05002712 return ret;
2713}
2714
2715static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2716{
2717 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2718}
2719
2720void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2721{
2722 struct descriptor_table dt = { limit, base };
2723
2724 kvm_x86_ops->set_gdt(vcpu, &dt);
2725}
2726
2727void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2728{
2729 struct descriptor_table dt = { limit, base };
2730
2731 kvm_x86_ops->set_idt(vcpu, &dt);
2732}
2733
2734void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2735 unsigned long *rflags)
2736{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002737 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002738 *rflags = kvm_x86_ops->get_rflags(vcpu);
2739}
2740
2741unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2742{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002743 unsigned long value;
2744
Hollis Blanchard8776e512007-10-31 17:24:24 -05002745 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2746 switch (cr) {
2747 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002748 value = vcpu->arch.cr0;
2749 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002750 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002751 value = vcpu->arch.cr2;
2752 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002753 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002754 value = vcpu->arch.cr3;
2755 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002756 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002757 value = vcpu->arch.cr4;
2758 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002759 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002760 value = kvm_get_cr8(vcpu);
2761 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002762 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002763 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002764 return 0;
2765 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02002766 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2767 (u32)((u64)value >> 32), handler);
2768
2769 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002770}
2771
2772void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2773 unsigned long *rflags)
2774{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002775 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2776 (u32)((u64)val >> 32), handler);
2777
Hollis Blanchard8776e512007-10-31 17:24:24 -05002778 switch (cr) {
2779 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002780 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002781 *rflags = kvm_x86_ops->get_rflags(vcpu);
2782 break;
2783 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002784 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002785 break;
2786 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002787 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002788 break;
2789 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002790 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002791 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002792 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002793 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002794 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002795 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002796 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002797 }
2798}
2799
Dan Kenigsberg07716712007-11-21 17:10:04 +02002800static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2801{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002802 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2803 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002804
2805 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2806 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08002807 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002808 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002809 if (ej->function == e->function) {
2810 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2811 return j;
2812 }
2813 }
2814 return 0; /* silence gcc, even though control never reaches here */
2815}
2816
2817/* find an entry with matching function, matching index (if needed), and that
2818 * should be read next (if it's stateful) */
2819static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2820 u32 function, u32 index)
2821{
2822 if (e->function != function)
2823 return 0;
2824 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2825 return 0;
2826 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2827 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2828 return 0;
2829 return 1;
2830}
2831
Hollis Blanchard8776e512007-10-31 17:24:24 -05002832void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2833{
2834 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002835 u32 function, index;
2836 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002837
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002838 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
2839 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
2840 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
2841 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
2842 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
2843 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002844 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002845 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2846 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002847 if (is_matching_cpuid_entry(e, function, index)) {
2848 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2849 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002850 best = e;
2851 break;
2852 }
2853 /*
2854 * Both basic or both extended?
2855 */
2856 if (((e->function ^ function) & 0x80000000) == 0)
2857 if (!best || e->function > best->function)
2858 best = e;
2859 }
2860 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002861 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
2862 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
2863 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
2864 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002865 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05002866 kvm_x86_ops->skip_emulated_instruction(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002867 KVMTRACE_5D(CPUID, vcpu, function,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002868 (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
2869 (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
2870 (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
2871 (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002872}
2873EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002874
2875/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002876 * Check if userspace requested an interrupt window, and that the
2877 * interrupt window is open.
2878 *
2879 * No need to exit to userspace if we already have an interrupt queued.
2880 */
2881static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2882 struct kvm_run *kvm_run)
2883{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002884 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002885 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002886 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002887 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2888}
2889
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002890/*
2891 * Check if userspace requested a NMI window, and that the NMI window
2892 * is open.
2893 *
2894 * No need to exit to userspace if we already have a NMI queued.
2895 */
2896static int dm_request_for_nmi_injection(struct kvm_vcpu *vcpu,
2897 struct kvm_run *kvm_run)
2898{
2899 return (!vcpu->arch.nmi_pending &&
2900 kvm_run->request_nmi_window &&
2901 vcpu->arch.nmi_window_open);
2902}
2903
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002904static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2905 struct kvm_run *kvm_run)
2906{
2907 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002908 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002909 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002910 if (irqchip_in_kernel(vcpu->kvm)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002911 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002912 kvm_run->ready_for_nmi_injection = 1;
2913 } else {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002914 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002915 (vcpu->arch.interrupt_window_open &&
2916 vcpu->arch.irq_summary == 0);
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02002917 kvm_run->ready_for_nmi_injection =
2918 (vcpu->arch.nmi_window_open &&
2919 vcpu->arch.nmi_pending == 0);
2920 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002921}
2922
Avi Kivityb93463a2007-10-25 16:52:32 +02002923static void vapic_enter(struct kvm_vcpu *vcpu)
2924{
2925 struct kvm_lapic *apic = vcpu->arch.apic;
2926 struct page *page;
2927
2928 if (!apic || !apic->vapic_addr)
2929 return;
2930
2931 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002932
2933 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002934}
2935
2936static void vapic_exit(struct kvm_vcpu *vcpu)
2937{
2938 struct kvm_lapic *apic = vcpu->arch.apic;
2939
2940 if (!apic || !apic->vapic_addr)
2941 return;
2942
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03002943 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02002944 kvm_release_page_dirty(apic->vapic_page);
2945 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03002946 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02002947}
2948
Marcelo Tosattid7690172008-09-08 15:23:48 -03002949static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002950{
2951 int r;
2952
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002953 if (vcpu->requests)
2954 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
2955 kvm_mmu_unload(vcpu);
2956
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002957 r = kvm_mmu_reload(vcpu);
2958 if (unlikely(r))
2959 goto out;
2960
Avi Kivity2f52d582008-01-16 12:49:30 +02002961 if (vcpu->requests) {
2962 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03002963 __kvm_migrate_timers(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03002964 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
2965 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03002966 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2967 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002968 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2969 &vcpu->requests)) {
2970 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2971 r = 0;
2972 goto out;
2973 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01002974 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
2975 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2976 r = 0;
2977 goto out;
2978 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002979 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002980
Marcelo Tosatti06e05642008-06-06 16:37:36 -03002981 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002982 kvm_inject_pending_timer_irqs(vcpu);
2983
2984 preempt_disable();
2985
2986 kvm_x86_ops->prepare_guest_switch(vcpu);
2987 kvm_load_guest_fpu(vcpu);
2988
2989 local_irq_disable();
2990
Marcelo Tosattid7690172008-09-08 15:23:48 -03002991 if (vcpu->requests || need_resched() || signal_pending(current)) {
Avi Kivity6c142802008-01-15 18:27:32 +02002992 local_irq_enable();
2993 preempt_enable();
2994 r = 1;
2995 goto out;
2996 }
2997
Marcelo Tosatti29415c32008-08-01 20:09:13 -03002998 if (vcpu->guest_debug.enabled)
2999 kvm_x86_ops->guest_debug_pre(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003000
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03003001 vcpu->guest_mode = 1;
3002 /*
3003 * Make sure that guest_mode assignment won't happen after
3004 * testing the pending IRQ vector bitmap.
3005 */
3006 smp_wmb();
3007
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003008 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02003009 __queue_exception(vcpu);
3010 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003011 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02003012 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003013 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
3014
Avi Kivityb93463a2007-10-25 16:52:32 +02003015 kvm_lapic_sync_to_vapic(vcpu);
3016
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003017 up_read(&vcpu->kvm->slots_lock);
3018
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003019 kvm_guest_enter();
3020
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003021
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003022 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003023 kvm_x86_ops->run(vcpu, kvm_run);
3024
3025 vcpu->guest_mode = 0;
3026 local_irq_enable();
3027
3028 ++vcpu->stat.exits;
3029
3030 /*
3031 * We must have an instruction between local_irq_enable() and
3032 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3033 * the interrupt shadow. The stat.exits increment will do nicely.
3034 * But we need to prevent reordering, hence this barrier():
3035 */
3036 barrier();
3037
3038 kvm_guest_exit();
3039
3040 preempt_enable();
3041
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003042 down_read(&vcpu->kvm->slots_lock);
3043
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003044 /*
3045 * Profile KVM exit RIPs:
3046 */
3047 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003048 unsigned long rip = kvm_rip_read(vcpu);
3049 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003050 }
3051
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003052 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
3053 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02003054
Avi Kivityb93463a2007-10-25 16:52:32 +02003055 kvm_lapic_sync_from_vapic(vcpu);
3056
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003057 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003058out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003059 return r;
3060}
3061
3062static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3063{
3064 int r;
3065
3066 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003067 pr_debug("vcpu %d received sipi with vector # %x\n",
3068 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003069 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003070 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003071 if (r)
3072 return r;
3073 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003074 }
3075
Marcelo Tosattid7690172008-09-08 15:23:48 -03003076 down_read(&vcpu->kvm->slots_lock);
3077 vapic_enter(vcpu);
3078
3079 r = 1;
3080 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003081 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003082 r = vcpu_enter_guest(vcpu, kvm_run);
3083 else {
3084 up_read(&vcpu->kvm->slots_lock);
3085 kvm_vcpu_block(vcpu);
3086 down_read(&vcpu->kvm->slots_lock);
3087 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
3088 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
3089 vcpu->arch.mp_state =
3090 KVM_MP_STATE_RUNNABLE;
3091 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
3092 r = -EINTR;
3093 }
3094
3095 if (r > 0) {
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02003096 if (dm_request_for_nmi_injection(vcpu, kvm_run)) {
3097 r = -EINTR;
3098 kvm_run->exit_reason = KVM_EXIT_NMI;
3099 ++vcpu->stat.request_nmi_exits;
3100 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03003101 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
3102 r = -EINTR;
3103 kvm_run->exit_reason = KVM_EXIT_INTR;
3104 ++vcpu->stat.request_irq_exits;
3105 }
3106 if (signal_pending(current)) {
3107 r = -EINTR;
3108 kvm_run->exit_reason = KVM_EXIT_INTR;
3109 ++vcpu->stat.signal_exits;
3110 }
3111 if (need_resched()) {
3112 up_read(&vcpu->kvm->slots_lock);
3113 kvm_resched(vcpu);
3114 down_read(&vcpu->kvm->slots_lock);
3115 }
3116 }
3117 }
3118
3119 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003120 post_kvm_run_save(vcpu, kvm_run);
3121
Avi Kivityb93463a2007-10-25 16:52:32 +02003122 vapic_exit(vcpu);
3123
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003124 return r;
3125}
3126
3127int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3128{
3129 int r;
3130 sigset_t sigsaved;
3131
3132 vcpu_load(vcpu);
3133
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003134 if (vcpu->sigset_active)
3135 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3136
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003137 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3138 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003139 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003140 r = -EAGAIN;
3141 goto out;
3142 }
3143
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003144 /* re-sync apic's tpr */
3145 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003146 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003148 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003149 r = complete_pio(vcpu);
3150 if (r)
3151 goto out;
3152 }
3153#if CONFIG_HAS_IOMEM
3154 if (vcpu->mmio_needed) {
3155 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3156 vcpu->mmio_read_completed = 1;
3157 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003158
3159 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003160 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003161 vcpu->arch.mmio_fault_cr2, 0,
3162 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003163 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003164 if (r == EMULATE_DO_MMIO) {
3165 /*
3166 * Read-modify-write. Back to userspace.
3167 */
3168 r = 0;
3169 goto out;
3170 }
3171 }
3172#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003173 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3174 kvm_register_write(vcpu, VCPU_REGS_RAX,
3175 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003176
3177 r = __vcpu_run(vcpu, kvm_run);
3178
3179out:
3180 if (vcpu->sigset_active)
3181 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3182
3183 vcpu_put(vcpu);
3184 return r;
3185}
3186
3187int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3188{
3189 vcpu_load(vcpu);
3190
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003191 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3192 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3193 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3194 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3195 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3196 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3197 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3198 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003199#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003200 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3201 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3202 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3203 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3204 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3205 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3206 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3207 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003208#endif
3209
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003210 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003211 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3212
3213 /*
3214 * Don't leak debug flags in case they were set for guest debugging
3215 */
3216 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
3217 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3218
3219 vcpu_put(vcpu);
3220
3221 return 0;
3222}
3223
3224int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3225{
3226 vcpu_load(vcpu);
3227
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003228 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3229 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3230 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3231 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3232 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3233 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3234 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3235 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003236#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003237 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3238 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3239 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3240 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3241 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3242 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3243 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3244 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3245
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003246#endif
3247
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003248 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003249 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3250
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003251
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003252 vcpu->arch.exception.pending = false;
3253
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003254 vcpu_put(vcpu);
3255
3256 return 0;
3257}
3258
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003259void kvm_get_segment(struct kvm_vcpu *vcpu,
3260 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003261{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003262 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003263}
3264
3265void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3266{
3267 struct kvm_segment cs;
3268
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003269 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003270 *db = cs.db;
3271 *l = cs.l;
3272}
3273EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3274
3275int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3276 struct kvm_sregs *sregs)
3277{
3278 struct descriptor_table dt;
3279 int pending_vec;
3280
3281 vcpu_load(vcpu);
3282
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003283 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3284 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3285 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3286 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3287 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3288 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003289
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003290 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3291 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003292
3293 kvm_x86_ops->get_idt(vcpu, &dt);
3294 sregs->idt.limit = dt.limit;
3295 sregs->idt.base = dt.base;
3296 kvm_x86_ops->get_gdt(vcpu, &dt);
3297 sregs->gdt.limit = dt.limit;
3298 sregs->gdt.base = dt.base;
3299
3300 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003301 sregs->cr0 = vcpu->arch.cr0;
3302 sregs->cr2 = vcpu->arch.cr2;
3303 sregs->cr3 = vcpu->arch.cr3;
3304 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003305 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003306 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003307 sregs->apic_base = kvm_get_apic_base(vcpu);
3308
3309 if (irqchip_in_kernel(vcpu->kvm)) {
3310 memset(sregs->interrupt_bitmap, 0,
3311 sizeof sregs->interrupt_bitmap);
3312 pending_vec = kvm_x86_ops->get_irq(vcpu);
3313 if (pending_vec >= 0)
3314 set_bit(pending_vec,
3315 (unsigned long *)sregs->interrupt_bitmap);
3316 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003317 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003318 sizeof sregs->interrupt_bitmap);
3319
3320 vcpu_put(vcpu);
3321
3322 return 0;
3323}
3324
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003325int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3326 struct kvm_mp_state *mp_state)
3327{
3328 vcpu_load(vcpu);
3329 mp_state->mp_state = vcpu->arch.mp_state;
3330 vcpu_put(vcpu);
3331 return 0;
3332}
3333
3334int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3335 struct kvm_mp_state *mp_state)
3336{
3337 vcpu_load(vcpu);
3338 vcpu->arch.mp_state = mp_state->mp_state;
3339 vcpu_put(vcpu);
3340 return 0;
3341}
3342
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003343static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003344 struct kvm_segment *var, int seg)
3345{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003346 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003347}
3348
Izik Eidus37817f22008-03-24 23:14:53 +02003349static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3350 struct kvm_segment *kvm_desct)
3351{
3352 kvm_desct->base = seg_desc->base0;
3353 kvm_desct->base |= seg_desc->base1 << 16;
3354 kvm_desct->base |= seg_desc->base2 << 24;
3355 kvm_desct->limit = seg_desc->limit0;
3356 kvm_desct->limit |= seg_desc->limit << 16;
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003357 if (seg_desc->g) {
3358 kvm_desct->limit <<= 12;
3359 kvm_desct->limit |= 0xfff;
3360 }
Izik Eidus37817f22008-03-24 23:14:53 +02003361 kvm_desct->selector = selector;
3362 kvm_desct->type = seg_desc->type;
3363 kvm_desct->present = seg_desc->p;
3364 kvm_desct->dpl = seg_desc->dpl;
3365 kvm_desct->db = seg_desc->d;
3366 kvm_desct->s = seg_desc->s;
3367 kvm_desct->l = seg_desc->l;
3368 kvm_desct->g = seg_desc->g;
3369 kvm_desct->avl = seg_desc->avl;
3370 if (!selector)
3371 kvm_desct->unusable = 1;
3372 else
3373 kvm_desct->unusable = 0;
3374 kvm_desct->padding = 0;
3375}
3376
Amit Shahb8222ad2008-10-22 16:39:47 +05303377static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
3378 u16 selector,
3379 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02003380{
3381 if (selector & 1 << 2) {
3382 struct kvm_segment kvm_seg;
3383
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003384 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003385
3386 if (kvm_seg.unusable)
3387 dtable->limit = 0;
3388 else
3389 dtable->limit = kvm_seg.limit;
3390 dtable->base = kvm_seg.base;
3391 }
3392 else
3393 kvm_x86_ops->get_gdt(vcpu, dtable);
3394}
3395
3396/* allowed just for 8 bytes segments */
3397static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3398 struct desc_struct *seg_desc)
3399{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003400 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003401 struct descriptor_table dtable;
3402 u16 index = selector >> 3;
3403
Amit Shahb8222ad2008-10-22 16:39:47 +05303404 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003405
3406 if (dtable.limit < index * 8 + 7) {
3407 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3408 return 1;
3409 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003410 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3411 gpa += index * 8;
3412 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003413}
3414
3415/* allowed just for 8 bytes segments */
3416static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3417 struct desc_struct *seg_desc)
3418{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003419 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003420 struct descriptor_table dtable;
3421 u16 index = selector >> 3;
3422
Amit Shahb8222ad2008-10-22 16:39:47 +05303423 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003424
3425 if (dtable.limit < index * 8 + 7)
3426 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003427 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3428 gpa += index * 8;
3429 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003430}
3431
3432static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3433 struct desc_struct *seg_desc)
3434{
3435 u32 base_addr;
3436
3437 base_addr = seg_desc->base0;
3438 base_addr |= (seg_desc->base1 << 16);
3439 base_addr |= (seg_desc->base2 << 24);
3440
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003441 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02003442}
3443
Izik Eidus37817f22008-03-24 23:14:53 +02003444static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3445{
3446 struct kvm_segment kvm_seg;
3447
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003448 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003449 return kvm_seg.selector;
3450}
3451
3452static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3453 u16 selector,
3454 struct kvm_segment *kvm_seg)
3455{
3456 struct desc_struct seg_desc;
3457
3458 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3459 return 1;
3460 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3461 return 0;
3462}
3463
Harvey Harrison2259e3a2008-08-22 13:29:17 -07003464static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003465{
3466 struct kvm_segment segvar = {
3467 .base = selector << 4,
3468 .limit = 0xffff,
3469 .selector = selector,
3470 .type = 3,
3471 .present = 1,
3472 .dpl = 3,
3473 .db = 0,
3474 .s = 1,
3475 .l = 0,
3476 .g = 0,
3477 .avl = 0,
3478 .unusable = 0,
3479 };
3480 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
3481 return 0;
3482}
3483
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003484int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3485 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02003486{
3487 struct kvm_segment kvm_seg;
3488
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003489 if (!(vcpu->arch.cr0 & X86_CR0_PE))
3490 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003491 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3492 return 1;
3493 kvm_seg.type |= type_bits;
3494
3495 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3496 seg != VCPU_SREG_LDTR)
3497 if (!kvm_seg.s)
3498 kvm_seg.unusable = 1;
3499
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003500 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003501 return 0;
3502}
3503
3504static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3505 struct tss_segment_32 *tss)
3506{
3507 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003508 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003509 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003510 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3511 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3512 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3513 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3514 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3515 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3516 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3517 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003518 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3519 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3520 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3521 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3522 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3523 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3524 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3525 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3526}
3527
3528static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3529 struct tss_segment_32 *tss)
3530{
3531 kvm_set_cr3(vcpu, tss->cr3);
3532
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003533 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02003534 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3535
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003536 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
3537 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
3538 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
3539 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
3540 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
3541 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
3542 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
3543 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02003544
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003545 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003546 return 1;
3547
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003548 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003549 return 1;
3550
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003551 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003552 return 1;
3553
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003554 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003555 return 1;
3556
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003557 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003558 return 1;
3559
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003560 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02003561 return 1;
3562
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003563 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02003564 return 1;
3565 return 0;
3566}
3567
3568static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3569 struct tss_segment_16 *tss)
3570{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003571 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003572 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003573 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3574 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3575 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3576 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3577 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3578 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3579 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
3580 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003581
3582 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3583 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3584 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3585 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3586 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3587 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3588}
3589
3590static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3591 struct tss_segment_16 *tss)
3592{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003593 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02003594 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003595 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
3596 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
3597 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
3598 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
3599 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
3600 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
3601 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
3602 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02003603
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003604 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003605 return 1;
3606
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003607 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003608 return 1;
3609
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003610 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003611 return 1;
3612
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003613 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003614 return 1;
3615
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003616 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003617 return 1;
3618 return 0;
3619}
3620
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003621static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003622 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003623 struct desc_struct *nseg_desc)
3624{
3625 struct tss_segment_16 tss_segment_16;
3626 int ret = 0;
3627
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003628 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3629 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003630 goto out;
3631
3632 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02003633
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003634 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3635 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003636 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003637
3638 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3639 &tss_segment_16, sizeof tss_segment_16))
3640 goto out;
3641
Izik Eidus37817f22008-03-24 23:14:53 +02003642 if (load_state_from_tss16(vcpu, &tss_segment_16))
3643 goto out;
3644
3645 ret = 1;
3646out:
3647 return ret;
3648}
3649
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003650static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003651 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003652 struct desc_struct *nseg_desc)
3653{
3654 struct tss_segment_32 tss_segment_32;
3655 int ret = 0;
3656
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003657 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3658 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003659 goto out;
3660
3661 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02003662
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003663 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3664 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003665 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003666
3667 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3668 &tss_segment_32, sizeof tss_segment_32))
3669 goto out;
3670
Izik Eidus37817f22008-03-24 23:14:53 +02003671 if (load_state_from_tss32(vcpu, &tss_segment_32))
3672 goto out;
3673
3674 ret = 1;
3675out:
3676 return ret;
3677}
3678
3679int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3680{
3681 struct kvm_segment tr_seg;
3682 struct desc_struct cseg_desc;
3683 struct desc_struct nseg_desc;
3684 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003685 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
3686 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003687
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003688 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02003689
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003690 /* FIXME: Handle errors. Failure to read either TSS or their
3691 * descriptors should generate a pagefault.
3692 */
Izik Eidus37817f22008-03-24 23:14:53 +02003693 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3694 goto out;
3695
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003696 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02003697 goto out;
3698
Izik Eidus37817f22008-03-24 23:14:53 +02003699 if (reason != TASK_SWITCH_IRET) {
3700 int cpl;
3701
3702 cpl = kvm_x86_ops->get_cpl(vcpu);
3703 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3704 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3705 return 1;
3706 }
3707 }
3708
3709 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3710 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3711 return 1;
3712 }
3713
3714 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003715 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003716 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02003717 }
3718
3719 if (reason == TASK_SWITCH_IRET) {
3720 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3721 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
3722 }
3723
3724 kvm_x86_ops->skip_emulated_instruction(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003725
3726 if (nseg_desc.type & 8)
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003727 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003728 &nseg_desc);
3729 else
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003730 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003731 &nseg_desc);
3732
3733 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
3734 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3735 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
3736 }
3737
3738 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003739 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02003740 save_guest_segment_descriptor(vcpu, tss_selector,
3741 &nseg_desc);
3742 }
3743
3744 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
3745 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
3746 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003747 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003748out:
Izik Eidus37817f22008-03-24 23:14:53 +02003749 return ret;
3750}
3751EXPORT_SYMBOL_GPL(kvm_task_switch);
3752
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003753int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3754 struct kvm_sregs *sregs)
3755{
3756 int mmu_reset_needed = 0;
3757 int i, pending_vec, max_bits;
3758 struct descriptor_table dt;
3759
3760 vcpu_load(vcpu);
3761
3762 dt.limit = sregs->idt.limit;
3763 dt.base = sregs->idt.base;
3764 kvm_x86_ops->set_idt(vcpu, &dt);
3765 dt.limit = sregs->gdt.limit;
3766 dt.base = sregs->gdt.base;
3767 kvm_x86_ops->set_gdt(vcpu, &dt);
3768
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003769 vcpu->arch.cr2 = sregs->cr2;
3770 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
3771 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003772
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003773 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003774
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003775 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003776 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003777 kvm_set_apic_base(vcpu, sregs->apic_base);
3778
3779 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3780
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003781 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003782 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00003783 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003784
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003785 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003786 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3787 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003788 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003789
3790 if (mmu_reset_needed)
3791 kvm_mmu_reset_context(vcpu);
3792
3793 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003794 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3795 sizeof vcpu->arch.irq_pending);
3796 vcpu->arch.irq_summary = 0;
3797 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3798 if (vcpu->arch.irq_pending[i])
3799 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003800 } else {
3801 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3802 pending_vec = find_first_bit(
3803 (const unsigned long *)sregs->interrupt_bitmap,
3804 max_bits);
3805 /* Only pending external irq is handled here */
3806 if (pending_vec < max_bits) {
3807 kvm_x86_ops->set_irq(vcpu, pending_vec);
3808 pr_debug("Set back pending irq %d\n",
3809 pending_vec);
3810 }
Marcelo Tosattie4825802008-09-24 20:28:34 -03003811 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003812 }
3813
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003814 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3815 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3816 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3817 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3818 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3819 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003820
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003821 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3822 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003823
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03003824 /* Older userspace won't unhalt the vcpu on reset. */
3825 if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 &&
3826 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
3827 !(vcpu->arch.cr0 & X86_CR0_PE))
3828 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3829
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003830 vcpu_put(vcpu);
3831
3832 return 0;
3833}
3834
3835int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3836 struct kvm_debug_guest *dbg)
3837{
3838 int r;
3839
3840 vcpu_load(vcpu);
3841
3842 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3843
3844 vcpu_put(vcpu);
3845
3846 return r;
3847}
3848
3849/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05003850 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3851 * we have asm/x86/processor.h
3852 */
3853struct fxsave {
3854 u16 cwd;
3855 u16 swd;
3856 u16 twd;
3857 u16 fop;
3858 u64 rip;
3859 u64 rdp;
3860 u32 mxcsr;
3861 u32 mxcsr_mask;
3862 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3863#ifdef CONFIG_X86_64
3864 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3865#else
3866 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3867#endif
3868};
3869
Zhang Xiantao8b006792007-11-16 13:05:55 +08003870/*
3871 * Translate a guest virtual address to a guest physical address.
3872 */
3873int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3874 struct kvm_translation *tr)
3875{
3876 unsigned long vaddr = tr->linear_address;
3877 gpa_t gpa;
3878
3879 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003880 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003881 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003882 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08003883 tr->physical_address = gpa;
3884 tr->valid = gpa != UNMAPPED_GVA;
3885 tr->writeable = 1;
3886 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003887 vcpu_put(vcpu);
3888
3889 return 0;
3890}
3891
Hollis Blanchardd0752062007-10-31 17:24:25 -05003892int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3893{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003894 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003895
3896 vcpu_load(vcpu);
3897
3898 memcpy(fpu->fpr, fxsave->st_space, 128);
3899 fpu->fcw = fxsave->cwd;
3900 fpu->fsw = fxsave->swd;
3901 fpu->ftwx = fxsave->twd;
3902 fpu->last_opcode = fxsave->fop;
3903 fpu->last_ip = fxsave->rip;
3904 fpu->last_dp = fxsave->rdp;
3905 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3906
3907 vcpu_put(vcpu);
3908
3909 return 0;
3910}
3911
3912int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3913{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003914 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003915
3916 vcpu_load(vcpu);
3917
3918 memcpy(fxsave->st_space, fpu->fpr, 128);
3919 fxsave->cwd = fpu->fcw;
3920 fxsave->swd = fpu->fsw;
3921 fxsave->twd = fpu->ftwx;
3922 fxsave->fop = fpu->last_opcode;
3923 fxsave->rip = fpu->last_ip;
3924 fxsave->rdp = fpu->last_dp;
3925 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3926
3927 vcpu_put(vcpu);
3928
3929 return 0;
3930}
3931
3932void fx_init(struct kvm_vcpu *vcpu)
3933{
3934 unsigned after_mxcsr_mask;
3935
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02003936 /*
3937 * Touch the fpu the first time in non atomic context as if
3938 * this is the first fpu instruction the exception handler
3939 * will fire before the instruction returns and it'll have to
3940 * allocate ram with GFP_KERNEL.
3941 */
3942 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003943 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02003944
Hollis Blanchardd0752062007-10-31 17:24:25 -05003945 /* Initialize guest FPU by resetting ours and saving into guest's */
3946 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003947 kvm_fx_save(&vcpu->arch.host_fx_image);
3948 kvm_fx_finit();
3949 kvm_fx_save(&vcpu->arch.guest_fx_image);
3950 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003951 preempt_enable();
3952
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003953 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003954 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003955 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3956 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003957 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3958}
3959EXPORT_SYMBOL_GPL(fx_init);
3960
3961void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3962{
3963 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3964 return;
3965
3966 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003967 kvm_fx_save(&vcpu->arch.host_fx_image);
3968 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003969}
3970EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3971
3972void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3973{
3974 if (!vcpu->guest_fpu_loaded)
3975 return;
3976
3977 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003978 kvm_fx_save(&vcpu->arch.guest_fx_image);
3979 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003980 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003981}
3982EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003983
3984void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3985{
3986 kvm_x86_ops->vcpu_free(vcpu);
3987}
3988
3989struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3990 unsigned int id)
3991{
Avi Kivity26e52152007-11-20 15:30:24 +02003992 return kvm_x86_ops->vcpu_create(kvm, id);
3993}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003994
Avi Kivity26e52152007-11-20 15:30:24 +02003995int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3996{
3997 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003998
3999 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004000 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004001
Sheng Yang0bed3b52008-10-09 16:01:54 +08004002 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004003 vcpu_load(vcpu);
4004 r = kvm_arch_vcpu_reset(vcpu);
4005 if (r == 0)
4006 r = kvm_mmu_setup(vcpu);
4007 vcpu_put(vcpu);
4008 if (r < 0)
4009 goto free_vcpu;
4010
Avi Kivity26e52152007-11-20 15:30:24 +02004011 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004012free_vcpu:
4013 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004014 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004015}
4016
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004017void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004018{
4019 vcpu_load(vcpu);
4020 kvm_mmu_unload(vcpu);
4021 vcpu_put(vcpu);
4022
4023 kvm_x86_ops->vcpu_free(vcpu);
4024}
4025
4026int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4027{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004028 vcpu->arch.nmi_pending = false;
4029 vcpu->arch.nmi_injected = false;
4030
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004031 return kvm_x86_ops->vcpu_reset(vcpu);
4032}
4033
4034void kvm_arch_hardware_enable(void *garbage)
4035{
4036 kvm_x86_ops->hardware_enable(garbage);
4037}
4038
4039void kvm_arch_hardware_disable(void *garbage)
4040{
4041 kvm_x86_ops->hardware_disable(garbage);
4042}
4043
4044int kvm_arch_hardware_setup(void)
4045{
4046 return kvm_x86_ops->hardware_setup();
4047}
4048
4049void kvm_arch_hardware_unsetup(void)
4050{
4051 kvm_x86_ops->hardware_unsetup();
4052}
4053
4054void kvm_arch_check_processor_compat(void *rtn)
4055{
4056 kvm_x86_ops->check_processor_compatibility(rtn);
4057}
4058
4059int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4060{
4061 struct page *page;
4062 struct kvm *kvm;
4063 int r;
4064
4065 BUG_ON(vcpu->kvm == NULL);
4066 kvm = vcpu->kvm;
4067
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004068 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004069 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Avi Kivitya4535292008-04-13 17:54:35 +03004070 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004071 else
Avi Kivitya4535292008-04-13 17:54:35 +03004072 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004073
4074 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4075 if (!page) {
4076 r = -ENOMEM;
4077 goto fail;
4078 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004079 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004080
4081 r = kvm_mmu_create(vcpu);
4082 if (r < 0)
4083 goto fail_free_pio_data;
4084
4085 if (irqchip_in_kernel(kvm)) {
4086 r = kvm_create_lapic(vcpu);
4087 if (r < 0)
4088 goto fail_mmu_destroy;
4089 }
4090
4091 return 0;
4092
4093fail_mmu_destroy:
4094 kvm_mmu_destroy(vcpu);
4095fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004096 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004097fail:
4098 return r;
4099}
4100
4101void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4102{
4103 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004104 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004105 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004106 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004107 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004108}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004109
4110struct kvm *kvm_arch_create_vm(void)
4111{
4112 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4113
4114 if (!kvm)
4115 return ERR_PTR(-ENOMEM);
4116
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004117 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02004118 INIT_LIST_HEAD(&kvm->arch.oos_global_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004119 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004120
Sheng Yang5550af42008-10-15 20:15:06 +08004121 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4122 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
4123
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004124 return kvm;
4125}
4126
4127static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4128{
4129 vcpu_load(vcpu);
4130 kvm_mmu_unload(vcpu);
4131 vcpu_put(vcpu);
4132}
4133
4134static void kvm_free_vcpus(struct kvm *kvm)
4135{
4136 unsigned int i;
4137
4138 /*
4139 * Unpin any mmu pages first.
4140 */
4141 for (i = 0; i < KVM_MAX_VCPUS; ++i)
4142 if (kvm->vcpus[i])
4143 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
4144 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
4145 if (kvm->vcpus[i]) {
4146 kvm_arch_vcpu_free(kvm->vcpus[i]);
4147 kvm->vcpus[i] = NULL;
4148 }
4149 }
4150
4151}
4152
4153void kvm_arch_destroy_vm(struct kvm *kvm)
4154{
Amit Shahbfadade2008-09-16 18:04:28 +03004155 kvm_free_all_assigned_devices(kvm);
Sheng Yang6eb55812008-10-31 12:37:41 +08004156 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08004157 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08004158 kfree(kvm->arch.vpic);
4159 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004160 kvm_free_vcpus(kvm);
4161 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02004162 if (kvm->arch.apic_access_page)
4163 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004164 if (kvm->arch.ept_identity_pagetable)
4165 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004166 kfree(kvm);
4167}
Zhang Xiantao0de10342007-11-20 16:25:04 +08004168
4169int kvm_arch_set_memory_region(struct kvm *kvm,
4170 struct kvm_userspace_memory_region *mem,
4171 struct kvm_memory_slot old,
4172 int user_alloc)
4173{
4174 int npages = mem->memory_size >> PAGE_SHIFT;
4175 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4176
4177 /*To keep backward compatibility with older userspace,
4178 *x86 needs to hanlde !user_alloc case.
4179 */
4180 if (!user_alloc) {
4181 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004182 unsigned long userspace_addr;
4183
Izik Eidus72dc67a2008-02-10 18:04:15 +02004184 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004185 userspace_addr = do_mmap(NULL, 0,
4186 npages * PAGE_SIZE,
4187 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03004188 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004189 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004190 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004191
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004192 if (IS_ERR((void *)userspace_addr))
4193 return PTR_ERR((void *)userspace_addr);
4194
4195 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4196 spin_lock(&kvm->mmu_lock);
4197 memslot->userspace_addr = userspace_addr;
4198 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004199 } else {
4200 if (!old.user_alloc && old.rmap) {
4201 int ret;
4202
Izik Eidus72dc67a2008-02-10 18:04:15 +02004203 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004204 ret = do_munmap(current->mm, old.userspace_addr,
4205 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004206 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004207 if (ret < 0)
4208 printk(KERN_WARNING
4209 "kvm_vm_ioctl_set_memory_region: "
4210 "failed to munmap memory\n");
4211 }
4212 }
4213 }
4214
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004215 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004216 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4217 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4218 }
4219
4220 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
4221 kvm_flush_remote_tlbs(kvm);
4222
4223 return 0;
4224}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004225
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004226void kvm_arch_flush_shadow(struct kvm *kvm)
4227{
4228 kvm_mmu_zap_all(kvm);
4229}
4230
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004231int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4232{
Avi Kivitya4535292008-04-13 17:54:35 +03004233 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Jan Kiszka0496fbb2008-09-26 09:30:53 +02004234 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
4235 || vcpu->arch.nmi_pending;
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004236}
Zhang Xiantao57361992007-12-17 14:21:40 +08004237
4238static void vcpu_kick_intr(void *info)
4239{
4240#ifdef DEBUG
4241 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
4242 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
4243#endif
4244}
4245
4246void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4247{
4248 int ipi_pcpu = vcpu->cpu;
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004249 int cpu = get_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004250
4251 if (waitqueue_active(&vcpu->wq)) {
4252 wake_up_interruptible(&vcpu->wq);
4253 ++vcpu->stat.halt_wakeup;
4254 }
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004255 /*
4256 * We may be called synchronously with irqs disabled in guest mode,
4257 * So need not to call smp_call_function_single() in that case.
4258 */
4259 if (vcpu->guest_mode && vcpu->cpu != cpu)
Jens Axboe8691e5a2008-06-06 11:18:06 +02004260 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004261 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004262}