blob: 75e9df09784548486b3e89705c653e521b17841b [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010034#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080035#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050036#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010037#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030038#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#include <linux/cpufreq.h>
Carsten Otte043405e2007-10-10 17:16:19 +020040
41#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080042#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020043#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080044#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080045#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020046
Carsten Otte313a3dc2007-10-11 19:16:52 +020047#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010048#define CR0_RESERVED_BITS \
49 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
50 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
51 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
52#define CR4_RESERVED_BITS \
53 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
54 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
55 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
56 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
57
58#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080059
60#define KVM_MAX_MCE_BANKS 32
61#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
62
Joerg Roedel50a37eb2008-01-31 14:57:38 +010063/* EFER defaults:
64 * - enable syscall per default because its emulated by KVM
65 * - enable LME and LMA per default on 64 bit KVM
66 */
67#ifdef CONFIG_X86_64
68static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
69#else
70static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
71#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020072
Avi Kivityba1389b2007-11-18 16:24:12 +020073#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
74#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050075
Avi Kivity674eea02008-02-11 18:37:23 +020076static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
77 struct kvm_cpuid_entry2 __user *entries);
Alexander Grafd8017472008-11-25 20:17:11 +010078struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
79 u32 function, u32 index);
Avi Kivity674eea02008-02-11 18:37:23 +020080
Zhang Xiantao97896d02007-11-14 20:09:30 +080081struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030082EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080083
Hollis Blanchard417bc302007-10-31 17:24:23 -050084struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020085 { "pf_fixed", VCPU_STAT(pf_fixed) },
86 { "pf_guest", VCPU_STAT(pf_guest) },
87 { "tlb_flush", VCPU_STAT(tlb_flush) },
88 { "invlpg", VCPU_STAT(invlpg) },
89 { "exits", VCPU_STAT(exits) },
90 { "io_exits", VCPU_STAT(io_exits) },
91 { "mmio_exits", VCPU_STAT(mmio_exits) },
92 { "signal_exits", VCPU_STAT(signal_exits) },
93 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +080094 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020095 { "halt_exits", VCPU_STAT(halt_exits) },
96 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +053097 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020098 { "request_irq", VCPU_STAT(request_irq_exits) },
99 { "irq_exits", VCPU_STAT(irq_exits) },
100 { "host_state_reload", VCPU_STAT(host_state_reload) },
101 { "efer_reload", VCPU_STAT(efer_reload) },
102 { "fpu_reload", VCPU_STAT(fpu_reload) },
103 { "insn_emulation", VCPU_STAT(insn_emulation) },
104 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300105 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200106 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200107 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
108 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
109 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
110 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
111 { "mmu_flooded", VM_STAT(mmu_flooded) },
112 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200113 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300114 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200115 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300116 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500117 { NULL }
118};
119
Carsten Otte5fb76f92007-10-29 16:08:51 +0100120unsigned long segment_base(u16 selector)
121{
122 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200123 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100124 unsigned long table_base;
125 unsigned long v;
126
127 if (selector == 0)
128 return 0;
129
130 asm("sgdt %0" : "=m"(gdt));
131 table_base = gdt.base;
132
133 if (selector & 4) { /* from ldt */
134 u16 ldt_selector;
135
136 asm("sldt %0" : "=g"(ldt_selector));
137 table_base = segment_base(ldt_selector);
138 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200139 d = (struct desc_struct *)(table_base + (selector & ~7));
140 v = d->base0 | ((unsigned long)d->base1 << 16) |
141 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100142#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200143 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
144 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100145#endif
146 return v;
147}
148EXPORT_SYMBOL_GPL(segment_base);
149
Carsten Otte6866b832007-10-29 16:09:10 +0100150u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
151{
152 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800153 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100154 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800155 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100156}
157EXPORT_SYMBOL_GPL(kvm_get_apic_base);
158
159void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
160{
161 /* TODO: reserve bits check */
162 if (irqchip_in_kernel(vcpu->kvm))
163 kvm_lapic_set_base(vcpu, data);
164 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800165 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100166}
167EXPORT_SYMBOL_GPL(kvm_set_apic_base);
168
Avi Kivity298101d2007-11-25 13:41:11 +0200169void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
170{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800171 WARN_ON(vcpu->arch.exception.pending);
172 vcpu->arch.exception.pending = true;
173 vcpu->arch.exception.has_error_code = false;
174 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200175}
176EXPORT_SYMBOL_GPL(kvm_queue_exception);
177
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200178void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
179 u32 error_code)
180{
181 ++vcpu->stat.pf_guest;
Alexander Grafd8017472008-11-25 20:17:11 +0100182
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100183 if (vcpu->arch.exception.pending) {
184 if (vcpu->arch.exception.nr == PF_VECTOR) {
185 printk(KERN_DEBUG "kvm: inject_page_fault:"
186 " double fault 0x%lx\n", addr);
187 vcpu->arch.exception.nr = DF_VECTOR;
188 vcpu->arch.exception.error_code = 0;
189 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
190 /* triple fault -> shutdown */
191 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
192 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200193 return;
194 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800195 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200196 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
197}
198
Sheng Yang3419ffc2008-05-15 09:52:48 +0800199void kvm_inject_nmi(struct kvm_vcpu *vcpu)
200{
201 vcpu->arch.nmi_pending = 1;
202}
203EXPORT_SYMBOL_GPL(kvm_inject_nmi);
204
Avi Kivity298101d2007-11-25 13:41:11 +0200205void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
206{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800207 WARN_ON(vcpu->arch.exception.pending);
208 vcpu->arch.exception.pending = true;
209 vcpu->arch.exception.has_error_code = true;
210 vcpu->arch.exception.nr = nr;
211 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200212}
213EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
214
215static void __queue_exception(struct kvm_vcpu *vcpu)
216{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800217 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
218 vcpu->arch.exception.has_error_code,
219 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200220}
221
Carsten Ottea03490e2007-10-29 16:09:35 +0100222/*
223 * Load the pae pdptrs. Return true is they are all valid.
224 */
225int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
226{
227 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
228 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
229 int i;
230 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800231 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100232
Carsten Ottea03490e2007-10-29 16:09:35 +0100233 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
234 offset * sizeof(u64), sizeof(pdpte));
235 if (ret < 0) {
236 ret = 0;
237 goto out;
238 }
239 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Dong, Eddie20c466b2009-03-31 23:03:45 +0800240 if (is_present_pte(pdpte[i]) &&
241 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100242 ret = 0;
243 goto out;
244 }
245 }
246 ret = 1;
247
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800248 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100249out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100250
251 return ret;
252}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100253EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100254
Avi Kivityd835dfe2007-11-21 02:57:59 +0200255static bool pdptrs_changed(struct kvm_vcpu *vcpu)
256{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800257 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200258 bool changed = true;
259 int r;
260
261 if (is_long_mode(vcpu) || !is_pae(vcpu))
262 return false;
263
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800264 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200265 if (r < 0)
266 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800267 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200268out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200269
270 return changed;
271}
272
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200273void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100274{
275 if (cr0 & CR0_RESERVED_BITS) {
276 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800277 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200278 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100279 return;
280 }
281
282 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
283 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200284 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100285 return;
286 }
287
288 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
289 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
290 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200291 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100292 return;
293 }
294
295 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
296#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800297 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100298 int cs_db, cs_l;
299
300 if (!is_pae(vcpu)) {
301 printk(KERN_DEBUG "set_cr0: #GP, start paging "
302 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200303 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100304 return;
305 }
306 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
307 if (cs_l) {
308 printk(KERN_DEBUG "set_cr0: #GP, start paging "
309 "in long mode while CS.L == 1\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 } else
315#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800316 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100317 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
318 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200319 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100320 return;
321 }
322
323 }
324
325 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800326 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100327
Carsten Ottea03490e2007-10-29 16:09:35 +0100328 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100329 return;
330}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200331EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100332
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200333void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100334{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200335 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -0400336 KVMTRACE_1D(LMSW, vcpu,
337 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
338 handler);
Carsten Ottea03490e2007-10-29 16:09:35 +0100339}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200340EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100341
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200342void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100343{
Avi Kivitya2edf572009-05-24 22:19:00 +0300344 unsigned long old_cr4 = vcpu->arch.cr4;
345 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
346
Carsten Ottea03490e2007-10-29 16:09:35 +0100347 if (cr4 & CR4_RESERVED_BITS) {
348 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200349 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100350 return;
351 }
352
353 if (is_long_mode(vcpu)) {
354 if (!(cr4 & X86_CR4_PAE)) {
355 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
356 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200357 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100358 return;
359 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300360 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
361 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800362 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100363 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200364 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100365 return;
366 }
367
368 if (cr4 & X86_CR4_VMXE) {
369 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200370 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100371 return;
372 }
373 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800374 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200375 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100376 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100377}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200378EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100379
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200380void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100381{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800382 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300383 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200384 kvm_mmu_flush_tlb(vcpu);
385 return;
386 }
387
Carsten Ottea03490e2007-10-29 16:09:35 +0100388 if (is_long_mode(vcpu)) {
389 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
390 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200391 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100392 return;
393 }
394 } else {
395 if (is_pae(vcpu)) {
396 if (cr3 & CR3_PAE_RESERVED_BITS) {
397 printk(KERN_DEBUG
398 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200399 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100400 return;
401 }
402 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
403 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
404 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200405 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100406 return;
407 }
408 }
409 /*
410 * We don't check reserved bits in nonpae mode, because
411 * this isn't enforced, and VMware depends on this.
412 */
413 }
414
Carsten Ottea03490e2007-10-29 16:09:35 +0100415 /*
416 * Does the new cr3 value map to physical memory? (Note, we
417 * catch an invalid cr3 even in real-mode, because it would
418 * cause trouble later on when we turn on paging anyway.)
419 *
420 * A real CPU would silently accept an invalid cr3 and would
421 * attempt to use it - with largely undefined (and often hard
422 * to debug) behavior on the guest side.
423 */
424 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200425 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100426 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800427 vcpu->arch.cr3 = cr3;
428 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100429 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100430}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200431EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100432
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200433void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100434{
435 if (cr8 & CR8_RESERVED_BITS) {
436 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200437 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100438 return;
439 }
440 if (irqchip_in_kernel(vcpu->kvm))
441 kvm_lapic_set_tpr(vcpu, cr8);
442 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800443 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100444}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200445EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100446
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200447unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100448{
449 if (irqchip_in_kernel(vcpu->kvm))
450 return kvm_lapic_get_cr8(vcpu);
451 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800452 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100453}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200454EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100455
Alexander Grafd8017472008-11-25 20:17:11 +0100456static inline u32 bit(int bitno)
457{
458 return 1 << (bitno & 31);
459}
460
Carsten Otte043405e2007-10-10 17:16:19 +0200461/*
462 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
463 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
464 *
465 * This list is modified at module load time to reflect the
466 * capabilities of the host cpu.
467 */
468static u32 msrs_to_save[] = {
469 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
470 MSR_K6_STAR,
471#ifdef CONFIG_X86_64
472 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
473#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530474 MSR_IA32_TSC, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Alexander Grafb286d5d2008-11-25 20:17:05 +0100475 MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200476};
477
478static unsigned num_msrs_to_save;
479
480static u32 emulated_msrs[] = {
481 MSR_IA32_MISC_ENABLE,
482};
483
Carsten Otte15c4a642007-10-30 18:44:17 +0100484static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
485{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100486 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100487 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
488 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200489 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100490 return;
491 }
492
493 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800494 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100495 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200496 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100497 return;
498 }
499
Alexander Graf1b2fd702009-02-02 16:23:51 +0100500 if (efer & EFER_FFXSR) {
501 struct kvm_cpuid_entry2 *feat;
502
503 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
504 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
505 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
506 kvm_inject_gp(vcpu, 0);
507 return;
508 }
509 }
510
Alexander Grafd8017472008-11-25 20:17:11 +0100511 if (efer & EFER_SVME) {
512 struct kvm_cpuid_entry2 *feat;
513
514 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
515 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
516 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
517 kvm_inject_gp(vcpu, 0);
518 return;
519 }
520 }
521
Carsten Otte15c4a642007-10-30 18:44:17 +0100522 kvm_x86_ops->set_efer(vcpu, efer);
523
524 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800525 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100526
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800527 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300528
529 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
530 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100531}
532
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100533void kvm_enable_efer_bits(u64 mask)
534{
535 efer_reserved_bits &= ~mask;
536}
537EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
538
539
Carsten Otte15c4a642007-10-30 18:44:17 +0100540/*
541 * Writes msr value into into the appropriate "register".
542 * Returns 0 on success, non-0 otherwise.
543 * Assumes vcpu_load() was already called.
544 */
545int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
546{
547 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
548}
549
Carsten Otte313a3dc2007-10-11 19:16:52 +0200550/*
551 * Adapt set_msr() to msr_io()'s calling convention
552 */
553static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
554{
555 return kvm_set_msr(vcpu, index, *data);
556}
557
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200558static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
559{
560 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200561 struct pvclock_wall_clock wc;
562 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200563
564 if (!wall_clock)
565 return;
566
567 version++;
568
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200569 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
570
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200571 /*
572 * The guest calculates current wall clock time by adding
573 * system time (updated by kvm_write_guest_time below) to the
574 * wall clock specified here. guest system time equals host
575 * system time for us, thus we must fill in host boot time here.
576 */
577 now = current_kernel_time();
578 ktime_get_ts(&sys);
579 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
580
581 wc.sec = boot.tv_sec;
582 wc.nsec = boot.tv_nsec;
583 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200584
585 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
586
587 version++;
588 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200589}
590
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200591static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
592{
593 uint32_t quotient, remainder;
594
595 /* Don't try to replace with do_div(), this one calculates
596 * "(dividend << 32) / divisor" */
597 __asm__ ( "divl %4"
598 : "=a" (quotient), "=d" (remainder)
599 : "0" (0), "1" (dividend), "r" (divisor) );
600 return quotient;
601}
602
603static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
604{
605 uint64_t nsecs = 1000000000LL;
606 int32_t shift = 0;
607 uint64_t tps64;
608 uint32_t tps32;
609
610 tps64 = tsc_khz * 1000LL;
611 while (tps64 > nsecs*2) {
612 tps64 >>= 1;
613 shift--;
614 }
615
616 tps32 = (uint32_t)tps64;
617 while (tps32 <= (uint32_t)nsecs) {
618 tps32 <<= 1;
619 shift++;
620 }
621
622 hv_clock->tsc_shift = shift;
623 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
624
625 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700626 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200627 hv_clock->tsc_to_system_mul);
628}
629
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100630static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
631
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200632static void kvm_write_guest_time(struct kvm_vcpu *v)
633{
634 struct timespec ts;
635 unsigned long flags;
636 struct kvm_vcpu_arch *vcpu = &v->arch;
637 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300638 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200639
640 if ((!vcpu->time_page))
641 return;
642
Avi Kivity463656c2009-04-12 15:49:07 +0300643 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
644 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
645 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
646 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200647 }
Avi Kivity463656c2009-04-12 15:49:07 +0300648 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200649
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200650 /* Keep irq disabled to prevent changes to the clock */
651 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530652 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200653 ktime_get_ts(&ts);
654 local_irq_restore(flags);
655
656 /* With all the info we got, fill in the values */
657
658 vcpu->hv_clock.system_time = ts.tv_nsec +
659 (NSEC_PER_SEC * (u64)ts.tv_sec);
660 /*
661 * The interface expects us to write an even number signaling that the
662 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200663 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200664 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200665 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200666
667 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
668
669 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200670 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200671
672 kunmap_atomic(shared_kaddr, KM_USER0);
673
674 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
675}
676
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100677static int kvm_request_guest_time_update(struct kvm_vcpu *v)
678{
679 struct kvm_vcpu_arch *vcpu = &v->arch;
680
681 if (!vcpu->time_page)
682 return 0;
683 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
684 return 1;
685}
686
Avi Kivity9ba075a2008-05-26 20:06:35 +0300687static bool msr_mtrr_valid(unsigned msr)
688{
689 switch (msr) {
690 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
691 case MSR_MTRRfix64K_00000:
692 case MSR_MTRRfix16K_80000:
693 case MSR_MTRRfix16K_A0000:
694 case MSR_MTRRfix4K_C0000:
695 case MSR_MTRRfix4K_C8000:
696 case MSR_MTRRfix4K_D0000:
697 case MSR_MTRRfix4K_D8000:
698 case MSR_MTRRfix4K_E0000:
699 case MSR_MTRRfix4K_E8000:
700 case MSR_MTRRfix4K_F0000:
701 case MSR_MTRRfix4K_F8000:
702 case MSR_MTRRdefType:
703 case MSR_IA32_CR_PAT:
704 return true;
705 case 0x2f8:
706 return true;
707 }
708 return false;
709}
710
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300711static bool valid_pat_type(unsigned t)
712{
713 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
714}
715
716static bool valid_mtrr_type(unsigned t)
717{
718 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
719}
720
721static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
722{
723 int i;
724
725 if (!msr_mtrr_valid(msr))
726 return false;
727
728 if (msr == MSR_IA32_CR_PAT) {
729 for (i = 0; i < 8; i++)
730 if (!valid_pat_type((data >> (i * 8)) & 0xff))
731 return false;
732 return true;
733 } else if (msr == MSR_MTRRdefType) {
734 if (data & ~0xcff)
735 return false;
736 return valid_mtrr_type(data & 0xff);
737 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
738 for (i = 0; i < 8 ; i++)
739 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
740 return false;
741 return true;
742 }
743
744 /* variable MTRRs */
745 return valid_mtrr_type(data & 0xff);
746}
747
Avi Kivity9ba075a2008-05-26 20:06:35 +0300748static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
749{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800750 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
751
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300752 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300753 return 1;
754
Sheng Yang0bed3b52008-10-09 16:01:54 +0800755 if (msr == MSR_MTRRdefType) {
756 vcpu->arch.mtrr_state.def_type = data;
757 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
758 } else if (msr == MSR_MTRRfix64K_00000)
759 p[0] = data;
760 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
761 p[1 + msr - MSR_MTRRfix16K_80000] = data;
762 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
763 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
764 else if (msr == MSR_IA32_CR_PAT)
765 vcpu->arch.pat = data;
766 else { /* Variable MTRRs */
767 int idx, is_mtrr_mask;
768 u64 *pt;
769
770 idx = (msr - 0x200) / 2;
771 is_mtrr_mask = msr - 0x200 - 2 * idx;
772 if (!is_mtrr_mask)
773 pt =
774 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
775 else
776 pt =
777 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
778 *pt = data;
779 }
780
781 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300782 return 0;
783}
Carsten Otte15c4a642007-10-30 18:44:17 +0100784
Huang Ying890ca9a2009-05-11 16:48:15 +0800785static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
786{
787 u64 mcg_cap = vcpu->arch.mcg_cap;
788 unsigned bank_num = mcg_cap & 0xff;
789
790 switch (msr) {
791 case MSR_IA32_MCG_STATUS:
792 vcpu->arch.mcg_status = data;
793 break;
794 case MSR_IA32_MCG_CTL:
795 if (!(mcg_cap & MCG_CTL_P))
796 return 1;
797 if (data != 0 && data != ~(u64)0)
798 return -1;
799 vcpu->arch.mcg_ctl = data;
800 break;
801 default:
802 if (msr >= MSR_IA32_MC0_CTL &&
803 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
804 u32 offset = msr - MSR_IA32_MC0_CTL;
805 /* only 0 or all 1s can be written to IA32_MCi_CTL */
806 if ((offset & 0x3) == 0 &&
807 data != 0 && data != ~(u64)0)
808 return -1;
809 vcpu->arch.mce_banks[offset] = data;
810 break;
811 }
812 return 1;
813 }
814 return 0;
815}
816
Carsten Otte15c4a642007-10-30 18:44:17 +0100817int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
818{
819 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100820 case MSR_EFER:
821 set_efer(vcpu, data);
822 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200823 case MSR_IA32_DEBUGCTLMSR:
824 if (!data) {
825 /* We support the non-activated case already */
826 break;
827 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
828 /* Values other than LBR and BTF are vendor-specific,
829 thus reserved and should throw a #GP */
830 return 1;
831 }
832 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
833 __func__, data);
834 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100835 case MSR_IA32_UCODE_REV:
836 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200837 case MSR_VM_HSAVE_PA:
Carsten Otte15c4a642007-10-30 18:44:17 +0100838 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300839 case 0x200 ... 0x2ff:
840 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100841 case MSR_IA32_APICBASE:
842 kvm_set_apic_base(vcpu, data);
843 break;
844 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800845 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100846 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200847 case MSR_KVM_WALL_CLOCK:
848 vcpu->kvm->arch.wall_clock = data;
849 kvm_write_wall_clock(vcpu->kvm, data);
850 break;
851 case MSR_KVM_SYSTEM_TIME: {
852 if (vcpu->arch.time_page) {
853 kvm_release_page_dirty(vcpu->arch.time_page);
854 vcpu->arch.time_page = NULL;
855 }
856
857 vcpu->arch.time = data;
858
859 /* we verify if the enable bit is set... */
860 if (!(data & 1))
861 break;
862
863 /* ...but clean it before doing the actual write */
864 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
865
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200866 vcpu->arch.time_page =
867 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200868
869 if (is_error_page(vcpu->arch.time_page)) {
870 kvm_release_page_clean(vcpu->arch.time_page);
871 vcpu->arch.time_page = NULL;
872 }
873
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100874 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200875 break;
876 }
Huang Ying890ca9a2009-05-11 16:48:15 +0800877 case MSR_IA32_MCG_CTL:
878 case MSR_IA32_MCG_STATUS:
879 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
880 return set_msr_mce(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100881 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200882 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100883 return 1;
884 }
885 return 0;
886}
887EXPORT_SYMBOL_GPL(kvm_set_msr_common);
888
889
890/*
891 * Reads an msr value (of 'msr_index') into 'pdata'.
892 * Returns 0 on success, non-0 otherwise.
893 * Assumes vcpu_load() was already called.
894 */
895int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
896{
897 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
898}
899
Avi Kivity9ba075a2008-05-26 20:06:35 +0300900static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
901{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800902 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
903
Avi Kivity9ba075a2008-05-26 20:06:35 +0300904 if (!msr_mtrr_valid(msr))
905 return 1;
906
Sheng Yang0bed3b52008-10-09 16:01:54 +0800907 if (msr == MSR_MTRRdefType)
908 *pdata = vcpu->arch.mtrr_state.def_type +
909 (vcpu->arch.mtrr_state.enabled << 10);
910 else if (msr == MSR_MTRRfix64K_00000)
911 *pdata = p[0];
912 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
913 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
914 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
915 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
916 else if (msr == MSR_IA32_CR_PAT)
917 *pdata = vcpu->arch.pat;
918 else { /* Variable MTRRs */
919 int idx, is_mtrr_mask;
920 u64 *pt;
921
922 idx = (msr - 0x200) / 2;
923 is_mtrr_mask = msr - 0x200 - 2 * idx;
924 if (!is_mtrr_mask)
925 pt =
926 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
927 else
928 pt =
929 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
930 *pdata = *pt;
931 }
932
Avi Kivity9ba075a2008-05-26 20:06:35 +0300933 return 0;
934}
935
Huang Ying890ca9a2009-05-11 16:48:15 +0800936static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
937{
938 u64 data;
939 u64 mcg_cap = vcpu->arch.mcg_cap;
940 unsigned bank_num = mcg_cap & 0xff;
941
942 switch (msr) {
943 case MSR_IA32_P5_MC_ADDR:
944 case MSR_IA32_P5_MC_TYPE:
945 data = 0;
946 break;
947 case MSR_IA32_MCG_CAP:
948 data = vcpu->arch.mcg_cap;
949 break;
950 case MSR_IA32_MCG_CTL:
951 if (!(mcg_cap & MCG_CTL_P))
952 return 1;
953 data = vcpu->arch.mcg_ctl;
954 break;
955 case MSR_IA32_MCG_STATUS:
956 data = vcpu->arch.mcg_status;
957 break;
958 default:
959 if (msr >= MSR_IA32_MC0_CTL &&
960 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
961 u32 offset = msr - MSR_IA32_MC0_CTL;
962 data = vcpu->arch.mce_banks[offset];
963 break;
964 }
965 return 1;
966 }
967 *pdata = data;
968 return 0;
969}
970
Carsten Otte15c4a642007-10-30 18:44:17 +0100971int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
972{
973 u64 data;
974
975 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100976 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +0100977 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100978 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200979 case MSR_IA32_DEBUGCTLMSR:
980 case MSR_IA32_LASTBRANCHFROMIP:
981 case MSR_IA32_LASTBRANCHTOIP:
982 case MSR_IA32_LASTINTFROMIP:
983 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +0530984 case MSR_K8_SYSCFG:
985 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200986 case MSR_VM_HSAVE_PA:
Amit Shah7fe29e02009-03-20 12:39:00 +0530987 case MSR_P6_EVNTSEL0:
988 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +0530989 case MSR_K7_EVNTSEL0:
Carsten Otte15c4a642007-10-30 18:44:17 +0100990 data = 0;
991 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300992 case MSR_MTRRcap:
993 data = 0x500 | KVM_NR_VAR_MTRR;
994 break;
995 case 0x200 ... 0x2ff:
996 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +0100997 case 0xcd: /* fsb frequency */
998 data = 3;
999 break;
1000 case MSR_IA32_APICBASE:
1001 data = kvm_get_apic_base(vcpu);
1002 break;
1003 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001004 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001005 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001006 case MSR_IA32_PERF_STATUS:
1007 /* TSC increment by tick */
1008 data = 1000ULL;
1009 /* CPU multiplier */
1010 data |= (((uint64_t)4ULL) << 40);
1011 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001012 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001013 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001014 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001015 case MSR_KVM_WALL_CLOCK:
1016 data = vcpu->kvm->arch.wall_clock;
1017 break;
1018 case MSR_KVM_SYSTEM_TIME:
1019 data = vcpu->arch.time;
1020 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001021 case MSR_IA32_P5_MC_ADDR:
1022 case MSR_IA32_P5_MC_TYPE:
1023 case MSR_IA32_MCG_CAP:
1024 case MSR_IA32_MCG_CTL:
1025 case MSR_IA32_MCG_STATUS:
1026 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1027 return get_msr_mce(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001028 default:
1029 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1030 return 1;
1031 }
1032 *pdata = data;
1033 return 0;
1034}
1035EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1036
Carsten Otte313a3dc2007-10-11 19:16:52 +02001037/*
1038 * Read or write a bunch of msrs. All parameters are kernel addresses.
1039 *
1040 * @return number of msrs set successfully.
1041 */
1042static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1043 struct kvm_msr_entry *entries,
1044 int (*do_msr)(struct kvm_vcpu *vcpu,
1045 unsigned index, u64 *data))
1046{
1047 int i;
1048
1049 vcpu_load(vcpu);
1050
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001051 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001052 for (i = 0; i < msrs->nmsrs; ++i)
1053 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1054 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001055 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001056
1057 vcpu_put(vcpu);
1058
1059 return i;
1060}
1061
1062/*
1063 * Read or write a bunch of msrs. Parameters are user addresses.
1064 *
1065 * @return number of msrs set successfully.
1066 */
1067static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1068 int (*do_msr)(struct kvm_vcpu *vcpu,
1069 unsigned index, u64 *data),
1070 int writeback)
1071{
1072 struct kvm_msrs msrs;
1073 struct kvm_msr_entry *entries;
1074 int r, n;
1075 unsigned size;
1076
1077 r = -EFAULT;
1078 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1079 goto out;
1080
1081 r = -E2BIG;
1082 if (msrs.nmsrs >= MAX_IO_MSRS)
1083 goto out;
1084
1085 r = -ENOMEM;
1086 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1087 entries = vmalloc(size);
1088 if (!entries)
1089 goto out;
1090
1091 r = -EFAULT;
1092 if (copy_from_user(entries, user_msrs->entries, size))
1093 goto out_free;
1094
1095 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1096 if (r < 0)
1097 goto out_free;
1098
1099 r = -EFAULT;
1100 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1101 goto out_free;
1102
1103 r = n;
1104
1105out_free:
1106 vfree(entries);
1107out:
1108 return r;
1109}
1110
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001111int kvm_dev_ioctl_check_extension(long ext)
1112{
1113 int r;
1114
1115 switch (ext) {
1116 case KVM_CAP_IRQCHIP:
1117 case KVM_CAP_HLT:
1118 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001119 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001120 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001121 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001122 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001123 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001124 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001125 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001126 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001127 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001128 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecb2009-05-20 10:30:49 -04001129 case KVM_CAP_IRQFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001130 case KVM_CAP_PIT2:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001131 r = 1;
1132 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001133 case KVM_CAP_COALESCED_MMIO:
1134 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1135 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001136 case KVM_CAP_VAPIC:
1137 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1138 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001139 case KVM_CAP_NR_VCPUS:
1140 r = KVM_MAX_VCPUS;
1141 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001142 case KVM_CAP_NR_MEMSLOTS:
1143 r = KVM_MEMORY_SLOTS;
1144 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001145 case KVM_CAP_PV_MMU:
1146 r = !tdp_enabled;
1147 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001148 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001149 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001150 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001151 case KVM_CAP_MCE:
1152 r = KVM_MAX_MCE_BANKS;
1153 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001154 default:
1155 r = 0;
1156 break;
1157 }
1158 return r;
1159
1160}
1161
Carsten Otte043405e2007-10-10 17:16:19 +02001162long kvm_arch_dev_ioctl(struct file *filp,
1163 unsigned int ioctl, unsigned long arg)
1164{
1165 void __user *argp = (void __user *)arg;
1166 long r;
1167
1168 switch (ioctl) {
1169 case KVM_GET_MSR_INDEX_LIST: {
1170 struct kvm_msr_list __user *user_msr_list = argp;
1171 struct kvm_msr_list msr_list;
1172 unsigned n;
1173
1174 r = -EFAULT;
1175 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1176 goto out;
1177 n = msr_list.nmsrs;
1178 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1179 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1180 goto out;
1181 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001182 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001183 goto out;
1184 r = -EFAULT;
1185 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1186 num_msrs_to_save * sizeof(u32)))
1187 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001188 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001189 &emulated_msrs,
1190 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1191 goto out;
1192 r = 0;
1193 break;
1194 }
Avi Kivity674eea02008-02-11 18:37:23 +02001195 case KVM_GET_SUPPORTED_CPUID: {
1196 struct kvm_cpuid2 __user *cpuid_arg = argp;
1197 struct kvm_cpuid2 cpuid;
1198
1199 r = -EFAULT;
1200 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1201 goto out;
1202 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001203 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001204 if (r)
1205 goto out;
1206
1207 r = -EFAULT;
1208 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1209 goto out;
1210 r = 0;
1211 break;
1212 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001213 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1214 u64 mce_cap;
1215
1216 mce_cap = KVM_MCE_CAP_SUPPORTED;
1217 r = -EFAULT;
1218 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1219 goto out;
1220 r = 0;
1221 break;
1222 }
Carsten Otte043405e2007-10-10 17:16:19 +02001223 default:
1224 r = -EINVAL;
1225 }
1226out:
1227 return r;
1228}
1229
Carsten Otte313a3dc2007-10-11 19:16:52 +02001230void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1231{
1232 kvm_x86_ops->vcpu_load(vcpu, cpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001233 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001234}
1235
1236void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1237{
1238 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001239 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001240}
1241
Dan Kenigsberg07716712007-11-21 17:10:04 +02001242static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001243{
Avi Kivitye286e862009-05-03 18:50:55 +03001244 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001245
Avi Kivitye286e862009-05-03 18:50:55 +03001246 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001247 return efer & EFER_NX;
1248}
1249
1250static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1251{
1252 int i;
1253 struct kvm_cpuid_entry2 *e, *entry;
1254
Carsten Otte313a3dc2007-10-11 19:16:52 +02001255 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001256 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1257 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001258 if (e->function == 0x80000001) {
1259 entry = e;
1260 break;
1261 }
1262 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001263 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001264 entry->edx &= ~(1 << 20);
1265 printk(KERN_INFO "kvm: guest NX capability removed\n");
1266 }
1267}
1268
Dan Kenigsberg07716712007-11-21 17:10:04 +02001269/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001270static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1271 struct kvm_cpuid *cpuid,
1272 struct kvm_cpuid_entry __user *entries)
1273{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001274 int r, i;
1275 struct kvm_cpuid_entry *cpuid_entries;
1276
1277 r = -E2BIG;
1278 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1279 goto out;
1280 r = -ENOMEM;
1281 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1282 if (!cpuid_entries)
1283 goto out;
1284 r = -EFAULT;
1285 if (copy_from_user(cpuid_entries, entries,
1286 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1287 goto out_free;
1288 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001289 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1290 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1291 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1292 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1293 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1294 vcpu->arch.cpuid_entries[i].index = 0;
1295 vcpu->arch.cpuid_entries[i].flags = 0;
1296 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1297 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1298 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001299 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001300 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001301 cpuid_fix_nx_cap(vcpu);
1302 r = 0;
1303
1304out_free:
1305 vfree(cpuid_entries);
1306out:
1307 return r;
1308}
1309
1310static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001311 struct kvm_cpuid2 *cpuid,
1312 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001313{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001314 int r;
1315
1316 r = -E2BIG;
1317 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1318 goto out;
1319 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001320 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001321 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001322 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001324 return 0;
1325
1326out:
1327 return r;
1328}
1329
Dan Kenigsberg07716712007-11-21 17:10:04 +02001330static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001331 struct kvm_cpuid2 *cpuid,
1332 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001333{
1334 int r;
1335
1336 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001337 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001338 goto out;
1339 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001340 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001341 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001342 goto out;
1343 return 0;
1344
1345out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001346 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001347 return r;
1348}
1349
Dan Kenigsberg07716712007-11-21 17:10:04 +02001350static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001351 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001352{
1353 entry->function = function;
1354 entry->index = index;
1355 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001356 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001357 entry->flags = 0;
1358}
1359
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001360#define F(x) bit(X86_FEATURE_##x)
1361
Dan Kenigsberg07716712007-11-21 17:10:04 +02001362static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1363 u32 index, int *nent, int maxnent)
1364{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001365 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001366#ifdef CONFIG_X86_64
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001367 unsigned f_lm = F(LM);
1368#else
1369 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001370#endif
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001371
1372 /* cpuid 1.edx */
1373 const u32 kvm_supported_word0_x86_features =
1374 F(FPU) | F(VME) | F(DE) | F(PSE) |
1375 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1376 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1377 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1378 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1379 0 /* Reserved, DS, ACPI */ | F(MMX) |
1380 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1381 0 /* HTT, TM, Reserved, PBE */;
1382 /* cpuid 0x80000001.edx */
1383 const u32 kvm_supported_word1_x86_features =
1384 F(FPU) | F(VME) | F(DE) | F(PSE) |
1385 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1386 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1387 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1388 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1389 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1390 F(FXSR) | F(FXSR_OPT) | 0 /* GBPAGES */ | 0 /* RDTSCP */ |
1391 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1392 /* cpuid 1.ecx */
1393 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001394 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1395 0 /* DS-CPL, VMX, SMX, EST */ |
1396 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1397 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1398 0 /* Reserved, DCA */ | F(XMM4_1) |
1399 F(XMM4_2) | 0 /* x2APIC */ | F(MOVBE) | F(POPCNT) |
1400 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001401 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001402 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001403 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1404 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1405 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1406 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001407
Amit Shah19355472009-01-14 16:56:00 +00001408 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001409 get_cpu();
1410 do_cpuid_1_ent(entry, function, index);
1411 ++*nent;
1412
1413 switch (function) {
1414 case 0:
1415 entry->eax = min(entry->eax, (u32)0xb);
1416 break;
1417 case 1:
1418 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001419 entry->ecx &= kvm_supported_word4_x86_features;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001420 break;
1421 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1422 * may return different values. This forces us to get_cpu() before
1423 * issuing the first command, and also to emulate this annoying behavior
1424 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1425 case 2: {
1426 int t, times = entry->eax & 0xff;
1427
1428 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001429 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001430 for (t = 1; t < times && *nent < maxnent; ++t) {
1431 do_cpuid_1_ent(&entry[t], function, 0);
1432 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1433 ++*nent;
1434 }
1435 break;
1436 }
1437 /* function 4 and 0xb have additional index. */
1438 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001439 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001440
1441 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1442 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001443 for (i = 1; *nent < maxnent; ++i) {
1444 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001445 if (!cache_type)
1446 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001447 do_cpuid_1_ent(&entry[i], function, i);
1448 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001449 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1450 ++*nent;
1451 }
1452 break;
1453 }
1454 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001455 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001456
1457 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1458 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001459 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001460 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001461 if (!level_type)
1462 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001463 do_cpuid_1_ent(&entry[i], function, i);
1464 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001465 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1466 ++*nent;
1467 }
1468 break;
1469 }
1470 case 0x80000000:
1471 entry->eax = min(entry->eax, 0x8000001a);
1472 break;
1473 case 0x80000001:
1474 entry->edx &= kvm_supported_word1_x86_features;
1475 entry->ecx &= kvm_supported_word6_x86_features;
1476 break;
1477 }
1478 put_cpu();
1479}
1480
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001481#undef F
1482
Avi Kivity674eea02008-02-11 18:37:23 +02001483static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001484 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001485{
1486 struct kvm_cpuid_entry2 *cpuid_entries;
1487 int limit, nent = 0, r = -E2BIG;
1488 u32 func;
1489
1490 if (cpuid->nent < 1)
1491 goto out;
1492 r = -ENOMEM;
1493 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1494 if (!cpuid_entries)
1495 goto out;
1496
1497 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1498 limit = cpuid_entries[0].eax;
1499 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1500 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001501 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001502 r = -E2BIG;
1503 if (nent >= cpuid->nent)
1504 goto out_free;
1505
1506 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1507 limit = cpuid_entries[nent - 1].eax;
1508 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1509 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001510 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001511 r = -E2BIG;
1512 if (nent >= cpuid->nent)
1513 goto out_free;
1514
Dan Kenigsberg07716712007-11-21 17:10:04 +02001515 r = -EFAULT;
1516 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001517 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001518 goto out_free;
1519 cpuid->nent = nent;
1520 r = 0;
1521
1522out_free:
1523 vfree(cpuid_entries);
1524out:
1525 return r;
1526}
1527
Carsten Otte313a3dc2007-10-11 19:16:52 +02001528static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1529 struct kvm_lapic_state *s)
1530{
1531 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001532 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001533 vcpu_put(vcpu);
1534
1535 return 0;
1536}
1537
1538static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1539 struct kvm_lapic_state *s)
1540{
1541 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001542 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001543 kvm_apic_post_state_restore(vcpu);
1544 vcpu_put(vcpu);
1545
1546 return 0;
1547}
1548
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001549static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1550 struct kvm_interrupt *irq)
1551{
1552 if (irq->irq < 0 || irq->irq >= 256)
1553 return -EINVAL;
1554 if (irqchip_in_kernel(vcpu->kvm))
1555 return -ENXIO;
1556 vcpu_load(vcpu);
1557
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001558 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001559
1560 vcpu_put(vcpu);
1561
1562 return 0;
1563}
1564
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001565static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1566{
1567 vcpu_load(vcpu);
1568 kvm_inject_nmi(vcpu);
1569 vcpu_put(vcpu);
1570
1571 return 0;
1572}
1573
Avi Kivityb209749f2007-10-22 16:50:39 +02001574static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1575 struct kvm_tpr_access_ctl *tac)
1576{
1577 if (tac->flags)
1578 return -EINVAL;
1579 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1580 return 0;
1581}
1582
Huang Ying890ca9a2009-05-11 16:48:15 +08001583static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1584 u64 mcg_cap)
1585{
1586 int r;
1587 unsigned bank_num = mcg_cap & 0xff, bank;
1588
1589 r = -EINVAL;
1590 if (!bank_num)
1591 goto out;
1592 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1593 goto out;
1594 r = 0;
1595 vcpu->arch.mcg_cap = mcg_cap;
1596 /* Init IA32_MCG_CTL to all 1s */
1597 if (mcg_cap & MCG_CTL_P)
1598 vcpu->arch.mcg_ctl = ~(u64)0;
1599 /* Init IA32_MCi_CTL to all 1s */
1600 for (bank = 0; bank < bank_num; bank++)
1601 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1602out:
1603 return r;
1604}
1605
1606static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1607 struct kvm_x86_mce *mce)
1608{
1609 u64 mcg_cap = vcpu->arch.mcg_cap;
1610 unsigned bank_num = mcg_cap & 0xff;
1611 u64 *banks = vcpu->arch.mce_banks;
1612
1613 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1614 return -EINVAL;
1615 /*
1616 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1617 * reporting is disabled
1618 */
1619 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1620 vcpu->arch.mcg_ctl != ~(u64)0)
1621 return 0;
1622 banks += 4 * mce->bank;
1623 /*
1624 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1625 * reporting is disabled for the bank
1626 */
1627 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1628 return 0;
1629 if (mce->status & MCI_STATUS_UC) {
1630 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1631 !(vcpu->arch.cr4 & X86_CR4_MCE)) {
1632 printk(KERN_DEBUG "kvm: set_mce: "
1633 "injects mce exception while "
1634 "previous one is in progress!\n");
1635 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1636 return 0;
1637 }
1638 if (banks[1] & MCI_STATUS_VAL)
1639 mce->status |= MCI_STATUS_OVER;
1640 banks[2] = mce->addr;
1641 banks[3] = mce->misc;
1642 vcpu->arch.mcg_status = mce->mcg_status;
1643 banks[1] = mce->status;
1644 kvm_queue_exception(vcpu, MC_VECTOR);
1645 } else if (!(banks[1] & MCI_STATUS_VAL)
1646 || !(banks[1] & MCI_STATUS_UC)) {
1647 if (banks[1] & MCI_STATUS_VAL)
1648 mce->status |= MCI_STATUS_OVER;
1649 banks[2] = mce->addr;
1650 banks[3] = mce->misc;
1651 banks[1] = mce->status;
1652 } else
1653 banks[1] |= MCI_STATUS_OVER;
1654 return 0;
1655}
1656
Carsten Otte313a3dc2007-10-11 19:16:52 +02001657long kvm_arch_vcpu_ioctl(struct file *filp,
1658 unsigned int ioctl, unsigned long arg)
1659{
1660 struct kvm_vcpu *vcpu = filp->private_data;
1661 void __user *argp = (void __user *)arg;
1662 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001663 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001664
1665 switch (ioctl) {
1666 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001667 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001668
Dave Hansenb772ff32008-08-11 10:01:47 -07001669 r = -ENOMEM;
1670 if (!lapic)
1671 goto out;
1672 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001673 if (r)
1674 goto out;
1675 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001676 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001677 goto out;
1678 r = 0;
1679 break;
1680 }
1681 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001682 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1683 r = -ENOMEM;
1684 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001685 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001686 r = -EFAULT;
1687 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1688 goto out;
1689 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001690 if (r)
1691 goto out;
1692 r = 0;
1693 break;
1694 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001695 case KVM_INTERRUPT: {
1696 struct kvm_interrupt irq;
1697
1698 r = -EFAULT;
1699 if (copy_from_user(&irq, argp, sizeof irq))
1700 goto out;
1701 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1702 if (r)
1703 goto out;
1704 r = 0;
1705 break;
1706 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001707 case KVM_NMI: {
1708 r = kvm_vcpu_ioctl_nmi(vcpu);
1709 if (r)
1710 goto out;
1711 r = 0;
1712 break;
1713 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001714 case KVM_SET_CPUID: {
1715 struct kvm_cpuid __user *cpuid_arg = argp;
1716 struct kvm_cpuid cpuid;
1717
1718 r = -EFAULT;
1719 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1720 goto out;
1721 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1722 if (r)
1723 goto out;
1724 break;
1725 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001726 case KVM_SET_CPUID2: {
1727 struct kvm_cpuid2 __user *cpuid_arg = argp;
1728 struct kvm_cpuid2 cpuid;
1729
1730 r = -EFAULT;
1731 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1732 goto out;
1733 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001734 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001735 if (r)
1736 goto out;
1737 break;
1738 }
1739 case KVM_GET_CPUID2: {
1740 struct kvm_cpuid2 __user *cpuid_arg = argp;
1741 struct kvm_cpuid2 cpuid;
1742
1743 r = -EFAULT;
1744 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1745 goto out;
1746 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001747 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001748 if (r)
1749 goto out;
1750 r = -EFAULT;
1751 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1752 goto out;
1753 r = 0;
1754 break;
1755 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001756 case KVM_GET_MSRS:
1757 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1758 break;
1759 case KVM_SET_MSRS:
1760 r = msr_io(vcpu, argp, do_set_msr, 0);
1761 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001762 case KVM_TPR_ACCESS_REPORTING: {
1763 struct kvm_tpr_access_ctl tac;
1764
1765 r = -EFAULT;
1766 if (copy_from_user(&tac, argp, sizeof tac))
1767 goto out;
1768 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1769 if (r)
1770 goto out;
1771 r = -EFAULT;
1772 if (copy_to_user(argp, &tac, sizeof tac))
1773 goto out;
1774 r = 0;
1775 break;
1776 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001777 case KVM_SET_VAPIC_ADDR: {
1778 struct kvm_vapic_addr va;
1779
1780 r = -EINVAL;
1781 if (!irqchip_in_kernel(vcpu->kvm))
1782 goto out;
1783 r = -EFAULT;
1784 if (copy_from_user(&va, argp, sizeof va))
1785 goto out;
1786 r = 0;
1787 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1788 break;
1789 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001790 case KVM_X86_SETUP_MCE: {
1791 u64 mcg_cap;
1792
1793 r = -EFAULT;
1794 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
1795 goto out;
1796 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
1797 break;
1798 }
1799 case KVM_X86_SET_MCE: {
1800 struct kvm_x86_mce mce;
1801
1802 r = -EFAULT;
1803 if (copy_from_user(&mce, argp, sizeof mce))
1804 goto out;
1805 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
1806 break;
1807 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001808 default:
1809 r = -EINVAL;
1810 }
1811out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08001812 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001813 return r;
1814}
1815
Carsten Otte1fe779f2007-10-29 16:08:35 +01001816static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1817{
1818 int ret;
1819
1820 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1821 return -1;
1822 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1823 return ret;
1824}
1825
1826static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1827 u32 kvm_nr_mmu_pages)
1828{
1829 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1830 return -EINVAL;
1831
Izik Eidus72dc67a2008-02-10 18:04:15 +02001832 down_write(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001833 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001834
1835 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001836 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001837
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001838 spin_unlock(&kvm->mmu_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001839 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001840 return 0;
1841}
1842
1843static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1844{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001845 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001846}
1847
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001848gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1849{
1850 int i;
1851 struct kvm_mem_alias *alias;
1852
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001853 for (i = 0; i < kvm->arch.naliases; ++i) {
1854 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001855 if (gfn >= alias->base_gfn
1856 && gfn < alias->base_gfn + alias->npages)
1857 return alias->target_gfn + gfn - alias->base_gfn;
1858 }
1859 return gfn;
1860}
1861
Carsten Otte1fe779f2007-10-29 16:08:35 +01001862/*
1863 * Set a new alias region. Aliases map a portion of physical memory into
1864 * another portion. This is useful for memory windows, for example the PC
1865 * VGA region.
1866 */
1867static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1868 struct kvm_memory_alias *alias)
1869{
1870 int r, n;
1871 struct kvm_mem_alias *p;
1872
1873 r = -EINVAL;
1874 /* General sanity checks */
1875 if (alias->memory_size & (PAGE_SIZE - 1))
1876 goto out;
1877 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1878 goto out;
1879 if (alias->slot >= KVM_ALIAS_SLOTS)
1880 goto out;
1881 if (alias->guest_phys_addr + alias->memory_size
1882 < alias->guest_phys_addr)
1883 goto out;
1884 if (alias->target_phys_addr + alias->memory_size
1885 < alias->target_phys_addr)
1886 goto out;
1887
Izik Eidus72dc67a2008-02-10 18:04:15 +02001888 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001889 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001890
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001891 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001892 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1893 p->npages = alias->memory_size >> PAGE_SHIFT;
1894 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1895
1896 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001897 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001898 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001899 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001900
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001901 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001902 kvm_mmu_zap_all(kvm);
1903
Izik Eidus72dc67a2008-02-10 18:04:15 +02001904 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001905
1906 return 0;
1907
1908out:
1909 return r;
1910}
1911
1912static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1913{
1914 int r;
1915
1916 r = 0;
1917 switch (chip->chip_id) {
1918 case KVM_IRQCHIP_PIC_MASTER:
1919 memcpy(&chip->chip.pic,
1920 &pic_irqchip(kvm)->pics[0],
1921 sizeof(struct kvm_pic_state));
1922 break;
1923 case KVM_IRQCHIP_PIC_SLAVE:
1924 memcpy(&chip->chip.pic,
1925 &pic_irqchip(kvm)->pics[1],
1926 sizeof(struct kvm_pic_state));
1927 break;
1928 case KVM_IRQCHIP_IOAPIC:
1929 memcpy(&chip->chip.ioapic,
1930 ioapic_irqchip(kvm),
1931 sizeof(struct kvm_ioapic_state));
1932 break;
1933 default:
1934 r = -EINVAL;
1935 break;
1936 }
1937 return r;
1938}
1939
1940static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1941{
1942 int r;
1943
1944 r = 0;
1945 switch (chip->chip_id) {
1946 case KVM_IRQCHIP_PIC_MASTER:
1947 memcpy(&pic_irqchip(kvm)->pics[0],
1948 &chip->chip.pic,
1949 sizeof(struct kvm_pic_state));
1950 break;
1951 case KVM_IRQCHIP_PIC_SLAVE:
1952 memcpy(&pic_irqchip(kvm)->pics[1],
1953 &chip->chip.pic,
1954 sizeof(struct kvm_pic_state));
1955 break;
1956 case KVM_IRQCHIP_IOAPIC:
1957 memcpy(ioapic_irqchip(kvm),
1958 &chip->chip.ioapic,
1959 sizeof(struct kvm_ioapic_state));
1960 break;
1961 default:
1962 r = -EINVAL;
1963 break;
1964 }
1965 kvm_pic_update_irq(pic_irqchip(kvm));
1966 return r;
1967}
1968
Sheng Yange0f63cb2008-03-04 00:50:59 +08001969static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1970{
1971 int r = 0;
1972
1973 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1974 return r;
1975}
1976
1977static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1978{
1979 int r = 0;
1980
1981 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1982 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1983 return r;
1984}
1985
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001986static int kvm_vm_ioctl_reinject(struct kvm *kvm,
1987 struct kvm_reinject_control *control)
1988{
1989 if (!kvm->arch.vpit)
1990 return -ENXIO;
1991 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
1992 return 0;
1993}
1994
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001995/*
1996 * Get (and clear) the dirty memory log for a memory slot.
1997 */
1998int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1999 struct kvm_dirty_log *log)
2000{
2001 int r;
2002 int n;
2003 struct kvm_memory_slot *memslot;
2004 int is_dirty = 0;
2005
Izik Eidus72dc67a2008-02-10 18:04:15 +02002006 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002007
2008 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2009 if (r)
2010 goto out;
2011
2012 /* If nothing is dirty, don't bother messing with page tables. */
2013 if (is_dirty) {
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002014 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002015 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002016 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002017 kvm_flush_remote_tlbs(kvm);
2018 memslot = &kvm->memslots[log->slot];
2019 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2020 memset(memslot->dirty_bitmap, 0, n);
2021 }
2022 r = 0;
2023out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002024 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002025 return r;
2026}
2027
Carsten Otte1fe779f2007-10-29 16:08:35 +01002028long kvm_arch_vm_ioctl(struct file *filp,
2029 unsigned int ioctl, unsigned long arg)
2030{
2031 struct kvm *kvm = filp->private_data;
2032 void __user *argp = (void __user *)arg;
2033 int r = -EINVAL;
Dave Hansenf0d66272008-08-11 10:01:45 -07002034 /*
2035 * This union makes it completely explicit to gcc-3.x
2036 * that these two variables' stack usage should be
2037 * combined, not added together.
2038 */
2039 union {
2040 struct kvm_pit_state ps;
2041 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002042 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002043 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002044
2045 switch (ioctl) {
2046 case KVM_SET_TSS_ADDR:
2047 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2048 if (r < 0)
2049 goto out;
2050 break;
2051 case KVM_SET_MEMORY_REGION: {
2052 struct kvm_memory_region kvm_mem;
2053 struct kvm_userspace_memory_region kvm_userspace_mem;
2054
2055 r = -EFAULT;
2056 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2057 goto out;
2058 kvm_userspace_mem.slot = kvm_mem.slot;
2059 kvm_userspace_mem.flags = kvm_mem.flags;
2060 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2061 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2062 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2063 if (r)
2064 goto out;
2065 break;
2066 }
2067 case KVM_SET_NR_MMU_PAGES:
2068 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2069 if (r)
2070 goto out;
2071 break;
2072 case KVM_GET_NR_MMU_PAGES:
2073 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2074 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002075 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002076 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002077 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002078 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002079 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002080 if (r)
2081 goto out;
2082 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002083 case KVM_CREATE_IRQCHIP:
2084 r = -ENOMEM;
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08002085 kvm->arch.vpic = kvm_create_pic(kvm);
2086 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002087 r = kvm_ioapic_init(kvm);
2088 if (r) {
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08002089 kfree(kvm->arch.vpic);
2090 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002091 goto out;
2092 }
2093 } else
2094 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +02002095 r = kvm_setup_default_irq_routing(kvm);
2096 if (r) {
2097 kfree(kvm->arch.vpic);
2098 kfree(kvm->arch.vioapic);
2099 goto out;
2100 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002101 break;
Sheng Yang78376992008-01-28 05:10:22 +08002102 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002103 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2104 goto create_pit;
2105 case KVM_CREATE_PIT2:
2106 r = -EFAULT;
2107 if (copy_from_user(&u.pit_config, argp,
2108 sizeof(struct kvm_pit_config)))
2109 goto out;
2110 create_pit:
Avi Kivity269e05e2009-01-05 15:21:42 +02002111 mutex_lock(&kvm->lock);
2112 r = -EEXIST;
2113 if (kvm->arch.vpit)
2114 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002115 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002116 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002117 if (kvm->arch.vpit)
2118 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002119 create_pit_unlock:
2120 mutex_unlock(&kvm->lock);
Sheng Yang78376992008-01-28 05:10:22 +08002121 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002122 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002123 case KVM_IRQ_LINE: {
2124 struct kvm_irq_level irq_event;
2125
2126 r = -EFAULT;
2127 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2128 goto out;
2129 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002130 __s32 status;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002131 mutex_lock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +02002132 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2133 irq_event.irq, irq_event.level);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002134 mutex_unlock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +02002135 if (ioctl == KVM_IRQ_LINE_STATUS) {
2136 irq_event.status = status;
2137 if (copy_to_user(argp, &irq_event,
2138 sizeof irq_event))
2139 goto out;
2140 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002141 r = 0;
2142 }
2143 break;
2144 }
2145 case KVM_GET_IRQCHIP: {
2146 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002147 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002148
Dave Hansenf0d66272008-08-11 10:01:45 -07002149 r = -ENOMEM;
2150 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002151 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002152 r = -EFAULT;
2153 if (copy_from_user(chip, argp, sizeof *chip))
2154 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002155 r = -ENXIO;
2156 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002157 goto get_irqchip_out;
2158 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2159 if (r)
2160 goto get_irqchip_out;
2161 r = -EFAULT;
2162 if (copy_to_user(argp, chip, sizeof *chip))
2163 goto get_irqchip_out;
2164 r = 0;
2165 get_irqchip_out:
2166 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002167 if (r)
2168 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002169 break;
2170 }
2171 case KVM_SET_IRQCHIP: {
2172 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002173 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002174
Dave Hansenf0d66272008-08-11 10:01:45 -07002175 r = -ENOMEM;
2176 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002177 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002178 r = -EFAULT;
2179 if (copy_from_user(chip, argp, sizeof *chip))
2180 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002181 r = -ENXIO;
2182 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002183 goto set_irqchip_out;
2184 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2185 if (r)
2186 goto set_irqchip_out;
2187 r = 0;
2188 set_irqchip_out:
2189 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002190 if (r)
2191 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002192 break;
2193 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002194 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002195 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002196 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002197 goto out;
2198 r = -ENXIO;
2199 if (!kvm->arch.vpit)
2200 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002201 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002202 if (r)
2203 goto out;
2204 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002205 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002206 goto out;
2207 r = 0;
2208 break;
2209 }
2210 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002211 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002212 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002213 goto out;
2214 r = -ENXIO;
2215 if (!kvm->arch.vpit)
2216 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002217 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002218 if (r)
2219 goto out;
2220 r = 0;
2221 break;
2222 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002223 case KVM_REINJECT_CONTROL: {
2224 struct kvm_reinject_control control;
2225 r = -EFAULT;
2226 if (copy_from_user(&control, argp, sizeof(control)))
2227 goto out;
2228 r = kvm_vm_ioctl_reinject(kvm, &control);
2229 if (r)
2230 goto out;
2231 r = 0;
2232 break;
2233 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002234 default:
2235 ;
2236 }
2237out:
2238 return r;
2239}
2240
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002241static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002242{
2243 u32 dummy[2];
2244 unsigned i, j;
2245
2246 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2247 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2248 continue;
2249 if (j < i)
2250 msrs_to_save[j] = msrs_to_save[i];
2251 j++;
2252 }
2253 num_msrs_to_save = j;
2254}
2255
Carsten Ottebbd9b642007-10-30 18:44:21 +01002256/*
2257 * Only apic need an MMIO device hook, so shortcut now..
2258 */
2259static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002260 gpa_t addr, int len,
2261 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002262{
2263 struct kvm_io_device *dev;
2264
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002265 if (vcpu->arch.apic) {
2266 dev = &vcpu->arch.apic->dev;
Gregory Haskinsd76685c2009-06-01 12:54:50 -04002267 if (kvm_iodevice_in_range(dev, addr, len, is_write))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002268 return dev;
2269 }
2270 return NULL;
2271}
2272
2273
2274static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002275 gpa_t addr, int len,
2276 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002277{
2278 struct kvm_io_device *dev;
2279
Laurent Vivier92760492008-05-30 16:05:53 +02002280 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002281 if (dev == NULL)
Laurent Vivier92760492008-05-30 16:05:53 +02002282 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
2283 is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002284 return dev;
2285}
2286
Hannes Edercded19f2009-02-21 02:19:13 +01002287static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2288 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002289{
2290 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002291 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002292
2293 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002294 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002295 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02002296 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002297 int ret;
2298
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002299 if (gpa == UNMAPPED_GVA) {
2300 r = X86EMUL_PROPAGATE_FAULT;
2301 goto out;
2302 }
Izik Eidus77c20022008-12-29 01:42:19 +02002303 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002304 if (ret < 0) {
2305 r = X86EMUL_UNHANDLEABLE;
2306 goto out;
2307 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002308
Izik Eidus77c20022008-12-29 01:42:19 +02002309 bytes -= toread;
2310 data += toread;
2311 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002312 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002313out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002314 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002315}
Izik Eidus77c20022008-12-29 01:42:19 +02002316
Hannes Edercded19f2009-02-21 02:19:13 +01002317static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2318 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02002319{
2320 void *data = val;
2321 int r = X86EMUL_CONTINUE;
2322
2323 while (bytes) {
2324 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2325 unsigned offset = addr & (PAGE_SIZE-1);
2326 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2327 int ret;
2328
2329 if (gpa == UNMAPPED_GVA) {
2330 r = X86EMUL_PROPAGATE_FAULT;
2331 goto out;
2332 }
2333 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2334 if (ret < 0) {
2335 r = X86EMUL_UNHANDLEABLE;
2336 goto out;
2337 }
2338
2339 bytes -= towrite;
2340 data += towrite;
2341 addr += towrite;
2342 }
2343out:
2344 return r;
2345}
2346
Carsten Ottebbd9b642007-10-30 18:44:21 +01002347
Carsten Ottebbd9b642007-10-30 18:44:21 +01002348static int emulator_read_emulated(unsigned long addr,
2349 void *val,
2350 unsigned int bytes,
2351 struct kvm_vcpu *vcpu)
2352{
2353 struct kvm_io_device *mmio_dev;
2354 gpa_t gpa;
2355
2356 if (vcpu->mmio_read_completed) {
2357 memcpy(val, vcpu->mmio_data, bytes);
2358 vcpu->mmio_read_completed = 0;
2359 return X86EMUL_CONTINUE;
2360 }
2361
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002362 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002363
2364 /* For APIC access vmexit */
2365 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2366 goto mmio;
2367
Izik Eidus77c20022008-12-29 01:42:19 +02002368 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2369 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002370 return X86EMUL_CONTINUE;
2371 if (gpa == UNMAPPED_GVA)
2372 return X86EMUL_PROPAGATE_FAULT;
2373
2374mmio:
2375 /*
2376 * Is this MMIO handled locally?
2377 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002378 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002379 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002380 if (mmio_dev) {
2381 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002382 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002383 return X86EMUL_CONTINUE;
2384 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002385 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002386
2387 vcpu->mmio_needed = 1;
2388 vcpu->mmio_phys_addr = gpa;
2389 vcpu->mmio_size = bytes;
2390 vcpu->mmio_is_write = 0;
2391
2392 return X86EMUL_UNHANDLEABLE;
2393}
2394
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002395int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002396 const void *val, int bytes)
2397{
2398 int ret;
2399
2400 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2401 if (ret < 0)
2402 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002403 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002404 return 1;
2405}
2406
Carsten Ottebbd9b642007-10-30 18:44:21 +01002407static int emulator_write_emulated_onepage(unsigned long addr,
2408 const void *val,
2409 unsigned int bytes,
2410 struct kvm_vcpu *vcpu)
2411{
2412 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002413 gpa_t gpa;
2414
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002415 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002416
2417 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002418 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002419 return X86EMUL_PROPAGATE_FAULT;
2420 }
2421
2422 /* For APIC access vmexit */
2423 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2424 goto mmio;
2425
2426 if (emulator_write_phys(vcpu, gpa, val, bytes))
2427 return X86EMUL_CONTINUE;
2428
2429mmio:
2430 /*
2431 * Is this MMIO handled locally?
2432 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002433 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02002434 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002435 if (mmio_dev) {
2436 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002437 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002438 return X86EMUL_CONTINUE;
2439 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002440 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002441
2442 vcpu->mmio_needed = 1;
2443 vcpu->mmio_phys_addr = gpa;
2444 vcpu->mmio_size = bytes;
2445 vcpu->mmio_is_write = 1;
2446 memcpy(vcpu->mmio_data, val, bytes);
2447
2448 return X86EMUL_CONTINUE;
2449}
2450
2451int emulator_write_emulated(unsigned long addr,
2452 const void *val,
2453 unsigned int bytes,
2454 struct kvm_vcpu *vcpu)
2455{
2456 /* Crossing a page boundary? */
2457 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2458 int rc, now;
2459
2460 now = -addr & ~PAGE_MASK;
2461 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2462 if (rc != X86EMUL_CONTINUE)
2463 return rc;
2464 addr += now;
2465 val += now;
2466 bytes -= now;
2467 }
2468 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2469}
2470EXPORT_SYMBOL_GPL(emulator_write_emulated);
2471
2472static int emulator_cmpxchg_emulated(unsigned long addr,
2473 const void *old,
2474 const void *new,
2475 unsigned int bytes,
2476 struct kvm_vcpu *vcpu)
2477{
2478 static int reported;
2479
2480 if (!reported) {
2481 reported = 1;
2482 printk(KERN_WARNING "kvm: emulating exchange as write\n");
2483 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002484#ifndef CONFIG_X86_64
2485 /* guests cmpxchg8b have to be emulated atomically */
2486 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002487 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002488 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002489 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002490 u64 val;
2491
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002492 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2493
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002494 if (gpa == UNMAPPED_GVA ||
2495 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2496 goto emul_write;
2497
2498 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2499 goto emul_write;
2500
2501 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002502
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002503 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002504
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002505 kaddr = kmap_atomic(page, KM_USER0);
2506 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2507 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002508 kvm_release_page_dirty(page);
2509 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002510emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002511#endif
2512
Carsten Ottebbd9b642007-10-30 18:44:21 +01002513 return emulator_write_emulated(addr, new, bytes, vcpu);
2514}
2515
2516static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2517{
2518 return kvm_x86_ops->get_segment_base(vcpu, seg);
2519}
2520
2521int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2522{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002523 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002524 return X86EMUL_CONTINUE;
2525}
2526
2527int emulate_clts(struct kvm_vcpu *vcpu)
2528{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002529 KVMTRACE_0D(CLTS, vcpu, handler);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002530 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002531 return X86EMUL_CONTINUE;
2532}
2533
2534int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2535{
2536 struct kvm_vcpu *vcpu = ctxt->vcpu;
2537
2538 switch (dr) {
2539 case 0 ... 3:
2540 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2541 return X86EMUL_CONTINUE;
2542 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002543 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002544 return X86EMUL_UNHANDLEABLE;
2545 }
2546}
2547
2548int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2549{
2550 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2551 int exception;
2552
2553 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2554 if (exception) {
2555 /* FIXME: better handling */
2556 return X86EMUL_UNHANDLEABLE;
2557 }
2558 return X86EMUL_CONTINUE;
2559}
2560
2561void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2562{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002563 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002564 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002565 unsigned long rip_linear;
2566
Avi Kivityf76c7102008-06-13 22:45:42 +03002567 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002568 return;
2569
Glauber Costa25be4602008-06-10 10:46:53 -03002570 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2571
Izik Eidus77c20022008-12-29 01:42:19 +02002572 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002573
2574 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2575 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002576}
2577EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2578
Harvey Harrison14af3f32008-02-19 10:25:50 -08002579static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02002580 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002581 .read_emulated = emulator_read_emulated,
2582 .write_emulated = emulator_write_emulated,
2583 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2584};
2585
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002586static void cache_all_regs(struct kvm_vcpu *vcpu)
2587{
2588 kvm_register_read(vcpu, VCPU_REGS_RAX);
2589 kvm_register_read(vcpu, VCPU_REGS_RSP);
2590 kvm_register_read(vcpu, VCPU_REGS_RIP);
2591 vcpu->arch.regs_dirty = ~0;
2592}
2593
Carsten Ottebbd9b642007-10-30 18:44:21 +01002594int emulate_instruction(struct kvm_vcpu *vcpu,
2595 struct kvm_run *run,
2596 unsigned long cr2,
2597 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002598 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002599{
Glauber Costa310b5d32009-05-12 16:21:06 -04002600 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08002601 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002602
Avi Kivity26eef702008-07-03 14:59:22 +03002603 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002604 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002605 /*
2606 * TODO: fix x86_emulate.c to use guest_read/write_register
2607 * instead of direct ->regs accesses, can save hundred cycles
2608 * on Intel for instructions that don't read/change RSP, for
2609 * for example.
2610 */
2611 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002612
2613 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002614 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002615
Sheng Yang571008d2008-01-02 14:49:22 +08002616 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002617 int cs_db, cs_l;
2618 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2619
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002620 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2621 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2622 vcpu->arch.emulate_ctxt.mode =
2623 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002624 ? X86EMUL_MODE_REAL : cs_l
2625 ? X86EMUL_MODE_PROT64 : cs_db
2626 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2627
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002628 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002629
2630 /* Reject the instructions other than VMCALL/VMMCALL when
2631 * try to emulate invalid opcode */
2632 c = &vcpu->arch.emulate_ctxt.decode;
2633 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2634 (!(c->twobyte && c->b == 0x01 &&
2635 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2636 c->modrm_mod == 3 && c->modrm_rm == 1)))
2637 return EMULATE_FAIL;
2638
Avi Kivityf2b57562007-11-18 15:17:51 +02002639 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002640 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002641 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002642 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2643 return EMULATE_DONE;
2644 return EMULATE_FAIL;
2645 }
2646 }
2647
Gleb Natapovba8afb62009-04-12 13:36:57 +03002648 if (emulation_type & EMULTYPE_SKIP) {
2649 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
2650 return EMULATE_DONE;
2651 }
2652
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002653 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04002654 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
2655
2656 if (r == 0)
2657 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002658
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002659 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002660 return EMULATE_DO_MMIO;
2661
2662 if ((r || vcpu->mmio_is_write) && run) {
2663 run->exit_reason = KVM_EXIT_MMIO;
2664 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2665 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2666 run->mmio.len = vcpu->mmio_size;
2667 run->mmio.is_write = vcpu->mmio_is_write;
2668 }
2669
2670 if (r) {
2671 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2672 return EMULATE_DONE;
2673 if (!vcpu->mmio_needed) {
2674 kvm_report_emulation_failure(vcpu, "mmio");
2675 return EMULATE_FAIL;
2676 }
2677 return EMULATE_DO_MMIO;
2678 }
2679
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002680 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002681
2682 if (vcpu->mmio_is_write) {
2683 vcpu->mmio_needed = 0;
2684 return EMULATE_DO_MMIO;
2685 }
2686
2687 return EMULATE_DONE;
2688}
2689EXPORT_SYMBOL_GPL(emulate_instruction);
2690
Carsten Ottede7d7892007-10-30 18:44:25 +01002691static int pio_copy_data(struct kvm_vcpu *vcpu)
2692{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002693 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02002694 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01002695 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02002696 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002697
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002698 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2699 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02002700 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01002701 else
Izik Eidus0f346072008-12-29 01:42:20 +02002702 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
2703 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002704}
2705
2706int complete_pio(struct kvm_vcpu *vcpu)
2707{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002708 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002709 long delta;
2710 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002711 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002712
2713 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002714 if (io->in) {
2715 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2716 memcpy(&val, vcpu->arch.pio_data, io->size);
2717 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2718 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002719 } else {
2720 if (io->in) {
2721 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002722 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002723 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002724 }
2725
2726 delta = 1;
2727 if (io->rep) {
2728 delta *= io->cur_count;
2729 /*
2730 * The size of the register should really depend on
2731 * current address size.
2732 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002733 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2734 val -= delta;
2735 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002736 }
2737 if (io->down)
2738 delta = -delta;
2739 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002740 if (io->in) {
2741 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2742 val += delta;
2743 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2744 } else {
2745 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2746 val += delta;
2747 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2748 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002749 }
2750
Carsten Ottede7d7892007-10-30 18:44:25 +01002751 io->count -= io->cur_count;
2752 io->cur_count = 0;
2753
2754 return 0;
2755}
2756
2757static void kernel_pio(struct kvm_io_device *pio_dev,
2758 struct kvm_vcpu *vcpu,
2759 void *pd)
2760{
2761 /* TODO: String I/O for in kernel device */
2762
2763 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002764 if (vcpu->arch.pio.in)
2765 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2766 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002767 pd);
2768 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002769 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2770 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002771 pd);
2772 mutex_unlock(&vcpu->kvm->lock);
2773}
2774
2775static void pio_string_write(struct kvm_io_device *pio_dev,
2776 struct kvm_vcpu *vcpu)
2777{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002778 struct kvm_pio_request *io = &vcpu->arch.pio;
2779 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002780 int i;
2781
2782 mutex_lock(&vcpu->kvm->lock);
2783 for (i = 0; i < io->cur_count; i++) {
2784 kvm_iodevice_write(pio_dev, io->port,
2785 io->size,
2786 pd);
2787 pd += io->size;
2788 }
2789 mutex_unlock(&vcpu->kvm->lock);
2790}
2791
2792static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002793 gpa_t addr, int len,
2794 int is_write)
Carsten Ottede7d7892007-10-30 18:44:25 +01002795{
Laurent Vivier92760492008-05-30 16:05:53 +02002796 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
Carsten Ottede7d7892007-10-30 18:44:25 +01002797}
2798
2799int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2800 int size, unsigned port)
2801{
2802 struct kvm_io_device *pio_dev;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002803 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002804
2805 vcpu->run->exit_reason = KVM_EXIT_IO;
2806 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002807 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002808 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002809 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2810 vcpu->run->io.port = vcpu->arch.pio.port = port;
2811 vcpu->arch.pio.in = in;
2812 vcpu->arch.pio.string = 0;
2813 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002814 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002815
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002816 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2817 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2818 handler);
2819 else
2820 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2821 handler);
2822
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002823 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2824 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002825
Laurent Vivier92760492008-05-30 16:05:53 +02002826 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
Carsten Ottede7d7892007-10-30 18:44:25 +01002827 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002828 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002829 complete_pio(vcpu);
2830 return 1;
2831 }
2832 return 0;
2833}
2834EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2835
2836int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2837 int size, unsigned long count, int down,
2838 gva_t address, int rep, unsigned port)
2839{
2840 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02002841 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002842 struct kvm_io_device *pio_dev;
2843
2844 vcpu->run->exit_reason = KVM_EXIT_IO;
2845 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002846 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002847 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2849 vcpu->run->io.port = vcpu->arch.pio.port = port;
2850 vcpu->arch.pio.in = in;
2851 vcpu->arch.pio.string = 1;
2852 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002853 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002854
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002855 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2856 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2857 handler);
2858 else
2859 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2860 handler);
2861
Carsten Ottede7d7892007-10-30 18:44:25 +01002862 if (!count) {
2863 kvm_x86_ops->skip_emulated_instruction(vcpu);
2864 return 1;
2865 }
2866
2867 if (!down)
2868 in_page = PAGE_SIZE - offset_in_page(address);
2869 else
2870 in_page = offset_in_page(address) + size;
2871 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02002872 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01002873 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002874 if (down) {
2875 /*
2876 * String I/O in reverse. Yuck. Kill the guest, fix later.
2877 */
2878 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002879 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002880 return 1;
2881 }
2882 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002883 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002884
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002885 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002886 kvm_x86_ops->skip_emulated_instruction(vcpu);
2887
Izik Eidus0f346072008-12-29 01:42:20 +02002888 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01002889
Laurent Vivier92760492008-05-30 16:05:53 +02002890 pio_dev = vcpu_find_pio_dev(vcpu, port,
2891 vcpu->arch.pio.cur_count,
2892 !vcpu->arch.pio.in);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002893 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002894 /* string PIO write */
2895 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02002896 if (ret == X86EMUL_PROPAGATE_FAULT) {
2897 kvm_inject_gp(vcpu, 0);
2898 return 1;
2899 }
2900 if (ret == 0 && pio_dev) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002901 pio_string_write(pio_dev, vcpu);
2902 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002903 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002904 ret = 1;
2905 }
2906 } else if (pio_dev)
2907 pr_unimpl(vcpu, "no string pio read support yet, "
2908 "port %x size %d count %ld\n",
2909 port, size, count);
2910
2911 return ret;
2912}
2913EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2914
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002915static void bounce_off(void *info)
2916{
2917 /* nothing */
2918}
2919
2920static unsigned int ref_freq;
2921static unsigned long tsc_khz_ref;
2922
2923static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
2924 void *data)
2925{
2926 struct cpufreq_freqs *freq = data;
2927 struct kvm *kvm;
2928 struct kvm_vcpu *vcpu;
2929 int i, send_ipi = 0;
2930
2931 if (!ref_freq)
2932 ref_freq = freq->old;
2933
2934 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
2935 return 0;
2936 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
2937 return 0;
2938 per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
2939
2940 spin_lock(&kvm_lock);
2941 list_for_each_entry(kvm, &vm_list, vm_list) {
2942 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2943 vcpu = kvm->vcpus[i];
2944 if (!vcpu)
2945 continue;
2946 if (vcpu->cpu != freq->cpu)
2947 continue;
2948 if (!kvm_request_guest_time_update(vcpu))
2949 continue;
2950 if (vcpu->cpu != smp_processor_id())
2951 send_ipi++;
2952 }
2953 }
2954 spin_unlock(&kvm_lock);
2955
2956 if (freq->old < freq->new && send_ipi) {
2957 /*
2958 * We upscale the frequency. Must make the guest
2959 * doesn't see old kvmclock values while running with
2960 * the new frequency, otherwise we risk the guest sees
2961 * time go backwards.
2962 *
2963 * In case we update the frequency for another cpu
2964 * (which might be in guest context) send an interrupt
2965 * to kick the cpu out of guest context. Next time
2966 * guest context is entered kvmclock will be updated,
2967 * so the guest will not see stale values.
2968 */
2969 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
2970 }
2971 return 0;
2972}
2973
2974static struct notifier_block kvmclock_cpufreq_notifier_block = {
2975 .notifier_call = kvmclock_cpufreq_notifier
2976};
2977
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002978int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002979{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01002980 int r, cpu;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002981 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2982
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002983 if (kvm_x86_ops) {
2984 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002985 r = -EEXIST;
2986 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002987 }
2988
2989 if (!ops->cpu_has_kvm_support()) {
2990 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002991 r = -EOPNOTSUPP;
2992 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002993 }
2994 if (ops->disabled_by_bios()) {
2995 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002996 r = -EOPNOTSUPP;
2997 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002998 }
2999
Avi Kivity97db56c2008-01-13 13:23:56 +02003000 r = kvm_mmu_module_init();
3001 if (r)
3002 goto out;
3003
3004 kvm_init_msr_list();
3005
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003006 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003007 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003008 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3009 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003010 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003011
3012 for_each_possible_cpu(cpu)
3013 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
3014 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
3015 tsc_khz_ref = tsc_khz;
3016 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3017 CPUFREQ_TRANSITION_NOTIFIER);
3018 }
3019
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003020 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003021
3022out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003023 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003024}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003025
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003026void kvm_arch_exit(void)
3027{
Jan Kiszka888d2562009-04-17 19:24:58 +02003028 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3029 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3030 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003031 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003032 kvm_mmu_module_exit();
3033}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003034
Hollis Blanchard8776e512007-10-31 17:24:24 -05003035int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3036{
3037 ++vcpu->stat.halt_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003038 KVMTRACE_0D(HLT, vcpu, handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003039 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003040 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003041 return 1;
3042 } else {
3043 vcpu->run->exit_reason = KVM_EXIT_HLT;
3044 return 0;
3045 }
3046}
3047EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3048
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003049static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3050 unsigned long a1)
3051{
3052 if (is_long_mode(vcpu))
3053 return a0;
3054 else
3055 return a0 | ((gpa_t)a1 << 32);
3056}
3057
Hollis Blanchard8776e512007-10-31 17:24:24 -05003058int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3059{
3060 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003061 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003062
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003063 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3064 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3065 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3066 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3067 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003068
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003069 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
3070
Hollis Blanchard8776e512007-10-31 17:24:24 -05003071 if (!is_long_mode(vcpu)) {
3072 nr &= 0xFFFFFFFF;
3073 a0 &= 0xFFFFFFFF;
3074 a1 &= 0xFFFFFFFF;
3075 a2 &= 0xFFFFFFFF;
3076 a3 &= 0xFFFFFFFF;
3077 }
3078
3079 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003080 case KVM_HC_VAPIC_POLL_IRQ:
3081 ret = 0;
3082 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003083 case KVM_HC_MMU_OP:
3084 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3085 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003086 default:
3087 ret = -KVM_ENOSYS;
3088 break;
3089 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003090 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303091 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003092 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003093}
3094EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3095
3096int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3097{
3098 char instruction[3];
3099 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003100 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003101
Hollis Blanchard8776e512007-10-31 17:24:24 -05003102
3103 /*
3104 * Blow out the MMU to ensure that no other VCPU has an active mapping
3105 * to ensure that the updated hypercall appears atomically across all
3106 * VCPUs.
3107 */
3108 kvm_mmu_zap_all(vcpu->kvm);
3109
Hollis Blanchard8776e512007-10-31 17:24:24 -05003110 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003111 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003112 != X86EMUL_CONTINUE)
3113 ret = -EFAULT;
3114
Hollis Blanchard8776e512007-10-31 17:24:24 -05003115 return ret;
3116}
3117
3118static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3119{
3120 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3121}
3122
3123void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3124{
3125 struct descriptor_table dt = { limit, base };
3126
3127 kvm_x86_ops->set_gdt(vcpu, &dt);
3128}
3129
3130void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3131{
3132 struct descriptor_table dt = { limit, base };
3133
3134 kvm_x86_ops->set_idt(vcpu, &dt);
3135}
3136
3137void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3138 unsigned long *rflags)
3139{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003140 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003141 *rflags = kvm_x86_ops->get_rflags(vcpu);
3142}
3143
3144unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3145{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003146 unsigned long value;
3147
Hollis Blanchard8776e512007-10-31 17:24:24 -05003148 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3149 switch (cr) {
3150 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003151 value = vcpu->arch.cr0;
3152 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003153 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003154 value = vcpu->arch.cr2;
3155 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003156 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003157 value = vcpu->arch.cr3;
3158 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003159 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003160 value = vcpu->arch.cr4;
3161 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003162 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003163 value = kvm_get_cr8(vcpu);
3164 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003165 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003166 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003167 return 0;
3168 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003169 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
3170 (u32)((u64)value >> 32), handler);
3171
3172 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003173}
3174
3175void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3176 unsigned long *rflags)
3177{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003178 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
3179 (u32)((u64)val >> 32), handler);
3180
Hollis Blanchard8776e512007-10-31 17:24:24 -05003181 switch (cr) {
3182 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003183 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003184 *rflags = kvm_x86_ops->get_rflags(vcpu);
3185 break;
3186 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003187 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003188 break;
3189 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003190 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003191 break;
3192 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003193 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003194 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003195 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003196 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003197 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003198 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003199 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003200 }
3201}
3202
Dan Kenigsberg07716712007-11-21 17:10:04 +02003203static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3204{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003205 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3206 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02003207
3208 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3209 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08003210 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003211 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003212 if (ej->function == e->function) {
3213 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3214 return j;
3215 }
3216 }
3217 return 0; /* silence gcc, even though control never reaches here */
3218}
3219
3220/* find an entry with matching function, matching index (if needed), and that
3221 * should be read next (if it's stateful) */
3222static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3223 u32 function, u32 index)
3224{
3225 if (e->function != function)
3226 return 0;
3227 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3228 return 0;
3229 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00003230 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02003231 return 0;
3232 return 1;
3233}
3234
Alexander Grafd8017472008-11-25 20:17:11 +01003235struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3236 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003237{
3238 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01003239 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003240
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003241 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01003242 struct kvm_cpuid_entry2 *e;
3243
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003244 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003245 if (is_matching_cpuid_entry(e, function, index)) {
3246 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3247 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003248 best = e;
3249 break;
3250 }
3251 /*
3252 * Both basic or both extended?
3253 */
3254 if (((e->function ^ function) & 0x80000000) == 0)
3255 if (!best || e->function > best->function)
3256 best = e;
3257 }
Alexander Grafd8017472008-11-25 20:17:11 +01003258 return best;
3259}
3260
Dong, Eddie82725b22009-03-30 16:21:08 +08003261int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3262{
3263 struct kvm_cpuid_entry2 *best;
3264
3265 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3266 if (best)
3267 return best->eax & 0xff;
3268 return 36;
3269}
3270
Alexander Grafd8017472008-11-25 20:17:11 +01003271void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3272{
3273 u32 function, index;
3274 struct kvm_cpuid_entry2 *best;
3275
3276 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3277 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3278 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3279 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3280 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3281 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3282 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003283 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003284 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3285 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3286 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3287 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003288 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003289 kvm_x86_ops->skip_emulated_instruction(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003290 KVMTRACE_5D(CPUID, vcpu, function,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003291 (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
3292 (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
3293 (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
3294 (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003295}
3296EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003297
3298/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003299 * Check if userspace requested an interrupt window, and that the
3300 * interrupt window is open.
3301 *
3302 * No need to exit to userspace if we already have an interrupt queued.
3303 */
3304static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
3305 struct kvm_run *kvm_run)
3306{
Gleb Natapov80618232009-04-21 17:44:56 +03003307 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003308 kvm_run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03003309 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003310}
3311
3312static void post_kvm_run_save(struct kvm_vcpu *vcpu,
3313 struct kvm_run *kvm_run)
3314{
3315 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003316 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003317 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01003318 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003319 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01003320 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003321 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03003322 kvm_arch_interrupt_allowed(vcpu) &&
3323 !kvm_cpu_has_interrupt(vcpu) &&
3324 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003325}
3326
Avi Kivityb93463a2007-10-25 16:52:32 +02003327static void vapic_enter(struct kvm_vcpu *vcpu)
3328{
3329 struct kvm_lapic *apic = vcpu->arch.apic;
3330 struct page *page;
3331
3332 if (!apic || !apic->vapic_addr)
3333 return;
3334
3335 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003336
3337 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003338}
3339
3340static void vapic_exit(struct kvm_vcpu *vcpu)
3341{
3342 struct kvm_lapic *apic = vcpu->arch.apic;
3343
3344 if (!apic || !apic->vapic_addr)
3345 return;
3346
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003347 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003348 kvm_release_page_dirty(apic->vapic_page);
3349 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003350 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003351}
3352
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003353static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3354{
3355 int max_irr, tpr;
3356
3357 if (!kvm_x86_ops->update_cr8_intercept)
3358 return;
3359
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003360 if (!vcpu->arch.apic->vapic_addr)
3361 max_irr = kvm_lapic_find_highest_irr(vcpu);
3362 else
3363 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003364
3365 if (max_irr != -1)
3366 max_irr >>= 4;
3367
3368 tpr = kvm_lapic_get_cr8(vcpu);
3369
3370 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3371}
3372
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003373static void inject_pending_irq(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003374{
3375 /* try to reinject previous events if any */
3376 if (vcpu->arch.nmi_injected) {
3377 kvm_x86_ops->set_nmi(vcpu);
3378 return;
3379 }
3380
3381 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003382 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003383 return;
3384 }
3385
3386 /* try to inject new event if pending */
3387 if (vcpu->arch.nmi_pending) {
3388 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3389 vcpu->arch.nmi_pending = false;
3390 vcpu->arch.nmi_injected = true;
3391 kvm_x86_ops->set_nmi(vcpu);
3392 }
3393 } else if (kvm_cpu_has_interrupt(vcpu)) {
3394 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003395 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3396 false);
3397 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003398 }
3399 }
3400}
3401
Marcelo Tosattid7690172008-09-08 15:23:48 -03003402static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003403{
3404 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003405 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
3406 kvm_run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003407
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003408 if (vcpu->requests)
3409 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3410 kvm_mmu_unload(vcpu);
3411
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003412 r = kvm_mmu_reload(vcpu);
3413 if (unlikely(r))
3414 goto out;
3415
Avi Kivity2f52d582008-01-16 12:49:30 +02003416 if (vcpu->requests) {
3417 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003418 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003419 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3420 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03003421 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3422 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003423 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3424 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003425 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3426 &vcpu->requests)) {
3427 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
3428 r = 0;
3429 goto out;
3430 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003431 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3432 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
3433 r = 0;
3434 goto out;
3435 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003436 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003437
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003438 preempt_disable();
3439
3440 kvm_x86_ops->prepare_guest_switch(vcpu);
3441 kvm_load_guest_fpu(vcpu);
3442
3443 local_irq_disable();
3444
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003445 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3446 smp_mb__after_clear_bit();
3447
Marcelo Tosattid7690172008-09-08 15:23:48 -03003448 if (vcpu->requests || need_resched() || signal_pending(current)) {
Avi Kivity6c142802008-01-15 18:27:32 +02003449 local_irq_enable();
3450 preempt_enable();
3451 r = 1;
3452 goto out;
3453 }
3454
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003455 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02003456 __queue_exception(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02003457 else
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003458 inject_pending_irq(vcpu, kvm_run);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003459
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003460 /* enable NMI/IRQ window open exits if needed */
3461 if (vcpu->arch.nmi_pending)
3462 kvm_x86_ops->enable_nmi_window(vcpu);
3463 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3464 kvm_x86_ops->enable_irq_window(vcpu);
3465
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003466 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003467 update_cr8_intercept(vcpu);
3468 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003469 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003470
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003471 up_read(&vcpu->kvm->slots_lock);
3472
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003473 kvm_guest_enter();
3474
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003475 get_debugreg(vcpu->arch.host_dr6, 6);
3476 get_debugreg(vcpu->arch.host_dr7, 7);
3477 if (unlikely(vcpu->arch.switch_db_regs)) {
3478 get_debugreg(vcpu->arch.host_db[0], 0);
3479 get_debugreg(vcpu->arch.host_db[1], 1);
3480 get_debugreg(vcpu->arch.host_db[2], 2);
3481 get_debugreg(vcpu->arch.host_db[3], 3);
3482
3483 set_debugreg(0, 7);
3484 set_debugreg(vcpu->arch.eff_db[0], 0);
3485 set_debugreg(vcpu->arch.eff_db[1], 1);
3486 set_debugreg(vcpu->arch.eff_db[2], 2);
3487 set_debugreg(vcpu->arch.eff_db[3], 3);
3488 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003489
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003490 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003491 kvm_x86_ops->run(vcpu, kvm_run);
3492
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003493 if (unlikely(vcpu->arch.switch_db_regs)) {
3494 set_debugreg(0, 7);
3495 set_debugreg(vcpu->arch.host_db[0], 0);
3496 set_debugreg(vcpu->arch.host_db[1], 1);
3497 set_debugreg(vcpu->arch.host_db[2], 2);
3498 set_debugreg(vcpu->arch.host_db[3], 3);
3499 }
3500 set_debugreg(vcpu->arch.host_dr6, 6);
3501 set_debugreg(vcpu->arch.host_dr7, 7);
3502
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003503 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003504 local_irq_enable();
3505
3506 ++vcpu->stat.exits;
3507
3508 /*
3509 * We must have an instruction between local_irq_enable() and
3510 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3511 * the interrupt shadow. The stat.exits increment will do nicely.
3512 * But we need to prevent reordering, hence this barrier():
3513 */
3514 barrier();
3515
3516 kvm_guest_exit();
3517
3518 preempt_enable();
3519
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003520 down_read(&vcpu->kvm->slots_lock);
3521
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003522 /*
3523 * Profile KVM exit RIPs:
3524 */
3525 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003526 unsigned long rip = kvm_rip_read(vcpu);
3527 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003528 }
3529
Avi Kivity298101d2007-11-25 13:41:11 +02003530
Avi Kivityb93463a2007-10-25 16:52:32 +02003531 kvm_lapic_sync_from_vapic(vcpu);
3532
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003533 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003534out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003535 return r;
3536}
3537
Gleb Natapov09cec752009-03-23 15:11:44 +02003538
Marcelo Tosattid7690172008-09-08 15:23:48 -03003539static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3540{
3541 int r;
3542
3543 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003544 pr_debug("vcpu %d received sipi with vector # %x\n",
3545 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003546 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003547 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003548 if (r)
3549 return r;
3550 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003551 }
3552
Marcelo Tosattid7690172008-09-08 15:23:48 -03003553 down_read(&vcpu->kvm->slots_lock);
3554 vapic_enter(vcpu);
3555
3556 r = 1;
3557 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003558 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003559 r = vcpu_enter_guest(vcpu, kvm_run);
3560 else {
3561 up_read(&vcpu->kvm->slots_lock);
3562 kvm_vcpu_block(vcpu);
3563 down_read(&vcpu->kvm->slots_lock);
3564 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02003565 {
3566 switch(vcpu->arch.mp_state) {
3567 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003568 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02003569 KVM_MP_STATE_RUNNABLE;
3570 case KVM_MP_STATE_RUNNABLE:
3571 break;
3572 case KVM_MP_STATE_SIPI_RECEIVED:
3573 default:
3574 r = -EINTR;
3575 break;
3576 }
3577 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03003578 }
3579
Gleb Natapov09cec752009-03-23 15:11:44 +02003580 if (r <= 0)
3581 break;
3582
3583 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
3584 if (kvm_cpu_has_pending_timer(vcpu))
3585 kvm_inject_pending_timer_irqs(vcpu);
3586
3587 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
3588 r = -EINTR;
3589 kvm_run->exit_reason = KVM_EXIT_INTR;
3590 ++vcpu->stat.request_irq_exits;
3591 }
3592 if (signal_pending(current)) {
3593 r = -EINTR;
3594 kvm_run->exit_reason = KVM_EXIT_INTR;
3595 ++vcpu->stat.signal_exits;
3596 }
3597 if (need_resched()) {
3598 up_read(&vcpu->kvm->slots_lock);
3599 kvm_resched(vcpu);
3600 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003601 }
3602 }
3603
3604 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003605 post_kvm_run_save(vcpu, kvm_run);
3606
Avi Kivityb93463a2007-10-25 16:52:32 +02003607 vapic_exit(vcpu);
3608
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003609 return r;
3610}
3611
3612int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3613{
3614 int r;
3615 sigset_t sigsaved;
3616
3617 vcpu_load(vcpu);
3618
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003619 if (vcpu->sigset_active)
3620 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3621
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003622 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3623 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003624 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003625 r = -EAGAIN;
3626 goto out;
3627 }
3628
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003629 /* re-sync apic's tpr */
3630 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003631 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003632
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003633 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003634 r = complete_pio(vcpu);
3635 if (r)
3636 goto out;
3637 }
3638#if CONFIG_HAS_IOMEM
3639 if (vcpu->mmio_needed) {
3640 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3641 vcpu->mmio_read_completed = 1;
3642 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003643
3644 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003645 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003646 vcpu->arch.mmio_fault_cr2, 0,
3647 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003648 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003649 if (r == EMULATE_DO_MMIO) {
3650 /*
3651 * Read-modify-write. Back to userspace.
3652 */
3653 r = 0;
3654 goto out;
3655 }
3656 }
3657#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003658 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3659 kvm_register_write(vcpu, VCPU_REGS_RAX,
3660 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003661
3662 r = __vcpu_run(vcpu, kvm_run);
3663
3664out:
3665 if (vcpu->sigset_active)
3666 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3667
3668 vcpu_put(vcpu);
3669 return r;
3670}
3671
3672int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3673{
3674 vcpu_load(vcpu);
3675
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003676 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3677 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3678 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3679 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3680 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3681 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3682 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3683 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003684#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003685 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3686 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3687 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3688 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3689 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3690 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3691 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3692 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003693#endif
3694
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003695 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003696 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3697
3698 /*
3699 * Don't leak debug flags in case they were set for guest debugging
3700 */
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003701 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003702 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3703
3704 vcpu_put(vcpu);
3705
3706 return 0;
3707}
3708
3709int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3710{
3711 vcpu_load(vcpu);
3712
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003713 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3714 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3715 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3716 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3717 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3718 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3719 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3720 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003721#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003722 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3723 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3724 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3725 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3726 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3727 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3728 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3729 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3730
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003731#endif
3732
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003733 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003734 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3735
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003736
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003737 vcpu->arch.exception.pending = false;
3738
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003739 vcpu_put(vcpu);
3740
3741 return 0;
3742}
3743
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003744void kvm_get_segment(struct kvm_vcpu *vcpu,
3745 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003746{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003747 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003748}
3749
3750void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3751{
3752 struct kvm_segment cs;
3753
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003754 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003755 *db = cs.db;
3756 *l = cs.l;
3757}
3758EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3759
3760int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3761 struct kvm_sregs *sregs)
3762{
3763 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003764
3765 vcpu_load(vcpu);
3766
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003767 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3768 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3769 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3770 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3771 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3772 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003773
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003774 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3775 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003776
3777 kvm_x86_ops->get_idt(vcpu, &dt);
3778 sregs->idt.limit = dt.limit;
3779 sregs->idt.base = dt.base;
3780 kvm_x86_ops->get_gdt(vcpu, &dt);
3781 sregs->gdt.limit = dt.limit;
3782 sregs->gdt.base = dt.base;
3783
3784 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003785 sregs->cr0 = vcpu->arch.cr0;
3786 sregs->cr2 = vcpu->arch.cr2;
3787 sregs->cr3 = vcpu->arch.cr3;
3788 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003789 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003790 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003791 sregs->apic_base = kvm_get_apic_base(vcpu);
3792
Gleb Natapov923c61b2009-05-11 13:35:48 +03003793 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003794
Gleb Natapov36752c92009-05-11 13:35:53 +03003795 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03003796 set_bit(vcpu->arch.interrupt.nr,
3797 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03003798
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003799 vcpu_put(vcpu);
3800
3801 return 0;
3802}
3803
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003804int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3805 struct kvm_mp_state *mp_state)
3806{
3807 vcpu_load(vcpu);
3808 mp_state->mp_state = vcpu->arch.mp_state;
3809 vcpu_put(vcpu);
3810 return 0;
3811}
3812
3813int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3814 struct kvm_mp_state *mp_state)
3815{
3816 vcpu_load(vcpu);
3817 vcpu->arch.mp_state = mp_state->mp_state;
3818 vcpu_put(vcpu);
3819 return 0;
3820}
3821
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003822static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003823 struct kvm_segment *var, int seg)
3824{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003825 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003826}
3827
Izik Eidus37817f22008-03-24 23:14:53 +02003828static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3829 struct kvm_segment *kvm_desct)
3830{
3831 kvm_desct->base = seg_desc->base0;
3832 kvm_desct->base |= seg_desc->base1 << 16;
3833 kvm_desct->base |= seg_desc->base2 << 24;
3834 kvm_desct->limit = seg_desc->limit0;
3835 kvm_desct->limit |= seg_desc->limit << 16;
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003836 if (seg_desc->g) {
3837 kvm_desct->limit <<= 12;
3838 kvm_desct->limit |= 0xfff;
3839 }
Izik Eidus37817f22008-03-24 23:14:53 +02003840 kvm_desct->selector = selector;
3841 kvm_desct->type = seg_desc->type;
3842 kvm_desct->present = seg_desc->p;
3843 kvm_desct->dpl = seg_desc->dpl;
3844 kvm_desct->db = seg_desc->d;
3845 kvm_desct->s = seg_desc->s;
3846 kvm_desct->l = seg_desc->l;
3847 kvm_desct->g = seg_desc->g;
3848 kvm_desct->avl = seg_desc->avl;
3849 if (!selector)
3850 kvm_desct->unusable = 1;
3851 else
3852 kvm_desct->unusable = 0;
3853 kvm_desct->padding = 0;
3854}
3855
Amit Shahb8222ad2008-10-22 16:39:47 +05303856static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
3857 u16 selector,
3858 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02003859{
3860 if (selector & 1 << 2) {
3861 struct kvm_segment kvm_seg;
3862
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003863 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003864
3865 if (kvm_seg.unusable)
3866 dtable->limit = 0;
3867 else
3868 dtable->limit = kvm_seg.limit;
3869 dtable->base = kvm_seg.base;
3870 }
3871 else
3872 kvm_x86_ops->get_gdt(vcpu, dtable);
3873}
3874
3875/* allowed just for 8 bytes segments */
3876static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3877 struct desc_struct *seg_desc)
3878{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003879 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003880 struct descriptor_table dtable;
3881 u16 index = selector >> 3;
3882
Amit Shahb8222ad2008-10-22 16:39:47 +05303883 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003884
3885 if (dtable.limit < index * 8 + 7) {
3886 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3887 return 1;
3888 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003889 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3890 gpa += index * 8;
3891 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003892}
3893
3894/* allowed just for 8 bytes segments */
3895static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3896 struct desc_struct *seg_desc)
3897{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003898 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003899 struct descriptor_table dtable;
3900 u16 index = selector >> 3;
3901
Amit Shahb8222ad2008-10-22 16:39:47 +05303902 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02003903
3904 if (dtable.limit < index * 8 + 7)
3905 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003906 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3907 gpa += index * 8;
3908 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003909}
3910
3911static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3912 struct desc_struct *seg_desc)
3913{
3914 u32 base_addr;
3915
3916 base_addr = seg_desc->base0;
3917 base_addr |= (seg_desc->base1 << 16);
3918 base_addr |= (seg_desc->base2 << 24);
3919
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003920 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02003921}
3922
Izik Eidus37817f22008-03-24 23:14:53 +02003923static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3924{
3925 struct kvm_segment kvm_seg;
3926
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003927 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003928 return kvm_seg.selector;
3929}
3930
3931static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3932 u16 selector,
3933 struct kvm_segment *kvm_seg)
3934{
3935 struct desc_struct seg_desc;
3936
3937 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3938 return 1;
3939 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3940 return 0;
3941}
3942
Harvey Harrison2259e3a2008-08-22 13:29:17 -07003943static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003944{
3945 struct kvm_segment segvar = {
3946 .base = selector << 4,
3947 .limit = 0xffff,
3948 .selector = selector,
3949 .type = 3,
3950 .present = 1,
3951 .dpl = 3,
3952 .db = 0,
3953 .s = 1,
3954 .l = 0,
3955 .g = 0,
3956 .avl = 0,
3957 .unusable = 0,
3958 };
3959 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
3960 return 0;
3961}
3962
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003963int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3964 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02003965{
3966 struct kvm_segment kvm_seg;
3967
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03003968 if (!(vcpu->arch.cr0 & X86_CR0_PE))
3969 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003970 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3971 return 1;
3972 kvm_seg.type |= type_bits;
3973
3974 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3975 seg != VCPU_SREG_LDTR)
3976 if (!kvm_seg.s)
3977 kvm_seg.unusable = 1;
3978
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003979 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003980 return 0;
3981}
3982
3983static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3984 struct tss_segment_32 *tss)
3985{
3986 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003987 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003988 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003989 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3990 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3991 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3992 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3993 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3994 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3995 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3996 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003997 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3998 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3999 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4000 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4001 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4002 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4003 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004004}
4005
4006static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4007 struct tss_segment_32 *tss)
4008{
4009 kvm_set_cr3(vcpu, tss->cr3);
4010
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004011 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02004012 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
4013
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004014 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4015 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4016 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4017 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4018 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4019 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4020 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4021 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004022
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004023 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004024 return 1;
4025
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004026 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004027 return 1;
4028
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004029 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004030 return 1;
4031
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004032 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004033 return 1;
4034
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004035 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004036 return 1;
4037
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004038 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004039 return 1;
4040
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004041 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004042 return 1;
4043 return 0;
4044}
4045
4046static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4047 struct tss_segment_16 *tss)
4048{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004049 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004050 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004051 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4052 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4053 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4054 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4055 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4056 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4057 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4058 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004059
4060 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4061 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4062 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4063 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4064 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4065 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
4066}
4067
4068static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4069 struct tss_segment_16 *tss)
4070{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004071 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02004072 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004073 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4074 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4075 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4076 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4077 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4078 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4079 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4080 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02004081
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004082 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004083 return 1;
4084
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004085 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004086 return 1;
4087
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004088 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004089 return 1;
4090
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004091 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004092 return 1;
4093
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004094 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004095 return 1;
4096 return 0;
4097}
4098
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004099static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004100 u16 old_tss_sel, u32 old_tss_base,
4101 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02004102{
4103 struct tss_segment_16 tss_segment_16;
4104 int ret = 0;
4105
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004106 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4107 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004108 goto out;
4109
4110 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02004111
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004112 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4113 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004114 goto out;
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004115
4116 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4117 &tss_segment_16, sizeof tss_segment_16))
4118 goto out;
4119
Gleb Natapovb237ac32009-03-30 16:03:24 +03004120 if (old_tss_sel != 0xffff) {
4121 tss_segment_16.prev_task_link = old_tss_sel;
4122
4123 if (kvm_write_guest(vcpu->kvm,
4124 get_tss_base_addr(vcpu, nseg_desc),
4125 &tss_segment_16.prev_task_link,
4126 sizeof tss_segment_16.prev_task_link))
4127 goto out;
4128 }
4129
Izik Eidus37817f22008-03-24 23:14:53 +02004130 if (load_state_from_tss16(vcpu, &tss_segment_16))
4131 goto out;
4132
4133 ret = 1;
4134out:
4135 return ret;
4136}
4137
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004138static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004139 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02004140 struct desc_struct *nseg_desc)
4141{
4142 struct tss_segment_32 tss_segment_32;
4143 int ret = 0;
4144
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004145 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4146 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004147 goto out;
4148
4149 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02004150
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004151 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4152 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004153 goto out;
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004154
4155 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4156 &tss_segment_32, sizeof tss_segment_32))
4157 goto out;
4158
Gleb Natapovb237ac32009-03-30 16:03:24 +03004159 if (old_tss_sel != 0xffff) {
4160 tss_segment_32.prev_task_link = old_tss_sel;
4161
4162 if (kvm_write_guest(vcpu->kvm,
4163 get_tss_base_addr(vcpu, nseg_desc),
4164 &tss_segment_32.prev_task_link,
4165 sizeof tss_segment_32.prev_task_link))
4166 goto out;
4167 }
4168
Izik Eidus37817f22008-03-24 23:14:53 +02004169 if (load_state_from_tss32(vcpu, &tss_segment_32))
4170 goto out;
4171
4172 ret = 1;
4173out:
4174 return ret;
4175}
4176
4177int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4178{
4179 struct kvm_segment tr_seg;
4180 struct desc_struct cseg_desc;
4181 struct desc_struct nseg_desc;
4182 int ret = 0;
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004183 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4184 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004185
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004186 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02004187
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004188 /* FIXME: Handle errors. Failure to read either TSS or their
4189 * descriptors should generate a pagefault.
4190 */
Izik Eidus37817f22008-03-24 23:14:53 +02004191 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4192 goto out;
4193
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004194 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02004195 goto out;
4196
Izik Eidus37817f22008-03-24 23:14:53 +02004197 if (reason != TASK_SWITCH_IRET) {
4198 int cpl;
4199
4200 cpl = kvm_x86_ops->get_cpl(vcpu);
4201 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4202 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4203 return 1;
4204 }
4205 }
4206
4207 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
4208 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4209 return 1;
4210 }
4211
4212 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004213 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf2008-07-16 19:07:11 -03004214 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004215 }
4216
4217 if (reason == TASK_SWITCH_IRET) {
4218 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4219 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
4220 }
4221
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004222 /* set back link to prev task only if NT bit is set in eflags
4223 note that old_tss_sel is not used afetr this point */
4224 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4225 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02004226
Gleb Natapovb237ac32009-03-30 16:03:24 +03004227 /* set back link to prev task only if NT bit is set in eflags
4228 note that old_tss_sel is not used afetr this point */
4229 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4230 old_tss_sel = 0xffff;
4231
Izik Eidus37817f22008-03-24 23:14:53 +02004232 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03004233 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4234 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004235 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03004236 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4237 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004238
4239 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
4240 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4241 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
4242 }
4243
4244 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004245 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02004246 save_guest_segment_descriptor(vcpu, tss_selector,
4247 &nseg_desc);
4248 }
4249
4250 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4251 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4252 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004253 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004254out:
Izik Eidus37817f22008-03-24 23:14:53 +02004255 return ret;
4256}
4257EXPORT_SYMBOL_GPL(kvm_task_switch);
4258
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004259int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4260 struct kvm_sregs *sregs)
4261{
4262 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004263 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004264 struct descriptor_table dt;
4265
4266 vcpu_load(vcpu);
4267
4268 dt.limit = sregs->idt.limit;
4269 dt.base = sregs->idt.base;
4270 kvm_x86_ops->set_idt(vcpu, &dt);
4271 dt.limit = sregs->gdt.limit;
4272 dt.base = sregs->gdt.base;
4273 kvm_x86_ops->set_gdt(vcpu, &dt);
4274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004275 vcpu->arch.cr2 = sregs->cr2;
4276 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Marcelo Tosatti59839df2009-04-16 08:30:44 -03004277
4278 down_read(&vcpu->kvm->slots_lock);
4279 if (gfn_to_memslot(vcpu->kvm, sregs->cr3 >> PAGE_SHIFT))
4280 vcpu->arch.cr3 = sregs->cr3;
4281 else
4282 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
4283 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004284
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004285 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004286
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004287 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004288 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004289 kvm_set_apic_base(vcpu, sregs->apic_base);
4290
4291 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4292
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004293 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004294 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004295 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004296
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004297 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004298 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4299 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004300 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004301
4302 if (mmu_reset_needed)
4303 kvm_mmu_reset_context(vcpu);
4304
Gleb Natapov923c61b2009-05-11 13:35:48 +03004305 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4306 pending_vec = find_first_bit(
4307 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4308 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004309 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004310 pr_debug("Set back pending irq %d\n", pending_vec);
4311 if (irqchip_in_kernel(vcpu->kvm))
4312 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004313 }
4314
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004315 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4316 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4317 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4318 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4319 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4320 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004321
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004322 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4323 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004324
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004325 /* Older userspace won't unhalt the vcpu on reset. */
4326 if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 &&
4327 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4328 !(vcpu->arch.cr0 & X86_CR0_PE))
4329 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4330
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004331 vcpu_put(vcpu);
4332
4333 return 0;
4334}
4335
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004336int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4337 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004338{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004339 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004340
4341 vcpu_load(vcpu);
4342
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004343 if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) ==
4344 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) {
4345 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4346 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4347 vcpu->arch.switch_db_regs =
4348 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4349 } else {
4350 for (i = 0; i < KVM_NR_DB_REGS; i++)
4351 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4352 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4353 }
4354
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004355 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
4356
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004357 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4358 kvm_queue_exception(vcpu, DB_VECTOR);
4359 else if (dbg->control & KVM_GUESTDBG_INJECT_BP)
4360 kvm_queue_exception(vcpu, BP_VECTOR);
4361
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004362 vcpu_put(vcpu);
4363
4364 return r;
4365}
4366
4367/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05004368 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4369 * we have asm/x86/processor.h
4370 */
4371struct fxsave {
4372 u16 cwd;
4373 u16 swd;
4374 u16 twd;
4375 u16 fop;
4376 u64 rip;
4377 u64 rdp;
4378 u32 mxcsr;
4379 u32 mxcsr_mask;
4380 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4381#ifdef CONFIG_X86_64
4382 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4383#else
4384 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4385#endif
4386};
4387
Zhang Xiantao8b006792007-11-16 13:05:55 +08004388/*
4389 * Translate a guest virtual address to a guest physical address.
4390 */
4391int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4392 struct kvm_translation *tr)
4393{
4394 unsigned long vaddr = tr->linear_address;
4395 gpa_t gpa;
4396
4397 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004398 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004399 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004400 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004401 tr->physical_address = gpa;
4402 tr->valid = gpa != UNMAPPED_GVA;
4403 tr->writeable = 1;
4404 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004405 vcpu_put(vcpu);
4406
4407 return 0;
4408}
4409
Hollis Blanchardd0752062007-10-31 17:24:25 -05004410int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4411{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004412 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004413
4414 vcpu_load(vcpu);
4415
4416 memcpy(fpu->fpr, fxsave->st_space, 128);
4417 fpu->fcw = fxsave->cwd;
4418 fpu->fsw = fxsave->swd;
4419 fpu->ftwx = fxsave->twd;
4420 fpu->last_opcode = fxsave->fop;
4421 fpu->last_ip = fxsave->rip;
4422 fpu->last_dp = fxsave->rdp;
4423 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4424
4425 vcpu_put(vcpu);
4426
4427 return 0;
4428}
4429
4430int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4431{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004432 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004433
4434 vcpu_load(vcpu);
4435
4436 memcpy(fxsave->st_space, fpu->fpr, 128);
4437 fxsave->cwd = fpu->fcw;
4438 fxsave->swd = fpu->fsw;
4439 fxsave->twd = fpu->ftwx;
4440 fxsave->fop = fpu->last_opcode;
4441 fxsave->rip = fpu->last_ip;
4442 fxsave->rdp = fpu->last_dp;
4443 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4444
4445 vcpu_put(vcpu);
4446
4447 return 0;
4448}
4449
4450void fx_init(struct kvm_vcpu *vcpu)
4451{
4452 unsigned after_mxcsr_mask;
4453
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004454 /*
4455 * Touch the fpu the first time in non atomic context as if
4456 * this is the first fpu instruction the exception handler
4457 * will fire before the instruction returns and it'll have to
4458 * allocate ram with GFP_KERNEL.
4459 */
4460 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004461 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004462
Hollis Blanchardd0752062007-10-31 17:24:25 -05004463 /* Initialize guest FPU by resetting ours and saving into guest's */
4464 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004465 kvm_fx_save(&vcpu->arch.host_fx_image);
4466 kvm_fx_finit();
4467 kvm_fx_save(&vcpu->arch.guest_fx_image);
4468 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004469 preempt_enable();
4470
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004471 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004472 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004473 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4474 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004475 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4476}
4477EXPORT_SYMBOL_GPL(fx_init);
4478
4479void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4480{
4481 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4482 return;
4483
4484 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004485 kvm_fx_save(&vcpu->arch.host_fx_image);
4486 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004487}
4488EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4489
4490void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4491{
4492 if (!vcpu->guest_fpu_loaded)
4493 return;
4494
4495 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004496 kvm_fx_save(&vcpu->arch.guest_fx_image);
4497 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004498 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004499}
4500EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004501
4502void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4503{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01004504 if (vcpu->arch.time_page) {
4505 kvm_release_page_dirty(vcpu->arch.time_page);
4506 vcpu->arch.time_page = NULL;
4507 }
4508
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004509 kvm_x86_ops->vcpu_free(vcpu);
4510}
4511
4512struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4513 unsigned int id)
4514{
Avi Kivity26e52152007-11-20 15:30:24 +02004515 return kvm_x86_ops->vcpu_create(kvm, id);
4516}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004517
Avi Kivity26e52152007-11-20 15:30:24 +02004518int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4519{
4520 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004521
4522 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004523 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004524
Sheng Yang0bed3b52008-10-09 16:01:54 +08004525 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004526 vcpu_load(vcpu);
4527 r = kvm_arch_vcpu_reset(vcpu);
4528 if (r == 0)
4529 r = kvm_mmu_setup(vcpu);
4530 vcpu_put(vcpu);
4531 if (r < 0)
4532 goto free_vcpu;
4533
Avi Kivity26e52152007-11-20 15:30:24 +02004534 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004535free_vcpu:
4536 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004537 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004538}
4539
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004540void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004541{
4542 vcpu_load(vcpu);
4543 kvm_mmu_unload(vcpu);
4544 vcpu_put(vcpu);
4545
4546 kvm_x86_ops->vcpu_free(vcpu);
4547}
4548
4549int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4550{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004551 vcpu->arch.nmi_pending = false;
4552 vcpu->arch.nmi_injected = false;
4553
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004554 vcpu->arch.switch_db_regs = 0;
4555 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4556 vcpu->arch.dr6 = DR6_FIXED_1;
4557 vcpu->arch.dr7 = DR7_FIXED_1;
4558
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004559 return kvm_x86_ops->vcpu_reset(vcpu);
4560}
4561
4562void kvm_arch_hardware_enable(void *garbage)
4563{
4564 kvm_x86_ops->hardware_enable(garbage);
4565}
4566
4567void kvm_arch_hardware_disable(void *garbage)
4568{
4569 kvm_x86_ops->hardware_disable(garbage);
4570}
4571
4572int kvm_arch_hardware_setup(void)
4573{
4574 return kvm_x86_ops->hardware_setup();
4575}
4576
4577void kvm_arch_hardware_unsetup(void)
4578{
4579 kvm_x86_ops->hardware_unsetup();
4580}
4581
4582void kvm_arch_check_processor_compat(void *rtn)
4583{
4584 kvm_x86_ops->check_processor_compatibility(rtn);
4585}
4586
4587int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4588{
4589 struct page *page;
4590 struct kvm *kvm;
4591 int r;
4592
4593 BUG_ON(vcpu->kvm == NULL);
4594 kvm = vcpu->kvm;
4595
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004596 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004597 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Avi Kivitya4535292008-04-13 17:54:35 +03004598 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004599 else
Avi Kivitya4535292008-04-13 17:54:35 +03004600 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004601
4602 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4603 if (!page) {
4604 r = -ENOMEM;
4605 goto fail;
4606 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004607 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004608
4609 r = kvm_mmu_create(vcpu);
4610 if (r < 0)
4611 goto fail_free_pio_data;
4612
4613 if (irqchip_in_kernel(kvm)) {
4614 r = kvm_create_lapic(vcpu);
4615 if (r < 0)
4616 goto fail_mmu_destroy;
4617 }
4618
Huang Ying890ca9a2009-05-11 16:48:15 +08004619 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
4620 GFP_KERNEL);
4621 if (!vcpu->arch.mce_banks) {
4622 r = -ENOMEM;
4623 goto fail_mmu_destroy;
4624 }
4625 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
4626
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004627 return 0;
4628
4629fail_mmu_destroy:
4630 kvm_mmu_destroy(vcpu);
4631fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004632 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004633fail:
4634 return r;
4635}
4636
4637void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4638{
4639 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004640 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004641 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004642 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004643 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004644}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004645
4646struct kvm *kvm_arch_create_vm(void)
4647{
4648 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4649
4650 if (!kvm)
4651 return ERR_PTR(-ENOMEM);
4652
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004653 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004654 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004655
Sheng Yang5550af42008-10-15 20:15:06 +08004656 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4657 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
4658
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01004659 rdtscll(kvm->arch.vm_init_tsc);
4660
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004661 return kvm;
4662}
4663
4664static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4665{
4666 vcpu_load(vcpu);
4667 kvm_mmu_unload(vcpu);
4668 vcpu_put(vcpu);
4669}
4670
4671static void kvm_free_vcpus(struct kvm *kvm)
4672{
4673 unsigned int i;
4674
4675 /*
4676 * Unpin any mmu pages first.
4677 */
4678 for (i = 0; i < KVM_MAX_VCPUS; ++i)
4679 if (kvm->vcpus[i])
4680 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
4681 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
4682 if (kvm->vcpus[i]) {
4683 kvm_arch_vcpu_free(kvm->vcpus[i]);
4684 kvm->vcpus[i] = NULL;
4685 }
4686 }
4687
4688}
4689
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004690void kvm_arch_sync_events(struct kvm *kvm)
4691{
Sheng Yangba4cef32009-01-06 10:03:03 +08004692 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004693}
4694
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004695void kvm_arch_destroy_vm(struct kvm *kvm)
4696{
Sheng Yang6eb55812008-10-31 12:37:41 +08004697 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08004698 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb2007-12-14 10:17:34 +08004699 kfree(kvm->arch.vpic);
4700 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004701 kvm_free_vcpus(kvm);
4702 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02004703 if (kvm->arch.apic_access_page)
4704 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004705 if (kvm->arch.ept_identity_pagetable)
4706 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004707 kfree(kvm);
4708}
Zhang Xiantao0de10342007-11-20 16:25:04 +08004709
4710int kvm_arch_set_memory_region(struct kvm *kvm,
4711 struct kvm_userspace_memory_region *mem,
4712 struct kvm_memory_slot old,
4713 int user_alloc)
4714{
4715 int npages = mem->memory_size >> PAGE_SHIFT;
4716 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4717
4718 /*To keep backward compatibility with older userspace,
4719 *x86 needs to hanlde !user_alloc case.
4720 */
4721 if (!user_alloc) {
4722 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004723 unsigned long userspace_addr;
4724
Izik Eidus72dc67a2008-02-10 18:04:15 +02004725 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004726 userspace_addr = do_mmap(NULL, 0,
4727 npages * PAGE_SIZE,
4728 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03004729 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004730 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004731 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004732
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004733 if (IS_ERR((void *)userspace_addr))
4734 return PTR_ERR((void *)userspace_addr);
4735
4736 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4737 spin_lock(&kvm->mmu_lock);
4738 memslot->userspace_addr = userspace_addr;
4739 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004740 } else {
4741 if (!old.user_alloc && old.rmap) {
4742 int ret;
4743
Izik Eidus72dc67a2008-02-10 18:04:15 +02004744 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004745 ret = do_munmap(current->mm, old.userspace_addr,
4746 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004747 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004748 if (ret < 0)
4749 printk(KERN_WARNING
4750 "kvm_vm_ioctl_set_memory_region: "
4751 "failed to munmap memory\n");
4752 }
4753 }
4754 }
4755
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004756 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004757 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004758 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4759 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4760 }
4761
4762 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004763 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004764 kvm_flush_remote_tlbs(kvm);
4765
4766 return 0;
4767}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004768
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004769void kvm_arch_flush_shadow(struct kvm *kvm)
4770{
4771 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004772 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004773}
4774
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004775int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4776{
Avi Kivitya4535292008-04-13 17:54:35 +03004777 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Jan Kiszka0496fbb2008-09-26 09:30:53 +02004778 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
4779 || vcpu->arch.nmi_pending;
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004780}
Zhang Xiantao57361992007-12-17 14:21:40 +08004781
Zhang Xiantao57361992007-12-17 14:21:40 +08004782void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4783{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004784 int me;
4785 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08004786
4787 if (waitqueue_active(&vcpu->wq)) {
4788 wake_up_interruptible(&vcpu->wq);
4789 ++vcpu->stat.halt_wakeup;
4790 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004791
4792 me = get_cpu();
4793 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
4794 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
4795 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004796 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004797}
Gleb Natapov78646122009-03-23 12:12:11 +02004798
4799int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
4800{
4801 return kvm_x86_ops->interrupt_allowed(vcpu);
4802}