blob: 2f0696bc7d2f63b0a458709ff1d6736d4144e7af [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.
7 *
8 * Authors:
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
16
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Sheng Yang78376992008-01-28 05:10:22 +080020#include "i8254.h"
Izik Eidus37817f22008-03-24 23:14:53 +020021#include "tss.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030022#include "kvm_cache_regs.h"
Carsten Otte313a3dc2007-10-11 19:16:52 +020023
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020024#include <linux/clocksource.h>
Carsten Otte313a3dc2007-10-11 19:16:52 +020025#include <linux/kvm.h>
26#include <linux/fs.h>
27#include <linux/vmalloc.h>
Carsten Otte5fb76f92007-10-29 16:08:51 +010028#include <linux/module.h>
Zhang Xiantao0de10342007-11-20 16:25:04 +080029#include <linux/mman.h>
Marcelo Tosatti2bacc552007-12-12 10:46:12 -050030#include <linux/highmem.h>
Carsten Otte043405e2007-10-10 17:16:19 +020031
32#include <asm/uaccess.h>
Zhang Xiantaod825ed02007-11-14 20:08:51 +080033#include <asm/msr.h>
Avi Kivitya5f61302008-02-20 17:57:21 +020034#include <asm/desc.h>
Carsten Otte043405e2007-10-10 17:16:19 +020035
Carsten Otte313a3dc2007-10-11 19:16:52 +020036#define MAX_IO_MSRS 256
Carsten Ottea03490e2007-10-29 16:09:35 +010037#define CR0_RESERVED_BITS \
38 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
39 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
40 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
41#define CR4_RESERVED_BITS \
42 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
43 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
44 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
45 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
46
47#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
Joerg Roedel50a37eb2008-01-31 14:57:38 +010048/* EFER defaults:
49 * - enable syscall per default because its emulated by KVM
50 * - enable LME and LMA per default on 64 bit KVM
51 */
52#ifdef CONFIG_X86_64
53static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
54#else
55static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
56#endif
Carsten Otte313a3dc2007-10-11 19:16:52 +020057
Avi Kivityba1389b2007-11-18 16:24:12 +020058#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
59#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
Hollis Blanchard417bc302007-10-31 17:24:23 -050060
Avi Kivity674eea02008-02-11 18:37:23 +020061static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
62 struct kvm_cpuid_entry2 __user *entries);
63
Zhang Xiantao97896d02007-11-14 20:09:30 +080064struct kvm_x86_ops *kvm_x86_ops;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030065EXPORT_SYMBOL_GPL(kvm_x86_ops);
Zhang Xiantao97896d02007-11-14 20:09:30 +080066
Hollis Blanchard417bc302007-10-31 17:24:23 -050067struct kvm_stats_debugfs_item debugfs_entries[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +020068 { "pf_fixed", VCPU_STAT(pf_fixed) },
69 { "pf_guest", VCPU_STAT(pf_guest) },
70 { "tlb_flush", VCPU_STAT(tlb_flush) },
71 { "invlpg", VCPU_STAT(invlpg) },
72 { "exits", VCPU_STAT(exits) },
73 { "io_exits", VCPU_STAT(io_exits) },
74 { "mmio_exits", VCPU_STAT(mmio_exits) },
75 { "signal_exits", VCPU_STAT(signal_exits) },
76 { "irq_window", VCPU_STAT(irq_window_exits) },
Sheng Yangf08864b2008-05-15 18:23:25 +080077 { "nmi_window", VCPU_STAT(nmi_window_exits) },
Avi Kivityba1389b2007-11-18 16:24:12 +020078 { "halt_exits", VCPU_STAT(halt_exits) },
79 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Amit Shahf11c3a82008-02-21 01:00:30 +053080 { "hypercalls", VCPU_STAT(hypercalls) },
Avi Kivityba1389b2007-11-18 16:24:12 +020081 { "request_irq", VCPU_STAT(request_irq_exits) },
82 { "irq_exits", VCPU_STAT(irq_exits) },
83 { "host_state_reload", VCPU_STAT(host_state_reload) },
84 { "efer_reload", VCPU_STAT(efer_reload) },
85 { "fpu_reload", VCPU_STAT(fpu_reload) },
86 { "insn_emulation", VCPU_STAT(insn_emulation) },
87 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
Avi Kivity4cee5762007-11-18 16:37:07 +020088 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
89 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
90 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
91 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
92 { "mmu_flooded", VM_STAT(mmu_flooded) },
93 { "mmu_recycled", VM_STAT(mmu_recycled) },
Avi Kivitydfc5aa02007-12-18 19:47:18 +020094 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
Avi Kivity0f74a242007-11-20 23:01:14 +020095 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Marcelo Tosatti05da4552008-02-23 11:44:30 -030096 { "largepages", VM_STAT(lpages) },
Hollis Blanchard417bc302007-10-31 17:24:23 -050097 { NULL }
98};
99
100
Carsten Otte5fb76f92007-10-29 16:08:51 +0100101unsigned long segment_base(u16 selector)
102{
103 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200104 struct desc_struct *d;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100105 unsigned long table_base;
106 unsigned long v;
107
108 if (selector == 0)
109 return 0;
110
111 asm("sgdt %0" : "=m"(gdt));
112 table_base = gdt.base;
113
114 if (selector & 4) { /* from ldt */
115 u16 ldt_selector;
116
117 asm("sldt %0" : "=g"(ldt_selector));
118 table_base = segment_base(ldt_selector);
119 }
Avi Kivitya5f61302008-02-20 17:57:21 +0200120 d = (struct desc_struct *)(table_base + (selector & ~7));
121 v = d->base0 | ((unsigned long)d->base1 << 16) |
122 ((unsigned long)d->base2 << 24);
Carsten Otte5fb76f92007-10-29 16:08:51 +0100123#ifdef CONFIG_X86_64
Avi Kivitya5f61302008-02-20 17:57:21 +0200124 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
125 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
Carsten Otte5fb76f92007-10-29 16:08:51 +0100126#endif
127 return v;
128}
129EXPORT_SYMBOL_GPL(segment_base);
130
Carsten Otte6866b832007-10-29 16:09:10 +0100131u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
132{
133 if (irqchip_in_kernel(vcpu->kvm))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800134 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100135 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800136 return vcpu->arch.apic_base;
Carsten Otte6866b832007-10-29 16:09:10 +0100137}
138EXPORT_SYMBOL_GPL(kvm_get_apic_base);
139
140void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
141{
142 /* TODO: reserve bits check */
143 if (irqchip_in_kernel(vcpu->kvm))
144 kvm_lapic_set_base(vcpu, data);
145 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800146 vcpu->arch.apic_base = data;
Carsten Otte6866b832007-10-29 16:09:10 +0100147}
148EXPORT_SYMBOL_GPL(kvm_set_apic_base);
149
Avi Kivity298101d2007-11-25 13:41:11 +0200150void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
151{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800152 WARN_ON(vcpu->arch.exception.pending);
153 vcpu->arch.exception.pending = true;
154 vcpu->arch.exception.has_error_code = false;
155 vcpu->arch.exception.nr = nr;
Avi Kivity298101d2007-11-25 13:41:11 +0200156}
157EXPORT_SYMBOL_GPL(kvm_queue_exception);
158
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200159void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
160 u32 error_code)
161{
162 ++vcpu->stat.pf_guest;
Joerg Roedel71c4dfa2008-02-26 16:49:16 +0100163 if (vcpu->arch.exception.pending) {
164 if (vcpu->arch.exception.nr == PF_VECTOR) {
165 printk(KERN_DEBUG "kvm: inject_page_fault:"
166 " double fault 0x%lx\n", addr);
167 vcpu->arch.exception.nr = DF_VECTOR;
168 vcpu->arch.exception.error_code = 0;
169 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
170 /* triple fault -> shutdown */
171 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
172 }
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200173 return;
174 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800175 vcpu->arch.cr2 = addr;
Avi Kivityc3c91fe2007-11-25 14:04:58 +0200176 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
177}
178
Sheng Yang3419ffc2008-05-15 09:52:48 +0800179void kvm_inject_nmi(struct kvm_vcpu *vcpu)
180{
181 vcpu->arch.nmi_pending = 1;
182}
183EXPORT_SYMBOL_GPL(kvm_inject_nmi);
184
Avi Kivity298101d2007-11-25 13:41:11 +0200185void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
186{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800187 WARN_ON(vcpu->arch.exception.pending);
188 vcpu->arch.exception.pending = true;
189 vcpu->arch.exception.has_error_code = true;
190 vcpu->arch.exception.nr = nr;
191 vcpu->arch.exception.error_code = error_code;
Avi Kivity298101d2007-11-25 13:41:11 +0200192}
193EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
194
195static void __queue_exception(struct kvm_vcpu *vcpu)
196{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800197 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
198 vcpu->arch.exception.has_error_code,
199 vcpu->arch.exception.error_code);
Avi Kivity298101d2007-11-25 13:41:11 +0200200}
201
Carsten Ottea03490e2007-10-29 16:09:35 +0100202/*
203 * Load the pae pdptrs. Return true is they are all valid.
204 */
205int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
206{
207 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
208 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
209 int i;
210 int ret;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800211 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Carsten Ottea03490e2007-10-29 16:09:35 +0100212
Carsten Ottea03490e2007-10-29 16:09:35 +0100213 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
214 offset * sizeof(u64), sizeof(pdpte));
215 if (ret < 0) {
216 ret = 0;
217 goto out;
218 }
219 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
220 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
221 ret = 0;
222 goto out;
223 }
224 }
225 ret = 1;
226
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800227 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
Carsten Ottea03490e2007-10-29 16:09:35 +0100228out:
Carsten Ottea03490e2007-10-29 16:09:35 +0100229
230 return ret;
231}
Joerg Roedelcc4b6872008-02-07 13:47:43 +0100232EXPORT_SYMBOL_GPL(load_pdptrs);
Carsten Ottea03490e2007-10-29 16:09:35 +0100233
Avi Kivityd835dfe2007-11-21 02:57:59 +0200234static bool pdptrs_changed(struct kvm_vcpu *vcpu)
235{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800236 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
Avi Kivityd835dfe2007-11-21 02:57:59 +0200237 bool changed = true;
238 int r;
239
240 if (is_long_mode(vcpu) || !is_pae(vcpu))
241 return false;
242
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
Avi Kivityd835dfe2007-11-21 02:57:59 +0200244 if (r < 0)
245 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800246 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
Avi Kivityd835dfe2007-11-21 02:57:59 +0200247out:
Avi Kivityd835dfe2007-11-21 02:57:59 +0200248
249 return changed;
250}
251
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200252void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Carsten Ottea03490e2007-10-29 16:09:35 +0100253{
254 if (cr0 & CR0_RESERVED_BITS) {
255 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800256 cr0, vcpu->arch.cr0);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200257 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100258 return;
259 }
260
261 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
262 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200263 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100264 return;
265 }
266
267 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
268 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
269 "and a clear PE flag\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200270 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100271 return;
272 }
273
274 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
275#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800276 if ((vcpu->arch.shadow_efer & EFER_LME)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100277 int cs_db, cs_l;
278
279 if (!is_pae(vcpu)) {
280 printk(KERN_DEBUG "set_cr0: #GP, start paging "
281 "in long mode while PAE is disabled\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200282 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100283 return;
284 }
285 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
286 if (cs_l) {
287 printk(KERN_DEBUG "set_cr0: #GP, start paging "
288 "in long mode while CS.L == 1\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200289 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100290 return;
291
292 }
293 } else
294#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800295 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100296 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
297 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200298 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100299 return;
300 }
301
302 }
303
304 kvm_x86_ops->set_cr0(vcpu, cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800305 vcpu->arch.cr0 = cr0;
Carsten Ottea03490e2007-10-29 16:09:35 +0100306
Carsten Ottea03490e2007-10-29 16:09:35 +0100307 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100308 return;
309}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200310EXPORT_SYMBOL_GPL(kvm_set_cr0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100311
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200312void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
Carsten Ottea03490e2007-10-29 16:09:35 +0100313{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200314 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -0400315 KVMTRACE_1D(LMSW, vcpu,
316 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
317 handler);
Carsten Ottea03490e2007-10-29 16:09:35 +0100318}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200319EXPORT_SYMBOL_GPL(kvm_lmsw);
Carsten Ottea03490e2007-10-29 16:09:35 +0100320
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200321void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Carsten Ottea03490e2007-10-29 16:09:35 +0100322{
323 if (cr4 & CR4_RESERVED_BITS) {
324 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200325 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100326 return;
327 }
328
329 if (is_long_mode(vcpu)) {
330 if (!(cr4 & X86_CR4_PAE)) {
331 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
332 "in long mode\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200333 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100334 return;
335 }
336 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800337 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
Carsten Ottea03490e2007-10-29 16:09:35 +0100338 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200339 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100340 return;
341 }
342
343 if (cr4 & X86_CR4_VMXE) {
344 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200345 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100346 return;
347 }
348 kvm_x86_ops->set_cr4(vcpu, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800349 vcpu->arch.cr4 = cr4;
Carsten Ottea03490e2007-10-29 16:09:35 +0100350 kvm_mmu_reset_context(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100351}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200352EXPORT_SYMBOL_GPL(kvm_set_cr4);
Carsten Ottea03490e2007-10-29 16:09:35 +0100353
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200354void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
Carsten Ottea03490e2007-10-29 16:09:35 +0100355{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800356 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
Avi Kivityd835dfe2007-11-21 02:57:59 +0200357 kvm_mmu_flush_tlb(vcpu);
358 return;
359 }
360
Carsten Ottea03490e2007-10-29 16:09:35 +0100361 if (is_long_mode(vcpu)) {
362 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
363 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200364 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100365 return;
366 }
367 } else {
368 if (is_pae(vcpu)) {
369 if (cr3 & CR3_PAE_RESERVED_BITS) {
370 printk(KERN_DEBUG
371 "set_cr3: #GP, reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200372 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100373 return;
374 }
375 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
376 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
377 "reserved bits\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200378 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100379 return;
380 }
381 }
382 /*
383 * We don't check reserved bits in nonpae mode, because
384 * this isn't enforced, and VMware depends on this.
385 */
386 }
387
Carsten Ottea03490e2007-10-29 16:09:35 +0100388 /*
389 * Does the new cr3 value map to physical memory? (Note, we
390 * catch an invalid cr3 even in real-mode, because it would
391 * cause trouble later on when we turn on paging anyway.)
392 *
393 * A real CPU would silently accept an invalid cr3 and would
394 * attempt to use it - with largely undefined (and often hard
395 * to debug) behavior on the guest side.
396 */
397 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200398 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100399 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800400 vcpu->arch.cr3 = cr3;
401 vcpu->arch.mmu.new_cr3(vcpu);
Carsten Ottea03490e2007-10-29 16:09:35 +0100402 }
Carsten Ottea03490e2007-10-29 16:09:35 +0100403}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200404EXPORT_SYMBOL_GPL(kvm_set_cr3);
Carsten Ottea03490e2007-10-29 16:09:35 +0100405
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200406void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
Carsten Ottea03490e2007-10-29 16:09:35 +0100407{
408 if (cr8 & CR8_RESERVED_BITS) {
409 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200410 kvm_inject_gp(vcpu, 0);
Carsten Ottea03490e2007-10-29 16:09:35 +0100411 return;
412 }
413 if (irqchip_in_kernel(vcpu->kvm))
414 kvm_lapic_set_tpr(vcpu, cr8);
415 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800416 vcpu->arch.cr8 = cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100417}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200418EXPORT_SYMBOL_GPL(kvm_set_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100419
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200420unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
Carsten Ottea03490e2007-10-29 16:09:35 +0100421{
422 if (irqchip_in_kernel(vcpu->kvm))
423 return kvm_lapic_get_cr8(vcpu);
424 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800425 return vcpu->arch.cr8;
Carsten Ottea03490e2007-10-29 16:09:35 +0100426}
Avi Kivity2d3ad1f2008-02-24 11:20:43 +0200427EXPORT_SYMBOL_GPL(kvm_get_cr8);
Carsten Ottea03490e2007-10-29 16:09:35 +0100428
Carsten Otte043405e2007-10-10 17:16:19 +0200429/*
430 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
431 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
432 *
433 * This list is modified at module load time to reflect the
434 * capabilities of the host cpu.
435 */
436static u32 msrs_to_save[] = {
437 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
438 MSR_K6_STAR,
439#ifdef CONFIG_X86_64
440 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
441#endif
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200442 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
Alexander Graf847f0ad2008-02-21 12:11:01 +0100443 MSR_IA32_PERF_STATUS,
Carsten Otte043405e2007-10-10 17:16:19 +0200444};
445
446static unsigned num_msrs_to_save;
447
448static u32 emulated_msrs[] = {
449 MSR_IA32_MISC_ENABLE,
450};
451
Carsten Otte15c4a642007-10-30 18:44:17 +0100452static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
453{
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100454 if (efer & efer_reserved_bits) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100455 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
456 efer);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200457 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100458 return;
459 }
460
461 if (is_paging(vcpu)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800462 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100463 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +0200464 kvm_inject_gp(vcpu, 0);
Carsten Otte15c4a642007-10-30 18:44:17 +0100465 return;
466 }
467
468 kvm_x86_ops->set_efer(vcpu, efer);
469
470 efer &= ~EFER_LMA;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800471 efer |= vcpu->arch.shadow_efer & EFER_LMA;
Carsten Otte15c4a642007-10-30 18:44:17 +0100472
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800473 vcpu->arch.shadow_efer = efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100474}
475
Joerg Roedelf2b4b7d2008-01-31 14:57:37 +0100476void kvm_enable_efer_bits(u64 mask)
477{
478 efer_reserved_bits &= ~mask;
479}
480EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
481
482
Carsten Otte15c4a642007-10-30 18:44:17 +0100483/*
484 * Writes msr value into into the appropriate "register".
485 * Returns 0 on success, non-0 otherwise.
486 * Assumes vcpu_load() was already called.
487 */
488int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
489{
490 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
491}
492
Carsten Otte313a3dc2007-10-11 19:16:52 +0200493/*
494 * Adapt set_msr() to msr_io()'s calling convention
495 */
496static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
497{
498 return kvm_set_msr(vcpu, index, *data);
499}
500
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200501static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
502{
503 static int version;
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200504 struct pvclock_wall_clock wc;
505 struct timespec now, sys, boot;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200506
507 if (!wall_clock)
508 return;
509
510 version++;
511
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200512 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
513
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200514 /*
515 * The guest calculates current wall clock time by adding
516 * system time (updated by kvm_write_guest_time below) to the
517 * wall clock specified here. guest system time equals host
518 * system time for us, thus we must fill in host boot time here.
519 */
520 now = current_kernel_time();
521 ktime_get_ts(&sys);
522 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
523
524 wc.sec = boot.tv_sec;
525 wc.nsec = boot.tv_nsec;
526 wc.version = version;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200527
528 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
529
530 version++;
531 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200532}
533
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200534static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
535{
536 uint32_t quotient, remainder;
537
538 /* Don't try to replace with do_div(), this one calculates
539 * "(dividend << 32) / divisor" */
540 __asm__ ( "divl %4"
541 : "=a" (quotient), "=d" (remainder)
542 : "0" (0), "1" (dividend), "r" (divisor) );
543 return quotient;
544}
545
546static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
547{
548 uint64_t nsecs = 1000000000LL;
549 int32_t shift = 0;
550 uint64_t tps64;
551 uint32_t tps32;
552
553 tps64 = tsc_khz * 1000LL;
554 while (tps64 > nsecs*2) {
555 tps64 >>= 1;
556 shift--;
557 }
558
559 tps32 = (uint32_t)tps64;
560 while (tps32 <= (uint32_t)nsecs) {
561 tps32 <<= 1;
562 shift++;
563 }
564
565 hv_clock->tsc_shift = shift;
566 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
567
568 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
569 __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
570 hv_clock->tsc_to_system_mul);
571}
572
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200573static void kvm_write_guest_time(struct kvm_vcpu *v)
574{
575 struct timespec ts;
576 unsigned long flags;
577 struct kvm_vcpu_arch *vcpu = &v->arch;
578 void *shared_kaddr;
579
580 if ((!vcpu->time_page))
581 return;
582
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200583 if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
584 kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
585 vcpu->hv_clock_tsc_khz = tsc_khz;
586 }
587
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200588 /* Keep irq disabled to prevent changes to the clock */
589 local_irq_save(flags);
590 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
591 &vcpu->hv_clock.tsc_timestamp);
592 ktime_get_ts(&ts);
593 local_irq_restore(flags);
594
595 /* With all the info we got, fill in the values */
596
597 vcpu->hv_clock.system_time = ts.tv_nsec +
598 (NSEC_PER_SEC * (u64)ts.tv_sec);
599 /*
600 * The interface expects us to write an even number signaling that the
601 * update is finished. Since the guest won't see the intermediate
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200602 * state, we just increase by 2 at the end.
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200603 */
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200604 vcpu->hv_clock.version += 2;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200605
606 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
607
608 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
Gerd Hoffmann50d0a0f2008-06-03 16:17:31 +0200609 sizeof(vcpu->hv_clock));
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200610
611 kunmap_atomic(shared_kaddr, KM_USER0);
612
613 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
614}
615
Avi Kivity9ba075a2008-05-26 20:06:35 +0300616static bool msr_mtrr_valid(unsigned msr)
617{
618 switch (msr) {
619 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
620 case MSR_MTRRfix64K_00000:
621 case MSR_MTRRfix16K_80000:
622 case MSR_MTRRfix16K_A0000:
623 case MSR_MTRRfix4K_C0000:
624 case MSR_MTRRfix4K_C8000:
625 case MSR_MTRRfix4K_D0000:
626 case MSR_MTRRfix4K_D8000:
627 case MSR_MTRRfix4K_E0000:
628 case MSR_MTRRfix4K_E8000:
629 case MSR_MTRRfix4K_F0000:
630 case MSR_MTRRfix4K_F8000:
631 case MSR_MTRRdefType:
632 case MSR_IA32_CR_PAT:
633 return true;
634 case 0x2f8:
635 return true;
636 }
637 return false;
638}
639
640static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
641{
642 if (!msr_mtrr_valid(msr))
643 return 1;
644
645 vcpu->arch.mtrr[msr - 0x200] = data;
646 return 0;
647}
Carsten Otte15c4a642007-10-30 18:44:17 +0100648
649int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
650{
651 switch (msr) {
Carsten Otte15c4a642007-10-30 18:44:17 +0100652 case MSR_EFER:
653 set_efer(vcpu, data);
654 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100655 case MSR_IA32_MC0_STATUS:
656 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800657 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100658 break;
659 case MSR_IA32_MCG_STATUS:
660 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800661 __func__, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100662 break;
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100663 case MSR_IA32_MCG_CTL:
664 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800665 __func__, data);
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100666 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100667 case MSR_IA32_UCODE_REV:
668 case MSR_IA32_UCODE_WRITE:
Carsten Otte15c4a642007-10-30 18:44:17 +0100669 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300670 case 0x200 ... 0x2ff:
671 return set_msr_mtrr(vcpu, msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100672 case MSR_IA32_APICBASE:
673 kvm_set_apic_base(vcpu, data);
674 break;
675 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800676 vcpu->arch.ia32_misc_enable_msr = data;
Carsten Otte15c4a642007-10-30 18:44:17 +0100677 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200678 case MSR_KVM_WALL_CLOCK:
679 vcpu->kvm->arch.wall_clock = data;
680 kvm_write_wall_clock(vcpu->kvm, data);
681 break;
682 case MSR_KVM_SYSTEM_TIME: {
683 if (vcpu->arch.time_page) {
684 kvm_release_page_dirty(vcpu->arch.time_page);
685 vcpu->arch.time_page = NULL;
686 }
687
688 vcpu->arch.time = data;
689
690 /* we verify if the enable bit is set... */
691 if (!(data & 1))
692 break;
693
694 /* ...but clean it before doing the actual write */
695 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
696
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200697 down_read(&current->mm->mmap_sem);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200698 vcpu->arch.time_page =
699 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200700 up_read(&current->mm->mmap_sem);
701
702 if (is_error_page(vcpu->arch.time_page)) {
703 kvm_release_page_clean(vcpu->arch.time_page);
704 vcpu->arch.time_page = NULL;
705 }
706
707 kvm_write_guest_time(vcpu);
708 break;
709 }
Carsten Otte15c4a642007-10-30 18:44:17 +0100710 default:
Avi Kivity565f1fb2007-12-19 12:02:40 +0200711 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
Carsten Otte15c4a642007-10-30 18:44:17 +0100712 return 1;
713 }
714 return 0;
715}
716EXPORT_SYMBOL_GPL(kvm_set_msr_common);
717
718
719/*
720 * Reads an msr value (of 'msr_index') into 'pdata'.
721 * Returns 0 on success, non-0 otherwise.
722 * Assumes vcpu_load() was already called.
723 */
724int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
725{
726 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
727}
728
Avi Kivity9ba075a2008-05-26 20:06:35 +0300729static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
730{
731 if (!msr_mtrr_valid(msr))
732 return 1;
733
734 *pdata = vcpu->arch.mtrr[msr - 0x200];
735 return 0;
736}
737
Carsten Otte15c4a642007-10-30 18:44:17 +0100738int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
739{
740 u64 data;
741
742 switch (msr) {
743 case 0xc0010010: /* SYSCFG */
744 case 0xc0010015: /* HWCR */
745 case MSR_IA32_PLATFORM_ID:
746 case MSR_IA32_P5_MC_ADDR:
747 case MSR_IA32_P5_MC_TYPE:
748 case MSR_IA32_MC0_CTL:
749 case MSR_IA32_MCG_STATUS:
750 case MSR_IA32_MCG_CAP:
Joerg Roedelc7ac6792008-02-11 20:28:27 +0100751 case MSR_IA32_MCG_CTL:
Carsten Otte15c4a642007-10-30 18:44:17 +0100752 case MSR_IA32_MC0_MISC:
753 case MSR_IA32_MC0_MISC+4:
754 case MSR_IA32_MC0_MISC+8:
755 case MSR_IA32_MC0_MISC+12:
756 case MSR_IA32_MC0_MISC+16:
757 case MSR_IA32_UCODE_REV:
Carsten Otte15c4a642007-10-30 18:44:17 +0100758 case MSR_IA32_EBL_CR_POWERON:
Carsten Otte15c4a642007-10-30 18:44:17 +0100759 data = 0;
760 break;
Avi Kivity9ba075a2008-05-26 20:06:35 +0300761 case MSR_MTRRcap:
762 data = 0x500 | KVM_NR_VAR_MTRR;
763 break;
764 case 0x200 ... 0x2ff:
765 return get_msr_mtrr(vcpu, msr, pdata);
Carsten Otte15c4a642007-10-30 18:44:17 +0100766 case 0xcd: /* fsb frequency */
767 data = 3;
768 break;
769 case MSR_IA32_APICBASE:
770 data = kvm_get_apic_base(vcpu);
771 break;
772 case MSR_IA32_MISC_ENABLE:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800773 data = vcpu->arch.ia32_misc_enable_msr;
Carsten Otte15c4a642007-10-30 18:44:17 +0100774 break;
Alexander Graf847f0ad2008-02-21 12:11:01 +0100775 case MSR_IA32_PERF_STATUS:
776 /* TSC increment by tick */
777 data = 1000ULL;
778 /* CPU multiplier */
779 data |= (((uint64_t)4ULL) << 40);
780 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100781 case MSR_EFER:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800782 data = vcpu->arch.shadow_efer;
Carsten Otte15c4a642007-10-30 18:44:17 +0100783 break;
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200784 case MSR_KVM_WALL_CLOCK:
785 data = vcpu->kvm->arch.wall_clock;
786 break;
787 case MSR_KVM_SYSTEM_TIME:
788 data = vcpu->arch.time;
789 break;
Carsten Otte15c4a642007-10-30 18:44:17 +0100790 default:
791 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
792 return 1;
793 }
794 *pdata = data;
795 return 0;
796}
797EXPORT_SYMBOL_GPL(kvm_get_msr_common);
798
Carsten Otte313a3dc2007-10-11 19:16:52 +0200799/*
800 * Read or write a bunch of msrs. All parameters are kernel addresses.
801 *
802 * @return number of msrs set successfully.
803 */
804static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
805 struct kvm_msr_entry *entries,
806 int (*do_msr)(struct kvm_vcpu *vcpu,
807 unsigned index, u64 *data))
808{
809 int i;
810
811 vcpu_load(vcpu);
812
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300813 down_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200814 for (i = 0; i < msrs->nmsrs; ++i)
815 if (do_msr(vcpu, entries[i].index, &entries[i].data))
816 break;
Marcelo Tosatti3200f402008-03-29 20:17:59 -0300817 up_read(&vcpu->kvm->slots_lock);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200818
819 vcpu_put(vcpu);
820
821 return i;
822}
823
824/*
825 * Read or write a bunch of msrs. Parameters are user addresses.
826 *
827 * @return number of msrs set successfully.
828 */
829static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
830 int (*do_msr)(struct kvm_vcpu *vcpu,
831 unsigned index, u64 *data),
832 int writeback)
833{
834 struct kvm_msrs msrs;
835 struct kvm_msr_entry *entries;
836 int r, n;
837 unsigned size;
838
839 r = -EFAULT;
840 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
841 goto out;
842
843 r = -E2BIG;
844 if (msrs.nmsrs >= MAX_IO_MSRS)
845 goto out;
846
847 r = -ENOMEM;
848 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
849 entries = vmalloc(size);
850 if (!entries)
851 goto out;
852
853 r = -EFAULT;
854 if (copy_from_user(entries, user_msrs->entries, size))
855 goto out_free;
856
857 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
858 if (r < 0)
859 goto out_free;
860
861 r = -EFAULT;
862 if (writeback && copy_to_user(user_msrs->entries, entries, size))
863 goto out_free;
864
865 r = n;
866
867out_free:
868 vfree(entries);
869out:
870 return r;
871}
872
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800873int kvm_dev_ioctl_check_extension(long ext)
874{
875 int r;
876
877 switch (ext) {
878 case KVM_CAP_IRQCHIP:
879 case KVM_CAP_HLT:
880 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
881 case KVM_CAP_USER_MEMORY:
882 case KVM_CAP_SET_TSS_ADDR:
Dan Kenigsberg07716712007-11-21 17:10:04 +0200883 case KVM_CAP_EXT_CPUID:
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200884 case KVM_CAP_CLOCKSOURCE:
Sheng Yang78376992008-01-28 05:10:22 +0800885 case KVM_CAP_PIT:
Marcelo Tosattia28e4f52008-02-22 12:21:36 -0500886 case KVM_CAP_NOP_IO_DELAY:
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -0300887 case KVM_CAP_MP_STATE:
Avi Kivityed848622008-07-29 11:30:57 +0300888 case KVM_CAP_SYNC_MMU:
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800889 r = 1;
890 break;
Laurent Vivier542472b2008-05-30 16:05:55 +0200891 case KVM_CAP_COALESCED_MMIO:
892 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
893 break;
Avi Kivity774ead32007-12-26 13:57:04 +0200894 case KVM_CAP_VAPIC:
895 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
896 break;
Avi Kivityf7252302008-02-20 11:53:16 +0200897 case KVM_CAP_NR_VCPUS:
898 r = KVM_MAX_VCPUS;
899 break;
Avi Kivitya988b912008-02-20 11:59:20 +0200900 case KVM_CAP_NR_MEMSLOTS:
901 r = KVM_MEMORY_SLOTS;
902 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500903 case KVM_CAP_PV_MMU:
904 r = !tdp_enabled;
905 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +0800906 default:
907 r = 0;
908 break;
909 }
910 return r;
911
912}
913
Carsten Otte043405e2007-10-10 17:16:19 +0200914long kvm_arch_dev_ioctl(struct file *filp,
915 unsigned int ioctl, unsigned long arg)
916{
917 void __user *argp = (void __user *)arg;
918 long r;
919
920 switch (ioctl) {
921 case KVM_GET_MSR_INDEX_LIST: {
922 struct kvm_msr_list __user *user_msr_list = argp;
923 struct kvm_msr_list msr_list;
924 unsigned n;
925
926 r = -EFAULT;
927 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
928 goto out;
929 n = msr_list.nmsrs;
930 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
931 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
932 goto out;
933 r = -E2BIG;
934 if (n < num_msrs_to_save)
935 goto out;
936 r = -EFAULT;
937 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
938 num_msrs_to_save * sizeof(u32)))
939 goto out;
940 if (copy_to_user(user_msr_list->indices
941 + num_msrs_to_save * sizeof(u32),
942 &emulated_msrs,
943 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
944 goto out;
945 r = 0;
946 break;
947 }
Avi Kivity674eea02008-02-11 18:37:23 +0200948 case KVM_GET_SUPPORTED_CPUID: {
949 struct kvm_cpuid2 __user *cpuid_arg = argp;
950 struct kvm_cpuid2 cpuid;
951
952 r = -EFAULT;
953 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
954 goto out;
955 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
956 cpuid_arg->entries);
957 if (r)
958 goto out;
959
960 r = -EFAULT;
961 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
962 goto out;
963 r = 0;
964 break;
965 }
Carsten Otte043405e2007-10-10 17:16:19 +0200966 default:
967 r = -EINVAL;
968 }
969out:
970 return r;
971}
972
Carsten Otte313a3dc2007-10-11 19:16:52 +0200973void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
974{
975 kvm_x86_ops->vcpu_load(vcpu, cpu);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200976 kvm_write_guest_time(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200977}
978
979void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
980{
981 kvm_x86_ops->vcpu_put(vcpu);
Amit Shah9327fd12007-11-15 18:38:46 +0200982 kvm_put_guest_fpu(vcpu);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200983}
984
Dan Kenigsberg07716712007-11-21 17:10:04 +0200985static int is_efer_nx(void)
Carsten Otte313a3dc2007-10-11 19:16:52 +0200986{
987 u64 efer;
Carsten Otte313a3dc2007-10-11 19:16:52 +0200988
989 rdmsrl(MSR_EFER, efer);
Dan Kenigsberg07716712007-11-21 17:10:04 +0200990 return efer & EFER_NX;
991}
992
993static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
994{
995 int i;
996 struct kvm_cpuid_entry2 *e, *entry;
997
Carsten Otte313a3dc2007-10-11 19:16:52 +0200998 entry = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800999 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1000 e = &vcpu->arch.cpuid_entries[i];
Carsten Otte313a3dc2007-10-11 19:16:52 +02001001 if (e->function == 0x80000001) {
1002 entry = e;
1003 break;
1004 }
1005 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001006 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
Carsten Otte313a3dc2007-10-11 19:16:52 +02001007 entry->edx &= ~(1 << 20);
1008 printk(KERN_INFO "kvm: guest NX capability removed\n");
1009 }
1010}
1011
Dan Kenigsberg07716712007-11-21 17:10:04 +02001012/* when an old userspace process fills a new kernel module */
Carsten Otte313a3dc2007-10-11 19:16:52 +02001013static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1014 struct kvm_cpuid *cpuid,
1015 struct kvm_cpuid_entry __user *entries)
1016{
Dan Kenigsberg07716712007-11-21 17:10:04 +02001017 int r, i;
1018 struct kvm_cpuid_entry *cpuid_entries;
1019
1020 r = -E2BIG;
1021 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1022 goto out;
1023 r = -ENOMEM;
1024 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1025 if (!cpuid_entries)
1026 goto out;
1027 r = -EFAULT;
1028 if (copy_from_user(cpuid_entries, entries,
1029 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1030 goto out_free;
1031 for (i = 0; i < cpuid->nent; i++) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001032 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1033 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1034 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1035 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1036 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1037 vcpu->arch.cpuid_entries[i].index = 0;
1038 vcpu->arch.cpuid_entries[i].flags = 0;
1039 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1040 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1041 vcpu->arch.cpuid_entries[i].padding[2] = 0;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001042 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001043 vcpu->arch.cpuid_nent = cpuid->nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001044 cpuid_fix_nx_cap(vcpu);
1045 r = 0;
1046
1047out_free:
1048 vfree(cpuid_entries);
1049out:
1050 return r;
1051}
1052
1053static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1054 struct kvm_cpuid2 *cpuid,
1055 struct kvm_cpuid_entry2 __user *entries)
1056{
Carsten Otte313a3dc2007-10-11 19:16:52 +02001057 int r;
1058
1059 r = -E2BIG;
1060 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1061 goto out;
1062 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001063 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001064 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
Carsten Otte313a3dc2007-10-11 19:16:52 +02001065 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001066 vcpu->arch.cpuid_nent = cpuid->nent;
Carsten Otte313a3dc2007-10-11 19:16:52 +02001067 return 0;
1068
1069out:
1070 return r;
1071}
1072
Dan Kenigsberg07716712007-11-21 17:10:04 +02001073static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1074 struct kvm_cpuid2 *cpuid,
1075 struct kvm_cpuid_entry2 __user *entries)
1076{
1077 int r;
1078
1079 r = -E2BIG;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001080 if (cpuid->nent < vcpu->arch.cpuid_nent)
Dan Kenigsberg07716712007-11-21 17:10:04 +02001081 goto out;
1082 r = -EFAULT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001083 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1084 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
Dan Kenigsberg07716712007-11-21 17:10:04 +02001085 goto out;
1086 return 0;
1087
1088out:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001089 cpuid->nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001090 return r;
1091}
1092
1093static inline u32 bit(int bitno)
1094{
1095 return 1 << (bitno & 31);
1096}
1097
1098static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1099 u32 index)
1100{
1101 entry->function = function;
1102 entry->index = index;
1103 cpuid_count(entry->function, entry->index,
1104 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1105 entry->flags = 0;
1106}
1107
1108static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1109 u32 index, int *nent, int maxnent)
1110{
1111 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1112 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1113 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1114 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1115 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1116 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1117 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1118 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1119 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1120 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1121 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1122 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1123 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1124 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1125 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1126 bit(X86_FEATURE_PGE) |
1127 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1128 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1129 bit(X86_FEATURE_SYSCALL) |
1130 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1131#ifdef CONFIG_X86_64
1132 bit(X86_FEATURE_LM) |
1133#endif
1134 bit(X86_FEATURE_MMXEXT) |
1135 bit(X86_FEATURE_3DNOWEXT) |
1136 bit(X86_FEATURE_3DNOW);
1137 const u32 kvm_supported_word3_x86_features =
1138 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1139 const u32 kvm_supported_word6_x86_features =
1140 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1141
1142 /* all func 2 cpuid_count() should be called on the same cpu */
1143 get_cpu();
1144 do_cpuid_1_ent(entry, function, index);
1145 ++*nent;
1146
1147 switch (function) {
1148 case 0:
1149 entry->eax = min(entry->eax, (u32)0xb);
1150 break;
1151 case 1:
1152 entry->edx &= kvm_supported_word0_x86_features;
1153 entry->ecx &= kvm_supported_word3_x86_features;
1154 break;
1155 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1156 * may return different values. This forces us to get_cpu() before
1157 * issuing the first command, and also to emulate this annoying behavior
1158 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1159 case 2: {
1160 int t, times = entry->eax & 0xff;
1161
1162 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1163 for (t = 1; t < times && *nent < maxnent; ++t) {
1164 do_cpuid_1_ent(&entry[t], function, 0);
1165 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1166 ++*nent;
1167 }
1168 break;
1169 }
1170 /* function 4 and 0xb have additional index. */
1171 case 4: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001172 int i, cache_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001173
1174 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1175 /* read more entries until cache_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001176 for (i = 1; *nent < maxnent; ++i) {
1177 cache_type = entry[i - 1].eax & 0x1f;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001178 if (!cache_type)
1179 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001180 do_cpuid_1_ent(&entry[i], function, i);
1181 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001182 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1183 ++*nent;
1184 }
1185 break;
1186 }
1187 case 0xb: {
Harvey Harrison14af3f32008-02-19 10:25:50 -08001188 int i, level_type;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001189
1190 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1191 /* read more entries until level_type is zero */
Harvey Harrison14af3f32008-02-19 10:25:50 -08001192 for (i = 1; *nent < maxnent; ++i) {
1193 level_type = entry[i - 1].ecx & 0xff;
Dan Kenigsberg07716712007-11-21 17:10:04 +02001194 if (!level_type)
1195 break;
Harvey Harrison14af3f32008-02-19 10:25:50 -08001196 do_cpuid_1_ent(&entry[i], function, i);
1197 entry[i].flags |=
Dan Kenigsberg07716712007-11-21 17:10:04 +02001198 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1199 ++*nent;
1200 }
1201 break;
1202 }
1203 case 0x80000000:
1204 entry->eax = min(entry->eax, 0x8000001a);
1205 break;
1206 case 0x80000001:
1207 entry->edx &= kvm_supported_word1_x86_features;
1208 entry->ecx &= kvm_supported_word6_x86_features;
1209 break;
1210 }
1211 put_cpu();
1212}
1213
Avi Kivity674eea02008-02-11 18:37:23 +02001214static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
Dan Kenigsberg07716712007-11-21 17:10:04 +02001215 struct kvm_cpuid_entry2 __user *entries)
1216{
1217 struct kvm_cpuid_entry2 *cpuid_entries;
1218 int limit, nent = 0, r = -E2BIG;
1219 u32 func;
1220
1221 if (cpuid->nent < 1)
1222 goto out;
1223 r = -ENOMEM;
1224 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1225 if (!cpuid_entries)
1226 goto out;
1227
1228 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1229 limit = cpuid_entries[0].eax;
1230 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1231 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1232 &nent, cpuid->nent);
1233 r = -E2BIG;
1234 if (nent >= cpuid->nent)
1235 goto out_free;
1236
1237 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1238 limit = cpuid_entries[nent - 1].eax;
1239 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1240 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1241 &nent, cpuid->nent);
1242 r = -EFAULT;
1243 if (copy_to_user(entries, cpuid_entries,
1244 nent * sizeof(struct kvm_cpuid_entry2)))
1245 goto out_free;
1246 cpuid->nent = nent;
1247 r = 0;
1248
1249out_free:
1250 vfree(cpuid_entries);
1251out:
1252 return r;
1253}
1254
Carsten Otte313a3dc2007-10-11 19:16:52 +02001255static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1256 struct kvm_lapic_state *s)
1257{
1258 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001260 vcpu_put(vcpu);
1261
1262 return 0;
1263}
1264
1265static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1266 struct kvm_lapic_state *s)
1267{
1268 vcpu_load(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001269 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
Carsten Otte313a3dc2007-10-11 19:16:52 +02001270 kvm_apic_post_state_restore(vcpu);
1271 vcpu_put(vcpu);
1272
1273 return 0;
1274}
1275
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001276static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1277 struct kvm_interrupt *irq)
1278{
1279 if (irq->irq < 0 || irq->irq >= 256)
1280 return -EINVAL;
1281 if (irqchip_in_kernel(vcpu->kvm))
1282 return -ENXIO;
1283 vcpu_load(vcpu);
1284
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001285 set_bit(irq->irq, vcpu->arch.irq_pending);
1286 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001287
1288 vcpu_put(vcpu);
1289
1290 return 0;
1291}
1292
Avi Kivityb209749f2007-10-22 16:50:39 +02001293static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1294 struct kvm_tpr_access_ctl *tac)
1295{
1296 if (tac->flags)
1297 return -EINVAL;
1298 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1299 return 0;
1300}
1301
Carsten Otte313a3dc2007-10-11 19:16:52 +02001302long kvm_arch_vcpu_ioctl(struct file *filp,
1303 unsigned int ioctl, unsigned long arg)
1304{
1305 struct kvm_vcpu *vcpu = filp->private_data;
1306 void __user *argp = (void __user *)arg;
1307 int r;
1308
1309 switch (ioctl) {
1310 case KVM_GET_LAPIC: {
1311 struct kvm_lapic_state lapic;
1312
1313 memset(&lapic, 0, sizeof lapic);
1314 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1315 if (r)
1316 goto out;
1317 r = -EFAULT;
1318 if (copy_to_user(argp, &lapic, sizeof lapic))
1319 goto out;
1320 r = 0;
1321 break;
1322 }
1323 case KVM_SET_LAPIC: {
1324 struct kvm_lapic_state lapic;
1325
1326 r = -EFAULT;
1327 if (copy_from_user(&lapic, argp, sizeof lapic))
1328 goto out;
1329 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1330 if (r)
1331 goto out;
1332 r = 0;
1333 break;
1334 }
Zhang Xiantaof77bc6a2007-11-21 04:36:41 +08001335 case KVM_INTERRUPT: {
1336 struct kvm_interrupt irq;
1337
1338 r = -EFAULT;
1339 if (copy_from_user(&irq, argp, sizeof irq))
1340 goto out;
1341 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1342 if (r)
1343 goto out;
1344 r = 0;
1345 break;
1346 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001347 case KVM_SET_CPUID: {
1348 struct kvm_cpuid __user *cpuid_arg = argp;
1349 struct kvm_cpuid cpuid;
1350
1351 r = -EFAULT;
1352 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1353 goto out;
1354 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1355 if (r)
1356 goto out;
1357 break;
1358 }
Dan Kenigsberg07716712007-11-21 17:10:04 +02001359 case KVM_SET_CPUID2: {
1360 struct kvm_cpuid2 __user *cpuid_arg = argp;
1361 struct kvm_cpuid2 cpuid;
1362
1363 r = -EFAULT;
1364 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1365 goto out;
1366 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1367 cpuid_arg->entries);
1368 if (r)
1369 goto out;
1370 break;
1371 }
1372 case KVM_GET_CPUID2: {
1373 struct kvm_cpuid2 __user *cpuid_arg = argp;
1374 struct kvm_cpuid2 cpuid;
1375
1376 r = -EFAULT;
1377 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1378 goto out;
1379 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1380 cpuid_arg->entries);
1381 if (r)
1382 goto out;
1383 r = -EFAULT;
1384 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1385 goto out;
1386 r = 0;
1387 break;
1388 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001389 case KVM_GET_MSRS:
1390 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1391 break;
1392 case KVM_SET_MSRS:
1393 r = msr_io(vcpu, argp, do_set_msr, 0);
1394 break;
Avi Kivityb209749f2007-10-22 16:50:39 +02001395 case KVM_TPR_ACCESS_REPORTING: {
1396 struct kvm_tpr_access_ctl tac;
1397
1398 r = -EFAULT;
1399 if (copy_from_user(&tac, argp, sizeof tac))
1400 goto out;
1401 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1402 if (r)
1403 goto out;
1404 r = -EFAULT;
1405 if (copy_to_user(argp, &tac, sizeof tac))
1406 goto out;
1407 r = 0;
1408 break;
1409 };
Avi Kivityb93463a2007-10-25 16:52:32 +02001410 case KVM_SET_VAPIC_ADDR: {
1411 struct kvm_vapic_addr va;
1412
1413 r = -EINVAL;
1414 if (!irqchip_in_kernel(vcpu->kvm))
1415 goto out;
1416 r = -EFAULT;
1417 if (copy_from_user(&va, argp, sizeof va))
1418 goto out;
1419 r = 0;
1420 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1421 break;
1422 }
Carsten Otte313a3dc2007-10-11 19:16:52 +02001423 default:
1424 r = -EINVAL;
1425 }
1426out:
1427 return r;
1428}
1429
Carsten Otte1fe779f2007-10-29 16:08:35 +01001430static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1431{
1432 int ret;
1433
1434 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1435 return -1;
1436 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1437 return ret;
1438}
1439
1440static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1441 u32 kvm_nr_mmu_pages)
1442{
1443 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1444 return -EINVAL;
1445
Izik Eidus72dc67a2008-02-10 18:04:15 +02001446 down_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001447
1448 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001449 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001450
Izik Eidus72dc67a2008-02-10 18:04:15 +02001451 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001452 return 0;
1453}
1454
1455static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1456{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001457 return kvm->arch.n_alloc_mmu_pages;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001458}
1459
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001460gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1461{
1462 int i;
1463 struct kvm_mem_alias *alias;
1464
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001465 for (i = 0; i < kvm->arch.naliases; ++i) {
1466 alias = &kvm->arch.aliases[i];
Zhang Xiantaoe9f85cd2007-11-22 11:20:33 +08001467 if (gfn >= alias->base_gfn
1468 && gfn < alias->base_gfn + alias->npages)
1469 return alias->target_gfn + gfn - alias->base_gfn;
1470 }
1471 return gfn;
1472}
1473
Carsten Otte1fe779f2007-10-29 16:08:35 +01001474/*
1475 * Set a new alias region. Aliases map a portion of physical memory into
1476 * another portion. This is useful for memory windows, for example the PC
1477 * VGA region.
1478 */
1479static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1480 struct kvm_memory_alias *alias)
1481{
1482 int r, n;
1483 struct kvm_mem_alias *p;
1484
1485 r = -EINVAL;
1486 /* General sanity checks */
1487 if (alias->memory_size & (PAGE_SIZE - 1))
1488 goto out;
1489 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1490 goto out;
1491 if (alias->slot >= KVM_ALIAS_SLOTS)
1492 goto out;
1493 if (alias->guest_phys_addr + alias->memory_size
1494 < alias->guest_phys_addr)
1495 goto out;
1496 if (alias->target_phys_addr + alias->memory_size
1497 < alias->target_phys_addr)
1498 goto out;
1499
Izik Eidus72dc67a2008-02-10 18:04:15 +02001500 down_write(&kvm->slots_lock);
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001501 spin_lock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001502
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001503 p = &kvm->arch.aliases[alias->slot];
Carsten Otte1fe779f2007-10-29 16:08:35 +01001504 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1505 p->npages = alias->memory_size >> PAGE_SHIFT;
1506 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1507
1508 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001509 if (kvm->arch.aliases[n - 1].npages)
Carsten Otte1fe779f2007-10-29 16:08:35 +01001510 break;
Zhang Xiantaod69fb812007-12-14 09:54:20 +08001511 kvm->arch.naliases = n;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001512
Andrea Arcangelia1708ce2008-07-25 16:26:39 +02001513 spin_unlock(&kvm->mmu_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001514 kvm_mmu_zap_all(kvm);
1515
Izik Eidus72dc67a2008-02-10 18:04:15 +02001516 up_write(&kvm->slots_lock);
Carsten Otte1fe779f2007-10-29 16:08:35 +01001517
1518 return 0;
1519
1520out:
1521 return r;
1522}
1523
1524static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1525{
1526 int r;
1527
1528 r = 0;
1529 switch (chip->chip_id) {
1530 case KVM_IRQCHIP_PIC_MASTER:
1531 memcpy(&chip->chip.pic,
1532 &pic_irqchip(kvm)->pics[0],
1533 sizeof(struct kvm_pic_state));
1534 break;
1535 case KVM_IRQCHIP_PIC_SLAVE:
1536 memcpy(&chip->chip.pic,
1537 &pic_irqchip(kvm)->pics[1],
1538 sizeof(struct kvm_pic_state));
1539 break;
1540 case KVM_IRQCHIP_IOAPIC:
1541 memcpy(&chip->chip.ioapic,
1542 ioapic_irqchip(kvm),
1543 sizeof(struct kvm_ioapic_state));
1544 break;
1545 default:
1546 r = -EINVAL;
1547 break;
1548 }
1549 return r;
1550}
1551
1552static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1553{
1554 int r;
1555
1556 r = 0;
1557 switch (chip->chip_id) {
1558 case KVM_IRQCHIP_PIC_MASTER:
1559 memcpy(&pic_irqchip(kvm)->pics[0],
1560 &chip->chip.pic,
1561 sizeof(struct kvm_pic_state));
1562 break;
1563 case KVM_IRQCHIP_PIC_SLAVE:
1564 memcpy(&pic_irqchip(kvm)->pics[1],
1565 &chip->chip.pic,
1566 sizeof(struct kvm_pic_state));
1567 break;
1568 case KVM_IRQCHIP_IOAPIC:
1569 memcpy(ioapic_irqchip(kvm),
1570 &chip->chip.ioapic,
1571 sizeof(struct kvm_ioapic_state));
1572 break;
1573 default:
1574 r = -EINVAL;
1575 break;
1576 }
1577 kvm_pic_update_irq(pic_irqchip(kvm));
1578 return r;
1579}
1580
Sheng Yange0f63cb2008-03-04 00:50:59 +08001581static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1582{
1583 int r = 0;
1584
1585 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1586 return r;
1587}
1588
1589static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1590{
1591 int r = 0;
1592
1593 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1594 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1595 return r;
1596}
1597
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001598/*
1599 * Get (and clear) the dirty memory log for a memory slot.
1600 */
1601int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1602 struct kvm_dirty_log *log)
1603{
1604 int r;
1605 int n;
1606 struct kvm_memory_slot *memslot;
1607 int is_dirty = 0;
1608
Izik Eidus72dc67a2008-02-10 18:04:15 +02001609 down_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001610
1611 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1612 if (r)
1613 goto out;
1614
1615 /* If nothing is dirty, don't bother messing with page tables. */
1616 if (is_dirty) {
1617 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1618 kvm_flush_remote_tlbs(kvm);
1619 memslot = &kvm->memslots[log->slot];
1620 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1621 memset(memslot->dirty_bitmap, 0, n);
1622 }
1623 r = 0;
1624out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001625 up_write(&kvm->slots_lock);
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001626 return r;
1627}
1628
Carsten Otte1fe779f2007-10-29 16:08:35 +01001629long kvm_arch_vm_ioctl(struct file *filp,
1630 unsigned int ioctl, unsigned long arg)
1631{
1632 struct kvm *kvm = filp->private_data;
1633 void __user *argp = (void __user *)arg;
1634 int r = -EINVAL;
1635
1636 switch (ioctl) {
1637 case KVM_SET_TSS_ADDR:
1638 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1639 if (r < 0)
1640 goto out;
1641 break;
1642 case KVM_SET_MEMORY_REGION: {
1643 struct kvm_memory_region kvm_mem;
1644 struct kvm_userspace_memory_region kvm_userspace_mem;
1645
1646 r = -EFAULT;
1647 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1648 goto out;
1649 kvm_userspace_mem.slot = kvm_mem.slot;
1650 kvm_userspace_mem.flags = kvm_mem.flags;
1651 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1652 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1653 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1654 if (r)
1655 goto out;
1656 break;
1657 }
1658 case KVM_SET_NR_MMU_PAGES:
1659 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1660 if (r)
1661 goto out;
1662 break;
1663 case KVM_GET_NR_MMU_PAGES:
1664 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1665 break;
1666 case KVM_SET_MEMORY_ALIAS: {
1667 struct kvm_memory_alias alias;
1668
1669 r = -EFAULT;
1670 if (copy_from_user(&alias, argp, sizeof alias))
1671 goto out;
1672 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1673 if (r)
1674 goto out;
1675 break;
1676 }
1677 case KVM_CREATE_IRQCHIP:
1678 r = -ENOMEM;
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001679 kvm->arch.vpic = kvm_create_pic(kvm);
1680 if (kvm->arch.vpic) {
Carsten Otte1fe779f2007-10-29 16:08:35 +01001681 r = kvm_ioapic_init(kvm);
1682 if (r) {
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001683 kfree(kvm->arch.vpic);
1684 kvm->arch.vpic = NULL;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001685 goto out;
1686 }
1687 } else
1688 goto out;
1689 break;
Sheng Yang78376992008-01-28 05:10:22 +08001690 case KVM_CREATE_PIT:
1691 r = -ENOMEM;
1692 kvm->arch.vpit = kvm_create_pit(kvm);
1693 if (kvm->arch.vpit)
1694 r = 0;
1695 break;
Carsten Otte1fe779f2007-10-29 16:08:35 +01001696 case KVM_IRQ_LINE: {
1697 struct kvm_irq_level irq_event;
1698
1699 r = -EFAULT;
1700 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1701 goto out;
1702 if (irqchip_in_kernel(kvm)) {
1703 mutex_lock(&kvm->lock);
1704 if (irq_event.irq < 16)
1705 kvm_pic_set_irq(pic_irqchip(kvm),
1706 irq_event.irq,
1707 irq_event.level);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08001708 kvm_ioapic_set_irq(kvm->arch.vioapic,
Carsten Otte1fe779f2007-10-29 16:08:35 +01001709 irq_event.irq,
1710 irq_event.level);
1711 mutex_unlock(&kvm->lock);
1712 r = 0;
1713 }
1714 break;
1715 }
1716 case KVM_GET_IRQCHIP: {
1717 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1718 struct kvm_irqchip chip;
1719
1720 r = -EFAULT;
1721 if (copy_from_user(&chip, argp, sizeof chip))
1722 goto out;
1723 r = -ENXIO;
1724 if (!irqchip_in_kernel(kvm))
1725 goto out;
1726 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1727 if (r)
1728 goto out;
1729 r = -EFAULT;
1730 if (copy_to_user(argp, &chip, sizeof chip))
1731 goto out;
1732 r = 0;
1733 break;
1734 }
1735 case KVM_SET_IRQCHIP: {
1736 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1737 struct kvm_irqchip chip;
1738
1739 r = -EFAULT;
1740 if (copy_from_user(&chip, argp, sizeof chip))
1741 goto out;
1742 r = -ENXIO;
1743 if (!irqchip_in_kernel(kvm))
1744 goto out;
1745 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1746 if (r)
1747 goto out;
1748 r = 0;
1749 break;
1750 }
Sheng Yange0f63cb2008-03-04 00:50:59 +08001751 case KVM_GET_PIT: {
1752 struct kvm_pit_state ps;
1753 r = -EFAULT;
1754 if (copy_from_user(&ps, argp, sizeof ps))
1755 goto out;
1756 r = -ENXIO;
1757 if (!kvm->arch.vpit)
1758 goto out;
1759 r = kvm_vm_ioctl_get_pit(kvm, &ps);
1760 if (r)
1761 goto out;
1762 r = -EFAULT;
1763 if (copy_to_user(argp, &ps, sizeof ps))
1764 goto out;
1765 r = 0;
1766 break;
1767 }
1768 case KVM_SET_PIT: {
1769 struct kvm_pit_state ps;
1770 r = -EFAULT;
1771 if (copy_from_user(&ps, argp, sizeof ps))
1772 goto out;
1773 r = -ENXIO;
1774 if (!kvm->arch.vpit)
1775 goto out;
1776 r = kvm_vm_ioctl_set_pit(kvm, &ps);
1777 if (r)
1778 goto out;
1779 r = 0;
1780 break;
1781 }
Carsten Otte1fe779f2007-10-29 16:08:35 +01001782 default:
1783 ;
1784 }
1785out:
1786 return r;
1787}
1788
Zhang Xiantaoa16b0432007-11-16 14:38:21 +08001789static void kvm_init_msr_list(void)
Carsten Otte043405e2007-10-10 17:16:19 +02001790{
1791 u32 dummy[2];
1792 unsigned i, j;
1793
1794 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1795 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1796 continue;
1797 if (j < i)
1798 msrs_to_save[j] = msrs_to_save[i];
1799 j++;
1800 }
1801 num_msrs_to_save = j;
1802}
1803
Carsten Ottebbd9b642007-10-30 18:44:21 +01001804/*
1805 * Only apic need an MMIO device hook, so shortcut now..
1806 */
1807static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02001808 gpa_t addr, int len,
1809 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001810{
1811 struct kvm_io_device *dev;
1812
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001813 if (vcpu->arch.apic) {
1814 dev = &vcpu->arch.apic->dev;
Laurent Vivier92760492008-05-30 16:05:53 +02001815 if (dev->in_range(dev, addr, len, is_write))
Carsten Ottebbd9b642007-10-30 18:44:21 +01001816 return dev;
1817 }
1818 return NULL;
1819}
1820
1821
1822static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02001823 gpa_t addr, int len,
1824 int is_write)
Carsten Ottebbd9b642007-10-30 18:44:21 +01001825{
1826 struct kvm_io_device *dev;
1827
Laurent Vivier92760492008-05-30 16:05:53 +02001828 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001829 if (dev == NULL)
Laurent Vivier92760492008-05-30 16:05:53 +02001830 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
1831 is_write);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001832 return dev;
1833}
1834
1835int emulator_read_std(unsigned long addr,
1836 void *val,
1837 unsigned int bytes,
1838 struct kvm_vcpu *vcpu)
1839{
1840 void *data = val;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001841 int r = X86EMUL_CONTINUE;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001842
1843 while (bytes) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001844 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001845 unsigned offset = addr & (PAGE_SIZE-1);
1846 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1847 int ret;
1848
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001849 if (gpa == UNMAPPED_GVA) {
1850 r = X86EMUL_PROPAGATE_FAULT;
1851 goto out;
1852 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001853 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001854 if (ret < 0) {
1855 r = X86EMUL_UNHANDLEABLE;
1856 goto out;
1857 }
Carsten Ottebbd9b642007-10-30 18:44:21 +01001858
1859 bytes -= tocopy;
1860 data += tocopy;
1861 addr += tocopy;
1862 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001863out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001864 return r;
Carsten Ottebbd9b642007-10-30 18:44:21 +01001865}
1866EXPORT_SYMBOL_GPL(emulator_read_std);
1867
Carsten Ottebbd9b642007-10-30 18:44:21 +01001868static int emulator_read_emulated(unsigned long addr,
1869 void *val,
1870 unsigned int bytes,
1871 struct kvm_vcpu *vcpu)
1872{
1873 struct kvm_io_device *mmio_dev;
1874 gpa_t gpa;
1875
1876 if (vcpu->mmio_read_completed) {
1877 memcpy(val, vcpu->mmio_data, bytes);
1878 vcpu->mmio_read_completed = 0;
1879 return X86EMUL_CONTINUE;
1880 }
1881
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001882 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001883
1884 /* For APIC access vmexit */
1885 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1886 goto mmio;
1887
1888 if (emulator_read_std(addr, val, bytes, vcpu)
1889 == X86EMUL_CONTINUE)
1890 return X86EMUL_CONTINUE;
1891 if (gpa == UNMAPPED_GVA)
1892 return X86EMUL_PROPAGATE_FAULT;
1893
1894mmio:
1895 /*
1896 * Is this MMIO handled locally?
1897 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001898 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02001899 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001900 if (mmio_dev) {
1901 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001902 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001903 return X86EMUL_CONTINUE;
1904 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001905 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001906
1907 vcpu->mmio_needed = 1;
1908 vcpu->mmio_phys_addr = gpa;
1909 vcpu->mmio_size = bytes;
1910 vcpu->mmio_is_write = 0;
1911
1912 return X86EMUL_UNHANDLEABLE;
1913}
1914
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001915int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity9f811282008-03-02 14:06:05 +02001916 const void *val, int bytes)
1917{
1918 int ret;
1919
1920 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1921 if (ret < 0)
1922 return 0;
1923 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1924 return 1;
1925}
1926
Carsten Ottebbd9b642007-10-30 18:44:21 +01001927static int emulator_write_emulated_onepage(unsigned long addr,
1928 const void *val,
1929 unsigned int bytes,
1930 struct kvm_vcpu *vcpu)
1931{
1932 struct kvm_io_device *mmio_dev;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001933 gpa_t gpa;
1934
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001935 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001936
1937 if (gpa == UNMAPPED_GVA) {
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001938 kvm_inject_page_fault(vcpu, addr, 2);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001939 return X86EMUL_PROPAGATE_FAULT;
1940 }
1941
1942 /* For APIC access vmexit */
1943 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1944 goto mmio;
1945
1946 if (emulator_write_phys(vcpu, gpa, val, bytes))
1947 return X86EMUL_CONTINUE;
1948
1949mmio:
1950 /*
1951 * Is this MMIO handled locally?
1952 */
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001953 mutex_lock(&vcpu->kvm->lock);
Laurent Vivier92760492008-05-30 16:05:53 +02001954 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001955 if (mmio_dev) {
1956 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001957 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001958 return X86EMUL_CONTINUE;
1959 }
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001960 mutex_unlock(&vcpu->kvm->lock);
Carsten Ottebbd9b642007-10-30 18:44:21 +01001961
1962 vcpu->mmio_needed = 1;
1963 vcpu->mmio_phys_addr = gpa;
1964 vcpu->mmio_size = bytes;
1965 vcpu->mmio_is_write = 1;
1966 memcpy(vcpu->mmio_data, val, bytes);
1967
1968 return X86EMUL_CONTINUE;
1969}
1970
1971int emulator_write_emulated(unsigned long addr,
1972 const void *val,
1973 unsigned int bytes,
1974 struct kvm_vcpu *vcpu)
1975{
1976 /* Crossing a page boundary? */
1977 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1978 int rc, now;
1979
1980 now = -addr & ~PAGE_MASK;
1981 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1982 if (rc != X86EMUL_CONTINUE)
1983 return rc;
1984 addr += now;
1985 val += now;
1986 bytes -= now;
1987 }
1988 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1989}
1990EXPORT_SYMBOL_GPL(emulator_write_emulated);
1991
1992static int emulator_cmpxchg_emulated(unsigned long addr,
1993 const void *old,
1994 const void *new,
1995 unsigned int bytes,
1996 struct kvm_vcpu *vcpu)
1997{
1998 static int reported;
1999
2000 if (!reported) {
2001 reported = 1;
2002 printk(KERN_WARNING "kvm: emulating exchange as write\n");
2003 }
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002004#ifndef CONFIG_X86_64
2005 /* guests cmpxchg8b have to be emulated atomically */
2006 if (bytes == 8) {
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002007 gpa_t gpa;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002008 struct page *page;
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002009 char *kaddr;
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002010 u64 val;
2011
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002012 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2013
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002014 if (gpa == UNMAPPED_GVA ||
2015 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2016 goto emul_write;
2017
2018 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2019 goto emul_write;
2020
2021 val = *(u64 *)new;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002022
2023 down_read(&current->mm->mmap_sem);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002024 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002025 up_read(&current->mm->mmap_sem);
2026
Andrew Mortonc0b49b02008-02-04 22:27:18 -08002027 kaddr = kmap_atomic(page, KM_USER0);
2028 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2029 kunmap_atomic(kaddr, KM_USER0);
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002030 kvm_release_page_dirty(page);
2031 }
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002032emul_write:
Marcelo Tosatti2bacc552007-12-12 10:46:12 -05002033#endif
2034
Carsten Ottebbd9b642007-10-30 18:44:21 +01002035 return emulator_write_emulated(addr, new, bytes, vcpu);
2036}
2037
2038static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2039{
2040 return kvm_x86_ops->get_segment_base(vcpu, seg);
2041}
2042
2043int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2044{
2045 return X86EMUL_CONTINUE;
2046}
2047
2048int emulate_clts(struct kvm_vcpu *vcpu)
2049{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002050 KVMTRACE_0D(CLTS, vcpu, handler);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002051 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002052 return X86EMUL_CONTINUE;
2053}
2054
2055int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2056{
2057 struct kvm_vcpu *vcpu = ctxt->vcpu;
2058
2059 switch (dr) {
2060 case 0 ... 3:
2061 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2062 return X86EMUL_CONTINUE;
2063 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002064 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002065 return X86EMUL_UNHANDLEABLE;
2066 }
2067}
2068
2069int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2070{
2071 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2072 int exception;
2073
2074 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2075 if (exception) {
2076 /* FIXME: better handling */
2077 return X86EMUL_UNHANDLEABLE;
2078 }
2079 return X86EMUL_CONTINUE;
2080}
2081
2082void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2083{
Carsten Ottebbd9b642007-10-30 18:44:21 +01002084 u8 opcodes[4];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002085 unsigned long rip = kvm_rip_read(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002086 unsigned long rip_linear;
2087
Avi Kivityf76c7102008-06-13 22:45:42 +03002088 if (!printk_ratelimit())
Carsten Ottebbd9b642007-10-30 18:44:21 +01002089 return;
2090
Glauber Costa25be4602008-06-10 10:46:53 -03002091 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2092
Carsten Ottebbd9b642007-10-30 18:44:21 +01002093 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
2094
2095 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2096 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002097}
2098EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2099
Harvey Harrison14af3f32008-02-19 10:25:50 -08002100static struct x86_emulate_ops emulate_ops = {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002101 .read_std = emulator_read_std,
Carsten Ottebbd9b642007-10-30 18:44:21 +01002102 .read_emulated = emulator_read_emulated,
2103 .write_emulated = emulator_write_emulated,
2104 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2105};
2106
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002107static void cache_all_regs(struct kvm_vcpu *vcpu)
2108{
2109 kvm_register_read(vcpu, VCPU_REGS_RAX);
2110 kvm_register_read(vcpu, VCPU_REGS_RSP);
2111 kvm_register_read(vcpu, VCPU_REGS_RIP);
2112 vcpu->arch.regs_dirty = ~0;
2113}
2114
Carsten Ottebbd9b642007-10-30 18:44:21 +01002115int emulate_instruction(struct kvm_vcpu *vcpu,
2116 struct kvm_run *run,
2117 unsigned long cr2,
2118 u16 error_code,
Sheng Yang571008d2008-01-02 14:49:22 +08002119 int emulation_type)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002120{
2121 int r;
Sheng Yang571008d2008-01-02 14:49:22 +08002122 struct decode_cache *c;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002123
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002124 vcpu->arch.mmio_fault_cr2 = cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002125 /*
2126 * TODO: fix x86_emulate.c to use guest_read/write_register
2127 * instead of direct ->regs accesses, can save hundred cycles
2128 * on Intel for instructions that don't read/change RSP, for
2129 * for example.
2130 */
2131 cache_all_regs(vcpu);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002132
2133 vcpu->mmio_is_write = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002134 vcpu->arch.pio.string = 0;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002135
Sheng Yang571008d2008-01-02 14:49:22 +08002136 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
Carsten Ottebbd9b642007-10-30 18:44:21 +01002137 int cs_db, cs_l;
2138 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2139
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002140 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2141 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2142 vcpu->arch.emulate_ctxt.mode =
2143 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002144 ? X86EMUL_MODE_REAL : cs_l
2145 ? X86EMUL_MODE_PROT64 : cs_db
2146 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2147
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Sheng Yang571008d2008-01-02 14:49:22 +08002149
2150 /* Reject the instructions other than VMCALL/VMMCALL when
2151 * try to emulate invalid opcode */
2152 c = &vcpu->arch.emulate_ctxt.decode;
2153 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2154 (!(c->twobyte && c->b == 0x01 &&
2155 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2156 c->modrm_mod == 3 && c->modrm_rm == 1)))
2157 return EMULATE_FAIL;
2158
Avi Kivityf2b57562007-11-18 15:17:51 +02002159 ++vcpu->stat.insn_emulation;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002160 if (r) {
Avi Kivityf2b57562007-11-18 15:17:51 +02002161 ++vcpu->stat.insn_emulation_fail;
Carsten Ottebbd9b642007-10-30 18:44:21 +01002162 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2163 return EMULATE_DONE;
2164 return EMULATE_FAIL;
2165 }
2166 }
2167
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002168 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002169
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002170 if (vcpu->arch.pio.string)
Carsten Ottebbd9b642007-10-30 18:44:21 +01002171 return EMULATE_DO_MMIO;
2172
2173 if ((r || vcpu->mmio_is_write) && run) {
2174 run->exit_reason = KVM_EXIT_MMIO;
2175 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2176 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2177 run->mmio.len = vcpu->mmio_size;
2178 run->mmio.is_write = vcpu->mmio_is_write;
2179 }
2180
2181 if (r) {
2182 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2183 return EMULATE_DONE;
2184 if (!vcpu->mmio_needed) {
2185 kvm_report_emulation_failure(vcpu, "mmio");
2186 return EMULATE_FAIL;
2187 }
2188 return EMULATE_DO_MMIO;
2189 }
2190
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002191 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
Carsten Ottebbd9b642007-10-30 18:44:21 +01002192
2193 if (vcpu->mmio_is_write) {
2194 vcpu->mmio_needed = 0;
2195 return EMULATE_DO_MMIO;
2196 }
2197
2198 return EMULATE_DONE;
2199}
2200EXPORT_SYMBOL_GPL(emulate_instruction);
2201
Carsten Ottede7d7892007-10-30 18:44:25 +01002202static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2203{
2204 int i;
2205
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002206 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2207 if (vcpu->arch.pio.guest_pages[i]) {
2208 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2209 vcpu->arch.pio.guest_pages[i] = NULL;
Carsten Ottede7d7892007-10-30 18:44:25 +01002210 }
2211}
2212
2213static int pio_copy_data(struct kvm_vcpu *vcpu)
2214{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002215 void *p = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002216 void *q;
2217 unsigned bytes;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002218 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
Carsten Ottede7d7892007-10-30 18:44:25 +01002219
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002220 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
Carsten Ottede7d7892007-10-30 18:44:25 +01002221 PAGE_KERNEL);
2222 if (!q) {
2223 free_pio_guest_pages(vcpu);
2224 return -ENOMEM;
2225 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002226 q += vcpu->arch.pio.guest_page_offset;
2227 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2228 if (vcpu->arch.pio.in)
Carsten Ottede7d7892007-10-30 18:44:25 +01002229 memcpy(q, p, bytes);
2230 else
2231 memcpy(p, q, bytes);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002232 q -= vcpu->arch.pio.guest_page_offset;
Carsten Ottede7d7892007-10-30 18:44:25 +01002233 vunmap(q);
2234 free_pio_guest_pages(vcpu);
2235 return 0;
2236}
2237
2238int complete_pio(struct kvm_vcpu *vcpu)
2239{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002240 struct kvm_pio_request *io = &vcpu->arch.pio;
Carsten Ottede7d7892007-10-30 18:44:25 +01002241 long delta;
2242 int r;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002243 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002244
2245 if (!io->string) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002246 if (io->in) {
2247 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2248 memcpy(&val, vcpu->arch.pio_data, io->size);
2249 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
2250 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002251 } else {
2252 if (io->in) {
2253 r = pio_copy_data(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002254 if (r)
Carsten Ottede7d7892007-10-30 18:44:25 +01002255 return r;
Carsten Ottede7d7892007-10-30 18:44:25 +01002256 }
2257
2258 delta = 1;
2259 if (io->rep) {
2260 delta *= io->cur_count;
2261 /*
2262 * The size of the register should really depend on
2263 * current address size.
2264 */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002265 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
2266 val -= delta;
2267 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
Carsten Ottede7d7892007-10-30 18:44:25 +01002268 }
2269 if (io->down)
2270 delta = -delta;
2271 delta *= io->size;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002272 if (io->in) {
2273 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
2274 val += delta;
2275 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
2276 } else {
2277 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
2278 val += delta;
2279 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
2280 }
Carsten Ottede7d7892007-10-30 18:44:25 +01002281 }
2282
Carsten Ottede7d7892007-10-30 18:44:25 +01002283 io->count -= io->cur_count;
2284 io->cur_count = 0;
2285
2286 return 0;
2287}
2288
2289static void kernel_pio(struct kvm_io_device *pio_dev,
2290 struct kvm_vcpu *vcpu,
2291 void *pd)
2292{
2293 /* TODO: String I/O for in kernel device */
2294
2295 mutex_lock(&vcpu->kvm->lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002296 if (vcpu->arch.pio.in)
2297 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2298 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002299 pd);
2300 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002301 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2302 vcpu->arch.pio.size,
Carsten Ottede7d7892007-10-30 18:44:25 +01002303 pd);
2304 mutex_unlock(&vcpu->kvm->lock);
2305}
2306
2307static void pio_string_write(struct kvm_io_device *pio_dev,
2308 struct kvm_vcpu *vcpu)
2309{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002310 struct kvm_pio_request *io = &vcpu->arch.pio;
2311 void *pd = vcpu->arch.pio_data;
Carsten Ottede7d7892007-10-30 18:44:25 +01002312 int i;
2313
2314 mutex_lock(&vcpu->kvm->lock);
2315 for (i = 0; i < io->cur_count; i++) {
2316 kvm_iodevice_write(pio_dev, io->port,
2317 io->size,
2318 pd);
2319 pd += io->size;
2320 }
2321 mutex_unlock(&vcpu->kvm->lock);
2322}
2323
2324static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +02002325 gpa_t addr, int len,
2326 int is_write)
Carsten Ottede7d7892007-10-30 18:44:25 +01002327{
Laurent Vivier92760492008-05-30 16:05:53 +02002328 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
Carsten Ottede7d7892007-10-30 18:44:25 +01002329}
2330
2331int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2332 int size, unsigned port)
2333{
2334 struct kvm_io_device *pio_dev;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002335 unsigned long val;
Carsten Ottede7d7892007-10-30 18:44:25 +01002336
2337 vcpu->run->exit_reason = KVM_EXIT_IO;
2338 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002339 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002340 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002341 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2342 vcpu->run->io.port = vcpu->arch.pio.port = port;
2343 vcpu->arch.pio.in = in;
2344 vcpu->arch.pio.string = 0;
2345 vcpu->arch.pio.down = 0;
2346 vcpu->arch.pio.guest_page_offset = 0;
2347 vcpu->arch.pio.rep = 0;
Carsten Ottede7d7892007-10-30 18:44:25 +01002348
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002349 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2350 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2351 handler);
2352 else
2353 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2354 handler);
2355
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002356 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
2357 memcpy(vcpu->arch.pio_data, &val, 4);
Carsten Ottede7d7892007-10-30 18:44:25 +01002358
2359 kvm_x86_ops->skip_emulated_instruction(vcpu);
2360
Laurent Vivier92760492008-05-30 16:05:53 +02002361 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
Carsten Ottede7d7892007-10-30 18:44:25 +01002362 if (pio_dev) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002363 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
Carsten Ottede7d7892007-10-30 18:44:25 +01002364 complete_pio(vcpu);
2365 return 1;
2366 }
2367 return 0;
2368}
2369EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2370
2371int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2372 int size, unsigned long count, int down,
2373 gva_t address, int rep, unsigned port)
2374{
2375 unsigned now, in_page;
2376 int i, ret = 0;
2377 int nr_pages = 1;
2378 struct page *page;
2379 struct kvm_io_device *pio_dev;
2380
2381 vcpu->run->exit_reason = KVM_EXIT_IO;
2382 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002383 vcpu->run->io.size = vcpu->arch.pio.size = size;
Carsten Ottede7d7892007-10-30 18:44:25 +01002384 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002385 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2386 vcpu->run->io.port = vcpu->arch.pio.port = port;
2387 vcpu->arch.pio.in = in;
2388 vcpu->arch.pio.string = 1;
2389 vcpu->arch.pio.down = down;
2390 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2391 vcpu->arch.pio.rep = rep;
Carsten Ottede7d7892007-10-30 18:44:25 +01002392
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002393 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2394 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2395 handler);
2396 else
2397 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2398 handler);
2399
Carsten Ottede7d7892007-10-30 18:44:25 +01002400 if (!count) {
2401 kvm_x86_ops->skip_emulated_instruction(vcpu);
2402 return 1;
2403 }
2404
2405 if (!down)
2406 in_page = PAGE_SIZE - offset_in_page(address);
2407 else
2408 in_page = offset_in_page(address) + size;
2409 now = min(count, (unsigned long)in_page / size);
2410 if (!now) {
2411 /*
2412 * String I/O straddles page boundary. Pin two guest pages
2413 * so that we satisfy atomicity constraints. Do just one
2414 * transaction to avoid complexity.
2415 */
2416 nr_pages = 2;
2417 now = 1;
2418 }
2419 if (down) {
2420 /*
2421 * String I/O in reverse. Yuck. Kill the guest, fix later.
2422 */
2423 pr_unimpl(vcpu, "guest string pio down\n");
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002424 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002425 return 1;
2426 }
2427 vcpu->run->io.count = now;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002428 vcpu->arch.pio.cur_count = now;
Carsten Ottede7d7892007-10-30 18:44:25 +01002429
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002430 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
Carsten Ottede7d7892007-10-30 18:44:25 +01002431 kvm_x86_ops->skip_emulated_instruction(vcpu);
2432
2433 for (i = 0; i < nr_pages; ++i) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002434 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002435 vcpu->arch.pio.guest_pages[i] = page;
Carsten Ottede7d7892007-10-30 18:44:25 +01002436 if (!page) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002437 kvm_inject_gp(vcpu, 0);
Carsten Ottede7d7892007-10-30 18:44:25 +01002438 free_pio_guest_pages(vcpu);
2439 return 1;
2440 }
2441 }
2442
Laurent Vivier92760492008-05-30 16:05:53 +02002443 pio_dev = vcpu_find_pio_dev(vcpu, port,
2444 vcpu->arch.pio.cur_count,
2445 !vcpu->arch.pio.in);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002446 if (!vcpu->arch.pio.in) {
Carsten Ottede7d7892007-10-30 18:44:25 +01002447 /* string PIO write */
2448 ret = pio_copy_data(vcpu);
2449 if (ret >= 0 && pio_dev) {
2450 pio_string_write(pio_dev, vcpu);
2451 complete_pio(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002452 if (vcpu->arch.pio.count == 0)
Carsten Ottede7d7892007-10-30 18:44:25 +01002453 ret = 1;
2454 }
2455 } else if (pio_dev)
2456 pr_unimpl(vcpu, "no string pio read support yet, "
2457 "port %x size %d count %ld\n",
2458 port, size, count);
2459
2460 return ret;
2461}
2462EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2463
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002464int kvm_arch_init(void *opaque)
Carsten Otte043405e2007-10-10 17:16:19 +02002465{
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002466 int r;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002467 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2468
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002469 if (kvm_x86_ops) {
2470 printk(KERN_ERR "kvm: already loaded the other module\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002471 r = -EEXIST;
2472 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002473 }
2474
2475 if (!ops->cpu_has_kvm_support()) {
2476 printk(KERN_ERR "kvm: no hardware support\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002477 r = -EOPNOTSUPP;
2478 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002479 }
2480 if (ops->disabled_by_bios()) {
2481 printk(KERN_ERR "kvm: disabled by bios\n");
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002482 r = -EOPNOTSUPP;
2483 goto out;
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002484 }
2485
Avi Kivity97db56c2008-01-13 13:23:56 +02002486 r = kvm_mmu_module_init();
2487 if (r)
2488 goto out;
2489
2490 kvm_init_msr_list();
2491
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002492 kvm_x86_ops = ops;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002493 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
Sheng Yang7b523452008-04-25 21:13:50 +08002494 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2495 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
2496 PT_DIRTY_MASK, PT64_NX_MASK, 0);
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002497 return 0;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002498
2499out:
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002500 return r;
Carsten Otte043405e2007-10-10 17:16:19 +02002501}
Hollis Blanchard8776e512007-10-31 17:24:24 -05002502
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002503void kvm_arch_exit(void)
2504{
2505 kvm_x86_ops = NULL;
Zhang Xiantao56c6d282007-11-18 20:43:21 +08002506 kvm_mmu_module_exit();
2507}
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08002508
Hollis Blanchard8776e512007-10-31 17:24:24 -05002509int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2510{
2511 ++vcpu->stat.halt_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002512 KVMTRACE_0D(HLT, vcpu, handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002513 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +03002514 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002515 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002516 kvm_vcpu_block(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002517 down_read(&vcpu->kvm->slots_lock);
Avi Kivitya4535292008-04-13 17:54:35 +03002518 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002519 return -EINTR;
2520 return 1;
2521 } else {
2522 vcpu->run->exit_reason = KVM_EXIT_HLT;
2523 return 0;
2524 }
2525}
2526EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2527
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002528static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2529 unsigned long a1)
2530{
2531 if (is_long_mode(vcpu))
2532 return a0;
2533 else
2534 return a0 | ((gpa_t)a1 << 32);
2535}
2536
Hollis Blanchard8776e512007-10-31 17:24:24 -05002537int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2538{
2539 unsigned long nr, a0, a1, a2, a3, ret;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002540 int r = 1;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002541
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002542 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
2543 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
2544 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
2545 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
2546 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002547
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002548 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2549
Hollis Blanchard8776e512007-10-31 17:24:24 -05002550 if (!is_long_mode(vcpu)) {
2551 nr &= 0xFFFFFFFF;
2552 a0 &= 0xFFFFFFFF;
2553 a1 &= 0xFFFFFFFF;
2554 a2 &= 0xFFFFFFFF;
2555 a3 &= 0xFFFFFFFF;
2556 }
2557
2558 switch (nr) {
Avi Kivityb93463a2007-10-25 16:52:32 +02002559 case KVM_HC_VAPIC_POLL_IRQ:
2560 ret = 0;
2561 break;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002562 case KVM_HC_MMU_OP:
2563 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2564 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002565 default:
2566 ret = -KVM_ENOSYS;
2567 break;
2568 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002569 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
Amit Shahf11c3a82008-02-21 01:00:30 +05302570 ++vcpu->stat.hypercalls;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002571 return r;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002572}
2573EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2574
2575int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2576{
2577 char instruction[3];
2578 int ret = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002579 unsigned long rip = kvm_rip_read(vcpu);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002580
Hollis Blanchard8776e512007-10-31 17:24:24 -05002581
2582 /*
2583 * Blow out the MMU to ensure that no other VCPU has an active mapping
2584 * to ensure that the updated hypercall appears atomically across all
2585 * VCPUs.
2586 */
2587 kvm_mmu_zap_all(vcpu->kvm);
2588
Hollis Blanchard8776e512007-10-31 17:24:24 -05002589 kvm_x86_ops->patch_hypercall(vcpu, instruction);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002590 if (emulator_write_emulated(rip, instruction, 3, vcpu)
Hollis Blanchard8776e512007-10-31 17:24:24 -05002591 != X86EMUL_CONTINUE)
2592 ret = -EFAULT;
2593
Hollis Blanchard8776e512007-10-31 17:24:24 -05002594 return ret;
2595}
2596
2597static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2598{
2599 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2600}
2601
2602void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2603{
2604 struct descriptor_table dt = { limit, base };
2605
2606 kvm_x86_ops->set_gdt(vcpu, &dt);
2607}
2608
2609void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2610{
2611 struct descriptor_table dt = { limit, base };
2612
2613 kvm_x86_ops->set_idt(vcpu, &dt);
2614}
2615
2616void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2617 unsigned long *rflags)
2618{
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002619 kvm_lmsw(vcpu, msw);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002620 *rflags = kvm_x86_ops->get_rflags(vcpu);
2621}
2622
2623unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2624{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002625 unsigned long value;
2626
Hollis Blanchard8776e512007-10-31 17:24:24 -05002627 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2628 switch (cr) {
2629 case 0:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002630 value = vcpu->arch.cr0;
2631 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002632 case 2:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002633 value = vcpu->arch.cr2;
2634 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002635 case 3:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002636 value = vcpu->arch.cr3;
2637 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002638 case 4:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002639 value = vcpu->arch.cr4;
2640 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002641 case 8:
Joerg Roedel54e445c2008-04-30 17:56:02 +02002642 value = kvm_get_cr8(vcpu);
2643 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002644 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002645 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002646 return 0;
2647 }
Joerg Roedel54e445c2008-04-30 17:56:02 +02002648 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2649 (u32)((u64)value >> 32), handler);
2650
2651 return value;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002652}
2653
2654void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2655 unsigned long *rflags)
2656{
Joerg Roedel54e445c2008-04-30 17:56:02 +02002657 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2658 (u32)((u64)val >> 32), handler);
2659
Hollis Blanchard8776e512007-10-31 17:24:24 -05002660 switch (cr) {
2661 case 0:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002662 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002663 *rflags = kvm_x86_ops->get_rflags(vcpu);
2664 break;
2665 case 2:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002666 vcpu->arch.cr2 = val;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002667 break;
2668 case 3:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002669 kvm_set_cr3(vcpu, val);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002670 break;
2671 case 4:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002672 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
Hollis Blanchard8776e512007-10-31 17:24:24 -05002673 break;
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002674 case 8:
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002675 kvm_set_cr8(vcpu, val & 0xfUL);
Joerg Roedel152ff9b2007-12-06 15:46:52 +01002676 break;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002677 default:
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002678 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002679 }
2680}
2681
Dan Kenigsberg07716712007-11-21 17:10:04 +02002682static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2683{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002684 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2685 int j, nent = vcpu->arch.cpuid_nent;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002686
2687 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2688 /* when no next entry is found, the current entry[i] is reselected */
2689 for (j = i + 1; j == i; j = (j + 1) % nent) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002690 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002691 if (ej->function == e->function) {
2692 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2693 return j;
2694 }
2695 }
2696 return 0; /* silence gcc, even though control never reaches here */
2697}
2698
2699/* find an entry with matching function, matching index (if needed), and that
2700 * should be read next (if it's stateful) */
2701static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2702 u32 function, u32 index)
2703{
2704 if (e->function != function)
2705 return 0;
2706 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2707 return 0;
2708 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2709 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2710 return 0;
2711 return 1;
2712}
2713
Hollis Blanchard8776e512007-10-31 17:24:24 -05002714void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2715{
2716 int i;
Dan Kenigsberg07716712007-11-21 17:10:04 +02002717 u32 function, index;
2718 struct kvm_cpuid_entry2 *e, *best;
Hollis Blanchard8776e512007-10-31 17:24:24 -05002719
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002720 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
2721 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
2722 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
2723 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
2724 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
2725 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002726 best = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002727 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2728 e = &vcpu->arch.cpuid_entries[i];
Dan Kenigsberg07716712007-11-21 17:10:04 +02002729 if (is_matching_cpuid_entry(e, function, index)) {
2730 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2731 move_to_next_stateful_cpuid_entry(vcpu, i);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002732 best = e;
2733 break;
2734 }
2735 /*
2736 * Both basic or both extended?
2737 */
2738 if (((e->function ^ function) & 0x80000000) == 0)
2739 if (!best || e->function > best->function)
2740 best = e;
2741 }
2742 if (best) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002743 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
2744 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
2745 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
2746 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002747 }
Hollis Blanchard8776e512007-10-31 17:24:24 -05002748 kvm_x86_ops->skip_emulated_instruction(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002749 KVMTRACE_5D(CPUID, vcpu, function,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002750 (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
2751 (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
2752 (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
2753 (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
Hollis Blanchard8776e512007-10-31 17:24:24 -05002754}
2755EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
Hollis Blanchardd0752062007-10-31 17:24:25 -05002756
2757/*
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002758 * Check if userspace requested an interrupt window, and that the
2759 * interrupt window is open.
2760 *
2761 * No need to exit to userspace if we already have an interrupt queued.
2762 */
2763static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2764 struct kvm_run *kvm_run)
2765{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002766 return (!vcpu->arch.irq_summary &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002767 kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002768 vcpu->arch.interrupt_window_open &&
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002769 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2770}
2771
2772static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2773 struct kvm_run *kvm_run)
2774{
2775 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002776 kvm_run->cr8 = kvm_get_cr8(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002777 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2778 if (irqchip_in_kernel(vcpu->kvm))
2779 kvm_run->ready_for_interrupt_injection = 1;
2780 else
2781 kvm_run->ready_for_interrupt_injection =
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002782 (vcpu->arch.interrupt_window_open &&
2783 vcpu->arch.irq_summary == 0);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002784}
2785
Avi Kivityb93463a2007-10-25 16:52:32 +02002786static void vapic_enter(struct kvm_vcpu *vcpu)
2787{
2788 struct kvm_lapic *apic = vcpu->arch.apic;
2789 struct page *page;
2790
2791 if (!apic || !apic->vapic_addr)
2792 return;
2793
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002794 down_read(&current->mm->mmap_sem);
Avi Kivityb93463a2007-10-25 16:52:32 +02002795 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002796 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002797
2798 vcpu->arch.apic->vapic_page = page;
Avi Kivityb93463a2007-10-25 16:52:32 +02002799}
2800
2801static void vapic_exit(struct kvm_vcpu *vcpu)
2802{
2803 struct kvm_lapic *apic = vcpu->arch.apic;
2804
2805 if (!apic || !apic->vapic_addr)
2806 return;
2807
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03002808 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02002809 kvm_release_page_dirty(apic->vapic_page);
2810 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
Marcelo Tosattif8b78fa2008-06-23 12:04:25 -03002811 up_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02002812}
2813
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002814static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2815{
2816 int r;
2817
Avi Kivitya4535292008-04-13 17:54:35 +03002818 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002819 pr_debug("vcpu %d received sipi with vector # %x\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002820 vcpu->vcpu_id, vcpu->arch.sipi_vector);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002821 kvm_lapic_reset(vcpu);
2822 r = kvm_x86_ops->vcpu_reset(vcpu);
2823 if (r)
2824 return r;
Avi Kivitya4535292008-04-13 17:54:35 +03002825 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002826 }
2827
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002828 down_read(&vcpu->kvm->slots_lock);
Avi Kivityb93463a2007-10-25 16:52:32 +02002829 vapic_enter(vcpu);
2830
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002831preempted:
2832 if (vcpu->guest_debug.enabled)
2833 kvm_x86_ops->guest_debug_pre(vcpu);
2834
2835again:
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05002836 if (vcpu->requests)
2837 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
2838 kvm_mmu_unload(vcpu);
2839
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002840 r = kvm_mmu_reload(vcpu);
2841 if (unlikely(r))
2842 goto out;
2843
Avi Kivity2f52d582008-01-16 12:49:30 +02002844 if (vcpu->requests) {
2845 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
Marcelo Tosatti2f599712008-05-27 12:10:20 -03002846 __kvm_migrate_timers(vcpu);
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03002847 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2848 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivityb93463a2007-10-25 16:52:32 +02002849 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2850 &vcpu->requests)) {
2851 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2852 r = 0;
2853 goto out;
2854 }
Joerg Roedel71c4dfa2008-02-26 16:49:16 +01002855 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
2856 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2857 r = 0;
2858 goto out;
2859 }
Avi Kivity2f52d582008-01-16 12:49:30 +02002860 }
Avi Kivityb93463a2007-10-25 16:52:32 +02002861
Marcelo Tosatti06e05642008-06-06 16:37:36 -03002862 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002863 kvm_inject_pending_timer_irqs(vcpu);
2864
2865 preempt_disable();
2866
2867 kvm_x86_ops->prepare_guest_switch(vcpu);
2868 kvm_load_guest_fpu(vcpu);
2869
2870 local_irq_disable();
2871
Marcelo Tosattid4acf7e2008-06-06 16:37:35 -03002872 if (vcpu->requests || need_resched()) {
Avi Kivity6c1428012008-01-15 18:27:32 +02002873 local_irq_enable();
2874 preempt_enable();
2875 r = 1;
2876 goto out;
2877 }
2878
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002879 if (signal_pending(current)) {
2880 local_irq_enable();
2881 preempt_enable();
2882 r = -EINTR;
2883 kvm_run->exit_reason = KVM_EXIT_INTR;
2884 ++vcpu->stat.signal_exits;
2885 goto out;
2886 }
2887
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03002888 vcpu->guest_mode = 1;
2889 /*
2890 * Make sure that guest_mode assignment won't happen after
2891 * testing the pending IRQ vector bitmap.
2892 */
2893 smp_wmb();
2894
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002895 if (vcpu->arch.exception.pending)
Avi Kivity298101d2007-11-25 13:41:11 +02002896 __queue_exception(vcpu);
2897 else if (irqchip_in_kernel(vcpu->kvm))
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002898 kvm_x86_ops->inject_pending_irq(vcpu);
Avi Kivityeb9774f2007-11-25 17:45:31 +02002899 else
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002900 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2901
Avi Kivityb93463a2007-10-25 16:52:32 +02002902 kvm_lapic_sync_to_vapic(vcpu);
2903
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002904 up_read(&vcpu->kvm->slots_lock);
2905
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002906 kvm_guest_enter();
2907
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002908
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002909 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002910 kvm_x86_ops->run(vcpu, kvm_run);
2911
2912 vcpu->guest_mode = 0;
2913 local_irq_enable();
2914
2915 ++vcpu->stat.exits;
2916
2917 /*
2918 * We must have an instruction between local_irq_enable() and
2919 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2920 * the interrupt shadow. The stat.exits increment will do nicely.
2921 * But we need to prevent reordering, hence this barrier():
2922 */
2923 barrier();
2924
2925 kvm_guest_exit();
2926
2927 preempt_enable();
2928
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002929 down_read(&vcpu->kvm->slots_lock);
2930
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002931 /*
2932 * Profile KVM exit RIPs:
2933 */
2934 if (unlikely(prof_on == KVM_PROFILING)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002935 unsigned long rip = kvm_rip_read(vcpu);
2936 profile_hit(KVM_PROFILING, (void *)rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002937 }
2938
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002939 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2940 vcpu->arch.exception.pending = false;
Avi Kivity298101d2007-11-25 13:41:11 +02002941
Avi Kivityb93463a2007-10-25 16:52:32 +02002942 kvm_lapic_sync_from_vapic(vcpu);
2943
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002944 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2945
2946 if (r > 0) {
2947 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2948 r = -EINTR;
2949 kvm_run->exit_reason = KVM_EXIT_INTR;
2950 ++vcpu->stat.request_irq_exits;
2951 goto out;
2952 }
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002953 if (!need_resched())
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002954 goto again;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002955 }
2956
2957out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002958 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002959 if (r > 0) {
2960 kvm_resched(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002961 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002962 goto preempted;
2963 }
2964
2965 post_kvm_run_save(vcpu, kvm_run);
2966
Avi Kivityb93463a2007-10-25 16:52:32 +02002967 vapic_exit(vcpu);
2968
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002969 return r;
2970}
2971
2972int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2973{
2974 int r;
2975 sigset_t sigsaved;
2976
2977 vcpu_load(vcpu);
2978
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002979 if (vcpu->sigset_active)
2980 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2981
Avi Kivityac9f6dc2008-07-06 15:48:31 +03002982 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
2983 kvm_vcpu_block(vcpu);
2984 r = -EAGAIN;
2985 goto out;
2986 }
2987
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002988 /* re-sync apic's tpr */
2989 if (!irqchip_in_kernel(vcpu->kvm))
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002990 kvm_set_cr8(vcpu, kvm_run->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002991
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002992 if (vcpu->arch.pio.cur_count) {
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05002993 r = complete_pio(vcpu);
2994 if (r)
2995 goto out;
2996 }
2997#if CONFIG_HAS_IOMEM
2998 if (vcpu->mmio_needed) {
2999 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3000 vcpu->mmio_read_completed = 1;
3001 vcpu->mmio_needed = 0;
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003002
3003 down_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003004 r = emulate_instruction(vcpu, kvm_run,
Sheng Yang571008d2008-01-02 14:49:22 +08003005 vcpu->arch.mmio_fault_cr2, 0,
3006 EMULTYPE_NO_DECODE);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003007 up_read(&vcpu->kvm->slots_lock);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003008 if (r == EMULATE_DO_MMIO) {
3009 /*
3010 * Read-modify-write. Back to userspace.
3011 */
3012 r = 0;
3013 goto out;
3014 }
3015 }
3016#endif
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003017 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
3018 kvm_register_write(vcpu, VCPU_REGS_RAX,
3019 kvm_run->hypercall.ret);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003020
3021 r = __vcpu_run(vcpu, kvm_run);
3022
3023out:
3024 if (vcpu->sigset_active)
3025 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3026
3027 vcpu_put(vcpu);
3028 return r;
3029}
3030
3031int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3032{
3033 vcpu_load(vcpu);
3034
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003035 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3036 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3037 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3038 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3039 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3040 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
3041 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3042 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003043#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003044 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
3045 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
3046 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
3047 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
3048 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
3049 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
3050 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
3051 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003052#endif
3053
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003054 regs->rip = kvm_rip_read(vcpu);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003055 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3056
3057 /*
3058 * Don't leak debug flags in case they were set for guest debugging
3059 */
3060 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
3061 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3062
3063 vcpu_put(vcpu);
3064
3065 return 0;
3066}
3067
3068int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3069{
3070 vcpu_load(vcpu);
3071
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003072 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
3073 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
3074 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
3075 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
3076 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
3077 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
3078 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
3079 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003080#ifdef CONFIG_X86_64
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003081 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
3082 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
3083 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
3084 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
3085 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
3086 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
3087 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
3088 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
3089
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003090#endif
3091
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003092 kvm_rip_write(vcpu, regs->rip);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003093 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3094
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003095
Jan Kiszkab4f14ab2008-04-30 17:59:04 +02003096 vcpu->arch.exception.pending = false;
3097
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003098 vcpu_put(vcpu);
3099
3100 return 0;
3101}
3102
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003103void kvm_get_segment(struct kvm_vcpu *vcpu,
3104 struct kvm_segment *var, int seg)
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003105{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003106 kvm_x86_ops->get_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003107}
3108
3109void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3110{
3111 struct kvm_segment cs;
3112
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003113 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003114 *db = cs.db;
3115 *l = cs.l;
3116}
3117EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3118
3119int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3120 struct kvm_sregs *sregs)
3121{
3122 struct descriptor_table dt;
3123 int pending_vec;
3124
3125 vcpu_load(vcpu);
3126
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003127 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3128 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3129 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3130 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3131 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3132 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003133
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003134 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3135 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003136
3137 kvm_x86_ops->get_idt(vcpu, &dt);
3138 sregs->idt.limit = dt.limit;
3139 sregs->idt.base = dt.base;
3140 kvm_x86_ops->get_gdt(vcpu, &dt);
3141 sregs->gdt.limit = dt.limit;
3142 sregs->gdt.base = dt.base;
3143
3144 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003145 sregs->cr0 = vcpu->arch.cr0;
3146 sregs->cr2 = vcpu->arch.cr2;
3147 sregs->cr3 = vcpu->arch.cr3;
3148 sregs->cr4 = vcpu->arch.cr4;
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003149 sregs->cr8 = kvm_get_cr8(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003150 sregs->efer = vcpu->arch.shadow_efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003151 sregs->apic_base = kvm_get_apic_base(vcpu);
3152
3153 if (irqchip_in_kernel(vcpu->kvm)) {
3154 memset(sregs->interrupt_bitmap, 0,
3155 sizeof sregs->interrupt_bitmap);
3156 pending_vec = kvm_x86_ops->get_irq(vcpu);
3157 if (pending_vec >= 0)
3158 set_bit(pending_vec,
3159 (unsigned long *)sregs->interrupt_bitmap);
3160 } else
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003161 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003162 sizeof sregs->interrupt_bitmap);
3163
3164 vcpu_put(vcpu);
3165
3166 return 0;
3167}
3168
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003169int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3170 struct kvm_mp_state *mp_state)
3171{
3172 vcpu_load(vcpu);
3173 mp_state->mp_state = vcpu->arch.mp_state;
3174 vcpu_put(vcpu);
3175 return 0;
3176}
3177
3178int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3179 struct kvm_mp_state *mp_state)
3180{
3181 vcpu_load(vcpu);
3182 vcpu->arch.mp_state = mp_state->mp_state;
3183 vcpu_put(vcpu);
3184 return 0;
3185}
3186
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003187static void kvm_set_segment(struct kvm_vcpu *vcpu,
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003188 struct kvm_segment *var, int seg)
3189{
Harvey Harrison14af3f32008-02-19 10:25:50 -08003190 kvm_x86_ops->set_segment(vcpu, var, seg);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003191}
3192
Izik Eidus37817f22008-03-24 23:14:53 +02003193static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3194 struct kvm_segment *kvm_desct)
3195{
3196 kvm_desct->base = seg_desc->base0;
3197 kvm_desct->base |= seg_desc->base1 << 16;
3198 kvm_desct->base |= seg_desc->base2 << 24;
3199 kvm_desct->limit = seg_desc->limit0;
3200 kvm_desct->limit |= seg_desc->limit << 16;
Marcelo Tosattic93cd3a2008-07-19 19:08:07 -03003201 if (seg_desc->g) {
3202 kvm_desct->limit <<= 12;
3203 kvm_desct->limit |= 0xfff;
3204 }
Izik Eidus37817f22008-03-24 23:14:53 +02003205 kvm_desct->selector = selector;
3206 kvm_desct->type = seg_desc->type;
3207 kvm_desct->present = seg_desc->p;
3208 kvm_desct->dpl = seg_desc->dpl;
3209 kvm_desct->db = seg_desc->d;
3210 kvm_desct->s = seg_desc->s;
3211 kvm_desct->l = seg_desc->l;
3212 kvm_desct->g = seg_desc->g;
3213 kvm_desct->avl = seg_desc->avl;
3214 if (!selector)
3215 kvm_desct->unusable = 1;
3216 else
3217 kvm_desct->unusable = 0;
3218 kvm_desct->padding = 0;
3219}
3220
3221static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
3222 u16 selector,
3223 struct descriptor_table *dtable)
3224{
3225 if (selector & 1 << 2) {
3226 struct kvm_segment kvm_seg;
3227
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003228 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
Izik Eidus37817f22008-03-24 23:14:53 +02003229
3230 if (kvm_seg.unusable)
3231 dtable->limit = 0;
3232 else
3233 dtable->limit = kvm_seg.limit;
3234 dtable->base = kvm_seg.base;
3235 }
3236 else
3237 kvm_x86_ops->get_gdt(vcpu, dtable);
3238}
3239
3240/* allowed just for 8 bytes segments */
3241static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3242 struct desc_struct *seg_desc)
3243{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003244 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003245 struct descriptor_table dtable;
3246 u16 index = selector >> 3;
3247
3248 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3249
3250 if (dtable.limit < index * 8 + 7) {
3251 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3252 return 1;
3253 }
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003254 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3255 gpa += index * 8;
3256 return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003257}
3258
3259/* allowed just for 8 bytes segments */
3260static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3261 struct desc_struct *seg_desc)
3262{
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003263 gpa_t gpa;
Izik Eidus37817f22008-03-24 23:14:53 +02003264 struct descriptor_table dtable;
3265 u16 index = selector >> 3;
3266
3267 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3268
3269 if (dtable.limit < index * 8 + 7)
3270 return 1;
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003271 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
3272 gpa += index * 8;
3273 return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
Izik Eidus37817f22008-03-24 23:14:53 +02003274}
3275
3276static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3277 struct desc_struct *seg_desc)
3278{
3279 u32 base_addr;
3280
3281 base_addr = seg_desc->base0;
3282 base_addr |= (seg_desc->base1 << 16);
3283 base_addr |= (seg_desc->base2 << 24);
3284
Marcelo Tosatti98899aa2008-07-16 19:07:10 -03003285 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
Izik Eidus37817f22008-03-24 23:14:53 +02003286}
3287
Izik Eidus37817f22008-03-24 23:14:53 +02003288static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3289{
3290 struct kvm_segment kvm_seg;
3291
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003292 kvm_get_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003293 return kvm_seg.selector;
3294}
3295
3296static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3297 u16 selector,
3298 struct kvm_segment *kvm_seg)
3299{
3300 struct desc_struct seg_desc;
3301
3302 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3303 return 1;
3304 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3305 return 0;
3306}
3307
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003308int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3309 int type_bits, int seg)
Izik Eidus37817f22008-03-24 23:14:53 +02003310{
3311 struct kvm_segment kvm_seg;
3312
3313 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3314 return 1;
3315 kvm_seg.type |= type_bits;
3316
3317 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3318 seg != VCPU_SREG_LDTR)
3319 if (!kvm_seg.s)
3320 kvm_seg.unusable = 1;
3321
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003322 kvm_set_segment(vcpu, &kvm_seg, seg);
Izik Eidus37817f22008-03-24 23:14:53 +02003323 return 0;
3324}
3325
3326static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3327 struct tss_segment_32 *tss)
3328{
3329 tss->cr3 = vcpu->arch.cr3;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003330 tss->eip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003331 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003332 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3333 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3334 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3335 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3336 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3337 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3338 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
3339 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003340 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3341 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3342 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3343 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3344 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3345 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3346 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3347 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3348}
3349
3350static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3351 struct tss_segment_32 *tss)
3352{
3353 kvm_set_cr3(vcpu, tss->cr3);
3354
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003355 kvm_rip_write(vcpu, tss->eip);
Izik Eidus37817f22008-03-24 23:14:53 +02003356 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3357
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003358 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
3359 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
3360 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
3361 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
3362 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
3363 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
3364 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
3365 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
Izik Eidus37817f22008-03-24 23:14:53 +02003366
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003367 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003368 return 1;
3369
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003370 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003371 return 1;
3372
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003373 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003374 return 1;
3375
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003376 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003377 return 1;
3378
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003379 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003380 return 1;
3381
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003382 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
Izik Eidus37817f22008-03-24 23:14:53 +02003383 return 1;
3384
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003385 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
Izik Eidus37817f22008-03-24 23:14:53 +02003386 return 1;
3387 return 0;
3388}
3389
3390static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3391 struct tss_segment_16 *tss)
3392{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003393 tss->ip = kvm_rip_read(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003394 tss->flag = kvm_x86_ops->get_rflags(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003395 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
3396 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
3397 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
3398 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
3399 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
3400 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
3401 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
3402 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
Izik Eidus37817f22008-03-24 23:14:53 +02003403
3404 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3405 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3406 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3407 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3408 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3409 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3410}
3411
3412static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3413 struct tss_segment_16 *tss)
3414{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003415 kvm_rip_write(vcpu, tss->ip);
Izik Eidus37817f22008-03-24 23:14:53 +02003416 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003417 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
3418 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
3419 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
3420 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
3421 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
3422 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
3423 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
3424 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
Izik Eidus37817f22008-03-24 23:14:53 +02003425
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003426 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
Izik Eidus37817f22008-03-24 23:14:53 +02003427 return 1;
3428
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003429 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
Izik Eidus37817f22008-03-24 23:14:53 +02003430 return 1;
3431
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003432 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
Izik Eidus37817f22008-03-24 23:14:53 +02003433 return 1;
3434
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003435 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
Izik Eidus37817f22008-03-24 23:14:53 +02003436 return 1;
3437
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003438 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
Izik Eidus37817f22008-03-24 23:14:53 +02003439 return 1;
3440 return 0;
3441}
3442
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003443static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003444 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003445 struct desc_struct *nseg_desc)
3446{
3447 struct tss_segment_16 tss_segment_16;
3448 int ret = 0;
3449
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003450 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3451 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003452 goto out;
3453
3454 save_state_to_tss16(vcpu, &tss_segment_16);
Izik Eidus37817f22008-03-24 23:14:53 +02003455
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003456 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
3457 sizeof tss_segment_16))
Izik Eidus37817f22008-03-24 23:14:53 +02003458 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003459
3460 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3461 &tss_segment_16, sizeof tss_segment_16))
3462 goto out;
3463
Izik Eidus37817f22008-03-24 23:14:53 +02003464 if (load_state_from_tss16(vcpu, &tss_segment_16))
3465 goto out;
3466
3467 ret = 1;
3468out:
3469 return ret;
3470}
3471
Harvey Harrison8b2cf732008-04-27 12:14:13 -07003472static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003473 u32 old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003474 struct desc_struct *nseg_desc)
3475{
3476 struct tss_segment_32 tss_segment_32;
3477 int ret = 0;
3478
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003479 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3480 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003481 goto out;
3482
3483 save_state_to_tss32(vcpu, &tss_segment_32);
Izik Eidus37817f22008-03-24 23:14:53 +02003484
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003485 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
3486 sizeof tss_segment_32))
Izik Eidus37817f22008-03-24 23:14:53 +02003487 goto out;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003488
3489 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
3490 &tss_segment_32, sizeof tss_segment_32))
3491 goto out;
3492
Izik Eidus37817f22008-03-24 23:14:53 +02003493 if (load_state_from_tss32(vcpu, &tss_segment_32))
3494 goto out;
3495
3496 ret = 1;
3497out:
3498 return ret;
3499}
3500
3501int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3502{
3503 struct kvm_segment tr_seg;
3504 struct desc_struct cseg_desc;
3505 struct desc_struct nseg_desc;
3506 int ret = 0;
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003507 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
3508 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003509
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003510 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
Izik Eidus37817f22008-03-24 23:14:53 +02003511
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003512 /* FIXME: Handle errors. Failure to read either TSS or their
3513 * descriptors should generate a pagefault.
3514 */
Izik Eidus37817f22008-03-24 23:14:53 +02003515 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3516 goto out;
3517
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003518 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
Izik Eidus37817f22008-03-24 23:14:53 +02003519 goto out;
3520
Izik Eidus37817f22008-03-24 23:14:53 +02003521 if (reason != TASK_SWITCH_IRET) {
3522 int cpl;
3523
3524 cpl = kvm_x86_ops->get_cpl(vcpu);
3525 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3526 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3527 return 1;
3528 }
3529 }
3530
3531 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3532 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3533 return 1;
3534 }
3535
3536 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003537 cseg_desc.type &= ~(1 << 1); //clear the B flag
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003538 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
Izik Eidus37817f22008-03-24 23:14:53 +02003539 }
3540
3541 if (reason == TASK_SWITCH_IRET) {
3542 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3543 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
3544 }
3545
3546 kvm_x86_ops->skip_emulated_instruction(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003547
3548 if (nseg_desc.type & 8)
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003549 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003550 &nseg_desc);
3551 else
Marcelo Tosatti34198bf82008-07-16 19:07:11 -03003552 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
Izik Eidus37817f22008-03-24 23:14:53 +02003553 &nseg_desc);
3554
3555 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
3556 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3557 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
3558 }
3559
3560 if (reason != TASK_SWITCH_IRET) {
Izik Eidus3fe913e2008-04-28 18:23:52 +03003561 nseg_desc.type |= (1 << 1);
Izik Eidus37817f22008-03-24 23:14:53 +02003562 save_guest_segment_descriptor(vcpu, tss_selector,
3563 &nseg_desc);
3564 }
3565
3566 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
3567 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
3568 tr_seg.type = 11;
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003569 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
Izik Eidus37817f22008-03-24 23:14:53 +02003570out:
Izik Eidus37817f22008-03-24 23:14:53 +02003571 return ret;
3572}
3573EXPORT_SYMBOL_GPL(kvm_task_switch);
3574
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003575int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3576 struct kvm_sregs *sregs)
3577{
3578 int mmu_reset_needed = 0;
3579 int i, pending_vec, max_bits;
3580 struct descriptor_table dt;
3581
3582 vcpu_load(vcpu);
3583
3584 dt.limit = sregs->idt.limit;
3585 dt.base = sregs->idt.base;
3586 kvm_x86_ops->set_idt(vcpu, &dt);
3587 dt.limit = sregs->gdt.limit;
3588 dt.base = sregs->gdt.base;
3589 kvm_x86_ops->set_gdt(vcpu, &dt);
3590
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003591 vcpu->arch.cr2 = sregs->cr2;
3592 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
3593 vcpu->arch.cr3 = sregs->cr3;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003594
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003595 kvm_set_cr8(vcpu, sregs->cr8);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003596
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003597 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003598 kvm_x86_ops->set_efer(vcpu, sregs->efer);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003599 kvm_set_apic_base(vcpu, sregs->apic_base);
3600
3601 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3602
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003603 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003604 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
Paul Knowlesd7306162008-02-06 11:02:35 +00003605 vcpu->arch.cr0 = sregs->cr0;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003606
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003607 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003608 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3609 if (!is_long_mode(vcpu) && is_pae(vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003610 load_pdptrs(vcpu, vcpu->arch.cr3);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003611
3612 if (mmu_reset_needed)
3613 kvm_mmu_reset_context(vcpu);
3614
3615 if (!irqchip_in_kernel(vcpu->kvm)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003616 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3617 sizeof vcpu->arch.irq_pending);
3618 vcpu->arch.irq_summary = 0;
3619 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3620 if (vcpu->arch.irq_pending[i])
3621 __set_bit(i, &vcpu->arch.irq_summary);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003622 } else {
3623 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3624 pending_vec = find_first_bit(
3625 (const unsigned long *)sregs->interrupt_bitmap,
3626 max_bits);
3627 /* Only pending external irq is handled here */
3628 if (pending_vec < max_bits) {
3629 kvm_x86_ops->set_irq(vcpu, pending_vec);
3630 pr_debug("Set back pending irq %d\n",
3631 pending_vec);
3632 }
3633 }
3634
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003635 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3636 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3637 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3638 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3639 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3640 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003641
Guillaume Thouvenin3e6e0aa2008-05-27 10:18:46 +02003642 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3643 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
Hollis Blanchardb6c7a5d2007-11-01 14:16:10 -05003644
3645 vcpu_put(vcpu);
3646
3647 return 0;
3648}
3649
3650int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3651 struct kvm_debug_guest *dbg)
3652{
3653 int r;
3654
3655 vcpu_load(vcpu);
3656
3657 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3658
3659 vcpu_put(vcpu);
3660
3661 return r;
3662}
3663
3664/*
Hollis Blanchardd0752062007-10-31 17:24:25 -05003665 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3666 * we have asm/x86/processor.h
3667 */
3668struct fxsave {
3669 u16 cwd;
3670 u16 swd;
3671 u16 twd;
3672 u16 fop;
3673 u64 rip;
3674 u64 rdp;
3675 u32 mxcsr;
3676 u32 mxcsr_mask;
3677 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3678#ifdef CONFIG_X86_64
3679 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3680#else
3681 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3682#endif
3683};
3684
Zhang Xiantao8b006792007-11-16 13:05:55 +08003685/*
3686 * Translate a guest virtual address to a guest physical address.
3687 */
3688int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3689 struct kvm_translation *tr)
3690{
3691 unsigned long vaddr = tr->linear_address;
3692 gpa_t gpa;
3693
3694 vcpu_load(vcpu);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003695 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003696 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003697 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantao8b006792007-11-16 13:05:55 +08003698 tr->physical_address = gpa;
3699 tr->valid = gpa != UNMAPPED_GVA;
3700 tr->writeable = 1;
3701 tr->usermode = 0;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003702 vcpu_put(vcpu);
3703
3704 return 0;
3705}
3706
Hollis Blanchardd0752062007-10-31 17:24:25 -05003707int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3708{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003709 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003710
3711 vcpu_load(vcpu);
3712
3713 memcpy(fpu->fpr, fxsave->st_space, 128);
3714 fpu->fcw = fxsave->cwd;
3715 fpu->fsw = fxsave->swd;
3716 fpu->ftwx = fxsave->twd;
3717 fpu->last_opcode = fxsave->fop;
3718 fpu->last_ip = fxsave->rip;
3719 fpu->last_dp = fxsave->rdp;
3720 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3721
3722 vcpu_put(vcpu);
3723
3724 return 0;
3725}
3726
3727int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3728{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003729 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003730
3731 vcpu_load(vcpu);
3732
3733 memcpy(fxsave->st_space, fpu->fpr, 128);
3734 fxsave->cwd = fpu->fcw;
3735 fxsave->swd = fpu->fsw;
3736 fxsave->twd = fpu->ftwx;
3737 fxsave->fop = fpu->last_opcode;
3738 fxsave->rip = fpu->last_ip;
3739 fxsave->rdp = fpu->last_dp;
3740 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3741
3742 vcpu_put(vcpu);
3743
3744 return 0;
3745}
3746
3747void fx_init(struct kvm_vcpu *vcpu)
3748{
3749 unsigned after_mxcsr_mask;
3750
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02003751 /*
3752 * Touch the fpu the first time in non atomic context as if
3753 * this is the first fpu instruction the exception handler
3754 * will fire before the instruction returns and it'll have to
3755 * allocate ram with GFP_KERNEL.
3756 */
3757 if (!used_math())
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003758 kvm_fx_save(&vcpu->arch.host_fx_image);
Andrea Arcangelibc1a34f2008-05-01 18:43:33 +02003759
Hollis Blanchardd0752062007-10-31 17:24:25 -05003760 /* Initialize guest FPU by resetting ours and saving into guest's */
3761 preempt_disable();
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003762 kvm_fx_save(&vcpu->arch.host_fx_image);
3763 kvm_fx_finit();
3764 kvm_fx_save(&vcpu->arch.guest_fx_image);
3765 kvm_fx_restore(&vcpu->arch.host_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003766 preempt_enable();
3767
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003768 vcpu->arch.cr0 |= X86_CR0_ET;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003769 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003770 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3771 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
Hollis Blanchardd0752062007-10-31 17:24:25 -05003772 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3773}
3774EXPORT_SYMBOL_GPL(fx_init);
3775
3776void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3777{
3778 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3779 return;
3780
3781 vcpu->guest_fpu_loaded = 1;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003782 kvm_fx_save(&vcpu->arch.host_fx_image);
3783 kvm_fx_restore(&vcpu->arch.guest_fx_image);
Hollis Blanchardd0752062007-10-31 17:24:25 -05003784}
3785EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3786
3787void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3788{
3789 if (!vcpu->guest_fpu_loaded)
3790 return;
3791
3792 vcpu->guest_fpu_loaded = 0;
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003793 kvm_fx_save(&vcpu->arch.guest_fx_image);
3794 kvm_fx_restore(&vcpu->arch.host_fx_image);
Avi Kivityf096ed82007-11-18 13:54:33 +02003795 ++vcpu->stat.fpu_reload;
Hollis Blanchardd0752062007-10-31 17:24:25 -05003796}
3797EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003798
3799void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3800{
3801 kvm_x86_ops->vcpu_free(vcpu);
3802}
3803
3804struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3805 unsigned int id)
3806{
Avi Kivity26e52152007-11-20 15:30:24 +02003807 return kvm_x86_ops->vcpu_create(kvm, id);
3808}
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003809
Avi Kivity26e52152007-11-20 15:30:24 +02003810int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3811{
3812 int r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003813
3814 /* We do fxsave: this must be aligned. */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003815 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003816
3817 vcpu_load(vcpu);
3818 r = kvm_arch_vcpu_reset(vcpu);
3819 if (r == 0)
3820 r = kvm_mmu_setup(vcpu);
3821 vcpu_put(vcpu);
3822 if (r < 0)
3823 goto free_vcpu;
3824
Avi Kivity26e52152007-11-20 15:30:24 +02003825 return 0;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003826free_vcpu:
3827 kvm_x86_ops->vcpu_free(vcpu);
Avi Kivity26e52152007-11-20 15:30:24 +02003828 return r;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003829}
3830
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003831void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003832{
3833 vcpu_load(vcpu);
3834 kvm_mmu_unload(vcpu);
3835 vcpu_put(vcpu);
3836
3837 kvm_x86_ops->vcpu_free(vcpu);
3838}
3839
3840int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3841{
3842 return kvm_x86_ops->vcpu_reset(vcpu);
3843}
3844
3845void kvm_arch_hardware_enable(void *garbage)
3846{
3847 kvm_x86_ops->hardware_enable(garbage);
3848}
3849
3850void kvm_arch_hardware_disable(void *garbage)
3851{
3852 kvm_x86_ops->hardware_disable(garbage);
3853}
3854
3855int kvm_arch_hardware_setup(void)
3856{
3857 return kvm_x86_ops->hardware_setup();
3858}
3859
3860void kvm_arch_hardware_unsetup(void)
3861{
3862 kvm_x86_ops->hardware_unsetup();
3863}
3864
3865void kvm_arch_check_processor_compat(void *rtn)
3866{
3867 kvm_x86_ops->check_processor_compatibility(rtn);
3868}
3869
3870int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3871{
3872 struct page *page;
3873 struct kvm *kvm;
3874 int r;
3875
3876 BUG_ON(vcpu->kvm == NULL);
3877 kvm = vcpu->kvm;
3878
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003879 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003880 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
Avi Kivitya4535292008-04-13 17:54:35 +03003881 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003882 else
Avi Kivitya4535292008-04-13 17:54:35 +03003883 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003884
3885 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3886 if (!page) {
3887 r = -ENOMEM;
3888 goto fail;
3889 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003890 vcpu->arch.pio_data = page_address(page);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003891
3892 r = kvm_mmu_create(vcpu);
3893 if (r < 0)
3894 goto fail_free_pio_data;
3895
3896 if (irqchip_in_kernel(kvm)) {
3897 r = kvm_create_lapic(vcpu);
3898 if (r < 0)
3899 goto fail_mmu_destroy;
3900 }
3901
3902 return 0;
3903
3904fail_mmu_destroy:
3905 kvm_mmu_destroy(vcpu);
3906fail_free_pio_data:
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003907 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003908fail:
3909 return r;
3910}
3911
3912void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3913{
3914 kvm_free_lapic(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003915 down_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003916 kvm_mmu_destroy(vcpu);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003917 up_read(&vcpu->kvm->slots_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003918 free_page((unsigned long)vcpu->arch.pio_data);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08003919}
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003920
3921struct kvm *kvm_arch_create_vm(void)
3922{
3923 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3924
3925 if (!kvm)
3926 return ERR_PTR(-ENOMEM);
3927
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003928 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003929
3930 return kvm;
3931}
3932
3933static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3934{
3935 vcpu_load(vcpu);
3936 kvm_mmu_unload(vcpu);
3937 vcpu_put(vcpu);
3938}
3939
3940static void kvm_free_vcpus(struct kvm *kvm)
3941{
3942 unsigned int i;
3943
3944 /*
3945 * Unpin any mmu pages first.
3946 */
3947 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3948 if (kvm->vcpus[i])
3949 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3950 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3951 if (kvm->vcpus[i]) {
3952 kvm_arch_vcpu_free(kvm->vcpus[i]);
3953 kvm->vcpus[i] = NULL;
3954 }
3955 }
3956
3957}
3958
3959void kvm_arch_destroy_vm(struct kvm *kvm)
3960{
Sheng Yang78376992008-01-28 05:10:22 +08003961 kvm_free_pit(kvm);
Zhang Xiantaod7deeeb02007-12-14 10:17:34 +08003962 kfree(kvm->arch.vpic);
3963 kfree(kvm->arch.vioapic);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003964 kvm_free_vcpus(kvm);
3965 kvm_free_physmem(kvm);
Avi Kivity3d458302008-03-25 11:26:13 +02003966 if (kvm->arch.apic_access_page)
3967 put_page(kvm->arch.apic_access_page);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003968 if (kvm->arch.ept_identity_pagetable)
3969 put_page(kvm->arch.ept_identity_pagetable);
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +08003970 kfree(kvm);
3971}
Zhang Xiantao0de10342007-11-20 16:25:04 +08003972
3973int kvm_arch_set_memory_region(struct kvm *kvm,
3974 struct kvm_userspace_memory_region *mem,
3975 struct kvm_memory_slot old,
3976 int user_alloc)
3977{
3978 int npages = mem->memory_size >> PAGE_SHIFT;
3979 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3980
3981 /*To keep backward compatibility with older userspace,
3982 *x86 needs to hanlde !user_alloc case.
3983 */
3984 if (!user_alloc) {
3985 if (npages && !old.rmap) {
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02003986 unsigned long userspace_addr;
3987
Izik Eidus72dc67a2008-02-10 18:04:15 +02003988 down_write(&current->mm->mmap_sem);
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02003989 userspace_addr = do_mmap(NULL, 0,
3990 npages * PAGE_SIZE,
3991 PROT_READ | PROT_WRITE,
3992 MAP_SHARED | MAP_ANONYMOUS,
3993 0);
Izik Eidus72dc67a2008-02-10 18:04:15 +02003994 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08003995
Andrea Arcangeli604b38a2008-07-25 16:32:03 +02003996 if (IS_ERR((void *)userspace_addr))
3997 return PTR_ERR((void *)userspace_addr);
3998
3999 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4000 spin_lock(&kvm->mmu_lock);
4001 memslot->userspace_addr = userspace_addr;
4002 spin_unlock(&kvm->mmu_lock);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004003 } else {
4004 if (!old.user_alloc && old.rmap) {
4005 int ret;
4006
Izik Eidus72dc67a2008-02-10 18:04:15 +02004007 down_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004008 ret = do_munmap(current->mm, old.userspace_addr,
4009 old.npages * PAGE_SIZE);
Izik Eidus72dc67a2008-02-10 18:04:15 +02004010 up_write(&current->mm->mmap_sem);
Zhang Xiantao0de10342007-11-20 16:25:04 +08004011 if (ret < 0)
4012 printk(KERN_WARNING
4013 "kvm_vm_ioctl_set_memory_region: "
4014 "failed to munmap memory\n");
4015 }
4016 }
4017 }
4018
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08004019 if (!kvm->arch.n_requested_mmu_pages) {
Zhang Xiantao0de10342007-11-20 16:25:04 +08004020 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4021 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4022 }
4023
4024 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
4025 kvm_flush_remote_tlbs(kvm);
4026
4027 return 0;
4028}
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004029
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03004030void kvm_arch_flush_shadow(struct kvm *kvm)
4031{
4032 kvm_mmu_zap_all(kvm);
4033}
4034
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004035int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4036{
Avi Kivitya4535292008-04-13 17:54:35 +03004037 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
4038 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
Zhang Xiantao1d737c82007-12-14 09:35:10 +08004039}
Zhang Xiantao57361992007-12-17 14:21:40 +08004040
4041static void vcpu_kick_intr(void *info)
4042{
4043#ifdef DEBUG
4044 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
4045 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
4046#endif
4047}
4048
4049void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4050{
4051 int ipi_pcpu = vcpu->cpu;
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004052 int cpu = get_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004053
4054 if (waitqueue_active(&vcpu->wq)) {
4055 wake_up_interruptible(&vcpu->wq);
4056 ++vcpu->stat.halt_wakeup;
4057 }
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004058 /*
4059 * We may be called synchronously with irqs disabled in guest mode,
4060 * So need not to call smp_call_function_single() in that case.
4061 */
4062 if (vcpu->guest_mode && vcpu->cpu != cpu)
Jens Axboe8691e5a2008-06-06 11:18:06 +02004063 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Marcelo Tosattie9571ed2008-04-11 15:01:22 -03004064 put_cpu();
Zhang Xiantao57361992007-12-17 14:21:40 +08004065}