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> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 37 | #include <linux/iommu.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 38 | #include <linux/intel-iommu.h> |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 39 | #include <linux/cpufreq.h> |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 40 | #include <linux/user-return-notifier.h> |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 41 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 43 | #include <linux/perf_event.h> |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 44 | #include <trace/events/kvm.h> |
Xiao Guangrong | 2ed152a | 2010-03-10 19:00:43 +0800 | [diff] [blame] | 45 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 46 | #define CREATE_TRACE_POINTS |
| 47 | #include "trace.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 48 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 50 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 51 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 52 | #include <asm/desc.h> |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 53 | #include <asm/mtrr.h> |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 54 | #include <asm/mce.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 55 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 56 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 57 | #define CR0_RESERVED_BITS \ |
| 58 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 59 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 60 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 61 | #define CR4_RESERVED_BITS \ |
| 62 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 63 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 64 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 65 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 66 | |
| 67 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 68 | |
| 69 | #define KVM_MAX_MCE_BANKS 32 |
| 70 | #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P |
| 71 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 72 | /* EFER defaults: |
| 73 | * - enable syscall per default because its emulated by KVM |
| 74 | * - enable LME and LMA per default on 64 bit KVM |
| 75 | */ |
| 76 | #ifdef CONFIG_X86_64 |
| 77 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 78 | #else |
| 79 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 80 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 81 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 82 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 83 | #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] | 84 | |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 85 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 86 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 87 | struct kvm_cpuid_entry2 __user *entries); |
| 88 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 89 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 91 | |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 92 | int ignore_msrs = 0; |
| 93 | module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR); |
| 94 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 95 | #define KVM_NR_SHARED_MSRS 16 |
| 96 | |
| 97 | struct kvm_shared_msrs_global { |
| 98 | int nr; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 99 | u32 msrs[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct kvm_shared_msrs { |
| 103 | struct user_return_notifier urn; |
| 104 | bool registered; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 105 | struct kvm_shared_msr_values { |
| 106 | u64 host; |
| 107 | u64 curr; |
| 108 | } values[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; |
| 112 | static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs); |
| 113 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 114 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 115 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 116 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 117 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 118 | { "invlpg", VCPU_STAT(invlpg) }, |
| 119 | { "exits", VCPU_STAT(exits) }, |
| 120 | { "io_exits", VCPU_STAT(io_exits) }, |
| 121 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 122 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 123 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 124 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 125 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 126 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 127 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 128 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 129 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 130 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 131 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 132 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 133 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 134 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 135 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 136 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 137 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 138 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 139 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 140 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 141 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 142 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 143 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 144 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 145 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 146 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 147 | { NULL } |
| 148 | }; |
| 149 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 150 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 151 | { |
| 152 | unsigned slot; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 153 | struct kvm_shared_msrs *locals |
| 154 | = container_of(urn, struct kvm_shared_msrs, urn); |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 155 | struct kvm_shared_msr_values *values; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 156 | |
| 157 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 158 | values = &locals->values[slot]; |
| 159 | if (values->host != values->curr) { |
| 160 | wrmsrl(shared_msrs_global.msrs[slot], values->host); |
| 161 | values->curr = values->host; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | locals->registered = false; |
| 165 | user_return_notifier_unregister(urn); |
| 166 | } |
| 167 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 168 | static void shared_msr_update(unsigned slot, u32 msr) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 169 | { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 170 | struct kvm_shared_msrs *smsr; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 171 | u64 value; |
| 172 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 173 | smsr = &__get_cpu_var(shared_msrs); |
| 174 | /* only read, and nobody should modify it at this time, |
| 175 | * so don't need lock */ |
| 176 | if (slot >= shared_msrs_global.nr) { |
| 177 | printk(KERN_ERR "kvm: invalid MSR slot!"); |
| 178 | return; |
| 179 | } |
| 180 | rdmsrl_safe(msr, &value); |
| 181 | smsr->values[slot].host = value; |
| 182 | smsr->values[slot].curr = value; |
| 183 | } |
| 184 | |
| 185 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
| 186 | { |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 187 | if (slot >= shared_msrs_global.nr) |
| 188 | shared_msrs_global.nr = slot + 1; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 189 | shared_msrs_global.msrs[slot] = msr; |
| 190 | /* we need ensured the shared_msr_global have been updated */ |
| 191 | smp_wmb(); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 192 | } |
| 193 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); |
| 194 | |
| 195 | static void kvm_shared_msr_cpu_online(void) |
| 196 | { |
| 197 | unsigned i; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 198 | |
| 199 | for (i = 0; i < shared_msrs_global.nr; ++i) |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 200 | shared_msr_update(i, shared_msrs_global.msrs[i]); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 201 | } |
| 202 | |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 203 | void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 204 | { |
| 205 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 206 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 207 | if (((value ^ smsr->values[slot].curr) & mask) == 0) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 208 | return; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 209 | smsr->values[slot].curr = value; |
| 210 | wrmsrl(shared_msrs_global.msrs[slot], value); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 211 | if (!smsr->registered) { |
| 212 | smsr->urn.on_user_return = kvm_on_user_return; |
| 213 | user_return_notifier_register(&smsr->urn); |
| 214 | smsr->registered = true; |
| 215 | } |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); |
| 218 | |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 219 | static void drop_user_return_notifiers(void *ignore) |
| 220 | { |
| 221 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 222 | |
| 223 | if (smsr->registered) |
| 224 | kvm_on_user_return(&smsr->urn); |
| 225 | } |
| 226 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 227 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 228 | { |
| 229 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 230 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 231 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 232 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 233 | } |
| 234 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 235 | |
| 236 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 237 | { |
| 238 | /* TODO: reserve bits check */ |
| 239 | if (irqchip_in_kernel(vcpu->kvm)) |
| 240 | kvm_lapic_set_base(vcpu, data); |
| 241 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 242 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 243 | } |
| 244 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 245 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 246 | #define EXCPT_BENIGN 0 |
| 247 | #define EXCPT_CONTRIBUTORY 1 |
| 248 | #define EXCPT_PF 2 |
| 249 | |
| 250 | static int exception_class(int vector) |
| 251 | { |
| 252 | switch (vector) { |
| 253 | case PF_VECTOR: |
| 254 | return EXCPT_PF; |
| 255 | case DE_VECTOR: |
| 256 | case TS_VECTOR: |
| 257 | case NP_VECTOR: |
| 258 | case SS_VECTOR: |
| 259 | case GP_VECTOR: |
| 260 | return EXCPT_CONTRIBUTORY; |
| 261 | default: |
| 262 | break; |
| 263 | } |
| 264 | return EXCPT_BENIGN; |
| 265 | } |
| 266 | |
| 267 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 268 | unsigned nr, bool has_error, u32 error_code, |
| 269 | bool reinject) |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 270 | { |
| 271 | u32 prev_nr; |
| 272 | int class1, class2; |
| 273 | |
| 274 | if (!vcpu->arch.exception.pending) { |
| 275 | queue: |
| 276 | vcpu->arch.exception.pending = true; |
| 277 | vcpu->arch.exception.has_error_code = has_error; |
| 278 | vcpu->arch.exception.nr = nr; |
| 279 | vcpu->arch.exception.error_code = error_code; |
Joerg Roedel | 3f0fd29 | 2010-05-05 16:04:41 +0200 | [diff] [blame] | 280 | vcpu->arch.exception.reinject = reinject; |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
| 284 | /* to check exception */ |
| 285 | prev_nr = vcpu->arch.exception.nr; |
| 286 | if (prev_nr == DF_VECTOR) { |
| 287 | /* triple fault -> shutdown */ |
| 288 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 289 | return; |
| 290 | } |
| 291 | class1 = exception_class(prev_nr); |
| 292 | class2 = exception_class(nr); |
| 293 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 294 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
| 295 | /* generate double fault per SDM Table 5-5 */ |
| 296 | vcpu->arch.exception.pending = true; |
| 297 | vcpu->arch.exception.has_error_code = true; |
| 298 | vcpu->arch.exception.nr = DF_VECTOR; |
| 299 | vcpu->arch.exception.error_code = 0; |
| 300 | } else |
| 301 | /* replace previous exception with a new one in a hope |
| 302 | that instruction re-execution will regenerate lost |
| 303 | exception */ |
| 304 | goto queue; |
| 305 | } |
| 306 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 307 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 308 | { |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 309 | kvm_multiple_exception(vcpu, nr, false, 0, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 310 | } |
| 311 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 312 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 313 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 314 | { |
| 315 | kvm_multiple_exception(vcpu, nr, false, 0, true); |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); |
| 318 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 319 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 320 | u32 error_code) |
| 321 | { |
| 322 | ++vcpu->stat.pf_guest; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 323 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 324 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 325 | } |
| 326 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 327 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 328 | { |
| 329 | vcpu->arch.nmi_pending = 1; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 332 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 333 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 334 | { |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 335 | kvm_multiple_exception(vcpu, nr, true, error_code, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 336 | } |
| 337 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 338 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 339 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 340 | { |
| 341 | kvm_multiple_exception(vcpu, nr, true, error_code, true); |
| 342 | } |
| 343 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); |
| 344 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 345 | /* |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 346 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 347 | * a #GP and return false. |
| 348 | */ |
| 349 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 350 | { |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 351 | if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl) |
| 352 | return true; |
| 353 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 354 | return false; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 355 | } |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 356 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Load the pae pdptrs. Return true is they are all valid. |
| 360 | */ |
| 361 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 362 | { |
| 363 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 364 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 365 | int i; |
| 366 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 367 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 368 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 369 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 370 | offset * sizeof(u64), sizeof(pdpte)); |
| 371 | if (ret < 0) { |
| 372 | ret = 0; |
| 373 | goto out; |
| 374 | } |
| 375 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 376 | if (is_present_gpte(pdpte[i]) && |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 377 | (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 378 | ret = 0; |
| 379 | goto out; |
| 380 | } |
| 381 | } |
| 382 | ret = 1; |
| 383 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 384 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 385 | __set_bit(VCPU_EXREG_PDPTR, |
| 386 | (unsigned long *)&vcpu->arch.regs_avail); |
| 387 | __set_bit(VCPU_EXREG_PDPTR, |
| 388 | (unsigned long *)&vcpu->arch.regs_dirty); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 389 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 390 | |
| 391 | return ret; |
| 392 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 393 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 394 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 395 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 396 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 397 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 398 | bool changed = true; |
| 399 | int r; |
| 400 | |
| 401 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 402 | return false; |
| 403 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 404 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 405 | (unsigned long *)&vcpu->arch.regs_avail)) |
| 406 | return true; |
| 407 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 408 | 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] | 409 | if (r < 0) |
| 410 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 411 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 412 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 413 | |
| 414 | return changed; |
| 415 | } |
| 416 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 417 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 418 | { |
Avi Kivity | f9a48e6 | 2010-01-06 19:10:22 +0200 | [diff] [blame] | 419 | cr0 |= X86_CR0_ET; |
| 420 | |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 421 | #ifdef CONFIG_X86_64 |
| 422 | if (cr0 & 0xffffffff00000000UL) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 423 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 424 | return; |
| 425 | } |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 426 | #endif |
| 427 | |
| 428 | cr0 &= ~CR0_RESERVED_BITS; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 429 | |
| 430 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 431 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 436 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
| 440 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 441 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 442 | if ((vcpu->arch.efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 443 | int cs_db, cs_l; |
| 444 | |
| 445 | if (!is_pae(vcpu)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 446 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 450 | if (cs_l) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 451 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 452 | return; |
| 453 | |
| 454 | } |
| 455 | } else |
| 456 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 457 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 458 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | } |
| 463 | |
| 464 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 465 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 466 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 467 | return; |
| 468 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 469 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 470 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 471 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 472 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 473 | kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 474 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 475 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 476 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 477 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 478 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 479 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 480 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE; |
| 481 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 482 | if (cr4 & CR4_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 483 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
| 487 | if (is_long_mode(vcpu)) { |
| 488 | if (!(cr4 & X86_CR4_PAE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 489 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 490 | return; |
| 491 | } |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 492 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 493 | && ((cr4 ^ old_cr4) & pdptr_bits) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 494 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 495 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
| 499 | if (cr4 & X86_CR4_VMXE) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 500 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 504 | vcpu->arch.cr4 = cr4; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 505 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 506 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 507 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 508 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 509 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 510 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 511 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 512 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 513 | kvm_mmu_flush_tlb(vcpu); |
| 514 | return; |
| 515 | } |
| 516 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 517 | if (is_long_mode(vcpu)) { |
| 518 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 519 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 520 | return; |
| 521 | } |
| 522 | } else { |
| 523 | if (is_pae(vcpu)) { |
| 524 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 525 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 529 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | } |
| 533 | /* |
| 534 | * We don't check reserved bits in nonpae mode, because |
| 535 | * this isn't enforced, and VMware depends on this. |
| 536 | */ |
| 537 | } |
| 538 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 539 | /* |
| 540 | * Does the new cr3 value map to physical memory? (Note, we |
| 541 | * catch an invalid cr3 even in real-mode, because it would |
| 542 | * cause trouble later on when we turn on paging anyway.) |
| 543 | * |
| 544 | * A real CPU would silently accept an invalid cr3 and would |
| 545 | * attempt to use it - with largely undefined (and often hard |
| 546 | * to debug) behavior on the guest side. |
| 547 | */ |
| 548 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 549 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 550 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 551 | vcpu->arch.cr3 = cr3; |
| 552 | vcpu->arch.mmu.new_cr3(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 553 | } |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 554 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 555 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 556 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 557 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 558 | { |
| 559 | if (cr8 & CR8_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 560 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 561 | return; |
| 562 | } |
| 563 | if (irqchip_in_kernel(vcpu->kvm)) |
| 564 | kvm_lapic_set_tpr(vcpu, cr8); |
| 565 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 566 | vcpu->arch.cr8 = cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 567 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 568 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 569 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 570 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 571 | { |
| 572 | if (irqchip_in_kernel(vcpu->kvm)) |
| 573 | return kvm_lapic_get_cr8(vcpu); |
| 574 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 575 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 576 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 577 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 578 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 579 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
| 580 | { |
| 581 | switch (dr) { |
| 582 | case 0 ... 3: |
| 583 | vcpu->arch.db[dr] = val; |
| 584 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 585 | vcpu->arch.eff_db[dr] = val; |
| 586 | break; |
| 587 | case 4: |
| 588 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) { |
| 589 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 590 | return 1; |
| 591 | } |
| 592 | /* fall through */ |
| 593 | case 6: |
| 594 | if (val & 0xffffffff00000000ULL) { |
| 595 | kvm_inject_gp(vcpu, 0); |
| 596 | return 1; |
| 597 | } |
| 598 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1; |
| 599 | break; |
| 600 | case 5: |
| 601 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) { |
| 602 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 603 | return 1; |
| 604 | } |
| 605 | /* fall through */ |
| 606 | default: /* 7 */ |
| 607 | if (val & 0xffffffff00000000ULL) { |
| 608 | kvm_inject_gp(vcpu, 0); |
| 609 | return 1; |
| 610 | } |
| 611 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
| 612 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 613 | kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7); |
| 614 | vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK); |
| 615 | } |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
| 622 | |
| 623 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
| 624 | { |
| 625 | switch (dr) { |
| 626 | case 0 ... 3: |
| 627 | *val = vcpu->arch.db[dr]; |
| 628 | break; |
| 629 | case 4: |
| 630 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) { |
| 631 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 632 | return 1; |
| 633 | } |
| 634 | /* fall through */ |
| 635 | case 6: |
| 636 | *val = vcpu->arch.dr6; |
| 637 | break; |
| 638 | case 5: |
| 639 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) { |
| 640 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 641 | return 1; |
| 642 | } |
| 643 | /* fall through */ |
| 644 | default: /* 7 */ |
| 645 | *val = vcpu->arch.dr7; |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
| 652 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 653 | static inline u32 bit(int bitno) |
| 654 | { |
| 655 | return 1 << (bitno & 31); |
| 656 | } |
| 657 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 658 | /* |
| 659 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 660 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 661 | * |
| 662 | * This list is modified at module load time to reflect the |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 663 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
| 664 | * kvm-specific. Those are put in the beginning of the list. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 665 | */ |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 666 | |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 667 | #define KVM_SAVE_MSRS_BEGIN 7 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 668 | static u32 msrs_to_save[] = { |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 669 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 670 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 671 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 672 | HV_X64_MSR_APIC_ASSIST_PAGE, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 673 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 674 | MSR_K6_STAR, |
| 675 | #ifdef CONFIG_X86_64 |
| 676 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 677 | #endif |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 678 | MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 679 | }; |
| 680 | |
| 681 | static unsigned num_msrs_to_save; |
| 682 | |
| 683 | static u32 emulated_msrs[] = { |
| 684 | MSR_IA32_MISC_ENABLE, |
| 685 | }; |
| 686 | |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 687 | static int set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 688 | { |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 689 | if (efer & efer_reserved_bits) |
| 690 | return 1; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 691 | |
| 692 | if (is_paging(vcpu) |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 693 | && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) |
| 694 | return 1; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 695 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 696 | if (efer & EFER_FFXSR) { |
| 697 | struct kvm_cpuid_entry2 *feat; |
| 698 | |
| 699 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 700 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) |
| 701 | return 1; |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 704 | if (efer & EFER_SVME) { |
| 705 | struct kvm_cpuid_entry2 *feat; |
| 706 | |
| 707 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 708 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) |
| 709 | return 1; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 712 | kvm_x86_ops->set_efer(vcpu, efer); |
| 713 | |
| 714 | efer &= ~EFER_LMA; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 715 | efer |= vcpu->arch.efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 716 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 717 | vcpu->arch.efer = efer; |
Avi Kivity | 9645bb56 | 2009-03-31 11:31:54 +0300 | [diff] [blame] | 718 | |
| 719 | vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled; |
| 720 | kvm_mmu_reset_context(vcpu); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 721 | |
| 722 | return 0; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 725 | void kvm_enable_efer_bits(u64 mask) |
| 726 | { |
| 727 | efer_reserved_bits &= ~mask; |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 730 | |
| 731 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 732 | /* |
| 733 | * Writes msr value into into the appropriate "register". |
| 734 | * Returns 0 on success, non-0 otherwise. |
| 735 | * Assumes vcpu_load() was already called. |
| 736 | */ |
| 737 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 738 | { |
| 739 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 740 | } |
| 741 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 742 | /* |
| 743 | * Adapt set_msr() to msr_io()'s calling convention |
| 744 | */ |
| 745 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 746 | { |
| 747 | return kvm_set_msr(vcpu, index, *data); |
| 748 | } |
| 749 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 750 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 751 | { |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 752 | int version; |
| 753 | int r; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 754 | struct pvclock_wall_clock wc; |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 755 | struct timespec boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 756 | |
| 757 | if (!wall_clock) |
| 758 | return; |
| 759 | |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 760 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
| 761 | if (r) |
| 762 | return; |
| 763 | |
| 764 | if (version & 1) |
| 765 | ++version; /* first time write, random junk */ |
| 766 | |
| 767 | ++version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 768 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 769 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 770 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 771 | /* |
| 772 | * The guest calculates current wall clock time by adding |
| 773 | * system time (updated by kvm_write_guest_time below) to the |
| 774 | * wall clock specified here. guest system time equals host |
| 775 | * system time for us, thus we must fill in host boot time here. |
| 776 | */ |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 777 | getboottime(&boot); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 778 | |
| 779 | wc.sec = boot.tv_sec; |
| 780 | wc.nsec = boot.tv_nsec; |
| 781 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 782 | |
| 783 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 784 | |
| 785 | version++; |
| 786 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 787 | } |
| 788 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 789 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 790 | { |
| 791 | uint32_t quotient, remainder; |
| 792 | |
| 793 | /* Don't try to replace with do_div(), this one calculates |
| 794 | * "(dividend << 32) / divisor" */ |
| 795 | __asm__ ( "divl %4" |
| 796 | : "=a" (quotient), "=d" (remainder) |
| 797 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 798 | return quotient; |
| 799 | } |
| 800 | |
| 801 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 802 | { |
| 803 | uint64_t nsecs = 1000000000LL; |
| 804 | int32_t shift = 0; |
| 805 | uint64_t tps64; |
| 806 | uint32_t tps32; |
| 807 | |
| 808 | tps64 = tsc_khz * 1000LL; |
| 809 | while (tps64 > nsecs*2) { |
| 810 | tps64 >>= 1; |
| 811 | shift--; |
| 812 | } |
| 813 | |
| 814 | tps32 = (uint32_t)tps64; |
| 815 | while (tps32 <= (uint32_t)nsecs) { |
| 816 | tps32 <<= 1; |
| 817 | shift++; |
| 818 | } |
| 819 | |
| 820 | hv_clock->tsc_shift = shift; |
| 821 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 822 | |
| 823 | 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] | 824 | __func__, tsc_khz, hv_clock->tsc_shift, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 825 | hv_clock->tsc_to_system_mul); |
| 826 | } |
| 827 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 828 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
| 829 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 830 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
| 831 | { |
| 832 | struct timespec ts; |
| 833 | unsigned long flags; |
| 834 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 835 | void *shared_kaddr; |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 836 | unsigned long this_tsc_khz; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 837 | |
| 838 | if ((!vcpu->time_page)) |
| 839 | return; |
| 840 | |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 841 | this_tsc_khz = get_cpu_var(cpu_tsc_khz); |
| 842 | if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) { |
| 843 | kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock); |
| 844 | vcpu->hv_clock_tsc_khz = this_tsc_khz; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 845 | } |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 846 | put_cpu_var(cpu_tsc_khz); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 847 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 848 | /* Keep irq disabled to prevent changes to the clock */ |
| 849 | local_irq_save(flags); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 850 | kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 851 | ktime_get_ts(&ts); |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 852 | monotonic_to_bootbased(&ts); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 853 | local_irq_restore(flags); |
| 854 | |
| 855 | /* With all the info we got, fill in the values */ |
| 856 | |
| 857 | vcpu->hv_clock.system_time = ts.tv_nsec + |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 858 | (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset; |
| 859 | |
Glauber Costa | 371bcf6 | 2010-05-11 12:17:46 -0400 | [diff] [blame^] | 860 | vcpu->hv_clock.flags = 0; |
| 861 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 862 | /* |
| 863 | * The interface expects us to write an even number signaling that the |
| 864 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 865 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 866 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 867 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 868 | |
| 869 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 870 | |
| 871 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 872 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 873 | |
| 874 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 875 | |
| 876 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
| 877 | } |
| 878 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 879 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) |
| 880 | { |
| 881 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 882 | |
| 883 | if (!vcpu->time_page) |
| 884 | return 0; |
| 885 | set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests); |
| 886 | return 1; |
| 887 | } |
| 888 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 889 | static bool msr_mtrr_valid(unsigned msr) |
| 890 | { |
| 891 | switch (msr) { |
| 892 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 893 | case MSR_MTRRfix64K_00000: |
| 894 | case MSR_MTRRfix16K_80000: |
| 895 | case MSR_MTRRfix16K_A0000: |
| 896 | case MSR_MTRRfix4K_C0000: |
| 897 | case MSR_MTRRfix4K_C8000: |
| 898 | case MSR_MTRRfix4K_D0000: |
| 899 | case MSR_MTRRfix4K_D8000: |
| 900 | case MSR_MTRRfix4K_E0000: |
| 901 | case MSR_MTRRfix4K_E8000: |
| 902 | case MSR_MTRRfix4K_F0000: |
| 903 | case MSR_MTRRfix4K_F8000: |
| 904 | case MSR_MTRRdefType: |
| 905 | case MSR_IA32_CR_PAT: |
| 906 | return true; |
| 907 | case 0x2f8: |
| 908 | return true; |
| 909 | } |
| 910 | return false; |
| 911 | } |
| 912 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 913 | static bool valid_pat_type(unsigned t) |
| 914 | { |
| 915 | return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */ |
| 916 | } |
| 917 | |
| 918 | static bool valid_mtrr_type(unsigned t) |
| 919 | { |
| 920 | return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */ |
| 921 | } |
| 922 | |
| 923 | static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 924 | { |
| 925 | int i; |
| 926 | |
| 927 | if (!msr_mtrr_valid(msr)) |
| 928 | return false; |
| 929 | |
| 930 | if (msr == MSR_IA32_CR_PAT) { |
| 931 | for (i = 0; i < 8; i++) |
| 932 | if (!valid_pat_type((data >> (i * 8)) & 0xff)) |
| 933 | return false; |
| 934 | return true; |
| 935 | } else if (msr == MSR_MTRRdefType) { |
| 936 | if (data & ~0xcff) |
| 937 | return false; |
| 938 | return valid_mtrr_type(data & 0xff); |
| 939 | } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { |
| 940 | for (i = 0; i < 8 ; i++) |
| 941 | if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) |
| 942 | return false; |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | /* variable MTRRs */ |
| 947 | return valid_mtrr_type(data & 0xff); |
| 948 | } |
| 949 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 950 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 951 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 952 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 953 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 954 | if (!mtrr_valid(vcpu, msr, data)) |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 955 | return 1; |
| 956 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 957 | if (msr == MSR_MTRRdefType) { |
| 958 | vcpu->arch.mtrr_state.def_type = data; |
| 959 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; |
| 960 | } else if (msr == MSR_MTRRfix64K_00000) |
| 961 | p[0] = data; |
| 962 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 963 | p[1 + msr - MSR_MTRRfix16K_80000] = data; |
| 964 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 965 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; |
| 966 | else if (msr == MSR_IA32_CR_PAT) |
| 967 | vcpu->arch.pat = data; |
| 968 | else { /* Variable MTRRs */ |
| 969 | int idx, is_mtrr_mask; |
| 970 | u64 *pt; |
| 971 | |
| 972 | idx = (msr - 0x200) / 2; |
| 973 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 974 | if (!is_mtrr_mask) |
| 975 | pt = |
| 976 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 977 | else |
| 978 | pt = |
| 979 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 980 | *pt = data; |
| 981 | } |
| 982 | |
| 983 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 984 | return 0; |
| 985 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 986 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 987 | static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 988 | { |
| 989 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 990 | unsigned bank_num = mcg_cap & 0xff; |
| 991 | |
| 992 | switch (msr) { |
| 993 | case MSR_IA32_MCG_STATUS: |
| 994 | vcpu->arch.mcg_status = data; |
| 995 | break; |
| 996 | case MSR_IA32_MCG_CTL: |
| 997 | if (!(mcg_cap & MCG_CTL_P)) |
| 998 | return 1; |
| 999 | if (data != 0 && data != ~(u64)0) |
| 1000 | return -1; |
| 1001 | vcpu->arch.mcg_ctl = data; |
| 1002 | break; |
| 1003 | default: |
| 1004 | if (msr >= MSR_IA32_MC0_CTL && |
| 1005 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1006 | u32 offset = msr - MSR_IA32_MC0_CTL; |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 1007 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 1008 | * some Linux kernels though clear bit 10 in bank 4 to |
| 1009 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 1010 | * this to avoid an uncatched #GP in the guest |
| 1011 | */ |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1012 | if ((offset & 0x3) == 0 && |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 1013 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1014 | return -1; |
| 1015 | vcpu->arch.mce_banks[offset] = data; |
| 1016 | break; |
| 1017 | } |
| 1018 | return 1; |
| 1019 | } |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1023 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 1024 | { |
| 1025 | struct kvm *kvm = vcpu->kvm; |
| 1026 | int lm = is_long_mode(vcpu); |
| 1027 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 1028 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 1029 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 1030 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 1031 | u32 page_num = data & ~PAGE_MASK; |
| 1032 | u64 page_addr = data & PAGE_MASK; |
| 1033 | u8 *page; |
| 1034 | int r; |
| 1035 | |
| 1036 | r = -E2BIG; |
| 1037 | if (page_num >= blob_size) |
| 1038 | goto out; |
| 1039 | r = -ENOMEM; |
| 1040 | page = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1041 | if (!page) |
| 1042 | goto out; |
| 1043 | r = -EFAULT; |
| 1044 | if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE)) |
| 1045 | goto out_free; |
| 1046 | if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE)) |
| 1047 | goto out_free; |
| 1048 | r = 0; |
| 1049 | out_free: |
| 1050 | kfree(page); |
| 1051 | out: |
| 1052 | return r; |
| 1053 | } |
| 1054 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1055 | static bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 1056 | { |
| 1057 | return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 1058 | } |
| 1059 | |
| 1060 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 1061 | { |
| 1062 | bool r = false; |
| 1063 | switch (msr) { |
| 1064 | case HV_X64_MSR_GUEST_OS_ID: |
| 1065 | case HV_X64_MSR_HYPERCALL: |
| 1066 | r = true; |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | return r; |
| 1071 | } |
| 1072 | |
| 1073 | static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1074 | { |
| 1075 | struct kvm *kvm = vcpu->kvm; |
| 1076 | |
| 1077 | switch (msr) { |
| 1078 | case HV_X64_MSR_GUEST_OS_ID: |
| 1079 | kvm->arch.hv_guest_os_id = data; |
| 1080 | /* setting guest os id to zero disables hypercall page */ |
| 1081 | if (!kvm->arch.hv_guest_os_id) |
| 1082 | kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 1083 | break; |
| 1084 | case HV_X64_MSR_HYPERCALL: { |
| 1085 | u64 gfn; |
| 1086 | unsigned long addr; |
| 1087 | u8 instructions[4]; |
| 1088 | |
| 1089 | /* if guest os id is not set hypercall should remain disabled */ |
| 1090 | if (!kvm->arch.hv_guest_os_id) |
| 1091 | break; |
| 1092 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1093 | kvm->arch.hv_hypercall = data; |
| 1094 | break; |
| 1095 | } |
| 1096 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 1097 | addr = gfn_to_hva(kvm, gfn); |
| 1098 | if (kvm_is_error_hva(addr)) |
| 1099 | return 1; |
| 1100 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 1101 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 1102 | if (copy_to_user((void __user *)addr, instructions, 4)) |
| 1103 | return 1; |
| 1104 | kvm->arch.hv_hypercall = data; |
| 1105 | break; |
| 1106 | } |
| 1107 | default: |
| 1108 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1109 | "data 0x%llx\n", msr, data); |
| 1110 | return 1; |
| 1111 | } |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1116 | { |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1117 | switch (msr) { |
| 1118 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 1119 | unsigned long addr; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1120 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1121 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 1122 | vcpu->arch.hv_vapic = data; |
| 1123 | break; |
| 1124 | } |
| 1125 | addr = gfn_to_hva(vcpu->kvm, data >> |
| 1126 | HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT); |
| 1127 | if (kvm_is_error_hva(addr)) |
| 1128 | return 1; |
| 1129 | if (clear_user((void __user *)addr, PAGE_SIZE)) |
| 1130 | return 1; |
| 1131 | vcpu->arch.hv_vapic = data; |
| 1132 | break; |
| 1133 | } |
| 1134 | case HV_X64_MSR_EOI: |
| 1135 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1136 | case HV_X64_MSR_ICR: |
| 1137 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1138 | case HV_X64_MSR_TPR: |
| 1139 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
| 1140 | default: |
| 1141 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1142 | "data 0x%llx\n", msr, data); |
| 1143 | return 1; |
| 1144 | } |
| 1145 | |
| 1146 | return 0; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1149 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1150 | { |
| 1151 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1152 | case MSR_EFER: |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 1153 | return set_efer(vcpu, data); |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1154 | case MSR_K7_HWCR: |
| 1155 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
Joerg Roedel | 8249402 | 2010-02-24 18:59:16 +0100 | [diff] [blame] | 1156 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1157 | if (data != 0) { |
| 1158 | pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 1159 | data); |
| 1160 | return 1; |
| 1161 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1162 | break; |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1163 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 1164 | if (data != 0) { |
| 1165 | pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 1166 | "0x%llx\n", data); |
| 1167 | return 1; |
| 1168 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1169 | break; |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1170 | case MSR_AMD64_NB_CFG: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 1171 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1172 | case MSR_IA32_DEBUGCTLMSR: |
| 1173 | if (!data) { |
| 1174 | /* We support the non-activated case already */ |
| 1175 | break; |
| 1176 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 1177 | /* Values other than LBR and BTF are vendor-specific, |
| 1178 | thus reserved and should throw a #GP */ |
| 1179 | return 1; |
| 1180 | } |
| 1181 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 1182 | __func__, data); |
| 1183 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1184 | case MSR_IA32_UCODE_REV: |
| 1185 | case MSR_IA32_UCODE_WRITE: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1186 | case MSR_VM_HSAVE_PA: |
Andre Przywara | 6098ca9 | 2009-07-03 16:00:14 +0200 | [diff] [blame] | 1187 | case MSR_AMD64_PATCH_LOADER: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1188 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1189 | case 0x200 ... 0x2ff: |
| 1190 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1191 | case MSR_IA32_APICBASE: |
| 1192 | kvm_set_apic_base(vcpu, data); |
| 1193 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1194 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1195 | return kvm_x2apic_msr_write(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1196 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1197 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1198 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1199 | case MSR_KVM_WALL_CLOCK_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1200 | case MSR_KVM_WALL_CLOCK: |
| 1201 | vcpu->kvm->arch.wall_clock = data; |
| 1202 | kvm_write_wall_clock(vcpu->kvm, data); |
| 1203 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1204 | case MSR_KVM_SYSTEM_TIME_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1205 | case MSR_KVM_SYSTEM_TIME: { |
| 1206 | if (vcpu->arch.time_page) { |
| 1207 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 1208 | vcpu->arch.time_page = NULL; |
| 1209 | } |
| 1210 | |
| 1211 | vcpu->arch.time = data; |
| 1212 | |
| 1213 | /* we verify if the enable bit is set... */ |
| 1214 | if (!(data & 1)) |
| 1215 | break; |
| 1216 | |
| 1217 | /* ...but clean it before doing the actual write */ |
| 1218 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 1219 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1220 | vcpu->arch.time_page = |
| 1221 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1222 | |
| 1223 | if (is_error_page(vcpu->arch.time_page)) { |
| 1224 | kvm_release_page_clean(vcpu->arch.time_page); |
| 1225 | vcpu->arch.time_page = NULL; |
| 1226 | } |
| 1227 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1228 | kvm_request_guest_time_update(vcpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1229 | break; |
| 1230 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1231 | case MSR_IA32_MCG_CTL: |
| 1232 | case MSR_IA32_MCG_STATUS: |
| 1233 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1234 | return set_msr_mce(vcpu, msr, data); |
Andre Przywara | 71db602 | 2009-06-12 22:01:29 +0200 | [diff] [blame] | 1235 | |
| 1236 | /* Performance counters are not protected by a CPUID bit, |
| 1237 | * so we should check all of them in the generic path for the sake of |
| 1238 | * cross vendor migration. |
| 1239 | * Writing a zero into the event select MSRs disables them, |
| 1240 | * which we perfectly emulate ;-). Any other value should be at least |
| 1241 | * reported, some guests depend on them. |
| 1242 | */ |
| 1243 | case MSR_P6_EVNTSEL0: |
| 1244 | case MSR_P6_EVNTSEL1: |
| 1245 | case MSR_K7_EVNTSEL0: |
| 1246 | case MSR_K7_EVNTSEL1: |
| 1247 | case MSR_K7_EVNTSEL2: |
| 1248 | case MSR_K7_EVNTSEL3: |
| 1249 | if (data != 0) |
| 1250 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1251 | "0x%x data 0x%llx\n", msr, data); |
| 1252 | break; |
| 1253 | /* at least RHEL 4 unconditionally writes to the perfctr registers, |
| 1254 | * so we ignore writes to make it happy. |
| 1255 | */ |
| 1256 | case MSR_P6_PERFCTR0: |
| 1257 | case MSR_P6_PERFCTR1: |
| 1258 | case MSR_K7_PERFCTR0: |
| 1259 | case MSR_K7_PERFCTR1: |
| 1260 | case MSR_K7_PERFCTR2: |
| 1261 | case MSR_K7_PERFCTR3: |
| 1262 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1263 | "0x%x data 0x%llx\n", msr, data); |
| 1264 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1265 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1266 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1267 | int r; |
| 1268 | mutex_lock(&vcpu->kvm->lock); |
| 1269 | r = set_msr_hyperv_pw(vcpu, msr, data); |
| 1270 | mutex_unlock(&vcpu->kvm->lock); |
| 1271 | return r; |
| 1272 | } else |
| 1273 | return set_msr_hyperv(vcpu, msr, data); |
| 1274 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1275 | default: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1276 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 1277 | return xen_hvm_config(vcpu, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1278 | if (!ignore_msrs) { |
| 1279 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", |
| 1280 | msr, data); |
| 1281 | return 1; |
| 1282 | } else { |
| 1283 | pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", |
| 1284 | msr, data); |
| 1285 | break; |
| 1286 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1287 | } |
| 1288 | return 0; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1291 | |
| 1292 | |
| 1293 | /* |
| 1294 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1295 | * Returns 0 on success, non-0 otherwise. |
| 1296 | * Assumes vcpu_load() was already called. |
| 1297 | */ |
| 1298 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1299 | { |
| 1300 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 1301 | } |
| 1302 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1303 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1304 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1305 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 1306 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1307 | if (!msr_mtrr_valid(msr)) |
| 1308 | return 1; |
| 1309 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1310 | if (msr == MSR_MTRRdefType) |
| 1311 | *pdata = vcpu->arch.mtrr_state.def_type + |
| 1312 | (vcpu->arch.mtrr_state.enabled << 10); |
| 1313 | else if (msr == MSR_MTRRfix64K_00000) |
| 1314 | *pdata = p[0]; |
| 1315 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 1316 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; |
| 1317 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 1318 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; |
| 1319 | else if (msr == MSR_IA32_CR_PAT) |
| 1320 | *pdata = vcpu->arch.pat; |
| 1321 | else { /* Variable MTRRs */ |
| 1322 | int idx, is_mtrr_mask; |
| 1323 | u64 *pt; |
| 1324 | |
| 1325 | idx = (msr - 0x200) / 2; |
| 1326 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 1327 | if (!is_mtrr_mask) |
| 1328 | pt = |
| 1329 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 1330 | else |
| 1331 | pt = |
| 1332 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 1333 | *pdata = *pt; |
| 1334 | } |
| 1335 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
| 1338 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1339 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1340 | { |
| 1341 | u64 data; |
| 1342 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 1343 | unsigned bank_num = mcg_cap & 0xff; |
| 1344 | |
| 1345 | switch (msr) { |
| 1346 | case MSR_IA32_P5_MC_ADDR: |
| 1347 | case MSR_IA32_P5_MC_TYPE: |
| 1348 | data = 0; |
| 1349 | break; |
| 1350 | case MSR_IA32_MCG_CAP: |
| 1351 | data = vcpu->arch.mcg_cap; |
| 1352 | break; |
| 1353 | case MSR_IA32_MCG_CTL: |
| 1354 | if (!(mcg_cap & MCG_CTL_P)) |
| 1355 | return 1; |
| 1356 | data = vcpu->arch.mcg_ctl; |
| 1357 | break; |
| 1358 | case MSR_IA32_MCG_STATUS: |
| 1359 | data = vcpu->arch.mcg_status; |
| 1360 | break; |
| 1361 | default: |
| 1362 | if (msr >= MSR_IA32_MC0_CTL && |
| 1363 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1364 | u32 offset = msr - MSR_IA32_MC0_CTL; |
| 1365 | data = vcpu->arch.mce_banks[offset]; |
| 1366 | break; |
| 1367 | } |
| 1368 | return 1; |
| 1369 | } |
| 1370 | *pdata = data; |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1374 | static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1375 | { |
| 1376 | u64 data = 0; |
| 1377 | struct kvm *kvm = vcpu->kvm; |
| 1378 | |
| 1379 | switch (msr) { |
| 1380 | case HV_X64_MSR_GUEST_OS_ID: |
| 1381 | data = kvm->arch.hv_guest_os_id; |
| 1382 | break; |
| 1383 | case HV_X64_MSR_HYPERCALL: |
| 1384 | data = kvm->arch.hv_hypercall; |
| 1385 | break; |
| 1386 | default: |
| 1387 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1388 | return 1; |
| 1389 | } |
| 1390 | |
| 1391 | *pdata = data; |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1396 | { |
| 1397 | u64 data = 0; |
| 1398 | |
| 1399 | switch (msr) { |
| 1400 | case HV_X64_MSR_VP_INDEX: { |
| 1401 | int r; |
| 1402 | struct kvm_vcpu *v; |
| 1403 | kvm_for_each_vcpu(r, v, vcpu->kvm) |
| 1404 | if (v == vcpu) |
| 1405 | data = r; |
| 1406 | break; |
| 1407 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1408 | case HV_X64_MSR_EOI: |
| 1409 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1410 | case HV_X64_MSR_ICR: |
| 1411 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1412 | case HV_X64_MSR_TPR: |
| 1413 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1414 | default: |
| 1415 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1416 | return 1; |
| 1417 | } |
| 1418 | *pdata = data; |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1422 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1423 | { |
| 1424 | u64 data; |
| 1425 | |
| 1426 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1427 | case MSR_IA32_PLATFORM_ID: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1428 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1429 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1430 | case MSR_IA32_DEBUGCTLMSR: |
| 1431 | case MSR_IA32_LASTBRANCHFROMIP: |
| 1432 | case MSR_IA32_LASTBRANCHTOIP: |
| 1433 | case MSR_IA32_LASTINTFROMIP: |
| 1434 | case MSR_IA32_LASTINTTOIP: |
Jaswinder Singh Rajput | 60af2ec | 2009-05-14 11:00:10 +0530 | [diff] [blame] | 1435 | case MSR_K8_SYSCFG: |
| 1436 | case MSR_K7_HWCR: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1437 | case MSR_VM_HSAVE_PA: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1438 | case MSR_P6_PERFCTR0: |
| 1439 | case MSR_P6_PERFCTR1: |
Amit Shah | 7fe29e0 | 2009-03-20 12:39:00 +0530 | [diff] [blame] | 1440 | case MSR_P6_EVNTSEL0: |
| 1441 | case MSR_P6_EVNTSEL1: |
Amit Shah | 9e69962 | 2009-06-15 13:25:34 +0530 | [diff] [blame] | 1442 | case MSR_K7_EVNTSEL0: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1443 | case MSR_K7_PERFCTR0: |
Andre Przywara | 1fdbd48 | 2009-06-24 12:44:34 +0200 | [diff] [blame] | 1444 | case MSR_K8_INT_PENDING_MSG: |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1445 | case MSR_AMD64_NB_CFG: |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1446 | case MSR_FAM10H_MMIO_CONF_BASE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1447 | data = 0; |
| 1448 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1449 | case MSR_MTRRcap: |
| 1450 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 1451 | break; |
| 1452 | case 0x200 ... 0x2ff: |
| 1453 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1454 | case 0xcd: /* fsb frequency */ |
| 1455 | data = 3; |
| 1456 | break; |
| 1457 | case MSR_IA32_APICBASE: |
| 1458 | data = kvm_get_apic_base(vcpu); |
| 1459 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1460 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1461 | return kvm_x2apic_msr_read(vcpu, msr, pdata); |
| 1462 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1463 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1464 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1465 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 1466 | case MSR_IA32_PERF_STATUS: |
| 1467 | /* TSC increment by tick */ |
| 1468 | data = 1000ULL; |
| 1469 | /* CPU multiplier */ |
| 1470 | data |= (((uint64_t)4ULL) << 40); |
| 1471 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1472 | case MSR_EFER: |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1473 | data = vcpu->arch.efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1474 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1475 | case MSR_KVM_WALL_CLOCK: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1476 | case MSR_KVM_WALL_CLOCK_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1477 | data = vcpu->kvm->arch.wall_clock; |
| 1478 | break; |
| 1479 | case MSR_KVM_SYSTEM_TIME: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1480 | case MSR_KVM_SYSTEM_TIME_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1481 | data = vcpu->arch.time; |
| 1482 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1483 | case MSR_IA32_P5_MC_ADDR: |
| 1484 | case MSR_IA32_P5_MC_TYPE: |
| 1485 | case MSR_IA32_MCG_CAP: |
| 1486 | case MSR_IA32_MCG_CTL: |
| 1487 | case MSR_IA32_MCG_STATUS: |
| 1488 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1489 | return get_msr_mce(vcpu, msr, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1490 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1491 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1492 | int r; |
| 1493 | mutex_lock(&vcpu->kvm->lock); |
| 1494 | r = get_msr_hyperv_pw(vcpu, msr, pdata); |
| 1495 | mutex_unlock(&vcpu->kvm->lock); |
| 1496 | return r; |
| 1497 | } else |
| 1498 | return get_msr_hyperv(vcpu, msr, pdata); |
| 1499 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1500 | default: |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1501 | if (!ignore_msrs) { |
| 1502 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 1503 | return 1; |
| 1504 | } else { |
| 1505 | pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr); |
| 1506 | data = 0; |
| 1507 | } |
| 1508 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1509 | } |
| 1510 | *pdata = data; |
| 1511 | return 0; |
| 1512 | } |
| 1513 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1514 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1515 | /* |
| 1516 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1517 | * |
| 1518 | * @return number of msrs set successfully. |
| 1519 | */ |
| 1520 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 1521 | struct kvm_msr_entry *entries, |
| 1522 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1523 | unsigned index, u64 *data)) |
| 1524 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1525 | int i, idx; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1526 | |
| 1527 | vcpu_load(vcpu); |
| 1528 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1529 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1530 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1531 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1532 | break; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1533 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1534 | |
| 1535 | vcpu_put(vcpu); |
| 1536 | |
| 1537 | return i; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1542 | * |
| 1543 | * @return number of msrs set successfully. |
| 1544 | */ |
| 1545 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 1546 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1547 | unsigned index, u64 *data), |
| 1548 | int writeback) |
| 1549 | { |
| 1550 | struct kvm_msrs msrs; |
| 1551 | struct kvm_msr_entry *entries; |
| 1552 | int r, n; |
| 1553 | unsigned size; |
| 1554 | |
| 1555 | r = -EFAULT; |
| 1556 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1557 | goto out; |
| 1558 | |
| 1559 | r = -E2BIG; |
| 1560 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1561 | goto out; |
| 1562 | |
| 1563 | r = -ENOMEM; |
| 1564 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 1565 | entries = vmalloc(size); |
| 1566 | if (!entries) |
| 1567 | goto out; |
| 1568 | |
| 1569 | r = -EFAULT; |
| 1570 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1571 | goto out_free; |
| 1572 | |
| 1573 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 1574 | if (r < 0) |
| 1575 | goto out_free; |
| 1576 | |
| 1577 | r = -EFAULT; |
| 1578 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1579 | goto out_free; |
| 1580 | |
| 1581 | r = n; |
| 1582 | |
| 1583 | out_free: |
| 1584 | vfree(entries); |
| 1585 | out: |
| 1586 | return r; |
| 1587 | } |
| 1588 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1589 | int kvm_dev_ioctl_check_extension(long ext) |
| 1590 | { |
| 1591 | int r; |
| 1592 | |
| 1593 | switch (ext) { |
| 1594 | case KVM_CAP_IRQCHIP: |
| 1595 | case KVM_CAP_HLT: |
| 1596 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1597 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1598 | case KVM_CAP_EXT_CPUID: |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1599 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1600 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1601 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1602 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 1603 | case KVM_CAP_SYNC_MMU: |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 1604 | case KVM_CAP_REINJECT_CONTROL: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 1605 | case KVM_CAP_IRQ_INJECT_STATUS: |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 1606 | case KVM_CAP_ASSIGN_DEV_IRQ: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1607 | case KVM_CAP_IRQFD: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 1608 | case KVM_CAP_IOEVENTFD: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 1609 | case KVM_CAP_PIT2: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 1610 | case KVM_CAP_PIT_STATE2: |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 1611 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1612 | case KVM_CAP_XEN_HVM: |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 1613 | case KVM_CAP_ADJUST_CLOCK: |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 1614 | case KVM_CAP_VCPU_EVENTS: |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1615 | case KVM_CAP_HYPERV: |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1616 | case KVM_CAP_HYPERV_VAPIC: |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 1617 | case KVM_CAP_HYPERV_SPIN: |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 1618 | case KVM_CAP_PCI_SEGMENT: |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 1619 | case KVM_CAP_DEBUGREGS: |
Jan Kiszka | d2be165 | 2010-02-23 17:47:57 +0100 | [diff] [blame] | 1620 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1621 | r = 1; |
| 1622 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 1623 | case KVM_CAP_COALESCED_MMIO: |
| 1624 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1625 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1626 | case KVM_CAP_VAPIC: |
| 1627 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 1628 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 1629 | case KVM_CAP_NR_VCPUS: |
| 1630 | r = KVM_MAX_VCPUS; |
| 1631 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 1632 | case KVM_CAP_NR_MEMSLOTS: |
| 1633 | r = KVM_MEMORY_SLOTS; |
| 1634 | break; |
Marcelo Tosatti | a68a6a7 | 2009-10-01 19:28:39 -0300 | [diff] [blame] | 1635 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 1636 | r = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 1637 | break; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1638 | case KVM_CAP_IOMMU: |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 1639 | r = iommu_found(); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1640 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1641 | case KVM_CAP_MCE: |
| 1642 | r = KVM_MAX_MCE_BANKS; |
| 1643 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1644 | default: |
| 1645 | r = 0; |
| 1646 | break; |
| 1647 | } |
| 1648 | return r; |
| 1649 | |
| 1650 | } |
| 1651 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1652 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1653 | unsigned int ioctl, unsigned long arg) |
| 1654 | { |
| 1655 | void __user *argp = (void __user *)arg; |
| 1656 | long r; |
| 1657 | |
| 1658 | switch (ioctl) { |
| 1659 | case KVM_GET_MSR_INDEX_LIST: { |
| 1660 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1661 | struct kvm_msr_list msr_list; |
| 1662 | unsigned n; |
| 1663 | |
| 1664 | r = -EFAULT; |
| 1665 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1666 | goto out; |
| 1667 | n = msr_list.nmsrs; |
| 1668 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1669 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1670 | goto out; |
| 1671 | r = -E2BIG; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1672 | if (n < msr_list.nmsrs) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1673 | goto out; |
| 1674 | r = -EFAULT; |
| 1675 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1676 | num_msrs_to_save * sizeof(u32))) |
| 1677 | goto out; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1678 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1679 | &emulated_msrs, |
| 1680 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1681 | goto out; |
| 1682 | r = 0; |
| 1683 | break; |
| 1684 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1685 | case KVM_GET_SUPPORTED_CPUID: { |
| 1686 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1687 | struct kvm_cpuid2 cpuid; |
| 1688 | |
| 1689 | r = -EFAULT; |
| 1690 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1691 | goto out; |
| 1692 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1693 | cpuid_arg->entries); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1694 | if (r) |
| 1695 | goto out; |
| 1696 | |
| 1697 | r = -EFAULT; |
| 1698 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1699 | goto out; |
| 1700 | r = 0; |
| 1701 | break; |
| 1702 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1703 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
| 1704 | u64 mce_cap; |
| 1705 | |
| 1706 | mce_cap = KVM_MCE_CAP_SUPPORTED; |
| 1707 | r = -EFAULT; |
| 1708 | if (copy_to_user(argp, &mce_cap, sizeof mce_cap)) |
| 1709 | goto out; |
| 1710 | r = 0; |
| 1711 | break; |
| 1712 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1713 | default: |
| 1714 | r = -EINVAL; |
| 1715 | } |
| 1716 | out: |
| 1717 | return r; |
| 1718 | } |
| 1719 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1720 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1721 | { |
| 1722 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 1723 | if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) { |
| 1724 | unsigned long khz = cpufreq_quick_get(cpu); |
| 1725 | if (!khz) |
| 1726 | khz = tsc_khz; |
| 1727 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 1728 | } |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1729 | kvm_request_guest_time_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1733 | { |
Amit Shah | 9327fd1 | 2007-11-15 18:38:46 +0200 | [diff] [blame] | 1734 | kvm_put_guest_fpu(vcpu); |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1735 | kvm_x86_ops->vcpu_put(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1738 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1739 | { |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1740 | unsigned long long efer = 0; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1741 | |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1742 | rdmsrl_safe(MSR_EFER, &efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1743 | return efer & EFER_NX; |
| 1744 | } |
| 1745 | |
| 1746 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1747 | { |
| 1748 | int i; |
| 1749 | struct kvm_cpuid_entry2 *e, *entry; |
| 1750 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1751 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1752 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1753 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1754 | if (e->function == 0x80000001) { |
| 1755 | entry = e; |
| 1756 | break; |
| 1757 | } |
| 1758 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1759 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1760 | entry->edx &= ~(1 << 20); |
| 1761 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1762 | } |
| 1763 | } |
| 1764 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1765 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1766 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1767 | struct kvm_cpuid *cpuid, |
| 1768 | struct kvm_cpuid_entry __user *entries) |
| 1769 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1770 | int r, i; |
| 1771 | struct kvm_cpuid_entry *cpuid_entries; |
| 1772 | |
| 1773 | r = -E2BIG; |
| 1774 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1775 | goto out; |
| 1776 | r = -ENOMEM; |
| 1777 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1778 | if (!cpuid_entries) |
| 1779 | goto out; |
| 1780 | r = -EFAULT; |
| 1781 | if (copy_from_user(cpuid_entries, entries, |
| 1782 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1783 | goto out_free; |
| 1784 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1785 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1786 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1787 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1788 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1789 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1790 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1791 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1792 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1793 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1794 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1795 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1796 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1797 | cpuid_fix_nx_cap(vcpu); |
| 1798 | r = 0; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1799 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1800 | kvm_x86_ops->cpuid_update(vcpu); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1801 | |
| 1802 | out_free: |
| 1803 | vfree(cpuid_entries); |
| 1804 | out: |
| 1805 | return r; |
| 1806 | } |
| 1807 | |
| 1808 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1809 | struct kvm_cpuid2 *cpuid, |
| 1810 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1811 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1812 | int r; |
| 1813 | |
| 1814 | r = -E2BIG; |
| 1815 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1816 | goto out; |
| 1817 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1818 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1819 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1820 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1821 | vcpu->arch.cpuid_nent = cpuid->nent; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1822 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1823 | kvm_x86_ops->cpuid_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1824 | return 0; |
| 1825 | |
| 1826 | out: |
| 1827 | return r; |
| 1828 | } |
| 1829 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1830 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1831 | struct kvm_cpuid2 *cpuid, |
| 1832 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1833 | { |
| 1834 | int r; |
| 1835 | |
| 1836 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1837 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1838 | goto out; |
| 1839 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1840 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1841 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1842 | goto out; |
| 1843 | return 0; |
| 1844 | |
| 1845 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1846 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1847 | return r; |
| 1848 | } |
| 1849 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1850 | static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1851 | u32 index) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1852 | { |
| 1853 | entry->function = function; |
| 1854 | entry->index = index; |
| 1855 | cpuid_count(entry->function, entry->index, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1856 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1857 | entry->flags = 0; |
| 1858 | } |
| 1859 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1860 | #define F(x) bit(X86_FEATURE_##x) |
| 1861 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1862 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1863 | u32 index, int *nent, int maxnent) |
| 1864 | { |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1865 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1866 | #ifdef CONFIG_X86_64 |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1867 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) |
| 1868 | ? F(GBPAGES) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1869 | unsigned f_lm = F(LM); |
| 1870 | #else |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1871 | unsigned f_gbpages = 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1872 | unsigned f_lm = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1873 | #endif |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1874 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1875 | |
| 1876 | /* cpuid 1.edx */ |
| 1877 | const u32 kvm_supported_word0_x86_features = |
| 1878 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1879 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1880 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 1881 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1882 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | |
| 1883 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 1884 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 1885 | 0 /* HTT, TM, Reserved, PBE */; |
| 1886 | /* cpuid 0x80000001.edx */ |
| 1887 | const u32 kvm_supported_word1_x86_features = |
| 1888 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1889 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1890 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 1891 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1892 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
| 1893 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1894 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1895 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); |
| 1896 | /* cpuid 1.ecx */ |
| 1897 | const u32 kvm_supported_word4_x86_features = |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1898 | F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ | |
| 1899 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 1900 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
| 1901 | 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ | |
| 1902 | 0 /* Reserved, DCA */ | F(XMM4_1) | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1903 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1904 | 0 /* Reserved, XSAVE, OSXSAVE */; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1905 | /* cpuid 0x80000001.ecx */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1906 | const u32 kvm_supported_word6_x86_features = |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1907 | F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ | |
| 1908 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
| 1909 | F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) | |
| 1910 | 0 /* SKINIT */ | 0 /* WDT */; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1911 | |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1912 | /* all calls to cpuid_count() should be made on the same cpu */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1913 | get_cpu(); |
| 1914 | do_cpuid_1_ent(entry, function, index); |
| 1915 | ++*nent; |
| 1916 | |
| 1917 | switch (function) { |
| 1918 | case 0: |
| 1919 | entry->eax = min(entry->eax, (u32)0xb); |
| 1920 | break; |
| 1921 | case 1: |
| 1922 | entry->edx &= kvm_supported_word0_x86_features; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1923 | entry->ecx &= kvm_supported_word4_x86_features; |
Gleb Natapov | 0d1de2d9 | 2009-07-12 16:10:55 +0300 | [diff] [blame] | 1924 | /* we support x2apic emulation even if host does not support |
| 1925 | * it since we emulate x2apic in software */ |
| 1926 | entry->ecx |= F(X2APIC); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1927 | break; |
| 1928 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 1929 | * may return different values. This forces us to get_cpu() before |
| 1930 | * issuing the first command, and also to emulate this annoying behavior |
| 1931 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 1932 | case 2: { |
| 1933 | int t, times = entry->eax & 0xff; |
| 1934 | |
| 1935 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 1936 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1937 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 1938 | do_cpuid_1_ent(&entry[t], function, 0); |
| 1939 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1940 | ++*nent; |
| 1941 | } |
| 1942 | break; |
| 1943 | } |
| 1944 | /* function 4 and 0xb have additional index. */ |
| 1945 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1946 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1947 | |
| 1948 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1949 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1950 | for (i = 1; *nent < maxnent; ++i) { |
| 1951 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1952 | if (!cache_type) |
| 1953 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1954 | do_cpuid_1_ent(&entry[i], function, i); |
| 1955 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1956 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1957 | ++*nent; |
| 1958 | } |
| 1959 | break; |
| 1960 | } |
| 1961 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1962 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1963 | |
| 1964 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1965 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1966 | for (i = 1; *nent < maxnent; ++i) { |
Nitin A Kamble | 0853d2c | 2008-11-05 15:37:36 -0800 | [diff] [blame] | 1967 | level_type = entry[i - 1].ecx & 0xff00; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1968 | if (!level_type) |
| 1969 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1970 | do_cpuid_1_ent(&entry[i], function, i); |
| 1971 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1972 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1973 | ++*nent; |
| 1974 | } |
| 1975 | break; |
| 1976 | } |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 1977 | case KVM_CPUID_SIGNATURE: { |
| 1978 | char signature[12] = "KVMKVMKVM\0\0"; |
| 1979 | u32 *sigptr = (u32 *)signature; |
| 1980 | entry->eax = 0; |
| 1981 | entry->ebx = sigptr[0]; |
| 1982 | entry->ecx = sigptr[1]; |
| 1983 | entry->edx = sigptr[2]; |
| 1984 | break; |
| 1985 | } |
| 1986 | case KVM_CPUID_FEATURES: |
| 1987 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | |
| 1988 | (1 << KVM_FEATURE_NOP_IO_DELAY) | |
Glauber Costa | 371bcf6 | 2010-05-11 12:17:46 -0400 | [diff] [blame^] | 1989 | (1 << KVM_FEATURE_CLOCKSOURCE2) | |
| 1990 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT); |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 1991 | entry->ebx = 0; |
| 1992 | entry->ecx = 0; |
| 1993 | entry->edx = 0; |
| 1994 | break; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1995 | case 0x80000000: |
| 1996 | entry->eax = min(entry->eax, 0x8000001a); |
| 1997 | break; |
| 1998 | case 0x80000001: |
| 1999 | entry->edx &= kvm_supported_word1_x86_features; |
| 2000 | entry->ecx &= kvm_supported_word6_x86_features; |
| 2001 | break; |
| 2002 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 2003 | |
| 2004 | kvm_x86_ops->set_supported_cpuid(function, entry); |
| 2005 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2006 | put_cpu(); |
| 2007 | } |
| 2008 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2009 | #undef F |
| 2010 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 2011 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2012 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2013 | { |
| 2014 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 2015 | int limit, nent = 0, r = -E2BIG; |
| 2016 | u32 func; |
| 2017 | |
| 2018 | if (cpuid->nent < 1) |
| 2019 | goto out; |
Avi Kivity | 6a54435 | 2009-10-04 16:45:13 +0200 | [diff] [blame] | 2020 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 2021 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2022 | r = -ENOMEM; |
| 2023 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 2024 | if (!cpuid_entries) |
| 2025 | goto out; |
| 2026 | |
| 2027 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 2028 | limit = cpuid_entries[0].eax; |
| 2029 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 2030 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2031 | &nent, cpuid->nent); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2032 | r = -E2BIG; |
| 2033 | if (nent >= cpuid->nent) |
| 2034 | goto out_free; |
| 2035 | |
| 2036 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 2037 | limit = cpuid_entries[nent - 1].eax; |
| 2038 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 2039 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2040 | &nent, cpuid->nent); |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 2041 | |
| 2042 | |
| 2043 | |
| 2044 | r = -E2BIG; |
| 2045 | if (nent >= cpuid->nent) |
| 2046 | goto out_free; |
| 2047 | |
| 2048 | do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent, |
| 2049 | cpuid->nent); |
| 2050 | |
| 2051 | r = -E2BIG; |
| 2052 | if (nent >= cpuid->nent) |
| 2053 | goto out_free; |
| 2054 | |
| 2055 | do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent, |
| 2056 | cpuid->nent); |
| 2057 | |
Mark McLoughlin | cb00764 | 2009-05-12 12:36:44 +0100 | [diff] [blame] | 2058 | r = -E2BIG; |
| 2059 | if (nent >= cpuid->nent) |
| 2060 | goto out_free; |
| 2061 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2062 | r = -EFAULT; |
| 2063 | if (copy_to_user(entries, cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2064 | nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2065 | goto out_free; |
| 2066 | cpuid->nent = nent; |
| 2067 | r = 0; |
| 2068 | |
| 2069 | out_free: |
| 2070 | vfree(cpuid_entries); |
| 2071 | out: |
| 2072 | return r; |
| 2073 | } |
| 2074 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2075 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 2076 | struct kvm_lapic_state *s) |
| 2077 | { |
| 2078 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2079 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2080 | vcpu_put(vcpu); |
| 2081 | |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 2086 | struct kvm_lapic_state *s) |
| 2087 | { |
| 2088 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2089 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2090 | kvm_apic_post_state_restore(vcpu); |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 2091 | update_cr8_intercept(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2092 | vcpu_put(vcpu); |
| 2093 | |
| 2094 | return 0; |
| 2095 | } |
| 2096 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2097 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 2098 | struct kvm_interrupt *irq) |
| 2099 | { |
| 2100 | if (irq->irq < 0 || irq->irq >= 256) |
| 2101 | return -EINVAL; |
| 2102 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2103 | return -ENXIO; |
| 2104 | vcpu_load(vcpu); |
| 2105 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2106 | kvm_queue_interrupt(vcpu, irq->irq, false); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2107 | |
| 2108 | vcpu_put(vcpu); |
| 2109 | |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2113 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 2114 | { |
| 2115 | vcpu_load(vcpu); |
| 2116 | kvm_inject_nmi(vcpu); |
| 2117 | vcpu_put(vcpu); |
| 2118 | |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2122 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 2123 | struct kvm_tpr_access_ctl *tac) |
| 2124 | { |
| 2125 | if (tac->flags) |
| 2126 | return -EINVAL; |
| 2127 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2131 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 2132 | u64 mcg_cap) |
| 2133 | { |
| 2134 | int r; |
| 2135 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 2136 | |
| 2137 | r = -EINVAL; |
Jan Kiszka | a9e38c3e | 2009-10-23 09:37:00 +0200 | [diff] [blame] | 2138 | if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2139 | goto out; |
| 2140 | if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000)) |
| 2141 | goto out; |
| 2142 | r = 0; |
| 2143 | vcpu->arch.mcg_cap = mcg_cap; |
| 2144 | /* Init IA32_MCG_CTL to all 1s */ |
| 2145 | if (mcg_cap & MCG_CTL_P) |
| 2146 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 2147 | /* Init IA32_MCi_CTL to all 1s */ |
| 2148 | for (bank = 0; bank < bank_num; bank++) |
| 2149 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
| 2150 | out: |
| 2151 | return r; |
| 2152 | } |
| 2153 | |
| 2154 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 2155 | struct kvm_x86_mce *mce) |
| 2156 | { |
| 2157 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2158 | unsigned bank_num = mcg_cap & 0xff; |
| 2159 | u64 *banks = vcpu->arch.mce_banks; |
| 2160 | |
| 2161 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 2162 | return -EINVAL; |
| 2163 | /* |
| 2164 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 2165 | * reporting is disabled |
| 2166 | */ |
| 2167 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 2168 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 2169 | return 0; |
| 2170 | banks += 4 * mce->bank; |
| 2171 | /* |
| 2172 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 2173 | * reporting is disabled for the bank |
| 2174 | */ |
| 2175 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 2176 | return 0; |
| 2177 | if (mce->status & MCI_STATUS_UC) { |
| 2178 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2179 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2180 | printk(KERN_DEBUG "kvm: set_mce: " |
| 2181 | "injects mce exception while " |
| 2182 | "previous one is in progress!\n"); |
| 2183 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | if (banks[1] & MCI_STATUS_VAL) |
| 2187 | mce->status |= MCI_STATUS_OVER; |
| 2188 | banks[2] = mce->addr; |
| 2189 | banks[3] = mce->misc; |
| 2190 | vcpu->arch.mcg_status = mce->mcg_status; |
| 2191 | banks[1] = mce->status; |
| 2192 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 2193 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 2194 | || !(banks[1] & MCI_STATUS_UC)) { |
| 2195 | if (banks[1] & MCI_STATUS_VAL) |
| 2196 | mce->status |= MCI_STATUS_OVER; |
| 2197 | banks[2] = mce->addr; |
| 2198 | banks[3] = mce->misc; |
| 2199 | banks[1] = mce->status; |
| 2200 | } else |
| 2201 | banks[1] |= MCI_STATUS_OVER; |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2205 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 2206 | struct kvm_vcpu_events *events) |
| 2207 | { |
| 2208 | vcpu_load(vcpu); |
| 2209 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2210 | events->exception.injected = |
| 2211 | vcpu->arch.exception.pending && |
| 2212 | !kvm_exception_is_soft(vcpu->arch.exception.nr); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2213 | events->exception.nr = vcpu->arch.exception.nr; |
| 2214 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
| 2215 | events->exception.error_code = vcpu->arch.exception.error_code; |
| 2216 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2217 | events->interrupt.injected = |
| 2218 | vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2219 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2220 | events->interrupt.soft = 0; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2221 | events->interrupt.shadow = |
| 2222 | kvm_x86_ops->get_interrupt_shadow(vcpu, |
| 2223 | KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2224 | |
| 2225 | events->nmi.injected = vcpu->arch.nmi_injected; |
| 2226 | events->nmi.pending = vcpu->arch.nmi_pending; |
| 2227 | events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); |
| 2228 | |
| 2229 | events->sipi_vector = vcpu->arch.sipi_vector; |
| 2230 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2231 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2232 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2233 | | KVM_VCPUEVENT_VALID_SHADOW); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2234 | |
| 2235 | vcpu_put(vcpu); |
| 2236 | } |
| 2237 | |
| 2238 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 2239 | struct kvm_vcpu_events *events) |
| 2240 | { |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2241 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2242 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2243 | | KVM_VCPUEVENT_VALID_SHADOW)) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2244 | return -EINVAL; |
| 2245 | |
| 2246 | vcpu_load(vcpu); |
| 2247 | |
| 2248 | vcpu->arch.exception.pending = events->exception.injected; |
| 2249 | vcpu->arch.exception.nr = events->exception.nr; |
| 2250 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 2251 | vcpu->arch.exception.error_code = events->exception.error_code; |
| 2252 | |
| 2253 | vcpu->arch.interrupt.pending = events->interrupt.injected; |
| 2254 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 2255 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
| 2256 | if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm)) |
| 2257 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2258 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
| 2259 | kvm_x86_ops->set_interrupt_shadow(vcpu, |
| 2260 | events->interrupt.shadow); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2261 | |
| 2262 | vcpu->arch.nmi_injected = events->nmi.injected; |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2263 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 2264 | vcpu->arch.nmi_pending = events->nmi.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2265 | kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked); |
| 2266 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2267 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR) |
| 2268 | vcpu->arch.sipi_vector = events->sipi_vector; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2269 | |
| 2270 | vcpu_put(vcpu); |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2275 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 2276 | struct kvm_debugregs *dbgregs) |
| 2277 | { |
| 2278 | vcpu_load(vcpu); |
| 2279 | |
| 2280 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
| 2281 | dbgregs->dr6 = vcpu->arch.dr6; |
| 2282 | dbgregs->dr7 = vcpu->arch.dr7; |
| 2283 | dbgregs->flags = 0; |
| 2284 | |
| 2285 | vcpu_put(vcpu); |
| 2286 | } |
| 2287 | |
| 2288 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 2289 | struct kvm_debugregs *dbgregs) |
| 2290 | { |
| 2291 | if (dbgregs->flags) |
| 2292 | return -EINVAL; |
| 2293 | |
| 2294 | vcpu_load(vcpu); |
| 2295 | |
| 2296 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
| 2297 | vcpu->arch.dr6 = dbgregs->dr6; |
| 2298 | vcpu->arch.dr7 = dbgregs->dr7; |
| 2299 | |
| 2300 | vcpu_put(vcpu); |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2305 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 2306 | unsigned int ioctl, unsigned long arg) |
| 2307 | { |
| 2308 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2309 | void __user *argp = (void __user *)arg; |
| 2310 | int r; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2311 | struct kvm_lapic_state *lapic = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2312 | |
| 2313 | switch (ioctl) { |
| 2314 | case KVM_GET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2315 | r = -EINVAL; |
| 2316 | if (!vcpu->arch.apic) |
| 2317 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2318 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2319 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2320 | r = -ENOMEM; |
| 2321 | if (!lapic) |
| 2322 | goto out; |
| 2323 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2324 | if (r) |
| 2325 | goto out; |
| 2326 | r = -EFAULT; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2327 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2328 | goto out; |
| 2329 | r = 0; |
| 2330 | break; |
| 2331 | } |
| 2332 | case KVM_SET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2333 | r = -EINVAL; |
| 2334 | if (!vcpu->arch.apic) |
| 2335 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2336 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
| 2337 | r = -ENOMEM; |
| 2338 | if (!lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2339 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2340 | r = -EFAULT; |
| 2341 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
| 2342 | goto out; |
| 2343 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2344 | if (r) |
| 2345 | goto out; |
| 2346 | r = 0; |
| 2347 | break; |
| 2348 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2349 | case KVM_INTERRUPT: { |
| 2350 | struct kvm_interrupt irq; |
| 2351 | |
| 2352 | r = -EFAULT; |
| 2353 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 2354 | goto out; |
| 2355 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 2356 | if (r) |
| 2357 | goto out; |
| 2358 | r = 0; |
| 2359 | break; |
| 2360 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2361 | case KVM_NMI: { |
| 2362 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 2363 | if (r) |
| 2364 | goto out; |
| 2365 | r = 0; |
| 2366 | break; |
| 2367 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2368 | case KVM_SET_CPUID: { |
| 2369 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 2370 | struct kvm_cpuid cpuid; |
| 2371 | |
| 2372 | r = -EFAULT; |
| 2373 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2374 | goto out; |
| 2375 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 2376 | if (r) |
| 2377 | goto out; |
| 2378 | break; |
| 2379 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2380 | case KVM_SET_CPUID2: { |
| 2381 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2382 | struct kvm_cpuid2 cpuid; |
| 2383 | |
| 2384 | r = -EFAULT; |
| 2385 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2386 | goto out; |
| 2387 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2388 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2389 | if (r) |
| 2390 | goto out; |
| 2391 | break; |
| 2392 | } |
| 2393 | case KVM_GET_CPUID2: { |
| 2394 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2395 | struct kvm_cpuid2 cpuid; |
| 2396 | |
| 2397 | r = -EFAULT; |
| 2398 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2399 | goto out; |
| 2400 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2401 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2402 | if (r) |
| 2403 | goto out; |
| 2404 | r = -EFAULT; |
| 2405 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 2406 | goto out; |
| 2407 | r = 0; |
| 2408 | break; |
| 2409 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2410 | case KVM_GET_MSRS: |
| 2411 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 2412 | break; |
| 2413 | case KVM_SET_MSRS: |
| 2414 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 2415 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2416 | case KVM_TPR_ACCESS_REPORTING: { |
| 2417 | struct kvm_tpr_access_ctl tac; |
| 2418 | |
| 2419 | r = -EFAULT; |
| 2420 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 2421 | goto out; |
| 2422 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 2423 | if (r) |
| 2424 | goto out; |
| 2425 | r = -EFAULT; |
| 2426 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 2427 | goto out; |
| 2428 | r = 0; |
| 2429 | break; |
| 2430 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2431 | case KVM_SET_VAPIC_ADDR: { |
| 2432 | struct kvm_vapic_addr va; |
| 2433 | |
| 2434 | r = -EINVAL; |
| 2435 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 2436 | goto out; |
| 2437 | r = -EFAULT; |
| 2438 | if (copy_from_user(&va, argp, sizeof va)) |
| 2439 | goto out; |
| 2440 | r = 0; |
| 2441 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 2442 | break; |
| 2443 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2444 | case KVM_X86_SETUP_MCE: { |
| 2445 | u64 mcg_cap; |
| 2446 | |
| 2447 | r = -EFAULT; |
| 2448 | if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap)) |
| 2449 | goto out; |
| 2450 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 2451 | break; |
| 2452 | } |
| 2453 | case KVM_X86_SET_MCE: { |
| 2454 | struct kvm_x86_mce mce; |
| 2455 | |
| 2456 | r = -EFAULT; |
| 2457 | if (copy_from_user(&mce, argp, sizeof mce)) |
| 2458 | goto out; |
| 2459 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 2460 | break; |
| 2461 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2462 | case KVM_GET_VCPU_EVENTS: { |
| 2463 | struct kvm_vcpu_events events; |
| 2464 | |
| 2465 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 2466 | |
| 2467 | r = -EFAULT; |
| 2468 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 2469 | break; |
| 2470 | r = 0; |
| 2471 | break; |
| 2472 | } |
| 2473 | case KVM_SET_VCPU_EVENTS: { |
| 2474 | struct kvm_vcpu_events events; |
| 2475 | |
| 2476 | r = -EFAULT; |
| 2477 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 2478 | break; |
| 2479 | |
| 2480 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 2481 | break; |
| 2482 | } |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2483 | case KVM_GET_DEBUGREGS: { |
| 2484 | struct kvm_debugregs dbgregs; |
| 2485 | |
| 2486 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 2487 | |
| 2488 | r = -EFAULT; |
| 2489 | if (copy_to_user(argp, &dbgregs, |
| 2490 | sizeof(struct kvm_debugregs))) |
| 2491 | break; |
| 2492 | r = 0; |
| 2493 | break; |
| 2494 | } |
| 2495 | case KVM_SET_DEBUGREGS: { |
| 2496 | struct kvm_debugregs dbgregs; |
| 2497 | |
| 2498 | r = -EFAULT; |
| 2499 | if (copy_from_user(&dbgregs, argp, |
| 2500 | sizeof(struct kvm_debugregs))) |
| 2501 | break; |
| 2502 | |
| 2503 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 2504 | break; |
| 2505 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2506 | default: |
| 2507 | r = -EINVAL; |
| 2508 | } |
| 2509 | out: |
Wei Yongjun | 7a6ce84 | 2009-03-31 16:47:44 +0800 | [diff] [blame] | 2510 | kfree(lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2511 | return r; |
| 2512 | } |
| 2513 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2514 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 2515 | { |
| 2516 | int ret; |
| 2517 | |
| 2518 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 2519 | return -1; |
| 2520 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 2521 | return ret; |
| 2522 | } |
| 2523 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2524 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 2525 | u64 ident_addr) |
| 2526 | { |
| 2527 | kvm->arch.ept_identity_map_addr = ident_addr; |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2531 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 2532 | u32 kvm_nr_mmu_pages) |
| 2533 | { |
| 2534 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 2535 | return -EINVAL; |
| 2536 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2537 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2538 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2539 | |
| 2540 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2541 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2542 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2543 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2544 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2545 | return 0; |
| 2546 | } |
| 2547 | |
| 2548 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 2549 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2550 | return kvm->arch.n_alloc_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2553 | gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn) |
| 2554 | { |
| 2555 | int i; |
| 2556 | struct kvm_mem_alias *alias; |
| 2557 | struct kvm_mem_aliases *aliases; |
| 2558 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2559 | aliases = kvm_aliases(kvm); |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2560 | |
| 2561 | for (i = 0; i < aliases->naliases; ++i) { |
| 2562 | alias = &aliases->aliases[i]; |
| 2563 | if (alias->flags & KVM_ALIAS_INVALID) |
| 2564 | continue; |
| 2565 | if (gfn >= alias->base_gfn |
| 2566 | && gfn < alias->base_gfn + alias->npages) |
| 2567 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2568 | } |
| 2569 | return gfn; |
| 2570 | } |
| 2571 | |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2572 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 2573 | { |
| 2574 | int i; |
| 2575 | struct kvm_mem_alias *alias; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2576 | struct kvm_mem_aliases *aliases; |
| 2577 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2578 | aliases = kvm_aliases(kvm); |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2579 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2580 | for (i = 0; i < aliases->naliases; ++i) { |
| 2581 | alias = &aliases->aliases[i]; |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2582 | if (gfn >= alias->base_gfn |
| 2583 | && gfn < alias->base_gfn + alias->npages) |
| 2584 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2585 | } |
| 2586 | return gfn; |
| 2587 | } |
| 2588 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2589 | /* |
| 2590 | * Set a new alias region. Aliases map a portion of physical memory into |
| 2591 | * another portion. This is useful for memory windows, for example the PC |
| 2592 | * VGA region. |
| 2593 | */ |
| 2594 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 2595 | struct kvm_memory_alias *alias) |
| 2596 | { |
| 2597 | int r, n; |
| 2598 | struct kvm_mem_alias *p; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2599 | struct kvm_mem_aliases *aliases, *old_aliases; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2600 | |
| 2601 | r = -EINVAL; |
| 2602 | /* General sanity checks */ |
| 2603 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 2604 | goto out; |
| 2605 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 2606 | goto out; |
| 2607 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 2608 | goto out; |
| 2609 | if (alias->guest_phys_addr + alias->memory_size |
| 2610 | < alias->guest_phys_addr) |
| 2611 | goto out; |
| 2612 | if (alias->target_phys_addr + alias->memory_size |
| 2613 | < alias->target_phys_addr) |
| 2614 | goto out; |
| 2615 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2616 | r = -ENOMEM; |
| 2617 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2618 | if (!aliases) |
| 2619 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2620 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2621 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2622 | |
| 2623 | /* invalidate any gfn reference in case of deletion/shrinking */ |
| 2624 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
| 2625 | aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID; |
| 2626 | old_aliases = kvm->arch.aliases; |
| 2627 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2628 | synchronize_srcu_expedited(&kvm->srcu); |
| 2629 | kvm_mmu_zap_all(kvm); |
| 2630 | kfree(old_aliases); |
| 2631 | |
| 2632 | r = -ENOMEM; |
| 2633 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2634 | if (!aliases) |
| 2635 | goto out_unlock; |
| 2636 | |
| 2637 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2638 | |
| 2639 | p = &aliases->aliases[alias->slot]; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2640 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 2641 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 2642 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2643 | p->flags &= ~(KVM_ALIAS_INVALID); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2644 | |
| 2645 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2646 | if (aliases->aliases[n - 1].npages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2647 | break; |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2648 | aliases->naliases = n; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2649 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2650 | old_aliases = kvm->arch.aliases; |
| 2651 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2652 | synchronize_srcu_expedited(&kvm->srcu); |
| 2653 | kfree(old_aliases); |
| 2654 | r = 0; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2655 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2656 | out_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2657 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2658 | out: |
| 2659 | return r; |
| 2660 | } |
| 2661 | |
| 2662 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2663 | { |
| 2664 | int r; |
| 2665 | |
| 2666 | r = 0; |
| 2667 | switch (chip->chip_id) { |
| 2668 | case KVM_IRQCHIP_PIC_MASTER: |
| 2669 | memcpy(&chip->chip.pic, |
| 2670 | &pic_irqchip(kvm)->pics[0], |
| 2671 | sizeof(struct kvm_pic_state)); |
| 2672 | break; |
| 2673 | case KVM_IRQCHIP_PIC_SLAVE: |
| 2674 | memcpy(&chip->chip.pic, |
| 2675 | &pic_irqchip(kvm)->pics[1], |
| 2676 | sizeof(struct kvm_pic_state)); |
| 2677 | break; |
| 2678 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2679 | r = kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2680 | break; |
| 2681 | default: |
| 2682 | r = -EINVAL; |
| 2683 | break; |
| 2684 | } |
| 2685 | return r; |
| 2686 | } |
| 2687 | |
| 2688 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2689 | { |
| 2690 | int r; |
| 2691 | |
| 2692 | r = 0; |
| 2693 | switch (chip->chip_id) { |
| 2694 | case KVM_IRQCHIP_PIC_MASTER: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2695 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2696 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 2697 | &chip->chip.pic, |
| 2698 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2699 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2700 | break; |
| 2701 | case KVM_IRQCHIP_PIC_SLAVE: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2702 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2703 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 2704 | &chip->chip.pic, |
| 2705 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2706 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2707 | break; |
| 2708 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2709 | r = kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2710 | break; |
| 2711 | default: |
| 2712 | r = -EINVAL; |
| 2713 | break; |
| 2714 | } |
| 2715 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 2716 | return r; |
| 2717 | } |
| 2718 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2719 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2720 | { |
| 2721 | int r = 0; |
| 2722 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2723 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2724 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2725 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2726 | return r; |
| 2727 | } |
| 2728 | |
| 2729 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2730 | { |
| 2731 | int r = 0; |
| 2732 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2733 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2734 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2735 | kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); |
| 2736 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2737 | return r; |
| 2738 | } |
| 2739 | |
| 2740 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2741 | { |
| 2742 | int r = 0; |
| 2743 | |
| 2744 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2745 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 2746 | sizeof(ps->channels)); |
| 2747 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 2748 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2749 | return r; |
| 2750 | } |
| 2751 | |
| 2752 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2753 | { |
| 2754 | int r = 0, start = 0; |
| 2755 | u32 prev_legacy, cur_legacy; |
| 2756 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2757 | prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2758 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2759 | if (!prev_legacy && cur_legacy) |
| 2760 | start = 1; |
| 2761 | memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, |
| 2762 | sizeof(kvm->arch.vpit->pit_state.channels)); |
| 2763 | kvm->arch.vpit->pit_state.flags = ps->flags; |
| 2764 | kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start); |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2765 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2766 | return r; |
| 2767 | } |
| 2768 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2769 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 2770 | struct kvm_reinject_control *control) |
| 2771 | { |
| 2772 | if (!kvm->arch.vpit) |
| 2773 | return -ENXIO; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2774 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2775 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2776 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2777 | return 0; |
| 2778 | } |
| 2779 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2780 | /* |
| 2781 | * Get (and clear) the dirty memory log for a memory slot. |
| 2782 | */ |
| 2783 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 2784 | struct kvm_dirty_log *log) |
| 2785 | { |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2786 | int r, i; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2787 | struct kvm_memory_slot *memslot; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2788 | unsigned long n; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2789 | unsigned long is_dirty = 0; |
| 2790 | unsigned long *dirty_bitmap = NULL; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2791 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2792 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2793 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2794 | r = -EINVAL; |
| 2795 | if (log->slot >= KVM_MEMORY_SLOTS) |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2796 | goto out; |
| 2797 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2798 | memslot = &kvm->memslots->memslots[log->slot]; |
| 2799 | r = -ENOENT; |
| 2800 | if (!memslot->dirty_bitmap) |
| 2801 | goto out; |
| 2802 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2803 | n = kvm_dirty_bitmap_bytes(memslot); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2804 | |
| 2805 | r = -ENOMEM; |
| 2806 | dirty_bitmap = vmalloc(n); |
| 2807 | if (!dirty_bitmap) |
| 2808 | goto out; |
| 2809 | memset(dirty_bitmap, 0, n); |
| 2810 | |
| 2811 | for (i = 0; !is_dirty && i < n/sizeof(long); i++) |
| 2812 | is_dirty = memslot->dirty_bitmap[i]; |
| 2813 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2814 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 2815 | if (is_dirty) { |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2816 | struct kvm_memslots *slots, *old_slots; |
| 2817 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2818 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2819 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2820 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2821 | |
| 2822 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 2823 | if (!slots) |
| 2824 | goto out_free; |
| 2825 | |
| 2826 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 2827 | slots->memslots[log->slot].dirty_bitmap = dirty_bitmap; |
| 2828 | |
| 2829 | old_slots = kvm->memslots; |
| 2830 | rcu_assign_pointer(kvm->memslots, slots); |
| 2831 | synchronize_srcu_expedited(&kvm->srcu); |
| 2832 | dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap; |
| 2833 | kfree(old_slots); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2834 | } |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2835 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2836 | r = 0; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2837 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) |
| 2838 | r = -EFAULT; |
| 2839 | out_free: |
| 2840 | vfree(dirty_bitmap); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2841 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2842 | mutex_unlock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2843 | return r; |
| 2844 | } |
| 2845 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2846 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2847 | unsigned int ioctl, unsigned long arg) |
| 2848 | { |
| 2849 | struct kvm *kvm = filp->private_data; |
| 2850 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2851 | int r = -ENOTTY; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2852 | /* |
| 2853 | * This union makes it completely explicit to gcc-3.x |
| 2854 | * that these two variables' stack usage should be |
| 2855 | * combined, not added together. |
| 2856 | */ |
| 2857 | union { |
| 2858 | struct kvm_pit_state ps; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2859 | struct kvm_pit_state2 ps2; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2860 | struct kvm_memory_alias alias; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2861 | struct kvm_pit_config pit_config; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2862 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2863 | |
| 2864 | switch (ioctl) { |
| 2865 | case KVM_SET_TSS_ADDR: |
| 2866 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 2867 | if (r < 0) |
| 2868 | goto out; |
| 2869 | break; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2870 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 2871 | u64 ident_addr; |
| 2872 | |
| 2873 | r = -EFAULT; |
| 2874 | if (copy_from_user(&ident_addr, argp, sizeof ident_addr)) |
| 2875 | goto out; |
| 2876 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
| 2877 | if (r < 0) |
| 2878 | goto out; |
| 2879 | break; |
| 2880 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2881 | case KVM_SET_MEMORY_REGION: { |
| 2882 | struct kvm_memory_region kvm_mem; |
| 2883 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2884 | |
| 2885 | r = -EFAULT; |
| 2886 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 2887 | goto out; |
| 2888 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 2889 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 2890 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 2891 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 2892 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2893 | if (r) |
| 2894 | goto out; |
| 2895 | break; |
| 2896 | } |
| 2897 | case KVM_SET_NR_MMU_PAGES: |
| 2898 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 2899 | if (r) |
| 2900 | goto out; |
| 2901 | break; |
| 2902 | case KVM_GET_NR_MMU_PAGES: |
| 2903 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 2904 | break; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2905 | case KVM_SET_MEMORY_ALIAS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2906 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2907 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2908 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2909 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2910 | if (r) |
| 2911 | goto out; |
| 2912 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2913 | case KVM_CREATE_IRQCHIP: { |
| 2914 | struct kvm_pic *vpic; |
| 2915 | |
| 2916 | mutex_lock(&kvm->lock); |
| 2917 | r = -EEXIST; |
| 2918 | if (kvm->arch.vpic) |
| 2919 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2920 | r = -ENOMEM; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2921 | vpic = kvm_create_pic(kvm); |
| 2922 | if (vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2923 | r = kvm_ioapic_init(kvm); |
| 2924 | if (r) { |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2925 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, |
| 2926 | &vpic->dev); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2927 | kfree(vpic); |
| 2928 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2929 | } |
| 2930 | } else |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2931 | goto create_irqchip_unlock; |
| 2932 | smp_wmb(); |
| 2933 | kvm->arch.vpic = vpic; |
| 2934 | smp_wmb(); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2935 | r = kvm_setup_default_irq_routing(kvm); |
| 2936 | if (r) { |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2937 | mutex_lock(&kvm->irq_lock); |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2938 | kvm_ioapic_destroy(kvm); |
| 2939 | kvm_destroy_pic(kvm); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2940 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2941 | } |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2942 | create_irqchip_unlock: |
| 2943 | mutex_unlock(&kvm->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2944 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2945 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2946 | case KVM_CREATE_PIT: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2947 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 2948 | goto create_pit; |
| 2949 | case KVM_CREATE_PIT2: |
| 2950 | r = -EFAULT; |
| 2951 | if (copy_from_user(&u.pit_config, argp, |
| 2952 | sizeof(struct kvm_pit_config))) |
| 2953 | goto out; |
| 2954 | create_pit: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2955 | mutex_lock(&kvm->slots_lock); |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2956 | r = -EEXIST; |
| 2957 | if (kvm->arch.vpit) |
| 2958 | goto create_pit_unlock; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2959 | r = -ENOMEM; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2960 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2961 | if (kvm->arch.vpit) |
| 2962 | r = 0; |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2963 | create_pit_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2964 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2965 | break; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2966 | case KVM_IRQ_LINE_STATUS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2967 | case KVM_IRQ_LINE: { |
| 2968 | struct kvm_irq_level irq_event; |
| 2969 | |
| 2970 | r = -EFAULT; |
| 2971 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 2972 | goto out; |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame] | 2973 | r = -ENXIO; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2974 | if (irqchip_in_kernel(kvm)) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2975 | __s32 status; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2976 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 2977 | irq_event.irq, irq_event.level); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2978 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame] | 2979 | r = -EFAULT; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2980 | irq_event.status = status; |
| 2981 | if (copy_to_user(argp, &irq_event, |
| 2982 | sizeof irq_event)) |
| 2983 | goto out; |
| 2984 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2985 | r = 0; |
| 2986 | } |
| 2987 | break; |
| 2988 | } |
| 2989 | case KVM_GET_IRQCHIP: { |
| 2990 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2991 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2992 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2993 | r = -ENOMEM; |
| 2994 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2995 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2996 | r = -EFAULT; |
| 2997 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2998 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2999 | r = -ENXIO; |
| 3000 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3001 | goto get_irqchip_out; |
| 3002 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 3003 | if (r) |
| 3004 | goto get_irqchip_out; |
| 3005 | r = -EFAULT; |
| 3006 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 3007 | goto get_irqchip_out; |
| 3008 | r = 0; |
| 3009 | get_irqchip_out: |
| 3010 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3011 | if (r) |
| 3012 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | case KVM_SET_IRQCHIP: { |
| 3016 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3017 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3018 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3019 | r = -ENOMEM; |
| 3020 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3021 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3022 | r = -EFAULT; |
| 3023 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 3024 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3025 | r = -ENXIO; |
| 3026 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3027 | goto set_irqchip_out; |
| 3028 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 3029 | if (r) |
| 3030 | goto set_irqchip_out; |
| 3031 | r = 0; |
| 3032 | set_irqchip_out: |
| 3033 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3034 | if (r) |
| 3035 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3036 | break; |
| 3037 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3038 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3039 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3040 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3041 | goto out; |
| 3042 | r = -ENXIO; |
| 3043 | if (!kvm->arch.vpit) |
| 3044 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3045 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3046 | if (r) |
| 3047 | goto out; |
| 3048 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3049 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3050 | goto out; |
| 3051 | r = 0; |
| 3052 | break; |
| 3053 | } |
| 3054 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3055 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3056 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3057 | goto out; |
| 3058 | r = -ENXIO; |
| 3059 | if (!kvm->arch.vpit) |
| 3060 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3061 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3062 | if (r) |
| 3063 | goto out; |
| 3064 | r = 0; |
| 3065 | break; |
| 3066 | } |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 3067 | case KVM_GET_PIT2: { |
| 3068 | r = -ENXIO; |
| 3069 | if (!kvm->arch.vpit) |
| 3070 | goto out; |
| 3071 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 3072 | if (r) |
| 3073 | goto out; |
| 3074 | r = -EFAULT; |
| 3075 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 3076 | goto out; |
| 3077 | r = 0; |
| 3078 | break; |
| 3079 | } |
| 3080 | case KVM_SET_PIT2: { |
| 3081 | r = -EFAULT; |
| 3082 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 3083 | goto out; |
| 3084 | r = -ENXIO; |
| 3085 | if (!kvm->arch.vpit) |
| 3086 | goto out; |
| 3087 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
| 3088 | if (r) |
| 3089 | goto out; |
| 3090 | r = 0; |
| 3091 | break; |
| 3092 | } |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 3093 | case KVM_REINJECT_CONTROL: { |
| 3094 | struct kvm_reinject_control control; |
| 3095 | r = -EFAULT; |
| 3096 | if (copy_from_user(&control, argp, sizeof(control))) |
| 3097 | goto out; |
| 3098 | r = kvm_vm_ioctl_reinject(kvm, &control); |
| 3099 | if (r) |
| 3100 | goto out; |
| 3101 | r = 0; |
| 3102 | break; |
| 3103 | } |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 3104 | case KVM_XEN_HVM_CONFIG: { |
| 3105 | r = -EFAULT; |
| 3106 | if (copy_from_user(&kvm->arch.xen_hvm_config, argp, |
| 3107 | sizeof(struct kvm_xen_hvm_config))) |
| 3108 | goto out; |
| 3109 | r = -EINVAL; |
| 3110 | if (kvm->arch.xen_hvm_config.flags) |
| 3111 | goto out; |
| 3112 | r = 0; |
| 3113 | break; |
| 3114 | } |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 3115 | case KVM_SET_CLOCK: { |
| 3116 | struct timespec now; |
| 3117 | struct kvm_clock_data user_ns; |
| 3118 | u64 now_ns; |
| 3119 | s64 delta; |
| 3120 | |
| 3121 | r = -EFAULT; |
| 3122 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 3123 | goto out; |
| 3124 | |
| 3125 | r = -EINVAL; |
| 3126 | if (user_ns.flags) |
| 3127 | goto out; |
| 3128 | |
| 3129 | r = 0; |
| 3130 | ktime_get_ts(&now); |
| 3131 | now_ns = timespec_to_ns(&now); |
| 3132 | delta = user_ns.clock - now_ns; |
| 3133 | kvm->arch.kvmclock_offset = delta; |
| 3134 | break; |
| 3135 | } |
| 3136 | case KVM_GET_CLOCK: { |
| 3137 | struct timespec now; |
| 3138 | struct kvm_clock_data user_ns; |
| 3139 | u64 now_ns; |
| 3140 | |
| 3141 | ktime_get_ts(&now); |
| 3142 | now_ns = timespec_to_ns(&now); |
| 3143 | user_ns.clock = kvm->arch.kvmclock_offset + now_ns; |
| 3144 | user_ns.flags = 0; |
| 3145 | |
| 3146 | r = -EFAULT; |
| 3147 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 3148 | goto out; |
| 3149 | r = 0; |
| 3150 | break; |
| 3151 | } |
| 3152 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3153 | default: |
| 3154 | ; |
| 3155 | } |
| 3156 | out: |
| 3157 | return r; |
| 3158 | } |
| 3159 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 3160 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3161 | { |
| 3162 | u32 dummy[2]; |
| 3163 | unsigned i, j; |
| 3164 | |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 3165 | /* skip the first msrs in the list. KVM-specific */ |
| 3166 | for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) { |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3167 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 3168 | continue; |
| 3169 | if (j < i) |
| 3170 | msrs_to_save[j] = msrs_to_save[i]; |
| 3171 | j++; |
| 3172 | } |
| 3173 | num_msrs_to_save = j; |
| 3174 | } |
| 3175 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3176 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 3177 | const void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3178 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3179 | if (vcpu->arch.apic && |
| 3180 | !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v)) |
| 3181 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3182 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3183 | return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3186 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3187 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3188 | if (vcpu->arch.apic && |
| 3189 | !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v)) |
| 3190 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3191 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3192 | return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3193 | } |
| 3194 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3195 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
| 3196 | struct kvm_segment *var, int seg) |
| 3197 | { |
| 3198 | kvm_x86_ops->set_segment(vcpu, var, seg); |
| 3199 | } |
| 3200 | |
| 3201 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 3202 | struct kvm_segment *var, int seg) |
| 3203 | { |
| 3204 | kvm_x86_ops->get_segment(vcpu, var, seg); |
| 3205 | } |
| 3206 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3207 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3208 | { |
| 3209 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3210 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3211 | } |
| 3212 | |
| 3213 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3214 | { |
| 3215 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3216 | access |= PFERR_FETCH_MASK; |
| 3217 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3218 | } |
| 3219 | |
| 3220 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3221 | { |
| 3222 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3223 | access |= PFERR_WRITE_MASK; |
| 3224 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3225 | } |
| 3226 | |
| 3227 | /* uses this to access any guest's mapped memory without checking CPL */ |
| 3228 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3229 | { |
| 3230 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error); |
| 3231 | } |
| 3232 | |
| 3233 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 3234 | struct kvm_vcpu *vcpu, u32 access, |
| 3235 | u32 *error) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3236 | { |
| 3237 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3238 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3239 | |
| 3240 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3241 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3242 | unsigned offset = addr & (PAGE_SIZE-1); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3243 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3244 | int ret; |
| 3245 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3246 | if (gpa == UNMAPPED_GVA) { |
| 3247 | r = X86EMUL_PROPAGATE_FAULT; |
| 3248 | goto out; |
| 3249 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3250 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3251 | if (ret < 0) { |
| 3252 | r = X86EMUL_UNHANDLEABLE; |
| 3253 | goto out; |
| 3254 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3255 | |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3256 | bytes -= toread; |
| 3257 | data += toread; |
| 3258 | addr += toread; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3259 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3260 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3261 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3262 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3263 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3264 | /* used for instruction fetching */ |
| 3265 | static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3266 | struct kvm_vcpu *vcpu, u32 *error) |
| 3267 | { |
| 3268 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3269 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, |
| 3270 | access | PFERR_FETCH_MASK, error); |
| 3271 | } |
| 3272 | |
| 3273 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3274 | struct kvm_vcpu *vcpu, u32 *error) |
| 3275 | { |
| 3276 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3277 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
| 3278 | error); |
| 3279 | } |
| 3280 | |
| 3281 | static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes, |
| 3282 | struct kvm_vcpu *vcpu, u32 *error) |
| 3283 | { |
| 3284 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error); |
| 3285 | } |
| 3286 | |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3287 | static int kvm_write_guest_virt_system(gva_t addr, void *val, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3288 | unsigned int bytes, |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3289 | struct kvm_vcpu *vcpu, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3290 | u32 *error) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3291 | { |
| 3292 | void *data = val; |
| 3293 | int r = X86EMUL_CONTINUE; |
| 3294 | |
| 3295 | while (bytes) { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3296 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, |
| 3297 | PFERR_WRITE_MASK, error); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3298 | unsigned offset = addr & (PAGE_SIZE-1); |
| 3299 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 3300 | int ret; |
| 3301 | |
| 3302 | if (gpa == UNMAPPED_GVA) { |
| 3303 | r = X86EMUL_PROPAGATE_FAULT; |
| 3304 | goto out; |
| 3305 | } |
| 3306 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); |
| 3307 | if (ret < 0) { |
| 3308 | r = X86EMUL_UNHANDLEABLE; |
| 3309 | goto out; |
| 3310 | } |
| 3311 | |
| 3312 | bytes -= towrite; |
| 3313 | data += towrite; |
| 3314 | addr += towrite; |
| 3315 | } |
| 3316 | out: |
| 3317 | return r; |
| 3318 | } |
| 3319 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3320 | static int emulator_read_emulated(unsigned long addr, |
| 3321 | void *val, |
| 3322 | unsigned int bytes, |
| 3323 | struct kvm_vcpu *vcpu) |
| 3324 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3325 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3326 | u32 error_code; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3327 | |
| 3328 | if (vcpu->mmio_read_completed) { |
| 3329 | memcpy(val, vcpu->mmio_data, bytes); |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3330 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
| 3331 | vcpu->mmio_phys_addr, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3332 | vcpu->mmio_read_completed = 0; |
| 3333 | return X86EMUL_CONTINUE; |
| 3334 | } |
| 3335 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3336 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code); |
| 3337 | |
| 3338 | if (gpa == UNMAPPED_GVA) { |
| 3339 | kvm_inject_page_fault(vcpu, addr, error_code); |
| 3340 | return X86EMUL_PROPAGATE_FAULT; |
| 3341 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3342 | |
| 3343 | /* For APIC access vmexit */ |
| 3344 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3345 | goto mmio; |
| 3346 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3347 | if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3348 | == X86EMUL_CONTINUE) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3349 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3350 | |
| 3351 | mmio: |
| 3352 | /* |
| 3353 | * Is this MMIO handled locally? |
| 3354 | */ |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3355 | if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) { |
| 3356 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3357 | return X86EMUL_CONTINUE; |
| 3358 | } |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3359 | |
| 3360 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3361 | |
| 3362 | vcpu->mmio_needed = 1; |
| 3363 | vcpu->mmio_phys_addr = gpa; |
| 3364 | vcpu->mmio_size = bytes; |
| 3365 | vcpu->mmio_is_write = 0; |
| 3366 | |
| 3367 | return X86EMUL_UNHANDLEABLE; |
| 3368 | } |
| 3369 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3370 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3371 | const void *val, int bytes) |
| 3372 | { |
| 3373 | int ret; |
| 3374 | |
| 3375 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 3376 | if (ret < 0) |
| 3377 | return 0; |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 3378 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3379 | return 1; |
| 3380 | } |
| 3381 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3382 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 3383 | const void *val, |
| 3384 | unsigned int bytes, |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3385 | struct kvm_vcpu *vcpu) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3386 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3387 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3388 | u32 error_code; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3389 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3390 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3391 | |
| 3392 | if (gpa == UNMAPPED_GVA) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3393 | kvm_inject_page_fault(vcpu, addr, error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3394 | return X86EMUL_PROPAGATE_FAULT; |
| 3395 | } |
| 3396 | |
| 3397 | /* For APIC access vmexit */ |
| 3398 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3399 | goto mmio; |
| 3400 | |
| 3401 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 3402 | return X86EMUL_CONTINUE; |
| 3403 | |
| 3404 | mmio: |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3405 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3406 | /* |
| 3407 | * Is this MMIO handled locally? |
| 3408 | */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3409 | if (!vcpu_mmio_write(vcpu, gpa, bytes, val)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3410 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3411 | |
| 3412 | vcpu->mmio_needed = 1; |
| 3413 | vcpu->mmio_phys_addr = gpa; |
| 3414 | vcpu->mmio_size = bytes; |
| 3415 | vcpu->mmio_is_write = 1; |
| 3416 | memcpy(vcpu->mmio_data, val, bytes); |
| 3417 | |
| 3418 | return X86EMUL_CONTINUE; |
| 3419 | } |
| 3420 | |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3421 | int emulator_write_emulated(unsigned long addr, |
| 3422 | const void *val, |
| 3423 | unsigned int bytes, |
| 3424 | struct kvm_vcpu *vcpu) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3425 | { |
| 3426 | /* Crossing a page boundary? */ |
| 3427 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 3428 | int rc, now; |
| 3429 | |
| 3430 | now = -addr & ~PAGE_MASK; |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3431 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3432 | if (rc != X86EMUL_CONTINUE) |
| 3433 | return rc; |
| 3434 | addr += now; |
| 3435 | val += now; |
| 3436 | bytes -= now; |
| 3437 | } |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3438 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3439 | } |
| 3440 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 3441 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3442 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
| 3443 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) |
| 3444 | |
| 3445 | #ifdef CONFIG_X86_64 |
| 3446 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) |
| 3447 | #else |
| 3448 | # define CMPXCHG64(ptr, old, new) \ |
Jan Kiszka | 9749a6c | 2010-03-20 10:14:13 +0100 | [diff] [blame] | 3449 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3450 | #endif |
| 3451 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3452 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 3453 | const void *old, |
| 3454 | const void *new, |
| 3455 | unsigned int bytes, |
| 3456 | struct kvm_vcpu *vcpu) |
| 3457 | { |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3458 | gpa_t gpa; |
| 3459 | struct page *page; |
| 3460 | char *kaddr; |
| 3461 | bool exchanged; |
| 3462 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3463 | /* guests cmpxchg8b have to be emulated atomically */ |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3464 | if (bytes > 8 || (bytes & (bytes - 1))) |
| 3465 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3466 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3467 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3468 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3469 | if (gpa == UNMAPPED_GVA || |
| 3470 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3471 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3472 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3473 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 3474 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3475 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3476 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3477 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3478 | kaddr = kmap_atomic(page, KM_USER0); |
| 3479 | kaddr += offset_in_page(gpa); |
| 3480 | switch (bytes) { |
| 3481 | case 1: |
| 3482 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); |
| 3483 | break; |
| 3484 | case 2: |
| 3485 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); |
| 3486 | break; |
| 3487 | case 4: |
| 3488 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); |
| 3489 | break; |
| 3490 | case 8: |
| 3491 | exchanged = CMPXCHG64(kaddr, old, new); |
| 3492 | break; |
| 3493 | default: |
| 3494 | BUG(); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3495 | } |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3496 | kunmap_atomic(kaddr, KM_USER0); |
| 3497 | kvm_release_page_dirty(page); |
| 3498 | |
| 3499 | if (!exchanged) |
| 3500 | return X86EMUL_CMPXCHG_FAILED; |
| 3501 | |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3502 | kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1); |
| 3503 | |
| 3504 | return X86EMUL_CONTINUE; |
Avi Kivity | 4a5f48f | 2010-03-15 13:59:55 +0200 | [diff] [blame] | 3505 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3506 | emul_write: |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3507 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3508 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3509 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 3510 | } |
| 3511 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3512 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
| 3513 | { |
| 3514 | /* TODO: String I/O for in kernel device */ |
| 3515 | int r; |
| 3516 | |
| 3517 | if (vcpu->arch.pio.in) |
| 3518 | r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port, |
| 3519 | vcpu->arch.pio.size, pd); |
| 3520 | else |
| 3521 | r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
| 3522 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 3523 | pd); |
| 3524 | return r; |
| 3525 | } |
| 3526 | |
| 3527 | |
| 3528 | static int emulator_pio_in_emulated(int size, unsigned short port, void *val, |
| 3529 | unsigned int count, struct kvm_vcpu *vcpu) |
| 3530 | { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3531 | if (vcpu->arch.pio.count) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3532 | goto data_avail; |
| 3533 | |
| 3534 | trace_kvm_pio(1, port, size, 1); |
| 3535 | |
| 3536 | vcpu->arch.pio.port = port; |
| 3537 | vcpu->arch.pio.in = 1; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3538 | vcpu->arch.pio.count = count; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3539 | vcpu->arch.pio.size = size; |
| 3540 | |
| 3541 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
| 3542 | data_avail: |
| 3543 | memcpy(val, vcpu->arch.pio_data, size * count); |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3544 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3545 | return 1; |
| 3546 | } |
| 3547 | |
| 3548 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3549 | vcpu->run->io.direction = KVM_EXIT_IO_IN; |
| 3550 | vcpu->run->io.size = size; |
| 3551 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 3552 | vcpu->run->io.count = count; |
| 3553 | vcpu->run->io.port = port; |
| 3554 | |
| 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | static int emulator_pio_out_emulated(int size, unsigned short port, |
| 3559 | const void *val, unsigned int count, |
| 3560 | struct kvm_vcpu *vcpu) |
| 3561 | { |
| 3562 | trace_kvm_pio(0, port, size, 1); |
| 3563 | |
| 3564 | vcpu->arch.pio.port = port; |
| 3565 | vcpu->arch.pio.in = 0; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3566 | vcpu->arch.pio.count = count; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3567 | vcpu->arch.pio.size = size; |
| 3568 | |
| 3569 | memcpy(vcpu->arch.pio_data, val, size * count); |
| 3570 | |
| 3571 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3572 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3573 | return 1; |
| 3574 | } |
| 3575 | |
| 3576 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3577 | vcpu->run->io.direction = KVM_EXIT_IO_OUT; |
| 3578 | vcpu->run->io.size = size; |
| 3579 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 3580 | vcpu->run->io.count = count; |
| 3581 | vcpu->run->io.port = port; |
| 3582 | |
| 3583 | return 0; |
| 3584 | } |
| 3585 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3586 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3587 | { |
| 3588 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 3589 | } |
| 3590 | |
| 3591 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 3592 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3593 | kvm_mmu_invlpg(vcpu, address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3594 | return X86EMUL_CONTINUE; |
| 3595 | } |
| 3596 | |
| 3597 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 3598 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3599 | kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 3600 | kvm_x86_ops->fpu_activate(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3601 | return X86EMUL_CONTINUE; |
| 3602 | } |
| 3603 | |
| 3604 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 3605 | { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 3606 | return kvm_get_dr(ctxt->vcpu, dr, dest); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 3610 | { |
| 3611 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3612 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 3613 | return kvm_set_dr(ctxt->vcpu, dr, value & mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 3617 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3618 | u8 opcodes[4]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3619 | unsigned long rip = kvm_rip_read(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3620 | unsigned long rip_linear; |
| 3621 | |
Avi Kivity | f76c710 | 2008-06-13 22:45:42 +0300 | [diff] [blame] | 3622 | if (!printk_ratelimit()) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3623 | return; |
| 3624 | |
Glauber Costa | 25be460 | 2008-06-10 10:46:53 -0300 | [diff] [blame] | 3625 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 3626 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3627 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3628 | |
| 3629 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 3630 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3631 | } |
| 3632 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 3633 | |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3634 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 3635 | { |
| 3636 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 3637 | } |
| 3638 | |
| 3639 | static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu) |
| 3640 | { |
| 3641 | unsigned long value; |
| 3642 | |
| 3643 | switch (cr) { |
| 3644 | case 0: |
| 3645 | value = kvm_read_cr0(vcpu); |
| 3646 | break; |
| 3647 | case 2: |
| 3648 | value = vcpu->arch.cr2; |
| 3649 | break; |
| 3650 | case 3: |
| 3651 | value = vcpu->arch.cr3; |
| 3652 | break; |
| 3653 | case 4: |
| 3654 | value = kvm_read_cr4(vcpu); |
| 3655 | break; |
| 3656 | case 8: |
| 3657 | value = kvm_get_cr8(vcpu); |
| 3658 | break; |
| 3659 | default: |
| 3660 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | return value; |
| 3665 | } |
| 3666 | |
| 3667 | static void emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu) |
| 3668 | { |
| 3669 | switch (cr) { |
| 3670 | case 0: |
| 3671 | kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
| 3672 | break; |
| 3673 | case 2: |
| 3674 | vcpu->arch.cr2 = val; |
| 3675 | break; |
| 3676 | case 3: |
| 3677 | kvm_set_cr3(vcpu, val); |
| 3678 | break; |
| 3679 | case 4: |
| 3680 | kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
| 3681 | break; |
| 3682 | case 8: |
| 3683 | kvm_set_cr8(vcpu, val & 0xfUL); |
| 3684 | break; |
| 3685 | default: |
| 3686 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
| 3687 | } |
| 3688 | } |
| 3689 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 3690 | static int emulator_get_cpl(struct kvm_vcpu *vcpu) |
| 3691 | { |
| 3692 | return kvm_x86_ops->get_cpl(vcpu); |
| 3693 | } |
| 3694 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3695 | static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu) |
| 3696 | { |
| 3697 | kvm_x86_ops->get_gdt(vcpu, dt); |
| 3698 | } |
| 3699 | |
| 3700 | static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg, |
| 3701 | struct kvm_vcpu *vcpu) |
| 3702 | { |
| 3703 | struct kvm_segment var; |
| 3704 | |
| 3705 | kvm_get_segment(vcpu, &var, seg); |
| 3706 | |
| 3707 | if (var.unusable) |
| 3708 | return false; |
| 3709 | |
| 3710 | if (var.g) |
| 3711 | var.limit >>= 12; |
| 3712 | set_desc_limit(desc, var.limit); |
| 3713 | set_desc_base(desc, (unsigned long)var.base); |
| 3714 | desc->type = var.type; |
| 3715 | desc->s = var.s; |
| 3716 | desc->dpl = var.dpl; |
| 3717 | desc->p = var.present; |
| 3718 | desc->avl = var.avl; |
| 3719 | desc->l = var.l; |
| 3720 | desc->d = var.db; |
| 3721 | desc->g = var.g; |
| 3722 | |
| 3723 | return true; |
| 3724 | } |
| 3725 | |
| 3726 | static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg, |
| 3727 | struct kvm_vcpu *vcpu) |
| 3728 | { |
| 3729 | struct kvm_segment var; |
| 3730 | |
| 3731 | /* needed to preserve selector */ |
| 3732 | kvm_get_segment(vcpu, &var, seg); |
| 3733 | |
| 3734 | var.base = get_desc_base(desc); |
| 3735 | var.limit = get_desc_limit(desc); |
| 3736 | if (desc->g) |
| 3737 | var.limit = (var.limit << 12) | 0xfff; |
| 3738 | var.type = desc->type; |
| 3739 | var.present = desc->p; |
| 3740 | var.dpl = desc->dpl; |
| 3741 | var.db = desc->d; |
| 3742 | var.s = desc->s; |
| 3743 | var.l = desc->l; |
| 3744 | var.g = desc->g; |
| 3745 | var.avl = desc->avl; |
| 3746 | var.present = desc->p; |
| 3747 | var.unusable = !var.present; |
| 3748 | var.padding = 0; |
| 3749 | |
| 3750 | kvm_set_segment(vcpu, &var, seg); |
| 3751 | return; |
| 3752 | } |
| 3753 | |
| 3754 | static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu) |
| 3755 | { |
| 3756 | struct kvm_segment kvm_seg; |
| 3757 | |
| 3758 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 3759 | return kvm_seg.selector; |
| 3760 | } |
| 3761 | |
| 3762 | static void emulator_set_segment_selector(u16 sel, int seg, |
| 3763 | struct kvm_vcpu *vcpu) |
| 3764 | { |
| 3765 | struct kvm_segment kvm_seg; |
| 3766 | |
| 3767 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 3768 | kvm_seg.selector = sel; |
| 3769 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 3770 | } |
| 3771 | |
Gleb Natapov | 482ac18 | 2010-03-21 13:08:20 +0200 | [diff] [blame] | 3772 | static void emulator_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 3773 | { |
| 3774 | kvm_x86_ops->set_rflags(vcpu, rflags); |
| 3775 | } |
| 3776 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3777 | static struct x86_emulate_ops emulate_ops = { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3778 | .read_std = kvm_read_guest_virt_system, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3779 | .write_std = kvm_write_guest_virt_system, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3780 | .fetch = kvm_fetch_guest_virt, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3781 | .read_emulated = emulator_read_emulated, |
| 3782 | .write_emulated = emulator_write_emulated, |
| 3783 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3784 | .pio_in_emulated = emulator_pio_in_emulated, |
| 3785 | .pio_out_emulated = emulator_pio_out_emulated, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3786 | .get_cached_descriptor = emulator_get_cached_descriptor, |
| 3787 | .set_cached_descriptor = emulator_set_cached_descriptor, |
| 3788 | .get_segment_selector = emulator_get_segment_selector, |
| 3789 | .set_segment_selector = emulator_set_segment_selector, |
| 3790 | .get_gdt = emulator_get_gdt, |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3791 | .get_cr = emulator_get_cr, |
| 3792 | .set_cr = emulator_set_cr, |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 3793 | .cpl = emulator_get_cpl, |
Gleb Natapov | 482ac18 | 2010-03-21 13:08:20 +0200 | [diff] [blame] | 3794 | .set_rflags = emulator_set_rflags, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3795 | }; |
| 3796 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3797 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 3798 | { |
| 3799 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3800 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3801 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 3802 | vcpu->arch.regs_dirty = ~0; |
| 3803 | } |
| 3804 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3805 | int emulate_instruction(struct kvm_vcpu *vcpu, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3806 | unsigned long cr2, |
| 3807 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3808 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3809 | { |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3810 | int r, shadow_mask; |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3811 | struct decode_cache *c; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3812 | struct kvm_run *run = vcpu->run; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3813 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 3814 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3815 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3816 | /* |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 3817 | * TODO: fix emulate.c to use guest_read/write_register |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3818 | * instead of direct ->regs accesses, can save hundred cycles |
| 3819 | * on Intel for instructions that don't read/change RSP, for |
| 3820 | * for example. |
| 3821 | */ |
| 3822 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3823 | |
| 3824 | vcpu->mmio_is_write = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3825 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3826 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3827 | int cs_db, cs_l; |
| 3828 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 3829 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3830 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
Jan Kiszka | 83bf000 | 2010-02-23 17:47:59 +0100 | [diff] [blame] | 3831 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
Gleb Natapov | 063db06 | 2010-03-18 15:20:06 +0200 | [diff] [blame] | 3832 | vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3833 | vcpu->arch.emulate_ctxt.mode = |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3834 | (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3835 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3836 | ? X86EMUL_MODE_VM86 : cs_l |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3837 | ? X86EMUL_MODE_PROT64 : cs_db |
| 3838 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 3839 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3840 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Avi Kivity | e46479f | 2010-04-11 13:05:16 +0300 | [diff] [blame] | 3841 | trace_kvm_emulate_insn_start(vcpu); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3842 | |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3843 | /* Only allow emulation of specific instructions on #UD |
| 3844 | * (namely VMMCALL, sysenter, sysexit, syscall)*/ |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3845 | c = &vcpu->arch.emulate_ctxt.decode; |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3846 | if (emulation_type & EMULTYPE_TRAP_UD) { |
| 3847 | if (!c->twobyte) |
| 3848 | return EMULATE_FAIL; |
| 3849 | switch (c->b) { |
| 3850 | case 0x01: /* VMMCALL */ |
| 3851 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
| 3852 | return EMULATE_FAIL; |
| 3853 | break; |
| 3854 | case 0x34: /* sysenter */ |
| 3855 | case 0x35: /* sysexit */ |
| 3856 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3857 | return EMULATE_FAIL; |
| 3858 | break; |
| 3859 | case 0x05: /* syscall */ |
| 3860 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3861 | return EMULATE_FAIL; |
| 3862 | break; |
| 3863 | default: |
| 3864 | return EMULATE_FAIL; |
| 3865 | } |
| 3866 | |
| 3867 | if (!(c->modrm_reg == 0 || c->modrm_reg == 3)) |
| 3868 | return EMULATE_FAIL; |
| 3869 | } |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3870 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3871 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3872 | if (r) { |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3873 | ++vcpu->stat.insn_emulation_fail; |
Avi Kivity | e46479f | 2010-04-11 13:05:16 +0300 | [diff] [blame] | 3874 | trace_kvm_emulate_insn_failed(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3875 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 3876 | return EMULATE_DONE; |
| 3877 | return EMULATE_FAIL; |
| 3878 | } |
| 3879 | } |
| 3880 | |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 3881 | if (emulation_type & EMULTYPE_SKIP) { |
| 3882 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip); |
| 3883 | return EMULATE_DONE; |
| 3884 | } |
| 3885 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3886 | restart: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3887 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3888 | shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; |
| 3889 | |
| 3890 | if (r == 0) |
| 3891 | kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3892 | |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3893 | if (vcpu->arch.pio.count) { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3894 | if (!vcpu->arch.pio.in) |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3895 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3896 | return EMULATE_DO_MMIO; |
| 3897 | } |
| 3898 | |
Gleb Natapov | 112592d | 2010-02-21 15:00:47 +0200 | [diff] [blame] | 3899 | if (r || vcpu->mmio_is_write) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3900 | run->exit_reason = KVM_EXIT_MMIO; |
| 3901 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 3902 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 3903 | run->mmio.len = vcpu->mmio_size; |
| 3904 | run->mmio.is_write = vcpu->mmio_is_write; |
| 3905 | } |
| 3906 | |
| 3907 | if (r) { |
| 3908 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3909 | goto done; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3910 | if (!vcpu->mmio_needed) { |
Avi Kivity | e46479f | 2010-04-11 13:05:16 +0300 | [diff] [blame] | 3911 | ++vcpu->stat.insn_emulation_fail; |
| 3912 | trace_kvm_emulate_insn_failed(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3913 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 3914 | return EMULATE_FAIL; |
| 3915 | } |
| 3916 | return EMULATE_DO_MMIO; |
| 3917 | } |
| 3918 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3919 | if (vcpu->mmio_is_write) { |
| 3920 | vcpu->mmio_needed = 0; |
| 3921 | return EMULATE_DO_MMIO; |
| 3922 | } |
| 3923 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 3924 | done: |
| 3925 | if (vcpu->arch.exception.pending) |
| 3926 | vcpu->arch.emulate_ctxt.restart = false; |
| 3927 | |
| 3928 | if (vcpu->arch.emulate_ctxt.restart) |
| 3929 | goto restart; |
| 3930 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3931 | return EMULATE_DONE; |
| 3932 | } |
| 3933 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 3934 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3935 | int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port) |
| 3936 | { |
| 3937 | unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3938 | int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu); |
| 3939 | /* do not return to emulator after return from userspace */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3940 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3941 | return ret; |
| 3942 | } |
| 3943 | EXPORT_SYMBOL_GPL(kvm_fast_pio_out); |
| 3944 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3945 | static void bounce_off(void *info) |
| 3946 | { |
| 3947 | /* nothing */ |
| 3948 | } |
| 3949 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3950 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 3951 | void *data) |
| 3952 | { |
| 3953 | struct cpufreq_freqs *freq = data; |
| 3954 | struct kvm *kvm; |
| 3955 | struct kvm_vcpu *vcpu; |
| 3956 | int i, send_ipi = 0; |
| 3957 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3958 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 3959 | return 0; |
| 3960 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 3961 | return 0; |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 3962 | per_cpu(cpu_tsc_khz, freq->cpu) = freq->new; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3963 | |
| 3964 | spin_lock(&kvm_lock); |
| 3965 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 3966 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3967 | if (vcpu->cpu != freq->cpu) |
| 3968 | continue; |
| 3969 | if (!kvm_request_guest_time_update(vcpu)) |
| 3970 | continue; |
| 3971 | if (vcpu->cpu != smp_processor_id()) |
| 3972 | send_ipi++; |
| 3973 | } |
| 3974 | } |
| 3975 | spin_unlock(&kvm_lock); |
| 3976 | |
| 3977 | if (freq->old < freq->new && send_ipi) { |
| 3978 | /* |
| 3979 | * We upscale the frequency. Must make the guest |
| 3980 | * doesn't see old kvmclock values while running with |
| 3981 | * the new frequency, otherwise we risk the guest sees |
| 3982 | * time go backwards. |
| 3983 | * |
| 3984 | * In case we update the frequency for another cpu |
| 3985 | * (which might be in guest context) send an interrupt |
| 3986 | * to kick the cpu out of guest context. Next time |
| 3987 | * guest context is entered kvmclock will be updated, |
| 3988 | * so the guest will not see stale values. |
| 3989 | */ |
| 3990 | smp_call_function_single(freq->cpu, bounce_off, NULL, 1); |
| 3991 | } |
| 3992 | return 0; |
| 3993 | } |
| 3994 | |
| 3995 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
| 3996 | .notifier_call = kvmclock_cpufreq_notifier |
| 3997 | }; |
| 3998 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3999 | static void kvm_timer_init(void) |
| 4000 | { |
| 4001 | int cpu; |
| 4002 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4003 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4004 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 4005 | CPUFREQ_TRANSITION_NOTIFIER); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 4006 | for_each_online_cpu(cpu) { |
| 4007 | unsigned long khz = cpufreq_get(cpu); |
| 4008 | if (!khz) |
| 4009 | khz = tsc_khz; |
| 4010 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 4011 | } |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 4012 | } else { |
| 4013 | for_each_possible_cpu(cpu) |
| 4014 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4018 | static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
| 4019 | |
| 4020 | static int kvm_is_in_guest(void) |
| 4021 | { |
| 4022 | return percpu_read(current_vcpu) != NULL; |
| 4023 | } |
| 4024 | |
| 4025 | static int kvm_is_user_mode(void) |
| 4026 | { |
| 4027 | int user_mode = 3; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4028 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4029 | if (percpu_read(current_vcpu)) |
| 4030 | user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4031 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4032 | return user_mode != 0; |
| 4033 | } |
| 4034 | |
| 4035 | static unsigned long kvm_get_guest_ip(void) |
| 4036 | { |
| 4037 | unsigned long ip = 0; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4038 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4039 | if (percpu_read(current_vcpu)) |
| 4040 | ip = kvm_rip_read(percpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4041 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4042 | return ip; |
| 4043 | } |
| 4044 | |
| 4045 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
| 4046 | .is_in_guest = kvm_is_in_guest, |
| 4047 | .is_user_mode = kvm_is_user_mode, |
| 4048 | .get_guest_ip = kvm_get_guest_ip, |
| 4049 | }; |
| 4050 | |
| 4051 | void kvm_before_handle_nmi(struct kvm_vcpu *vcpu) |
| 4052 | { |
| 4053 | percpu_write(current_vcpu, vcpu); |
| 4054 | } |
| 4055 | EXPORT_SYMBOL_GPL(kvm_before_handle_nmi); |
| 4056 | |
| 4057 | void kvm_after_handle_nmi(struct kvm_vcpu *vcpu) |
| 4058 | { |
| 4059 | percpu_write(current_vcpu, NULL); |
| 4060 | } |
| 4061 | EXPORT_SYMBOL_GPL(kvm_after_handle_nmi); |
| 4062 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4063 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 4064 | { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4065 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4066 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 4067 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4068 | if (kvm_x86_ops) { |
| 4069 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4070 | r = -EEXIST; |
| 4071 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | if (!ops->cpu_has_kvm_support()) { |
| 4075 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4076 | r = -EOPNOTSUPP; |
| 4077 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4078 | } |
| 4079 | if (ops->disabled_by_bios()) { |
| 4080 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4081 | r = -EOPNOTSUPP; |
| 4082 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4083 | } |
| 4084 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 4085 | r = kvm_mmu_module_init(); |
| 4086 | if (r) |
| 4087 | goto out; |
| 4088 | |
| 4089 | kvm_init_msr_list(); |
| 4090 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4091 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4092 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 4093 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 4094 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4095 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4096 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4097 | kvm_timer_init(); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4098 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4099 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
| 4100 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4101 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4102 | |
| 4103 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4104 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 4105 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4106 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4107 | void kvm_arch_exit(void) |
| 4108 | { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4109 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
| 4110 | |
Jan Kiszka | 888d256 | 2009-04-17 19:24:58 +0200 | [diff] [blame] | 4111 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 4112 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 4113 | CPUFREQ_TRANSITION_NOTIFIER); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4114 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4115 | kvm_mmu_module_exit(); |
| 4116 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4117 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4118 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 4119 | { |
| 4120 | ++vcpu->stat.halt_exits; |
| 4121 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4122 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4123 | return 1; |
| 4124 | } else { |
| 4125 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 4126 | return 0; |
| 4127 | } |
| 4128 | } |
| 4129 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 4130 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4131 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 4132 | unsigned long a1) |
| 4133 | { |
| 4134 | if (is_long_mode(vcpu)) |
| 4135 | return a0; |
| 4136 | else |
| 4137 | return a0 | ((gpa_t)a1 << 32); |
| 4138 | } |
| 4139 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4140 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 4141 | { |
| 4142 | u64 param, ingpa, outgpa, ret; |
| 4143 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 4144 | bool fast, longmode; |
| 4145 | int cs_db, cs_l; |
| 4146 | |
| 4147 | /* |
| 4148 | * hypercall generates UD from non zero cpl and real mode |
| 4149 | * per HYPER-V spec |
| 4150 | */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 4151 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4152 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4153 | return 0; |
| 4154 | } |
| 4155 | |
| 4156 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 4157 | longmode = is_long_mode(vcpu) && cs_l == 1; |
| 4158 | |
| 4159 | if (!longmode) { |
Gleb Natapov | ccd4693 | 2010-01-19 15:06:38 +0200 | [diff] [blame] | 4160 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 4161 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 4162 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 4163 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 4164 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 4165 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4166 | } |
| 4167 | #ifdef CONFIG_X86_64 |
| 4168 | else { |
| 4169 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4170 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4171 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 4172 | } |
| 4173 | #endif |
| 4174 | |
| 4175 | code = param & 0xffff; |
| 4176 | fast = (param >> 16) & 0x1; |
| 4177 | rep_cnt = (param >> 32) & 0xfff; |
| 4178 | rep_idx = (param >> 48) & 0xfff; |
| 4179 | |
| 4180 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 4181 | |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 4182 | switch (code) { |
| 4183 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 4184 | kvm_vcpu_on_spin(vcpu); |
| 4185 | break; |
| 4186 | default: |
| 4187 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 4188 | break; |
| 4189 | } |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4190 | |
| 4191 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 4192 | if (longmode) { |
| 4193 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 4194 | } else { |
| 4195 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 4196 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 4197 | } |
| 4198 | |
| 4199 | return 1; |
| 4200 | } |
| 4201 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4202 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 4203 | { |
| 4204 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4205 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4206 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4207 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 4208 | return kvm_hv_hypercall(vcpu); |
| 4209 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4210 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4211 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4212 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4213 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4214 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4215 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4216 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4217 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4218 | if (!is_long_mode(vcpu)) { |
| 4219 | nr &= 0xFFFFFFFF; |
| 4220 | a0 &= 0xFFFFFFFF; |
| 4221 | a1 &= 0xFFFFFFFF; |
| 4222 | a2 &= 0xFFFFFFFF; |
| 4223 | a3 &= 0xFFFFFFFF; |
| 4224 | } |
| 4225 | |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 4226 | if (kvm_x86_ops->get_cpl(vcpu) != 0) { |
| 4227 | ret = -KVM_EPERM; |
| 4228 | goto out; |
| 4229 | } |
| 4230 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4231 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4232 | case KVM_HC_VAPIC_POLL_IRQ: |
| 4233 | ret = 0; |
| 4234 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4235 | case KVM_HC_MMU_OP: |
| 4236 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 4237 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4238 | default: |
| 4239 | ret = -KVM_ENOSYS; |
| 4240 | break; |
| 4241 | } |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 4242 | out: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4243 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 4244 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4245 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4246 | } |
| 4247 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 4248 | |
| 4249 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 4250 | { |
| 4251 | char instruction[3]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4252 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4253 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4254 | /* |
| 4255 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 4256 | * to ensure that the updated hypercall appears atomically across all |
| 4257 | * VCPUs. |
| 4258 | */ |
| 4259 | kvm_mmu_zap_all(vcpu->kvm); |
| 4260 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4261 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4262 | |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 4263 | return emulator_write_emulated(rip, instruction, 3, vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4264 | } |
| 4265 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4266 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4267 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4268 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4269 | |
| 4270 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 4271 | } |
| 4272 | |
| 4273 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4274 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4275 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4276 | |
| 4277 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 4278 | } |
| 4279 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4280 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 4281 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4282 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 4283 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4284 | |
| 4285 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4286 | /* 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] | 4287 | for (j = i + 1; ; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4288 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4289 | if (ej->function == e->function) { |
| 4290 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4291 | return j; |
| 4292 | } |
| 4293 | } |
| 4294 | return 0; /* silence gcc, even though control never reaches here */ |
| 4295 | } |
| 4296 | |
| 4297 | /* find an entry with matching function, matching index (if needed), and that |
| 4298 | * should be read next (if it's stateful) */ |
| 4299 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 4300 | u32 function, u32 index) |
| 4301 | { |
| 4302 | if (e->function != function) |
| 4303 | return 0; |
| 4304 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 4305 | return 0; |
| 4306 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4307 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4308 | return 0; |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4312 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 4313 | u32 function, u32 index) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4314 | { |
| 4315 | int i; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4316 | struct kvm_cpuid_entry2 *best = NULL; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4317 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4318 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4319 | struct kvm_cpuid_entry2 *e; |
| 4320 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4321 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4322 | if (is_matching_cpuid_entry(e, function, index)) { |
| 4323 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 4324 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4325 | best = e; |
| 4326 | break; |
| 4327 | } |
| 4328 | /* |
| 4329 | * Both basic or both extended? |
| 4330 | */ |
| 4331 | if (((e->function ^ function) & 0x80000000) == 0) |
| 4332 | if (!best || e->function > best->function) |
| 4333 | best = e; |
| 4334 | } |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4335 | return best; |
| 4336 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4337 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4338 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4339 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
| 4340 | { |
| 4341 | struct kvm_cpuid_entry2 *best; |
| 4342 | |
Avi Kivity | f7a7119 | 2010-04-11 15:33:32 +0300 | [diff] [blame] | 4343 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); |
| 4344 | if (!best || best->eax < 0x80000008) |
| 4345 | goto not_found; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4346 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 4347 | if (best) |
| 4348 | return best->eax & 0xff; |
Avi Kivity | f7a7119 | 2010-04-11 15:33:32 +0300 | [diff] [blame] | 4349 | not_found: |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4350 | return 36; |
| 4351 | } |
| 4352 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4353 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 4354 | { |
| 4355 | u32 function, index; |
| 4356 | struct kvm_cpuid_entry2 *best; |
| 4357 | |
| 4358 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4359 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4360 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 4361 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 4362 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 4363 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
| 4364 | best = kvm_find_cpuid_entry(vcpu, function, index); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4365 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4366 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 4367 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 4368 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 4369 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4370 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4371 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4372 | trace_kvm_cpuid(function, |
| 4373 | kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 4374 | kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 4375 | kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 4376 | kvm_register_read(vcpu, VCPU_REGS_RDX)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4377 | } |
| 4378 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4379 | |
| 4380 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4381 | * Check if userspace requested an interrupt window, and that the |
| 4382 | * interrupt window is open. |
| 4383 | * |
| 4384 | * No need to exit to userspace if we already have an interrupt queued. |
| 4385 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4386 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4387 | { |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4388 | return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4389 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 5df5664 | 2009-04-21 17:44:59 +0300 | [diff] [blame] | 4390 | kvm_arch_interrupt_allowed(vcpu)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4391 | } |
| 4392 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4393 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4394 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4395 | struct kvm_run *kvm_run = vcpu->run; |
| 4396 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4397 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4398 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4399 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4400 | if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4401 | kvm_run->ready_for_interrupt_injection = 1; |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4402 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4403 | kvm_run->ready_for_interrupt_injection = |
Gleb Natapov | fa9726b | 2009-05-11 13:35:47 +0300 | [diff] [blame] | 4404 | kvm_arch_interrupt_allowed(vcpu) && |
| 4405 | !kvm_cpu_has_interrupt(vcpu) && |
| 4406 | !kvm_event_needs_reinjection(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4407 | } |
| 4408 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4409 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 4410 | { |
| 4411 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 4412 | struct page *page; |
| 4413 | |
| 4414 | if (!apic || !apic->vapic_addr) |
| 4415 | return; |
| 4416 | |
| 4417 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4418 | |
| 4419 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 4423 | { |
| 4424 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4425 | int idx; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4426 | |
| 4427 | if (!apic || !apic->vapic_addr) |
| 4428 | return; |
| 4429 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4430 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4431 | kvm_release_page_dirty(apic->vapic_page); |
| 4432 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4433 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4434 | } |
| 4435 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4436 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 4437 | { |
| 4438 | int max_irr, tpr; |
| 4439 | |
| 4440 | if (!kvm_x86_ops->update_cr8_intercept) |
| 4441 | return; |
| 4442 | |
Avi Kivity | 88c808f | 2009-08-17 22:49:40 +0300 | [diff] [blame] | 4443 | if (!vcpu->arch.apic) |
| 4444 | return; |
| 4445 | |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4446 | if (!vcpu->arch.apic->vapic_addr) |
| 4447 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 4448 | else |
| 4449 | max_irr = -1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4450 | |
| 4451 | if (max_irr != -1) |
| 4452 | max_irr >>= 4; |
| 4453 | |
| 4454 | tpr = kvm_lapic_get_cr8(vcpu); |
| 4455 | |
| 4456 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); |
| 4457 | } |
| 4458 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4459 | static void inject_pending_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4460 | { |
| 4461 | /* try to reinject previous events if any */ |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4462 | if (vcpu->arch.exception.pending) { |
Avi Kivity | 5c1c85d0 | 2010-03-11 13:01:59 +0200 | [diff] [blame] | 4463 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
| 4464 | vcpu->arch.exception.has_error_code, |
| 4465 | vcpu->arch.exception.error_code); |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4466 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 4467 | vcpu->arch.exception.has_error_code, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 4468 | vcpu->arch.exception.error_code, |
| 4469 | vcpu->arch.exception.reinject); |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4470 | return; |
| 4471 | } |
| 4472 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4473 | if (vcpu->arch.nmi_injected) { |
| 4474 | kvm_x86_ops->set_nmi(vcpu); |
| 4475 | return; |
| 4476 | } |
| 4477 | |
| 4478 | if (vcpu->arch.interrupt.pending) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4479 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4480 | return; |
| 4481 | } |
| 4482 | |
| 4483 | /* try to inject new event if pending */ |
| 4484 | if (vcpu->arch.nmi_pending) { |
| 4485 | if (kvm_x86_ops->nmi_allowed(vcpu)) { |
| 4486 | vcpu->arch.nmi_pending = false; |
| 4487 | vcpu->arch.nmi_injected = true; |
| 4488 | kvm_x86_ops->set_nmi(vcpu); |
| 4489 | } |
| 4490 | } else if (kvm_cpu_has_interrupt(vcpu)) { |
| 4491 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4492 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
| 4493 | false); |
| 4494 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4499 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4500 | { |
| 4501 | int r; |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4502 | bool req_int_win = !irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4503 | vcpu->run->request_interrupt_window; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4504 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 4505 | if (vcpu->requests) |
| 4506 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 4507 | kvm_mmu_unload(vcpu); |
| 4508 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4509 | r = kvm_mmu_reload(vcpu); |
| 4510 | if (unlikely(r)) |
| 4511 | goto out; |
| 4512 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4513 | if (vcpu->requests) { |
| 4514 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 4515 | __kvm_migrate_timers(vcpu); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4516 | if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests)) |
| 4517 | kvm_write_guest_time(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 4518 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
| 4519 | kvm_mmu_sync_roots(vcpu); |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 4520 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 4521 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4522 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
| 4523 | &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4524 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4525 | r = 0; |
| 4526 | goto out; |
| 4527 | } |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4528 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4529 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4530 | r = 0; |
| 4531 | goto out; |
| 4532 | } |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4533 | if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) { |
| 4534 | vcpu->fpu_active = 0; |
| 4535 | kvm_x86_ops->fpu_deactivate(vcpu); |
| 4536 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4537 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4538 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4539 | preempt_disable(); |
| 4540 | |
| 4541 | kvm_x86_ops->prepare_guest_switch(vcpu); |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 4542 | if (vcpu->fpu_active) |
| 4543 | kvm_load_guest_fpu(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4544 | |
| 4545 | local_irq_disable(); |
| 4546 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4547 | clear_bit(KVM_REQ_KICK, &vcpu->requests); |
| 4548 | smp_mb__after_clear_bit(); |
| 4549 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4550 | if (vcpu->requests || need_resched() || signal_pending(current)) { |
Gleb Natapov | c7f0f24 | 2009-07-07 15:27:32 +0300 | [diff] [blame] | 4551 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Avi Kivity | 6c142801 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 4552 | local_irq_enable(); |
| 4553 | preempt_enable(); |
| 4554 | r = 1; |
| 4555 | goto out; |
| 4556 | } |
| 4557 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4558 | inject_pending_event(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4559 | |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4560 | /* enable NMI/IRQ window open exits if needed */ |
| 4561 | if (vcpu->arch.nmi_pending) |
| 4562 | kvm_x86_ops->enable_nmi_window(vcpu); |
| 4563 | else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) |
| 4564 | kvm_x86_ops->enable_irq_window(vcpu); |
| 4565 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4566 | if (kvm_lapic_enabled(vcpu)) { |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4567 | update_cr8_intercept(vcpu); |
| 4568 | kvm_lapic_sync_to_vapic(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4569 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4570 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4571 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4572 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4573 | kvm_guest_enter(); |
| 4574 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4575 | if (unlikely(vcpu->arch.switch_db_regs)) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4576 | set_debugreg(0, 7); |
| 4577 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 4578 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 4579 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 4580 | set_debugreg(vcpu->arch.eff_db[3], 3); |
| 4581 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4582 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4583 | trace_kvm_entry(vcpu->vcpu_id); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4584 | kvm_x86_ops->run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4585 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4586 | /* |
| 4587 | * If the guest has used debug registers, at least dr7 |
| 4588 | * will be disabled while returning to the host. |
| 4589 | * If we don't have active breakpoints in the host, we don't |
| 4590 | * care about the messed up debug address registers. But if |
| 4591 | * we have some of them active, restore the old state. |
| 4592 | */ |
Frederic Weisbecker | 59d8eb5 | 2009-11-10 11:03:12 +0100 | [diff] [blame] | 4593 | if (hw_breakpoint_active()) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4594 | hw_breakpoint_restore(); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4595 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4596 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4597 | local_irq_enable(); |
| 4598 | |
| 4599 | ++vcpu->stat.exits; |
| 4600 | |
| 4601 | /* |
| 4602 | * We must have an instruction between local_irq_enable() and |
| 4603 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 4604 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 4605 | * But we need to prevent reordering, hence this barrier(): |
| 4606 | */ |
| 4607 | barrier(); |
| 4608 | |
| 4609 | kvm_guest_exit(); |
| 4610 | |
| 4611 | preempt_enable(); |
| 4612 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4613 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4614 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4615 | /* |
| 4616 | * Profile KVM exit RIPs: |
| 4617 | */ |
| 4618 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4619 | unsigned long rip = kvm_rip_read(vcpu); |
| 4620 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4621 | } |
| 4622 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4623 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4624 | kvm_lapic_sync_from_vapic(vcpu); |
| 4625 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4626 | r = kvm_x86_ops->handle_exit(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4627 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4628 | return r; |
| 4629 | } |
| 4630 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4631 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4632 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4633 | { |
| 4634 | int r; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4635 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4636 | |
| 4637 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 4638 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 4639 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4640 | kvm_lapic_reset(vcpu); |
Gleb Natapov | 5f17928 | 2008-10-07 15:42:33 +0200 | [diff] [blame] | 4641 | r = kvm_arch_vcpu_reset(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4642 | if (r) |
| 4643 | return r; |
| 4644 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4645 | } |
| 4646 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4647 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4648 | vapic_enter(vcpu); |
| 4649 | |
| 4650 | r = 1; |
| 4651 | while (r > 0) { |
Gleb Natapov | af2152f | 2008-09-22 14:28:53 +0300 | [diff] [blame] | 4652 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4653 | r = vcpu_enter_guest(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4654 | else { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4655 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4656 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4657 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4658 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests)) |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4659 | { |
| 4660 | switch(vcpu->arch.mp_state) { |
| 4661 | case KVM_MP_STATE_HALTED: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4662 | vcpu->arch.mp_state = |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4663 | KVM_MP_STATE_RUNNABLE; |
| 4664 | case KVM_MP_STATE_RUNNABLE: |
| 4665 | break; |
| 4666 | case KVM_MP_STATE_SIPI_RECEIVED: |
| 4667 | default: |
| 4668 | r = -EINTR; |
| 4669 | break; |
| 4670 | } |
| 4671 | } |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4672 | } |
| 4673 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4674 | if (r <= 0) |
| 4675 | break; |
| 4676 | |
| 4677 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
| 4678 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 4679 | kvm_inject_pending_timer_irqs(vcpu); |
| 4680 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4681 | if (dm_request_for_irq_injection(vcpu)) { |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4682 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4683 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4684 | ++vcpu->stat.request_irq_exits; |
| 4685 | } |
| 4686 | if (signal_pending(current)) { |
| 4687 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4688 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4689 | ++vcpu->stat.signal_exits; |
| 4690 | } |
| 4691 | if (need_resched()) { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4692 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4693 | kvm_resched(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4694 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4698 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4699 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4700 | vapic_exit(vcpu); |
| 4701 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4702 | return r; |
| 4703 | } |
| 4704 | |
| 4705 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4706 | { |
| 4707 | int r; |
| 4708 | sigset_t sigsaved; |
| 4709 | |
| 4710 | vcpu_load(vcpu); |
| 4711 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4712 | if (vcpu->sigset_active) |
| 4713 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 4714 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4715 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 4716 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4717 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4718 | r = -EAGAIN; |
| 4719 | goto out; |
| 4720 | } |
| 4721 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4722 | /* re-sync apic's tpr */ |
| 4723 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4724 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4725 | |
Gleb Natapov | 92bf974 | 2010-03-18 15:20:28 +0200 | [diff] [blame] | 4726 | if (vcpu->arch.pio.count || vcpu->mmio_needed || |
| 4727 | vcpu->arch.emulate_ctxt.restart) { |
| 4728 | if (vcpu->mmio_needed) { |
| 4729 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 4730 | vcpu->mmio_read_completed = 1; |
| 4731 | vcpu->mmio_needed = 0; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 4732 | } |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4733 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4734 | r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE); |
| 4735 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 4736 | if (r == EMULATE_DO_MMIO) { |
| 4737 | r = 0; |
| 4738 | goto out; |
| 4739 | } |
| 4740 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4741 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 4742 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 4743 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4744 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4745 | r = __vcpu_run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4746 | |
| 4747 | out: |
Marcelo Tosatti | f1d86e4 | 2010-05-03 23:04:27 -0300 | [diff] [blame] | 4748 | post_kvm_run_save(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4749 | if (vcpu->sigset_active) |
| 4750 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 4751 | |
| 4752 | vcpu_put(vcpu); |
| 4753 | return r; |
| 4754 | } |
| 4755 | |
| 4756 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4757 | { |
| 4758 | vcpu_load(vcpu); |
| 4759 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4760 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4761 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4762 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4763 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4764 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 4765 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 4766 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 4767 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4768 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4769 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 4770 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 4771 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 4772 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 4773 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 4774 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 4775 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 4776 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4777 | #endif |
| 4778 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4779 | regs->rip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4780 | regs->rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4781 | |
| 4782 | vcpu_put(vcpu); |
| 4783 | |
| 4784 | return 0; |
| 4785 | } |
| 4786 | |
| 4787 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4788 | { |
| 4789 | vcpu_load(vcpu); |
| 4790 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4791 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 4792 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 4793 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 4794 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 4795 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 4796 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 4797 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 4798 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4799 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4800 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 4801 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 4802 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 4803 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 4804 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 4805 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 4806 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 4807 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4808 | #endif |
| 4809 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4810 | kvm_rip_write(vcpu, regs->rip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4811 | kvm_set_rflags(vcpu, regs->rflags); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4812 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 4813 | vcpu->arch.exception.pending = false; |
| 4814 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4815 | vcpu_put(vcpu); |
| 4816 | |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4820 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4821 | { |
| 4822 | struct kvm_segment cs; |
| 4823 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4824 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4825 | *db = cs.db; |
| 4826 | *l = cs.l; |
| 4827 | } |
| 4828 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 4829 | |
| 4830 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 4831 | struct kvm_sregs *sregs) |
| 4832 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4833 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4834 | |
| 4835 | vcpu_load(vcpu); |
| 4836 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4837 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 4838 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 4839 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 4840 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 4841 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 4842 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4843 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4844 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 4845 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4846 | |
| 4847 | kvm_x86_ops->get_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4848 | sregs->idt.limit = dt.size; |
| 4849 | sregs->idt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4850 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4851 | sregs->gdt.limit = dt.size; |
| 4852 | sregs->gdt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4853 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4854 | sregs->cr0 = kvm_read_cr0(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4855 | sregs->cr2 = vcpu->arch.cr2; |
| 4856 | sregs->cr3 = vcpu->arch.cr3; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4857 | sregs->cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4858 | sregs->cr8 = kvm_get_cr8(vcpu); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4859 | sregs->efer = vcpu->arch.efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4860 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 4861 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4862 | memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4863 | |
Gleb Natapov | 36752c9 | 2009-05-11 13:35:53 +0300 | [diff] [blame] | 4864 | if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft) |
Gleb Natapov | 14d0bc1 | 2009-04-21 17:45:11 +0300 | [diff] [blame] | 4865 | set_bit(vcpu->arch.interrupt.nr, |
| 4866 | (unsigned long *)sregs->interrupt_bitmap); |
Gleb Natapov | 16d7a19 | 2009-04-21 17:45:10 +0300 | [diff] [blame] | 4867 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4868 | vcpu_put(vcpu); |
| 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 4873 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 4874 | struct kvm_mp_state *mp_state) |
| 4875 | { |
| 4876 | vcpu_load(vcpu); |
| 4877 | mp_state->mp_state = vcpu->arch.mp_state; |
| 4878 | vcpu_put(vcpu); |
| 4879 | return 0; |
| 4880 | } |
| 4881 | |
| 4882 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 4883 | struct kvm_mp_state *mp_state) |
| 4884 | { |
| 4885 | vcpu_load(vcpu); |
| 4886 | vcpu->arch.mp_state = mp_state->mp_state; |
| 4887 | vcpu_put(vcpu); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4891 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason, |
| 4892 | bool has_error_code, u32 error_code) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4893 | { |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 4894 | int cs_db, cs_l, ret; |
| 4895 | cache_all_regs(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4896 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 4897 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4898 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 4899 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
| 4900 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 4901 | vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu); |
| 4902 | vcpu->arch.emulate_ctxt.mode = |
| 4903 | (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
| 4904 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 4905 | ? X86EMUL_MODE_VM86 : cs_l |
| 4906 | ? X86EMUL_MODE_PROT64 : cs_db |
| 4907 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4908 | |
Gleb Natapov | ceffb45 | 2010-03-18 15:20:19 +0200 | [diff] [blame] | 4909 | ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4910 | tss_selector, reason, has_error_code, |
| 4911 | error_code); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4912 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 4913 | if (ret) |
| 4914 | return EMULATE_FAIL; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4915 | |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 4916 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
| 4917 | return EMULATE_DONE; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4918 | } |
| 4919 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 4920 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4921 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 4922 | struct kvm_sregs *sregs) |
| 4923 | { |
| 4924 | int mmu_reset_needed = 0; |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4925 | int pending_vec, max_bits; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4926 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4927 | |
| 4928 | vcpu_load(vcpu); |
| 4929 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4930 | dt.size = sregs->idt.limit; |
| 4931 | dt.address = sregs->idt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4932 | kvm_x86_ops->set_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4933 | dt.size = sregs->gdt.limit; |
| 4934 | dt.address = sregs->gdt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4935 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 4936 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4937 | vcpu->arch.cr2 = sregs->cr2; |
| 4938 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
Jan Kiszka | dc7e795 | 2009-07-01 20:52:03 +0200 | [diff] [blame] | 4939 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4940 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4941 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4942 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4943 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4944 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4945 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 4946 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4947 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4948 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 4949 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4950 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4951 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4952 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4953 | if (!is_long_mode(vcpu) && is_pae(vcpu)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4954 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4955 | mmu_reset_needed = 1; |
| 4956 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4957 | |
| 4958 | if (mmu_reset_needed) |
| 4959 | kvm_mmu_reset_context(vcpu); |
| 4960 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4961 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 4962 | pending_vec = find_first_bit( |
| 4963 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 4964 | if (pending_vec < max_bits) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4965 | kvm_queue_interrupt(vcpu, pending_vec, false); |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4966 | pr_debug("Set back pending irq %d\n", pending_vec); |
| 4967 | if (irqchip_in_kernel(vcpu->kvm)) |
| 4968 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4969 | } |
| 4970 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4971 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 4972 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 4973 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 4974 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 4975 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 4976 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4977 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4978 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 4979 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4980 | |
Mikhail Ershov | 5f0269f | 2009-08-03 14:58:25 +0300 | [diff] [blame] | 4981 | update_cr8_intercept(vcpu); |
| 4982 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 4983 | /* Older userspace won't unhalt the vcpu on reset. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 4984 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 4985 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 4986 | !is_protmode(vcpu)) |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 4987 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4988 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4989 | vcpu_put(vcpu); |
| 4990 | |
| 4991 | return 0; |
| 4992 | } |
| 4993 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4994 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 4995 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4996 | { |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 4997 | unsigned long rflags; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4998 | int i, r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4999 | |
| 5000 | vcpu_load(vcpu); |
| 5001 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5002 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 5003 | r = -EBUSY; |
| 5004 | if (vcpu->arch.exception.pending) |
| 5005 | goto unlock_out; |
| 5006 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 5007 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5008 | else |
| 5009 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 5010 | } |
| 5011 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5012 | /* |
| 5013 | * Read rflags as long as potentially injected trace flags are still |
| 5014 | * filtered out. |
| 5015 | */ |
| 5016 | rflags = kvm_get_rflags(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5017 | |
| 5018 | vcpu->guest_debug = dbg->control; |
| 5019 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 5020 | vcpu->guest_debug = 0; |
| 5021 | |
| 5022 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5023 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 5024 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
| 5025 | vcpu->arch.switch_db_regs = |
| 5026 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); |
| 5027 | } else { |
| 5028 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 5029 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 5030 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); |
| 5031 | } |
| 5032 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5033 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 5034 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 5035 | get_segment_base(vcpu, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5036 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5037 | /* |
| 5038 | * Trigger an rflags update that will inject or remove the trace |
| 5039 | * flags. |
| 5040 | */ |
| 5041 | kvm_set_rflags(vcpu, rflags); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5042 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5043 | kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 5044 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5045 | r = 0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5046 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5047 | unlock_out: |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5048 | vcpu_put(vcpu); |
| 5049 | |
| 5050 | return r; |
| 5051 | } |
| 5052 | |
| 5053 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5054 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 5055 | * we have asm/x86/processor.h |
| 5056 | */ |
| 5057 | struct fxsave { |
| 5058 | u16 cwd; |
| 5059 | u16 swd; |
| 5060 | u16 twd; |
| 5061 | u16 fop; |
| 5062 | u64 rip; |
| 5063 | u64 rdp; |
| 5064 | u32 mxcsr; |
| 5065 | u32 mxcsr_mask; |
| 5066 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 5067 | #ifdef CONFIG_X86_64 |
| 5068 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 5069 | #else |
| 5070 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 5071 | #endif |
| 5072 | }; |
| 5073 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5074 | /* |
| 5075 | * Translate a guest virtual address to a guest physical address. |
| 5076 | */ |
| 5077 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 5078 | struct kvm_translation *tr) |
| 5079 | { |
| 5080 | unsigned long vaddr = tr->linear_address; |
| 5081 | gpa_t gpa; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5082 | int idx; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5083 | |
| 5084 | vcpu_load(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5085 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5086 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5087 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5088 | tr->physical_address = gpa; |
| 5089 | tr->valid = gpa != UNMAPPED_GVA; |
| 5090 | tr->writeable = 1; |
| 5091 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5092 | vcpu_put(vcpu); |
| 5093 | |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5097 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5098 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5099 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5100 | |
| 5101 | vcpu_load(vcpu); |
| 5102 | |
| 5103 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 5104 | fpu->fcw = fxsave->cwd; |
| 5105 | fpu->fsw = fxsave->swd; |
| 5106 | fpu->ftwx = fxsave->twd; |
| 5107 | fpu->last_opcode = fxsave->fop; |
| 5108 | fpu->last_ip = fxsave->rip; |
| 5109 | fpu->last_dp = fxsave->rdp; |
| 5110 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 5111 | |
| 5112 | vcpu_put(vcpu); |
| 5113 | |
| 5114 | return 0; |
| 5115 | } |
| 5116 | |
| 5117 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5118 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5119 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5120 | |
| 5121 | vcpu_load(vcpu); |
| 5122 | |
| 5123 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 5124 | fxsave->cwd = fpu->fcw; |
| 5125 | fxsave->swd = fpu->fsw; |
| 5126 | fxsave->twd = fpu->ftwx; |
| 5127 | fxsave->fop = fpu->last_opcode; |
| 5128 | fxsave->rip = fpu->last_ip; |
| 5129 | fxsave->rdp = fpu->last_dp; |
| 5130 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 5131 | |
| 5132 | vcpu_put(vcpu); |
| 5133 | |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | void fx_init(struct kvm_vcpu *vcpu) |
| 5138 | { |
| 5139 | unsigned after_mxcsr_mask; |
| 5140 | |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5141 | /* |
| 5142 | * Touch the fpu the first time in non atomic context as if |
| 5143 | * this is the first fpu instruction the exception handler |
| 5144 | * will fire before the instruction returns and it'll have to |
| 5145 | * allocate ram with GFP_KERNEL. |
| 5146 | */ |
| 5147 | if (!used_math()) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5148 | kvm_fx_save(&vcpu->arch.host_fx_image); |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5149 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5150 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 5151 | preempt_disable(); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5152 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5153 | kvm_fx_finit(); |
| 5154 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5155 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5156 | preempt_enable(); |
| 5157 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5158 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5159 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5160 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
| 5161 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5162 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 5163 | } |
| 5164 | EXPORT_SYMBOL_GPL(fx_init); |
| 5165 | |
| 5166 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 5167 | { |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 5168 | if (vcpu->guest_fpu_loaded) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5169 | return; |
| 5170 | |
| 5171 | vcpu->guest_fpu_loaded = 1; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5172 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5173 | kvm_fx_restore(&vcpu->arch.guest_fx_image); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5174 | trace_kvm_fpu(1); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5175 | } |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5176 | |
| 5177 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 5178 | { |
| 5179 | if (!vcpu->guest_fpu_loaded) |
| 5180 | return; |
| 5181 | |
| 5182 | vcpu->guest_fpu_loaded = 0; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5183 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5184 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 5185 | ++vcpu->stat.fpu_reload; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 5186 | set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5187 | trace_kvm_fpu(0); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5188 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5189 | |
| 5190 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 5191 | { |
Joerg Roedel | 7f1ea20 | 2009-02-25 16:08:31 +0100 | [diff] [blame] | 5192 | if (vcpu->arch.time_page) { |
| 5193 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 5194 | vcpu->arch.time_page = NULL; |
| 5195 | } |
| 5196 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5197 | kvm_x86_ops->vcpu_free(vcpu); |
| 5198 | } |
| 5199 | |
| 5200 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 5201 | unsigned int id) |
| 5202 | { |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5203 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 5204 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5205 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5206 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 5207 | { |
| 5208 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5209 | |
| 5210 | /* We do fxsave: this must be aligned. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5211 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5212 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 5213 | vcpu->arch.mtrr_state.have_fixed = 1; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5214 | vcpu_load(vcpu); |
| 5215 | r = kvm_arch_vcpu_reset(vcpu); |
| 5216 | if (r == 0) |
| 5217 | r = kvm_mmu_setup(vcpu); |
| 5218 | vcpu_put(vcpu); |
| 5219 | if (r < 0) |
| 5220 | goto free_vcpu; |
| 5221 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5222 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5223 | free_vcpu: |
| 5224 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5225 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5226 | } |
| 5227 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 5228 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5229 | { |
| 5230 | vcpu_load(vcpu); |
| 5231 | kvm_mmu_unload(vcpu); |
| 5232 | vcpu_put(vcpu); |
| 5233 | |
| 5234 | kvm_x86_ops->vcpu_free(vcpu); |
| 5235 | } |
| 5236 | |
| 5237 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 5238 | { |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 5239 | vcpu->arch.nmi_pending = false; |
| 5240 | vcpu->arch.nmi_injected = false; |
| 5241 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5242 | vcpu->arch.switch_db_regs = 0; |
| 5243 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
| 5244 | vcpu->arch.dr6 = DR6_FIXED_1; |
| 5245 | vcpu->arch.dr7 = DR7_FIXED_1; |
| 5246 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5247 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 5248 | } |
| 5249 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5250 | int kvm_arch_hardware_enable(void *garbage) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5251 | { |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 5252 | /* |
| 5253 | * Since this may be called from a hotplug notifcation, |
| 5254 | * we can't get the CPU frequency directly. |
| 5255 | */ |
| 5256 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
| 5257 | int cpu = raw_smp_processor_id(); |
| 5258 | per_cpu(cpu_tsc_khz, cpu) = 0; |
| 5259 | } |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 5260 | |
| 5261 | kvm_shared_msr_cpu_online(); |
| 5262 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5263 | return kvm_x86_ops->hardware_enable(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | void kvm_arch_hardware_disable(void *garbage) |
| 5267 | { |
| 5268 | kvm_x86_ops->hardware_disable(garbage); |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 5269 | drop_user_return_notifiers(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | int kvm_arch_hardware_setup(void) |
| 5273 | { |
| 5274 | return kvm_x86_ops->hardware_setup(); |
| 5275 | } |
| 5276 | |
| 5277 | void kvm_arch_hardware_unsetup(void) |
| 5278 | { |
| 5279 | kvm_x86_ops->hardware_unsetup(); |
| 5280 | } |
| 5281 | |
| 5282 | void kvm_arch_check_processor_compat(void *rtn) |
| 5283 | { |
| 5284 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 5285 | } |
| 5286 | |
| 5287 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 5288 | { |
| 5289 | struct page *page; |
| 5290 | struct kvm *kvm; |
| 5291 | int r; |
| 5292 | |
| 5293 | BUG_ON(vcpu->kvm == NULL); |
| 5294 | kvm = vcpu->kvm; |
| 5295 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5296 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5297 | if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu)) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5298 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5299 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5300 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5301 | |
| 5302 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 5303 | if (!page) { |
| 5304 | r = -ENOMEM; |
| 5305 | goto fail; |
| 5306 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5307 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5308 | |
| 5309 | r = kvm_mmu_create(vcpu); |
| 5310 | if (r < 0) |
| 5311 | goto fail_free_pio_data; |
| 5312 | |
| 5313 | if (irqchip_in_kernel(kvm)) { |
| 5314 | r = kvm_create_lapic(vcpu); |
| 5315 | if (r < 0) |
| 5316 | goto fail_mmu_destroy; |
| 5317 | } |
| 5318 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5319 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 5320 | GFP_KERNEL); |
| 5321 | if (!vcpu->arch.mce_banks) { |
| 5322 | r = -ENOMEM; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5323 | goto fail_free_lapic; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5324 | } |
| 5325 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 5326 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5327 | return 0; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5328 | fail_free_lapic: |
| 5329 | kvm_free_lapic(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5330 | fail_mmu_destroy: |
| 5331 | kvm_mmu_destroy(vcpu); |
| 5332 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5333 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5334 | fail: |
| 5335 | return r; |
| 5336 | } |
| 5337 | |
| 5338 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 5339 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5340 | int idx; |
| 5341 | |
Wei Yongjun | 36cb93f | 2010-01-22 14:18:47 +0800 | [diff] [blame] | 5342 | kfree(vcpu->arch.mce_banks); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5343 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5344 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5345 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5346 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5347 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5348 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5349 | |
| 5350 | struct kvm *kvm_arch_create_vm(void) |
| 5351 | { |
| 5352 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 5353 | |
| 5354 | if (!kvm) |
| 5355 | return ERR_PTR(-ENOMEM); |
| 5356 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5357 | kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 5358 | if (!kvm->arch.aliases) { |
| 5359 | kfree(kvm); |
| 5360 | return ERR_PTR(-ENOMEM); |
| 5361 | } |
| 5362 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5363 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 5364 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5365 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 5366 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 5367 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 5368 | |
Marcelo Tosatti | 53f658b3 | 2008-12-11 20:45:05 +0100 | [diff] [blame] | 5369 | rdtscll(kvm->arch.vm_init_tsc); |
| 5370 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5371 | return kvm; |
| 5372 | } |
| 5373 | |
| 5374 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 5375 | { |
| 5376 | vcpu_load(vcpu); |
| 5377 | kvm_mmu_unload(vcpu); |
| 5378 | vcpu_put(vcpu); |
| 5379 | } |
| 5380 | |
| 5381 | static void kvm_free_vcpus(struct kvm *kvm) |
| 5382 | { |
| 5383 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5384 | struct kvm_vcpu *vcpu; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5385 | |
| 5386 | /* |
| 5387 | * Unpin any mmu pages first. |
| 5388 | */ |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5389 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5390 | kvm_unload_vcpu_mmu(vcpu); |
| 5391 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5392 | kvm_arch_vcpu_free(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5393 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5394 | mutex_lock(&kvm->lock); |
| 5395 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 5396 | kvm->vcpus[i] = NULL; |
| 5397 | |
| 5398 | atomic_set(&kvm->online_vcpus, 0); |
| 5399 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5400 | } |
| 5401 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5402 | void kvm_arch_sync_events(struct kvm *kvm) |
| 5403 | { |
Sheng Yang | ba4cef3 | 2009-01-06 10:03:03 +0800 | [diff] [blame] | 5404 | kvm_free_all_assigned_devices(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5405 | } |
| 5406 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5407 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 5408 | { |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 5409 | kvm_iommu_unmap_guest(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 5410 | kvm_free_pit(kvm); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 5411 | kfree(kvm->arch.vpic); |
| 5412 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5413 | kvm_free_vcpus(kvm); |
| 5414 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 5415 | if (kvm->arch.apic_access_page) |
| 5416 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5417 | if (kvm->arch.ept_identity_pagetable) |
| 5418 | put_page(kvm->arch.ept_identity_pagetable); |
Marcelo Tosatti | 6474920 | 2010-01-19 12:45:23 -0200 | [diff] [blame] | 5419 | cleanup_srcu_struct(&kvm->srcu); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5420 | kfree(kvm->arch.aliases); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5421 | kfree(kvm); |
| 5422 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5423 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5424 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 5425 | struct kvm_memory_slot *memslot, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5426 | struct kvm_memory_slot old, |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5427 | struct kvm_userspace_memory_region *mem, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5428 | int user_alloc) |
| 5429 | { |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5430 | int npages = memslot->npages; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5431 | |
| 5432 | /*To keep backward compatibility with older userspace, |
| 5433 | *x86 needs to hanlde !user_alloc case. |
| 5434 | */ |
| 5435 | if (!user_alloc) { |
| 5436 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5437 | unsigned long userspace_addr; |
| 5438 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5439 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5440 | userspace_addr = do_mmap(NULL, 0, |
| 5441 | npages * PAGE_SIZE, |
| 5442 | PROT_READ | PROT_WRITE, |
Avi Kivity | acee3c0 | 2008-08-26 17:22:47 +0300 | [diff] [blame] | 5443 | MAP_PRIVATE | MAP_ANONYMOUS, |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5444 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5445 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5446 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5447 | if (IS_ERR((void *)userspace_addr)) |
| 5448 | return PTR_ERR((void *)userspace_addr); |
| 5449 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5450 | memslot->userspace_addr = userspace_addr; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5451 | } |
| 5452 | } |
| 5453 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5454 | |
| 5455 | return 0; |
| 5456 | } |
| 5457 | |
| 5458 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 5459 | struct kvm_userspace_memory_region *mem, |
| 5460 | struct kvm_memory_slot old, |
| 5461 | int user_alloc) |
| 5462 | { |
| 5463 | |
| 5464 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 5465 | |
| 5466 | if (!user_alloc && !old.user_alloc && old.rmap && !npages) { |
| 5467 | int ret; |
| 5468 | |
| 5469 | down_write(¤t->mm->mmap_sem); |
| 5470 | ret = do_munmap(current->mm, old.userspace_addr, |
| 5471 | old.npages * PAGE_SIZE); |
| 5472 | up_write(¤t->mm->mmap_sem); |
| 5473 | if (ret < 0) |
| 5474 | printk(KERN_WARNING |
| 5475 | "kvm_vm_ioctl_set_memory_region: " |
| 5476 | "failed to munmap memory\n"); |
| 5477 | } |
| 5478 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5479 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5480 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5481 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 5482 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 5483 | } |
| 5484 | |
| 5485 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5486 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5487 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5488 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5489 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 5490 | { |
| 5491 | kvm_mmu_zap_all(kvm); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 5492 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5493 | } |
| 5494 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5495 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 5496 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5497 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 5498 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
| 5499 | || vcpu->arch.nmi_pending || |
| 5500 | (kvm_arch_interrupt_allowed(vcpu) && |
| 5501 | kvm_cpu_has_interrupt(vcpu)); |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5502 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5503 | |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5504 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 5505 | { |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5506 | int me; |
| 5507 | int cpu = vcpu->cpu; |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5508 | |
| 5509 | if (waitqueue_active(&vcpu->wq)) { |
| 5510 | wake_up_interruptible(&vcpu->wq); |
| 5511 | ++vcpu->stat.halt_wakeup; |
| 5512 | } |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5513 | |
| 5514 | me = get_cpu(); |
| 5515 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) |
| 5516 | if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests)) |
| 5517 | smp_send_reschedule(cpu); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 5518 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5519 | } |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5520 | |
| 5521 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5522 | { |
| 5523 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 5524 | } |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5525 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5526 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 5527 | { |
| 5528 | unsigned long current_rip = kvm_rip_read(vcpu) + |
| 5529 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 5530 | |
| 5531 | return current_rip == linear_rip; |
| 5532 | } |
| 5533 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 5534 | |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5535 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 5536 | { |
| 5537 | unsigned long rflags; |
| 5538 | |
| 5539 | rflags = kvm_x86_ops->get_rflags(vcpu); |
| 5540 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5541 | rflags &= ~X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5542 | return rflags; |
| 5543 | } |
| 5544 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 5545 | |
| 5546 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 5547 | { |
| 5548 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5549 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5550 | rflags |= X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5551 | kvm_x86_ops->set_rflags(vcpu, rflags); |
| 5552 | } |
| 5553 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 5554 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5555 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
| 5556 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 5557 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 5558 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 5559 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 5560 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 5561 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 5562 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 5563 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 5564 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 5565 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
Joerg Roedel | 2e554e8 | 2010-02-24 18:59:14 +0100 | [diff] [blame] | 5566 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |