blob: 132c5100d4f4f59607882fec59c426f6ecebb9f8 [file] [log] [blame]
Carsten Otte043405e2007-10-10 17:16:19 +02001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * derived from drivers/kvm/kvm_main.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03007 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
Carsten Otte043405e2007-10-10 17:16:19 +02009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030013 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
Carsten Otte043405e2007-10-10 17:16:19 +020015 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 *
19 */
20
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080024#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020025#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030026#include "kvm_cache_regs.h"
Avi Kivity26eef702008-07-03 14:59:22 +030027#include "x86.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020028
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#include <linux/clocksource.h>
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +030030#include <linux/interrupt.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020031#include <linux/kvm.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010034#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080035#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050036#include <linux/highmem.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010037#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030038#include <linux/intel-iommu.h>
Gerd Hoffmannc8076602009-02-04 17:52:04 +010039#include <linux/cpufreq.h>
Avi Kivityaec51dc2009-07-01 16:01:02 +030040#include <trace/events/kvm.h>
41#undef TRACE_INCLUDE_FILE
Marcelo Tosatti229456f2009-06-17 09:22:14 -030042#define CREATE_TRACE_POINTS
43#include "trace.h"
Carsten Otte043405e2007-10-10 17:16:19 +020044
45#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080046#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020047#include <asm/desc.h>
Sheng Yang0bed3b52008-10-09 16:01:54 +080048#include <asm/mtrr.h>
Huang Ying890ca9a2009-05-11 16:48:15 +080049#include <asm/mce.h>
Carsten Otte043405e2007-10-10 17:16:19 +020050
Carsten Otte313a3dc2007-10-11 19:16:52 +020051#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010052#define CR0_RESERVED_BITS \
53 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
54 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
55 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
56#define CR4_RESERVED_BITS \
57 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
58 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
59 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
60 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
61
62#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Huang Ying890ca9a2009-05-11 16:48:15 +080063
64#define KVM_MAX_MCE_BANKS 32
65#define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
66
Joerg Roedel50a37eb2008-01-31 14:57:38 +010067/* EFER defaults:
68 * - enable syscall per default because its emulated by KVM
69 * - enable LME and LMA per default on 64 bit KVM
70 */
71#ifdef CONFIG_X86_64
72static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
73#else
74static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
75#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020076
Avi Kivityba1389b2007-11-18 16:24:12 +020077#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
78#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050079
Avi Kivity674eea02008-02-11 18:37:23 +020080static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
81 struct kvm_cpuid_entry2 __user *entries);
82
Zhang Xiantao97896d02007-11-14 20:09:30 +080083struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030084EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080085
Andre Przywaraed85c062009-06-25 12:36:49 +020086int ignore_msrs = 0;
87module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
88
Hollis Blanchard417bc302007-10-31 17:24:23 -050089struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020090 { "pf_fixed", VCPU_STAT(pf_fixed) },
91 { "pf_guest", VCPU_STAT(pf_guest) },
92 { "tlb_flush", VCPU_STAT(tlb_flush) },
93 { "invlpg", VCPU_STAT(invlpg) },
94 { "exits", VCPU_STAT(exits) },
95 { "io_exits", VCPU_STAT(io_exits) },
96 { "mmio_exits", VCPU_STAT(mmio_exits) },
97 { "signal_exits", VCPU_STAT(signal_exits) },
98 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +080099 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200100 { "halt_exits", VCPU_STAT(halt_exits) },
101 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +0530102 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +0200103 { "request_irq", VCPU_STAT(request_irq_exits) },
104 { "irq_exits", VCPU_STAT(irq_exits) },
105 { "host_state_reload", VCPU_STAT(host_state_reload) },
106 { "efer_reload", VCPU_STAT(efer_reload) },
107 { "fpu_reload", VCPU_STAT(fpu_reload) },
108 { "insn_emulation", VCPU_STAT(insn_emulation) },
109 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivityfa89a812008-09-01 15:57:51 +0300110 { "irq_injections", VCPU_STAT(irq_injections) },
Jan Kiszkac4abb7c2008-09-26 09:30:55 +0200111 { "nmi_injections", VCPU_STAT(nmi_injections) },
Avi Kivity4cee5762007-11-18 16:37:07 +0200112 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
113 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
114 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
115 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
116 { "mmu_flooded", VM_STAT(mmu_flooded) },
117 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200118 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300119 { "mmu_unsync", VM_STAT(mmu_unsync) },
Avi Kivity0f74a242007-11-20 23:01:14 +0200120 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300121 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -0500122 { NULL }
123};
124
Carsten Otte5fb76f92007-10-29 16:08:51 +0100125unsigned long segment_base(u16 selector)
126{
127 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200128 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100129 unsigned long table_base;
130 unsigned long v;
131
132 if (selector == 0)
133 return 0;
134
Akinobu Mitab792c342009-07-19 00:00:01 +0900135 kvm_get_gdt(&gdt);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100136 table_base = gdt.base;
137
138 if (selector & 4) { /* from ldt */
Akinobu Mitab792c342009-07-19 00:00:01 +0900139 u16 ldt_selector = kvm_read_ldt();
Carsten Otte5fb76f92007-10-29 16:08:51 +0100140
Carsten Otte5fb76f92007-10-29 16:08:51 +0100141 table_base = segment_base(ldt_selector);
142 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200143 d = (struct desc_struct *)(table_base + (selector & ~7));
Akinobu Mita46a359e2009-07-18 23:58:32 +0900144 v = get_desc_base(d);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100145#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200146 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
147 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100148#endif
149 return v;
150}
151EXPORT_SYMBOL_GPL(segment_base);
152
Carsten Otte6866b832007-10-29 16:09:10 +0100153u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
154{
155 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800156 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100157 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800158 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100159}
160EXPORT_SYMBOL_GPL(kvm_get_apic_base);
161
162void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
163{
164 /* TODO: reserve bits check */
165 if (irqchip_in_kernel(vcpu->kvm))
166 kvm_lapic_set_base(vcpu, data);
167 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800168 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100169}
170EXPORT_SYMBOL_GPL(kvm_set_apic_base);
171
Avi Kivity298101d2007-11-25 13:41:11 +0200172void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
173{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800174 WARN_ON(vcpu->arch.exception.pending);
175 vcpu->arch.exception.pending = true;
176 vcpu->arch.exception.has_error_code = false;
177 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200178}
179EXPORT_SYMBOL_GPL(kvm_queue_exception);
180
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200181void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
182 u32 error_code)
183{
184 ++vcpu->stat.pf_guest;
Alexander Grafd8017472008-11-25 20:17:11 +0100185
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100186 if (vcpu->arch.exception.pending) {
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300187 switch(vcpu->arch.exception.nr) {
188 case DF_VECTOR:
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100189 /* triple fault -> shutdown */
190 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
Gleb Natapov6edf14d2009-06-11 11:26:11 +0300191 return;
192 case PF_VECTOR:
193 vcpu->arch.exception.nr = DF_VECTOR;
194 vcpu->arch.exception.error_code = 0;
195 return;
196 default:
197 /* replace previous exception with a new one in a hope
198 that instruction re-execution will regenerate lost
199 exception */
200 vcpu->arch.exception.pending = false;
201 break;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100202 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200203 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800204 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200205 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
206}
207
Sheng Yang3419ffc2008-05-15 09:52:48 +0800208void kvm_inject_nmi(struct kvm_vcpu *vcpu)
209{
210 vcpu->arch.nmi_pending = 1;
211}
212EXPORT_SYMBOL_GPL(kvm_inject_nmi);
213
Avi Kivity298101d2007-11-25 13:41:11 +0200214void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
215{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800216 WARN_ON(vcpu->arch.exception.pending);
217 vcpu->arch.exception.pending = true;
218 vcpu->arch.exception.has_error_code = true;
219 vcpu->arch.exception.nr = nr;
220 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200221}
222EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
223
Carsten Ottea03490e2007-10-29 16:09:35 +0100224/*
225 * Load the pae pdptrs. Return true is they are all valid.
226 */
227int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
228{
229 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
230 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
231 int i;
232 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800233 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100234
Carsten Ottea03490e2007-10-29 16:09:35 +0100235 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
236 offset * sizeof(u64), sizeof(pdpte));
237 if (ret < 0) {
238 ret = 0;
239 goto out;
240 }
241 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
Avi Kivity43a37952009-06-10 14:12:05 +0300242 if (is_present_gpte(pdpte[i]) &&
Dong, Eddie20c466b2009-03-31 23:03:45 +0800243 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100244 ret = 0;
245 goto out;
246 }
247 }
248 ret = 1;
249
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800250 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300251 __set_bit(VCPU_EXREG_PDPTR,
252 (unsigned long *)&vcpu->arch.regs_avail);
253 __set_bit(VCPU_EXREG_PDPTR,
254 (unsigned long *)&vcpu->arch.regs_dirty);
Carsten Ottea03490e2007-10-29 16:09:35 +0100255out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100256
257 return ret;
258}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100259EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100260
Avi Kivityd835dfe2007-11-21 02:57:59 +0200261static bool pdptrs_changed(struct kvm_vcpu *vcpu)
262{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800263 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200264 bool changed = true;
265 int r;
266
267 if (is_long_mode(vcpu) || !is_pae(vcpu))
268 return false;
269
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300270 if (!test_bit(VCPU_EXREG_PDPTR,
271 (unsigned long *)&vcpu->arch.regs_avail))
272 return true;
273
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800274 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200275 if (r < 0)
276 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800277 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200278out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200279
280 return changed;
281}
282
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200283void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100284{
285 if (cr0 & CR0_RESERVED_BITS) {
286 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800287 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200288 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100289 return;
290 }
291
292 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
293 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200294 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100295 return;
296 }
297
298 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
299 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
300 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200301 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100302 return;
303 }
304
305 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
306#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800307 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100308 int cs_db, cs_l;
309
310 if (!is_pae(vcpu)) {
311 printk(KERN_DEBUG "set_cr0: #GP, start paging "
312 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200313 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100314 return;
315 }
316 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
317 if (cs_l) {
318 printk(KERN_DEBUG "set_cr0: #GP, start paging "
319 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200320 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100321 return;
322
323 }
324 } else
325#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800326 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100327 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
328 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200329 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100330 return;
331 }
332
333 }
334
335 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800336 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100337
Carsten Ottea03490e2007-10-29 16:09:35 +0100338 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100339 return;
340}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200341EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100342
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200343void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100344{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200345 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Carsten Ottea03490e2007-10-29 16:09:35 +0100346}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200347EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100348
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200349void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100350{
Avi Kivitya2edf572009-05-24 22:19:00 +0300351 unsigned long old_cr4 = vcpu->arch.cr4;
352 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
353
Carsten Ottea03490e2007-10-29 16:09:35 +0100354 if (cr4 & CR4_RESERVED_BITS) {
355 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200356 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100357 return;
358 }
359
360 if (is_long_mode(vcpu)) {
361 if (!(cr4 & X86_CR4_PAE)) {
362 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
363 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200364 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100365 return;
366 }
Avi Kivitya2edf572009-05-24 22:19:00 +0300367 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
368 && ((cr4 ^ old_cr4) & pdptr_bits)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100370 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200371 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100372 return;
373 }
374
375 if (cr4 & X86_CR4_VMXE) {
376 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200377 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100378 return;
379 }
380 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800381 vcpu->arch.cr4 = cr4;
Avi Kivity5a41acc2009-01-11 17:19:35 +0200382 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
Carsten Ottea03490e2007-10-29 16:09:35 +0100383 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100384}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200385EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100386
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200387void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100388{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800389 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -0300390 kvm_mmu_sync_roots(vcpu);
Avi Kivityd835dfe2007-11-21 02:57:59 +0200391 kvm_mmu_flush_tlb(vcpu);
392 return;
393 }
394
Carsten Ottea03490e2007-10-29 16:09:35 +0100395 if (is_long_mode(vcpu)) {
396 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
397 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200398 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100399 return;
400 }
401 } else {
402 if (is_pae(vcpu)) {
403 if (cr3 & CR3_PAE_RESERVED_BITS) {
404 printk(KERN_DEBUG
405 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200406 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100407 return;
408 }
409 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
410 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
411 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200412 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100413 return;
414 }
415 }
416 /*
417 * We don't check reserved bits in nonpae mode, because
418 * this isn't enforced, and VMware depends on this.
419 */
420 }
421
Carsten Ottea03490e2007-10-29 16:09:35 +0100422 /*
423 * Does the new cr3 value map to physical memory? (Note, we
424 * catch an invalid cr3 even in real-mode, because it would
425 * cause trouble later on when we turn on paging anyway.)
426 *
427 * A real CPU would silently accept an invalid cr3 and would
428 * attempt to use it - with largely undefined (and often hard
429 * to debug) behavior on the guest side.
430 */
431 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200432 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100433 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800434 vcpu->arch.cr3 = cr3;
435 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100436 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100437}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200438EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100439
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200440void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100441{
442 if (cr8 & CR8_RESERVED_BITS) {
443 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200444 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100445 return;
446 }
447 if (irqchip_in_kernel(vcpu->kvm))
448 kvm_lapic_set_tpr(vcpu, cr8);
449 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800450 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100451}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200452EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100453
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200454unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100455{
456 if (irqchip_in_kernel(vcpu->kvm))
457 return kvm_lapic_get_cr8(vcpu);
458 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800459 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100460}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200461EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100462
Alexander Grafd8017472008-11-25 20:17:11 +0100463static inline u32 bit(int bitno)
464{
465 return 1 << (bitno & 31);
466}
467
Carsten Otte043405e2007-10-10 17:16:19 +0200468/*
469 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
470 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
471 *
472 * This list is modified at module load time to reflect the
473 * capabilities of the host cpu.
474 */
475static u32 msrs_to_save[] = {
476 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
477 MSR_K6_STAR,
478#ifdef CONFIG_X86_64
479 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
480#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530481 MSR_IA32_TSC, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Alexander Grafb286d5d2008-11-25 20:17:05 +0100482 MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
Carsten Otte043405e2007-10-10 17:16:19 +0200483};
484
485static unsigned num_msrs_to_save;
486
487static u32 emulated_msrs[] = {
488 MSR_IA32_MISC_ENABLE,
489};
490
Carsten Otte15c4a642007-10-30 18:44:17 +0100491static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
492{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100493 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100494 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
495 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200496 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100497 return;
498 }
499
500 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800501 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100502 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200503 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100504 return;
505 }
506
Alexander Graf1b2fd702009-02-02 16:23:51 +0100507 if (efer & EFER_FFXSR) {
508 struct kvm_cpuid_entry2 *feat;
509
510 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
511 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
512 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
513 kvm_inject_gp(vcpu, 0);
514 return;
515 }
516 }
517
Alexander Grafd8017472008-11-25 20:17:11 +0100518 if (efer & EFER_SVME) {
519 struct kvm_cpuid_entry2 *feat;
520
521 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
522 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
523 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
524 kvm_inject_gp(vcpu, 0);
525 return;
526 }
527 }
528
Carsten Otte15c4a642007-10-30 18:44:17 +0100529 kvm_x86_ops->set_efer(vcpu, efer);
530
531 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800532 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100533
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800534 vcpu->arch.shadow_efer = efer;
Avi Kivity9645bb562009-03-31 11:31:54 +0300535
536 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
537 kvm_mmu_reset_context(vcpu);
Carsten Otte15c4a642007-10-30 18:44:17 +0100538}
539
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100540void kvm_enable_efer_bits(u64 mask)
541{
542 efer_reserved_bits &= ~mask;
543}
544EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
545
546
Carsten Otte15c4a642007-10-30 18:44:17 +0100547/*
548 * Writes msr value into into the appropriate "register".
549 * Returns 0 on success, non-0 otherwise.
550 * Assumes vcpu_load() was already called.
551 */
552int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
553{
554 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
555}
556
Carsten Otte313a3dc2007-10-11 19:16:52 +0200557/*
558 * Adapt set_msr() to msr_io()'s calling convention
559 */
560static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
561{
562 return kvm_set_msr(vcpu, index, *data);
563}
564
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200565static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
566{
567 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200568 struct pvclock_wall_clock wc;
569 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200570
571 if (!wall_clock)
572 return;
573
574 version++;
575
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200576 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
577
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200578 /*
579 * The guest calculates current wall clock time by adding
580 * system time (updated by kvm_write_guest_time below) to the
581 * wall clock specified here. guest system time equals host
582 * system time for us, thus we must fill in host boot time here.
583 */
584 now = current_kernel_time();
585 ktime_get_ts(&sys);
586 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
587
588 wc.sec = boot.tv_sec;
589 wc.nsec = boot.tv_nsec;
590 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200591
592 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
593
594 version++;
595 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200596}
597
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200598static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
599{
600 uint32_t quotient, remainder;
601
602 /* Don't try to replace with do_div(), this one calculates
603 * "(dividend << 32) / divisor" */
604 __asm__ ( "divl %4"
605 : "=a" (quotient), "=d" (remainder)
606 : "0" (0), "1" (dividend), "r" (divisor) );
607 return quotient;
608}
609
610static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
611{
612 uint64_t nsecs = 1000000000LL;
613 int32_t shift = 0;
614 uint64_t tps64;
615 uint32_t tps32;
616
617 tps64 = tsc_khz * 1000LL;
618 while (tps64 > nsecs*2) {
619 tps64 >>= 1;
620 shift--;
621 }
622
623 tps32 = (uint32_t)tps64;
624 while (tps32 <= (uint32_t)nsecs) {
625 tps32 <<= 1;
626 shift++;
627 }
628
629 hv_clock->tsc_shift = shift;
630 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
631
632 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700633 __func__, tsc_khz, hv_clock->tsc_shift,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200634 hv_clock->tsc_to_system_mul);
635}
636
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100637static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
638
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200639static void kvm_write_guest_time(struct kvm_vcpu *v)
640{
641 struct timespec ts;
642 unsigned long flags;
643 struct kvm_vcpu_arch *vcpu = &v->arch;
644 void *shared_kaddr;
Avi Kivity463656c2009-04-12 15:49:07 +0300645 unsigned long this_tsc_khz;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200646
647 if ((!vcpu->time_page))
648 return;
649
Avi Kivity463656c2009-04-12 15:49:07 +0300650 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
651 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
652 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
653 vcpu->hv_clock_tsc_khz = this_tsc_khz;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200654 }
Avi Kivity463656c2009-04-12 15:49:07 +0300655 put_cpu_var(cpu_tsc_khz);
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200656
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200657 /* Keep irq disabled to prevent changes to the clock */
658 local_irq_save(flags);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530659 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200660 ktime_get_ts(&ts);
661 local_irq_restore(flags);
662
663 /* With all the info we got, fill in the values */
664
665 vcpu->hv_clock.system_time = ts.tv_nsec +
666 (NSEC_PER_SEC * (u64)ts.tv_sec);
667 /*
668 * The interface expects us to write an even number signaling that the
669 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200670 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200671 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200672 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200673
674 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
675
676 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200677 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200678
679 kunmap_atomic(shared_kaddr, KM_USER0);
680
681 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
682}
683
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100684static int kvm_request_guest_time_update(struct kvm_vcpu *v)
685{
686 struct kvm_vcpu_arch *vcpu = &v->arch;
687
688 if (!vcpu->time_page)
689 return 0;
690 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
691 return 1;
692}
693
Avi Kivity9ba075a2008-05-26 20:06:35 +0300694static bool msr_mtrr_valid(unsigned msr)
695{
696 switch (msr) {
697 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
698 case MSR_MTRRfix64K_00000:
699 case MSR_MTRRfix16K_80000:
700 case MSR_MTRRfix16K_A0000:
701 case MSR_MTRRfix4K_C0000:
702 case MSR_MTRRfix4K_C8000:
703 case MSR_MTRRfix4K_D0000:
704 case MSR_MTRRfix4K_D8000:
705 case MSR_MTRRfix4K_E0000:
706 case MSR_MTRRfix4K_E8000:
707 case MSR_MTRRfix4K_F0000:
708 case MSR_MTRRfix4K_F8000:
709 case MSR_MTRRdefType:
710 case MSR_IA32_CR_PAT:
711 return true;
712 case 0x2f8:
713 return true;
714 }
715 return false;
716}
717
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300718static bool valid_pat_type(unsigned t)
719{
720 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
721}
722
723static bool valid_mtrr_type(unsigned t)
724{
725 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
726}
727
728static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
729{
730 int i;
731
732 if (!msr_mtrr_valid(msr))
733 return false;
734
735 if (msr == MSR_IA32_CR_PAT) {
736 for (i = 0; i < 8; i++)
737 if (!valid_pat_type((data >> (i * 8)) & 0xff))
738 return false;
739 return true;
740 } else if (msr == MSR_MTRRdefType) {
741 if (data & ~0xcff)
742 return false;
743 return valid_mtrr_type(data & 0xff);
744 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
745 for (i = 0; i < 8 ; i++)
746 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
747 return false;
748 return true;
749 }
750
751 /* variable MTRRs */
752 return valid_mtrr_type(data & 0xff);
753}
754
Avi Kivity9ba075a2008-05-26 20:06:35 +0300755static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
756{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800757 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
758
Marcelo Tosattid6289b92009-06-22 15:27:56 -0300759 if (!mtrr_valid(vcpu, msr, data))
Avi Kivity9ba075a2008-05-26 20:06:35 +0300760 return 1;
761
Sheng Yang0bed3b52008-10-09 16:01:54 +0800762 if (msr == MSR_MTRRdefType) {
763 vcpu->arch.mtrr_state.def_type = data;
764 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
765 } else if (msr == MSR_MTRRfix64K_00000)
766 p[0] = data;
767 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
768 p[1 + msr - MSR_MTRRfix16K_80000] = data;
769 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
770 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
771 else if (msr == MSR_IA32_CR_PAT)
772 vcpu->arch.pat = data;
773 else { /* Variable MTRRs */
774 int idx, is_mtrr_mask;
775 u64 *pt;
776
777 idx = (msr - 0x200) / 2;
778 is_mtrr_mask = msr - 0x200 - 2 * idx;
779 if (!is_mtrr_mask)
780 pt =
781 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
782 else
783 pt =
784 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
785 *pt = data;
786 }
787
788 kvm_mmu_reset_context(vcpu);
Avi Kivity9ba075a2008-05-26 20:06:35 +0300789 return 0;
790}
Carsten Otte15c4a642007-10-30 18:44:17 +0100791
Huang Ying890ca9a2009-05-11 16:48:15 +0800792static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
793{
794 u64 mcg_cap = vcpu->arch.mcg_cap;
795 unsigned bank_num = mcg_cap & 0xff;
796
797 switch (msr) {
798 case MSR_IA32_MCG_STATUS:
799 vcpu->arch.mcg_status = data;
800 break;
801 case MSR_IA32_MCG_CTL:
802 if (!(mcg_cap & MCG_CTL_P))
803 return 1;
804 if (data != 0 && data != ~(u64)0)
805 return -1;
806 vcpu->arch.mcg_ctl = data;
807 break;
808 default:
809 if (msr >= MSR_IA32_MC0_CTL &&
810 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
811 u32 offset = msr - MSR_IA32_MC0_CTL;
812 /* only 0 or all 1s can be written to IA32_MCi_CTL */
813 if ((offset & 0x3) == 0 &&
814 data != 0 && data != ~(u64)0)
815 return -1;
816 vcpu->arch.mce_banks[offset] = data;
817 break;
818 }
819 return 1;
820 }
821 return 0;
822}
823
Carsten Otte15c4a642007-10-30 18:44:17 +0100824int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
825{
826 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100827 case MSR_EFER:
828 set_efer(vcpu, data);
829 break;
Andre Przywara8f1589d2009-06-24 12:44:33 +0200830 case MSR_K7_HWCR:
831 data &= ~(u64)0x40; /* ignore flush filter disable */
832 if (data != 0) {
833 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
834 data);
835 return 1;
836 }
837 break;
Andre Przywaraf7c6d142009-07-02 15:04:14 +0200838 case MSR_FAM10H_MMIO_CONF_BASE:
839 if (data != 0) {
840 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
841 "0x%llx\n", data);
842 return 1;
843 }
844 break;
Andre Przywarac323c0e2009-06-24 15:37:05 +0200845 case MSR_AMD64_NB_CFG:
846 break;
Alexander Grafb5e2fec2008-07-22 08:00:45 +0200847 case MSR_IA32_DEBUGCTLMSR:
848 if (!data) {
849 /* We support the non-activated case already */
850 break;
851 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
852 /* Values other than LBR and BTF are vendor-specific,
853 thus reserved and should throw a #GP */
854 return 1;
855 }
856 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
857 __func__, data);
858 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100859 case MSR_IA32_UCODE_REV:
860 case MSR_IA32_UCODE_WRITE:
Avi Kivity61a6bd62008-12-29 17:32:28 +0200861 case MSR_VM_HSAVE_PA:
Andre Przywara6098ca92009-07-03 16:00:14 +0200862 case MSR_AMD64_PATCH_LOADER:
Carsten Otte15c4a642007-10-30 18:44:17 +0100863 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300864 case 0x200 ... 0x2ff:
865 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100866 case MSR_IA32_APICBASE:
867 kvm_set_apic_base(vcpu, data);
868 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +0300869 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
870 return kvm_x2apic_msr_write(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100871 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800872 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100873 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200874 case MSR_KVM_WALL_CLOCK:
875 vcpu->kvm->arch.wall_clock = data;
876 kvm_write_wall_clock(vcpu->kvm, data);
877 break;
878 case MSR_KVM_SYSTEM_TIME: {
879 if (vcpu->arch.time_page) {
880 kvm_release_page_dirty(vcpu->arch.time_page);
881 vcpu->arch.time_page = NULL;
882 }
883
884 vcpu->arch.time = data;
885
886 /* we verify if the enable bit is set... */
887 if (!(data & 1))
888 break;
889
890 /* ...but clean it before doing the actual write */
891 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
892
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200893 vcpu->arch.time_page =
894 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200895
896 if (is_error_page(vcpu->arch.time_page)) {
897 kvm_release_page_clean(vcpu->arch.time_page);
898 vcpu->arch.time_page = NULL;
899 }
900
Gerd Hoffmannc8076602009-02-04 17:52:04 +0100901 kvm_request_guest_time_update(vcpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200902 break;
903 }
Huang Ying890ca9a2009-05-11 16:48:15 +0800904 case MSR_IA32_MCG_CTL:
905 case MSR_IA32_MCG_STATUS:
906 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
907 return set_msr_mce(vcpu, msr, data);
Andre Przywara71db6022009-06-12 22:01:29 +0200908
909 /* Performance counters are not protected by a CPUID bit,
910 * so we should check all of them in the generic path for the sake of
911 * cross vendor migration.
912 * Writing a zero into the event select MSRs disables them,
913 * which we perfectly emulate ;-). Any other value should be at least
914 * reported, some guests depend on them.
915 */
916 case MSR_P6_EVNTSEL0:
917 case MSR_P6_EVNTSEL1:
918 case MSR_K7_EVNTSEL0:
919 case MSR_K7_EVNTSEL1:
920 case MSR_K7_EVNTSEL2:
921 case MSR_K7_EVNTSEL3:
922 if (data != 0)
923 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
924 "0x%x data 0x%llx\n", msr, data);
925 break;
926 /* at least RHEL 4 unconditionally writes to the perfctr registers,
927 * so we ignore writes to make it happy.
928 */
929 case MSR_P6_PERFCTR0:
930 case MSR_P6_PERFCTR1:
931 case MSR_K7_PERFCTR0:
932 case MSR_K7_PERFCTR1:
933 case MSR_K7_PERFCTR2:
934 case MSR_K7_PERFCTR3:
935 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
936 "0x%x data 0x%llx\n", msr, data);
937 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100938 default:
Andre Przywaraed85c062009-06-25 12:36:49 +0200939 if (!ignore_msrs) {
940 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
941 msr, data);
942 return 1;
943 } else {
944 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
945 msr, data);
946 break;
947 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100948 }
949 return 0;
950}
951EXPORT_SYMBOL_GPL(kvm_set_msr_common);
952
953
954/*
955 * Reads an msr value (of 'msr_index') into 'pdata'.
956 * Returns 0 on success, non-0 otherwise.
957 * Assumes vcpu_load() was already called.
958 */
959int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
960{
961 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
962}
963
Avi Kivity9ba075a2008-05-26 20:06:35 +0300964static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
965{
Sheng Yang0bed3b52008-10-09 16:01:54 +0800966 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
967
Avi Kivity9ba075a2008-05-26 20:06:35 +0300968 if (!msr_mtrr_valid(msr))
969 return 1;
970
Sheng Yang0bed3b52008-10-09 16:01:54 +0800971 if (msr == MSR_MTRRdefType)
972 *pdata = vcpu->arch.mtrr_state.def_type +
973 (vcpu->arch.mtrr_state.enabled << 10);
974 else if (msr == MSR_MTRRfix64K_00000)
975 *pdata = p[0];
976 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
977 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
978 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
979 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
980 else if (msr == MSR_IA32_CR_PAT)
981 *pdata = vcpu->arch.pat;
982 else { /* Variable MTRRs */
983 int idx, is_mtrr_mask;
984 u64 *pt;
985
986 idx = (msr - 0x200) / 2;
987 is_mtrr_mask = msr - 0x200 - 2 * idx;
988 if (!is_mtrr_mask)
989 pt =
990 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
991 else
992 pt =
993 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
994 *pdata = *pt;
995 }
996
Avi Kivity9ba075a2008-05-26 20:06:35 +0300997 return 0;
998}
999
Huang Ying890ca9a2009-05-11 16:48:15 +08001000static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1001{
1002 u64 data;
1003 u64 mcg_cap = vcpu->arch.mcg_cap;
1004 unsigned bank_num = mcg_cap & 0xff;
1005
1006 switch (msr) {
1007 case MSR_IA32_P5_MC_ADDR:
1008 case MSR_IA32_P5_MC_TYPE:
1009 data = 0;
1010 break;
1011 case MSR_IA32_MCG_CAP:
1012 data = vcpu->arch.mcg_cap;
1013 break;
1014 case MSR_IA32_MCG_CTL:
1015 if (!(mcg_cap & MCG_CTL_P))
1016 return 1;
1017 data = vcpu->arch.mcg_ctl;
1018 break;
1019 case MSR_IA32_MCG_STATUS:
1020 data = vcpu->arch.mcg_status;
1021 break;
1022 default:
1023 if (msr >= MSR_IA32_MC0_CTL &&
1024 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1025 u32 offset = msr - MSR_IA32_MC0_CTL;
1026 data = vcpu->arch.mce_banks[offset];
1027 break;
1028 }
1029 return 1;
1030 }
1031 *pdata = data;
1032 return 0;
1033}
1034
Carsten Otte15c4a642007-10-30 18:44:17 +01001035int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1036{
1037 u64 data;
1038
1039 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +01001040 case MSR_IA32_PLATFORM_ID:
Carsten Otte15c4a642007-10-30 18:44:17 +01001041 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +01001042 case MSR_IA32_EBL_CR_POWERON:
Alexander Grafb5e2fec2008-07-22 08:00:45 +02001043 case MSR_IA32_DEBUGCTLMSR:
1044 case MSR_IA32_LASTBRANCHFROMIP:
1045 case MSR_IA32_LASTBRANCHTOIP:
1046 case MSR_IA32_LASTINTFROMIP:
1047 case MSR_IA32_LASTINTTOIP:
Jaswinder Singh Rajput60af2ec2009-05-14 11:00:10 +05301048 case MSR_K8_SYSCFG:
1049 case MSR_K7_HWCR:
Avi Kivity61a6bd62008-12-29 17:32:28 +02001050 case MSR_VM_HSAVE_PA:
Amit Shah1f3ee612009-06-30 16:24:28 +05301051 case MSR_P6_PERFCTR0:
1052 case MSR_P6_PERFCTR1:
Amit Shah7fe29e02009-03-20 12:39:00 +05301053 case MSR_P6_EVNTSEL0:
1054 case MSR_P6_EVNTSEL1:
Amit Shah9e699622009-06-15 13:25:34 +05301055 case MSR_K7_EVNTSEL0:
Amit Shah1f3ee612009-06-30 16:24:28 +05301056 case MSR_K7_PERFCTR0:
Andre Przywara1fdbd482009-06-24 12:44:34 +02001057 case MSR_K8_INT_PENDING_MSG:
Andre Przywarac323c0e2009-06-24 15:37:05 +02001058 case MSR_AMD64_NB_CFG:
Andre Przywaraf7c6d142009-07-02 15:04:14 +02001059 case MSR_FAM10H_MMIO_CONF_BASE:
Carsten Otte15c4a642007-10-30 18:44:17 +01001060 data = 0;
1061 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +03001062 case MSR_MTRRcap:
1063 data = 0x500 | KVM_NR_VAR_MTRR;
1064 break;
1065 case 0x200 ... 0x2ff:
1066 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001067 case 0xcd: /* fsb frequency */
1068 data = 3;
1069 break;
1070 case MSR_IA32_APICBASE:
1071 data = kvm_get_apic_base(vcpu);
1072 break;
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001073 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1074 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1075 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001076 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001077 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +01001078 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +01001079 case MSR_IA32_PERF_STATUS:
1080 /* TSC increment by tick */
1081 data = 1000ULL;
1082 /* CPU multiplier */
1083 data |= (((uint64_t)4ULL) << 40);
1084 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001085 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001086 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +01001087 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -02001088 case MSR_KVM_WALL_CLOCK:
1089 data = vcpu->kvm->arch.wall_clock;
1090 break;
1091 case MSR_KVM_SYSTEM_TIME:
1092 data = vcpu->arch.time;
1093 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001094 case MSR_IA32_P5_MC_ADDR:
1095 case MSR_IA32_P5_MC_TYPE:
1096 case MSR_IA32_MCG_CAP:
1097 case MSR_IA32_MCG_CTL:
1098 case MSR_IA32_MCG_STATUS:
1099 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1100 return get_msr_mce(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +01001101 default:
Andre Przywaraed85c062009-06-25 12:36:49 +02001102 if (!ignore_msrs) {
1103 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1104 return 1;
1105 } else {
1106 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1107 data = 0;
1108 }
1109 break;
Carsten Otte15c4a642007-10-30 18:44:17 +01001110 }
1111 *pdata = data;
1112 return 0;
1113}
1114EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1115
Carsten Otte313a3dc2007-10-11 19:16:52 +02001116/*
1117 * Read or write a bunch of msrs. All parameters are kernel addresses.
1118 *
1119 * @return number of msrs set successfully.
1120 */
1121static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1122 struct kvm_msr_entry *entries,
1123 int (*do_msr)(struct kvm_vcpu *vcpu,
1124 unsigned index, u64 *data))
1125{
1126 int i;
1127
1128 vcpu_load(vcpu);
1129
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001130 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001131 for (i = 0; i < msrs->nmsrs; ++i)
1132 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1133 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001134 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001135
1136 vcpu_put(vcpu);
1137
1138 return i;
1139}
1140
1141/*
1142 * Read or write a bunch of msrs. Parameters are user addresses.
1143 *
1144 * @return number of msrs set successfully.
1145 */
1146static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1147 int (*do_msr)(struct kvm_vcpu *vcpu,
1148 unsigned index, u64 *data),
1149 int writeback)
1150{
1151 struct kvm_msrs msrs;
1152 struct kvm_msr_entry *entries;
1153 int r, n;
1154 unsigned size;
1155
1156 r = -EFAULT;
1157 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1158 goto out;
1159
1160 r = -E2BIG;
1161 if (msrs.nmsrs >= MAX_IO_MSRS)
1162 goto out;
1163
1164 r = -ENOMEM;
1165 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1166 entries = vmalloc(size);
1167 if (!entries)
1168 goto out;
1169
1170 r = -EFAULT;
1171 if (copy_from_user(entries, user_msrs->entries, size))
1172 goto out_free;
1173
1174 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1175 if (r < 0)
1176 goto out_free;
1177
1178 r = -EFAULT;
1179 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1180 goto out_free;
1181
1182 r = n;
1183
1184out_free:
1185 vfree(entries);
1186out:
1187 return r;
1188}
1189
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001190int kvm_dev_ioctl_check_extension(long ext)
1191{
1192 int r;
1193
1194 switch (ext) {
1195 case KVM_CAP_IRQCHIP:
1196 case KVM_CAP_HLT:
1197 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001198 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +02001199 case KVM_CAP_EXT_CPUID:
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001200 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +08001201 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -05001202 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001203 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +03001204 case KVM_CAP_SYNC_MMU:
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02001205 case KVM_CAP_REINJECT_CONTROL:
Gleb Natapov49256632009-02-04 17:28:14 +02001206 case KVM_CAP_IRQ_INJECT_STATUS:
Sheng Yange56d5322009-03-12 21:45:39 +08001207 case KVM_CAP_ASSIGN_DEV_IRQ:
Gregory Haskins721eecbf2009-05-20 10:30:49 -04001208 case KVM_CAP_IRQFD:
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04001209 case KVM_CAP_IOEVENTFD:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02001210 case KVM_CAP_PIT2:
Beth Kone9f42752009-07-07 11:50:38 -04001211 case KVM_CAP_PIT_STATE2:
Sheng Yangb927a3c2009-07-21 10:42:48 +08001212 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001213 r = 1;
1214 break;
Laurent Vivier542472b2008-05-30 16:05:55 +02001215 case KVM_CAP_COALESCED_MMIO:
1216 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1217 break;
Avi Kivity774ead32007-12-26 13:57:04 +02001218 case KVM_CAP_VAPIC:
1219 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1220 break;
Avi Kivityf7252302008-02-20 11:53:16 +02001221 case KVM_CAP_NR_VCPUS:
1222 r = KVM_MAX_VCPUS;
1223 break;
Avi Kivitya988b912008-02-20 11:59:20 +02001224 case KVM_CAP_NR_MEMSLOTS:
1225 r = KVM_MEMORY_SLOTS;
1226 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05001227 case KVM_CAP_PV_MMU:
1228 r = !tdp_enabled;
1229 break;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001230 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +01001231 r = iommu_found();
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001232 break;
Huang Ying890ca9a2009-05-11 16:48:15 +08001233 case KVM_CAP_MCE:
1234 r = KVM_MAX_MCE_BANKS;
1235 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08001236 default:
1237 r = 0;
1238 break;
1239 }
1240 return r;
1241
1242}
1243
Carsten Otte043405e2007-10-10 17:16:19 +02001244long kvm_arch_dev_ioctl(struct file *filp,
1245 unsigned int ioctl, unsigned long arg)
1246{
1247 void __user *argp = (void __user *)arg;
1248 long r;
1249
1250 switch (ioctl) {
1251 case KVM_GET_MSR_INDEX_LIST: {
1252 struct kvm_msr_list __user *user_msr_list = argp;
1253 struct kvm_msr_list msr_list;
1254 unsigned n;
1255
1256 r = -EFAULT;
1257 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1258 goto out;
1259 n = msr_list.nmsrs;
1260 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1261 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1262 goto out;
1263 r = -E2BIG;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001264 if (n < msr_list.nmsrs)
Carsten Otte043405e2007-10-10 17:16:19 +02001265 goto out;
1266 r = -EFAULT;
1267 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1268 num_msrs_to_save * sizeof(u32)))
1269 goto out;
Jan Kiszkae125e7b2009-07-02 21:45:47 +02001270 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
Carsten Otte043405e2007-10-10 17:16:19 +02001271 &emulated_msrs,
1272 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1273 goto out;
1274 r = 0;
1275 break;
1276 }
Avi Kivity674eea02008-02-11 18:37:23 +02001277 case KVM_GET_SUPPORTED_CPUID: {
1278 struct kvm_cpuid2 __user *cpuid_arg = argp;
1279 struct kvm_cpuid2 cpuid;
1280
1281 r = -EFAULT;
1282 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1283 goto out;
1284 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001285 cpuid_arg->entries);
Avi Kivity674eea02008-02-11 18:37:23 +02001286 if (r)
1287 goto out;
1288
1289 r = -EFAULT;
1290 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1291 goto out;
1292 r = 0;
1293 break;
1294 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001295 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1296 u64 mce_cap;
1297
1298 mce_cap = KVM_MCE_CAP_SUPPORTED;
1299 r = -EFAULT;
1300 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1301 goto out;
1302 r = 0;
1303 break;
1304 }
Carsten Otte043405e2007-10-10 17:16:19 +02001305 default:
1306 r = -EINVAL;
1307 }
1308out:
1309 return r;
1310}
1311
Carsten Otte313a3dc2007-10-11 19:16:52 +02001312void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1313{
1314 kvm_x86_ops->vcpu_load(vcpu, cpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01001315 kvm_request_guest_time_update(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001316}
1317
1318void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1319{
1320 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +02001321 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001322}
1323
Dan Kenigsberg07716712007-11-21 17:10:04 +02001324static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001325{
Avi Kivitye286e862009-05-03 18:50:55 +03001326 unsigned long long efer = 0;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001327
Avi Kivitye286e862009-05-03 18:50:55 +03001328 rdmsrl_safe(MSR_EFER, &efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001329 return efer & EFER_NX;
1330}
1331
1332static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1333{
1334 int i;
1335 struct kvm_cpuid_entry2 *e, *entry;
1336
Carsten Otte313a3dc2007-10-11 19:16:52 +02001337 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001338 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1339 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001340 if (e->function == 0x80000001) {
1341 entry = e;
1342 break;
1343 }
1344 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001345 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001346 entry->edx &= ~(1 << 20);
1347 printk(KERN_INFO "kvm: guest NX capability removed\n");
1348 }
1349}
1350
Dan Kenigsberg07716712007-11-21 17:10:04 +02001351/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001352static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1353 struct kvm_cpuid *cpuid,
1354 struct kvm_cpuid_entry __user *entries)
1355{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001356 int r, i;
1357 struct kvm_cpuid_entry *cpuid_entries;
1358
1359 r = -E2BIG;
1360 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1361 goto out;
1362 r = -ENOMEM;
1363 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1364 if (!cpuid_entries)
1365 goto out;
1366 r = -EFAULT;
1367 if (copy_from_user(cpuid_entries, entries,
1368 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1369 goto out_free;
1370 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001371 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1372 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1373 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1374 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1375 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1376 vcpu->arch.cpuid_entries[i].index = 0;
1377 vcpu->arch.cpuid_entries[i].flags = 0;
1378 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1379 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1380 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001381 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001382 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001383 cpuid_fix_nx_cap(vcpu);
1384 r = 0;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001385 kvm_apic_set_version(vcpu);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001386
1387out_free:
1388 vfree(cpuid_entries);
1389out:
1390 return r;
1391}
1392
1393static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001394 struct kvm_cpuid2 *cpuid,
1395 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001396{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001397 int r;
1398
1399 r = -E2BIG;
1400 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1401 goto out;
1402 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001403 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001404 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001405 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001406 vcpu->arch.cpuid_nent = cpuid->nent;
Gleb Natapovfc61b802009-07-05 17:39:35 +03001407 kvm_apic_set_version(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001408 return 0;
1409
1410out:
1411 return r;
1412}
1413
Dan Kenigsberg07716712007-11-21 17:10:04 +02001414static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
Amit Shah19355472009-01-14 16:56:00 +00001415 struct kvm_cpuid2 *cpuid,
1416 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001417{
1418 int r;
1419
1420 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001421 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001422 goto out;
1423 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001424 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001425 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001426 goto out;
1427 return 0;
1428
1429out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001430 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001431 return r;
1432}
1433
Dan Kenigsberg07716712007-11-21 17:10:04 +02001434static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
Amit Shah19355472009-01-14 16:56:00 +00001435 u32 index)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001436{
1437 entry->function = function;
1438 entry->index = index;
1439 cpuid_count(entry->function, entry->index,
Amit Shah19355472009-01-14 16:56:00 +00001440 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001441 entry->flags = 0;
1442}
1443
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001444#define F(x) bit(X86_FEATURE_##x)
1445
Dan Kenigsberg07716712007-11-21 17:10:04 +02001446static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1447 u32 index, int *nent, int maxnent)
1448{
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001449 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
Joerg Roedel344f4142009-07-27 16:30:48 +02001450 unsigned f_gbpages = kvm_x86_ops->gb_page_enable() ? F(GBPAGES) : 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001451#ifdef CONFIG_X86_64
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001452 unsigned f_lm = F(LM);
1453#else
1454 unsigned f_lm = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001455#endif
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001456
1457 /* cpuid 1.edx */
1458 const u32 kvm_supported_word0_x86_features =
1459 F(FPU) | F(VME) | F(DE) | F(PSE) |
1460 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1461 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1462 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1463 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1464 0 /* Reserved, DS, ACPI */ | F(MMX) |
1465 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1466 0 /* HTT, TM, Reserved, PBE */;
1467 /* cpuid 0x80000001.edx */
1468 const u32 kvm_supported_word1_x86_features =
1469 F(FPU) | F(VME) | F(DE) | F(PSE) |
1470 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1471 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1472 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1473 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1474 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
Joerg Roedel344f4142009-07-27 16:30:48 +02001475 F(FXSR) | F(FXSR_OPT) | f_gbpages | 0 /* RDTSCP */ |
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001476 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1477 /* cpuid 1.ecx */
1478 const u32 kvm_supported_word4_x86_features =
Avi Kivityd149c732009-05-10 14:41:56 +03001479 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1480 0 /* DS-CPL, VMX, SMX, EST */ |
1481 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1482 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1483 0 /* Reserved, DCA */ | F(XMM4_1) |
Gleb Natapov0105d1a2009-07-05 17:39:36 +03001484 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
Avi Kivityd149c732009-05-10 14:41:56 +03001485 0 /* Reserved, XSAVE, OSXSAVE */;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001486 /* cpuid 0x80000001.ecx */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001487 const u32 kvm_supported_word6_x86_features =
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001488 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1489 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1490 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1491 0 /* SKINIT */ | 0 /* WDT */;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001492
Amit Shah19355472009-01-14 16:56:00 +00001493 /* all calls to cpuid_count() should be made on the same cpu */
Dan Kenigsberg07716712007-11-21 17:10:04 +02001494 get_cpu();
1495 do_cpuid_1_ent(entry, function, index);
1496 ++*nent;
1497
1498 switch (function) {
1499 case 0:
1500 entry->eax = min(entry->eax, (u32)0xb);
1501 break;
1502 case 1:
1503 entry->edx &= kvm_supported_word0_x86_features;
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001504 entry->ecx &= kvm_supported_word4_x86_features;
Gleb Natapov0d1de2d92009-07-12 16:10:55 +03001505 /* we support x2apic emulation even if host does not support
1506 * it since we emulate x2apic in software */
1507 entry->ecx |= F(X2APIC);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001508 break;
1509 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1510 * may return different values. This forces us to get_cpu() before
1511 * issuing the first command, and also to emulate this annoying behavior
1512 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1513 case 2: {
1514 int t, times = entry->eax & 0xff;
1515
1516 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08001517 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001518 for (t = 1; t < times && *nent < maxnent; ++t) {
1519 do_cpuid_1_ent(&entry[t], function, 0);
1520 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1521 ++*nent;
1522 }
1523 break;
1524 }
1525 /* function 4 and 0xb have additional index. */
1526 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001527 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001528
1529 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1530 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001531 for (i = 1; *nent < maxnent; ++i) {
1532 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001533 if (!cache_type)
1534 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001535 do_cpuid_1_ent(&entry[i], function, i);
1536 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001537 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1538 ++*nent;
1539 }
1540 break;
1541 }
1542 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001543 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001544
1545 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1546 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001547 for (i = 1; *nent < maxnent; ++i) {
Nitin A Kamble0853d2c2008-11-05 15:37:36 -08001548 level_type = entry[i - 1].ecx & 0xff00;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001549 if (!level_type)
1550 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001551 do_cpuid_1_ent(&entry[i], function, i);
1552 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001553 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1554 ++*nent;
1555 }
1556 break;
1557 }
1558 case 0x80000000:
1559 entry->eax = min(entry->eax, 0x8000001a);
1560 break;
1561 case 0x80000001:
1562 entry->edx &= kvm_supported_word1_x86_features;
1563 entry->ecx &= kvm_supported_word6_x86_features;
1564 break;
1565 }
1566 put_cpu();
1567}
1568
Avi Kivity7faa4ee2009-05-10 13:55:35 +03001569#undef F
1570
Avi Kivity674eea02008-02-11 18:37:23 +02001571static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001572 struct kvm_cpuid_entry2 __user *entries)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001573{
1574 struct kvm_cpuid_entry2 *cpuid_entries;
1575 int limit, nent = 0, r = -E2BIG;
1576 u32 func;
1577
1578 if (cpuid->nent < 1)
1579 goto out;
1580 r = -ENOMEM;
1581 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1582 if (!cpuid_entries)
1583 goto out;
1584
1585 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1586 limit = cpuid_entries[0].eax;
1587 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1588 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001589 &nent, cpuid->nent);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001590 r = -E2BIG;
1591 if (nent >= cpuid->nent)
1592 goto out_free;
1593
1594 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1595 limit = cpuid_entries[nent - 1].eax;
1596 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1597 do_cpuid_ent(&cpuid_entries[nent], func, 0,
Amit Shah19355472009-01-14 16:56:00 +00001598 &nent, cpuid->nent);
Mark McLoughlincb007642009-05-12 12:36:44 +01001599 r = -E2BIG;
1600 if (nent >= cpuid->nent)
1601 goto out_free;
1602
Dan Kenigsberg07716712007-11-21 17:10:04 +02001603 r = -EFAULT;
1604 if (copy_to_user(entries, cpuid_entries,
Amit Shah19355472009-01-14 16:56:00 +00001605 nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001606 goto out_free;
1607 cpuid->nent = nent;
1608 r = 0;
1609
1610out_free:
1611 vfree(cpuid_entries);
1612out:
1613 return r;
1614}
1615
Carsten Otte313a3dc2007-10-11 19:16:52 +02001616static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1617 struct kvm_lapic_state *s)
1618{
1619 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001620 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001621 vcpu_put(vcpu);
1622
1623 return 0;
1624}
1625
1626static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1627 struct kvm_lapic_state *s)
1628{
1629 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001630 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001631 kvm_apic_post_state_restore(vcpu);
1632 vcpu_put(vcpu);
1633
1634 return 0;
1635}
1636
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001637static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1638 struct kvm_interrupt *irq)
1639{
1640 if (irq->irq < 0 || irq->irq >= 256)
1641 return -EINVAL;
1642 if (irqchip_in_kernel(vcpu->kvm))
1643 return -ENXIO;
1644 vcpu_load(vcpu);
1645
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001646 kvm_queue_interrupt(vcpu, irq->irq, false);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001647
1648 vcpu_put(vcpu);
1649
1650 return 0;
1651}
1652
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001653static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1654{
1655 vcpu_load(vcpu);
1656 kvm_inject_nmi(vcpu);
1657 vcpu_put(vcpu);
1658
1659 return 0;
1660}
1661
Avi Kivityb209749f2007-10-22 16:50:39 +02001662static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1663 struct kvm_tpr_access_ctl *tac)
1664{
1665 if (tac->flags)
1666 return -EINVAL;
1667 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1668 return 0;
1669}
1670
Huang Ying890ca9a2009-05-11 16:48:15 +08001671static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1672 u64 mcg_cap)
1673{
1674 int r;
1675 unsigned bank_num = mcg_cap & 0xff, bank;
1676
1677 r = -EINVAL;
1678 if (!bank_num)
1679 goto out;
1680 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1681 goto out;
1682 r = 0;
1683 vcpu->arch.mcg_cap = mcg_cap;
1684 /* Init IA32_MCG_CTL to all 1s */
1685 if (mcg_cap & MCG_CTL_P)
1686 vcpu->arch.mcg_ctl = ~(u64)0;
1687 /* Init IA32_MCi_CTL to all 1s */
1688 for (bank = 0; bank < bank_num; bank++)
1689 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1690out:
1691 return r;
1692}
1693
1694static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1695 struct kvm_x86_mce *mce)
1696{
1697 u64 mcg_cap = vcpu->arch.mcg_cap;
1698 unsigned bank_num = mcg_cap & 0xff;
1699 u64 *banks = vcpu->arch.mce_banks;
1700
1701 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1702 return -EINVAL;
1703 /*
1704 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1705 * reporting is disabled
1706 */
1707 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1708 vcpu->arch.mcg_ctl != ~(u64)0)
1709 return 0;
1710 banks += 4 * mce->bank;
1711 /*
1712 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1713 * reporting is disabled for the bank
1714 */
1715 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1716 return 0;
1717 if (mce->status & MCI_STATUS_UC) {
1718 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1719 !(vcpu->arch.cr4 & X86_CR4_MCE)) {
1720 printk(KERN_DEBUG "kvm: set_mce: "
1721 "injects mce exception while "
1722 "previous one is in progress!\n");
1723 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1724 return 0;
1725 }
1726 if (banks[1] & MCI_STATUS_VAL)
1727 mce->status |= MCI_STATUS_OVER;
1728 banks[2] = mce->addr;
1729 banks[3] = mce->misc;
1730 vcpu->arch.mcg_status = mce->mcg_status;
1731 banks[1] = mce->status;
1732 kvm_queue_exception(vcpu, MC_VECTOR);
1733 } else if (!(banks[1] & MCI_STATUS_VAL)
1734 || !(banks[1] & MCI_STATUS_UC)) {
1735 if (banks[1] & MCI_STATUS_VAL)
1736 mce->status |= MCI_STATUS_OVER;
1737 banks[2] = mce->addr;
1738 banks[3] = mce->misc;
1739 banks[1] = mce->status;
1740 } else
1741 banks[1] |= MCI_STATUS_OVER;
1742 return 0;
1743}
1744
Carsten Otte313a3dc2007-10-11 19:16:52 +02001745long kvm_arch_vcpu_ioctl(struct file *filp,
1746 unsigned int ioctl, unsigned long arg)
1747{
1748 struct kvm_vcpu *vcpu = filp->private_data;
1749 void __user *argp = (void __user *)arg;
1750 int r;
Dave Hansenb772ff32008-08-11 10:01:47 -07001751 struct kvm_lapic_state *lapic = NULL;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001752
1753 switch (ioctl) {
1754 case KVM_GET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001755 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001756
Dave Hansenb772ff32008-08-11 10:01:47 -07001757 r = -ENOMEM;
1758 if (!lapic)
1759 goto out;
1760 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001761 if (r)
1762 goto out;
1763 r = -EFAULT;
Dave Hansenb772ff32008-08-11 10:01:47 -07001764 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001765 goto out;
1766 r = 0;
1767 break;
1768 }
1769 case KVM_SET_LAPIC: {
Dave Hansenb772ff32008-08-11 10:01:47 -07001770 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
1771 r = -ENOMEM;
1772 if (!lapic)
Carsten Otte313a3dc2007-10-11 19:16:52 +02001773 goto out;
Dave Hansenb772ff32008-08-11 10:01:47 -07001774 r = -EFAULT;
1775 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
1776 goto out;
1777 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001778 if (r)
1779 goto out;
1780 r = 0;
1781 break;
1782 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001783 case KVM_INTERRUPT: {
1784 struct kvm_interrupt irq;
1785
1786 r = -EFAULT;
1787 if (copy_from_user(&irq, argp, sizeof irq))
1788 goto out;
1789 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1790 if (r)
1791 goto out;
1792 r = 0;
1793 break;
1794 }
Jan Kiszkac4abb7c2008-09-26 09:30:55 +02001795 case KVM_NMI: {
1796 r = kvm_vcpu_ioctl_nmi(vcpu);
1797 if (r)
1798 goto out;
1799 r = 0;
1800 break;
1801 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001802 case KVM_SET_CPUID: {
1803 struct kvm_cpuid __user *cpuid_arg = argp;
1804 struct kvm_cpuid cpuid;
1805
1806 r = -EFAULT;
1807 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1808 goto out;
1809 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1810 if (r)
1811 goto out;
1812 break;
1813 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001814 case KVM_SET_CPUID2: {
1815 struct kvm_cpuid2 __user *cpuid_arg = argp;
1816 struct kvm_cpuid2 cpuid;
1817
1818 r = -EFAULT;
1819 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1820 goto out;
1821 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001822 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001823 if (r)
1824 goto out;
1825 break;
1826 }
1827 case KVM_GET_CPUID2: {
1828 struct kvm_cpuid2 __user *cpuid_arg = argp;
1829 struct kvm_cpuid2 cpuid;
1830
1831 r = -EFAULT;
1832 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1833 goto out;
1834 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
Amit Shah19355472009-01-14 16:56:00 +00001835 cpuid_arg->entries);
Dan Kenigsberg07716712007-11-21 17:10:04 +02001836 if (r)
1837 goto out;
1838 r = -EFAULT;
1839 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1840 goto out;
1841 r = 0;
1842 break;
1843 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001844 case KVM_GET_MSRS:
1845 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1846 break;
1847 case KVM_SET_MSRS:
1848 r = msr_io(vcpu, argp, do_set_msr, 0);
1849 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001850 case KVM_TPR_ACCESS_REPORTING: {
1851 struct kvm_tpr_access_ctl tac;
1852
1853 r = -EFAULT;
1854 if (copy_from_user(&tac, argp, sizeof tac))
1855 goto out;
1856 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1857 if (r)
1858 goto out;
1859 r = -EFAULT;
1860 if (copy_to_user(argp, &tac, sizeof tac))
1861 goto out;
1862 r = 0;
1863 break;
1864 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001865 case KVM_SET_VAPIC_ADDR: {
1866 struct kvm_vapic_addr va;
1867
1868 r = -EINVAL;
1869 if (!irqchip_in_kernel(vcpu->kvm))
1870 goto out;
1871 r = -EFAULT;
1872 if (copy_from_user(&va, argp, sizeof va))
1873 goto out;
1874 r = 0;
1875 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1876 break;
1877 }
Huang Ying890ca9a2009-05-11 16:48:15 +08001878 case KVM_X86_SETUP_MCE: {
1879 u64 mcg_cap;
1880
1881 r = -EFAULT;
1882 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
1883 goto out;
1884 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
1885 break;
1886 }
1887 case KVM_X86_SET_MCE: {
1888 struct kvm_x86_mce mce;
1889
1890 r = -EFAULT;
1891 if (copy_from_user(&mce, argp, sizeof mce))
1892 goto out;
1893 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
1894 break;
1895 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001896 default:
1897 r = -EINVAL;
1898 }
1899out:
Wei Yongjun7a6ce842009-03-31 16:47:44 +08001900 kfree(lapic);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001901 return r;
1902}
1903
Carsten Otte1fe779f2007-10-29 16:08:35 +01001904static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1905{
1906 int ret;
1907
1908 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1909 return -1;
1910 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1911 return ret;
1912}
1913
Sheng Yangb927a3c2009-07-21 10:42:48 +08001914static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
1915 u64 ident_addr)
1916{
1917 kvm->arch.ept_identity_map_addr = ident_addr;
1918 return 0;
1919}
1920
Carsten Otte1fe779f2007-10-29 16:08:35 +01001921static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1922 u32 kvm_nr_mmu_pages)
1923{
1924 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1925 return -EINVAL;
1926
Izik Eidus72dc67a2008-02-10 18:04:15 +02001927 down_write(&kvm->slots_lock);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001928 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001929
1930 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001931 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001932
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03001933 spin_unlock(&kvm->mmu_lock);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001934 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001935 return 0;
1936}
1937
1938static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1939{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001940 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001941}
1942
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001943gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1944{
1945 int i;
1946 struct kvm_mem_alias *alias;
1947
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001948 for (i = 0; i < kvm->arch.naliases; ++i) {
1949 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001950 if (gfn >= alias->base_gfn
1951 && gfn < alias->base_gfn + alias->npages)
1952 return alias->target_gfn + gfn - alias->base_gfn;
1953 }
1954 return gfn;
1955}
1956
Carsten Otte1fe779f2007-10-29 16:08:35 +01001957/*
1958 * Set a new alias region. Aliases map a portion of physical memory into
1959 * another portion. This is useful for memory windows, for example the PC
1960 * VGA region.
1961 */
1962static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1963 struct kvm_memory_alias *alias)
1964{
1965 int r, n;
1966 struct kvm_mem_alias *p;
1967
1968 r = -EINVAL;
1969 /* General sanity checks */
1970 if (alias->memory_size & (PAGE_SIZE - 1))
1971 goto out;
1972 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1973 goto out;
1974 if (alias->slot >= KVM_ALIAS_SLOTS)
1975 goto out;
1976 if (alias->guest_phys_addr + alias->memory_size
1977 < alias->guest_phys_addr)
1978 goto out;
1979 if (alias->target_phys_addr + alias->memory_size
1980 < alias->target_phys_addr)
1981 goto out;
1982
Izik Eidus72dc67a2008-02-10 18:04:15 +02001983 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001984 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001985
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001986 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001987 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1988 p->npages = alias->memory_size >> PAGE_SHIFT;
1989 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1990
1991 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001992 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001993 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001994 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001995
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001996 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001997 kvm_mmu_zap_all(kvm);
1998
Izik Eidus72dc67a2008-02-10 18:04:15 +02001999 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002000
2001 return 0;
2002
2003out:
2004 return r;
2005}
2006
2007static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2008{
2009 int r;
2010
2011 r = 0;
2012 switch (chip->chip_id) {
2013 case KVM_IRQCHIP_PIC_MASTER:
2014 memcpy(&chip->chip.pic,
2015 &pic_irqchip(kvm)->pics[0],
2016 sizeof(struct kvm_pic_state));
2017 break;
2018 case KVM_IRQCHIP_PIC_SLAVE:
2019 memcpy(&chip->chip.pic,
2020 &pic_irqchip(kvm)->pics[1],
2021 sizeof(struct kvm_pic_state));
2022 break;
2023 case KVM_IRQCHIP_IOAPIC:
2024 memcpy(&chip->chip.ioapic,
2025 ioapic_irqchip(kvm),
2026 sizeof(struct kvm_ioapic_state));
2027 break;
2028 default:
2029 r = -EINVAL;
2030 break;
2031 }
2032 return r;
2033}
2034
2035static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2036{
2037 int r;
2038
2039 r = 0;
2040 switch (chip->chip_id) {
2041 case KVM_IRQCHIP_PIC_MASTER:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002042 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002043 memcpy(&pic_irqchip(kvm)->pics[0],
2044 &chip->chip.pic,
2045 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002046 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002047 break;
2048 case KVM_IRQCHIP_PIC_SLAVE:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002049 spin_lock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002050 memcpy(&pic_irqchip(kvm)->pics[1],
2051 &chip->chip.pic,
2052 sizeof(struct kvm_pic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002053 spin_unlock(&pic_irqchip(kvm)->lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002054 break;
2055 case KVM_IRQCHIP_IOAPIC:
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002056 mutex_lock(&kvm->irq_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002057 memcpy(ioapic_irqchip(kvm),
2058 &chip->chip.ioapic,
2059 sizeof(struct kvm_ioapic_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002060 mutex_unlock(&kvm->irq_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002061 break;
2062 default:
2063 r = -EINVAL;
2064 break;
2065 }
2066 kvm_pic_update_irq(pic_irqchip(kvm));
2067 return r;
2068}
2069
Sheng Yange0f63cb2008-03-04 00:50:59 +08002070static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2071{
2072 int r = 0;
2073
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002074 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002075 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002076 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002077 return r;
2078}
2079
2080static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2081{
2082 int r = 0;
2083
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002084 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002085 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
Beth Kone9f42752009-07-07 11:50:38 -04002086 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2087 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2088 return r;
2089}
2090
2091static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2092{
2093 int r = 0;
2094
2095 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2096 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2097 sizeof(ps->channels));
2098 ps->flags = kvm->arch.vpit->pit_state.flags;
2099 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2100 return r;
2101}
2102
2103static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2104{
2105 int r = 0, start = 0;
2106 u32 prev_legacy, cur_legacy;
2107 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2108 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2109 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2110 if (!prev_legacy && cur_legacy)
2111 start = 1;
2112 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2113 sizeof(kvm->arch.vpit->pit_state.channels));
2114 kvm->arch.vpit->pit_state.flags = ps->flags;
2115 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002116 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002117 return r;
2118}
2119
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002120static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2121 struct kvm_reinject_control *control)
2122{
2123 if (!kvm->arch.vpit)
2124 return -ENXIO;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002125 mutex_lock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002126 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
Marcelo Tosatti894a9c52009-06-23 15:05:14 -03002127 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002128 return 0;
2129}
2130
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002131/*
2132 * Get (and clear) the dirty memory log for a memory slot.
2133 */
2134int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2135 struct kvm_dirty_log *log)
2136{
2137 int r;
2138 int n;
2139 struct kvm_memory_slot *memslot;
2140 int is_dirty = 0;
2141
Izik Eidus72dc67a2008-02-10 18:04:15 +02002142 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002143
2144 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2145 if (r)
2146 goto out;
2147
2148 /* If nothing is dirty, don't bother messing with page tables. */
2149 if (is_dirty) {
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002150 spin_lock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002151 kvm_mmu_slot_remove_write_access(kvm, log->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03002152 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002153 kvm_flush_remote_tlbs(kvm);
2154 memslot = &kvm->memslots[log->slot];
2155 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2156 memset(memslot->dirty_bitmap, 0, n);
2157 }
2158 r = 0;
2159out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002160 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08002161 return r;
2162}
2163
Carsten Otte1fe779f2007-10-29 16:08:35 +01002164long kvm_arch_vm_ioctl(struct file *filp,
2165 unsigned int ioctl, unsigned long arg)
2166{
2167 struct kvm *kvm = filp->private_data;
2168 void __user *argp = (void __user *)arg;
2169 int r = -EINVAL;
Dave Hansenf0d66272008-08-11 10:01:45 -07002170 /*
2171 * This union makes it completely explicit to gcc-3.x
2172 * that these two variables' stack usage should be
2173 * combined, not added together.
2174 */
2175 union {
2176 struct kvm_pit_state ps;
Beth Kone9f42752009-07-07 11:50:38 -04002177 struct kvm_pit_state2 ps2;
Dave Hansenf0d66272008-08-11 10:01:45 -07002178 struct kvm_memory_alias alias;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002179 struct kvm_pit_config pit_config;
Dave Hansenf0d66272008-08-11 10:01:45 -07002180 } u;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002181
2182 switch (ioctl) {
2183 case KVM_SET_TSS_ADDR:
2184 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2185 if (r < 0)
2186 goto out;
2187 break;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002188 case KVM_SET_IDENTITY_MAP_ADDR: {
2189 u64 ident_addr;
2190
2191 r = -EFAULT;
2192 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2193 goto out;
2194 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2195 if (r < 0)
2196 goto out;
2197 break;
2198 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002199 case KVM_SET_MEMORY_REGION: {
2200 struct kvm_memory_region kvm_mem;
2201 struct kvm_userspace_memory_region kvm_userspace_mem;
2202
2203 r = -EFAULT;
2204 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2205 goto out;
2206 kvm_userspace_mem.slot = kvm_mem.slot;
2207 kvm_userspace_mem.flags = kvm_mem.flags;
2208 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2209 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2210 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2211 if (r)
2212 goto out;
2213 break;
2214 }
2215 case KVM_SET_NR_MMU_PAGES:
2216 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2217 if (r)
2218 goto out;
2219 break;
2220 case KVM_GET_NR_MMU_PAGES:
2221 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2222 break;
Dave Hansenf0d66272008-08-11 10:01:45 -07002223 case KVM_SET_MEMORY_ALIAS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002224 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002225 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
Carsten Otte1fe779f2007-10-29 16:08:35 +01002226 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002227 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002228 if (r)
2229 goto out;
2230 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002231 case KVM_CREATE_IRQCHIP:
2232 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08002233 kvm->arch.vpic = kvm_create_pic(kvm);
2234 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01002235 r = kvm_ioapic_init(kvm);
2236 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08002237 kfree(kvm->arch.vpic);
2238 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002239 goto out;
2240 }
2241 } else
2242 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +02002243 r = kvm_setup_default_irq_routing(kvm);
2244 if (r) {
2245 kfree(kvm->arch.vpic);
2246 kfree(kvm->arch.vioapic);
2247 goto out;
2248 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002249 break;
Sheng Yang78376992008-01-28 05:10:22 +08002250 case KVM_CREATE_PIT:
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002251 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2252 goto create_pit;
2253 case KVM_CREATE_PIT2:
2254 r = -EFAULT;
2255 if (copy_from_user(&u.pit_config, argp,
2256 sizeof(struct kvm_pit_config)))
2257 goto out;
2258 create_pit:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002259 down_write(&kvm->slots_lock);
Avi Kivity269e05e2009-01-05 15:21:42 +02002260 r = -EEXIST;
2261 if (kvm->arch.vpit)
2262 goto create_pit_unlock;
Sheng Yang78376992008-01-28 05:10:22 +08002263 r = -ENOMEM;
Jan Kiszkac5ff41c2009-05-14 22:42:53 +02002264 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
Sheng Yang78376992008-01-28 05:10:22 +08002265 if (kvm->arch.vpit)
2266 r = 0;
Avi Kivity269e05e2009-01-05 15:21:42 +02002267 create_pit_unlock:
Michael S. Tsirkin108b5662009-06-29 22:24:21 +03002268 up_write(&kvm->slots_lock);
Sheng Yang78376992008-01-28 05:10:22 +08002269 break;
Gleb Natapov49256632009-02-04 17:28:14 +02002270 case KVM_IRQ_LINE_STATUS:
Carsten Otte1fe779f2007-10-29 16:08:35 +01002271 case KVM_IRQ_LINE: {
2272 struct kvm_irq_level irq_event;
2273
2274 r = -EFAULT;
2275 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2276 goto out;
2277 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +02002278 __s32 status;
Marcelo Tosattifa40a822009-06-04 15:08:24 -03002279 mutex_lock(&kvm->irq_lock);
Gleb Natapov49256632009-02-04 17:28:14 +02002280 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2281 irq_event.irq, irq_event.level);
Marcelo Tosattifa40a822009-06-04 15:08:24 -03002282 mutex_unlock(&kvm->irq_lock);
Gleb Natapov49256632009-02-04 17:28:14 +02002283 if (ioctl == KVM_IRQ_LINE_STATUS) {
2284 irq_event.status = status;
2285 if (copy_to_user(argp, &irq_event,
2286 sizeof irq_event))
2287 goto out;
2288 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002289 r = 0;
2290 }
2291 break;
2292 }
2293 case KVM_GET_IRQCHIP: {
2294 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002295 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002296
Dave Hansenf0d66272008-08-11 10:01:45 -07002297 r = -ENOMEM;
2298 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002299 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002300 r = -EFAULT;
2301 if (copy_from_user(chip, argp, sizeof *chip))
2302 goto get_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002303 r = -ENXIO;
2304 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002305 goto get_irqchip_out;
2306 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2307 if (r)
2308 goto get_irqchip_out;
2309 r = -EFAULT;
2310 if (copy_to_user(argp, chip, sizeof *chip))
2311 goto get_irqchip_out;
2312 r = 0;
2313 get_irqchip_out:
2314 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002315 if (r)
2316 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002317 break;
2318 }
2319 case KVM_SET_IRQCHIP: {
2320 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
Dave Hansenf0d66272008-08-11 10:01:45 -07002321 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002322
Dave Hansenf0d66272008-08-11 10:01:45 -07002323 r = -ENOMEM;
2324 if (!chip)
Carsten Otte1fe779f2007-10-29 16:08:35 +01002325 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002326 r = -EFAULT;
2327 if (copy_from_user(chip, argp, sizeof *chip))
2328 goto set_irqchip_out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002329 r = -ENXIO;
2330 if (!irqchip_in_kernel(kvm))
Dave Hansenf0d66272008-08-11 10:01:45 -07002331 goto set_irqchip_out;
2332 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2333 if (r)
2334 goto set_irqchip_out;
2335 r = 0;
2336 set_irqchip_out:
2337 kfree(chip);
Carsten Otte1fe779f2007-10-29 16:08:35 +01002338 if (r)
2339 goto out;
Carsten Otte1fe779f2007-10-29 16:08:35 +01002340 break;
2341 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08002342 case KVM_GET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002343 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002344 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002345 goto out;
2346 r = -ENXIO;
2347 if (!kvm->arch.vpit)
2348 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002349 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002350 if (r)
2351 goto out;
2352 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002353 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002354 goto out;
2355 r = 0;
2356 break;
2357 }
2358 case KVM_SET_PIT: {
Sheng Yange0f63cb2008-03-04 00:50:59 +08002359 r = -EFAULT;
Dave Hansenf0d66272008-08-11 10:01:45 -07002360 if (copy_from_user(&u.ps, argp, sizeof u.ps))
Sheng Yange0f63cb2008-03-04 00:50:59 +08002361 goto out;
2362 r = -ENXIO;
2363 if (!kvm->arch.vpit)
2364 goto out;
Dave Hansenf0d66272008-08-11 10:01:45 -07002365 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
Sheng Yange0f63cb2008-03-04 00:50:59 +08002366 if (r)
2367 goto out;
2368 r = 0;
2369 break;
2370 }
Beth Kone9f42752009-07-07 11:50:38 -04002371 case KVM_GET_PIT2: {
2372 r = -ENXIO;
2373 if (!kvm->arch.vpit)
2374 goto out;
2375 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2376 if (r)
2377 goto out;
2378 r = -EFAULT;
2379 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2380 goto out;
2381 r = 0;
2382 break;
2383 }
2384 case KVM_SET_PIT2: {
2385 r = -EFAULT;
2386 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2387 goto out;
2388 r = -ENXIO;
2389 if (!kvm->arch.vpit)
2390 goto out;
2391 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2392 if (r)
2393 goto out;
2394 r = 0;
2395 break;
2396 }
Marcelo Tosatti52d939a2008-12-30 15:55:06 -02002397 case KVM_REINJECT_CONTROL: {
2398 struct kvm_reinject_control control;
2399 r = -EFAULT;
2400 if (copy_from_user(&control, argp, sizeof(control)))
2401 goto out;
2402 r = kvm_vm_ioctl_reinject(kvm, &control);
2403 if (r)
2404 goto out;
2405 r = 0;
2406 break;
2407 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01002408 default:
2409 ;
2410 }
2411out:
2412 return r;
2413}
2414
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08002415static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02002416{
2417 u32 dummy[2];
2418 unsigned i, j;
2419
2420 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2421 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2422 continue;
2423 if (j < i)
2424 msrs_to_save[j] = msrs_to_save[i];
2425 j++;
2426 }
2427 num_msrs_to_save = j;
2428}
2429
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002430static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2431 const void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002432{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002433 if (vcpu->arch.apic &&
2434 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2435 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002436
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002437 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002438}
2439
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002440static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002441{
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002442 if (vcpu->arch.apic &&
2443 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2444 return 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002445
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002446 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002447}
2448
Hannes Edercded19f2009-02-21 02:19:13 +01002449static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2450 struct kvm_vcpu *vcpu)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002451{
2452 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002453 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002454
2455 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002456 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002457 unsigned offset = addr & (PAGE_SIZE-1);
Izik Eidus77c20022008-12-29 01:42:19 +02002458 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002459 int ret;
2460
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002461 if (gpa == UNMAPPED_GVA) {
2462 r = X86EMUL_PROPAGATE_FAULT;
2463 goto out;
2464 }
Izik Eidus77c20022008-12-29 01:42:19 +02002465 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002466 if (ret < 0) {
2467 r = X86EMUL_UNHANDLEABLE;
2468 goto out;
2469 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01002470
Izik Eidus77c20022008-12-29 01:42:19 +02002471 bytes -= toread;
2472 data += toread;
2473 addr += toread;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002474 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002475out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002476 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002477}
Izik Eidus77c20022008-12-29 01:42:19 +02002478
Hannes Edercded19f2009-02-21 02:19:13 +01002479static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2480 struct kvm_vcpu *vcpu)
Izik Eidus77c20022008-12-29 01:42:19 +02002481{
2482 void *data = val;
2483 int r = X86EMUL_CONTINUE;
2484
2485 while (bytes) {
2486 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2487 unsigned offset = addr & (PAGE_SIZE-1);
2488 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2489 int ret;
2490
2491 if (gpa == UNMAPPED_GVA) {
2492 r = X86EMUL_PROPAGATE_FAULT;
2493 goto out;
2494 }
2495 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2496 if (ret < 0) {
2497 r = X86EMUL_UNHANDLEABLE;
2498 goto out;
2499 }
2500
2501 bytes -= towrite;
2502 data += towrite;
2503 addr += towrite;
2504 }
2505out:
2506 return r;
2507}
2508
Carsten Ottebbd9b642007-10-30 18:44:21 +01002509
Carsten Ottebbd9b642007-10-30 18:44:21 +01002510static int emulator_read_emulated(unsigned long addr,
2511 void *val,
2512 unsigned int bytes,
2513 struct kvm_vcpu *vcpu)
2514{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002515 gpa_t gpa;
2516
2517 if (vcpu->mmio_read_completed) {
2518 memcpy(val, vcpu->mmio_data, bytes);
Avi Kivityaec51dc2009-07-01 16:01:02 +03002519 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
2520 vcpu->mmio_phys_addr, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002521 vcpu->mmio_read_completed = 0;
2522 return X86EMUL_CONTINUE;
2523 }
2524
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002525 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002526
2527 /* For APIC access vmexit */
2528 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2529 goto mmio;
2530
Izik Eidus77c20022008-12-29 01:42:19 +02002531 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2532 == X86EMUL_CONTINUE)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002533 return X86EMUL_CONTINUE;
2534 if (gpa == UNMAPPED_GVA)
2535 return X86EMUL_PROPAGATE_FAULT;
2536
2537mmio:
2538 /*
2539 * Is this MMIO handled locally?
2540 */
Avi Kivityaec51dc2009-07-01 16:01:02 +03002541 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
2542 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002543 return X86EMUL_CONTINUE;
Avi Kivityaec51dc2009-07-01 16:01:02 +03002544 }
2545
2546 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002547
2548 vcpu->mmio_needed = 1;
2549 vcpu->mmio_phys_addr = gpa;
2550 vcpu->mmio_size = bytes;
2551 vcpu->mmio_is_write = 0;
2552
2553 return X86EMUL_UNHANDLEABLE;
2554}
2555
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002556int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02002557 const void *val, int bytes)
2558{
2559 int ret;
2560
2561 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2562 if (ret < 0)
2563 return 0;
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002564 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
Avi Kivity9f811282008-03-02 14:06:05 +02002565 return 1;
2566}
2567
Carsten Ottebbd9b642007-10-30 18:44:21 +01002568static int emulator_write_emulated_onepage(unsigned long addr,
2569 const void *val,
2570 unsigned int bytes,
2571 struct kvm_vcpu *vcpu)
2572{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002573 gpa_t gpa;
2574
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002575 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002576
2577 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002578 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002579 return X86EMUL_PROPAGATE_FAULT;
2580 }
2581
2582 /* For APIC access vmexit */
2583 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2584 goto mmio;
2585
2586 if (emulator_write_phys(vcpu, gpa, val, bytes))
2587 return X86EMUL_CONTINUE;
2588
2589mmio:
Avi Kivityaec51dc2009-07-01 16:01:02 +03002590 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002591 /*
2592 * Is this MMIO handled locally?
2593 */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002594 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
Carsten Ottebbd9b642007-10-30 18:44:21 +01002595 return X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002596
2597 vcpu->mmio_needed = 1;
2598 vcpu->mmio_phys_addr = gpa;
2599 vcpu->mmio_size = bytes;
2600 vcpu->mmio_is_write = 1;
2601 memcpy(vcpu->mmio_data, val, bytes);
2602
2603 return X86EMUL_CONTINUE;
2604}
2605
2606int emulator_write_emulated(unsigned long addr,
2607 const void *val,
2608 unsigned int bytes,
2609 struct kvm_vcpu *vcpu)
2610{
2611 /* Crossing a page boundary? */
2612 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2613 int rc, now;
2614
2615 now = -addr & ~PAGE_MASK;
2616 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2617 if (rc != X86EMUL_CONTINUE)
2618 return rc;
2619 addr += now;
2620 val += now;
2621 bytes -= now;
2622 }
2623 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2624}
2625EXPORT_SYMBOL_GPL(emulator_write_emulated);
2626
2627static int emulator_cmpxchg_emulated(unsigned long addr,
2628 const void *old,
2629 const void *new,
2630 unsigned int bytes,
2631 struct kvm_vcpu *vcpu)
2632{
2633 static int reported;
2634
2635 if (!reported) {
2636 reported = 1;
2637 printk(KERN_WARNING "kvm: emulating exchange as write\n");
2638 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002639#ifndef CONFIG_X86_64
2640 /* guests cmpxchg8b have to be emulated atomically */
2641 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002642 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002643 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002644 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002645 u64 val;
2646
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002647 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2648
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002649 if (gpa == UNMAPPED_GVA ||
2650 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2651 goto emul_write;
2652
2653 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2654 goto emul_write;
2655
2656 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002657
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002658 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002659
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002660 kaddr = kmap_atomic(page, KM_USER0);
2661 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2662 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002663 kvm_release_page_dirty(page);
2664 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002665emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002666#endif
2667
Carsten Ottebbd9b642007-10-30 18:44:21 +01002668 return emulator_write_emulated(addr, new, bytes, vcpu);
2669}
2670
2671static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2672{
2673 return kvm_x86_ops->get_segment_base(vcpu, seg);
2674}
2675
2676int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2677{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002678 kvm_mmu_invlpg(vcpu, address);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002679 return X86EMUL_CONTINUE;
2680}
2681
2682int emulate_clts(struct kvm_vcpu *vcpu)
2683{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002684 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002685 return X86EMUL_CONTINUE;
2686}
2687
2688int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2689{
2690 struct kvm_vcpu *vcpu = ctxt->vcpu;
2691
2692 switch (dr) {
2693 case 0 ... 3:
2694 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2695 return X86EMUL_CONTINUE;
2696 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002697 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002698 return X86EMUL_UNHANDLEABLE;
2699 }
2700}
2701
2702int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2703{
2704 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2705 int exception;
2706
2707 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2708 if (exception) {
2709 /* FIXME: better handling */
2710 return X86EMUL_UNHANDLEABLE;
2711 }
2712 return X86EMUL_CONTINUE;
2713}
2714
2715void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2716{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002717 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002718 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002719 unsigned long rip_linear;
2720
Avi Kivityf76c7102008-06-13 22:45:42 +03002721 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002722 return;
2723
Glauber Costa25be4602008-06-10 10:46:53 -03002724 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2725
Izik Eidus77c20022008-12-29 01:42:19 +02002726 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002727
2728 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2729 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002730}
2731EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2732
Harvey Harrison14af3f32008-02-19 10:25:50 -08002733static struct x86_emulate_ops emulate_ops = {
Izik Eidus77c20022008-12-29 01:42:19 +02002734 .read_std = kvm_read_guest_virt,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002735 .read_emulated = emulator_read_emulated,
2736 .write_emulated = emulator_write_emulated,
2737 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2738};
2739
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002740static void cache_all_regs(struct kvm_vcpu *vcpu)
2741{
2742 kvm_register_read(vcpu, VCPU_REGS_RAX);
2743 kvm_register_read(vcpu, VCPU_REGS_RSP);
2744 kvm_register_read(vcpu, VCPU_REGS_RIP);
2745 vcpu->arch.regs_dirty = ~0;
2746}
2747
Carsten Ottebbd9b642007-10-30 18:44:21 +01002748int emulate_instruction(struct kvm_vcpu *vcpu,
2749 struct kvm_run *run,
2750 unsigned long cr2,
2751 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002752 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002753{
Glauber Costa310b5d32009-05-12 16:21:06 -04002754 int r, shadow_mask;
Sheng Yang571008d2008-01-02 14:49:22 +08002755 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002756
Avi Kivity26eef702008-07-03 14:59:22 +03002757 kvm_clear_exception_queue(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002758 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002759 /*
2760 * TODO: fix x86_emulate.c to use guest_read/write_register
2761 * instead of direct ->regs accesses, can save hundred cycles
2762 * on Intel for instructions that don't read/change RSP, for
2763 * for example.
2764 */
2765 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002766
2767 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002768 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002769
Sheng Yang571008d2008-01-02 14:49:22 +08002770 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002771 int cs_db, cs_l;
2772 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2773
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002774 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2775 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2776 vcpu->arch.emulate_ctxt.mode =
2777 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002778 ? X86EMUL_MODE_REAL : cs_l
2779 ? X86EMUL_MODE_PROT64 : cs_db
2780 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2781
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002782 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002783
Andre Przywara0cb57622009-06-17 15:50:31 +02002784 /* Only allow emulation of specific instructions on #UD
2785 * (namely VMMCALL, sysenter, sysexit, syscall)*/
Sheng Yang571008d2008-01-02 14:49:22 +08002786 c = &vcpu->arch.emulate_ctxt.decode;
Andre Przywara0cb57622009-06-17 15:50:31 +02002787 if (emulation_type & EMULTYPE_TRAP_UD) {
2788 if (!c->twobyte)
2789 return EMULATE_FAIL;
2790 switch (c->b) {
2791 case 0x01: /* VMMCALL */
2792 if (c->modrm_mod != 3 || c->modrm_rm != 1)
2793 return EMULATE_FAIL;
2794 break;
2795 case 0x34: /* sysenter */
2796 case 0x35: /* sysexit */
2797 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2798 return EMULATE_FAIL;
2799 break;
2800 case 0x05: /* syscall */
2801 if (c->modrm_mod != 0 || c->modrm_rm != 0)
2802 return EMULATE_FAIL;
2803 break;
2804 default:
2805 return EMULATE_FAIL;
2806 }
2807
2808 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
2809 return EMULATE_FAIL;
2810 }
Sheng Yang571008d2008-01-02 14:49:22 +08002811
Avi Kivityf2b57562007-11-18 15:17:51 +02002812 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002813 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002814 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002815 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2816 return EMULATE_DONE;
2817 return EMULATE_FAIL;
2818 }
2819 }
2820
Gleb Natapovba8afb62009-04-12 13:36:57 +03002821 if (emulation_type & EMULTYPE_SKIP) {
2822 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
2823 return EMULATE_DONE;
2824 }
2825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002826 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Glauber Costa310b5d32009-05-12 16:21:06 -04002827 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
2828
2829 if (r == 0)
2830 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002831
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002832 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002833 return EMULATE_DO_MMIO;
2834
2835 if ((r || vcpu->mmio_is_write) && run) {
2836 run->exit_reason = KVM_EXIT_MMIO;
2837 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2838 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2839 run->mmio.len = vcpu->mmio_size;
2840 run->mmio.is_write = vcpu->mmio_is_write;
2841 }
2842
2843 if (r) {
2844 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2845 return EMULATE_DONE;
2846 if (!vcpu->mmio_needed) {
2847 kvm_report_emulation_failure(vcpu, "mmio");
2848 return EMULATE_FAIL;
2849 }
2850 return EMULATE_DO_MMIO;
2851 }
2852
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002853 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002854
2855 if (vcpu->mmio_is_write) {
2856 vcpu->mmio_needed = 0;
2857 return EMULATE_DO_MMIO;
2858 }
2859
2860 return EMULATE_DONE;
2861}
2862EXPORT_SYMBOL_GPL(emulate_instruction);
2863
Carsten Ottede7d7892007-10-30 18:44:25 +01002864static int pio_copy_data(struct kvm_vcpu *vcpu)
2865{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002866 void *p = vcpu->arch.pio_data;
Izik Eidus0f346072008-12-29 01:42:20 +02002867 gva_t q = vcpu->arch.pio.guest_gva;
Carsten Ottede7d7892007-10-30 18:44:25 +01002868 unsigned bytes;
Izik Eidus0f346072008-12-29 01:42:20 +02002869 int ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002870
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002871 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2872 if (vcpu->arch.pio.in)
Izik Eidus0f346072008-12-29 01:42:20 +02002873 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
Carsten Ottede7d7892007-10-30 18:44:25 +01002874 else
Izik Eidus0f346072008-12-29 01:42:20 +02002875 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
2876 return ret;
Carsten Ottede7d7892007-10-30 18:44:25 +01002877}
2878
2879int complete_pio(struct kvm_vcpu *vcpu)
2880{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002881 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002882 long delta;
2883 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002884 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002885
2886 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002887 if (io->in) {
2888 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2889 memcpy(&val, vcpu->arch.pio_data, io->size);
2890 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2891 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002892 } else {
2893 if (io->in) {
2894 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002895 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002896 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002897 }
2898
2899 delta = 1;
2900 if (io->rep) {
2901 delta *= io->cur_count;
2902 /*
2903 * The size of the register should really depend on
2904 * current address size.
2905 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002906 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2907 val -= delta;
2908 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002909 }
2910 if (io->down)
2911 delta = -delta;
2912 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002913 if (io->in) {
2914 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2915 val += delta;
2916 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2917 } else {
2918 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2919 val += delta;
2920 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2921 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002922 }
2923
Carsten Ottede7d7892007-10-30 18:44:25 +01002924 io->count -= io->cur_count;
2925 io->cur_count = 0;
2926
2927 return 0;
2928}
2929
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002930static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
Carsten Ottede7d7892007-10-30 18:44:25 +01002931{
2932 /* TODO: String I/O for in kernel device */
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002933 int r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002934
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002935 if (vcpu->arch.pio.in)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002936 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
2937 vcpu->arch.pio.size, pd);
Carsten Ottede7d7892007-10-30 18:44:25 +01002938 else
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002939 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
2940 vcpu->arch.pio.size, pd);
2941 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002942}
2943
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002944static int pio_string_write(struct kvm_vcpu *vcpu)
Carsten Ottede7d7892007-10-30 18:44:25 +01002945{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002946 struct kvm_pio_request *io = &vcpu->arch.pio;
2947 void *pd = vcpu->arch.pio_data;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002948 int i, r = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002949
Carsten Ottede7d7892007-10-30 18:44:25 +01002950 for (i = 0; i < io->cur_count; i++) {
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002951 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
2952 io->port, io->size, pd)) {
2953 r = -EOPNOTSUPP;
2954 break;
2955 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002956 pd += io->size;
2957 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002958 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002959}
2960
2961int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2962 int size, unsigned port)
2963{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002964 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002965
2966 vcpu->run->exit_reason = KVM_EXIT_IO;
2967 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002968 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002969 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002970 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2971 vcpu->run->io.port = vcpu->arch.pio.port = port;
2972 vcpu->arch.pio.in = in;
2973 vcpu->arch.pio.string = 0;
2974 vcpu->arch.pio.down = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002975 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002976
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002977 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
2978 size, 1);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002979
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002980 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2981 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002982
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03002983 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002984 complete_pio(vcpu);
2985 return 1;
2986 }
2987 return 0;
2988}
2989EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2990
2991int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2992 int size, unsigned long count, int down,
2993 gva_t address, int rep, unsigned port)
2994{
2995 unsigned now, in_page;
Izik Eidus0f346072008-12-29 01:42:20 +02002996 int ret = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002997
2998 vcpu->run->exit_reason = KVM_EXIT_IO;
2999 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003000 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01003001 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003002 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3003 vcpu->run->io.port = vcpu->arch.pio.port = port;
3004 vcpu->arch.pio.in = in;
3005 vcpu->arch.pio.string = 1;
3006 vcpu->arch.pio.down = down;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003007 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01003008
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003009 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3010 size, count);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003011
Carsten Ottede7d7892007-10-30 18:44:25 +01003012 if (!count) {
3013 kvm_x86_ops->skip_emulated_instruction(vcpu);
3014 return 1;
3015 }
3016
3017 if (!down)
3018 in_page = PAGE_SIZE - offset_in_page(address);
3019 else
3020 in_page = offset_in_page(address) + size;
3021 now = min(count, (unsigned long)in_page / size);
Izik Eidus0f346072008-12-29 01:42:20 +02003022 if (!now)
Carsten Ottede7d7892007-10-30 18:44:25 +01003023 now = 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01003024 if (down) {
3025 /*
3026 * String I/O in reverse. Yuck. Kill the guest, fix later.
3027 */
3028 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003029 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01003030 return 1;
3031 }
3032 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003033 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01003034
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003035 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01003036 kvm_x86_ops->skip_emulated_instruction(vcpu);
3037
Izik Eidus0f346072008-12-29 01:42:20 +02003038 vcpu->arch.pio.guest_gva = address;
Carsten Ottede7d7892007-10-30 18:44:25 +01003039
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003040 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003041 /* string PIO write */
3042 ret = pio_copy_data(vcpu);
Izik Eidus0f346072008-12-29 01:42:20 +02003043 if (ret == X86EMUL_PROPAGATE_FAULT) {
3044 kvm_inject_gp(vcpu, 0);
3045 return 1;
3046 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003047 if (ret == 0 && !pio_string_write(vcpu)) {
Carsten Ottede7d7892007-10-30 18:44:25 +01003048 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003049 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01003050 ret = 1;
3051 }
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03003052 }
3053 /* no string PIO read support yet */
Carsten Ottede7d7892007-10-30 18:44:25 +01003054
3055 return ret;
3056}
3057EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3058
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003059static void bounce_off(void *info)
3060{
3061 /* nothing */
3062}
3063
3064static unsigned int ref_freq;
3065static unsigned long tsc_khz_ref;
3066
3067static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3068 void *data)
3069{
3070 struct cpufreq_freqs *freq = data;
3071 struct kvm *kvm;
3072 struct kvm_vcpu *vcpu;
3073 int i, send_ipi = 0;
3074
3075 if (!ref_freq)
3076 ref_freq = freq->old;
3077
3078 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3079 return 0;
3080 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3081 return 0;
3082 per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
3083
3084 spin_lock(&kvm_lock);
3085 list_for_each_entry(kvm, &vm_list, vm_list) {
Gleb Natapov988a2ca2009-06-09 15:56:29 +03003086 kvm_for_each_vcpu(i, vcpu, kvm) {
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003087 if (vcpu->cpu != freq->cpu)
3088 continue;
3089 if (!kvm_request_guest_time_update(vcpu))
3090 continue;
3091 if (vcpu->cpu != smp_processor_id())
3092 send_ipi++;
3093 }
3094 }
3095 spin_unlock(&kvm_lock);
3096
3097 if (freq->old < freq->new && send_ipi) {
3098 /*
3099 * We upscale the frequency. Must make the guest
3100 * doesn't see old kvmclock values while running with
3101 * the new frequency, otherwise we risk the guest sees
3102 * time go backwards.
3103 *
3104 * In case we update the frequency for another cpu
3105 * (which might be in guest context) send an interrupt
3106 * to kick the cpu out of guest context. Next time
3107 * guest context is entered kvmclock will be updated,
3108 * so the guest will not see stale values.
3109 */
3110 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3111 }
3112 return 0;
3113}
3114
3115static struct notifier_block kvmclock_cpufreq_notifier_block = {
3116 .notifier_call = kvmclock_cpufreq_notifier
3117};
3118
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003119int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02003120{
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003121 int r, cpu;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003122 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3123
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003124 if (kvm_x86_ops) {
3125 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003126 r = -EEXIST;
3127 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003128 }
3129
3130 if (!ops->cpu_has_kvm_support()) {
3131 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003132 r = -EOPNOTSUPP;
3133 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003134 }
3135 if (ops->disabled_by_bios()) {
3136 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003137 r = -EOPNOTSUPP;
3138 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003139 }
3140
Avi Kivity97db56c2008-01-13 13:23:56 +02003141 r = kvm_mmu_module_init();
3142 if (r)
3143 goto out;
3144
3145 kvm_init_msr_list();
3146
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003147 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003148 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08003149 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3150 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003151 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003152
3153 for_each_possible_cpu(cpu)
3154 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
3155 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
3156 tsc_khz_ref = tsc_khz;
3157 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3158 CPUFREQ_TRANSITION_NOTIFIER);
3159 }
3160
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003161 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003162
3163out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003164 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02003165}
Hollis Blanchard8776e512007-10-31 17:24:24 -05003166
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003167void kvm_arch_exit(void)
3168{
Jan Kiszka888d2562009-04-17 19:24:58 +02003169 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3170 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3171 CPUFREQ_TRANSITION_NOTIFIER);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003172 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08003173 kvm_mmu_module_exit();
3174}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08003175
Hollis Blanchard8776e512007-10-31 17:24:24 -05003176int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3177{
3178 ++vcpu->stat.halt_exits;
3179 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03003180 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003181 return 1;
3182 } else {
3183 vcpu->run->exit_reason = KVM_EXIT_HLT;
3184 return 0;
3185 }
3186}
3187EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3188
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003189static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3190 unsigned long a1)
3191{
3192 if (is_long_mode(vcpu))
3193 return a0;
3194 else
3195 return a0 | ((gpa_t)a1 << 32);
3196}
3197
Hollis Blanchard8776e512007-10-31 17:24:24 -05003198int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3199{
3200 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003201 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003202
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003203 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3204 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3205 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3206 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3207 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003208
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003209 trace_kvm_hypercall(nr, a0, a1, a2, a3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003210
Hollis Blanchard8776e512007-10-31 17:24:24 -05003211 if (!is_long_mode(vcpu)) {
3212 nr &= 0xFFFFFFFF;
3213 a0 &= 0xFFFFFFFF;
3214 a1 &= 0xFFFFFFFF;
3215 a2 &= 0xFFFFFFFF;
3216 a3 &= 0xFFFFFFFF;
3217 }
3218
Jan Kiszka07708c42009-08-03 18:43:28 +02003219 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3220 ret = -KVM_EPERM;
3221 goto out;
3222 }
3223
Hollis Blanchard8776e512007-10-31 17:24:24 -05003224 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02003225 case KVM_HC_VAPIC_POLL_IRQ:
3226 ret = 0;
3227 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003228 case KVM_HC_MMU_OP:
3229 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3230 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003231 default:
3232 ret = -KVM_ENOSYS;
3233 break;
3234 }
Jan Kiszka07708c42009-08-03 18:43:28 +02003235out:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003236 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05303237 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003238 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003239}
3240EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3241
3242int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3243{
3244 char instruction[3];
3245 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003246 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003247
Hollis Blanchard8776e512007-10-31 17:24:24 -05003248
3249 /*
3250 * Blow out the MMU to ensure that no other VCPU has an active mapping
3251 * to ensure that the updated hypercall appears atomically across all
3252 * VCPUs.
3253 */
3254 kvm_mmu_zap_all(vcpu->kvm);
3255
Hollis Blanchard8776e512007-10-31 17:24:24 -05003256 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003257 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003258 != X86EMUL_CONTINUE)
3259 ret = -EFAULT;
3260
Hollis Blanchard8776e512007-10-31 17:24:24 -05003261 return ret;
3262}
3263
3264static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3265{
3266 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3267}
3268
3269void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3270{
3271 struct descriptor_table dt = { limit, base };
3272
3273 kvm_x86_ops->set_gdt(vcpu, &dt);
3274}
3275
3276void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3277{
3278 struct descriptor_table dt = { limit, base };
3279
3280 kvm_x86_ops->set_idt(vcpu, &dt);
3281}
3282
3283void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3284 unsigned long *rflags)
3285{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003286 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003287 *rflags = kvm_x86_ops->get_rflags(vcpu);
3288}
3289
3290unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3291{
Joerg Roedel54e445c2008-04-30 17:56:02 +02003292 unsigned long value;
3293
Hollis Blanchard8776e512007-10-31 17:24:24 -05003294 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3295 switch (cr) {
3296 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003297 value = vcpu->arch.cr0;
3298 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003299 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003300 value = vcpu->arch.cr2;
3301 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003302 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003303 value = vcpu->arch.cr3;
3304 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003305 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003306 value = vcpu->arch.cr4;
3307 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003308 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02003309 value = kvm_get_cr8(vcpu);
3310 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003311 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003312 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003313 return 0;
3314 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02003315
3316 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003317}
3318
3319void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3320 unsigned long *rflags)
3321{
3322 switch (cr) {
3323 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003324 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003325 *rflags = kvm_x86_ops->get_rflags(vcpu);
3326 break;
3327 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003328 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003329 break;
3330 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003331 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003332 break;
3333 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003334 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003335 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003336 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003337 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01003338 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003339 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003340 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003341 }
3342}
3343
Dan Kenigsberg07716712007-11-21 17:10:04 +02003344static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3345{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003346 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3347 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02003348
3349 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3350 /* when no next entry is found, the current entry[i] is reselected */
Nitin A Kamble0fdf8e52008-11-05 15:56:21 -08003351 for (j = i + 1; ; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003352 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003353 if (ej->function == e->function) {
3354 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3355 return j;
3356 }
3357 }
3358 return 0; /* silence gcc, even though control never reaches here */
3359}
3360
3361/* find an entry with matching function, matching index (if needed), and that
3362 * should be read next (if it's stateful) */
3363static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3364 u32 function, u32 index)
3365{
3366 if (e->function != function)
3367 return 0;
3368 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3369 return 0;
3370 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
Amit Shah19355472009-01-14 16:56:00 +00003371 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
Dan Kenigsberg07716712007-11-21 17:10:04 +02003372 return 0;
3373 return 1;
3374}
3375
Alexander Grafd8017472008-11-25 20:17:11 +01003376struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3377 u32 function, u32 index)
Hollis Blanchard8776e512007-10-31 17:24:24 -05003378{
3379 int i;
Alexander Grafd8017472008-11-25 20:17:11 +01003380 struct kvm_cpuid_entry2 *best = NULL;
Hollis Blanchard8776e512007-10-31 17:24:24 -05003381
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003382 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
Alexander Grafd8017472008-11-25 20:17:11 +01003383 struct kvm_cpuid_entry2 *e;
3384
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003385 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02003386 if (is_matching_cpuid_entry(e, function, index)) {
3387 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3388 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003389 best = e;
3390 break;
3391 }
3392 /*
3393 * Both basic or both extended?
3394 */
3395 if (((e->function ^ function) & 0x80000000) == 0)
3396 if (!best || e->function > best->function)
3397 best = e;
3398 }
Alexander Grafd8017472008-11-25 20:17:11 +01003399 return best;
3400}
3401
Dong, Eddie82725b22009-03-30 16:21:08 +08003402int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3403{
3404 struct kvm_cpuid_entry2 *best;
3405
3406 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3407 if (best)
3408 return best->eax & 0xff;
3409 return 36;
3410}
3411
Alexander Grafd8017472008-11-25 20:17:11 +01003412void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3413{
3414 u32 function, index;
3415 struct kvm_cpuid_entry2 *best;
3416
3417 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3418 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3419 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3420 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3421 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3422 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3423 best = kvm_find_cpuid_entry(vcpu, function, index);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003424 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003425 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3426 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3427 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3428 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05003429 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05003430 kvm_x86_ops->skip_emulated_instruction(vcpu);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003431 trace_kvm_cpuid(function,
3432 kvm_register_read(vcpu, VCPU_REGS_RAX),
3433 kvm_register_read(vcpu, VCPU_REGS_RBX),
3434 kvm_register_read(vcpu, VCPU_REGS_RCX),
3435 kvm_register_read(vcpu, VCPU_REGS_RDX));
Hollis Blanchard8776e512007-10-31 17:24:24 -05003436}
3437EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003438
3439/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003440 * Check if userspace requested an interrupt window, and that the
3441 * interrupt window is open.
3442 *
3443 * No need to exit to userspace if we already have an interrupt queued.
3444 */
3445static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
3446 struct kvm_run *kvm_run)
3447{
Gleb Natapov80618232009-04-21 17:44:56 +03003448 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003449 kvm_run->request_interrupt_window &&
Gleb Natapov5df56642009-04-21 17:44:59 +03003450 kvm_arch_interrupt_allowed(vcpu));
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003451}
3452
3453static void post_kvm_run_save(struct kvm_vcpu *vcpu,
3454 struct kvm_run *kvm_run)
3455{
3456 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003457 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003458 kvm_run->apic_base = kvm_get_apic_base(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01003459 if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003460 kvm_run->ready_for_interrupt_injection = 1;
Jan Kiszka45312202008-12-11 16:54:54 +01003461 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003462 kvm_run->ready_for_interrupt_injection =
Gleb Natapovfa9726b2009-05-11 13:35:47 +03003463 kvm_arch_interrupt_allowed(vcpu) &&
3464 !kvm_cpu_has_interrupt(vcpu) &&
3465 !kvm_event_needs_reinjection(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003466}
3467
Avi Kivityb93463a2007-10-25 16:52:32 +02003468static void vapic_enter(struct kvm_vcpu *vcpu)
3469{
3470 struct kvm_lapic *apic = vcpu->arch.apic;
3471 struct page *page;
3472
3473 if (!apic || !apic->vapic_addr)
3474 return;
3475
3476 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003477
3478 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02003479}
3480
3481static void vapic_exit(struct kvm_vcpu *vcpu)
3482{
3483 struct kvm_lapic *apic = vcpu->arch.apic;
3484
3485 if (!apic || !apic->vapic_addr)
3486 return;
3487
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003488 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003489 kvm_release_page_dirty(apic->vapic_page);
3490 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03003491 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02003492}
3493
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003494static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3495{
3496 int max_irr, tpr;
3497
3498 if (!kvm_x86_ops->update_cr8_intercept)
3499 return;
3500
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003501 if (!vcpu->arch.apic->vapic_addr)
3502 max_irr = kvm_lapic_find_highest_irr(vcpu);
3503 else
3504 max_irr = -1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003505
3506 if (max_irr != -1)
3507 max_irr >>= 4;
3508
3509 tpr = kvm_lapic_get_cr8(vcpu);
3510
3511 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3512}
3513
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03003514static void inject_pending_event(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003515{
3516 /* try to reinject previous events if any */
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03003517 if (vcpu->arch.exception.pending) {
3518 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
3519 vcpu->arch.exception.has_error_code,
3520 vcpu->arch.exception.error_code);
3521 return;
3522 }
3523
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003524 if (vcpu->arch.nmi_injected) {
3525 kvm_x86_ops->set_nmi(vcpu);
3526 return;
3527 }
3528
3529 if (vcpu->arch.interrupt.pending) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003530 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003531 return;
3532 }
3533
3534 /* try to inject new event if pending */
3535 if (vcpu->arch.nmi_pending) {
3536 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3537 vcpu->arch.nmi_pending = false;
3538 vcpu->arch.nmi_injected = true;
3539 kvm_x86_ops->set_nmi(vcpu);
3540 }
3541 } else if (kvm_cpu_has_interrupt(vcpu)) {
3542 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003543 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3544 false);
3545 kvm_x86_ops->set_irq(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003546 }
3547 }
3548}
3549
Marcelo Tosattid7690172008-09-08 15:23:48 -03003550static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003551{
3552 int r;
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003553 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
3554 kvm_run->request_interrupt_window;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003555
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05003556 if (vcpu->requests)
3557 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3558 kvm_mmu_unload(vcpu);
3559
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003560 r = kvm_mmu_reload(vcpu);
3561 if (unlikely(r))
3562 goto out;
3563
Avi Kivity2f52d582008-01-16 12:49:30 +02003564 if (vcpu->requests) {
3565 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03003566 __kvm_migrate_timers(vcpu);
Gerd Hoffmannc8076602009-02-04 17:52:04 +01003567 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3568 kvm_write_guest_time(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03003569 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3570 kvm_mmu_sync_roots(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03003571 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3572 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02003573 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3574 &vcpu->requests)) {
3575 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
3576 r = 0;
3577 goto out;
3578 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01003579 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3580 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
3581 r = 0;
3582 goto out;
3583 }
Avi Kivity2f52d582008-01-16 12:49:30 +02003584 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003585
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003586 preempt_disable();
3587
3588 kvm_x86_ops->prepare_guest_switch(vcpu);
3589 kvm_load_guest_fpu(vcpu);
3590
3591 local_irq_disable();
3592
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003593 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3594 smp_mb__after_clear_bit();
3595
Marcelo Tosattid7690172008-09-08 15:23:48 -03003596 if (vcpu->requests || need_resched() || signal_pending(current)) {
Gleb Natapovc7f0f242009-07-07 15:27:32 +03003597 set_bit(KVM_REQ_KICK, &vcpu->requests);
Avi Kivity6c142802008-01-15 18:27:32 +02003598 local_irq_enable();
3599 preempt_enable();
3600 r = 1;
3601 goto out;
3602 }
3603
Gleb Natapovb59bb7b2009-07-09 15:33:51 +03003604 inject_pending_event(vcpu, kvm_run);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003605
Gleb Natapov6a8b1d12009-05-11 13:35:51 +03003606 /* enable NMI/IRQ window open exits if needed */
3607 if (vcpu->arch.nmi_pending)
3608 kvm_x86_ops->enable_nmi_window(vcpu);
3609 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3610 kvm_x86_ops->enable_irq_window(vcpu);
3611
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003612 if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov8db3baa2009-05-11 13:35:54 +03003613 update_cr8_intercept(vcpu);
3614 kvm_lapic_sync_to_vapic(vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003615 }
Avi Kivityb93463a2007-10-25 16:52:32 +02003616
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003617 up_read(&vcpu->kvm->slots_lock);
3618
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003619 kvm_guest_enter();
3620
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003621 get_debugreg(vcpu->arch.host_dr6, 6);
3622 get_debugreg(vcpu->arch.host_dr7, 7);
3623 if (unlikely(vcpu->arch.switch_db_regs)) {
3624 get_debugreg(vcpu->arch.host_db[0], 0);
3625 get_debugreg(vcpu->arch.host_db[1], 1);
3626 get_debugreg(vcpu->arch.host_db[2], 2);
3627 get_debugreg(vcpu->arch.host_db[3], 3);
3628
3629 set_debugreg(0, 7);
3630 set_debugreg(vcpu->arch.eff_db[0], 0);
3631 set_debugreg(vcpu->arch.eff_db[1], 1);
3632 set_debugreg(vcpu->arch.eff_db[2], 2);
3633 set_debugreg(vcpu->arch.eff_db[3], 3);
3634 }
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003635
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003636 trace_kvm_entry(vcpu->vcpu_id);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003637 kvm_x86_ops->run(vcpu, kvm_run);
3638
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003639 if (unlikely(vcpu->arch.switch_db_regs)) {
3640 set_debugreg(0, 7);
3641 set_debugreg(vcpu->arch.host_db[0], 0);
3642 set_debugreg(vcpu->arch.host_db[1], 1);
3643 set_debugreg(vcpu->arch.host_db[2], 2);
3644 set_debugreg(vcpu->arch.host_db[3], 3);
3645 }
3646 set_debugreg(vcpu->arch.host_dr6, 6);
3647 set_debugreg(vcpu->arch.host_dr7, 7);
3648
Marcelo Tosatti32f88402009-05-07 17:55:12 -03003649 set_bit(KVM_REQ_KICK, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003650 local_irq_enable();
3651
3652 ++vcpu->stat.exits;
3653
3654 /*
3655 * We must have an instruction between local_irq_enable() and
3656 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3657 * the interrupt shadow. The stat.exits increment will do nicely.
3658 * But we need to prevent reordering, hence this barrier():
3659 */
3660 barrier();
3661
3662 kvm_guest_exit();
3663
3664 preempt_enable();
3665
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003666 down_read(&vcpu->kvm->slots_lock);
3667
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003668 /*
3669 * Profile KVM exit RIPs:
3670 */
3671 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003672 unsigned long rip = kvm_rip_read(vcpu);
3673 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003674 }
3675
Avi Kivity298101d2007-11-25 13:41:11 +02003676
Avi Kivityb93463a2007-10-25 16:52:32 +02003677 kvm_lapic_sync_from_vapic(vcpu);
3678
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003679 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003680out:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003681 return r;
3682}
3683
Gleb Natapov09cec752009-03-23 15:11:44 +02003684
Marcelo Tosattid7690172008-09-08 15:23:48 -03003685static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3686{
3687 int r;
3688
3689 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Jan Kiszka1b10bf32008-09-30 10:41:06 +02003690 pr_debug("vcpu %d received sipi with vector # %x\n",
3691 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003692 kvm_lapic_reset(vcpu);
Gleb Natapov5f179282008-10-07 15:42:33 +02003693 r = kvm_arch_vcpu_reset(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003694 if (r)
3695 return r;
3696 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003697 }
3698
Marcelo Tosattid7690172008-09-08 15:23:48 -03003699 down_read(&vcpu->kvm->slots_lock);
3700 vapic_enter(vcpu);
3701
3702 r = 1;
3703 while (r > 0) {
Gleb Natapovaf2152f2008-09-22 14:28:53 +03003704 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Marcelo Tosattid7690172008-09-08 15:23:48 -03003705 r = vcpu_enter_guest(vcpu, kvm_run);
3706 else {
3707 up_read(&vcpu->kvm->slots_lock);
3708 kvm_vcpu_block(vcpu);
3709 down_read(&vcpu->kvm->slots_lock);
3710 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
Gleb Natapov09cec752009-03-23 15:11:44 +02003711 {
3712 switch(vcpu->arch.mp_state) {
3713 case KVM_MP_STATE_HALTED:
Marcelo Tosattid7690172008-09-08 15:23:48 -03003714 vcpu->arch.mp_state =
Gleb Natapov09cec752009-03-23 15:11:44 +02003715 KVM_MP_STATE_RUNNABLE;
3716 case KVM_MP_STATE_RUNNABLE:
3717 break;
3718 case KVM_MP_STATE_SIPI_RECEIVED:
3719 default:
3720 r = -EINTR;
3721 break;
3722 }
3723 }
Marcelo Tosattid7690172008-09-08 15:23:48 -03003724 }
3725
Gleb Natapov09cec752009-03-23 15:11:44 +02003726 if (r <= 0)
3727 break;
3728
3729 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
3730 if (kvm_cpu_has_pending_timer(vcpu))
3731 kvm_inject_pending_timer_irqs(vcpu);
3732
3733 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
3734 r = -EINTR;
3735 kvm_run->exit_reason = KVM_EXIT_INTR;
3736 ++vcpu->stat.request_irq_exits;
3737 }
3738 if (signal_pending(current)) {
3739 r = -EINTR;
3740 kvm_run->exit_reason = KVM_EXIT_INTR;
3741 ++vcpu->stat.signal_exits;
3742 }
3743 if (need_resched()) {
3744 up_read(&vcpu->kvm->slots_lock);
3745 kvm_resched(vcpu);
3746 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003747 }
3748 }
3749
3750 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003751 post_kvm_run_save(vcpu, kvm_run);
3752
Avi Kivityb93463a2007-10-25 16:52:32 +02003753 vapic_exit(vcpu);
3754
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003755 return r;
3756}
3757
3758int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3759{
3760 int r;
3761 sigset_t sigsaved;
3762
3763 vcpu_load(vcpu);
3764
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003765 if (vcpu->sigset_active)
3766 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3767
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003768 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
3769 kvm_vcpu_block(vcpu);
Marcelo Tosattid7690172008-09-08 15:23:48 -03003770 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Avi Kivityac9f6dc2008-07-06 15:48:31 +03003771 r = -EAGAIN;
3772 goto out;
3773 }
3774
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003775 /* re-sync apic's tpr */
3776 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003777 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003778
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003779 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003780 r = complete_pio(vcpu);
3781 if (r)
3782 goto out;
3783 }
3784#if CONFIG_HAS_IOMEM
3785 if (vcpu->mmio_needed) {
3786 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3787 vcpu->mmio_read_completed = 1;
3788 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003789
3790 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003791 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003792 vcpu->arch.mmio_fault_cr2, 0,
3793 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003794 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003795 if (r == EMULATE_DO_MMIO) {
3796 /*
3797 * Read-modify-write. Back to userspace.
3798 */
3799 r = 0;
3800 goto out;
3801 }
3802 }
3803#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003804 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3805 kvm_register_write(vcpu, VCPU_REGS_RAX,
3806 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003807
3808 r = __vcpu_run(vcpu, kvm_run);
3809
3810out:
3811 if (vcpu->sigset_active)
3812 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3813
3814 vcpu_put(vcpu);
3815 return r;
3816}
3817
3818int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3819{
3820 vcpu_load(vcpu);
3821
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003822 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3823 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3824 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3825 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3826 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3827 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3828 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3829 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003830#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003831 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3832 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3833 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3834 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3835 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3836 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3837 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3838 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003839#endif
3840
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003841 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003842 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3843
3844 /*
3845 * Don't leak debug flags in case they were set for guest debugging
3846 */
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003847 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003848 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3849
3850 vcpu_put(vcpu);
3851
3852 return 0;
3853}
3854
3855int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3856{
3857 vcpu_load(vcpu);
3858
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003859 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3860 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3861 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3862 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3863 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3864 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3865 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3866 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003867#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003868 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3869 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3870 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3871 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3872 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3873 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3874 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3875 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3876
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003877#endif
3878
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003879 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003880 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3881
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003882
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003883 vcpu->arch.exception.pending = false;
3884
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003885 vcpu_put(vcpu);
3886
3887 return 0;
3888}
3889
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003890void kvm_get_segment(struct kvm_vcpu *vcpu,
3891 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003892{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003893 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003894}
3895
3896void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3897{
3898 struct kvm_segment cs;
3899
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003900 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003901 *db = cs.db;
3902 *l = cs.l;
3903}
3904EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3905
3906int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3907 struct kvm_sregs *sregs)
3908{
3909 struct descriptor_table dt;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003910
3911 vcpu_load(vcpu);
3912
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003913 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3914 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3915 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3916 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3917 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3918 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003919
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003920 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3921 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003922
3923 kvm_x86_ops->get_idt(vcpu, &dt);
3924 sregs->idt.limit = dt.limit;
3925 sregs->idt.base = dt.base;
3926 kvm_x86_ops->get_gdt(vcpu, &dt);
3927 sregs->gdt.limit = dt.limit;
3928 sregs->gdt.base = dt.base;
3929
3930 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003931 sregs->cr0 = vcpu->arch.cr0;
3932 sregs->cr2 = vcpu->arch.cr2;
3933 sregs->cr3 = vcpu->arch.cr3;
3934 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003935 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003936 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003937 sregs->apic_base = kvm_get_apic_base(vcpu);
3938
Gleb Natapov923c61b2009-05-11 13:35:48 +03003939 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003940
Gleb Natapov36752c92009-05-11 13:35:53 +03003941 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
Gleb Natapov14d0bc12009-04-21 17:45:11 +03003942 set_bit(vcpu->arch.interrupt.nr,
3943 (unsigned long *)sregs->interrupt_bitmap);
Gleb Natapov16d7a192009-04-21 17:45:10 +03003944
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003945 vcpu_put(vcpu);
3946
3947 return 0;
3948}
3949
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003950int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3951 struct kvm_mp_state *mp_state)
3952{
3953 vcpu_load(vcpu);
3954 mp_state->mp_state = vcpu->arch.mp_state;
3955 vcpu_put(vcpu);
3956 return 0;
3957}
3958
3959int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3960 struct kvm_mp_state *mp_state)
3961{
3962 vcpu_load(vcpu);
3963 vcpu->arch.mp_state = mp_state->mp_state;
3964 vcpu_put(vcpu);
3965 return 0;
3966}
3967
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003968static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003969 struct kvm_segment *var, int seg)
3970{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003971 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003972}
3973
Izik Eidus37817f22008-03-24 23:14:53 +02003974static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3975 struct kvm_segment *kvm_desct)
3976{
Akinobu Mita46a359e2009-07-18 23:58:32 +09003977 kvm_desct->base = get_desc_base(seg_desc);
3978 kvm_desct->limit = get_desc_limit(seg_desc);
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003979 if (seg_desc->g) {
3980 kvm_desct->limit <<= 12;
3981 kvm_desct->limit |= 0xfff;
3982 }
Izik Eidus37817f22008-03-24 23:14:53 +02003983 kvm_desct->selector = selector;
3984 kvm_desct->type = seg_desc->type;
3985 kvm_desct->present = seg_desc->p;
3986 kvm_desct->dpl = seg_desc->dpl;
3987 kvm_desct->db = seg_desc->d;
3988 kvm_desct->s = seg_desc->s;
3989 kvm_desct->l = seg_desc->l;
3990 kvm_desct->g = seg_desc->g;
3991 kvm_desct->avl = seg_desc->avl;
3992 if (!selector)
3993 kvm_desct->unusable = 1;
3994 else
3995 kvm_desct->unusable = 0;
3996 kvm_desct->padding = 0;
3997}
3998
Amit Shahb8222ad2008-10-22 16:39:47 +05303999static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4000 u16 selector,
4001 struct descriptor_table *dtable)
Izik Eidus37817f22008-03-24 23:14:53 +02004002{
4003 if (selector & 1 << 2) {
4004 struct kvm_segment kvm_seg;
4005
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004006 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004007
4008 if (kvm_seg.unusable)
4009 dtable->limit = 0;
4010 else
4011 dtable->limit = kvm_seg.limit;
4012 dtable->base = kvm_seg.base;
4013 }
4014 else
4015 kvm_x86_ops->get_gdt(vcpu, dtable);
4016}
4017
4018/* allowed just for 8 bytes segments */
4019static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4020 struct desc_struct *seg_desc)
4021{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004022 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02004023 struct descriptor_table dtable;
4024 u16 index = selector >> 3;
4025
Amit Shahb8222ad2008-10-22 16:39:47 +05304026 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004027
4028 if (dtable.limit < index * 8 + 7) {
4029 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4030 return 1;
4031 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004032 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
4033 gpa += index * 8;
4034 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02004035}
4036
4037/* allowed just for 8 bytes segments */
4038static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4039 struct desc_struct *seg_desc)
4040{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004041 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02004042 struct descriptor_table dtable;
4043 u16 index = selector >> 3;
4044
Amit Shahb8222ad2008-10-22 16:39:47 +05304045 get_segment_descriptor_dtable(vcpu, selector, &dtable);
Izik Eidus37817f22008-03-24 23:14:53 +02004046
4047 if (dtable.limit < index * 8 + 7)
4048 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004049 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
4050 gpa += index * 8;
4051 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02004052}
4053
4054static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
4055 struct desc_struct *seg_desc)
4056{
Akinobu Mita46a359e2009-07-18 23:58:32 +09004057 u32 base_addr = get_desc_base(seg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004058
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03004059 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02004060}
4061
Izik Eidus37817f22008-03-24 23:14:53 +02004062static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4063{
4064 struct kvm_segment kvm_seg;
4065
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004066 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004067 return kvm_seg.selector;
4068}
4069
4070static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4071 u16 selector,
4072 struct kvm_segment *kvm_seg)
4073{
4074 struct desc_struct seg_desc;
4075
4076 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4077 return 1;
4078 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4079 return 0;
4080}
4081
Harvey Harrison2259e3a2008-08-22 13:29:17 -07004082static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004083{
4084 struct kvm_segment segvar = {
4085 .base = selector << 4,
4086 .limit = 0xffff,
4087 .selector = selector,
4088 .type = 3,
4089 .present = 1,
4090 .dpl = 3,
4091 .db = 0,
4092 .s = 1,
4093 .l = 0,
4094 .g = 0,
4095 .avl = 0,
4096 .unusable = 0,
4097 };
4098 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4099 return 0;
4100}
4101
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004102int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4103 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02004104{
4105 struct kvm_segment kvm_seg;
4106
Avi Kivityf4bbd9a2008-08-20 15:51:42 +03004107 if (!(vcpu->arch.cr0 & X86_CR0_PE))
4108 return kvm_load_realmode_segment(vcpu, selector, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004109 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4110 return 1;
4111 kvm_seg.type |= type_bits;
4112
4113 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4114 seg != VCPU_SREG_LDTR)
4115 if (!kvm_seg.s)
4116 kvm_seg.unusable = 1;
4117
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004118 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02004119 return 0;
4120}
4121
4122static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4123 struct tss_segment_32 *tss)
4124{
4125 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004126 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004127 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004128 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4129 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4130 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4131 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4132 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4133 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4134 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4135 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004136 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4137 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4138 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4139 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4140 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4141 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4142 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02004143}
4144
4145static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4146 struct tss_segment_32 *tss)
4147{
4148 kvm_set_cr3(vcpu, tss->cr3);
4149
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004150 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02004151 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
4152
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004153 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4154 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4155 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4156 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4157 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4158 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4159 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4160 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02004161
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004162 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004163 return 1;
4164
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004165 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004166 return 1;
4167
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004168 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004169 return 1;
4170
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004171 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004172 return 1;
4173
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004174 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004175 return 1;
4176
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004177 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02004178 return 1;
4179
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004180 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02004181 return 1;
4182 return 0;
4183}
4184
4185static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4186 struct tss_segment_16 *tss)
4187{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004188 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004189 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004190 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4191 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4192 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4193 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4194 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4195 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4196 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4197 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02004198
4199 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4200 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4201 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4202 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4203 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4204 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
4205}
4206
4207static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4208 struct tss_segment_16 *tss)
4209{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004210 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02004211 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004212 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4213 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4214 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4215 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4216 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4217 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4218 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4219 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02004220
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004221 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02004222 return 1;
4223
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004224 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02004225 return 1;
4226
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004227 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02004228 return 1;
4229
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004230 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02004231 return 1;
4232
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004233 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02004234 return 1;
4235 return 0;
4236}
4237
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004238static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004239 u16 old_tss_sel, u32 old_tss_base,
4240 struct desc_struct *nseg_desc)
Izik Eidus37817f22008-03-24 23:14:53 +02004241{
4242 struct tss_segment_16 tss_segment_16;
4243 int ret = 0;
4244
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004245 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4246 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004247 goto out;
4248
4249 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02004250
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004251 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4252 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02004253 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004254
4255 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4256 &tss_segment_16, sizeof tss_segment_16))
4257 goto out;
4258
Gleb Natapovb237ac32009-03-30 16:03:24 +03004259 if (old_tss_sel != 0xffff) {
4260 tss_segment_16.prev_task_link = old_tss_sel;
4261
4262 if (kvm_write_guest(vcpu->kvm,
4263 get_tss_base_addr(vcpu, nseg_desc),
4264 &tss_segment_16.prev_task_link,
4265 sizeof tss_segment_16.prev_task_link))
4266 goto out;
4267 }
4268
Izik Eidus37817f22008-03-24 23:14:53 +02004269 if (load_state_from_tss16(vcpu, &tss_segment_16))
4270 goto out;
4271
4272 ret = 1;
4273out:
4274 return ret;
4275}
4276
Harvey Harrison8b2cf732008-04-27 12:14:13 -07004277static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Gleb Natapovb237ac32009-03-30 16:03:24 +03004278 u16 old_tss_sel, u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02004279 struct desc_struct *nseg_desc)
4280{
4281 struct tss_segment_32 tss_segment_32;
4282 int ret = 0;
4283
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004284 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4285 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004286 goto out;
4287
4288 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02004289
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004290 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4291 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02004292 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004293
4294 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4295 &tss_segment_32, sizeof tss_segment_32))
4296 goto out;
4297
Gleb Natapovb237ac32009-03-30 16:03:24 +03004298 if (old_tss_sel != 0xffff) {
4299 tss_segment_32.prev_task_link = old_tss_sel;
4300
4301 if (kvm_write_guest(vcpu->kvm,
4302 get_tss_base_addr(vcpu, nseg_desc),
4303 &tss_segment_32.prev_task_link,
4304 sizeof tss_segment_32.prev_task_link))
4305 goto out;
4306 }
4307
Izik Eidus37817f22008-03-24 23:14:53 +02004308 if (load_state_from_tss32(vcpu, &tss_segment_32))
4309 goto out;
4310
4311 ret = 1;
4312out:
4313 return ret;
4314}
4315
4316int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4317{
4318 struct kvm_segment tr_seg;
4319 struct desc_struct cseg_desc;
4320 struct desc_struct nseg_desc;
4321 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004322 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4323 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004324
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004325 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02004326
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004327 /* FIXME: Handle errors. Failure to read either TSS or their
4328 * descriptors should generate a pagefault.
4329 */
Izik Eidus37817f22008-03-24 23:14:53 +02004330 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4331 goto out;
4332
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004333 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02004334 goto out;
4335
Izik Eidus37817f22008-03-24 23:14:53 +02004336 if (reason != TASK_SWITCH_IRET) {
4337 int cpl;
4338
4339 cpl = kvm_x86_ops->get_cpl(vcpu);
4340 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4341 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4342 return 1;
4343 }
4344 }
4345
Akinobu Mita46a359e2009-07-18 23:58:32 +09004346 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
Izik Eidus37817f22008-03-24 23:14:53 +02004347 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4348 return 1;
4349 }
4350
4351 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004352 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03004353 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004354 }
4355
4356 if (reason == TASK_SWITCH_IRET) {
4357 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4358 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
4359 }
4360
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004361 /* set back link to prev task only if NT bit is set in eflags
4362 note that old_tss_sel is not used afetr this point */
4363 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4364 old_tss_sel = 0xffff;
Izik Eidus37817f22008-03-24 23:14:53 +02004365
Gleb Natapovb237ac32009-03-30 16:03:24 +03004366 /* set back link to prev task only if NT bit is set in eflags
4367 note that old_tss_sel is not used afetr this point */
4368 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4369 old_tss_sel = 0xffff;
4370
Izik Eidus37817f22008-03-24 23:14:53 +02004371 if (nseg_desc.type & 8)
Gleb Natapovb237ac32009-03-30 16:03:24 +03004372 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4373 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004374 else
Gleb Natapovb237ac32009-03-30 16:03:24 +03004375 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4376 old_tss_base, &nseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02004377
4378 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
4379 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
4380 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
4381 }
4382
4383 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03004384 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02004385 save_guest_segment_descriptor(vcpu, tss_selector,
4386 &nseg_desc);
4387 }
4388
4389 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4390 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4391 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004392 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02004393out:
Izik Eidus37817f22008-03-24 23:14:53 +02004394 return ret;
4395}
4396EXPORT_SYMBOL_GPL(kvm_task_switch);
4397
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004398int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4399 struct kvm_sregs *sregs)
4400{
4401 int mmu_reset_needed = 0;
Gleb Natapov923c61b2009-05-11 13:35:48 +03004402 int pending_vec, max_bits;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004403 struct descriptor_table dt;
4404
4405 vcpu_load(vcpu);
4406
4407 dt.limit = sregs->idt.limit;
4408 dt.base = sregs->idt.base;
4409 kvm_x86_ops->set_idt(vcpu, &dt);
4410 dt.limit = sregs->gdt.limit;
4411 dt.base = sregs->gdt.base;
4412 kvm_x86_ops->set_gdt(vcpu, &dt);
4413
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004414 vcpu->arch.cr2 = sregs->cr2;
4415 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
Jan Kiszkadc7e7952009-07-01 20:52:03 +02004416 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004417
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004418 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004419
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004420 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004421 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004422 kvm_set_apic_base(vcpu, sregs->apic_base);
4423
4424 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
4425
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004426 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004427 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00004428 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004429
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004430 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004431 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4432 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004433 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004434
4435 if (mmu_reset_needed)
4436 kvm_mmu_reset_context(vcpu);
4437
Gleb Natapov923c61b2009-05-11 13:35:48 +03004438 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4439 pending_vec = find_first_bit(
4440 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4441 if (pending_vec < max_bits) {
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004442 kvm_queue_interrupt(vcpu, pending_vec, false);
Gleb Natapov923c61b2009-05-11 13:35:48 +03004443 pr_debug("Set back pending irq %d\n", pending_vec);
4444 if (irqchip_in_kernel(vcpu->kvm))
4445 kvm_pic_clear_isr_ack(vcpu->kvm);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004446 }
4447
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004448 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4449 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4450 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4451 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4452 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4453 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004454
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02004455 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4456 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004457
Mikhail Ershov5f0269f2009-08-03 14:58:25 +03004458 update_cr8_intercept(vcpu);
4459
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004460 /* Older userspace won't unhalt the vcpu on reset. */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004461 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
Marcelo Tosatti9c3e4aa2008-09-10 16:40:55 -03004462 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4463 !(vcpu->arch.cr0 & X86_CR0_PE))
4464 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4465
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004466 vcpu_put(vcpu);
4467
4468 return 0;
4469}
4470
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004471int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4472 struct kvm_guest_debug *dbg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004473{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004474 int i, r;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004475
4476 vcpu_load(vcpu);
4477
Jan Kiszkaae675ef2008-12-15 13:52:10 +01004478 if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) ==
4479 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) {
4480 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4481 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4482 vcpu->arch.switch_db_regs =
4483 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4484 } else {
4485 for (i = 0; i < KVM_NR_DB_REGS; i++)
4486 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4487 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4488 }
4489
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004490 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
4491
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004492 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4493 kvm_queue_exception(vcpu, DB_VECTOR);
4494 else if (dbg->control & KVM_GUESTDBG_INJECT_BP)
4495 kvm_queue_exception(vcpu, BP_VECTOR);
4496
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05004497 vcpu_put(vcpu);
4498
4499 return r;
4500}
4501
4502/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05004503 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4504 * we have asm/x86/processor.h
4505 */
4506struct fxsave {
4507 u16 cwd;
4508 u16 swd;
4509 u16 twd;
4510 u16 fop;
4511 u64 rip;
4512 u64 rdp;
4513 u32 mxcsr;
4514 u32 mxcsr_mask;
4515 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4516#ifdef CONFIG_X86_64
4517 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4518#else
4519 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4520#endif
4521};
4522
Zhang Xiantao8b006792007-11-16 13:05:55 +08004523/*
4524 * Translate a guest virtual address to a guest physical address.
4525 */
4526int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4527 struct kvm_translation *tr)
4528{
4529 unsigned long vaddr = tr->linear_address;
4530 gpa_t gpa;
4531
4532 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004533 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004534 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004535 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08004536 tr->physical_address = gpa;
4537 tr->valid = gpa != UNMAPPED_GVA;
4538 tr->writeable = 1;
4539 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08004540 vcpu_put(vcpu);
4541
4542 return 0;
4543}
4544
Hollis Blanchardd0752062007-10-31 17:24:25 -05004545int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4546{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004547 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004548
4549 vcpu_load(vcpu);
4550
4551 memcpy(fpu->fpr, fxsave->st_space, 128);
4552 fpu->fcw = fxsave->cwd;
4553 fpu->fsw = fxsave->swd;
4554 fpu->ftwx = fxsave->twd;
4555 fpu->last_opcode = fxsave->fop;
4556 fpu->last_ip = fxsave->rip;
4557 fpu->last_dp = fxsave->rdp;
4558 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4559
4560 vcpu_put(vcpu);
4561
4562 return 0;
4563}
4564
4565int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4566{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004567 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004568
4569 vcpu_load(vcpu);
4570
4571 memcpy(fxsave->st_space, fpu->fpr, 128);
4572 fxsave->cwd = fpu->fcw;
4573 fxsave->swd = fpu->fsw;
4574 fxsave->twd = fpu->ftwx;
4575 fxsave->fop = fpu->last_opcode;
4576 fxsave->rip = fpu->last_ip;
4577 fxsave->rdp = fpu->last_dp;
4578 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4579
4580 vcpu_put(vcpu);
4581
4582 return 0;
4583}
4584
4585void fx_init(struct kvm_vcpu *vcpu)
4586{
4587 unsigned after_mxcsr_mask;
4588
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004589 /*
4590 * Touch the fpu the first time in non atomic context as if
4591 * this is the first fpu instruction the exception handler
4592 * will fire before the instruction returns and it'll have to
4593 * allocate ram with GFP_KERNEL.
4594 */
4595 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004596 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02004597
Hollis Blanchardd0752062007-10-31 17:24:25 -05004598 /* Initialize guest FPU by resetting ours and saving into guest's */
4599 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004600 kvm_fx_save(&vcpu->arch.host_fx_image);
4601 kvm_fx_finit();
4602 kvm_fx_save(&vcpu->arch.guest_fx_image);
4603 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004604 preempt_enable();
4605
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004606 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004607 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004608 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4609 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05004610 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4611}
4612EXPORT_SYMBOL_GPL(fx_init);
4613
4614void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4615{
4616 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4617 return;
4618
4619 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004620 kvm_fx_save(&vcpu->arch.host_fx_image);
4621 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05004622}
4623EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4624
4625void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4626{
4627 if (!vcpu->guest_fpu_loaded)
4628 return;
4629
4630 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03004631 kvm_fx_save(&vcpu->arch.guest_fx_image);
4632 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02004633 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05004634}
4635EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004636
4637void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4638{
Joerg Roedel7f1ea202009-02-25 16:08:31 +01004639 if (vcpu->arch.time_page) {
4640 kvm_release_page_dirty(vcpu->arch.time_page);
4641 vcpu->arch.time_page = NULL;
4642 }
4643
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004644 kvm_x86_ops->vcpu_free(vcpu);
4645}
4646
4647struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
4648 unsigned int id)
4649{
Avi Kivity26e52152007-11-20 15:30:24 +02004650 return kvm_x86_ops->vcpu_create(kvm, id);
4651}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004652
Avi Kivity26e52152007-11-20 15:30:24 +02004653int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
4654{
4655 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004656
4657 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004658 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004659
Sheng Yang0bed3b52008-10-09 16:01:54 +08004660 vcpu->arch.mtrr_state.have_fixed = 1;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004661 vcpu_load(vcpu);
4662 r = kvm_arch_vcpu_reset(vcpu);
4663 if (r == 0)
4664 r = kvm_mmu_setup(vcpu);
4665 vcpu_put(vcpu);
4666 if (r < 0)
4667 goto free_vcpu;
4668
Avi Kivity26e52152007-11-20 15:30:24 +02004669 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004670free_vcpu:
4671 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02004672 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004673}
4674
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06004675void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004676{
4677 vcpu_load(vcpu);
4678 kvm_mmu_unload(vcpu);
4679 vcpu_put(vcpu);
4680
4681 kvm_x86_ops->vcpu_free(vcpu);
4682}
4683
4684int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
4685{
Jan Kiszka448fa4a2008-09-26 09:30:48 +02004686 vcpu->arch.nmi_pending = false;
4687 vcpu->arch.nmi_injected = false;
4688
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004689 vcpu->arch.switch_db_regs = 0;
4690 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
4691 vcpu->arch.dr6 = DR6_FIXED_1;
4692 vcpu->arch.dr7 = DR7_FIXED_1;
4693
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004694 return kvm_x86_ops->vcpu_reset(vcpu);
4695}
4696
4697void kvm_arch_hardware_enable(void *garbage)
4698{
4699 kvm_x86_ops->hardware_enable(garbage);
4700}
4701
4702void kvm_arch_hardware_disable(void *garbage)
4703{
4704 kvm_x86_ops->hardware_disable(garbage);
4705}
4706
4707int kvm_arch_hardware_setup(void)
4708{
4709 return kvm_x86_ops->hardware_setup();
4710}
4711
4712void kvm_arch_hardware_unsetup(void)
4713{
4714 kvm_x86_ops->hardware_unsetup();
4715}
4716
4717void kvm_arch_check_processor_compat(void *rtn)
4718{
4719 kvm_x86_ops->check_processor_compatibility(rtn);
4720}
4721
4722int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
4723{
4724 struct page *page;
4725 struct kvm *kvm;
4726 int r;
4727
4728 BUG_ON(vcpu->kvm == NULL);
4729 kvm = vcpu->kvm;
4730
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004731 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004732 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
Avi Kivitya4535292008-04-13 17:54:35 +03004733 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004734 else
Avi Kivitya4535292008-04-13 17:54:35 +03004735 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004736
4737 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
4738 if (!page) {
4739 r = -ENOMEM;
4740 goto fail;
4741 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004742 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004743
4744 r = kvm_mmu_create(vcpu);
4745 if (r < 0)
4746 goto fail_free_pio_data;
4747
4748 if (irqchip_in_kernel(kvm)) {
4749 r = kvm_create_lapic(vcpu);
4750 if (r < 0)
4751 goto fail_mmu_destroy;
4752 }
4753
Huang Ying890ca9a2009-05-11 16:48:15 +08004754 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
4755 GFP_KERNEL);
4756 if (!vcpu->arch.mce_banks) {
4757 r = -ENOMEM;
4758 goto fail_mmu_destroy;
4759 }
4760 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
4761
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004762 return 0;
4763
4764fail_mmu_destroy:
4765 kvm_mmu_destroy(vcpu);
4766fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004767 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004768fail:
4769 return r;
4770}
4771
4772void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
4773{
4774 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004775 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004776 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004777 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004778 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004779}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004780
4781struct kvm *kvm_arch_create_vm(void)
4782{
4783 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
4784
4785 if (!kvm)
4786 return ERR_PTR(-ENOMEM);
4787
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004788 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Ben-Ami Yassour4d5c5d02008-07-28 19:26:26 +03004789 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004790
Sheng Yang5550af42008-10-15 20:15:06 +08004791 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4792 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
4793
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01004794 rdtscll(kvm->arch.vm_init_tsc);
4795
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004796 return kvm;
4797}
4798
4799static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
4800{
4801 vcpu_load(vcpu);
4802 kvm_mmu_unload(vcpu);
4803 vcpu_put(vcpu);
4804}
4805
4806static void kvm_free_vcpus(struct kvm *kvm)
4807{
4808 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03004809 struct kvm_vcpu *vcpu;
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004810
4811 /*
4812 * Unpin any mmu pages first.
4813 */
Gleb Natapov988a2ca2009-06-09 15:56:29 +03004814 kvm_for_each_vcpu(i, vcpu, kvm)
4815 kvm_unload_vcpu_mmu(vcpu);
4816 kvm_for_each_vcpu(i, vcpu, kvm)
4817 kvm_arch_vcpu_free(vcpu);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004818
Gleb Natapov988a2ca2009-06-09 15:56:29 +03004819 mutex_lock(&kvm->lock);
4820 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
4821 kvm->vcpus[i] = NULL;
4822
4823 atomic_set(&kvm->online_vcpus, 0);
4824 mutex_unlock(&kvm->lock);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004825}
4826
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004827void kvm_arch_sync_events(struct kvm *kvm)
4828{
Sheng Yangba4cef32009-01-06 10:03:03 +08004829 kvm_free_all_assigned_devices(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +08004830}
4831
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004832void kvm_arch_destroy_vm(struct kvm *kvm)
4833{
Sheng Yang6eb55812008-10-31 12:37:41 +08004834 kvm_iommu_unmap_guest(kvm);
Sheng Yang78376992008-01-28 05:10:22 +08004835 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08004836 kfree(kvm->arch.vpic);
4837 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004838 kvm_free_vcpus(kvm);
4839 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02004840 if (kvm->arch.apic_access_page)
4841 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004842 if (kvm->arch.ept_identity_pagetable)
4843 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08004844 kfree(kvm);
4845}
Zhang Xiantao0de10342007-11-20 16:25:04 +08004846
4847int kvm_arch_set_memory_region(struct kvm *kvm,
4848 struct kvm_userspace_memory_region *mem,
4849 struct kvm_memory_slot old,
4850 int user_alloc)
4851{
4852 int npages = mem->memory_size >> PAGE_SHIFT;
4853 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4854
4855 /*To keep backward compatibility with older userspace,
4856 *x86 needs to hanlde !user_alloc case.
4857 */
4858 if (!user_alloc) {
4859 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004860 unsigned long userspace_addr;
4861
Izik Eidus72dc67a2008-02-10 18:04:15 +02004862 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004863 userspace_addr = do_mmap(NULL, 0,
4864 npages * PAGE_SIZE,
4865 PROT_READ | PROT_WRITE,
Avi Kivityacee3c02008-08-26 17:22:47 +03004866 MAP_PRIVATE | MAP_ANONYMOUS,
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004867 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004868 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004869
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02004870 if (IS_ERR((void *)userspace_addr))
4871 return PTR_ERR((void *)userspace_addr);
4872
4873 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4874 spin_lock(&kvm->mmu_lock);
4875 memslot->userspace_addr = userspace_addr;
4876 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004877 } else {
4878 if (!old.user_alloc && old.rmap) {
4879 int ret;
4880
Izik Eidus72dc67a2008-02-10 18:04:15 +02004881 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004882 ret = do_munmap(current->mm, old.userspace_addr,
4883 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004884 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004885 if (ret < 0)
4886 printk(KERN_WARNING
4887 "kvm_vm_ioctl_set_memory_region: "
4888 "failed to munmap memory\n");
4889 }
4890 }
4891 }
4892
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004893 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004894 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004895 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4896 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4897 }
4898
4899 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
Marcelo Tosatti7c8a83b2009-05-12 18:55:43 -03004900 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004901 kvm_flush_remote_tlbs(kvm);
4902
4903 return 0;
4904}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004905
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004906void kvm_arch_flush_shadow(struct kvm *kvm)
4907{
4908 kvm_mmu_zap_all(kvm);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03004909 kvm_reload_remote_mmus(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004910}
4911
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004912int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4913{
Avi Kivitya4535292008-04-13 17:54:35 +03004914 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Gleb Natapova1b37102009-07-09 15:33:52 +03004915 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
4916 || vcpu->arch.nmi_pending ||
4917 (kvm_arch_interrupt_allowed(vcpu) &&
4918 kvm_cpu_has_interrupt(vcpu));
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004919}
Zhang Xiantao57361992007-12-17 14:21:40 +08004920
Zhang Xiantao57361992007-12-17 14:21:40 +08004921void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4922{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004923 int me;
4924 int cpu = vcpu->cpu;
Zhang Xiantao57361992007-12-17 14:21:40 +08004925
4926 if (waitqueue_active(&vcpu->wq)) {
4927 wake_up_interruptible(&vcpu->wq);
4928 ++vcpu->stat.halt_wakeup;
4929 }
Marcelo Tosatti32f88402009-05-07 17:55:12 -03004930
4931 me = get_cpu();
4932 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
4933 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
4934 smp_send_reschedule(cpu);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004935 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004936}
Gleb Natapov78646122009-03-23 12:12:11 +02004937
4938int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
4939{
4940 return kvm_x86_ops->interrupt_allowed(vcpu);
4941}
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004942
4943EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
4944EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
4945EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
4946EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
4947EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);