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