Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * derived from drivers/kvm/kvm_main.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 13 | * Amit Shah <amit.shah@qumranet.com> |
| 14 | * Ben-Ami Yassour <benami@il.ibm.com> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 15 | * |
| 16 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 17 | * the COPYING file in the top-level directory. |
| 18 | * |
| 19 | */ |
| 20 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 21 | #include <linux/kvm_host.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 22 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 23 | #include "mmu.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 24 | #include "i8254.h" |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 25 | #include "tss.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 26 | #include "kvm_cache_regs.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 27 | #include "x86.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 28 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 29 | #include <linux/clocksource.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 31 | #include <linux/kvm.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/vmalloc.h> |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 34 | #include <linux/module.h> |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 35 | #include <linux/mman.h> |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 36 | #include <linux/highmem.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 37 | #include <linux/intel-iommu.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 38 | |
| 39 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 40 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 41 | #include <asm/desc.h> |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 42 | #include <asm/mtrr.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 43 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 44 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 45 | #define CR0_RESERVED_BITS \ |
| 46 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 47 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 48 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 49 | #define CR4_RESERVED_BITS \ |
| 50 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 51 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 52 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 53 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 54 | |
| 55 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 56 | /* EFER defaults: |
| 57 | * - enable syscall per default because its emulated by KVM |
| 58 | * - enable LME and LMA per default on 64 bit KVM |
| 59 | */ |
| 60 | #ifdef CONFIG_X86_64 |
| 61 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 62 | #else |
| 63 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 64 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 65 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 66 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 67 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 68 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 69 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 70 | struct kvm_cpuid_entry2 __user *entries); |
| 71 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 72 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 74 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 75 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 76 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 77 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 78 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 79 | { "invlpg", VCPU_STAT(invlpg) }, |
| 80 | { "exits", VCPU_STAT(exits) }, |
| 81 | { "io_exits", VCPU_STAT(io_exits) }, |
| 82 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 83 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 84 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 85 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 86 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 87 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 88 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 89 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 90 | { "request_nmi", VCPU_STAT(request_nmi_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 91 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 92 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 93 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 94 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 95 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 96 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 97 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 98 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 99 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 100 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 101 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 102 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 103 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 104 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 105 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 106 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 107 | { "mmu_unsync_global", VM_STAT(mmu_unsync_global) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 108 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 109 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 110 | { NULL } |
| 111 | }; |
| 112 | |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 113 | unsigned long segment_base(u16 selector) |
| 114 | { |
| 115 | struct descriptor_table gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 116 | struct desc_struct *d; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 117 | unsigned long table_base; |
| 118 | unsigned long v; |
| 119 | |
| 120 | if (selector == 0) |
| 121 | return 0; |
| 122 | |
| 123 | asm("sgdt %0" : "=m"(gdt)); |
| 124 | table_base = gdt.base; |
| 125 | |
| 126 | if (selector & 4) { /* from ldt */ |
| 127 | u16 ldt_selector; |
| 128 | |
| 129 | asm("sldt %0" : "=g"(ldt_selector)); |
| 130 | table_base = segment_base(ldt_selector); |
| 131 | } |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 132 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 133 | v = d->base0 | ((unsigned long)d->base1 << 16) | |
| 134 | ((unsigned long)d->base2 << 24); |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 135 | #ifdef CONFIG_X86_64 |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 136 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 137 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 138 | #endif |
| 139 | return v; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(segment_base); |
| 142 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 143 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 144 | { |
| 145 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 146 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 147 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 148 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 149 | } |
| 150 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 151 | |
| 152 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 153 | { |
| 154 | /* TODO: reserve bits check */ |
| 155 | if (irqchip_in_kernel(vcpu->kvm)) |
| 156 | kvm_lapic_set_base(vcpu, data); |
| 157 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 158 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 161 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 162 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 163 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 164 | WARN_ON(vcpu->arch.exception.pending); |
| 165 | vcpu->arch.exception.pending = true; |
| 166 | vcpu->arch.exception.has_error_code = false; |
| 167 | vcpu->arch.exception.nr = nr; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 168 | } |
| 169 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 170 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 171 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 172 | u32 error_code) |
| 173 | { |
| 174 | ++vcpu->stat.pf_guest; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 175 | if (vcpu->arch.exception.pending) { |
| 176 | if (vcpu->arch.exception.nr == PF_VECTOR) { |
| 177 | printk(KERN_DEBUG "kvm: inject_page_fault:" |
| 178 | " double fault 0x%lx\n", addr); |
| 179 | vcpu->arch.exception.nr = DF_VECTOR; |
| 180 | vcpu->arch.exception.error_code = 0; |
| 181 | } else if (vcpu->arch.exception.nr == DF_VECTOR) { |
| 182 | /* triple fault -> shutdown */ |
| 183 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 184 | } |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 185 | return; |
| 186 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 187 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 188 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 189 | } |
| 190 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 191 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 192 | { |
| 193 | vcpu->arch.nmi_pending = 1; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 196 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 197 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 198 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 199 | WARN_ON(vcpu->arch.exception.pending); |
| 200 | vcpu->arch.exception.pending = true; |
| 201 | vcpu->arch.exception.has_error_code = true; |
| 202 | vcpu->arch.exception.nr = nr; |
| 203 | vcpu->arch.exception.error_code = error_code; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 204 | } |
| 205 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 206 | |
| 207 | static void __queue_exception(struct kvm_vcpu *vcpu) |
| 208 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 209 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 210 | vcpu->arch.exception.has_error_code, |
| 211 | vcpu->arch.exception.error_code); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 214 | /* |
| 215 | * Load the pae pdptrs. Return true is they are all valid. |
| 216 | */ |
| 217 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 218 | { |
| 219 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 220 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 221 | int i; |
| 222 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 223 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 224 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 225 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 226 | offset * sizeof(u64), sizeof(pdpte)); |
| 227 | if (ret < 0) { |
| 228 | ret = 0; |
| 229 | goto out; |
| 230 | } |
| 231 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
| 232 | if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) { |
| 233 | ret = 0; |
| 234 | goto out; |
| 235 | } |
| 236 | } |
| 237 | ret = 1; |
| 238 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 239 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 240 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 241 | |
| 242 | return ret; |
| 243 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 245 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 246 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 247 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 248 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 249 | bool changed = true; |
| 250 | int r; |
| 251 | |
| 252 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 253 | return false; |
| 254 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 255 | r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte)); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 256 | if (r < 0) |
| 257 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 258 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 259 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 260 | |
| 261 | return changed; |
| 262 | } |
| 263 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 264 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 265 | { |
| 266 | if (cr0 & CR0_RESERVED_BITS) { |
| 267 | printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n", |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 268 | cr0, vcpu->arch.cr0); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 269 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
| 273 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
| 274 | printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 275 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
| 279 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
| 280 | printk(KERN_DEBUG "set_cr0: #GP, set PG flag " |
| 281 | "and a clear PE flag\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 282 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 287 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 288 | if ((vcpu->arch.shadow_efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 289 | int cs_db, cs_l; |
| 290 | |
| 291 | if (!is_pae(vcpu)) { |
| 292 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 293 | "in long mode while PAE is disabled\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 294 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 298 | if (cs_l) { |
| 299 | printk(KERN_DEBUG "set_cr0: #GP, start paging " |
| 300 | "in long mode while CS.L == 1\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 301 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 302 | return; |
| 303 | |
| 304 | } |
| 305 | } else |
| 306 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 307 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 308 | printk(KERN_DEBUG "set_cr0: #GP, pdptrs " |
| 309 | "reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 310 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 317 | vcpu->arch.cr0 = cr0; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 318 | |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 319 | kvm_mmu_sync_global(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 320 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 321 | return; |
| 322 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 324 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 325 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 326 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 327 | kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 328 | KVMTRACE_1D(LMSW, vcpu, |
| 329 | (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)), |
| 330 | handler); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 331 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 333 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 334 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 335 | { |
| 336 | if (cr4 & CR4_RESERVED_BITS) { |
| 337 | printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 338 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | |
| 342 | if (is_long_mode(vcpu)) { |
| 343 | if (!(cr4 & X86_CR4_PAE)) { |
| 344 | printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while " |
| 345 | "in long mode\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 346 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 350 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 351 | printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 352 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | if (cr4 & X86_CR4_VMXE) { |
| 357 | printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 358 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 362 | vcpu->arch.cr4 = cr4; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 363 | kvm_mmu_sync_global(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 364 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 365 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 366 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 367 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 368 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 369 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 370 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 371 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 372 | kvm_mmu_flush_tlb(vcpu); |
| 373 | return; |
| 374 | } |
| 375 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 376 | if (is_long_mode(vcpu)) { |
| 377 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
| 378 | printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 379 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | } else { |
| 383 | if (is_pae(vcpu)) { |
| 384 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
| 385 | printk(KERN_DEBUG |
| 386 | "set_cr3: #GP, reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 387 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
| 391 | printk(KERN_DEBUG "set_cr3: #GP, pdptrs " |
| 392 | "reserved bits\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 393 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | } |
| 397 | /* |
| 398 | * We don't check reserved bits in nonpae mode, because |
| 399 | * this isn't enforced, and VMware depends on this. |
| 400 | */ |
| 401 | } |
| 402 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Does the new cr3 value map to physical memory? (Note, we |
| 405 | * catch an invalid cr3 even in real-mode, because it would |
| 406 | * cause trouble later on when we turn on paging anyway.) |
| 407 | * |
| 408 | * A real CPU would silently accept an invalid cr3 and would |
| 409 | * attempt to use it - with largely undefined (and often hard |
| 410 | * to debug) behavior on the guest side. |
| 411 | */ |
| 412 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 413 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 414 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 415 | vcpu->arch.cr3 = cr3; |
| 416 | vcpu->arch.mmu.new_cr3(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 417 | } |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 418 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 419 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 420 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 421 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 422 | { |
| 423 | if (cr8 & CR8_RESERVED_BITS) { |
| 424 | printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 425 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | if (irqchip_in_kernel(vcpu->kvm)) |
| 429 | kvm_lapic_set_tpr(vcpu, cr8); |
| 430 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 431 | vcpu->arch.cr8 = cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 432 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 433 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 434 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 435 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 436 | { |
| 437 | if (irqchip_in_kernel(vcpu->kvm)) |
| 438 | return kvm_lapic_get_cr8(vcpu); |
| 439 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 440 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 441 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 443 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 444 | /* |
| 445 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 446 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 447 | * |
| 448 | * This list is modified at module load time to reflect the |
| 449 | * capabilities of the host cpu. |
| 450 | */ |
| 451 | static u32 msrs_to_save[] = { |
| 452 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 453 | MSR_K6_STAR, |
| 454 | #ifdef CONFIG_X86_64 |
| 455 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 456 | #endif |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 457 | MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 458 | MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | static unsigned num_msrs_to_save; |
| 462 | |
| 463 | static u32 emulated_msrs[] = { |
| 464 | MSR_IA32_MISC_ENABLE, |
| 465 | }; |
| 466 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 467 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 468 | { |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 469 | if (efer & efer_reserved_bits) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 470 | printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n", |
| 471 | efer); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 472 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
| 476 | if (is_paging(vcpu) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 477 | && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 478 | printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 479 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | kvm_x86_ops->set_efer(vcpu, efer); |
| 484 | |
| 485 | efer &= ~EFER_LMA; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 486 | efer |= vcpu->arch.shadow_efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 487 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 488 | vcpu->arch.shadow_efer = efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 491 | void kvm_enable_efer_bits(u64 mask) |
| 492 | { |
| 493 | efer_reserved_bits &= ~mask; |
| 494 | } |
| 495 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 496 | |
| 497 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 498 | /* |
| 499 | * Writes msr value into into the appropriate "register". |
| 500 | * Returns 0 on success, non-0 otherwise. |
| 501 | * Assumes vcpu_load() was already called. |
| 502 | */ |
| 503 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 504 | { |
| 505 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 506 | } |
| 507 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 508 | /* |
| 509 | * Adapt set_msr() to msr_io()'s calling convention |
| 510 | */ |
| 511 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 512 | { |
| 513 | return kvm_set_msr(vcpu, index, *data); |
| 514 | } |
| 515 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 516 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 517 | { |
| 518 | static int version; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 519 | struct pvclock_wall_clock wc; |
| 520 | struct timespec now, sys, boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 521 | |
| 522 | if (!wall_clock) |
| 523 | return; |
| 524 | |
| 525 | version++; |
| 526 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 527 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 528 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 529 | /* |
| 530 | * The guest calculates current wall clock time by adding |
| 531 | * system time (updated by kvm_write_guest_time below) to the |
| 532 | * wall clock specified here. guest system time equals host |
| 533 | * system time for us, thus we must fill in host boot time here. |
| 534 | */ |
| 535 | now = current_kernel_time(); |
| 536 | ktime_get_ts(&sys); |
| 537 | boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys)); |
| 538 | |
| 539 | wc.sec = boot.tv_sec; |
| 540 | wc.nsec = boot.tv_nsec; |
| 541 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 542 | |
| 543 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 544 | |
| 545 | version++; |
| 546 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 547 | } |
| 548 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 549 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 550 | { |
| 551 | uint32_t quotient, remainder; |
| 552 | |
| 553 | /* Don't try to replace with do_div(), this one calculates |
| 554 | * "(dividend << 32) / divisor" */ |
| 555 | __asm__ ( "divl %4" |
| 556 | : "=a" (quotient), "=d" (remainder) |
| 557 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 558 | return quotient; |
| 559 | } |
| 560 | |
| 561 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 562 | { |
| 563 | uint64_t nsecs = 1000000000LL; |
| 564 | int32_t shift = 0; |
| 565 | uint64_t tps64; |
| 566 | uint32_t tps32; |
| 567 | |
| 568 | tps64 = tsc_khz * 1000LL; |
| 569 | while (tps64 > nsecs*2) { |
| 570 | tps64 >>= 1; |
| 571 | shift--; |
| 572 | } |
| 573 | |
| 574 | tps32 = (uint32_t)tps64; |
| 575 | while (tps32 <= (uint32_t)nsecs) { |
| 576 | tps32 <<= 1; |
| 577 | shift++; |
| 578 | } |
| 579 | |
| 580 | hv_clock->tsc_shift = shift; |
| 581 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 582 | |
| 583 | pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n", |
Harvey Harrison | 80a914d | 2008-10-15 22:01:25 -0700 | [diff] [blame] | 584 | __func__, tsc_khz, hv_clock->tsc_shift, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 585 | hv_clock->tsc_to_system_mul); |
| 586 | } |
| 587 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 588 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
| 589 | { |
| 590 | struct timespec ts; |
| 591 | unsigned long flags; |
| 592 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 593 | void *shared_kaddr; |
| 594 | |
| 595 | if ((!vcpu->time_page)) |
| 596 | return; |
| 597 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 598 | if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) { |
| 599 | kvm_set_time_scale(tsc_khz, &vcpu->hv_clock); |
| 600 | vcpu->hv_clock_tsc_khz = tsc_khz; |
| 601 | } |
| 602 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 603 | /* Keep irq disabled to prevent changes to the clock */ |
| 604 | local_irq_save(flags); |
| 605 | kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER, |
| 606 | &vcpu->hv_clock.tsc_timestamp); |
| 607 | ktime_get_ts(&ts); |
| 608 | local_irq_restore(flags); |
| 609 | |
| 610 | /* With all the info we got, fill in the values */ |
| 611 | |
| 612 | vcpu->hv_clock.system_time = ts.tv_nsec + |
| 613 | (NSEC_PER_SEC * (u64)ts.tv_sec); |
| 614 | /* |
| 615 | * The interface expects us to write an even number signaling that the |
| 616 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 617 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 618 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 619 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 620 | |
| 621 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 622 | |
| 623 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 624 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 625 | |
| 626 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 627 | |
| 628 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
| 629 | } |
| 630 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 631 | static bool msr_mtrr_valid(unsigned msr) |
| 632 | { |
| 633 | switch (msr) { |
| 634 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 635 | case MSR_MTRRfix64K_00000: |
| 636 | case MSR_MTRRfix16K_80000: |
| 637 | case MSR_MTRRfix16K_A0000: |
| 638 | case MSR_MTRRfix4K_C0000: |
| 639 | case MSR_MTRRfix4K_C8000: |
| 640 | case MSR_MTRRfix4K_D0000: |
| 641 | case MSR_MTRRfix4K_D8000: |
| 642 | case MSR_MTRRfix4K_E0000: |
| 643 | case MSR_MTRRfix4K_E8000: |
| 644 | case MSR_MTRRfix4K_F0000: |
| 645 | case MSR_MTRRfix4K_F8000: |
| 646 | case MSR_MTRRdefType: |
| 647 | case MSR_IA32_CR_PAT: |
| 648 | return true; |
| 649 | case 0x2f8: |
| 650 | return true; |
| 651 | } |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 656 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 657 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 658 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 659 | if (!msr_mtrr_valid(msr)) |
| 660 | return 1; |
| 661 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 662 | if (msr == MSR_MTRRdefType) { |
| 663 | vcpu->arch.mtrr_state.def_type = data; |
| 664 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; |
| 665 | } else if (msr == MSR_MTRRfix64K_00000) |
| 666 | p[0] = data; |
| 667 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 668 | p[1 + msr - MSR_MTRRfix16K_80000] = data; |
| 669 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 670 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; |
| 671 | else if (msr == MSR_IA32_CR_PAT) |
| 672 | vcpu->arch.pat = data; |
| 673 | else { /* Variable MTRRs */ |
| 674 | int idx, is_mtrr_mask; |
| 675 | u64 *pt; |
| 676 | |
| 677 | idx = (msr - 0x200) / 2; |
| 678 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 679 | if (!is_mtrr_mask) |
| 680 | pt = |
| 681 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 682 | else |
| 683 | pt = |
| 684 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 685 | *pt = data; |
| 686 | } |
| 687 | |
| 688 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 689 | return 0; |
| 690 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 691 | |
| 692 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 693 | { |
| 694 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 695 | case MSR_EFER: |
| 696 | set_efer(vcpu, data); |
| 697 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 698 | case MSR_IA32_MC0_STATUS: |
| 699 | pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 700 | __func__, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 701 | break; |
| 702 | case MSR_IA32_MCG_STATUS: |
| 703 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 704 | __func__, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 705 | break; |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 706 | case MSR_IA32_MCG_CTL: |
| 707 | pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 708 | __func__, data); |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 709 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 710 | case MSR_IA32_DEBUGCTLMSR: |
| 711 | if (!data) { |
| 712 | /* We support the non-activated case already */ |
| 713 | break; |
| 714 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 715 | /* Values other than LBR and BTF are vendor-specific, |
| 716 | thus reserved and should throw a #GP */ |
| 717 | return 1; |
| 718 | } |
| 719 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 720 | __func__, data); |
| 721 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 722 | case MSR_IA32_UCODE_REV: |
| 723 | case MSR_IA32_UCODE_WRITE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 724 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 725 | case 0x200 ... 0x2ff: |
| 726 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 727 | case MSR_IA32_APICBASE: |
| 728 | kvm_set_apic_base(vcpu, data); |
| 729 | break; |
| 730 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 731 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 732 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 733 | case MSR_KVM_WALL_CLOCK: |
| 734 | vcpu->kvm->arch.wall_clock = data; |
| 735 | kvm_write_wall_clock(vcpu->kvm, data); |
| 736 | break; |
| 737 | case MSR_KVM_SYSTEM_TIME: { |
| 738 | if (vcpu->arch.time_page) { |
| 739 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 740 | vcpu->arch.time_page = NULL; |
| 741 | } |
| 742 | |
| 743 | vcpu->arch.time = data; |
| 744 | |
| 745 | /* we verify if the enable bit is set... */ |
| 746 | if (!(data & 1)) |
| 747 | break; |
| 748 | |
| 749 | /* ...but clean it before doing the actual write */ |
| 750 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 751 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 752 | vcpu->arch.time_page = |
| 753 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 754 | |
| 755 | if (is_error_page(vcpu->arch.time_page)) { |
| 756 | kvm_release_page_clean(vcpu->arch.time_page); |
| 757 | vcpu->arch.time_page = NULL; |
| 758 | } |
| 759 | |
| 760 | kvm_write_guest_time(vcpu); |
| 761 | break; |
| 762 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 763 | default: |
Avi Kivity | 565f1fb | 2007-12-19 12:02:40 +0200 | [diff] [blame] | 764 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 765 | return 1; |
| 766 | } |
| 767 | return 0; |
| 768 | } |
| 769 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 770 | |
| 771 | |
| 772 | /* |
| 773 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 774 | * Returns 0 on success, non-0 otherwise. |
| 775 | * Assumes vcpu_load() was already called. |
| 776 | */ |
| 777 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 778 | { |
| 779 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 780 | } |
| 781 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 782 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 783 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 784 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 785 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 786 | if (!msr_mtrr_valid(msr)) |
| 787 | return 1; |
| 788 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 789 | if (msr == MSR_MTRRdefType) |
| 790 | *pdata = vcpu->arch.mtrr_state.def_type + |
| 791 | (vcpu->arch.mtrr_state.enabled << 10); |
| 792 | else if (msr == MSR_MTRRfix64K_00000) |
| 793 | *pdata = p[0]; |
| 794 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 795 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; |
| 796 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 797 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; |
| 798 | else if (msr == MSR_IA32_CR_PAT) |
| 799 | *pdata = vcpu->arch.pat; |
| 800 | else { /* Variable MTRRs */ |
| 801 | int idx, is_mtrr_mask; |
| 802 | u64 *pt; |
| 803 | |
| 804 | idx = (msr - 0x200) / 2; |
| 805 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 806 | if (!is_mtrr_mask) |
| 807 | pt = |
| 808 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 809 | else |
| 810 | pt = |
| 811 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 812 | *pdata = *pt; |
| 813 | } |
| 814 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 818 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 819 | { |
| 820 | u64 data; |
| 821 | |
| 822 | switch (msr) { |
| 823 | case 0xc0010010: /* SYSCFG */ |
| 824 | case 0xc0010015: /* HWCR */ |
| 825 | case MSR_IA32_PLATFORM_ID: |
| 826 | case MSR_IA32_P5_MC_ADDR: |
| 827 | case MSR_IA32_P5_MC_TYPE: |
| 828 | case MSR_IA32_MC0_CTL: |
| 829 | case MSR_IA32_MCG_STATUS: |
| 830 | case MSR_IA32_MCG_CAP: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 831 | case MSR_IA32_MCG_CTL: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 832 | case MSR_IA32_MC0_MISC: |
| 833 | case MSR_IA32_MC0_MISC+4: |
| 834 | case MSR_IA32_MC0_MISC+8: |
| 835 | case MSR_IA32_MC0_MISC+12: |
| 836 | case MSR_IA32_MC0_MISC+16: |
Joerg Roedel | a89c1ad | 2008-08-29 11:52:07 +0200 | [diff] [blame] | 837 | case MSR_IA32_MC0_MISC+20: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 838 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 839 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 840 | case MSR_IA32_DEBUGCTLMSR: |
| 841 | case MSR_IA32_LASTBRANCHFROMIP: |
| 842 | case MSR_IA32_LASTBRANCHTOIP: |
| 843 | case MSR_IA32_LASTINTFROMIP: |
| 844 | case MSR_IA32_LASTINTTOIP: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 845 | data = 0; |
| 846 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 847 | case MSR_MTRRcap: |
| 848 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 849 | break; |
| 850 | case 0x200 ... 0x2ff: |
| 851 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 852 | case 0xcd: /* fsb frequency */ |
| 853 | data = 3; |
| 854 | break; |
| 855 | case MSR_IA32_APICBASE: |
| 856 | data = kvm_get_apic_base(vcpu); |
| 857 | break; |
| 858 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 859 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 860 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 861 | case MSR_IA32_PERF_STATUS: |
| 862 | /* TSC increment by tick */ |
| 863 | data = 1000ULL; |
| 864 | /* CPU multiplier */ |
| 865 | data |= (((uint64_t)4ULL) << 40); |
| 866 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 867 | case MSR_EFER: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 868 | data = vcpu->arch.shadow_efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 869 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 870 | case MSR_KVM_WALL_CLOCK: |
| 871 | data = vcpu->kvm->arch.wall_clock; |
| 872 | break; |
| 873 | case MSR_KVM_SYSTEM_TIME: |
| 874 | data = vcpu->arch.time; |
| 875 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 876 | default: |
| 877 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 878 | return 1; |
| 879 | } |
| 880 | *pdata = data; |
| 881 | return 0; |
| 882 | } |
| 883 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 884 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 885 | /* |
| 886 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 887 | * |
| 888 | * @return number of msrs set successfully. |
| 889 | */ |
| 890 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 891 | struct kvm_msr_entry *entries, |
| 892 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 893 | unsigned index, u64 *data)) |
| 894 | { |
| 895 | int i; |
| 896 | |
| 897 | vcpu_load(vcpu); |
| 898 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 899 | down_read(&vcpu->kvm->slots_lock); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 900 | for (i = 0; i < msrs->nmsrs; ++i) |
| 901 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 902 | break; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 903 | up_read(&vcpu->kvm->slots_lock); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 904 | |
| 905 | vcpu_put(vcpu); |
| 906 | |
| 907 | return i; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 912 | * |
| 913 | * @return number of msrs set successfully. |
| 914 | */ |
| 915 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 916 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 917 | unsigned index, u64 *data), |
| 918 | int writeback) |
| 919 | { |
| 920 | struct kvm_msrs msrs; |
| 921 | struct kvm_msr_entry *entries; |
| 922 | int r, n; |
| 923 | unsigned size; |
| 924 | |
| 925 | r = -EFAULT; |
| 926 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 927 | goto out; |
| 928 | |
| 929 | r = -E2BIG; |
| 930 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 931 | goto out; |
| 932 | |
| 933 | r = -ENOMEM; |
| 934 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 935 | entries = vmalloc(size); |
| 936 | if (!entries) |
| 937 | goto out; |
| 938 | |
| 939 | r = -EFAULT; |
| 940 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 941 | goto out_free; |
| 942 | |
| 943 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 944 | if (r < 0) |
| 945 | goto out_free; |
| 946 | |
| 947 | r = -EFAULT; |
| 948 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 949 | goto out_free; |
| 950 | |
| 951 | r = n; |
| 952 | |
| 953 | out_free: |
| 954 | vfree(entries); |
| 955 | out: |
| 956 | return r; |
| 957 | } |
| 958 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 959 | int kvm_dev_ioctl_check_extension(long ext) |
| 960 | { |
| 961 | int r; |
| 962 | |
| 963 | switch (ext) { |
| 964 | case KVM_CAP_IRQCHIP: |
| 965 | case KVM_CAP_HLT: |
| 966 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 967 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 968 | case KVM_CAP_EXT_CPUID: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 969 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 970 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 971 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 972 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 973 | case KVM_CAP_SYNC_MMU: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 974 | r = 1; |
| 975 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 976 | case KVM_CAP_COALESCED_MMIO: |
| 977 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 978 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 979 | case KVM_CAP_VAPIC: |
| 980 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 981 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 982 | case KVM_CAP_NR_VCPUS: |
| 983 | r = KVM_MAX_VCPUS; |
| 984 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 985 | case KVM_CAP_NR_MEMSLOTS: |
| 986 | r = KVM_MEMORY_SLOTS; |
| 987 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 988 | case KVM_CAP_PV_MMU: |
| 989 | r = !tdp_enabled; |
| 990 | break; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 991 | case KVM_CAP_IOMMU: |
| 992 | r = intel_iommu_found(); |
| 993 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 994 | default: |
| 995 | r = 0; |
| 996 | break; |
| 997 | } |
| 998 | return r; |
| 999 | |
| 1000 | } |
| 1001 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1002 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1003 | unsigned int ioctl, unsigned long arg) |
| 1004 | { |
| 1005 | void __user *argp = (void __user *)arg; |
| 1006 | long r; |
| 1007 | |
| 1008 | switch (ioctl) { |
| 1009 | case KVM_GET_MSR_INDEX_LIST: { |
| 1010 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1011 | struct kvm_msr_list msr_list; |
| 1012 | unsigned n; |
| 1013 | |
| 1014 | r = -EFAULT; |
| 1015 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1016 | goto out; |
| 1017 | n = msr_list.nmsrs; |
| 1018 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1019 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1020 | goto out; |
| 1021 | r = -E2BIG; |
| 1022 | if (n < num_msrs_to_save) |
| 1023 | goto out; |
| 1024 | r = -EFAULT; |
| 1025 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1026 | num_msrs_to_save * sizeof(u32))) |
| 1027 | goto out; |
| 1028 | if (copy_to_user(user_msr_list->indices |
| 1029 | + num_msrs_to_save * sizeof(u32), |
| 1030 | &emulated_msrs, |
| 1031 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1032 | goto out; |
| 1033 | r = 0; |
| 1034 | break; |
| 1035 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1036 | case KVM_GET_SUPPORTED_CPUID: { |
| 1037 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1038 | struct kvm_cpuid2 cpuid; |
| 1039 | |
| 1040 | r = -EFAULT; |
| 1041 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1042 | goto out; |
| 1043 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
| 1044 | cpuid_arg->entries); |
| 1045 | if (r) |
| 1046 | goto out; |
| 1047 | |
| 1048 | r = -EFAULT; |
| 1049 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1050 | goto out; |
| 1051 | r = 0; |
| 1052 | break; |
| 1053 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1054 | default: |
| 1055 | r = -EINVAL; |
| 1056 | } |
| 1057 | out: |
| 1058 | return r; |
| 1059 | } |
| 1060 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1061 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1062 | { |
| 1063 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1064 | kvm_write_guest_time(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1068 | { |
| 1069 | kvm_x86_ops->vcpu_put(vcpu); |
Amit Shah | 9327fd1 | 2007-11-15 18:38:46 +0200 | [diff] [blame] | 1070 | kvm_put_guest_fpu(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1073 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1074 | { |
| 1075 | u64 efer; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1076 | |
| 1077 | rdmsrl(MSR_EFER, efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1078 | return efer & EFER_NX; |
| 1079 | } |
| 1080 | |
| 1081 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1082 | { |
| 1083 | int i; |
| 1084 | struct kvm_cpuid_entry2 *e, *entry; |
| 1085 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1086 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1087 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1088 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1089 | if (e->function == 0x80000001) { |
| 1090 | entry = e; |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1094 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1095 | entry->edx &= ~(1 << 20); |
| 1096 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1100 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1101 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1102 | struct kvm_cpuid *cpuid, |
| 1103 | struct kvm_cpuid_entry __user *entries) |
| 1104 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1105 | int r, i; |
| 1106 | struct kvm_cpuid_entry *cpuid_entries; |
| 1107 | |
| 1108 | r = -E2BIG; |
| 1109 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1110 | goto out; |
| 1111 | r = -ENOMEM; |
| 1112 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1113 | if (!cpuid_entries) |
| 1114 | goto out; |
| 1115 | r = -EFAULT; |
| 1116 | if (copy_from_user(cpuid_entries, entries, |
| 1117 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1118 | goto out_free; |
| 1119 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1120 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1121 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1122 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1123 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1124 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1125 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1126 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1127 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1128 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1129 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1130 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1131 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1132 | cpuid_fix_nx_cap(vcpu); |
| 1133 | r = 0; |
| 1134 | |
| 1135 | out_free: |
| 1136 | vfree(cpuid_entries); |
| 1137 | out: |
| 1138 | return r; |
| 1139 | } |
| 1140 | |
| 1141 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
| 1142 | struct kvm_cpuid2 *cpuid, |
| 1143 | struct kvm_cpuid_entry2 __user *entries) |
| 1144 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1145 | int r; |
| 1146 | |
| 1147 | r = -E2BIG; |
| 1148 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1149 | goto out; |
| 1150 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1151 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1152 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1153 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1154 | vcpu->arch.cpuid_nent = cpuid->nent; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1155 | return 0; |
| 1156 | |
| 1157 | out: |
| 1158 | return r; |
| 1159 | } |
| 1160 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1161 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
| 1162 | struct kvm_cpuid2 *cpuid, |
| 1163 | struct kvm_cpuid_entry2 __user *entries) |
| 1164 | { |
| 1165 | int r; |
| 1166 | |
| 1167 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1168 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1169 | goto out; |
| 1170 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1171 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
| 1172 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1173 | goto out; |
| 1174 | return 0; |
| 1175 | |
| 1176 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1177 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1178 | return r; |
| 1179 | } |
| 1180 | |
| 1181 | static inline u32 bit(int bitno) |
| 1182 | { |
| 1183 | return 1 << (bitno & 31); |
| 1184 | } |
| 1185 | |
| 1186 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1187 | u32 index) |
| 1188 | { |
| 1189 | entry->function = function; |
| 1190 | entry->index = index; |
| 1191 | cpuid_count(entry->function, entry->index, |
| 1192 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
| 1193 | entry->flags = 0; |
| 1194 | } |
| 1195 | |
| 1196 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1197 | u32 index, int *nent, int maxnent) |
| 1198 | { |
| 1199 | const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) | |
| 1200 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | |
| 1201 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | |
| 1202 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | |
| 1203 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | |
| 1204 | bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) | |
| 1205 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | |
| 1206 | bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) | |
| 1207 | bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) | |
| 1208 | bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP); |
| 1209 | const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) | |
| 1210 | bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) | |
| 1211 | bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) | |
| 1212 | bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) | |
| 1213 | bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) | |
| 1214 | bit(X86_FEATURE_PGE) | |
| 1215 | bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) | |
| 1216 | bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) | |
| 1217 | bit(X86_FEATURE_SYSCALL) | |
| 1218 | (bit(X86_FEATURE_NX) && is_efer_nx()) | |
| 1219 | #ifdef CONFIG_X86_64 |
| 1220 | bit(X86_FEATURE_LM) | |
| 1221 | #endif |
| 1222 | bit(X86_FEATURE_MMXEXT) | |
| 1223 | bit(X86_FEATURE_3DNOWEXT) | |
| 1224 | bit(X86_FEATURE_3DNOW); |
| 1225 | const u32 kvm_supported_word3_x86_features = |
| 1226 | bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16); |
| 1227 | const u32 kvm_supported_word6_x86_features = |
| 1228 | bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY); |
| 1229 | |
| 1230 | /* all func 2 cpuid_count() should be called on the same cpu */ |
| 1231 | get_cpu(); |
| 1232 | do_cpuid_1_ent(entry, function, index); |
| 1233 | ++*nent; |
| 1234 | |
| 1235 | switch (function) { |
| 1236 | case 0: |
| 1237 | entry->eax = min(entry->eax, (u32)0xb); |
| 1238 | break; |
| 1239 | case 1: |
| 1240 | entry->edx &= kvm_supported_word0_x86_features; |
| 1241 | entry->ecx &= kvm_supported_word3_x86_features; |
| 1242 | break; |
| 1243 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 1244 | * may return different values. This forces us to get_cpu() before |
| 1245 | * issuing the first command, and also to emulate this annoying behavior |
| 1246 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 1247 | case 2: { |
| 1248 | int t, times = entry->eax & 0xff; |
| 1249 | |
| 1250 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 1251 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1252 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 1253 | do_cpuid_1_ent(&entry[t], function, 0); |
| 1254 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1255 | ++*nent; |
| 1256 | } |
| 1257 | break; |
| 1258 | } |
| 1259 | /* function 4 and 0xb have additional index. */ |
| 1260 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1261 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1262 | |
| 1263 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1264 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1265 | for (i = 1; *nent < maxnent; ++i) { |
| 1266 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1267 | if (!cache_type) |
| 1268 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1269 | do_cpuid_1_ent(&entry[i], function, i); |
| 1270 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1271 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1272 | ++*nent; |
| 1273 | } |
| 1274 | break; |
| 1275 | } |
| 1276 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1277 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1278 | |
| 1279 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1280 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1281 | for (i = 1; *nent < maxnent; ++i) { |
Nitin A Kamble | 0853d2c | 2008-11-05 15:37:36 -0800 | [diff] [blame] | 1282 | level_type = entry[i - 1].ecx & 0xff00; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1283 | if (!level_type) |
| 1284 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1285 | do_cpuid_1_ent(&entry[i], function, i); |
| 1286 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1287 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1288 | ++*nent; |
| 1289 | } |
| 1290 | break; |
| 1291 | } |
| 1292 | case 0x80000000: |
| 1293 | entry->eax = min(entry->eax, 0x8000001a); |
| 1294 | break; |
| 1295 | case 0x80000001: |
| 1296 | entry->edx &= kvm_supported_word1_x86_features; |
| 1297 | entry->ecx &= kvm_supported_word6_x86_features; |
| 1298 | break; |
| 1299 | } |
| 1300 | put_cpu(); |
| 1301 | } |
| 1302 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1303 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1304 | struct kvm_cpuid_entry2 __user *entries) |
| 1305 | { |
| 1306 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 1307 | int limit, nent = 0, r = -E2BIG; |
| 1308 | u32 func; |
| 1309 | |
| 1310 | if (cpuid->nent < 1) |
| 1311 | goto out; |
| 1312 | r = -ENOMEM; |
| 1313 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 1314 | if (!cpuid_entries) |
| 1315 | goto out; |
| 1316 | |
| 1317 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 1318 | limit = cpuid_entries[0].eax; |
| 1319 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 1320 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
| 1321 | &nent, cpuid->nent); |
| 1322 | r = -E2BIG; |
| 1323 | if (nent >= cpuid->nent) |
| 1324 | goto out_free; |
| 1325 | |
| 1326 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 1327 | limit = cpuid_entries[nent - 1].eax; |
| 1328 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 1329 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
| 1330 | &nent, cpuid->nent); |
| 1331 | r = -EFAULT; |
| 1332 | if (copy_to_user(entries, cpuid_entries, |
| 1333 | nent * sizeof(struct kvm_cpuid_entry2))) |
| 1334 | goto out_free; |
| 1335 | cpuid->nent = nent; |
| 1336 | r = 0; |
| 1337 | |
| 1338 | out_free: |
| 1339 | vfree(cpuid_entries); |
| 1340 | out: |
| 1341 | return r; |
| 1342 | } |
| 1343 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1344 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 1345 | struct kvm_lapic_state *s) |
| 1346 | { |
| 1347 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1348 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1349 | vcpu_put(vcpu); |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 1355 | struct kvm_lapic_state *s) |
| 1356 | { |
| 1357 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1358 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1359 | kvm_apic_post_state_restore(vcpu); |
| 1360 | vcpu_put(vcpu); |
| 1361 | |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1365 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 1366 | struct kvm_interrupt *irq) |
| 1367 | { |
| 1368 | if (irq->irq < 0 || irq->irq >= 256) |
| 1369 | return -EINVAL; |
| 1370 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1371 | return -ENXIO; |
| 1372 | vcpu_load(vcpu); |
| 1373 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1374 | set_bit(irq->irq, vcpu->arch.irq_pending); |
| 1375 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1376 | |
| 1377 | vcpu_put(vcpu); |
| 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 1382 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 1383 | { |
| 1384 | vcpu_load(vcpu); |
| 1385 | kvm_inject_nmi(vcpu); |
| 1386 | vcpu_put(vcpu); |
| 1387 | |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1391 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 1392 | struct kvm_tpr_access_ctl *tac) |
| 1393 | { |
| 1394 | if (tac->flags) |
| 1395 | return -EINVAL; |
| 1396 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1400 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 1401 | unsigned int ioctl, unsigned long arg) |
| 1402 | { |
| 1403 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1404 | void __user *argp = (void __user *)arg; |
| 1405 | int r; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1406 | struct kvm_lapic_state *lapic = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1407 | |
| 1408 | switch (ioctl) { |
| 1409 | case KVM_GET_LAPIC: { |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1410 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1411 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1412 | r = -ENOMEM; |
| 1413 | if (!lapic) |
| 1414 | goto out; |
| 1415 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1416 | if (r) |
| 1417 | goto out; |
| 1418 | r = -EFAULT; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1419 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1420 | goto out; |
| 1421 | r = 0; |
| 1422 | break; |
| 1423 | } |
| 1424 | case KVM_SET_LAPIC: { |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1425 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
| 1426 | r = -ENOMEM; |
| 1427 | if (!lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1428 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1429 | r = -EFAULT; |
| 1430 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
| 1431 | goto out; |
| 1432 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1433 | if (r) |
| 1434 | goto out; |
| 1435 | r = 0; |
| 1436 | break; |
| 1437 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1438 | case KVM_INTERRUPT: { |
| 1439 | struct kvm_interrupt irq; |
| 1440 | |
| 1441 | r = -EFAULT; |
| 1442 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 1443 | goto out; |
| 1444 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 1445 | if (r) |
| 1446 | goto out; |
| 1447 | r = 0; |
| 1448 | break; |
| 1449 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 1450 | case KVM_NMI: { |
| 1451 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 1452 | if (r) |
| 1453 | goto out; |
| 1454 | r = 0; |
| 1455 | break; |
| 1456 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1457 | case KVM_SET_CPUID: { |
| 1458 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 1459 | struct kvm_cpuid cpuid; |
| 1460 | |
| 1461 | r = -EFAULT; |
| 1462 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1463 | goto out; |
| 1464 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 1465 | if (r) |
| 1466 | goto out; |
| 1467 | break; |
| 1468 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1469 | case KVM_SET_CPUID2: { |
| 1470 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1471 | struct kvm_cpuid2 cpuid; |
| 1472 | |
| 1473 | r = -EFAULT; |
| 1474 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1475 | goto out; |
| 1476 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
| 1477 | cpuid_arg->entries); |
| 1478 | if (r) |
| 1479 | goto out; |
| 1480 | break; |
| 1481 | } |
| 1482 | case KVM_GET_CPUID2: { |
| 1483 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1484 | struct kvm_cpuid2 cpuid; |
| 1485 | |
| 1486 | r = -EFAULT; |
| 1487 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1488 | goto out; |
| 1489 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
| 1490 | cpuid_arg->entries); |
| 1491 | if (r) |
| 1492 | goto out; |
| 1493 | r = -EFAULT; |
| 1494 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1495 | goto out; |
| 1496 | r = 0; |
| 1497 | break; |
| 1498 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1499 | case KVM_GET_MSRS: |
| 1500 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 1501 | break; |
| 1502 | case KVM_SET_MSRS: |
| 1503 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 1504 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1505 | case KVM_TPR_ACCESS_REPORTING: { |
| 1506 | struct kvm_tpr_access_ctl tac; |
| 1507 | |
| 1508 | r = -EFAULT; |
| 1509 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 1510 | goto out; |
| 1511 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 1512 | if (r) |
| 1513 | goto out; |
| 1514 | r = -EFAULT; |
| 1515 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 1516 | goto out; |
| 1517 | r = 0; |
| 1518 | break; |
| 1519 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 1520 | case KVM_SET_VAPIC_ADDR: { |
| 1521 | struct kvm_vapic_addr va; |
| 1522 | |
| 1523 | r = -EINVAL; |
| 1524 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1525 | goto out; |
| 1526 | r = -EFAULT; |
| 1527 | if (copy_from_user(&va, argp, sizeof va)) |
| 1528 | goto out; |
| 1529 | r = 0; |
| 1530 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 1531 | break; |
| 1532 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1533 | default: |
| 1534 | r = -EINVAL; |
| 1535 | } |
| 1536 | out: |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 1537 | if (lapic) |
| 1538 | kfree(lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1539 | return r; |
| 1540 | } |
| 1541 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1542 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 1543 | { |
| 1544 | int ret; |
| 1545 | |
| 1546 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 1547 | return -1; |
| 1548 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
| 1552 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 1553 | u32 kvm_nr_mmu_pages) |
| 1554 | { |
| 1555 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 1556 | return -EINVAL; |
| 1557 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1558 | down_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1559 | |
| 1560 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1561 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1562 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1563 | up_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 1568 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1569 | return kvm->arch.n_alloc_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1570 | } |
| 1571 | |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 1572 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 1573 | { |
| 1574 | int i; |
| 1575 | struct kvm_mem_alias *alias; |
| 1576 | |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1577 | for (i = 0; i < kvm->arch.naliases; ++i) { |
| 1578 | alias = &kvm->arch.aliases[i]; |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 1579 | if (gfn >= alias->base_gfn |
| 1580 | && gfn < alias->base_gfn + alias->npages) |
| 1581 | return alias->target_gfn + gfn - alias->base_gfn; |
| 1582 | } |
| 1583 | return gfn; |
| 1584 | } |
| 1585 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1586 | /* |
| 1587 | * Set a new alias region. Aliases map a portion of physical memory into |
| 1588 | * another portion. This is useful for memory windows, for example the PC |
| 1589 | * VGA region. |
| 1590 | */ |
| 1591 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 1592 | struct kvm_memory_alias *alias) |
| 1593 | { |
| 1594 | int r, n; |
| 1595 | struct kvm_mem_alias *p; |
| 1596 | |
| 1597 | r = -EINVAL; |
| 1598 | /* General sanity checks */ |
| 1599 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 1600 | goto out; |
| 1601 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 1602 | goto out; |
| 1603 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 1604 | goto out; |
| 1605 | if (alias->guest_phys_addr + alias->memory_size |
| 1606 | < alias->guest_phys_addr) |
| 1607 | goto out; |
| 1608 | if (alias->target_phys_addr + alias->memory_size |
| 1609 | < alias->target_phys_addr) |
| 1610 | goto out; |
| 1611 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1612 | down_write(&kvm->slots_lock); |
Andrea Arcangeli | a1708ce | 2008-07-25 16:26:39 +0200 | [diff] [blame] | 1613 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1614 | |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1615 | p = &kvm->arch.aliases[alias->slot]; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1616 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 1617 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 1618 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
| 1619 | |
| 1620 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1621 | if (kvm->arch.aliases[n - 1].npages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1622 | break; |
Zhang Xiantao | d69fb81 | 2007-12-14 09:54:20 +0800 | [diff] [blame] | 1623 | kvm->arch.naliases = n; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1624 | |
Andrea Arcangeli | a1708ce | 2008-07-25 16:26:39 +0200 | [diff] [blame] | 1625 | spin_unlock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1626 | kvm_mmu_zap_all(kvm); |
| 1627 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1628 | up_write(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1629 | |
| 1630 | return 0; |
| 1631 | |
| 1632 | out: |
| 1633 | return r; |
| 1634 | } |
| 1635 | |
| 1636 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 1637 | { |
| 1638 | int r; |
| 1639 | |
| 1640 | r = 0; |
| 1641 | switch (chip->chip_id) { |
| 1642 | case KVM_IRQCHIP_PIC_MASTER: |
| 1643 | memcpy(&chip->chip.pic, |
| 1644 | &pic_irqchip(kvm)->pics[0], |
| 1645 | sizeof(struct kvm_pic_state)); |
| 1646 | break; |
| 1647 | case KVM_IRQCHIP_PIC_SLAVE: |
| 1648 | memcpy(&chip->chip.pic, |
| 1649 | &pic_irqchip(kvm)->pics[1], |
| 1650 | sizeof(struct kvm_pic_state)); |
| 1651 | break; |
| 1652 | case KVM_IRQCHIP_IOAPIC: |
| 1653 | memcpy(&chip->chip.ioapic, |
| 1654 | ioapic_irqchip(kvm), |
| 1655 | sizeof(struct kvm_ioapic_state)); |
| 1656 | break; |
| 1657 | default: |
| 1658 | r = -EINVAL; |
| 1659 | break; |
| 1660 | } |
| 1661 | return r; |
| 1662 | } |
| 1663 | |
| 1664 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 1665 | { |
| 1666 | int r; |
| 1667 | |
| 1668 | r = 0; |
| 1669 | switch (chip->chip_id) { |
| 1670 | case KVM_IRQCHIP_PIC_MASTER: |
| 1671 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 1672 | &chip->chip.pic, |
| 1673 | sizeof(struct kvm_pic_state)); |
| 1674 | break; |
| 1675 | case KVM_IRQCHIP_PIC_SLAVE: |
| 1676 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 1677 | &chip->chip.pic, |
| 1678 | sizeof(struct kvm_pic_state)); |
| 1679 | break; |
| 1680 | case KVM_IRQCHIP_IOAPIC: |
| 1681 | memcpy(ioapic_irqchip(kvm), |
| 1682 | &chip->chip.ioapic, |
| 1683 | sizeof(struct kvm_ioapic_state)); |
| 1684 | break; |
| 1685 | default: |
| 1686 | r = -EINVAL; |
| 1687 | break; |
| 1688 | } |
| 1689 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 1690 | return r; |
| 1691 | } |
| 1692 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1693 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 1694 | { |
| 1695 | int r = 0; |
| 1696 | |
| 1697 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
| 1698 | return r; |
| 1699 | } |
| 1700 | |
| 1701 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 1702 | { |
| 1703 | int r = 0; |
| 1704 | |
| 1705 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
| 1706 | kvm_pit_load_count(kvm, 0, ps->channels[0].count); |
| 1707 | return r; |
| 1708 | } |
| 1709 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1710 | /* |
| 1711 | * Get (and clear) the dirty memory log for a memory slot. |
| 1712 | */ |
| 1713 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1714 | struct kvm_dirty_log *log) |
| 1715 | { |
| 1716 | int r; |
| 1717 | int n; |
| 1718 | struct kvm_memory_slot *memslot; |
| 1719 | int is_dirty = 0; |
| 1720 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1721 | down_write(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1722 | |
| 1723 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1724 | if (r) |
| 1725 | goto out; |
| 1726 | |
| 1727 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1728 | if (is_dirty) { |
| 1729 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
| 1730 | kvm_flush_remote_tlbs(kvm); |
| 1731 | memslot = &kvm->memslots[log->slot]; |
| 1732 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
| 1733 | memset(memslot->dirty_bitmap, 0, n); |
| 1734 | } |
| 1735 | r = 0; |
| 1736 | out: |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1737 | up_write(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 1738 | return r; |
| 1739 | } |
| 1740 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1741 | long kvm_arch_vm_ioctl(struct file *filp, |
| 1742 | unsigned int ioctl, unsigned long arg) |
| 1743 | { |
| 1744 | struct kvm *kvm = filp->private_data; |
| 1745 | void __user *argp = (void __user *)arg; |
| 1746 | int r = -EINVAL; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1747 | /* |
| 1748 | * This union makes it completely explicit to gcc-3.x |
| 1749 | * that these two variables' stack usage should be |
| 1750 | * combined, not added together. |
| 1751 | */ |
| 1752 | union { |
| 1753 | struct kvm_pit_state ps; |
| 1754 | struct kvm_memory_alias alias; |
| 1755 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1756 | |
| 1757 | switch (ioctl) { |
| 1758 | case KVM_SET_TSS_ADDR: |
| 1759 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 1760 | if (r < 0) |
| 1761 | goto out; |
| 1762 | break; |
| 1763 | case KVM_SET_MEMORY_REGION: { |
| 1764 | struct kvm_memory_region kvm_mem; |
| 1765 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1766 | |
| 1767 | r = -EFAULT; |
| 1768 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 1769 | goto out; |
| 1770 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 1771 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 1772 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 1773 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 1774 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 1775 | if (r) |
| 1776 | goto out; |
| 1777 | break; |
| 1778 | } |
| 1779 | case KVM_SET_NR_MMU_PAGES: |
| 1780 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 1781 | if (r) |
| 1782 | goto out; |
| 1783 | break; |
| 1784 | case KVM_GET_NR_MMU_PAGES: |
| 1785 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 1786 | break; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1787 | case KVM_SET_MEMORY_ALIAS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1788 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1789 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1790 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1791 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1792 | if (r) |
| 1793 | goto out; |
| 1794 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1795 | case KVM_CREATE_IRQCHIP: |
| 1796 | r = -ENOMEM; |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 1797 | kvm->arch.vpic = kvm_create_pic(kvm); |
| 1798 | if (kvm->arch.vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1799 | r = kvm_ioapic_init(kvm); |
| 1800 | if (r) { |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 1801 | kfree(kvm->arch.vpic); |
| 1802 | kvm->arch.vpic = NULL; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1803 | goto out; |
| 1804 | } |
| 1805 | } else |
| 1806 | goto out; |
| 1807 | break; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1808 | case KVM_CREATE_PIT: |
| 1809 | r = -ENOMEM; |
| 1810 | kvm->arch.vpit = kvm_create_pit(kvm); |
| 1811 | if (kvm->arch.vpit) |
| 1812 | r = 0; |
| 1813 | break; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1814 | case KVM_IRQ_LINE: { |
| 1815 | struct kvm_irq_level irq_event; |
| 1816 | |
| 1817 | r = -EFAULT; |
| 1818 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 1819 | goto out; |
| 1820 | if (irqchip_in_kernel(kvm)) { |
| 1821 | mutex_lock(&kvm->lock); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 1822 | kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 1823 | irq_event.irq, irq_event.level); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1824 | mutex_unlock(&kvm->lock); |
| 1825 | r = 0; |
| 1826 | } |
| 1827 | break; |
| 1828 | } |
| 1829 | case KVM_GET_IRQCHIP: { |
| 1830 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1831 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1832 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1833 | r = -ENOMEM; |
| 1834 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1835 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1836 | r = -EFAULT; |
| 1837 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 1838 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1839 | r = -ENXIO; |
| 1840 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1841 | goto get_irqchip_out; |
| 1842 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 1843 | if (r) |
| 1844 | goto get_irqchip_out; |
| 1845 | r = -EFAULT; |
| 1846 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 1847 | goto get_irqchip_out; |
| 1848 | r = 0; |
| 1849 | get_irqchip_out: |
| 1850 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1851 | if (r) |
| 1852 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1853 | break; |
| 1854 | } |
| 1855 | case KVM_SET_IRQCHIP: { |
| 1856 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1857 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1858 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1859 | r = -ENOMEM; |
| 1860 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1861 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1862 | r = -EFAULT; |
| 1863 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 1864 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1865 | r = -ENXIO; |
| 1866 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1867 | goto set_irqchip_out; |
| 1868 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 1869 | if (r) |
| 1870 | goto set_irqchip_out; |
| 1871 | r = 0; |
| 1872 | set_irqchip_out: |
| 1873 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1874 | if (r) |
| 1875 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1878 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1879 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1880 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1881 | goto out; |
| 1882 | r = -ENXIO; |
| 1883 | if (!kvm->arch.vpit) |
| 1884 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1885 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1886 | if (r) |
| 1887 | goto out; |
| 1888 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1889 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1890 | goto out; |
| 1891 | r = 0; |
| 1892 | break; |
| 1893 | } |
| 1894 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1895 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1896 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1897 | goto out; |
| 1898 | r = -ENXIO; |
| 1899 | if (!kvm->arch.vpit) |
| 1900 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 1901 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 1902 | if (r) |
| 1903 | goto out; |
| 1904 | r = 0; |
| 1905 | break; |
| 1906 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1907 | default: |
| 1908 | ; |
| 1909 | } |
| 1910 | out: |
| 1911 | return r; |
| 1912 | } |
| 1913 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 1914 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1915 | { |
| 1916 | u32 dummy[2]; |
| 1917 | unsigned i, j; |
| 1918 | |
| 1919 | for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) { |
| 1920 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 1921 | continue; |
| 1922 | if (j < i) |
| 1923 | msrs_to_save[j] = msrs_to_save[i]; |
| 1924 | j++; |
| 1925 | } |
| 1926 | num_msrs_to_save = j; |
| 1927 | } |
| 1928 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1929 | /* |
| 1930 | * Only apic need an MMIO device hook, so shortcut now.. |
| 1931 | */ |
| 1932 | static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1933 | gpa_t addr, int len, |
| 1934 | int is_write) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1935 | { |
| 1936 | struct kvm_io_device *dev; |
| 1937 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1938 | if (vcpu->arch.apic) { |
| 1939 | dev = &vcpu->arch.apic->dev; |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1940 | if (dev->in_range(dev, addr, len, is_write)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1941 | return dev; |
| 1942 | } |
| 1943 | return NULL; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1948 | gpa_t addr, int len, |
| 1949 | int is_write) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1950 | { |
| 1951 | struct kvm_io_device *dev; |
| 1952 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1953 | dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1954 | if (dev == NULL) |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 1955 | dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, |
| 1956 | is_write); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1957 | return dev; |
| 1958 | } |
| 1959 | |
| 1960 | int emulator_read_std(unsigned long addr, |
| 1961 | void *val, |
| 1962 | unsigned int bytes, |
| 1963 | struct kvm_vcpu *vcpu) |
| 1964 | { |
| 1965 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1966 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1967 | |
| 1968 | while (bytes) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1969 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1970 | unsigned offset = addr & (PAGE_SIZE-1); |
| 1971 | unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 1972 | int ret; |
| 1973 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1974 | if (gpa == UNMAPPED_GVA) { |
| 1975 | r = X86EMUL_PROPAGATE_FAULT; |
| 1976 | goto out; |
| 1977 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1978 | ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1979 | if (ret < 0) { |
| 1980 | r = X86EMUL_UNHANDLEABLE; |
| 1981 | goto out; |
| 1982 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1983 | |
| 1984 | bytes -= tocopy; |
| 1985 | data += tocopy; |
| 1986 | addr += tocopy; |
| 1987 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1988 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1989 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1990 | } |
| 1991 | EXPORT_SYMBOL_GPL(emulator_read_std); |
| 1992 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 1993 | static int emulator_read_emulated(unsigned long addr, |
| 1994 | void *val, |
| 1995 | unsigned int bytes, |
| 1996 | struct kvm_vcpu *vcpu) |
| 1997 | { |
| 1998 | struct kvm_io_device *mmio_dev; |
| 1999 | gpa_t gpa; |
| 2000 | |
| 2001 | if (vcpu->mmio_read_completed) { |
| 2002 | memcpy(val, vcpu->mmio_data, bytes); |
| 2003 | vcpu->mmio_read_completed = 0; |
| 2004 | return X86EMUL_CONTINUE; |
| 2005 | } |
| 2006 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2007 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2008 | |
| 2009 | /* For APIC access vmexit */ |
| 2010 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2011 | goto mmio; |
| 2012 | |
| 2013 | if (emulator_read_std(addr, val, bytes, vcpu) |
| 2014 | == X86EMUL_CONTINUE) |
| 2015 | return X86EMUL_CONTINUE; |
| 2016 | if (gpa == UNMAPPED_GVA) |
| 2017 | return X86EMUL_PROPAGATE_FAULT; |
| 2018 | |
| 2019 | mmio: |
| 2020 | /* |
| 2021 | * Is this MMIO handled locally? |
| 2022 | */ |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2023 | mutex_lock(&vcpu->kvm->lock); |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2024 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2025 | if (mmio_dev) { |
| 2026 | kvm_iodevice_read(mmio_dev, gpa, bytes, val); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2027 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2028 | return X86EMUL_CONTINUE; |
| 2029 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2030 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2031 | |
| 2032 | vcpu->mmio_needed = 1; |
| 2033 | vcpu->mmio_phys_addr = gpa; |
| 2034 | vcpu->mmio_size = bytes; |
| 2035 | vcpu->mmio_is_write = 0; |
| 2036 | |
| 2037 | return X86EMUL_UNHANDLEABLE; |
| 2038 | } |
| 2039 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2040 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 2041 | const void *val, int bytes) |
| 2042 | { |
| 2043 | int ret; |
| 2044 | |
| 2045 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 2046 | if (ret < 0) |
| 2047 | return 0; |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 2048 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 2049 | return 1; |
| 2050 | } |
| 2051 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2052 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 2053 | const void *val, |
| 2054 | unsigned int bytes, |
| 2055 | struct kvm_vcpu *vcpu) |
| 2056 | { |
| 2057 | struct kvm_io_device *mmio_dev; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2058 | gpa_t gpa; |
| 2059 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2060 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2061 | |
| 2062 | if (gpa == UNMAPPED_GVA) { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 2063 | kvm_inject_page_fault(vcpu, addr, 2); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2064 | return X86EMUL_PROPAGATE_FAULT; |
| 2065 | } |
| 2066 | |
| 2067 | /* For APIC access vmexit */ |
| 2068 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2069 | goto mmio; |
| 2070 | |
| 2071 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 2072 | return X86EMUL_CONTINUE; |
| 2073 | |
| 2074 | mmio: |
| 2075 | /* |
| 2076 | * Is this MMIO handled locally? |
| 2077 | */ |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2078 | mutex_lock(&vcpu->kvm->lock); |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2079 | mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2080 | if (mmio_dev) { |
| 2081 | kvm_iodevice_write(mmio_dev, gpa, bytes, val); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2082 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2083 | return X86EMUL_CONTINUE; |
| 2084 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2085 | mutex_unlock(&vcpu->kvm->lock); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2086 | |
| 2087 | vcpu->mmio_needed = 1; |
| 2088 | vcpu->mmio_phys_addr = gpa; |
| 2089 | vcpu->mmio_size = bytes; |
| 2090 | vcpu->mmio_is_write = 1; |
| 2091 | memcpy(vcpu->mmio_data, val, bytes); |
| 2092 | |
| 2093 | return X86EMUL_CONTINUE; |
| 2094 | } |
| 2095 | |
| 2096 | int emulator_write_emulated(unsigned long addr, |
| 2097 | const void *val, |
| 2098 | unsigned int bytes, |
| 2099 | struct kvm_vcpu *vcpu) |
| 2100 | { |
| 2101 | /* Crossing a page boundary? */ |
| 2102 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 2103 | int rc, now; |
| 2104 | |
| 2105 | now = -addr & ~PAGE_MASK; |
| 2106 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
| 2107 | if (rc != X86EMUL_CONTINUE) |
| 2108 | return rc; |
| 2109 | addr += now; |
| 2110 | val += now; |
| 2111 | bytes -= now; |
| 2112 | } |
| 2113 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
| 2114 | } |
| 2115 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 2116 | |
| 2117 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 2118 | const void *old, |
| 2119 | const void *new, |
| 2120 | unsigned int bytes, |
| 2121 | struct kvm_vcpu *vcpu) |
| 2122 | { |
| 2123 | static int reported; |
| 2124 | |
| 2125 | if (!reported) { |
| 2126 | reported = 1; |
| 2127 | printk(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 2128 | } |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2129 | #ifndef CONFIG_X86_64 |
| 2130 | /* guests cmpxchg8b have to be emulated atomically */ |
| 2131 | if (bytes == 8) { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2132 | gpa_t gpa; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2133 | struct page *page; |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 2134 | char *kaddr; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2135 | u64 val; |
| 2136 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2137 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr); |
| 2138 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2139 | if (gpa == UNMAPPED_GVA || |
| 2140 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 2141 | goto emul_write; |
| 2142 | |
| 2143 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 2144 | goto emul_write; |
| 2145 | |
| 2146 | val = *(u64 *)new; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2147 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2148 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2149 | |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 2150 | kaddr = kmap_atomic(page, KM_USER0); |
| 2151 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); |
| 2152 | kunmap_atomic(kaddr, KM_USER0); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2153 | kvm_release_page_dirty(page); |
| 2154 | } |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2155 | emul_write: |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 2156 | #endif |
| 2157 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2158 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 2159 | } |
| 2160 | |
| 2161 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2162 | { |
| 2163 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 2164 | } |
| 2165 | |
| 2166 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 2167 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2168 | kvm_mmu_invlpg(vcpu, address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2169 | return X86EMUL_CONTINUE; |
| 2170 | } |
| 2171 | |
| 2172 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 2173 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2174 | KVMTRACE_0D(CLTS, vcpu, handler); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2175 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2176 | return X86EMUL_CONTINUE; |
| 2177 | } |
| 2178 | |
| 2179 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 2180 | { |
| 2181 | struct kvm_vcpu *vcpu = ctxt->vcpu; |
| 2182 | |
| 2183 | switch (dr) { |
| 2184 | case 0 ... 3: |
| 2185 | *dest = kvm_x86_ops->get_dr(vcpu, dr); |
| 2186 | return X86EMUL_CONTINUE; |
| 2187 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2188 | pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2189 | return X86EMUL_UNHANDLEABLE; |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 2194 | { |
| 2195 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
| 2196 | int exception; |
| 2197 | |
| 2198 | kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception); |
| 2199 | if (exception) { |
| 2200 | /* FIXME: better handling */ |
| 2201 | return X86EMUL_UNHANDLEABLE; |
| 2202 | } |
| 2203 | return X86EMUL_CONTINUE; |
| 2204 | } |
| 2205 | |
| 2206 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 2207 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2208 | u8 opcodes[4]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2209 | unsigned long rip = kvm_rip_read(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2210 | unsigned long rip_linear; |
| 2211 | |
Avi Kivity | f76c710 | 2008-06-13 22:45:42 +0300 | [diff] [blame] | 2212 | if (!printk_ratelimit()) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2213 | return; |
| 2214 | |
Glauber Costa | 25be460 | 2008-06-10 10:46:53 -0300 | [diff] [blame] | 2215 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 2216 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2217 | emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu); |
| 2218 | |
| 2219 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 2220 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2221 | } |
| 2222 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 2223 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2224 | static struct x86_emulate_ops emulate_ops = { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2225 | .read_std = emulator_read_std, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2226 | .read_emulated = emulator_read_emulated, |
| 2227 | .write_emulated = emulator_write_emulated, |
| 2228 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 2229 | }; |
| 2230 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2231 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 2232 | { |
| 2233 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2234 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 2235 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 2236 | vcpu->arch.regs_dirty = ~0; |
| 2237 | } |
| 2238 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2239 | int emulate_instruction(struct kvm_vcpu *vcpu, |
| 2240 | struct kvm_run *run, |
| 2241 | unsigned long cr2, |
| 2242 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2243 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2244 | { |
| 2245 | int r; |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2246 | struct decode_cache *c; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2247 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 2248 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2249 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2250 | /* |
| 2251 | * TODO: fix x86_emulate.c to use guest_read/write_register |
| 2252 | * instead of direct ->regs accesses, can save hundred cycles |
| 2253 | * on Intel for instructions that don't read/change RSP, for |
| 2254 | * for example. |
| 2255 | */ |
| 2256 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2257 | |
| 2258 | vcpu->mmio_is_write = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2259 | vcpu->arch.pio.string = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2260 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2261 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2262 | int cs_db, cs_l; |
| 2263 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 2264 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2265 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
| 2266 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 2267 | vcpu->arch.emulate_ctxt.mode = |
| 2268 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2269 | ? X86EMUL_MODE_REAL : cs_l |
| 2270 | ? X86EMUL_MODE_PROT64 : cs_db |
| 2271 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 2272 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2273 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2274 | |
| 2275 | /* Reject the instructions other than VMCALL/VMMCALL when |
| 2276 | * try to emulate invalid opcode */ |
| 2277 | c = &vcpu->arch.emulate_ctxt.decode; |
| 2278 | if ((emulation_type & EMULTYPE_TRAP_UD) && |
| 2279 | (!(c->twobyte && c->b == 0x01 && |
| 2280 | (c->modrm_reg == 0 || c->modrm_reg == 3) && |
| 2281 | c->modrm_mod == 3 && c->modrm_rm == 1))) |
| 2282 | return EMULATE_FAIL; |
| 2283 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 2284 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2285 | if (r) { |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 2286 | ++vcpu->stat.insn_emulation_fail; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2287 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 2288 | return EMULATE_DONE; |
| 2289 | return EMULATE_FAIL; |
| 2290 | } |
| 2291 | } |
| 2292 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2293 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2294 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2295 | if (vcpu->arch.pio.string) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2296 | return EMULATE_DO_MMIO; |
| 2297 | |
| 2298 | if ((r || vcpu->mmio_is_write) && run) { |
| 2299 | run->exit_reason = KVM_EXIT_MMIO; |
| 2300 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 2301 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 2302 | run->mmio.len = vcpu->mmio_size; |
| 2303 | run->mmio.is_write = vcpu->mmio_is_write; |
| 2304 | } |
| 2305 | |
| 2306 | if (r) { |
| 2307 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 2308 | return EMULATE_DONE; |
| 2309 | if (!vcpu->mmio_needed) { |
| 2310 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 2311 | return EMULATE_FAIL; |
| 2312 | } |
| 2313 | return EMULATE_DO_MMIO; |
| 2314 | } |
| 2315 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2316 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 2317 | |
| 2318 | if (vcpu->mmio_is_write) { |
| 2319 | vcpu->mmio_needed = 0; |
| 2320 | return EMULATE_DO_MMIO; |
| 2321 | } |
| 2322 | |
| 2323 | return EMULATE_DONE; |
| 2324 | } |
| 2325 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 2326 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2327 | static void free_pio_guest_pages(struct kvm_vcpu *vcpu) |
| 2328 | { |
| 2329 | int i; |
| 2330 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2331 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i) |
| 2332 | if (vcpu->arch.pio.guest_pages[i]) { |
| 2333 | kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]); |
| 2334 | vcpu->arch.pio.guest_pages[i] = NULL; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
| 2339 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2340 | void *p = vcpu->arch.pio_data; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2341 | void *q; |
| 2342 | unsigned bytes; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2343 | int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2344 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2345 | q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2346 | PAGE_KERNEL); |
| 2347 | if (!q) { |
| 2348 | free_pio_guest_pages(vcpu); |
| 2349 | return -ENOMEM; |
| 2350 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2351 | q += vcpu->arch.pio.guest_page_offset; |
| 2352 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
| 2353 | if (vcpu->arch.pio.in) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2354 | memcpy(q, p, bytes); |
| 2355 | else |
| 2356 | memcpy(p, q, bytes); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2357 | q -= vcpu->arch.pio.guest_page_offset; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2358 | vunmap(q); |
| 2359 | free_pio_guest_pages(vcpu); |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | int complete_pio(struct kvm_vcpu *vcpu) |
| 2364 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2365 | struct kvm_pio_request *io = &vcpu->arch.pio; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2366 | long delta; |
| 2367 | int r; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2368 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2369 | |
| 2370 | if (!io->string) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2371 | if (io->in) { |
| 2372 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2373 | memcpy(&val, vcpu->arch.pio_data, io->size); |
| 2374 | kvm_register_write(vcpu, VCPU_REGS_RAX, val); |
| 2375 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2376 | } else { |
| 2377 | if (io->in) { |
| 2378 | r = pio_copy_data(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2379 | if (r) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2380 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | delta = 1; |
| 2384 | if (io->rep) { |
| 2385 | delta *= io->cur_count; |
| 2386 | /* |
| 2387 | * The size of the register should really depend on |
| 2388 | * current address size. |
| 2389 | */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2390 | val = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 2391 | val -= delta; |
| 2392 | kvm_register_write(vcpu, VCPU_REGS_RCX, val); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2393 | } |
| 2394 | if (io->down) |
| 2395 | delta = -delta; |
| 2396 | delta *= io->size; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2397 | if (io->in) { |
| 2398 | val = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 2399 | val += delta; |
| 2400 | kvm_register_write(vcpu, VCPU_REGS_RDI, val); |
| 2401 | } else { |
| 2402 | val = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 2403 | val += delta; |
| 2404 | kvm_register_write(vcpu, VCPU_REGS_RSI, val); |
| 2405 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2408 | io->count -= io->cur_count; |
| 2409 | io->cur_count = 0; |
| 2410 | |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
| 2414 | static void kernel_pio(struct kvm_io_device *pio_dev, |
| 2415 | struct kvm_vcpu *vcpu, |
| 2416 | void *pd) |
| 2417 | { |
| 2418 | /* TODO: String I/O for in kernel device */ |
| 2419 | |
| 2420 | mutex_lock(&vcpu->kvm->lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2421 | if (vcpu->arch.pio.in) |
| 2422 | kvm_iodevice_read(pio_dev, vcpu->arch.pio.port, |
| 2423 | vcpu->arch.pio.size, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2424 | pd); |
| 2425 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2426 | kvm_iodevice_write(pio_dev, vcpu->arch.pio.port, |
| 2427 | vcpu->arch.pio.size, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2428 | pd); |
| 2429 | mutex_unlock(&vcpu->kvm->lock); |
| 2430 | } |
| 2431 | |
| 2432 | static void pio_string_write(struct kvm_io_device *pio_dev, |
| 2433 | struct kvm_vcpu *vcpu) |
| 2434 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2435 | struct kvm_pio_request *io = &vcpu->arch.pio; |
| 2436 | void *pd = vcpu->arch.pio_data; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2437 | int i; |
| 2438 | |
| 2439 | mutex_lock(&vcpu->kvm->lock); |
| 2440 | for (i = 0; i < io->cur_count; i++) { |
| 2441 | kvm_iodevice_write(pio_dev, io->port, |
| 2442 | io->size, |
| 2443 | pd); |
| 2444 | pd += io->size; |
| 2445 | } |
| 2446 | mutex_unlock(&vcpu->kvm->lock); |
| 2447 | } |
| 2448 | |
| 2449 | static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu, |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2450 | gpa_t addr, int len, |
| 2451 | int is_write) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2452 | { |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2453 | return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 2457 | int size, unsigned port) |
| 2458 | { |
| 2459 | struct kvm_io_device *pio_dev; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2460 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2461 | |
| 2462 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 2463 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2464 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2465 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2466 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
| 2467 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 2468 | vcpu->arch.pio.in = in; |
| 2469 | vcpu->arch.pio.string = 0; |
| 2470 | vcpu->arch.pio.down = 0; |
| 2471 | vcpu->arch.pio.guest_page_offset = 0; |
| 2472 | vcpu->arch.pio.rep = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2473 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2474 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
| 2475 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, |
| 2476 | handler); |
| 2477 | else |
| 2478 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, |
| 2479 | handler); |
| 2480 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2481 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2482 | memcpy(vcpu->arch.pio_data, &val, 4); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2483 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2484 | pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2485 | if (pio_dev) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2486 | kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2487 | complete_pio(vcpu); |
| 2488 | return 1; |
| 2489 | } |
| 2490 | return 0; |
| 2491 | } |
| 2492 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 2493 | |
| 2494 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, |
| 2495 | int size, unsigned long count, int down, |
| 2496 | gva_t address, int rep, unsigned port) |
| 2497 | { |
| 2498 | unsigned now, in_page; |
| 2499 | int i, ret = 0; |
| 2500 | int nr_pages = 1; |
| 2501 | struct page *page; |
| 2502 | struct kvm_io_device *pio_dev; |
| 2503 | |
| 2504 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 2505 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2506 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2507 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2508 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
| 2509 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 2510 | vcpu->arch.pio.in = in; |
| 2511 | vcpu->arch.pio.string = 1; |
| 2512 | vcpu->arch.pio.down = down; |
| 2513 | vcpu->arch.pio.guest_page_offset = offset_in_page(address); |
| 2514 | vcpu->arch.pio.rep = rep; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2515 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2516 | if (vcpu->run->io.direction == KVM_EXIT_IO_IN) |
| 2517 | KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size, |
| 2518 | handler); |
| 2519 | else |
| 2520 | KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size, |
| 2521 | handler); |
| 2522 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2523 | if (!count) { |
| 2524 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 2525 | return 1; |
| 2526 | } |
| 2527 | |
| 2528 | if (!down) |
| 2529 | in_page = PAGE_SIZE - offset_in_page(address); |
| 2530 | else |
| 2531 | in_page = offset_in_page(address) + size; |
| 2532 | now = min(count, (unsigned long)in_page / size); |
| 2533 | if (!now) { |
| 2534 | /* |
| 2535 | * String I/O straddles page boundary. Pin two guest pages |
| 2536 | * so that we satisfy atomicity constraints. Do just one |
| 2537 | * transaction to avoid complexity. |
| 2538 | */ |
| 2539 | nr_pages = 2; |
| 2540 | now = 1; |
| 2541 | } |
| 2542 | if (down) { |
| 2543 | /* |
| 2544 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 2545 | */ |
| 2546 | pr_unimpl(vcpu, "guest string pio down\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2547 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2548 | return 1; |
| 2549 | } |
| 2550 | vcpu->run->io.count = now; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2551 | vcpu->arch.pio.cur_count = now; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2552 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2553 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2554 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 2555 | |
| 2556 | for (i = 0; i < nr_pages; ++i) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2557 | page = gva_to_page(vcpu, address + i * PAGE_SIZE); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2558 | vcpu->arch.pio.guest_pages[i] = page; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2559 | if (!page) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2560 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2561 | free_pio_guest_pages(vcpu); |
| 2562 | return 1; |
| 2563 | } |
| 2564 | } |
| 2565 | |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 2566 | pio_dev = vcpu_find_pio_dev(vcpu, port, |
| 2567 | vcpu->arch.pio.cur_count, |
| 2568 | !vcpu->arch.pio.in); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2569 | if (!vcpu->arch.pio.in) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2570 | /* string PIO write */ |
| 2571 | ret = pio_copy_data(vcpu); |
| 2572 | if (ret >= 0 && pio_dev) { |
| 2573 | pio_string_write(pio_dev, vcpu); |
| 2574 | complete_pio(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2575 | if (vcpu->arch.pio.count == 0) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 2576 | ret = 1; |
| 2577 | } |
| 2578 | } else if (pio_dev) |
| 2579 | pr_unimpl(vcpu, "no string pio read support yet, " |
| 2580 | "port %x size %d count %ld\n", |
| 2581 | port, size, count); |
| 2582 | |
| 2583 | return ret; |
| 2584 | } |
| 2585 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
| 2586 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2587 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2588 | { |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2589 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2590 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 2591 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2592 | if (kvm_x86_ops) { |
| 2593 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2594 | r = -EEXIST; |
| 2595 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | if (!ops->cpu_has_kvm_support()) { |
| 2599 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2600 | r = -EOPNOTSUPP; |
| 2601 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2602 | } |
| 2603 | if (ops->disabled_by_bios()) { |
| 2604 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2605 | r = -EOPNOTSUPP; |
| 2606 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 2609 | r = kvm_mmu_module_init(); |
| 2610 | if (r) |
| 2611 | goto out; |
| 2612 | |
| 2613 | kvm_init_msr_list(); |
| 2614 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2615 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2616 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2617 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 2618 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 2619 | PT_DIRTY_MASK, PT64_NX_MASK, 0, 0); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2620 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2621 | |
| 2622 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2623 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 2624 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2625 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2626 | void kvm_arch_exit(void) |
| 2627 | { |
| 2628 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 2629 | kvm_mmu_module_exit(); |
| 2630 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 2631 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2632 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 2633 | { |
| 2634 | ++vcpu->stat.halt_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2635 | KVMTRACE_0D(HLT, vcpu, handler); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2636 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 2637 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2638 | return 1; |
| 2639 | } else { |
| 2640 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 2641 | return 0; |
| 2642 | } |
| 2643 | } |
| 2644 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 2645 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2646 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 2647 | unsigned long a1) |
| 2648 | { |
| 2649 | if (is_long_mode(vcpu)) |
| 2650 | return a0; |
| 2651 | else |
| 2652 | return a0 | ((gpa_t)a1 << 32); |
| 2653 | } |
| 2654 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2655 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 2656 | { |
| 2657 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2658 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2659 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2660 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2661 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 2662 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 2663 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 2664 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2665 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2666 | KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler); |
| 2667 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2668 | if (!is_long_mode(vcpu)) { |
| 2669 | nr &= 0xFFFFFFFF; |
| 2670 | a0 &= 0xFFFFFFFF; |
| 2671 | a1 &= 0xFFFFFFFF; |
| 2672 | a2 &= 0xFFFFFFFF; |
| 2673 | a3 &= 0xFFFFFFFF; |
| 2674 | } |
| 2675 | |
| 2676 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2677 | case KVM_HC_VAPIC_POLL_IRQ: |
| 2678 | ret = 0; |
| 2679 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2680 | case KVM_HC_MMU_OP: |
| 2681 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 2682 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2683 | default: |
| 2684 | ret = -KVM_ENOSYS; |
| 2685 | break; |
| 2686 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2687 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 2688 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2689 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2690 | } |
| 2691 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 2692 | |
| 2693 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 2694 | { |
| 2695 | char instruction[3]; |
| 2696 | int ret = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2697 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2698 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2699 | |
| 2700 | /* |
| 2701 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 2702 | * to ensure that the updated hypercall appears atomically across all |
| 2703 | * VCPUs. |
| 2704 | */ |
| 2705 | kvm_mmu_zap_all(vcpu->kvm); |
| 2706 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2707 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2708 | if (emulator_write_emulated(rip, instruction, 3, vcpu) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2709 | != X86EMUL_CONTINUE) |
| 2710 | ret = -EFAULT; |
| 2711 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2712 | return ret; |
| 2713 | } |
| 2714 | |
| 2715 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 2716 | { |
| 2717 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 2718 | } |
| 2719 | |
| 2720 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 2721 | { |
| 2722 | struct descriptor_table dt = { limit, base }; |
| 2723 | |
| 2724 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 2725 | } |
| 2726 | |
| 2727 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 2728 | { |
| 2729 | struct descriptor_table dt = { limit, base }; |
| 2730 | |
| 2731 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 2732 | } |
| 2733 | |
| 2734 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 2735 | unsigned long *rflags) |
| 2736 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2737 | kvm_lmsw(vcpu, msw); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2738 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 2739 | } |
| 2740 | |
| 2741 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 2742 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2743 | unsigned long value; |
| 2744 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2745 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 2746 | switch (cr) { |
| 2747 | case 0: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2748 | value = vcpu->arch.cr0; |
| 2749 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2750 | case 2: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2751 | value = vcpu->arch.cr2; |
| 2752 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2753 | case 3: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2754 | value = vcpu->arch.cr3; |
| 2755 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2756 | case 4: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2757 | value = vcpu->arch.cr4; |
| 2758 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2759 | case 8: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2760 | value = kvm_get_cr8(vcpu); |
| 2761 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2762 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2763 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2764 | return 0; |
| 2765 | } |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2766 | KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value, |
| 2767 | (u32)((u64)value >> 32), handler); |
| 2768 | |
| 2769 | return value; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 2773 | unsigned long *rflags) |
| 2774 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 2775 | KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val, |
| 2776 | (u32)((u64)val >> 32), handler); |
| 2777 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2778 | switch (cr) { |
| 2779 | case 0: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2780 | kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2781 | *rflags = kvm_x86_ops->get_rflags(vcpu); |
| 2782 | break; |
| 2783 | case 2: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2784 | vcpu->arch.cr2 = val; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2785 | break; |
| 2786 | case 3: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2787 | kvm_set_cr3(vcpu, val); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2788 | break; |
| 2789 | case 4: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2790 | kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2791 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2792 | case 8: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2793 | kvm_set_cr8(vcpu, val & 0xfUL); |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 2794 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2795 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2796 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2800 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 2801 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2802 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 2803 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2804 | |
| 2805 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 2806 | /* when no next entry is found, the current entry[i] is reselected */ |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 2807 | for (j = i + 1; ; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2808 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2809 | if (ej->function == e->function) { |
| 2810 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 2811 | return j; |
| 2812 | } |
| 2813 | } |
| 2814 | return 0; /* silence gcc, even though control never reaches here */ |
| 2815 | } |
| 2816 | |
| 2817 | /* find an entry with matching function, matching index (if needed), and that |
| 2818 | * should be read next (if it's stateful) */ |
| 2819 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 2820 | u32 function, u32 index) |
| 2821 | { |
| 2822 | if (e->function != function) |
| 2823 | return 0; |
| 2824 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 2825 | return 0; |
| 2826 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
| 2827 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
| 2828 | return 0; |
| 2829 | return 1; |
| 2830 | } |
| 2831 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2832 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 2833 | { |
| 2834 | int i; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2835 | u32 function, index; |
| 2836 | struct kvm_cpuid_entry2 *e, *best; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2837 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2838 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 2839 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 2840 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 2841 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 2842 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 2843 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2844 | best = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2845 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 2846 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2847 | if (is_matching_cpuid_entry(e, function, index)) { |
| 2848 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 2849 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2850 | best = e; |
| 2851 | break; |
| 2852 | } |
| 2853 | /* |
| 2854 | * Both basic or both extended? |
| 2855 | */ |
| 2856 | if (((e->function ^ function) & 0x80000000) == 0) |
| 2857 | if (!best || e->function > best->function) |
| 2858 | best = e; |
| 2859 | } |
| 2860 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2861 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 2862 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 2863 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 2864 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2865 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2866 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2867 | KVMTRACE_5D(CPUID, vcpu, function, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2868 | (u32)kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 2869 | (u32)kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 2870 | (u32)kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 2871 | (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 2872 | } |
| 2873 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 2874 | |
| 2875 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2876 | * Check if userspace requested an interrupt window, and that the |
| 2877 | * interrupt window is open. |
| 2878 | * |
| 2879 | * No need to exit to userspace if we already have an interrupt queued. |
| 2880 | */ |
| 2881 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, |
| 2882 | struct kvm_run *kvm_run) |
| 2883 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2884 | return (!vcpu->arch.irq_summary && |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2885 | kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2886 | vcpu->arch.interrupt_window_open && |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2887 | (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF)); |
| 2888 | } |
| 2889 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2890 | /* |
| 2891 | * Check if userspace requested a NMI window, and that the NMI window |
| 2892 | * is open. |
| 2893 | * |
| 2894 | * No need to exit to userspace if we already have a NMI queued. |
| 2895 | */ |
| 2896 | static int dm_request_for_nmi_injection(struct kvm_vcpu *vcpu, |
| 2897 | struct kvm_run *kvm_run) |
| 2898 | { |
| 2899 | return (!vcpu->arch.nmi_pending && |
| 2900 | kvm_run->request_nmi_window && |
| 2901 | vcpu->arch.nmi_window_open); |
| 2902 | } |
| 2903 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2904 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, |
| 2905 | struct kvm_run *kvm_run) |
| 2906 | { |
| 2907 | kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2908 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2909 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2910 | if (irqchip_in_kernel(vcpu->kvm)) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2911 | kvm_run->ready_for_interrupt_injection = 1; |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2912 | kvm_run->ready_for_nmi_injection = 1; |
| 2913 | } else { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2914 | kvm_run->ready_for_interrupt_injection = |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2915 | (vcpu->arch.interrupt_window_open && |
| 2916 | vcpu->arch.irq_summary == 0); |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2917 | kvm_run->ready_for_nmi_injection = |
| 2918 | (vcpu->arch.nmi_window_open && |
| 2919 | vcpu->arch.nmi_pending == 0); |
| 2920 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2921 | } |
| 2922 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2923 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 2924 | { |
| 2925 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2926 | struct page *page; |
| 2927 | |
| 2928 | if (!apic || !apic->vapic_addr) |
| 2929 | return; |
| 2930 | |
| 2931 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2932 | |
| 2933 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 2937 | { |
| 2938 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 2939 | |
| 2940 | if (!apic || !apic->vapic_addr) |
| 2941 | return; |
| 2942 | |
Marcelo Tosatti | f8b78fa | 2008-06-23 12:04:25 -0300 | [diff] [blame] | 2943 | down_read(&vcpu->kvm->slots_lock); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2944 | kvm_release_page_dirty(apic->vapic_page); |
| 2945 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f8b78fa | 2008-06-23 12:04:25 -0300 | [diff] [blame] | 2946 | up_read(&vcpu->kvm->slots_lock); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2947 | } |
| 2948 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 2949 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2950 | { |
| 2951 | int r; |
| 2952 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 2953 | if (vcpu->requests) |
| 2954 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 2955 | kvm_mmu_unload(vcpu); |
| 2956 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2957 | r = kvm_mmu_reload(vcpu); |
| 2958 | if (unlikely(r)) |
| 2959 | goto out; |
| 2960 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 2961 | if (vcpu->requests) { |
| 2962 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 2963 | __kvm_migrate_timers(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2964 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
| 2965 | kvm_mmu_sync_roots(vcpu); |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 2966 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 2967 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2968 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
| 2969 | &vcpu->requests)) { |
| 2970 | kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS; |
| 2971 | r = 0; |
| 2972 | goto out; |
| 2973 | } |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 2974 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
| 2975 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2976 | r = 0; |
| 2977 | goto out; |
| 2978 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 2979 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2980 | |
Marcelo Tosatti | 06e0564 | 2008-06-06 16:37:36 -0300 | [diff] [blame] | 2981 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 2982 | kvm_inject_pending_timer_irqs(vcpu); |
| 2983 | |
| 2984 | preempt_disable(); |
| 2985 | |
| 2986 | kvm_x86_ops->prepare_guest_switch(vcpu); |
| 2987 | kvm_load_guest_fpu(vcpu); |
| 2988 | |
| 2989 | local_irq_disable(); |
| 2990 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 2991 | if (vcpu->requests || need_resched() || signal_pending(current)) { |
Avi Kivity | 6c14280 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 2992 | local_irq_enable(); |
| 2993 | preempt_enable(); |
| 2994 | r = 1; |
| 2995 | goto out; |
| 2996 | } |
| 2997 | |
Marcelo Tosatti | 29415c3 | 2008-08-01 20:09:13 -0300 | [diff] [blame] | 2998 | if (vcpu->guest_debug.enabled) |
| 2999 | kvm_x86_ops->guest_debug_pre(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3000 | |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 3001 | vcpu->guest_mode = 1; |
| 3002 | /* |
| 3003 | * Make sure that guest_mode assignment won't happen after |
| 3004 | * testing the pending IRQ vector bitmap. |
| 3005 | */ |
| 3006 | smp_wmb(); |
| 3007 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3008 | if (vcpu->arch.exception.pending) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3009 | __queue_exception(vcpu); |
| 3010 | else if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3011 | kvm_x86_ops->inject_pending_irq(vcpu); |
Avi Kivity | eb9774f | 2007-11-25 17:45:31 +0200 | [diff] [blame] | 3012 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3013 | kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run); |
| 3014 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3015 | kvm_lapic_sync_to_vapic(vcpu); |
| 3016 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3017 | up_read(&vcpu->kvm->slots_lock); |
| 3018 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3019 | kvm_guest_enter(); |
| 3020 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3021 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3022 | KVMTRACE_0D(VMENTRY, vcpu, entryexit); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3023 | kvm_x86_ops->run(vcpu, kvm_run); |
| 3024 | |
| 3025 | vcpu->guest_mode = 0; |
| 3026 | local_irq_enable(); |
| 3027 | |
| 3028 | ++vcpu->stat.exits; |
| 3029 | |
| 3030 | /* |
| 3031 | * We must have an instruction between local_irq_enable() and |
| 3032 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 3033 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 3034 | * But we need to prevent reordering, hence this barrier(): |
| 3035 | */ |
| 3036 | barrier(); |
| 3037 | |
| 3038 | kvm_guest_exit(); |
| 3039 | |
| 3040 | preempt_enable(); |
| 3041 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3042 | down_read(&vcpu->kvm->slots_lock); |
| 3043 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3044 | /* |
| 3045 | * Profile KVM exit RIPs: |
| 3046 | */ |
| 3047 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3048 | unsigned long rip = kvm_rip_read(vcpu); |
| 3049 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3050 | } |
| 3051 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3052 | if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu)) |
| 3053 | vcpu->arch.exception.pending = false; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3054 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3055 | kvm_lapic_sync_from_vapic(vcpu); |
| 3056 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3057 | r = kvm_x86_ops->handle_exit(kvm_run, vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3058 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3059 | return r; |
| 3060 | } |
| 3061 | |
| 3062 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 3063 | { |
| 3064 | int r; |
| 3065 | |
| 3066 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 3067 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 3068 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3069 | kvm_lapic_reset(vcpu); |
Gleb Natapov | 5f17928 | 2008-10-07 15:42:33 +0200 | [diff] [blame] | 3070 | r = kvm_arch_vcpu_reset(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3071 | if (r) |
| 3072 | return r; |
| 3073 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3074 | } |
| 3075 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3076 | down_read(&vcpu->kvm->slots_lock); |
| 3077 | vapic_enter(vcpu); |
| 3078 | |
| 3079 | r = 1; |
| 3080 | while (r > 0) { |
Gleb Natapov | af2152f | 2008-09-22 14:28:53 +0300 | [diff] [blame] | 3081 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3082 | r = vcpu_enter_guest(vcpu, kvm_run); |
| 3083 | else { |
| 3084 | up_read(&vcpu->kvm->slots_lock); |
| 3085 | kvm_vcpu_block(vcpu); |
| 3086 | down_read(&vcpu->kvm->slots_lock); |
| 3087 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests)) |
| 3088 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 3089 | vcpu->arch.mp_state = |
| 3090 | KVM_MP_STATE_RUNNABLE; |
| 3091 | if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) |
| 3092 | r = -EINTR; |
| 3093 | } |
| 3094 | |
| 3095 | if (r > 0) { |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 3096 | if (dm_request_for_nmi_injection(vcpu, kvm_run)) { |
| 3097 | r = -EINTR; |
| 3098 | kvm_run->exit_reason = KVM_EXIT_NMI; |
| 3099 | ++vcpu->stat.request_nmi_exits; |
| 3100 | } |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3101 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { |
| 3102 | r = -EINTR; |
| 3103 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 3104 | ++vcpu->stat.request_irq_exits; |
| 3105 | } |
| 3106 | if (signal_pending(current)) { |
| 3107 | r = -EINTR; |
| 3108 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 3109 | ++vcpu->stat.signal_exits; |
| 3110 | } |
| 3111 | if (need_resched()) { |
| 3112 | up_read(&vcpu->kvm->slots_lock); |
| 3113 | kvm_resched(vcpu); |
| 3114 | down_read(&vcpu->kvm->slots_lock); |
| 3115 | } |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | up_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3120 | post_kvm_run_save(vcpu, kvm_run); |
| 3121 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3122 | vapic_exit(vcpu); |
| 3123 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3124 | return r; |
| 3125 | } |
| 3126 | |
| 3127 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 3128 | { |
| 3129 | int r; |
| 3130 | sigset_t sigsaved; |
| 3131 | |
| 3132 | vcpu_load(vcpu); |
| 3133 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3134 | if (vcpu->sigset_active) |
| 3135 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 3136 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 3137 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 3138 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 3139 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 3140 | r = -EAGAIN; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3144 | /* re-sync apic's tpr */ |
| 3145 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3146 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3147 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3148 | if (vcpu->arch.pio.cur_count) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3149 | r = complete_pio(vcpu); |
| 3150 | if (r) |
| 3151 | goto out; |
| 3152 | } |
| 3153 | #if CONFIG_HAS_IOMEM |
| 3154 | if (vcpu->mmio_needed) { |
| 3155 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 3156 | vcpu->mmio_read_completed = 1; |
| 3157 | vcpu->mmio_needed = 0; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3158 | |
| 3159 | down_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3160 | r = emulate_instruction(vcpu, kvm_run, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3161 | vcpu->arch.mmio_fault_cr2, 0, |
| 3162 | EMULTYPE_NO_DECODE); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3163 | up_read(&vcpu->kvm->slots_lock); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3164 | if (r == EMULATE_DO_MMIO) { |
| 3165 | /* |
| 3166 | * Read-modify-write. Back to userspace. |
| 3167 | */ |
| 3168 | r = 0; |
| 3169 | goto out; |
| 3170 | } |
| 3171 | } |
| 3172 | #endif |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3173 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 3174 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 3175 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3176 | |
| 3177 | r = __vcpu_run(vcpu, kvm_run); |
| 3178 | |
| 3179 | out: |
| 3180 | if (vcpu->sigset_active) |
| 3181 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 3182 | |
| 3183 | vcpu_put(vcpu); |
| 3184 | return r; |
| 3185 | } |
| 3186 | |
| 3187 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3188 | { |
| 3189 | vcpu_load(vcpu); |
| 3190 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3191 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3192 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3193 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3194 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3195 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3196 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 3197 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3198 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3199 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3200 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 3201 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 3202 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 3203 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 3204 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 3205 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 3206 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 3207 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3208 | #endif |
| 3209 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3210 | regs->rip = kvm_rip_read(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3211 | regs->rflags = kvm_x86_ops->get_rflags(vcpu); |
| 3212 | |
| 3213 | /* |
| 3214 | * Don't leak debug flags in case they were set for guest debugging |
| 3215 | */ |
| 3216 | if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep) |
| 3217 | regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 3218 | |
| 3219 | vcpu_put(vcpu); |
| 3220 | |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
| 3224 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 3225 | { |
| 3226 | vcpu_load(vcpu); |
| 3227 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3228 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 3229 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 3230 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 3231 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 3232 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 3233 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 3234 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 3235 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3236 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3237 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 3238 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 3239 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 3240 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 3241 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 3242 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 3243 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 3244 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
| 3245 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3246 | #endif |
| 3247 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3248 | kvm_rip_write(vcpu, regs->rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3249 | kvm_x86_ops->set_rflags(vcpu, regs->rflags); |
| 3250 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3251 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 3252 | vcpu->arch.exception.pending = false; |
| 3253 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3254 | vcpu_put(vcpu); |
| 3255 | |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3259 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 3260 | struct kvm_segment *var, int seg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3261 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3262 | kvm_x86_ops->get_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3266 | { |
| 3267 | struct kvm_segment cs; |
| 3268 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3269 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3270 | *db = cs.db; |
| 3271 | *l = cs.l; |
| 3272 | } |
| 3273 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 3274 | |
| 3275 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 3276 | struct kvm_sregs *sregs) |
| 3277 | { |
| 3278 | struct descriptor_table dt; |
| 3279 | int pending_vec; |
| 3280 | |
| 3281 | vcpu_load(vcpu); |
| 3282 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3283 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 3284 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 3285 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 3286 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 3287 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 3288 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3289 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3290 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 3291 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3292 | |
| 3293 | kvm_x86_ops->get_idt(vcpu, &dt); |
| 3294 | sregs->idt.limit = dt.limit; |
| 3295 | sregs->idt.base = dt.base; |
| 3296 | kvm_x86_ops->get_gdt(vcpu, &dt); |
| 3297 | sregs->gdt.limit = dt.limit; |
| 3298 | sregs->gdt.base = dt.base; |
| 3299 | |
| 3300 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3301 | sregs->cr0 = vcpu->arch.cr0; |
| 3302 | sregs->cr2 = vcpu->arch.cr2; |
| 3303 | sregs->cr3 = vcpu->arch.cr3; |
| 3304 | sregs->cr4 = vcpu->arch.cr4; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3305 | sregs->cr8 = kvm_get_cr8(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3306 | sregs->efer = vcpu->arch.shadow_efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3307 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 3308 | |
| 3309 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 3310 | memset(sregs->interrupt_bitmap, 0, |
| 3311 | sizeof sregs->interrupt_bitmap); |
| 3312 | pending_vec = kvm_x86_ops->get_irq(vcpu); |
| 3313 | if (pending_vec >= 0) |
| 3314 | set_bit(pending_vec, |
| 3315 | (unsigned long *)sregs->interrupt_bitmap); |
| 3316 | } else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3317 | memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3318 | sizeof sregs->interrupt_bitmap); |
| 3319 | |
| 3320 | vcpu_put(vcpu); |
| 3321 | |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 3325 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 3326 | struct kvm_mp_state *mp_state) |
| 3327 | { |
| 3328 | vcpu_load(vcpu); |
| 3329 | mp_state->mp_state = vcpu->arch.mp_state; |
| 3330 | vcpu_put(vcpu); |
| 3331 | return 0; |
| 3332 | } |
| 3333 | |
| 3334 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 3335 | struct kvm_mp_state *mp_state) |
| 3336 | { |
| 3337 | vcpu_load(vcpu); |
| 3338 | vcpu->arch.mp_state = mp_state->mp_state; |
| 3339 | vcpu_put(vcpu); |
| 3340 | return 0; |
| 3341 | } |
| 3342 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3343 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3344 | struct kvm_segment *var, int seg) |
| 3345 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3346 | kvm_x86_ops->set_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3347 | } |
| 3348 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3349 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
| 3350 | struct kvm_segment *kvm_desct) |
| 3351 | { |
| 3352 | kvm_desct->base = seg_desc->base0; |
| 3353 | kvm_desct->base |= seg_desc->base1 << 16; |
| 3354 | kvm_desct->base |= seg_desc->base2 << 24; |
| 3355 | kvm_desct->limit = seg_desc->limit0; |
| 3356 | kvm_desct->limit |= seg_desc->limit << 16; |
Marcelo Tosatti | c93cd3a | 2008-07-19 19:08:07 -0300 | [diff] [blame] | 3357 | if (seg_desc->g) { |
| 3358 | kvm_desct->limit <<= 12; |
| 3359 | kvm_desct->limit |= 0xfff; |
| 3360 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3361 | kvm_desct->selector = selector; |
| 3362 | kvm_desct->type = seg_desc->type; |
| 3363 | kvm_desct->present = seg_desc->p; |
| 3364 | kvm_desct->dpl = seg_desc->dpl; |
| 3365 | kvm_desct->db = seg_desc->d; |
| 3366 | kvm_desct->s = seg_desc->s; |
| 3367 | kvm_desct->l = seg_desc->l; |
| 3368 | kvm_desct->g = seg_desc->g; |
| 3369 | kvm_desct->avl = seg_desc->avl; |
| 3370 | if (!selector) |
| 3371 | kvm_desct->unusable = 1; |
| 3372 | else |
| 3373 | kvm_desct->unusable = 0; |
| 3374 | kvm_desct->padding = 0; |
| 3375 | } |
| 3376 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 3377 | static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu, |
| 3378 | u16 selector, |
| 3379 | struct descriptor_table *dtable) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3380 | { |
| 3381 | if (selector & 1 << 2) { |
| 3382 | struct kvm_segment kvm_seg; |
| 3383 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3384 | kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3385 | |
| 3386 | if (kvm_seg.unusable) |
| 3387 | dtable->limit = 0; |
| 3388 | else |
| 3389 | dtable->limit = kvm_seg.limit; |
| 3390 | dtable->base = kvm_seg.base; |
| 3391 | } |
| 3392 | else |
| 3393 | kvm_x86_ops->get_gdt(vcpu, dtable); |
| 3394 | } |
| 3395 | |
| 3396 | /* allowed just for 8 bytes segments */ |
| 3397 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3398 | struct desc_struct *seg_desc) |
| 3399 | { |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3400 | gpa_t gpa; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3401 | struct descriptor_table dtable; |
| 3402 | u16 index = selector >> 3; |
| 3403 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 3404 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3405 | |
| 3406 | if (dtable.limit < index * 8 + 7) { |
| 3407 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); |
| 3408 | return 1; |
| 3409 | } |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3410 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
| 3411 | gpa += index * 8; |
| 3412 | return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | /* allowed just for 8 bytes segments */ |
| 3416 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3417 | struct desc_struct *seg_desc) |
| 3418 | { |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3419 | gpa_t gpa; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3420 | struct descriptor_table dtable; |
| 3421 | u16 index = selector >> 3; |
| 3422 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 3423 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3424 | |
| 3425 | if (dtable.limit < index * 8 + 7) |
| 3426 | return 1; |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3427 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base); |
| 3428 | gpa += index * 8; |
| 3429 | return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | static u32 get_tss_base_addr(struct kvm_vcpu *vcpu, |
| 3433 | struct desc_struct *seg_desc) |
| 3434 | { |
| 3435 | u32 base_addr; |
| 3436 | |
| 3437 | base_addr = seg_desc->base0; |
| 3438 | base_addr |= (seg_desc->base1 << 16); |
| 3439 | base_addr |= (seg_desc->base2 << 24); |
| 3440 | |
Marcelo Tosatti | 98899aa | 2008-07-16 19:07:10 -0300 | [diff] [blame] | 3441 | return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3442 | } |
| 3443 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3444 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) |
| 3445 | { |
| 3446 | struct kvm_segment kvm_seg; |
| 3447 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3448 | kvm_get_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3449 | return kvm_seg.selector; |
| 3450 | } |
| 3451 | |
| 3452 | static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu, |
| 3453 | u16 selector, |
| 3454 | struct kvm_segment *kvm_seg) |
| 3455 | { |
| 3456 | struct desc_struct seg_desc; |
| 3457 | |
| 3458 | if (load_guest_segment_descriptor(vcpu, selector, &seg_desc)) |
| 3459 | return 1; |
| 3460 | seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg); |
| 3461 | return 0; |
| 3462 | } |
| 3463 | |
Harvey Harrison | 2259e3a | 2008-08-22 13:29:17 -0700 | [diff] [blame] | 3464 | static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg) |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 3465 | { |
| 3466 | struct kvm_segment segvar = { |
| 3467 | .base = selector << 4, |
| 3468 | .limit = 0xffff, |
| 3469 | .selector = selector, |
| 3470 | .type = 3, |
| 3471 | .present = 1, |
| 3472 | .dpl = 3, |
| 3473 | .db = 0, |
| 3474 | .s = 1, |
| 3475 | .l = 0, |
| 3476 | .g = 0, |
| 3477 | .avl = 0, |
| 3478 | .unusable = 0, |
| 3479 | }; |
| 3480 | kvm_x86_ops->set_segment(vcpu, &segvar, seg); |
| 3481 | return 0; |
| 3482 | } |
| 3483 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3484 | int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 3485 | int type_bits, int seg) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3486 | { |
| 3487 | struct kvm_segment kvm_seg; |
| 3488 | |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 3489 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) |
| 3490 | return kvm_load_realmode_segment(vcpu, selector, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3491 | if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg)) |
| 3492 | return 1; |
| 3493 | kvm_seg.type |= type_bits; |
| 3494 | |
| 3495 | if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS && |
| 3496 | seg != VCPU_SREG_LDTR) |
| 3497 | if (!kvm_seg.s) |
| 3498 | kvm_seg.unusable = 1; |
| 3499 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3500 | kvm_set_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
| 3503 | |
| 3504 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, |
| 3505 | struct tss_segment_32 *tss) |
| 3506 | { |
| 3507 | tss->cr3 = vcpu->arch.cr3; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3508 | tss->eip = kvm_rip_read(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3509 | tss->eflags = kvm_x86_ops->get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3510 | tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3511 | tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3512 | tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3513 | tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3514 | tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3515 | tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 3516 | tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3517 | tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3518 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 3519 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 3520 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 3521 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 3522 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); |
| 3523 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); |
| 3524 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
| 3525 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); |
| 3526 | } |
| 3527 | |
| 3528 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, |
| 3529 | struct tss_segment_32 *tss) |
| 3530 | { |
| 3531 | kvm_set_cr3(vcpu, tss->cr3); |
| 3532 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3533 | kvm_rip_write(vcpu, tss->eip); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3534 | kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2); |
| 3535 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3536 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax); |
| 3537 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx); |
| 3538 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx); |
| 3539 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx); |
| 3540 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp); |
| 3541 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp); |
| 3542 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi); |
| 3543 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3544 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3545 | if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3546 | return 1; |
| 3547 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3548 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3549 | return 1; |
| 3550 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3551 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3552 | return 1; |
| 3553 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3554 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3555 | return 1; |
| 3556 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3557 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3558 | return 1; |
| 3559 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3560 | if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3561 | return 1; |
| 3562 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3563 | if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3564 | return 1; |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
| 3568 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, |
| 3569 | struct tss_segment_16 *tss) |
| 3570 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3571 | tss->ip = kvm_rip_read(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3572 | tss->flag = kvm_x86_ops->get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3573 | tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3574 | tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3575 | tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3576 | tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3577 | tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3578 | tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 3579 | tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3580 | tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3581 | |
| 3582 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 3583 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 3584 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 3585 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 3586 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
| 3587 | tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR); |
| 3588 | } |
| 3589 | |
| 3590 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, |
| 3591 | struct tss_segment_16 *tss) |
| 3592 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3593 | kvm_rip_write(vcpu, tss->ip); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3594 | kvm_x86_ops->set_rflags(vcpu, tss->flag | 2); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3595 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax); |
| 3596 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx); |
| 3597 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx); |
| 3598 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx); |
| 3599 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp); |
| 3600 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp); |
| 3601 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si); |
| 3602 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3603 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3604 | if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3605 | return 1; |
| 3606 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3607 | if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3608 | return 1; |
| 3609 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3610 | if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3611 | return 1; |
| 3612 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3613 | if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3614 | return 1; |
| 3615 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3616 | if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3617 | return 1; |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 3621 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3622 | u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3623 | struct desc_struct *nseg_desc) |
| 3624 | { |
| 3625 | struct tss_segment_16 tss_segment_16; |
| 3626 | int ret = 0; |
| 3627 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3628 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 3629 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3630 | goto out; |
| 3631 | |
| 3632 | save_state_to_tss16(vcpu, &tss_segment_16); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3633 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3634 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 3635 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3636 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3637 | |
| 3638 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), |
| 3639 | &tss_segment_16, sizeof tss_segment_16)) |
| 3640 | goto out; |
| 3641 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3642 | if (load_state_from_tss16(vcpu, &tss_segment_16)) |
| 3643 | goto out; |
| 3644 | |
| 3645 | ret = 1; |
| 3646 | out: |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 3650 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3651 | u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3652 | struct desc_struct *nseg_desc) |
| 3653 | { |
| 3654 | struct tss_segment_32 tss_segment_32; |
| 3655 | int ret = 0; |
| 3656 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3657 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 3658 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3659 | goto out; |
| 3660 | |
| 3661 | save_state_to_tss32(vcpu, &tss_segment_32); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3662 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3663 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 3664 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3665 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3666 | |
| 3667 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc), |
| 3668 | &tss_segment_32, sizeof tss_segment_32)) |
| 3669 | goto out; |
| 3670 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3671 | if (load_state_from_tss32(vcpu, &tss_segment_32)) |
| 3672 | goto out; |
| 3673 | |
| 3674 | ret = 1; |
| 3675 | out: |
| 3676 | return ret; |
| 3677 | } |
| 3678 | |
| 3679 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) |
| 3680 | { |
| 3681 | struct kvm_segment tr_seg; |
| 3682 | struct desc_struct cseg_desc; |
| 3683 | struct desc_struct nseg_desc; |
| 3684 | int ret = 0; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3685 | u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR); |
| 3686 | u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3687 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3688 | old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3689 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3690 | /* FIXME: Handle errors. Failure to read either TSS or their |
| 3691 | * descriptors should generate a pagefault. |
| 3692 | */ |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3693 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) |
| 3694 | goto out; |
| 3695 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3696 | if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3697 | goto out; |
| 3698 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3699 | if (reason != TASK_SWITCH_IRET) { |
| 3700 | int cpl; |
| 3701 | |
| 3702 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 3703 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { |
| 3704 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 3705 | return 1; |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) { |
| 3710 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); |
| 3711 | return 1; |
| 3712 | } |
| 3713 | |
| 3714 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 3715 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3716 | save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | if (reason == TASK_SWITCH_IRET) { |
| 3720 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); |
| 3721 | kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); |
| 3722 | } |
| 3723 | |
| 3724 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3725 | |
| 3726 | if (nseg_desc.type & 8) |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3727 | ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3728 | &nseg_desc); |
| 3729 | else |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 3730 | ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3731 | &nseg_desc); |
| 3732 | |
| 3733 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { |
| 3734 | u32 eflags = kvm_x86_ops->get_rflags(vcpu); |
| 3735 | kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT); |
| 3736 | } |
| 3737 | |
| 3738 | if (reason != TASK_SWITCH_IRET) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 3739 | nseg_desc.type |= (1 << 1); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3740 | save_guest_segment_descriptor(vcpu, tss_selector, |
| 3741 | &nseg_desc); |
| 3742 | } |
| 3743 | |
| 3744 | kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS); |
| 3745 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); |
| 3746 | tr_seg.type = 11; |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3747 | kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3748 | out: |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3749 | return ret; |
| 3750 | } |
| 3751 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 3752 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3753 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 3754 | struct kvm_sregs *sregs) |
| 3755 | { |
| 3756 | int mmu_reset_needed = 0; |
| 3757 | int i, pending_vec, max_bits; |
| 3758 | struct descriptor_table dt; |
| 3759 | |
| 3760 | vcpu_load(vcpu); |
| 3761 | |
| 3762 | dt.limit = sregs->idt.limit; |
| 3763 | dt.base = sregs->idt.base; |
| 3764 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 3765 | dt.limit = sregs->gdt.limit; |
| 3766 | dt.base = sregs->gdt.base; |
| 3767 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 3768 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3769 | vcpu->arch.cr2 = sregs->cr2; |
| 3770 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
| 3771 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3772 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3773 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3774 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3775 | mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3776 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3777 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 3778 | |
| 3779 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 3780 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3781 | mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3782 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 3783 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3784 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3785 | mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3786 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
| 3787 | if (!is_long_mode(vcpu) && is_pae(vcpu)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3788 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3789 | |
| 3790 | if (mmu_reset_needed) |
| 3791 | kvm_mmu_reset_context(vcpu); |
| 3792 | |
| 3793 | if (!irqchip_in_kernel(vcpu->kvm)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3794 | memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap, |
| 3795 | sizeof vcpu->arch.irq_pending); |
| 3796 | vcpu->arch.irq_summary = 0; |
| 3797 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i) |
| 3798 | if (vcpu->arch.irq_pending[i]) |
| 3799 | __set_bit(i, &vcpu->arch.irq_summary); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3800 | } else { |
| 3801 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 3802 | pending_vec = find_first_bit( |
| 3803 | (const unsigned long *)sregs->interrupt_bitmap, |
| 3804 | max_bits); |
| 3805 | /* Only pending external irq is handled here */ |
| 3806 | if (pending_vec < max_bits) { |
| 3807 | kvm_x86_ops->set_irq(vcpu, pending_vec); |
| 3808 | pr_debug("Set back pending irq %d\n", |
| 3809 | pending_vec); |
| 3810 | } |
Marcelo Tosatti | e482580 | 2008-09-24 20:28:34 -0300 | [diff] [blame] | 3811 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3812 | } |
| 3813 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3814 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 3815 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 3816 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 3817 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 3818 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 3819 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3820 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 3821 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 3822 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3823 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 3824 | /* Older userspace won't unhalt the vcpu on reset. */ |
| 3825 | if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 && |
| 3826 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
| 3827 | !(vcpu->arch.cr0 & X86_CR0_PE)) |
| 3828 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 3829 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 3830 | vcpu_put(vcpu); |
| 3831 | |
| 3832 | return 0; |
| 3833 | } |
| 3834 | |
| 3835 | int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu, |
| 3836 | struct kvm_debug_guest *dbg) |
| 3837 | { |
| 3838 | int r; |
| 3839 | |
| 3840 | vcpu_load(vcpu); |
| 3841 | |
| 3842 | r = kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 3843 | |
| 3844 | vcpu_put(vcpu); |
| 3845 | |
| 3846 | return r; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3850 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 3851 | * we have asm/x86/processor.h |
| 3852 | */ |
| 3853 | struct fxsave { |
| 3854 | u16 cwd; |
| 3855 | u16 swd; |
| 3856 | u16 twd; |
| 3857 | u16 fop; |
| 3858 | u64 rip; |
| 3859 | u64 rdp; |
| 3860 | u32 mxcsr; |
| 3861 | u32 mxcsr_mask; |
| 3862 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 3863 | #ifdef CONFIG_X86_64 |
| 3864 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 3865 | #else |
| 3866 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 3867 | #endif |
| 3868 | }; |
| 3869 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 3870 | /* |
| 3871 | * Translate a guest virtual address to a guest physical address. |
| 3872 | */ |
| 3873 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 3874 | struct kvm_translation *tr) |
| 3875 | { |
| 3876 | unsigned long vaddr = tr->linear_address; |
| 3877 | gpa_t gpa; |
| 3878 | |
| 3879 | vcpu_load(vcpu); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3880 | down_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3881 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3882 | up_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 3883 | tr->physical_address = gpa; |
| 3884 | tr->valid = gpa != UNMAPPED_GVA; |
| 3885 | tr->writeable = 1; |
| 3886 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 3887 | vcpu_put(vcpu); |
| 3888 | |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3892 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3893 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3894 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3895 | |
| 3896 | vcpu_load(vcpu); |
| 3897 | |
| 3898 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 3899 | fpu->fcw = fxsave->cwd; |
| 3900 | fpu->fsw = fxsave->swd; |
| 3901 | fpu->ftwx = fxsave->twd; |
| 3902 | fpu->last_opcode = fxsave->fop; |
| 3903 | fpu->last_ip = fxsave->rip; |
| 3904 | fpu->last_dp = fxsave->rdp; |
| 3905 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 3906 | |
| 3907 | vcpu_put(vcpu); |
| 3908 | |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 3913 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3914 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3915 | |
| 3916 | vcpu_load(vcpu); |
| 3917 | |
| 3918 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 3919 | fxsave->cwd = fpu->fcw; |
| 3920 | fxsave->swd = fpu->fsw; |
| 3921 | fxsave->twd = fpu->ftwx; |
| 3922 | fxsave->fop = fpu->last_opcode; |
| 3923 | fxsave->rip = fpu->last_ip; |
| 3924 | fxsave->rdp = fpu->last_dp; |
| 3925 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 3926 | |
| 3927 | vcpu_put(vcpu); |
| 3928 | |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
| 3932 | void fx_init(struct kvm_vcpu *vcpu) |
| 3933 | { |
| 3934 | unsigned after_mxcsr_mask; |
| 3935 | |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 3936 | /* |
| 3937 | * Touch the fpu the first time in non atomic context as if |
| 3938 | * this is the first fpu instruction the exception handler |
| 3939 | * will fire before the instruction returns and it'll have to |
| 3940 | * allocate ram with GFP_KERNEL. |
| 3941 | */ |
| 3942 | if (!used_math()) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 3943 | kvm_fx_save(&vcpu->arch.host_fx_image); |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 3944 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3945 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 3946 | preempt_disable(); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 3947 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 3948 | kvm_fx_finit(); |
| 3949 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 3950 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3951 | preempt_enable(); |
| 3952 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3953 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3954 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3955 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
| 3956 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3957 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 3958 | } |
| 3959 | EXPORT_SYMBOL_GPL(fx_init); |
| 3960 | |
| 3961 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 3962 | { |
| 3963 | if (!vcpu->fpu_active || vcpu->guest_fpu_loaded) |
| 3964 | return; |
| 3965 | |
| 3966 | vcpu->guest_fpu_loaded = 1; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 3967 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 3968 | kvm_fx_restore(&vcpu->arch.guest_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3969 | } |
| 3970 | EXPORT_SYMBOL_GPL(kvm_load_guest_fpu); |
| 3971 | |
| 3972 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 3973 | { |
| 3974 | if (!vcpu->guest_fpu_loaded) |
| 3975 | return; |
| 3976 | |
| 3977 | vcpu->guest_fpu_loaded = 0; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 3978 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 3979 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 3980 | ++vcpu->stat.fpu_reload; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 3981 | } |
| 3982 | EXPORT_SYMBOL_GPL(kvm_put_guest_fpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 3983 | |
| 3984 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 3985 | { |
| 3986 | kvm_x86_ops->vcpu_free(vcpu); |
| 3987 | } |
| 3988 | |
| 3989 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 3990 | unsigned int id) |
| 3991 | { |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 3992 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 3993 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 3994 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 3995 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 3996 | { |
| 3997 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 3998 | |
| 3999 | /* We do fxsave: this must be aligned. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4000 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4001 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 4002 | vcpu->arch.mtrr_state.have_fixed = 1; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4003 | vcpu_load(vcpu); |
| 4004 | r = kvm_arch_vcpu_reset(vcpu); |
| 4005 | if (r == 0) |
| 4006 | r = kvm_mmu_setup(vcpu); |
| 4007 | vcpu_put(vcpu); |
| 4008 | if (r < 0) |
| 4009 | goto free_vcpu; |
| 4010 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4011 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4012 | free_vcpu: |
| 4013 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 4014 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4015 | } |
| 4016 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 4017 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4018 | { |
| 4019 | vcpu_load(vcpu); |
| 4020 | kvm_mmu_unload(vcpu); |
| 4021 | vcpu_put(vcpu); |
| 4022 | |
| 4023 | kvm_x86_ops->vcpu_free(vcpu); |
| 4024 | } |
| 4025 | |
| 4026 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 4027 | { |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 4028 | vcpu->arch.nmi_pending = false; |
| 4029 | vcpu->arch.nmi_injected = false; |
| 4030 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4031 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 4032 | } |
| 4033 | |
| 4034 | void kvm_arch_hardware_enable(void *garbage) |
| 4035 | { |
| 4036 | kvm_x86_ops->hardware_enable(garbage); |
| 4037 | } |
| 4038 | |
| 4039 | void kvm_arch_hardware_disable(void *garbage) |
| 4040 | { |
| 4041 | kvm_x86_ops->hardware_disable(garbage); |
| 4042 | } |
| 4043 | |
| 4044 | int kvm_arch_hardware_setup(void) |
| 4045 | { |
| 4046 | return kvm_x86_ops->hardware_setup(); |
| 4047 | } |
| 4048 | |
| 4049 | void kvm_arch_hardware_unsetup(void) |
| 4050 | { |
| 4051 | kvm_x86_ops->hardware_unsetup(); |
| 4052 | } |
| 4053 | |
| 4054 | void kvm_arch_check_processor_compat(void *rtn) |
| 4055 | { |
| 4056 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 4057 | } |
| 4058 | |
| 4059 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 4060 | { |
| 4061 | struct page *page; |
| 4062 | struct kvm *kvm; |
| 4063 | int r; |
| 4064 | |
| 4065 | BUG_ON(vcpu->kvm == NULL); |
| 4066 | kvm = vcpu->kvm; |
| 4067 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4068 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4069 | if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4070 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4071 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4072 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4073 | |
| 4074 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 4075 | if (!page) { |
| 4076 | r = -ENOMEM; |
| 4077 | goto fail; |
| 4078 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4079 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4080 | |
| 4081 | r = kvm_mmu_create(vcpu); |
| 4082 | if (r < 0) |
| 4083 | goto fail_free_pio_data; |
| 4084 | |
| 4085 | if (irqchip_in_kernel(kvm)) { |
| 4086 | r = kvm_create_lapic(vcpu); |
| 4087 | if (r < 0) |
| 4088 | goto fail_mmu_destroy; |
| 4089 | } |
| 4090 | |
| 4091 | return 0; |
| 4092 | |
| 4093 | fail_mmu_destroy: |
| 4094 | kvm_mmu_destroy(vcpu); |
| 4095 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4096 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4097 | fail: |
| 4098 | return r; |
| 4099 | } |
| 4100 | |
| 4101 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 4102 | { |
| 4103 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4104 | down_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4105 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4106 | up_read(&vcpu->kvm->slots_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4107 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 4108 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4109 | |
| 4110 | struct kvm *kvm_arch_create_vm(void) |
| 4111 | { |
| 4112 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 4113 | |
| 4114 | if (!kvm) |
| 4115 | return ERR_PTR(-ENOMEM); |
| 4116 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 4117 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 4118 | INIT_LIST_HEAD(&kvm->arch.oos_global_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 4119 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4120 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 4121 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 4122 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 4123 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4124 | return kvm; |
| 4125 | } |
| 4126 | |
| 4127 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 4128 | { |
| 4129 | vcpu_load(vcpu); |
| 4130 | kvm_mmu_unload(vcpu); |
| 4131 | vcpu_put(vcpu); |
| 4132 | } |
| 4133 | |
| 4134 | static void kvm_free_vcpus(struct kvm *kvm) |
| 4135 | { |
| 4136 | unsigned int i; |
| 4137 | |
| 4138 | /* |
| 4139 | * Unpin any mmu pages first. |
| 4140 | */ |
| 4141 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 4142 | if (kvm->vcpus[i]) |
| 4143 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); |
| 4144 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 4145 | if (kvm->vcpus[i]) { |
| 4146 | kvm_arch_vcpu_free(kvm->vcpus[i]); |
| 4147 | kvm->vcpus[i] = NULL; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | } |
| 4152 | |
| 4153 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 4154 | { |
Amit Shah | bfadade | 2008-09-16 18:04:28 +0300 | [diff] [blame] | 4155 | kvm_free_all_assigned_devices(kvm); |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 4156 | kvm_iommu_unmap_guest(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 4157 | kvm_free_pit(kvm); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 4158 | kfree(kvm->arch.vpic); |
| 4159 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4160 | kvm_free_vcpus(kvm); |
| 4161 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 4162 | if (kvm->arch.apic_access_page) |
| 4163 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4164 | if (kvm->arch.ept_identity_pagetable) |
| 4165 | put_page(kvm->arch.ept_identity_pagetable); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 4166 | kfree(kvm); |
| 4167 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4168 | |
| 4169 | int kvm_arch_set_memory_region(struct kvm *kvm, |
| 4170 | struct kvm_userspace_memory_region *mem, |
| 4171 | struct kvm_memory_slot old, |
| 4172 | int user_alloc) |
| 4173 | { |
| 4174 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 4175 | struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot]; |
| 4176 | |
| 4177 | /*To keep backward compatibility with older userspace, |
| 4178 | *x86 needs to hanlde !user_alloc case. |
| 4179 | */ |
| 4180 | if (!user_alloc) { |
| 4181 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4182 | unsigned long userspace_addr; |
| 4183 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4184 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4185 | userspace_addr = do_mmap(NULL, 0, |
| 4186 | npages * PAGE_SIZE, |
| 4187 | PROT_READ | PROT_WRITE, |
Avi Kivity | acee3c0 | 2008-08-26 17:22:47 +0300 | [diff] [blame] | 4188 | MAP_PRIVATE | MAP_ANONYMOUS, |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4189 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4190 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4191 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 4192 | if (IS_ERR((void *)userspace_addr)) |
| 4193 | return PTR_ERR((void *)userspace_addr); |
| 4194 | |
| 4195 | /* set userspace_addr atomically for kvm_hva_to_rmapp */ |
| 4196 | spin_lock(&kvm->mmu_lock); |
| 4197 | memslot->userspace_addr = userspace_addr; |
| 4198 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4199 | } else { |
| 4200 | if (!old.user_alloc && old.rmap) { |
| 4201 | int ret; |
| 4202 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4203 | down_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4204 | ret = do_munmap(current->mm, old.userspace_addr, |
| 4205 | old.npages * PAGE_SIZE); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4206 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4207 | if (ret < 0) |
| 4208 | printk(KERN_WARNING |
| 4209 | "kvm_vm_ioctl_set_memory_region: " |
| 4210 | "failed to munmap memory\n"); |
| 4211 | } |
| 4212 | } |
| 4213 | } |
| 4214 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 4215 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 4216 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 4217 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 4218 | } |
| 4219 | |
| 4220 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
| 4221 | kvm_flush_remote_tlbs(kvm); |
| 4222 | |
| 4223 | return 0; |
| 4224 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4225 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 4226 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 4227 | { |
| 4228 | kvm_mmu_zap_all(kvm); |
| 4229 | } |
| 4230 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4231 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 4232 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4233 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
Jan Kiszka | 0496fbb | 2008-09-26 09:30:53 +0200 | [diff] [blame] | 4234 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
| 4235 | || vcpu->arch.nmi_pending; |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 4236 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4237 | |
| 4238 | static void vcpu_kick_intr(void *info) |
| 4239 | { |
| 4240 | #ifdef DEBUG |
| 4241 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info; |
| 4242 | printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu); |
| 4243 | #endif |
| 4244 | } |
| 4245 | |
| 4246 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 4247 | { |
| 4248 | int ipi_pcpu = vcpu->cpu; |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4249 | int cpu = get_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4250 | |
| 4251 | if (waitqueue_active(&vcpu->wq)) { |
| 4252 | wake_up_interruptible(&vcpu->wq); |
| 4253 | ++vcpu->stat.halt_wakeup; |
| 4254 | } |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4255 | /* |
| 4256 | * We may be called synchronously with irqs disabled in guest mode, |
| 4257 | * So need not to call smp_call_function_single() in that case. |
| 4258 | */ |
| 4259 | if (vcpu->guest_mode && vcpu->cpu != cpu) |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 4260 | smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 4261 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 4262 | } |