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> |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 43 | #include <trace/events/kvm.h> |
Xiao Guangrong | 2ed152a | 2010-03-10 19:00:43 +0800 | [diff] [blame] | 44 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 45 | #define CREATE_TRACE_POINTS |
| 46 | #include "trace.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 47 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 48 | #include <asm/debugreg.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 49 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 50 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 51 | #include <asm/desc.h> |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 52 | #include <asm/mtrr.h> |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 53 | #include <asm/mce.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 54 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 55 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 56 | #define CR0_RESERVED_BITS \ |
| 57 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 58 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 59 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 60 | #define CR4_RESERVED_BITS \ |
| 61 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 62 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 63 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 64 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 65 | |
| 66 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 67 | |
| 68 | #define KVM_MAX_MCE_BANKS 32 |
| 69 | #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P |
| 70 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 71 | /* EFER defaults: |
| 72 | * - enable syscall per default because its emulated by KVM |
| 73 | * - enable LME and LMA per default on 64 bit KVM |
| 74 | */ |
| 75 | #ifdef CONFIG_X86_64 |
| 76 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 77 | #else |
| 78 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 79 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 80 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 81 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 82 | #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] | 83 | |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 84 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 85 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 86 | struct kvm_cpuid_entry2 __user *entries); |
| 87 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 88 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 90 | |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 91 | int ignore_msrs = 0; |
| 92 | module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR); |
| 93 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 94 | #define KVM_NR_SHARED_MSRS 16 |
| 95 | |
| 96 | struct kvm_shared_msrs_global { |
| 97 | int nr; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 98 | u32 msrs[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct kvm_shared_msrs { |
| 102 | struct user_return_notifier urn; |
| 103 | bool registered; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 104 | struct kvm_shared_msr_values { |
| 105 | u64 host; |
| 106 | u64 curr; |
| 107 | } values[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; |
| 111 | static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs); |
| 112 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 113 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 114 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 115 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 116 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 117 | { "invlpg", VCPU_STAT(invlpg) }, |
| 118 | { "exits", VCPU_STAT(exits) }, |
| 119 | { "io_exits", VCPU_STAT(io_exits) }, |
| 120 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 121 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 122 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 123 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 124 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 125 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 126 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 127 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 128 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 129 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 130 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 131 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 132 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 133 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 134 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 135 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 136 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 137 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 138 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 139 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 140 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 141 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 142 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 143 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 144 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 145 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 146 | { NULL } |
| 147 | }; |
| 148 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 149 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 150 | { |
| 151 | unsigned slot; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 152 | struct kvm_shared_msrs *locals |
| 153 | = container_of(urn, struct kvm_shared_msrs, urn); |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 154 | struct kvm_shared_msr_values *values; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 155 | |
| 156 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 157 | values = &locals->values[slot]; |
| 158 | if (values->host != values->curr) { |
| 159 | wrmsrl(shared_msrs_global.msrs[slot], values->host); |
| 160 | values->curr = values->host; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | locals->registered = false; |
| 164 | user_return_notifier_unregister(urn); |
| 165 | } |
| 166 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 167 | static void shared_msr_update(unsigned slot, u32 msr) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 168 | { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 169 | struct kvm_shared_msrs *smsr; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 170 | u64 value; |
| 171 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 172 | smsr = &__get_cpu_var(shared_msrs); |
| 173 | /* only read, and nobody should modify it at this time, |
| 174 | * so don't need lock */ |
| 175 | if (slot >= shared_msrs_global.nr) { |
| 176 | printk(KERN_ERR "kvm: invalid MSR slot!"); |
| 177 | return; |
| 178 | } |
| 179 | rdmsrl_safe(msr, &value); |
| 180 | smsr->values[slot].host = value; |
| 181 | smsr->values[slot].curr = value; |
| 182 | } |
| 183 | |
| 184 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
| 185 | { |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 186 | if (slot >= shared_msrs_global.nr) |
| 187 | shared_msrs_global.nr = slot + 1; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 188 | shared_msrs_global.msrs[slot] = msr; |
| 189 | /* we need ensured the shared_msr_global have been updated */ |
| 190 | smp_wmb(); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 191 | } |
| 192 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); |
| 193 | |
| 194 | static void kvm_shared_msr_cpu_online(void) |
| 195 | { |
| 196 | unsigned i; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < shared_msrs_global.nr; ++i) |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 199 | shared_msr_update(i, shared_msrs_global.msrs[i]); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 202 | void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 203 | { |
| 204 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 205 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 206 | if (((value ^ smsr->values[slot].curr) & mask) == 0) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 207 | return; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 208 | smsr->values[slot].curr = value; |
| 209 | wrmsrl(shared_msrs_global.msrs[slot], value); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 210 | if (!smsr->registered) { |
| 211 | smsr->urn.on_user_return = kvm_on_user_return; |
| 212 | user_return_notifier_register(&smsr->urn); |
| 213 | smsr->registered = true; |
| 214 | } |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); |
| 217 | |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 218 | static void drop_user_return_notifiers(void *ignore) |
| 219 | { |
| 220 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 221 | |
| 222 | if (smsr->registered) |
| 223 | kvm_on_user_return(&smsr->urn); |
| 224 | } |
| 225 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 226 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 227 | { |
| 228 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 229 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 230 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 231 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 232 | } |
| 233 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 234 | |
| 235 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 236 | { |
| 237 | /* TODO: reserve bits check */ |
| 238 | if (irqchip_in_kernel(vcpu->kvm)) |
| 239 | kvm_lapic_set_base(vcpu, data); |
| 240 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 241 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 242 | } |
| 243 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 244 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 245 | #define EXCPT_BENIGN 0 |
| 246 | #define EXCPT_CONTRIBUTORY 1 |
| 247 | #define EXCPT_PF 2 |
| 248 | |
| 249 | static int exception_class(int vector) |
| 250 | { |
| 251 | switch (vector) { |
| 252 | case PF_VECTOR: |
| 253 | return EXCPT_PF; |
| 254 | case DE_VECTOR: |
| 255 | case TS_VECTOR: |
| 256 | case NP_VECTOR: |
| 257 | case SS_VECTOR: |
| 258 | case GP_VECTOR: |
| 259 | return EXCPT_CONTRIBUTORY; |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | return EXCPT_BENIGN; |
| 264 | } |
| 265 | |
| 266 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
| 267 | unsigned nr, bool has_error, u32 error_code) |
| 268 | { |
| 269 | u32 prev_nr; |
| 270 | int class1, class2; |
| 271 | |
| 272 | if (!vcpu->arch.exception.pending) { |
| 273 | queue: |
| 274 | vcpu->arch.exception.pending = true; |
| 275 | vcpu->arch.exception.has_error_code = has_error; |
| 276 | vcpu->arch.exception.nr = nr; |
| 277 | vcpu->arch.exception.error_code = error_code; |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | /* to check exception */ |
| 282 | prev_nr = vcpu->arch.exception.nr; |
| 283 | if (prev_nr == DF_VECTOR) { |
| 284 | /* triple fault -> shutdown */ |
| 285 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 286 | return; |
| 287 | } |
| 288 | class1 = exception_class(prev_nr); |
| 289 | class2 = exception_class(nr); |
| 290 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 291 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
| 292 | /* generate double fault per SDM Table 5-5 */ |
| 293 | vcpu->arch.exception.pending = true; |
| 294 | vcpu->arch.exception.has_error_code = true; |
| 295 | vcpu->arch.exception.nr = DF_VECTOR; |
| 296 | vcpu->arch.exception.error_code = 0; |
| 297 | } else |
| 298 | /* replace previous exception with a new one in a hope |
| 299 | that instruction re-execution will regenerate lost |
| 300 | exception */ |
| 301 | goto queue; |
| 302 | } |
| 303 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 304 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 305 | { |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 306 | kvm_multiple_exception(vcpu, nr, false, 0); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 309 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 310 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 311 | u32 error_code) |
| 312 | { |
| 313 | ++vcpu->stat.pf_guest; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 314 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 315 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 316 | } |
| 317 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 318 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 319 | { |
| 320 | vcpu->arch.nmi_pending = 1; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 323 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 324 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 325 | { |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 326 | kvm_multiple_exception(vcpu, nr, true, error_code); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 327 | } |
| 328 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 329 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 330 | /* |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 331 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 332 | * a #GP and return false. |
| 333 | */ |
| 334 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 335 | { |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 336 | if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl) |
| 337 | return true; |
| 338 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 339 | return false; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 340 | } |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 341 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Load the pae pdptrs. Return true is they are all valid. |
| 345 | */ |
| 346 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 347 | { |
| 348 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 349 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 350 | int i; |
| 351 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 352 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 353 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 354 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 355 | offset * sizeof(u64), sizeof(pdpte)); |
| 356 | if (ret < 0) { |
| 357 | ret = 0; |
| 358 | goto out; |
| 359 | } |
| 360 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 361 | if (is_present_gpte(pdpte[i]) && |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 362 | (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 363 | ret = 0; |
| 364 | goto out; |
| 365 | } |
| 366 | } |
| 367 | ret = 1; |
| 368 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 369 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 370 | __set_bit(VCPU_EXREG_PDPTR, |
| 371 | (unsigned long *)&vcpu->arch.regs_avail); |
| 372 | __set_bit(VCPU_EXREG_PDPTR, |
| 373 | (unsigned long *)&vcpu->arch.regs_dirty); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 374 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 375 | |
| 376 | return ret; |
| 377 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 379 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 380 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 381 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 382 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 383 | bool changed = true; |
| 384 | int r; |
| 385 | |
| 386 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 387 | return false; |
| 388 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 389 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 390 | (unsigned long *)&vcpu->arch.regs_avail)) |
| 391 | return true; |
| 392 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 393 | 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] | 394 | if (r < 0) |
| 395 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 396 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 397 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 398 | |
| 399 | return changed; |
| 400 | } |
| 401 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 402 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 403 | { |
Avi Kivity | f9a48e6 | 2010-01-06 19:10:22 +0200 | [diff] [blame] | 404 | cr0 |= X86_CR0_ET; |
| 405 | |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 406 | #ifdef CONFIG_X86_64 |
| 407 | if (cr0 & 0xffffffff00000000UL) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 408 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 409 | return; |
| 410 | } |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 411 | #endif |
| 412 | |
| 413 | cr0 &= ~CR0_RESERVED_BITS; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 414 | |
| 415 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 416 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
| 420 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 421 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 426 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 427 | if ((vcpu->arch.efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 428 | int cs_db, cs_l; |
| 429 | |
| 430 | if (!is_pae(vcpu)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 431 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 435 | if (cs_l) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 436 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 437 | return; |
| 438 | |
| 439 | } |
| 440 | } else |
| 441 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 442 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 443 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| 447 | } |
| 448 | |
| 449 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 450 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 451 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 452 | return; |
| 453 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 454 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 455 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 456 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 457 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 458 | kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 459 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 460 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 461 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 462 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 463 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 464 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 465 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE; |
| 466 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 467 | if (cr4 & CR4_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 468 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | |
| 472 | if (is_long_mode(vcpu)) { |
| 473 | if (!(cr4 & X86_CR4_PAE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 474 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 475 | return; |
| 476 | } |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 477 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 478 | && ((cr4 ^ old_cr4) & pdptr_bits) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 479 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 480 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
| 484 | if (cr4 & X86_CR4_VMXE) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 485 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 489 | vcpu->arch.cr4 = cr4; |
Avi Kivity | 5a41acc | 2009-01-11 17:19:35 +0200 | [diff] [blame] | 490 | vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 491 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 492 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 493 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 494 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 495 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 496 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 497 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 498 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 499 | kvm_mmu_flush_tlb(vcpu); |
| 500 | return; |
| 501 | } |
| 502 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 503 | if (is_long_mode(vcpu)) { |
| 504 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 505 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | } else { |
| 509 | if (is_pae(vcpu)) { |
| 510 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 511 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 515 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | } |
| 519 | /* |
| 520 | * We don't check reserved bits in nonpae mode, because |
| 521 | * this isn't enforced, and VMware depends on this. |
| 522 | */ |
| 523 | } |
| 524 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 525 | /* |
| 526 | * Does the new cr3 value map to physical memory? (Note, we |
| 527 | * catch an invalid cr3 even in real-mode, because it would |
| 528 | * cause trouble later on when we turn on paging anyway.) |
| 529 | * |
| 530 | * A real CPU would silently accept an invalid cr3 and would |
| 531 | * attempt to use it - with largely undefined (and often hard |
| 532 | * to debug) behavior on the guest side. |
| 533 | */ |
| 534 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 535 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 536 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 537 | vcpu->arch.cr3 = cr3; |
| 538 | vcpu->arch.mmu.new_cr3(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 539 | } |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 540 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 541 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 542 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 543 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 544 | { |
| 545 | if (cr8 & CR8_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 546 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | if (irqchip_in_kernel(vcpu->kvm)) |
| 550 | kvm_lapic_set_tpr(vcpu, cr8); |
| 551 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 552 | vcpu->arch.cr8 = cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 553 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 554 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 555 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 556 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 557 | { |
| 558 | if (irqchip_in_kernel(vcpu->kvm)) |
| 559 | return kvm_lapic_get_cr8(vcpu); |
| 560 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 561 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 562 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 563 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 564 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 565 | static inline u32 bit(int bitno) |
| 566 | { |
| 567 | return 1 << (bitno & 31); |
| 568 | } |
| 569 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 570 | /* |
| 571 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 572 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 573 | * |
| 574 | * This list is modified at module load time to reflect the |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 575 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
| 576 | * kvm-specific. Those are put in the beginning of the list. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 577 | */ |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 578 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 579 | #define KVM_SAVE_MSRS_BEGIN 5 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 580 | static u32 msrs_to_save[] = { |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 581 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 582 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 583 | HV_X64_MSR_APIC_ASSIST_PAGE, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 584 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 585 | MSR_K6_STAR, |
| 586 | #ifdef CONFIG_X86_64 |
| 587 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 588 | #endif |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 589 | 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] | 590 | }; |
| 591 | |
| 592 | static unsigned num_msrs_to_save; |
| 593 | |
| 594 | static u32 emulated_msrs[] = { |
| 595 | MSR_IA32_MISC_ENABLE, |
| 596 | }; |
| 597 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 598 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 599 | { |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 600 | if (efer & efer_reserved_bits) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 601 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | |
| 605 | if (is_paging(vcpu) |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 606 | && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 607 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 611 | if (efer & EFER_FFXSR) { |
| 612 | struct kvm_cpuid_entry2 *feat; |
| 613 | |
| 614 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 615 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) { |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 616 | kvm_inject_gp(vcpu, 0); |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 621 | if (efer & EFER_SVME) { |
| 622 | struct kvm_cpuid_entry2 *feat; |
| 623 | |
| 624 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 625 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 626 | kvm_inject_gp(vcpu, 0); |
| 627 | return; |
| 628 | } |
| 629 | } |
| 630 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 631 | kvm_x86_ops->set_efer(vcpu, efer); |
| 632 | |
| 633 | efer &= ~EFER_LMA; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 634 | efer |= vcpu->arch.efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 635 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 636 | vcpu->arch.efer = efer; |
Avi Kivity | 9645bb56 | 2009-03-31 11:31:54 +0300 | [diff] [blame] | 637 | |
| 638 | vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled; |
| 639 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 642 | void kvm_enable_efer_bits(u64 mask) |
| 643 | { |
| 644 | efer_reserved_bits &= ~mask; |
| 645 | } |
| 646 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 647 | |
| 648 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 649 | /* |
| 650 | * Writes msr value into into the appropriate "register". |
| 651 | * Returns 0 on success, non-0 otherwise. |
| 652 | * Assumes vcpu_load() was already called. |
| 653 | */ |
| 654 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 655 | { |
| 656 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 657 | } |
| 658 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 659 | /* |
| 660 | * Adapt set_msr() to msr_io()'s calling convention |
| 661 | */ |
| 662 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 663 | { |
| 664 | return kvm_set_msr(vcpu, index, *data); |
| 665 | } |
| 666 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 667 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 668 | { |
| 669 | static int version; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 670 | struct pvclock_wall_clock wc; |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 671 | struct timespec boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 672 | |
| 673 | if (!wall_clock) |
| 674 | return; |
| 675 | |
| 676 | version++; |
| 677 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 678 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 679 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 680 | /* |
| 681 | * The guest calculates current wall clock time by adding |
| 682 | * system time (updated by kvm_write_guest_time below) to the |
| 683 | * wall clock specified here. guest system time equals host |
| 684 | * system time for us, thus we must fill in host boot time here. |
| 685 | */ |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 686 | getboottime(&boot); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 687 | |
| 688 | wc.sec = boot.tv_sec; |
| 689 | wc.nsec = boot.tv_nsec; |
| 690 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 691 | |
| 692 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 693 | |
| 694 | version++; |
| 695 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 696 | } |
| 697 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 698 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 699 | { |
| 700 | uint32_t quotient, remainder; |
| 701 | |
| 702 | /* Don't try to replace with do_div(), this one calculates |
| 703 | * "(dividend << 32) / divisor" */ |
| 704 | __asm__ ( "divl %4" |
| 705 | : "=a" (quotient), "=d" (remainder) |
| 706 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 707 | return quotient; |
| 708 | } |
| 709 | |
| 710 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 711 | { |
| 712 | uint64_t nsecs = 1000000000LL; |
| 713 | int32_t shift = 0; |
| 714 | uint64_t tps64; |
| 715 | uint32_t tps32; |
| 716 | |
| 717 | tps64 = tsc_khz * 1000LL; |
| 718 | while (tps64 > nsecs*2) { |
| 719 | tps64 >>= 1; |
| 720 | shift--; |
| 721 | } |
| 722 | |
| 723 | tps32 = (uint32_t)tps64; |
| 724 | while (tps32 <= (uint32_t)nsecs) { |
| 725 | tps32 <<= 1; |
| 726 | shift++; |
| 727 | } |
| 728 | |
| 729 | hv_clock->tsc_shift = shift; |
| 730 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 731 | |
| 732 | 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] | 733 | __func__, tsc_khz, hv_clock->tsc_shift, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 734 | hv_clock->tsc_to_system_mul); |
| 735 | } |
| 736 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 737 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
| 738 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 739 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
| 740 | { |
| 741 | struct timespec ts; |
| 742 | unsigned long flags; |
| 743 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 744 | void *shared_kaddr; |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 745 | unsigned long this_tsc_khz; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 746 | |
| 747 | if ((!vcpu->time_page)) |
| 748 | return; |
| 749 | |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 750 | this_tsc_khz = get_cpu_var(cpu_tsc_khz); |
| 751 | if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) { |
| 752 | kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock); |
| 753 | vcpu->hv_clock_tsc_khz = this_tsc_khz; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 754 | } |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 755 | put_cpu_var(cpu_tsc_khz); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 756 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 757 | /* Keep irq disabled to prevent changes to the clock */ |
| 758 | local_irq_save(flags); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 759 | 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] | 760 | ktime_get_ts(&ts); |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 761 | monotonic_to_bootbased(&ts); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 762 | local_irq_restore(flags); |
| 763 | |
| 764 | /* With all the info we got, fill in the values */ |
| 765 | |
| 766 | vcpu->hv_clock.system_time = ts.tv_nsec + |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 767 | (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset; |
| 768 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 769 | /* |
| 770 | * The interface expects us to write an even number signaling that the |
| 771 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 772 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 773 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 774 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 775 | |
| 776 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 777 | |
| 778 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 779 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 780 | |
| 781 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 782 | |
| 783 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
| 784 | } |
| 785 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 786 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) |
| 787 | { |
| 788 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 789 | |
| 790 | if (!vcpu->time_page) |
| 791 | return 0; |
| 792 | set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests); |
| 793 | return 1; |
| 794 | } |
| 795 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 796 | static bool msr_mtrr_valid(unsigned msr) |
| 797 | { |
| 798 | switch (msr) { |
| 799 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 800 | case MSR_MTRRfix64K_00000: |
| 801 | case MSR_MTRRfix16K_80000: |
| 802 | case MSR_MTRRfix16K_A0000: |
| 803 | case MSR_MTRRfix4K_C0000: |
| 804 | case MSR_MTRRfix4K_C8000: |
| 805 | case MSR_MTRRfix4K_D0000: |
| 806 | case MSR_MTRRfix4K_D8000: |
| 807 | case MSR_MTRRfix4K_E0000: |
| 808 | case MSR_MTRRfix4K_E8000: |
| 809 | case MSR_MTRRfix4K_F0000: |
| 810 | case MSR_MTRRfix4K_F8000: |
| 811 | case MSR_MTRRdefType: |
| 812 | case MSR_IA32_CR_PAT: |
| 813 | return true; |
| 814 | case 0x2f8: |
| 815 | return true; |
| 816 | } |
| 817 | return false; |
| 818 | } |
| 819 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 820 | static bool valid_pat_type(unsigned t) |
| 821 | { |
| 822 | return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */ |
| 823 | } |
| 824 | |
| 825 | static bool valid_mtrr_type(unsigned t) |
| 826 | { |
| 827 | return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */ |
| 828 | } |
| 829 | |
| 830 | static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 831 | { |
| 832 | int i; |
| 833 | |
| 834 | if (!msr_mtrr_valid(msr)) |
| 835 | return false; |
| 836 | |
| 837 | if (msr == MSR_IA32_CR_PAT) { |
| 838 | for (i = 0; i < 8; i++) |
| 839 | if (!valid_pat_type((data >> (i * 8)) & 0xff)) |
| 840 | return false; |
| 841 | return true; |
| 842 | } else if (msr == MSR_MTRRdefType) { |
| 843 | if (data & ~0xcff) |
| 844 | return false; |
| 845 | return valid_mtrr_type(data & 0xff); |
| 846 | } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { |
| 847 | for (i = 0; i < 8 ; i++) |
| 848 | if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) |
| 849 | return false; |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | /* variable MTRRs */ |
| 854 | return valid_mtrr_type(data & 0xff); |
| 855 | } |
| 856 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 857 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 858 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 859 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 860 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 861 | if (!mtrr_valid(vcpu, msr, data)) |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 862 | return 1; |
| 863 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 864 | if (msr == MSR_MTRRdefType) { |
| 865 | vcpu->arch.mtrr_state.def_type = data; |
| 866 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; |
| 867 | } else if (msr == MSR_MTRRfix64K_00000) |
| 868 | p[0] = data; |
| 869 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 870 | p[1 + msr - MSR_MTRRfix16K_80000] = data; |
| 871 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 872 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; |
| 873 | else if (msr == MSR_IA32_CR_PAT) |
| 874 | vcpu->arch.pat = data; |
| 875 | else { /* Variable MTRRs */ |
| 876 | int idx, is_mtrr_mask; |
| 877 | u64 *pt; |
| 878 | |
| 879 | idx = (msr - 0x200) / 2; |
| 880 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 881 | if (!is_mtrr_mask) |
| 882 | pt = |
| 883 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 884 | else |
| 885 | pt = |
| 886 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 887 | *pt = data; |
| 888 | } |
| 889 | |
| 890 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 891 | return 0; |
| 892 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 893 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 894 | static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 895 | { |
| 896 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 897 | unsigned bank_num = mcg_cap & 0xff; |
| 898 | |
| 899 | switch (msr) { |
| 900 | case MSR_IA32_MCG_STATUS: |
| 901 | vcpu->arch.mcg_status = data; |
| 902 | break; |
| 903 | case MSR_IA32_MCG_CTL: |
| 904 | if (!(mcg_cap & MCG_CTL_P)) |
| 905 | return 1; |
| 906 | if (data != 0 && data != ~(u64)0) |
| 907 | return -1; |
| 908 | vcpu->arch.mcg_ctl = data; |
| 909 | break; |
| 910 | default: |
| 911 | if (msr >= MSR_IA32_MC0_CTL && |
| 912 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 913 | u32 offset = msr - MSR_IA32_MC0_CTL; |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 914 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 915 | * some Linux kernels though clear bit 10 in bank 4 to |
| 916 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 917 | * this to avoid an uncatched #GP in the guest |
| 918 | */ |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 919 | if ((offset & 0x3) == 0 && |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 920 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 921 | return -1; |
| 922 | vcpu->arch.mce_banks[offset] = data; |
| 923 | break; |
| 924 | } |
| 925 | return 1; |
| 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 930 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 931 | { |
| 932 | struct kvm *kvm = vcpu->kvm; |
| 933 | int lm = is_long_mode(vcpu); |
| 934 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 935 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 936 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 937 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 938 | u32 page_num = data & ~PAGE_MASK; |
| 939 | u64 page_addr = data & PAGE_MASK; |
| 940 | u8 *page; |
| 941 | int r; |
| 942 | |
| 943 | r = -E2BIG; |
| 944 | if (page_num >= blob_size) |
| 945 | goto out; |
| 946 | r = -ENOMEM; |
| 947 | page = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 948 | if (!page) |
| 949 | goto out; |
| 950 | r = -EFAULT; |
| 951 | if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE)) |
| 952 | goto out_free; |
| 953 | if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE)) |
| 954 | goto out_free; |
| 955 | r = 0; |
| 956 | out_free: |
| 957 | kfree(page); |
| 958 | out: |
| 959 | return r; |
| 960 | } |
| 961 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 962 | static bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 963 | { |
| 964 | return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 965 | } |
| 966 | |
| 967 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 968 | { |
| 969 | bool r = false; |
| 970 | switch (msr) { |
| 971 | case HV_X64_MSR_GUEST_OS_ID: |
| 972 | case HV_X64_MSR_HYPERCALL: |
| 973 | r = true; |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | return r; |
| 978 | } |
| 979 | |
| 980 | static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 981 | { |
| 982 | struct kvm *kvm = vcpu->kvm; |
| 983 | |
| 984 | switch (msr) { |
| 985 | case HV_X64_MSR_GUEST_OS_ID: |
| 986 | kvm->arch.hv_guest_os_id = data; |
| 987 | /* setting guest os id to zero disables hypercall page */ |
| 988 | if (!kvm->arch.hv_guest_os_id) |
| 989 | kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 990 | break; |
| 991 | case HV_X64_MSR_HYPERCALL: { |
| 992 | u64 gfn; |
| 993 | unsigned long addr; |
| 994 | u8 instructions[4]; |
| 995 | |
| 996 | /* if guest os id is not set hypercall should remain disabled */ |
| 997 | if (!kvm->arch.hv_guest_os_id) |
| 998 | break; |
| 999 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1000 | kvm->arch.hv_hypercall = data; |
| 1001 | break; |
| 1002 | } |
| 1003 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 1004 | addr = gfn_to_hva(kvm, gfn); |
| 1005 | if (kvm_is_error_hva(addr)) |
| 1006 | return 1; |
| 1007 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 1008 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 1009 | if (copy_to_user((void __user *)addr, instructions, 4)) |
| 1010 | return 1; |
| 1011 | kvm->arch.hv_hypercall = data; |
| 1012 | break; |
| 1013 | } |
| 1014 | default: |
| 1015 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1016 | "data 0x%llx\n", msr, data); |
| 1017 | return 1; |
| 1018 | } |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1023 | { |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1024 | switch (msr) { |
| 1025 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 1026 | unsigned long addr; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1027 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1028 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 1029 | vcpu->arch.hv_vapic = data; |
| 1030 | break; |
| 1031 | } |
| 1032 | addr = gfn_to_hva(vcpu->kvm, data >> |
| 1033 | HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT); |
| 1034 | if (kvm_is_error_hva(addr)) |
| 1035 | return 1; |
| 1036 | if (clear_user((void __user *)addr, PAGE_SIZE)) |
| 1037 | return 1; |
| 1038 | vcpu->arch.hv_vapic = data; |
| 1039 | break; |
| 1040 | } |
| 1041 | case HV_X64_MSR_EOI: |
| 1042 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1043 | case HV_X64_MSR_ICR: |
| 1044 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1045 | case HV_X64_MSR_TPR: |
| 1046 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
| 1047 | default: |
| 1048 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1049 | "data 0x%llx\n", msr, data); |
| 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1056 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1057 | { |
| 1058 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1059 | case MSR_EFER: |
| 1060 | set_efer(vcpu, data); |
| 1061 | break; |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1062 | case MSR_K7_HWCR: |
| 1063 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
Joerg Roedel | 8249402 | 2010-02-24 18:59:16 +0100 | [diff] [blame] | 1064 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1065 | if (data != 0) { |
| 1066 | pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 1067 | data); |
| 1068 | return 1; |
| 1069 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1070 | break; |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1071 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 1072 | if (data != 0) { |
| 1073 | pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 1074 | "0x%llx\n", data); |
| 1075 | return 1; |
| 1076 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1077 | break; |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1078 | case MSR_AMD64_NB_CFG: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 1079 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1080 | case MSR_IA32_DEBUGCTLMSR: |
| 1081 | if (!data) { |
| 1082 | /* We support the non-activated case already */ |
| 1083 | break; |
| 1084 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 1085 | /* Values other than LBR and BTF are vendor-specific, |
| 1086 | thus reserved and should throw a #GP */ |
| 1087 | return 1; |
| 1088 | } |
| 1089 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 1090 | __func__, data); |
| 1091 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1092 | case MSR_IA32_UCODE_REV: |
| 1093 | case MSR_IA32_UCODE_WRITE: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1094 | case MSR_VM_HSAVE_PA: |
Andre Przywara | 6098ca9 | 2009-07-03 16:00:14 +0200 | [diff] [blame] | 1095 | case MSR_AMD64_PATCH_LOADER: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1096 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1097 | case 0x200 ... 0x2ff: |
| 1098 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1099 | case MSR_IA32_APICBASE: |
| 1100 | kvm_set_apic_base(vcpu, data); |
| 1101 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1102 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1103 | return kvm_x2apic_msr_write(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1104 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1105 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1106 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1107 | case MSR_KVM_WALL_CLOCK: |
| 1108 | vcpu->kvm->arch.wall_clock = data; |
| 1109 | kvm_write_wall_clock(vcpu->kvm, data); |
| 1110 | break; |
| 1111 | case MSR_KVM_SYSTEM_TIME: { |
| 1112 | if (vcpu->arch.time_page) { |
| 1113 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 1114 | vcpu->arch.time_page = NULL; |
| 1115 | } |
| 1116 | |
| 1117 | vcpu->arch.time = data; |
| 1118 | |
| 1119 | /* we verify if the enable bit is set... */ |
| 1120 | if (!(data & 1)) |
| 1121 | break; |
| 1122 | |
| 1123 | /* ...but clean it before doing the actual write */ |
| 1124 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 1125 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1126 | vcpu->arch.time_page = |
| 1127 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1128 | |
| 1129 | if (is_error_page(vcpu->arch.time_page)) { |
| 1130 | kvm_release_page_clean(vcpu->arch.time_page); |
| 1131 | vcpu->arch.time_page = NULL; |
| 1132 | } |
| 1133 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1134 | kvm_request_guest_time_update(vcpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1135 | break; |
| 1136 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1137 | case MSR_IA32_MCG_CTL: |
| 1138 | case MSR_IA32_MCG_STATUS: |
| 1139 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1140 | return set_msr_mce(vcpu, msr, data); |
Andre Przywara | 71db602 | 2009-06-12 22:01:29 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* Performance counters are not protected by a CPUID bit, |
| 1143 | * so we should check all of them in the generic path for the sake of |
| 1144 | * cross vendor migration. |
| 1145 | * Writing a zero into the event select MSRs disables them, |
| 1146 | * which we perfectly emulate ;-). Any other value should be at least |
| 1147 | * reported, some guests depend on them. |
| 1148 | */ |
| 1149 | case MSR_P6_EVNTSEL0: |
| 1150 | case MSR_P6_EVNTSEL1: |
| 1151 | case MSR_K7_EVNTSEL0: |
| 1152 | case MSR_K7_EVNTSEL1: |
| 1153 | case MSR_K7_EVNTSEL2: |
| 1154 | case MSR_K7_EVNTSEL3: |
| 1155 | if (data != 0) |
| 1156 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1157 | "0x%x data 0x%llx\n", msr, data); |
| 1158 | break; |
| 1159 | /* at least RHEL 4 unconditionally writes to the perfctr registers, |
| 1160 | * so we ignore writes to make it happy. |
| 1161 | */ |
| 1162 | case MSR_P6_PERFCTR0: |
| 1163 | case MSR_P6_PERFCTR1: |
| 1164 | case MSR_K7_PERFCTR0: |
| 1165 | case MSR_K7_PERFCTR1: |
| 1166 | case MSR_K7_PERFCTR2: |
| 1167 | case MSR_K7_PERFCTR3: |
| 1168 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1169 | "0x%x data 0x%llx\n", msr, data); |
| 1170 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1171 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1172 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1173 | int r; |
| 1174 | mutex_lock(&vcpu->kvm->lock); |
| 1175 | r = set_msr_hyperv_pw(vcpu, msr, data); |
| 1176 | mutex_unlock(&vcpu->kvm->lock); |
| 1177 | return r; |
| 1178 | } else |
| 1179 | return set_msr_hyperv(vcpu, msr, data); |
| 1180 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1181 | default: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1182 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 1183 | return xen_hvm_config(vcpu, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1184 | if (!ignore_msrs) { |
| 1185 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", |
| 1186 | msr, data); |
| 1187 | return 1; |
| 1188 | } else { |
| 1189 | pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", |
| 1190 | msr, data); |
| 1191 | break; |
| 1192 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1193 | } |
| 1194 | return 0; |
| 1195 | } |
| 1196 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1197 | |
| 1198 | |
| 1199 | /* |
| 1200 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1201 | * Returns 0 on success, non-0 otherwise. |
| 1202 | * Assumes vcpu_load() was already called. |
| 1203 | */ |
| 1204 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1205 | { |
| 1206 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 1207 | } |
| 1208 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1209 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1210 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1211 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 1212 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1213 | if (!msr_mtrr_valid(msr)) |
| 1214 | return 1; |
| 1215 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1216 | if (msr == MSR_MTRRdefType) |
| 1217 | *pdata = vcpu->arch.mtrr_state.def_type + |
| 1218 | (vcpu->arch.mtrr_state.enabled << 10); |
| 1219 | else if (msr == MSR_MTRRfix64K_00000) |
| 1220 | *pdata = p[0]; |
| 1221 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 1222 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; |
| 1223 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 1224 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; |
| 1225 | else if (msr == MSR_IA32_CR_PAT) |
| 1226 | *pdata = vcpu->arch.pat; |
| 1227 | else { /* Variable MTRRs */ |
| 1228 | int idx, is_mtrr_mask; |
| 1229 | u64 *pt; |
| 1230 | |
| 1231 | idx = (msr - 0x200) / 2; |
| 1232 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 1233 | if (!is_mtrr_mask) |
| 1234 | pt = |
| 1235 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 1236 | else |
| 1237 | pt = |
| 1238 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 1239 | *pdata = *pt; |
| 1240 | } |
| 1241 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1245 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1246 | { |
| 1247 | u64 data; |
| 1248 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 1249 | unsigned bank_num = mcg_cap & 0xff; |
| 1250 | |
| 1251 | switch (msr) { |
| 1252 | case MSR_IA32_P5_MC_ADDR: |
| 1253 | case MSR_IA32_P5_MC_TYPE: |
| 1254 | data = 0; |
| 1255 | break; |
| 1256 | case MSR_IA32_MCG_CAP: |
| 1257 | data = vcpu->arch.mcg_cap; |
| 1258 | break; |
| 1259 | case MSR_IA32_MCG_CTL: |
| 1260 | if (!(mcg_cap & MCG_CTL_P)) |
| 1261 | return 1; |
| 1262 | data = vcpu->arch.mcg_ctl; |
| 1263 | break; |
| 1264 | case MSR_IA32_MCG_STATUS: |
| 1265 | data = vcpu->arch.mcg_status; |
| 1266 | break; |
| 1267 | default: |
| 1268 | if (msr >= MSR_IA32_MC0_CTL && |
| 1269 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1270 | u32 offset = msr - MSR_IA32_MC0_CTL; |
| 1271 | data = vcpu->arch.mce_banks[offset]; |
| 1272 | break; |
| 1273 | } |
| 1274 | return 1; |
| 1275 | } |
| 1276 | *pdata = data; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1280 | static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1281 | { |
| 1282 | u64 data = 0; |
| 1283 | struct kvm *kvm = vcpu->kvm; |
| 1284 | |
| 1285 | switch (msr) { |
| 1286 | case HV_X64_MSR_GUEST_OS_ID: |
| 1287 | data = kvm->arch.hv_guest_os_id; |
| 1288 | break; |
| 1289 | case HV_X64_MSR_HYPERCALL: |
| 1290 | data = kvm->arch.hv_hypercall; |
| 1291 | break; |
| 1292 | default: |
| 1293 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1294 | return 1; |
| 1295 | } |
| 1296 | |
| 1297 | *pdata = data; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1302 | { |
| 1303 | u64 data = 0; |
| 1304 | |
| 1305 | switch (msr) { |
| 1306 | case HV_X64_MSR_VP_INDEX: { |
| 1307 | int r; |
| 1308 | struct kvm_vcpu *v; |
| 1309 | kvm_for_each_vcpu(r, v, vcpu->kvm) |
| 1310 | if (v == vcpu) |
| 1311 | data = r; |
| 1312 | break; |
| 1313 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1314 | case HV_X64_MSR_EOI: |
| 1315 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1316 | case HV_X64_MSR_ICR: |
| 1317 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1318 | case HV_X64_MSR_TPR: |
| 1319 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1320 | default: |
| 1321 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1322 | return 1; |
| 1323 | } |
| 1324 | *pdata = data; |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1328 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1329 | { |
| 1330 | u64 data; |
| 1331 | |
| 1332 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1333 | case MSR_IA32_PLATFORM_ID: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1334 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1335 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1336 | case MSR_IA32_DEBUGCTLMSR: |
| 1337 | case MSR_IA32_LASTBRANCHFROMIP: |
| 1338 | case MSR_IA32_LASTBRANCHTOIP: |
| 1339 | case MSR_IA32_LASTINTFROMIP: |
| 1340 | case MSR_IA32_LASTINTTOIP: |
Jaswinder Singh Rajput | 60af2ec | 2009-05-14 11:00:10 +0530 | [diff] [blame] | 1341 | case MSR_K8_SYSCFG: |
| 1342 | case MSR_K7_HWCR: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1343 | case MSR_VM_HSAVE_PA: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1344 | case MSR_P6_PERFCTR0: |
| 1345 | case MSR_P6_PERFCTR1: |
Amit Shah | 7fe29e0 | 2009-03-20 12:39:00 +0530 | [diff] [blame] | 1346 | case MSR_P6_EVNTSEL0: |
| 1347 | case MSR_P6_EVNTSEL1: |
Amit Shah | 9e69962 | 2009-06-15 13:25:34 +0530 | [diff] [blame] | 1348 | case MSR_K7_EVNTSEL0: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1349 | case MSR_K7_PERFCTR0: |
Andre Przywara | 1fdbd48 | 2009-06-24 12:44:34 +0200 | [diff] [blame] | 1350 | case MSR_K8_INT_PENDING_MSG: |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1351 | case MSR_AMD64_NB_CFG: |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1352 | case MSR_FAM10H_MMIO_CONF_BASE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1353 | data = 0; |
| 1354 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1355 | case MSR_MTRRcap: |
| 1356 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 1357 | break; |
| 1358 | case 0x200 ... 0x2ff: |
| 1359 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1360 | case 0xcd: /* fsb frequency */ |
| 1361 | data = 3; |
| 1362 | break; |
| 1363 | case MSR_IA32_APICBASE: |
| 1364 | data = kvm_get_apic_base(vcpu); |
| 1365 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1366 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1367 | return kvm_x2apic_msr_read(vcpu, msr, pdata); |
| 1368 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1369 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1370 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1371 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 1372 | case MSR_IA32_PERF_STATUS: |
| 1373 | /* TSC increment by tick */ |
| 1374 | data = 1000ULL; |
| 1375 | /* CPU multiplier */ |
| 1376 | data |= (((uint64_t)4ULL) << 40); |
| 1377 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1378 | case MSR_EFER: |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1379 | data = vcpu->arch.efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1380 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1381 | case MSR_KVM_WALL_CLOCK: |
| 1382 | data = vcpu->kvm->arch.wall_clock; |
| 1383 | break; |
| 1384 | case MSR_KVM_SYSTEM_TIME: |
| 1385 | data = vcpu->arch.time; |
| 1386 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1387 | case MSR_IA32_P5_MC_ADDR: |
| 1388 | case MSR_IA32_P5_MC_TYPE: |
| 1389 | case MSR_IA32_MCG_CAP: |
| 1390 | case MSR_IA32_MCG_CTL: |
| 1391 | case MSR_IA32_MCG_STATUS: |
| 1392 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1393 | return get_msr_mce(vcpu, msr, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1394 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1395 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1396 | int r; |
| 1397 | mutex_lock(&vcpu->kvm->lock); |
| 1398 | r = get_msr_hyperv_pw(vcpu, msr, pdata); |
| 1399 | mutex_unlock(&vcpu->kvm->lock); |
| 1400 | return r; |
| 1401 | } else |
| 1402 | return get_msr_hyperv(vcpu, msr, pdata); |
| 1403 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1404 | default: |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1405 | if (!ignore_msrs) { |
| 1406 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 1407 | return 1; |
| 1408 | } else { |
| 1409 | pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr); |
| 1410 | data = 0; |
| 1411 | } |
| 1412 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1413 | } |
| 1414 | *pdata = data; |
| 1415 | return 0; |
| 1416 | } |
| 1417 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1418 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1419 | /* |
| 1420 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1421 | * |
| 1422 | * @return number of msrs set successfully. |
| 1423 | */ |
| 1424 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 1425 | struct kvm_msr_entry *entries, |
| 1426 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1427 | unsigned index, u64 *data)) |
| 1428 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1429 | int i, idx; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1430 | |
| 1431 | vcpu_load(vcpu); |
| 1432 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1433 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1434 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1435 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1436 | break; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1437 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1438 | |
| 1439 | vcpu_put(vcpu); |
| 1440 | |
| 1441 | return i; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1446 | * |
| 1447 | * @return number of msrs set successfully. |
| 1448 | */ |
| 1449 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 1450 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1451 | unsigned index, u64 *data), |
| 1452 | int writeback) |
| 1453 | { |
| 1454 | struct kvm_msrs msrs; |
| 1455 | struct kvm_msr_entry *entries; |
| 1456 | int r, n; |
| 1457 | unsigned size; |
| 1458 | |
| 1459 | r = -EFAULT; |
| 1460 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1461 | goto out; |
| 1462 | |
| 1463 | r = -E2BIG; |
| 1464 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1465 | goto out; |
| 1466 | |
| 1467 | r = -ENOMEM; |
| 1468 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 1469 | entries = vmalloc(size); |
| 1470 | if (!entries) |
| 1471 | goto out; |
| 1472 | |
| 1473 | r = -EFAULT; |
| 1474 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1475 | goto out_free; |
| 1476 | |
| 1477 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 1478 | if (r < 0) |
| 1479 | goto out_free; |
| 1480 | |
| 1481 | r = -EFAULT; |
| 1482 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1483 | goto out_free; |
| 1484 | |
| 1485 | r = n; |
| 1486 | |
| 1487 | out_free: |
| 1488 | vfree(entries); |
| 1489 | out: |
| 1490 | return r; |
| 1491 | } |
| 1492 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1493 | int kvm_dev_ioctl_check_extension(long ext) |
| 1494 | { |
| 1495 | int r; |
| 1496 | |
| 1497 | switch (ext) { |
| 1498 | case KVM_CAP_IRQCHIP: |
| 1499 | case KVM_CAP_HLT: |
| 1500 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1501 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1502 | case KVM_CAP_EXT_CPUID: |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1503 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1504 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1505 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1506 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 1507 | case KVM_CAP_SYNC_MMU: |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 1508 | case KVM_CAP_REINJECT_CONTROL: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 1509 | case KVM_CAP_IRQ_INJECT_STATUS: |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 1510 | case KVM_CAP_ASSIGN_DEV_IRQ: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1511 | case KVM_CAP_IRQFD: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 1512 | case KVM_CAP_IOEVENTFD: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 1513 | case KVM_CAP_PIT2: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 1514 | case KVM_CAP_PIT_STATE2: |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 1515 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1516 | case KVM_CAP_XEN_HVM: |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 1517 | case KVM_CAP_ADJUST_CLOCK: |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 1518 | case KVM_CAP_VCPU_EVENTS: |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1519 | case KVM_CAP_HYPERV: |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1520 | case KVM_CAP_HYPERV_VAPIC: |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 1521 | case KVM_CAP_HYPERV_SPIN: |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 1522 | case KVM_CAP_PCI_SEGMENT: |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 1523 | case KVM_CAP_DEBUGREGS: |
Jan Kiszka | d2be165 | 2010-02-23 17:47:57 +0100 | [diff] [blame] | 1524 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1525 | r = 1; |
| 1526 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 1527 | case KVM_CAP_COALESCED_MMIO: |
| 1528 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1529 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1530 | case KVM_CAP_VAPIC: |
| 1531 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 1532 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 1533 | case KVM_CAP_NR_VCPUS: |
| 1534 | r = KVM_MAX_VCPUS; |
| 1535 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 1536 | case KVM_CAP_NR_MEMSLOTS: |
| 1537 | r = KVM_MEMORY_SLOTS; |
| 1538 | break; |
Marcelo Tosatti | a68a6a7 | 2009-10-01 19:28:39 -0300 | [diff] [blame] | 1539 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 1540 | r = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 1541 | break; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1542 | case KVM_CAP_IOMMU: |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 1543 | r = iommu_found(); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1544 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1545 | case KVM_CAP_MCE: |
| 1546 | r = KVM_MAX_MCE_BANKS; |
| 1547 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1548 | default: |
| 1549 | r = 0; |
| 1550 | break; |
| 1551 | } |
| 1552 | return r; |
| 1553 | |
| 1554 | } |
| 1555 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1556 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1557 | unsigned int ioctl, unsigned long arg) |
| 1558 | { |
| 1559 | void __user *argp = (void __user *)arg; |
| 1560 | long r; |
| 1561 | |
| 1562 | switch (ioctl) { |
| 1563 | case KVM_GET_MSR_INDEX_LIST: { |
| 1564 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1565 | struct kvm_msr_list msr_list; |
| 1566 | unsigned n; |
| 1567 | |
| 1568 | r = -EFAULT; |
| 1569 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1570 | goto out; |
| 1571 | n = msr_list.nmsrs; |
| 1572 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1573 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1574 | goto out; |
| 1575 | r = -E2BIG; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1576 | if (n < msr_list.nmsrs) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1577 | goto out; |
| 1578 | r = -EFAULT; |
| 1579 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1580 | num_msrs_to_save * sizeof(u32))) |
| 1581 | goto out; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1582 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1583 | &emulated_msrs, |
| 1584 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1585 | goto out; |
| 1586 | r = 0; |
| 1587 | break; |
| 1588 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1589 | case KVM_GET_SUPPORTED_CPUID: { |
| 1590 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1591 | struct kvm_cpuid2 cpuid; |
| 1592 | |
| 1593 | r = -EFAULT; |
| 1594 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1595 | goto out; |
| 1596 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1597 | cpuid_arg->entries); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1598 | if (r) |
| 1599 | goto out; |
| 1600 | |
| 1601 | r = -EFAULT; |
| 1602 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1603 | goto out; |
| 1604 | r = 0; |
| 1605 | break; |
| 1606 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1607 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
| 1608 | u64 mce_cap; |
| 1609 | |
| 1610 | mce_cap = KVM_MCE_CAP_SUPPORTED; |
| 1611 | r = -EFAULT; |
| 1612 | if (copy_to_user(argp, &mce_cap, sizeof mce_cap)) |
| 1613 | goto out; |
| 1614 | r = 0; |
| 1615 | break; |
| 1616 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1617 | default: |
| 1618 | r = -EINVAL; |
| 1619 | } |
| 1620 | out: |
| 1621 | return r; |
| 1622 | } |
| 1623 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1624 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1625 | { |
| 1626 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 1627 | if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) { |
| 1628 | unsigned long khz = cpufreq_quick_get(cpu); |
| 1629 | if (!khz) |
| 1630 | khz = tsc_khz; |
| 1631 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 1632 | } |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1633 | kvm_request_guest_time_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1637 | { |
Amit Shah | 9327fd1 | 2007-11-15 18:38:46 +0200 | [diff] [blame] | 1638 | kvm_put_guest_fpu(vcpu); |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1639 | kvm_x86_ops->vcpu_put(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1640 | } |
| 1641 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1642 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1643 | { |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1644 | unsigned long long efer = 0; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1645 | |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1646 | rdmsrl_safe(MSR_EFER, &efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1647 | return efer & EFER_NX; |
| 1648 | } |
| 1649 | |
| 1650 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1651 | { |
| 1652 | int i; |
| 1653 | struct kvm_cpuid_entry2 *e, *entry; |
| 1654 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1655 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1656 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1657 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1658 | if (e->function == 0x80000001) { |
| 1659 | entry = e; |
| 1660 | break; |
| 1661 | } |
| 1662 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1663 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1664 | entry->edx &= ~(1 << 20); |
| 1665 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1666 | } |
| 1667 | } |
| 1668 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1669 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1670 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1671 | struct kvm_cpuid *cpuid, |
| 1672 | struct kvm_cpuid_entry __user *entries) |
| 1673 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1674 | int r, i; |
| 1675 | struct kvm_cpuid_entry *cpuid_entries; |
| 1676 | |
| 1677 | r = -E2BIG; |
| 1678 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1679 | goto out; |
| 1680 | r = -ENOMEM; |
| 1681 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1682 | if (!cpuid_entries) |
| 1683 | goto out; |
| 1684 | r = -EFAULT; |
| 1685 | if (copy_from_user(cpuid_entries, entries, |
| 1686 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1687 | goto out_free; |
| 1688 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1689 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1690 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1691 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1692 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1693 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1694 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1695 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1696 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1697 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1698 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1699 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1700 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1701 | cpuid_fix_nx_cap(vcpu); |
| 1702 | r = 0; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1703 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1704 | kvm_x86_ops->cpuid_update(vcpu); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1705 | |
| 1706 | out_free: |
| 1707 | vfree(cpuid_entries); |
| 1708 | out: |
| 1709 | return r; |
| 1710 | } |
| 1711 | |
| 1712 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1713 | struct kvm_cpuid2 *cpuid, |
| 1714 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1715 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1716 | int r; |
| 1717 | |
| 1718 | r = -E2BIG; |
| 1719 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1720 | goto out; |
| 1721 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1722 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1723 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1724 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1725 | vcpu->arch.cpuid_nent = cpuid->nent; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1726 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1727 | kvm_x86_ops->cpuid_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1728 | return 0; |
| 1729 | |
| 1730 | out: |
| 1731 | return r; |
| 1732 | } |
| 1733 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1734 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1735 | struct kvm_cpuid2 *cpuid, |
| 1736 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1737 | { |
| 1738 | int r; |
| 1739 | |
| 1740 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1741 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1742 | goto out; |
| 1743 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1744 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1745 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1746 | goto out; |
| 1747 | return 0; |
| 1748 | |
| 1749 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1750 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1751 | return r; |
| 1752 | } |
| 1753 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1754 | 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] | 1755 | u32 index) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1756 | { |
| 1757 | entry->function = function; |
| 1758 | entry->index = index; |
| 1759 | cpuid_count(entry->function, entry->index, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1760 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1761 | entry->flags = 0; |
| 1762 | } |
| 1763 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1764 | #define F(x) bit(X86_FEATURE_##x) |
| 1765 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1766 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1767 | u32 index, int *nent, int maxnent) |
| 1768 | { |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1769 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1770 | #ifdef CONFIG_X86_64 |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1771 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) |
| 1772 | ? F(GBPAGES) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1773 | unsigned f_lm = F(LM); |
| 1774 | #else |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1775 | unsigned f_gbpages = 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1776 | unsigned f_lm = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1777 | #endif |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1778 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1779 | |
| 1780 | /* cpuid 1.edx */ |
| 1781 | const u32 kvm_supported_word0_x86_features = |
| 1782 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1783 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1784 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 1785 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1786 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | |
| 1787 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 1788 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 1789 | 0 /* HTT, TM, Reserved, PBE */; |
| 1790 | /* cpuid 0x80000001.edx */ |
| 1791 | const u32 kvm_supported_word1_x86_features = |
| 1792 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1793 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1794 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 1795 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1796 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
| 1797 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1798 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1799 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); |
| 1800 | /* cpuid 1.ecx */ |
| 1801 | const u32 kvm_supported_word4_x86_features = |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1802 | F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ | |
| 1803 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 1804 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
| 1805 | 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ | |
| 1806 | 0 /* Reserved, DCA */ | F(XMM4_1) | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1807 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1808 | 0 /* Reserved, XSAVE, OSXSAVE */; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1809 | /* cpuid 0x80000001.ecx */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1810 | const u32 kvm_supported_word6_x86_features = |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1811 | F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ | |
| 1812 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
| 1813 | F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) | |
| 1814 | 0 /* SKINIT */ | 0 /* WDT */; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1815 | |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1816 | /* all calls to cpuid_count() should be made on the same cpu */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1817 | get_cpu(); |
| 1818 | do_cpuid_1_ent(entry, function, index); |
| 1819 | ++*nent; |
| 1820 | |
| 1821 | switch (function) { |
| 1822 | case 0: |
| 1823 | entry->eax = min(entry->eax, (u32)0xb); |
| 1824 | break; |
| 1825 | case 1: |
| 1826 | entry->edx &= kvm_supported_word0_x86_features; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1827 | entry->ecx &= kvm_supported_word4_x86_features; |
Gleb Natapov | 0d1de2d | 2009-07-12 16:10:55 +0300 | [diff] [blame] | 1828 | /* we support x2apic emulation even if host does not support |
| 1829 | * it since we emulate x2apic in software */ |
| 1830 | entry->ecx |= F(X2APIC); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1831 | break; |
| 1832 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 1833 | * may return different values. This forces us to get_cpu() before |
| 1834 | * issuing the first command, and also to emulate this annoying behavior |
| 1835 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 1836 | case 2: { |
| 1837 | int t, times = entry->eax & 0xff; |
| 1838 | |
| 1839 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 1840 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1841 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 1842 | do_cpuid_1_ent(&entry[t], function, 0); |
| 1843 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1844 | ++*nent; |
| 1845 | } |
| 1846 | break; |
| 1847 | } |
| 1848 | /* function 4 and 0xb have additional index. */ |
| 1849 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1850 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1851 | |
| 1852 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1853 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1854 | for (i = 1; *nent < maxnent; ++i) { |
| 1855 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1856 | if (!cache_type) |
| 1857 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1858 | do_cpuid_1_ent(&entry[i], function, i); |
| 1859 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1860 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1861 | ++*nent; |
| 1862 | } |
| 1863 | break; |
| 1864 | } |
| 1865 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1866 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1867 | |
| 1868 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1869 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1870 | for (i = 1; *nent < maxnent; ++i) { |
Nitin A Kamble | 0853d2c | 2008-11-05 15:37:36 -0800 | [diff] [blame] | 1871 | level_type = entry[i - 1].ecx & 0xff00; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1872 | if (!level_type) |
| 1873 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1874 | do_cpuid_1_ent(&entry[i], function, i); |
| 1875 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1876 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1877 | ++*nent; |
| 1878 | } |
| 1879 | break; |
| 1880 | } |
| 1881 | case 0x80000000: |
| 1882 | entry->eax = min(entry->eax, 0x8000001a); |
| 1883 | break; |
| 1884 | case 0x80000001: |
| 1885 | entry->edx &= kvm_supported_word1_x86_features; |
| 1886 | entry->ecx &= kvm_supported_word6_x86_features; |
| 1887 | break; |
| 1888 | } |
| 1889 | put_cpu(); |
| 1890 | } |
| 1891 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1892 | #undef F |
| 1893 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1894 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1895 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1896 | { |
| 1897 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 1898 | int limit, nent = 0, r = -E2BIG; |
| 1899 | u32 func; |
| 1900 | |
| 1901 | if (cpuid->nent < 1) |
| 1902 | goto out; |
Avi Kivity | 6a54435 | 2009-10-04 16:45:13 +0200 | [diff] [blame] | 1903 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1904 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1905 | r = -ENOMEM; |
| 1906 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 1907 | if (!cpuid_entries) |
| 1908 | goto out; |
| 1909 | |
| 1910 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 1911 | limit = cpuid_entries[0].eax; |
| 1912 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 1913 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1914 | &nent, cpuid->nent); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1915 | r = -E2BIG; |
| 1916 | if (nent >= cpuid->nent) |
| 1917 | goto out_free; |
| 1918 | |
| 1919 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 1920 | limit = cpuid_entries[nent - 1].eax; |
| 1921 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 1922 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1923 | &nent, cpuid->nent); |
Mark McLoughlin | cb00764 | 2009-05-12 12:36:44 +0100 | [diff] [blame] | 1924 | r = -E2BIG; |
| 1925 | if (nent >= cpuid->nent) |
| 1926 | goto out_free; |
| 1927 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1928 | r = -EFAULT; |
| 1929 | if (copy_to_user(entries, cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1930 | nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1931 | goto out_free; |
| 1932 | cpuid->nent = nent; |
| 1933 | r = 0; |
| 1934 | |
| 1935 | out_free: |
| 1936 | vfree(cpuid_entries); |
| 1937 | out: |
| 1938 | return r; |
| 1939 | } |
| 1940 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1941 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 1942 | struct kvm_lapic_state *s) |
| 1943 | { |
| 1944 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1945 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1946 | vcpu_put(vcpu); |
| 1947 | |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 1952 | struct kvm_lapic_state *s) |
| 1953 | { |
| 1954 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1955 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1956 | kvm_apic_post_state_restore(vcpu); |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 1957 | update_cr8_intercept(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1958 | vcpu_put(vcpu); |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1963 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 1964 | struct kvm_interrupt *irq) |
| 1965 | { |
| 1966 | if (irq->irq < 0 || irq->irq >= 256) |
| 1967 | return -EINVAL; |
| 1968 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1969 | return -ENXIO; |
| 1970 | vcpu_load(vcpu); |
| 1971 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1972 | kvm_queue_interrupt(vcpu, irq->irq, false); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1973 | |
| 1974 | vcpu_put(vcpu); |
| 1975 | |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 1979 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 1980 | { |
| 1981 | vcpu_load(vcpu); |
| 1982 | kvm_inject_nmi(vcpu); |
| 1983 | vcpu_put(vcpu); |
| 1984 | |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 1988 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 1989 | struct kvm_tpr_access_ctl *tac) |
| 1990 | { |
| 1991 | if (tac->flags) |
| 1992 | return -EINVAL; |
| 1993 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1997 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 1998 | u64 mcg_cap) |
| 1999 | { |
| 2000 | int r; |
| 2001 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 2002 | |
| 2003 | r = -EINVAL; |
Jan Kiszka | a9e38c3e | 2009-10-23 09:37:00 +0200 | [diff] [blame] | 2004 | if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2005 | goto out; |
| 2006 | if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000)) |
| 2007 | goto out; |
| 2008 | r = 0; |
| 2009 | vcpu->arch.mcg_cap = mcg_cap; |
| 2010 | /* Init IA32_MCG_CTL to all 1s */ |
| 2011 | if (mcg_cap & MCG_CTL_P) |
| 2012 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 2013 | /* Init IA32_MCi_CTL to all 1s */ |
| 2014 | for (bank = 0; bank < bank_num; bank++) |
| 2015 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
| 2016 | out: |
| 2017 | return r; |
| 2018 | } |
| 2019 | |
| 2020 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 2021 | struct kvm_x86_mce *mce) |
| 2022 | { |
| 2023 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2024 | unsigned bank_num = mcg_cap & 0xff; |
| 2025 | u64 *banks = vcpu->arch.mce_banks; |
| 2026 | |
| 2027 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 2028 | return -EINVAL; |
| 2029 | /* |
| 2030 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 2031 | * reporting is disabled |
| 2032 | */ |
| 2033 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 2034 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 2035 | return 0; |
| 2036 | banks += 4 * mce->bank; |
| 2037 | /* |
| 2038 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 2039 | * reporting is disabled for the bank |
| 2040 | */ |
| 2041 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 2042 | return 0; |
| 2043 | if (mce->status & MCI_STATUS_UC) { |
| 2044 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2045 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2046 | printk(KERN_DEBUG "kvm: set_mce: " |
| 2047 | "injects mce exception while " |
| 2048 | "previous one is in progress!\n"); |
| 2049 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 2050 | return 0; |
| 2051 | } |
| 2052 | if (banks[1] & MCI_STATUS_VAL) |
| 2053 | mce->status |= MCI_STATUS_OVER; |
| 2054 | banks[2] = mce->addr; |
| 2055 | banks[3] = mce->misc; |
| 2056 | vcpu->arch.mcg_status = mce->mcg_status; |
| 2057 | banks[1] = mce->status; |
| 2058 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 2059 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 2060 | || !(banks[1] & MCI_STATUS_UC)) { |
| 2061 | if (banks[1] & MCI_STATUS_VAL) |
| 2062 | mce->status |= MCI_STATUS_OVER; |
| 2063 | banks[2] = mce->addr; |
| 2064 | banks[3] = mce->misc; |
| 2065 | banks[1] = mce->status; |
| 2066 | } else |
| 2067 | banks[1] |= MCI_STATUS_OVER; |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2071 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 2072 | struct kvm_vcpu_events *events) |
| 2073 | { |
| 2074 | vcpu_load(vcpu); |
| 2075 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2076 | events->exception.injected = |
| 2077 | vcpu->arch.exception.pending && |
| 2078 | !kvm_exception_is_soft(vcpu->arch.exception.nr); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2079 | events->exception.nr = vcpu->arch.exception.nr; |
| 2080 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
| 2081 | events->exception.error_code = vcpu->arch.exception.error_code; |
| 2082 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2083 | events->interrupt.injected = |
| 2084 | vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2085 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2086 | events->interrupt.soft = 0; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2087 | events->interrupt.shadow = |
| 2088 | kvm_x86_ops->get_interrupt_shadow(vcpu, |
| 2089 | KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2090 | |
| 2091 | events->nmi.injected = vcpu->arch.nmi_injected; |
| 2092 | events->nmi.pending = vcpu->arch.nmi_pending; |
| 2093 | events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); |
| 2094 | |
| 2095 | events->sipi_vector = vcpu->arch.sipi_vector; |
| 2096 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2097 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2098 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2099 | | KVM_VCPUEVENT_VALID_SHADOW); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2100 | |
| 2101 | vcpu_put(vcpu); |
| 2102 | } |
| 2103 | |
| 2104 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 2105 | struct kvm_vcpu_events *events) |
| 2106 | { |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2107 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2108 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2109 | | KVM_VCPUEVENT_VALID_SHADOW)) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2110 | return -EINVAL; |
| 2111 | |
| 2112 | vcpu_load(vcpu); |
| 2113 | |
| 2114 | vcpu->arch.exception.pending = events->exception.injected; |
| 2115 | vcpu->arch.exception.nr = events->exception.nr; |
| 2116 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 2117 | vcpu->arch.exception.error_code = events->exception.error_code; |
| 2118 | |
| 2119 | vcpu->arch.interrupt.pending = events->interrupt.injected; |
| 2120 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 2121 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
| 2122 | if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm)) |
| 2123 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2124 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
| 2125 | kvm_x86_ops->set_interrupt_shadow(vcpu, |
| 2126 | events->interrupt.shadow); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2127 | |
| 2128 | vcpu->arch.nmi_injected = events->nmi.injected; |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2129 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 2130 | vcpu->arch.nmi_pending = events->nmi.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2131 | kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked); |
| 2132 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2133 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR) |
| 2134 | vcpu->arch.sipi_vector = events->sipi_vector; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2135 | |
| 2136 | vcpu_put(vcpu); |
| 2137 | |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2141 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 2142 | struct kvm_debugregs *dbgregs) |
| 2143 | { |
| 2144 | vcpu_load(vcpu); |
| 2145 | |
| 2146 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
| 2147 | dbgregs->dr6 = vcpu->arch.dr6; |
| 2148 | dbgregs->dr7 = vcpu->arch.dr7; |
| 2149 | dbgregs->flags = 0; |
| 2150 | |
| 2151 | vcpu_put(vcpu); |
| 2152 | } |
| 2153 | |
| 2154 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 2155 | struct kvm_debugregs *dbgregs) |
| 2156 | { |
| 2157 | if (dbgregs->flags) |
| 2158 | return -EINVAL; |
| 2159 | |
| 2160 | vcpu_load(vcpu); |
| 2161 | |
| 2162 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
| 2163 | vcpu->arch.dr6 = dbgregs->dr6; |
| 2164 | vcpu->arch.dr7 = dbgregs->dr7; |
| 2165 | |
| 2166 | vcpu_put(vcpu); |
| 2167 | |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2171 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 2172 | unsigned int ioctl, unsigned long arg) |
| 2173 | { |
| 2174 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2175 | void __user *argp = (void __user *)arg; |
| 2176 | int r; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2177 | struct kvm_lapic_state *lapic = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2178 | |
| 2179 | switch (ioctl) { |
| 2180 | case KVM_GET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2181 | r = -EINVAL; |
| 2182 | if (!vcpu->arch.apic) |
| 2183 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2184 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2185 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2186 | r = -ENOMEM; |
| 2187 | if (!lapic) |
| 2188 | goto out; |
| 2189 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2190 | if (r) |
| 2191 | goto out; |
| 2192 | r = -EFAULT; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2193 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2194 | goto out; |
| 2195 | r = 0; |
| 2196 | break; |
| 2197 | } |
| 2198 | case KVM_SET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2199 | r = -EINVAL; |
| 2200 | if (!vcpu->arch.apic) |
| 2201 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2202 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
| 2203 | r = -ENOMEM; |
| 2204 | if (!lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2205 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2206 | r = -EFAULT; |
| 2207 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
| 2208 | goto out; |
| 2209 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2210 | if (r) |
| 2211 | goto out; |
| 2212 | r = 0; |
| 2213 | break; |
| 2214 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2215 | case KVM_INTERRUPT: { |
| 2216 | struct kvm_interrupt irq; |
| 2217 | |
| 2218 | r = -EFAULT; |
| 2219 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 2220 | goto out; |
| 2221 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 2222 | if (r) |
| 2223 | goto out; |
| 2224 | r = 0; |
| 2225 | break; |
| 2226 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2227 | case KVM_NMI: { |
| 2228 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 2229 | if (r) |
| 2230 | goto out; |
| 2231 | r = 0; |
| 2232 | break; |
| 2233 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2234 | case KVM_SET_CPUID: { |
| 2235 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 2236 | struct kvm_cpuid cpuid; |
| 2237 | |
| 2238 | r = -EFAULT; |
| 2239 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2240 | goto out; |
| 2241 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 2242 | if (r) |
| 2243 | goto out; |
| 2244 | break; |
| 2245 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2246 | case KVM_SET_CPUID2: { |
| 2247 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2248 | struct kvm_cpuid2 cpuid; |
| 2249 | |
| 2250 | r = -EFAULT; |
| 2251 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2252 | goto out; |
| 2253 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2254 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2255 | if (r) |
| 2256 | goto out; |
| 2257 | break; |
| 2258 | } |
| 2259 | case KVM_GET_CPUID2: { |
| 2260 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2261 | struct kvm_cpuid2 cpuid; |
| 2262 | |
| 2263 | r = -EFAULT; |
| 2264 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2265 | goto out; |
| 2266 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2267 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2268 | if (r) |
| 2269 | goto out; |
| 2270 | r = -EFAULT; |
| 2271 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 2272 | goto out; |
| 2273 | r = 0; |
| 2274 | break; |
| 2275 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2276 | case KVM_GET_MSRS: |
| 2277 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 2278 | break; |
| 2279 | case KVM_SET_MSRS: |
| 2280 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 2281 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2282 | case KVM_TPR_ACCESS_REPORTING: { |
| 2283 | struct kvm_tpr_access_ctl tac; |
| 2284 | |
| 2285 | r = -EFAULT; |
| 2286 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 2287 | goto out; |
| 2288 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 2289 | if (r) |
| 2290 | goto out; |
| 2291 | r = -EFAULT; |
| 2292 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 2293 | goto out; |
| 2294 | r = 0; |
| 2295 | break; |
| 2296 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2297 | case KVM_SET_VAPIC_ADDR: { |
| 2298 | struct kvm_vapic_addr va; |
| 2299 | |
| 2300 | r = -EINVAL; |
| 2301 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 2302 | goto out; |
| 2303 | r = -EFAULT; |
| 2304 | if (copy_from_user(&va, argp, sizeof va)) |
| 2305 | goto out; |
| 2306 | r = 0; |
| 2307 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 2308 | break; |
| 2309 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2310 | case KVM_X86_SETUP_MCE: { |
| 2311 | u64 mcg_cap; |
| 2312 | |
| 2313 | r = -EFAULT; |
| 2314 | if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap)) |
| 2315 | goto out; |
| 2316 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 2317 | break; |
| 2318 | } |
| 2319 | case KVM_X86_SET_MCE: { |
| 2320 | struct kvm_x86_mce mce; |
| 2321 | |
| 2322 | r = -EFAULT; |
| 2323 | if (copy_from_user(&mce, argp, sizeof mce)) |
| 2324 | goto out; |
| 2325 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 2326 | break; |
| 2327 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2328 | case KVM_GET_VCPU_EVENTS: { |
| 2329 | struct kvm_vcpu_events events; |
| 2330 | |
| 2331 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 2332 | |
| 2333 | r = -EFAULT; |
| 2334 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 2335 | break; |
| 2336 | r = 0; |
| 2337 | break; |
| 2338 | } |
| 2339 | case KVM_SET_VCPU_EVENTS: { |
| 2340 | struct kvm_vcpu_events events; |
| 2341 | |
| 2342 | r = -EFAULT; |
| 2343 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 2344 | break; |
| 2345 | |
| 2346 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 2347 | break; |
| 2348 | } |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2349 | case KVM_GET_DEBUGREGS: { |
| 2350 | struct kvm_debugregs dbgregs; |
| 2351 | |
| 2352 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 2353 | |
| 2354 | r = -EFAULT; |
| 2355 | if (copy_to_user(argp, &dbgregs, |
| 2356 | sizeof(struct kvm_debugregs))) |
| 2357 | break; |
| 2358 | r = 0; |
| 2359 | break; |
| 2360 | } |
| 2361 | case KVM_SET_DEBUGREGS: { |
| 2362 | struct kvm_debugregs dbgregs; |
| 2363 | |
| 2364 | r = -EFAULT; |
| 2365 | if (copy_from_user(&dbgregs, argp, |
| 2366 | sizeof(struct kvm_debugregs))) |
| 2367 | break; |
| 2368 | |
| 2369 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 2370 | break; |
| 2371 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2372 | default: |
| 2373 | r = -EINVAL; |
| 2374 | } |
| 2375 | out: |
Wei Yongjun | 7a6ce84 | 2009-03-31 16:47:44 +0800 | [diff] [blame] | 2376 | kfree(lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2377 | return r; |
| 2378 | } |
| 2379 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2380 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 2381 | { |
| 2382 | int ret; |
| 2383 | |
| 2384 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 2385 | return -1; |
| 2386 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 2387 | return ret; |
| 2388 | } |
| 2389 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2390 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 2391 | u64 ident_addr) |
| 2392 | { |
| 2393 | kvm->arch.ept_identity_map_addr = ident_addr; |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2397 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 2398 | u32 kvm_nr_mmu_pages) |
| 2399 | { |
| 2400 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 2401 | return -EINVAL; |
| 2402 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2403 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2404 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2405 | |
| 2406 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2407 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2408 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2409 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2410 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2411 | return 0; |
| 2412 | } |
| 2413 | |
| 2414 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 2415 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2416 | return kvm->arch.n_alloc_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2419 | gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn) |
| 2420 | { |
| 2421 | int i; |
| 2422 | struct kvm_mem_alias *alias; |
| 2423 | struct kvm_mem_aliases *aliases; |
| 2424 | |
| 2425 | aliases = rcu_dereference(kvm->arch.aliases); |
| 2426 | |
| 2427 | for (i = 0; i < aliases->naliases; ++i) { |
| 2428 | alias = &aliases->aliases[i]; |
| 2429 | if (alias->flags & KVM_ALIAS_INVALID) |
| 2430 | continue; |
| 2431 | if (gfn >= alias->base_gfn |
| 2432 | && gfn < alias->base_gfn + alias->npages) |
| 2433 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2434 | } |
| 2435 | return gfn; |
| 2436 | } |
| 2437 | |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2438 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 2439 | { |
| 2440 | int i; |
| 2441 | struct kvm_mem_alias *alias; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2442 | struct kvm_mem_aliases *aliases; |
| 2443 | |
| 2444 | aliases = rcu_dereference(kvm->arch.aliases); |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2445 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2446 | for (i = 0; i < aliases->naliases; ++i) { |
| 2447 | alias = &aliases->aliases[i]; |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2448 | if (gfn >= alias->base_gfn |
| 2449 | && gfn < alias->base_gfn + alias->npages) |
| 2450 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2451 | } |
| 2452 | return gfn; |
| 2453 | } |
| 2454 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2455 | /* |
| 2456 | * Set a new alias region. Aliases map a portion of physical memory into |
| 2457 | * another portion. This is useful for memory windows, for example the PC |
| 2458 | * VGA region. |
| 2459 | */ |
| 2460 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 2461 | struct kvm_memory_alias *alias) |
| 2462 | { |
| 2463 | int r, n; |
| 2464 | struct kvm_mem_alias *p; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2465 | struct kvm_mem_aliases *aliases, *old_aliases; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2466 | |
| 2467 | r = -EINVAL; |
| 2468 | /* General sanity checks */ |
| 2469 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 2470 | goto out; |
| 2471 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 2472 | goto out; |
| 2473 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 2474 | goto out; |
| 2475 | if (alias->guest_phys_addr + alias->memory_size |
| 2476 | < alias->guest_phys_addr) |
| 2477 | goto out; |
| 2478 | if (alias->target_phys_addr + alias->memory_size |
| 2479 | < alias->target_phys_addr) |
| 2480 | goto out; |
| 2481 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2482 | r = -ENOMEM; |
| 2483 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2484 | if (!aliases) |
| 2485 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2486 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2487 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2488 | |
| 2489 | /* invalidate any gfn reference in case of deletion/shrinking */ |
| 2490 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
| 2491 | aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID; |
| 2492 | old_aliases = kvm->arch.aliases; |
| 2493 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2494 | synchronize_srcu_expedited(&kvm->srcu); |
| 2495 | kvm_mmu_zap_all(kvm); |
| 2496 | kfree(old_aliases); |
| 2497 | |
| 2498 | r = -ENOMEM; |
| 2499 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2500 | if (!aliases) |
| 2501 | goto out_unlock; |
| 2502 | |
| 2503 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2504 | |
| 2505 | p = &aliases->aliases[alias->slot]; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2506 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 2507 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 2508 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2509 | p->flags &= ~(KVM_ALIAS_INVALID); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2510 | |
| 2511 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2512 | if (aliases->aliases[n - 1].npages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2513 | break; |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2514 | aliases->naliases = n; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2515 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2516 | old_aliases = kvm->arch.aliases; |
| 2517 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2518 | synchronize_srcu_expedited(&kvm->srcu); |
| 2519 | kfree(old_aliases); |
| 2520 | r = 0; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2521 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2522 | out_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2523 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2524 | out: |
| 2525 | return r; |
| 2526 | } |
| 2527 | |
| 2528 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2529 | { |
| 2530 | int r; |
| 2531 | |
| 2532 | r = 0; |
| 2533 | switch (chip->chip_id) { |
| 2534 | case KVM_IRQCHIP_PIC_MASTER: |
| 2535 | memcpy(&chip->chip.pic, |
| 2536 | &pic_irqchip(kvm)->pics[0], |
| 2537 | sizeof(struct kvm_pic_state)); |
| 2538 | break; |
| 2539 | case KVM_IRQCHIP_PIC_SLAVE: |
| 2540 | memcpy(&chip->chip.pic, |
| 2541 | &pic_irqchip(kvm)->pics[1], |
| 2542 | sizeof(struct kvm_pic_state)); |
| 2543 | break; |
| 2544 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2545 | r = kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2546 | break; |
| 2547 | default: |
| 2548 | r = -EINVAL; |
| 2549 | break; |
| 2550 | } |
| 2551 | return r; |
| 2552 | } |
| 2553 | |
| 2554 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2555 | { |
| 2556 | int r; |
| 2557 | |
| 2558 | r = 0; |
| 2559 | switch (chip->chip_id) { |
| 2560 | case KVM_IRQCHIP_PIC_MASTER: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2561 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2562 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 2563 | &chip->chip.pic, |
| 2564 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2565 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2566 | break; |
| 2567 | case KVM_IRQCHIP_PIC_SLAVE: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2568 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2569 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 2570 | &chip->chip.pic, |
| 2571 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2572 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2573 | break; |
| 2574 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2575 | r = kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2576 | break; |
| 2577 | default: |
| 2578 | r = -EINVAL; |
| 2579 | break; |
| 2580 | } |
| 2581 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 2582 | return r; |
| 2583 | } |
| 2584 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2585 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2586 | { |
| 2587 | int r = 0; |
| 2588 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2589 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2590 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2591 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2592 | return r; |
| 2593 | } |
| 2594 | |
| 2595 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2596 | { |
| 2597 | int r = 0; |
| 2598 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2599 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2600 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2601 | kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); |
| 2602 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2603 | return r; |
| 2604 | } |
| 2605 | |
| 2606 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2607 | { |
| 2608 | int r = 0; |
| 2609 | |
| 2610 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2611 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 2612 | sizeof(ps->channels)); |
| 2613 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 2614 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2615 | return r; |
| 2616 | } |
| 2617 | |
| 2618 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2619 | { |
| 2620 | int r = 0, start = 0; |
| 2621 | u32 prev_legacy, cur_legacy; |
| 2622 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2623 | prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2624 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2625 | if (!prev_legacy && cur_legacy) |
| 2626 | start = 1; |
| 2627 | memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, |
| 2628 | sizeof(kvm->arch.vpit->pit_state.channels)); |
| 2629 | kvm->arch.vpit->pit_state.flags = ps->flags; |
| 2630 | 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] | 2631 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2632 | return r; |
| 2633 | } |
| 2634 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2635 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 2636 | struct kvm_reinject_control *control) |
| 2637 | { |
| 2638 | if (!kvm->arch.vpit) |
| 2639 | return -ENXIO; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2640 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2641 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2642 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2643 | return 0; |
| 2644 | } |
| 2645 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2646 | /* |
| 2647 | * Get (and clear) the dirty memory log for a memory slot. |
| 2648 | */ |
| 2649 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 2650 | struct kvm_dirty_log *log) |
| 2651 | { |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2652 | int r, i; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2653 | struct kvm_memory_slot *memslot; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2654 | unsigned long n; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2655 | unsigned long is_dirty = 0; |
| 2656 | unsigned long *dirty_bitmap = NULL; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2657 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2658 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2659 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2660 | r = -EINVAL; |
| 2661 | if (log->slot >= KVM_MEMORY_SLOTS) |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2662 | goto out; |
| 2663 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2664 | memslot = &kvm->memslots->memslots[log->slot]; |
| 2665 | r = -ENOENT; |
| 2666 | if (!memslot->dirty_bitmap) |
| 2667 | goto out; |
| 2668 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2669 | n = kvm_dirty_bitmap_bytes(memslot); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2670 | |
| 2671 | r = -ENOMEM; |
| 2672 | dirty_bitmap = vmalloc(n); |
| 2673 | if (!dirty_bitmap) |
| 2674 | goto out; |
| 2675 | memset(dirty_bitmap, 0, n); |
| 2676 | |
| 2677 | for (i = 0; !is_dirty && i < n/sizeof(long); i++) |
| 2678 | is_dirty = memslot->dirty_bitmap[i]; |
| 2679 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2680 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 2681 | if (is_dirty) { |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2682 | struct kvm_memslots *slots, *old_slots; |
| 2683 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2684 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2685 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2686 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2687 | |
| 2688 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 2689 | if (!slots) |
| 2690 | goto out_free; |
| 2691 | |
| 2692 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 2693 | slots->memslots[log->slot].dirty_bitmap = dirty_bitmap; |
| 2694 | |
| 2695 | old_slots = kvm->memslots; |
| 2696 | rcu_assign_pointer(kvm->memslots, slots); |
| 2697 | synchronize_srcu_expedited(&kvm->srcu); |
| 2698 | dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap; |
| 2699 | kfree(old_slots); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2700 | } |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2701 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2702 | r = 0; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2703 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) |
| 2704 | r = -EFAULT; |
| 2705 | out_free: |
| 2706 | vfree(dirty_bitmap); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2707 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2708 | mutex_unlock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2709 | return r; |
| 2710 | } |
| 2711 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2712 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2713 | unsigned int ioctl, unsigned long arg) |
| 2714 | { |
| 2715 | struct kvm *kvm = filp->private_data; |
| 2716 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2717 | int r = -ENOTTY; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2718 | /* |
| 2719 | * This union makes it completely explicit to gcc-3.x |
| 2720 | * that these two variables' stack usage should be |
| 2721 | * combined, not added together. |
| 2722 | */ |
| 2723 | union { |
| 2724 | struct kvm_pit_state ps; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2725 | struct kvm_pit_state2 ps2; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2726 | struct kvm_memory_alias alias; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2727 | struct kvm_pit_config pit_config; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2728 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2729 | |
| 2730 | switch (ioctl) { |
| 2731 | case KVM_SET_TSS_ADDR: |
| 2732 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 2733 | if (r < 0) |
| 2734 | goto out; |
| 2735 | break; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2736 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 2737 | u64 ident_addr; |
| 2738 | |
| 2739 | r = -EFAULT; |
| 2740 | if (copy_from_user(&ident_addr, argp, sizeof ident_addr)) |
| 2741 | goto out; |
| 2742 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
| 2743 | if (r < 0) |
| 2744 | goto out; |
| 2745 | break; |
| 2746 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2747 | case KVM_SET_MEMORY_REGION: { |
| 2748 | struct kvm_memory_region kvm_mem; |
| 2749 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2750 | |
| 2751 | r = -EFAULT; |
| 2752 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 2753 | goto out; |
| 2754 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 2755 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 2756 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 2757 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 2758 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2759 | if (r) |
| 2760 | goto out; |
| 2761 | break; |
| 2762 | } |
| 2763 | case KVM_SET_NR_MMU_PAGES: |
| 2764 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 2765 | if (r) |
| 2766 | goto out; |
| 2767 | break; |
| 2768 | case KVM_GET_NR_MMU_PAGES: |
| 2769 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 2770 | break; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2771 | case KVM_SET_MEMORY_ALIAS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2772 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2773 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2774 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2775 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2776 | if (r) |
| 2777 | goto out; |
| 2778 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2779 | case KVM_CREATE_IRQCHIP: { |
| 2780 | struct kvm_pic *vpic; |
| 2781 | |
| 2782 | mutex_lock(&kvm->lock); |
| 2783 | r = -EEXIST; |
| 2784 | if (kvm->arch.vpic) |
| 2785 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2786 | r = -ENOMEM; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2787 | vpic = kvm_create_pic(kvm); |
| 2788 | if (vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2789 | r = kvm_ioapic_init(kvm); |
| 2790 | if (r) { |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2791 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, |
| 2792 | &vpic->dev); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2793 | kfree(vpic); |
| 2794 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2795 | } |
| 2796 | } else |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2797 | goto create_irqchip_unlock; |
| 2798 | smp_wmb(); |
| 2799 | kvm->arch.vpic = vpic; |
| 2800 | smp_wmb(); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2801 | r = kvm_setup_default_irq_routing(kvm); |
| 2802 | if (r) { |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2803 | mutex_lock(&kvm->irq_lock); |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2804 | kvm_ioapic_destroy(kvm); |
| 2805 | kvm_destroy_pic(kvm); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2806 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2807 | } |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2808 | create_irqchip_unlock: |
| 2809 | mutex_unlock(&kvm->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2810 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2811 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2812 | case KVM_CREATE_PIT: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2813 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 2814 | goto create_pit; |
| 2815 | case KVM_CREATE_PIT2: |
| 2816 | r = -EFAULT; |
| 2817 | if (copy_from_user(&u.pit_config, argp, |
| 2818 | sizeof(struct kvm_pit_config))) |
| 2819 | goto out; |
| 2820 | create_pit: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2821 | mutex_lock(&kvm->slots_lock); |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2822 | r = -EEXIST; |
| 2823 | if (kvm->arch.vpit) |
| 2824 | goto create_pit_unlock; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2825 | r = -ENOMEM; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2826 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2827 | if (kvm->arch.vpit) |
| 2828 | r = 0; |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2829 | create_pit_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2830 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2831 | break; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2832 | case KVM_IRQ_LINE_STATUS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2833 | case KVM_IRQ_LINE: { |
| 2834 | struct kvm_irq_level irq_event; |
| 2835 | |
| 2836 | r = -EFAULT; |
| 2837 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 2838 | goto out; |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame^] | 2839 | r = -ENXIO; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2840 | if (irqchip_in_kernel(kvm)) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2841 | __s32 status; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2842 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 2843 | irq_event.irq, irq_event.level); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2844 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame^] | 2845 | r = -EFAULT; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2846 | irq_event.status = status; |
| 2847 | if (copy_to_user(argp, &irq_event, |
| 2848 | sizeof irq_event)) |
| 2849 | goto out; |
| 2850 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2851 | r = 0; |
| 2852 | } |
| 2853 | break; |
| 2854 | } |
| 2855 | case KVM_GET_IRQCHIP: { |
| 2856 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2857 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2858 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2859 | r = -ENOMEM; |
| 2860 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2861 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2862 | r = -EFAULT; |
| 2863 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2864 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2865 | r = -ENXIO; |
| 2866 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2867 | goto get_irqchip_out; |
| 2868 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 2869 | if (r) |
| 2870 | goto get_irqchip_out; |
| 2871 | r = -EFAULT; |
| 2872 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 2873 | goto get_irqchip_out; |
| 2874 | r = 0; |
| 2875 | get_irqchip_out: |
| 2876 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2877 | if (r) |
| 2878 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2879 | break; |
| 2880 | } |
| 2881 | case KVM_SET_IRQCHIP: { |
| 2882 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2883 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2884 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2885 | r = -ENOMEM; |
| 2886 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2887 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2888 | r = -EFAULT; |
| 2889 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2890 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2891 | r = -ENXIO; |
| 2892 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2893 | goto set_irqchip_out; |
| 2894 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 2895 | if (r) |
| 2896 | goto set_irqchip_out; |
| 2897 | r = 0; |
| 2898 | set_irqchip_out: |
| 2899 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2900 | if (r) |
| 2901 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2902 | break; |
| 2903 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2904 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2905 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2906 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2907 | goto out; |
| 2908 | r = -ENXIO; |
| 2909 | if (!kvm->arch.vpit) |
| 2910 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2911 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2912 | if (r) |
| 2913 | goto out; |
| 2914 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2915 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2916 | goto out; |
| 2917 | r = 0; |
| 2918 | break; |
| 2919 | } |
| 2920 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2921 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2922 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2923 | goto out; |
| 2924 | r = -ENXIO; |
| 2925 | if (!kvm->arch.vpit) |
| 2926 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2927 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2928 | if (r) |
| 2929 | goto out; |
| 2930 | r = 0; |
| 2931 | break; |
| 2932 | } |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2933 | case KVM_GET_PIT2: { |
| 2934 | r = -ENXIO; |
| 2935 | if (!kvm->arch.vpit) |
| 2936 | goto out; |
| 2937 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 2938 | if (r) |
| 2939 | goto out; |
| 2940 | r = -EFAULT; |
| 2941 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 2942 | goto out; |
| 2943 | r = 0; |
| 2944 | break; |
| 2945 | } |
| 2946 | case KVM_SET_PIT2: { |
| 2947 | r = -EFAULT; |
| 2948 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 2949 | goto out; |
| 2950 | r = -ENXIO; |
| 2951 | if (!kvm->arch.vpit) |
| 2952 | goto out; |
| 2953 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
| 2954 | if (r) |
| 2955 | goto out; |
| 2956 | r = 0; |
| 2957 | break; |
| 2958 | } |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2959 | case KVM_REINJECT_CONTROL: { |
| 2960 | struct kvm_reinject_control control; |
| 2961 | r = -EFAULT; |
| 2962 | if (copy_from_user(&control, argp, sizeof(control))) |
| 2963 | goto out; |
| 2964 | r = kvm_vm_ioctl_reinject(kvm, &control); |
| 2965 | if (r) |
| 2966 | goto out; |
| 2967 | r = 0; |
| 2968 | break; |
| 2969 | } |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2970 | case KVM_XEN_HVM_CONFIG: { |
| 2971 | r = -EFAULT; |
| 2972 | if (copy_from_user(&kvm->arch.xen_hvm_config, argp, |
| 2973 | sizeof(struct kvm_xen_hvm_config))) |
| 2974 | goto out; |
| 2975 | r = -EINVAL; |
| 2976 | if (kvm->arch.xen_hvm_config.flags) |
| 2977 | goto out; |
| 2978 | r = 0; |
| 2979 | break; |
| 2980 | } |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 2981 | case KVM_SET_CLOCK: { |
| 2982 | struct timespec now; |
| 2983 | struct kvm_clock_data user_ns; |
| 2984 | u64 now_ns; |
| 2985 | s64 delta; |
| 2986 | |
| 2987 | r = -EFAULT; |
| 2988 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 2989 | goto out; |
| 2990 | |
| 2991 | r = -EINVAL; |
| 2992 | if (user_ns.flags) |
| 2993 | goto out; |
| 2994 | |
| 2995 | r = 0; |
| 2996 | ktime_get_ts(&now); |
| 2997 | now_ns = timespec_to_ns(&now); |
| 2998 | delta = user_ns.clock - now_ns; |
| 2999 | kvm->arch.kvmclock_offset = delta; |
| 3000 | break; |
| 3001 | } |
| 3002 | case KVM_GET_CLOCK: { |
| 3003 | struct timespec now; |
| 3004 | struct kvm_clock_data user_ns; |
| 3005 | u64 now_ns; |
| 3006 | |
| 3007 | ktime_get_ts(&now); |
| 3008 | now_ns = timespec_to_ns(&now); |
| 3009 | user_ns.clock = kvm->arch.kvmclock_offset + now_ns; |
| 3010 | user_ns.flags = 0; |
| 3011 | |
| 3012 | r = -EFAULT; |
| 3013 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 3014 | goto out; |
| 3015 | r = 0; |
| 3016 | break; |
| 3017 | } |
| 3018 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3019 | default: |
| 3020 | ; |
| 3021 | } |
| 3022 | out: |
| 3023 | return r; |
| 3024 | } |
| 3025 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 3026 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3027 | { |
| 3028 | u32 dummy[2]; |
| 3029 | unsigned i, j; |
| 3030 | |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 3031 | /* skip the first msrs in the list. KVM-specific */ |
| 3032 | 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] | 3033 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 3034 | continue; |
| 3035 | if (j < i) |
| 3036 | msrs_to_save[j] = msrs_to_save[i]; |
| 3037 | j++; |
| 3038 | } |
| 3039 | num_msrs_to_save = j; |
| 3040 | } |
| 3041 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3042 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 3043 | const void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3044 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3045 | if (vcpu->arch.apic && |
| 3046 | !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v)) |
| 3047 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3048 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3049 | 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] | 3050 | } |
| 3051 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3052 | 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] | 3053 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3054 | if (vcpu->arch.apic && |
| 3055 | !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v)) |
| 3056 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3057 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3058 | 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] | 3059 | } |
| 3060 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3061 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3062 | { |
| 3063 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3064 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3065 | } |
| 3066 | |
| 3067 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3068 | { |
| 3069 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3070 | access |= PFERR_FETCH_MASK; |
| 3071 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3072 | } |
| 3073 | |
| 3074 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3075 | { |
| 3076 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3077 | access |= PFERR_WRITE_MASK; |
| 3078 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3079 | } |
| 3080 | |
| 3081 | /* uses this to access any guest's mapped memory without checking CPL */ |
| 3082 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3083 | { |
| 3084 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error); |
| 3085 | } |
| 3086 | |
| 3087 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 3088 | struct kvm_vcpu *vcpu, u32 access, |
| 3089 | u32 *error) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3090 | { |
| 3091 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3092 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3093 | |
| 3094 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3095 | 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] | 3096 | unsigned offset = addr & (PAGE_SIZE-1); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3097 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3098 | int ret; |
| 3099 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3100 | if (gpa == UNMAPPED_GVA) { |
| 3101 | r = X86EMUL_PROPAGATE_FAULT; |
| 3102 | goto out; |
| 3103 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3104 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3105 | if (ret < 0) { |
| 3106 | r = X86EMUL_UNHANDLEABLE; |
| 3107 | goto out; |
| 3108 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3109 | |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3110 | bytes -= toread; |
| 3111 | data += toread; |
| 3112 | addr += toread; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3113 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3114 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3115 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3116 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3117 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3118 | /* used for instruction fetching */ |
| 3119 | static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3120 | struct kvm_vcpu *vcpu, u32 *error) |
| 3121 | { |
| 3122 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3123 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, |
| 3124 | access | PFERR_FETCH_MASK, error); |
| 3125 | } |
| 3126 | |
| 3127 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3128 | struct kvm_vcpu *vcpu, u32 *error) |
| 3129 | { |
| 3130 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3131 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
| 3132 | error); |
| 3133 | } |
| 3134 | |
| 3135 | static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes, |
| 3136 | struct kvm_vcpu *vcpu, u32 *error) |
| 3137 | { |
| 3138 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error); |
| 3139 | } |
| 3140 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 3141 | static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3142 | struct kvm_vcpu *vcpu, u32 *error) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3143 | { |
| 3144 | void *data = val; |
| 3145 | int r = X86EMUL_CONTINUE; |
| 3146 | |
| 3147 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3148 | gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3149 | unsigned offset = addr & (PAGE_SIZE-1); |
| 3150 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 3151 | int ret; |
| 3152 | |
| 3153 | if (gpa == UNMAPPED_GVA) { |
| 3154 | r = X86EMUL_PROPAGATE_FAULT; |
| 3155 | goto out; |
| 3156 | } |
| 3157 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); |
| 3158 | if (ret < 0) { |
| 3159 | r = X86EMUL_UNHANDLEABLE; |
| 3160 | goto out; |
| 3161 | } |
| 3162 | |
| 3163 | bytes -= towrite; |
| 3164 | data += towrite; |
| 3165 | addr += towrite; |
| 3166 | } |
| 3167 | out: |
| 3168 | return r; |
| 3169 | } |
| 3170 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3171 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3172 | static int emulator_read_emulated(unsigned long addr, |
| 3173 | void *val, |
| 3174 | unsigned int bytes, |
| 3175 | struct kvm_vcpu *vcpu) |
| 3176 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3177 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3178 | u32 error_code; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3179 | |
| 3180 | if (vcpu->mmio_read_completed) { |
| 3181 | memcpy(val, vcpu->mmio_data, bytes); |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3182 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
| 3183 | vcpu->mmio_phys_addr, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3184 | vcpu->mmio_read_completed = 0; |
| 3185 | return X86EMUL_CONTINUE; |
| 3186 | } |
| 3187 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3188 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code); |
| 3189 | |
| 3190 | if (gpa == UNMAPPED_GVA) { |
| 3191 | kvm_inject_page_fault(vcpu, addr, error_code); |
| 3192 | return X86EMUL_PROPAGATE_FAULT; |
| 3193 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3194 | |
| 3195 | /* For APIC access vmexit */ |
| 3196 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3197 | goto mmio; |
| 3198 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3199 | if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3200 | == X86EMUL_CONTINUE) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3201 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3202 | |
| 3203 | mmio: |
| 3204 | /* |
| 3205 | * Is this MMIO handled locally? |
| 3206 | */ |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3207 | if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) { |
| 3208 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3209 | return X86EMUL_CONTINUE; |
| 3210 | } |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3211 | |
| 3212 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3213 | |
| 3214 | vcpu->mmio_needed = 1; |
| 3215 | vcpu->mmio_phys_addr = gpa; |
| 3216 | vcpu->mmio_size = bytes; |
| 3217 | vcpu->mmio_is_write = 0; |
| 3218 | |
| 3219 | return X86EMUL_UNHANDLEABLE; |
| 3220 | } |
| 3221 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3222 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3223 | const void *val, int bytes) |
| 3224 | { |
| 3225 | int ret; |
| 3226 | |
| 3227 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 3228 | if (ret < 0) |
| 3229 | return 0; |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 3230 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3231 | return 1; |
| 3232 | } |
| 3233 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3234 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 3235 | const void *val, |
| 3236 | unsigned int bytes, |
| 3237 | struct kvm_vcpu *vcpu) |
| 3238 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3239 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3240 | u32 error_code; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3241 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3242 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3243 | |
| 3244 | if (gpa == UNMAPPED_GVA) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3245 | kvm_inject_page_fault(vcpu, addr, error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3246 | return X86EMUL_PROPAGATE_FAULT; |
| 3247 | } |
| 3248 | |
| 3249 | /* For APIC access vmexit */ |
| 3250 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3251 | goto mmio; |
| 3252 | |
| 3253 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 3254 | return X86EMUL_CONTINUE; |
| 3255 | |
| 3256 | mmio: |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3257 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3258 | /* |
| 3259 | * Is this MMIO handled locally? |
| 3260 | */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3261 | if (!vcpu_mmio_write(vcpu, gpa, bytes, val)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3262 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3263 | |
| 3264 | vcpu->mmio_needed = 1; |
| 3265 | vcpu->mmio_phys_addr = gpa; |
| 3266 | vcpu->mmio_size = bytes; |
| 3267 | vcpu->mmio_is_write = 1; |
| 3268 | memcpy(vcpu->mmio_data, val, bytes); |
| 3269 | |
| 3270 | return X86EMUL_CONTINUE; |
| 3271 | } |
| 3272 | |
| 3273 | int emulator_write_emulated(unsigned long addr, |
| 3274 | const void *val, |
| 3275 | unsigned int bytes, |
| 3276 | struct kvm_vcpu *vcpu) |
| 3277 | { |
| 3278 | /* Crossing a page boundary? */ |
| 3279 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 3280 | int rc, now; |
| 3281 | |
| 3282 | now = -addr & ~PAGE_MASK; |
| 3283 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
| 3284 | if (rc != X86EMUL_CONTINUE) |
| 3285 | return rc; |
| 3286 | addr += now; |
| 3287 | val += now; |
| 3288 | bytes -= now; |
| 3289 | } |
| 3290 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
| 3291 | } |
| 3292 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 3293 | |
| 3294 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 3295 | const void *old, |
| 3296 | const void *new, |
| 3297 | unsigned int bytes, |
| 3298 | struct kvm_vcpu *vcpu) |
| 3299 | { |
Marcin Slusarz | 9f51e24 | 2009-08-09 21:54:00 +0200 | [diff] [blame] | 3300 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3301 | #ifndef CONFIG_X86_64 |
| 3302 | /* guests cmpxchg8b have to be emulated atomically */ |
| 3303 | if (bytes == 8) { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3304 | gpa_t gpa; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3305 | struct page *page; |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 3306 | char *kaddr; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3307 | u64 val; |
| 3308 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3309 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3310 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3311 | if (gpa == UNMAPPED_GVA || |
| 3312 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3313 | goto emul_write; |
| 3314 | |
| 3315 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 3316 | goto emul_write; |
| 3317 | |
| 3318 | val = *(u64 *)new; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3319 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3320 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3321 | |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 3322 | kaddr = kmap_atomic(page, KM_USER0); |
| 3323 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); |
| 3324 | kunmap_atomic(kaddr, KM_USER0); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3325 | kvm_release_page_dirty(page); |
| 3326 | } |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3327 | emul_write: |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3328 | #endif |
| 3329 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3330 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 3331 | } |
| 3332 | |
| 3333 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3334 | { |
| 3335 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 3336 | } |
| 3337 | |
| 3338 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 3339 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3340 | kvm_mmu_invlpg(vcpu, address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3341 | return X86EMUL_CONTINUE; |
| 3342 | } |
| 3343 | |
| 3344 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 3345 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3346 | 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] | 3347 | kvm_x86_ops->fpu_activate(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3348 | return X86EMUL_CONTINUE; |
| 3349 | } |
| 3350 | |
| 3351 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 3352 | { |
Jan Kiszka | c76de35 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 3353 | return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 3357 | { |
| 3358 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3359 | |
Jan Kiszka | c76de35 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 3360 | return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 3364 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3365 | u8 opcodes[4]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3366 | unsigned long rip = kvm_rip_read(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3367 | unsigned long rip_linear; |
| 3368 | |
Avi Kivity | f76c710 | 2008-06-13 22:45:42 +0300 | [diff] [blame] | 3369 | if (!printk_ratelimit()) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3370 | return; |
| 3371 | |
Glauber Costa | 25be460 | 2008-06-10 10:46:53 -0300 | [diff] [blame] | 3372 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 3373 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3374 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3375 | |
| 3376 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 3377 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3378 | } |
| 3379 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 3380 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3381 | static struct x86_emulate_ops emulate_ops = { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3382 | .read_std = kvm_read_guest_virt_system, |
| 3383 | .fetch = kvm_fetch_guest_virt, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3384 | .read_emulated = emulator_read_emulated, |
| 3385 | .write_emulated = emulator_write_emulated, |
| 3386 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 3387 | }; |
| 3388 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3389 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 3390 | { |
| 3391 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3392 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3393 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 3394 | vcpu->arch.regs_dirty = ~0; |
| 3395 | } |
| 3396 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3397 | int emulate_instruction(struct kvm_vcpu *vcpu, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3398 | unsigned long cr2, |
| 3399 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3400 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3401 | { |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3402 | int r, shadow_mask; |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3403 | struct decode_cache *c; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3404 | struct kvm_run *run = vcpu->run; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3405 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 3406 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3407 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3408 | /* |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 3409 | * TODO: fix emulate.c to use guest_read/write_register |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3410 | * instead of direct ->regs accesses, can save hundred cycles |
| 3411 | * on Intel for instructions that don't read/change RSP, for |
| 3412 | * for example. |
| 3413 | */ |
| 3414 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3415 | |
| 3416 | vcpu->mmio_is_write = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3417 | vcpu->arch.pio.string = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3418 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3419 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3420 | int cs_db, cs_l; |
| 3421 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 3422 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3423 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
Jan Kiszka | 83bf000 | 2010-02-23 17:47:59 +0100 | [diff] [blame] | 3424 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3425 | vcpu->arch.emulate_ctxt.mode = |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3426 | (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3427 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3428 | ? X86EMUL_MODE_VM86 : cs_l |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3429 | ? X86EMUL_MODE_PROT64 : cs_db |
| 3430 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 3431 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3432 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3433 | |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3434 | /* Only allow emulation of specific instructions on #UD |
| 3435 | * (namely VMMCALL, sysenter, sysexit, syscall)*/ |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3436 | c = &vcpu->arch.emulate_ctxt.decode; |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3437 | if (emulation_type & EMULTYPE_TRAP_UD) { |
| 3438 | if (!c->twobyte) |
| 3439 | return EMULATE_FAIL; |
| 3440 | switch (c->b) { |
| 3441 | case 0x01: /* VMMCALL */ |
| 3442 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
| 3443 | return EMULATE_FAIL; |
| 3444 | break; |
| 3445 | case 0x34: /* sysenter */ |
| 3446 | case 0x35: /* sysexit */ |
| 3447 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3448 | return EMULATE_FAIL; |
| 3449 | break; |
| 3450 | case 0x05: /* syscall */ |
| 3451 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3452 | return EMULATE_FAIL; |
| 3453 | break; |
| 3454 | default: |
| 3455 | return EMULATE_FAIL; |
| 3456 | } |
| 3457 | |
| 3458 | if (!(c->modrm_reg == 0 || c->modrm_reg == 3)) |
| 3459 | return EMULATE_FAIL; |
| 3460 | } |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3461 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3462 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3463 | if (r) { |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3464 | ++vcpu->stat.insn_emulation_fail; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3465 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 3466 | return EMULATE_DONE; |
| 3467 | return EMULATE_FAIL; |
| 3468 | } |
| 3469 | } |
| 3470 | |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 3471 | if (emulation_type & EMULTYPE_SKIP) { |
| 3472 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip); |
| 3473 | return EMULATE_DONE; |
| 3474 | } |
| 3475 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3476 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3477 | shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; |
| 3478 | |
| 3479 | if (r == 0) |
| 3480 | kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3481 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3482 | if (vcpu->arch.pio.string) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3483 | return EMULATE_DO_MMIO; |
| 3484 | |
Gleb Natapov | 112592d | 2010-02-21 15:00:47 +0200 | [diff] [blame] | 3485 | if (r || vcpu->mmio_is_write) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3486 | run->exit_reason = KVM_EXIT_MMIO; |
| 3487 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 3488 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 3489 | run->mmio.len = vcpu->mmio_size; |
| 3490 | run->mmio.is_write = vcpu->mmio_is_write; |
| 3491 | } |
| 3492 | |
| 3493 | if (r) { |
| 3494 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 3495 | return EMULATE_DONE; |
| 3496 | if (!vcpu->mmio_needed) { |
| 3497 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 3498 | return EMULATE_FAIL; |
| 3499 | } |
| 3500 | return EMULATE_DO_MMIO; |
| 3501 | } |
| 3502 | |
Jan Kiszka | 83bf000 | 2010-02-23 17:47:59 +0100 | [diff] [blame] | 3503 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3504 | |
| 3505 | if (vcpu->mmio_is_write) { |
| 3506 | vcpu->mmio_needed = 0; |
| 3507 | return EMULATE_DO_MMIO; |
| 3508 | } |
| 3509 | |
| 3510 | return EMULATE_DONE; |
| 3511 | } |
| 3512 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 3513 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3514 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
| 3515 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3516 | void *p = vcpu->arch.pio_data; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3517 | gva_t q = vcpu->arch.pio.guest_gva; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3518 | unsigned bytes; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3519 | int ret; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3520 | u32 error_code; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3521 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3522 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
| 3523 | if (vcpu->arch.pio.in) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3524 | ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3525 | else |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3526 | ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code); |
| 3527 | |
| 3528 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 3529 | kvm_inject_page_fault(vcpu, q, error_code); |
| 3530 | |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3531 | return ret; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | int complete_pio(struct kvm_vcpu *vcpu) |
| 3535 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3536 | struct kvm_pio_request *io = &vcpu->arch.pio; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3537 | long delta; |
| 3538 | int r; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3539 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3540 | |
| 3541 | if (!io->string) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3542 | if (io->in) { |
| 3543 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3544 | memcpy(&val, vcpu->arch.pio_data, io->size); |
| 3545 | kvm_register_write(vcpu, VCPU_REGS_RAX, val); |
| 3546 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3547 | } else { |
| 3548 | if (io->in) { |
| 3549 | r = pio_copy_data(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3550 | if (r) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3551 | goto out; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | delta = 1; |
| 3555 | if (io->rep) { |
| 3556 | delta *= io->cur_count; |
| 3557 | /* |
| 3558 | * The size of the register should really depend on |
| 3559 | * current address size. |
| 3560 | */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3561 | val = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3562 | val -= delta; |
| 3563 | kvm_register_write(vcpu, VCPU_REGS_RCX, val); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3564 | } |
| 3565 | if (io->down) |
| 3566 | delta = -delta; |
| 3567 | delta *= io->size; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3568 | if (io->in) { |
| 3569 | val = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 3570 | val += delta; |
| 3571 | kvm_register_write(vcpu, VCPU_REGS_RDI, val); |
| 3572 | } else { |
| 3573 | val = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3574 | val += delta; |
| 3575 | kvm_register_write(vcpu, VCPU_REGS_RSI, val); |
| 3576 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3577 | } |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3578 | out: |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3579 | io->count -= io->cur_count; |
| 3580 | io->cur_count = 0; |
| 3581 | |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3585 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3586 | { |
| 3587 | /* TODO: String I/O for in kernel device */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3588 | int r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3589 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3590 | if (vcpu->arch.pio.in) |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3591 | r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3592 | vcpu->arch.pio.size, pd); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3593 | else |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3594 | r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
| 3595 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 3596 | pd); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3597 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3598 | } |
| 3599 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3600 | static int pio_string_write(struct kvm_vcpu *vcpu) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3601 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3602 | struct kvm_pio_request *io = &vcpu->arch.pio; |
| 3603 | void *pd = vcpu->arch.pio_data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3604 | int i, r = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3605 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3606 | for (i = 0; i < io->cur_count; i++) { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3607 | if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3608 | io->port, io->size, pd)) { |
| 3609 | r = -EOPNOTSUPP; |
| 3610 | break; |
| 3611 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3612 | pd += io->size; |
| 3613 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3614 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3615 | } |
| 3616 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3617 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3618 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3619 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3620 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3621 | trace_kvm_pio(!in, port, size, 1); |
| 3622 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3623 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3624 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3625 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3626 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3627 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
| 3628 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 3629 | vcpu->arch.pio.in = in; |
| 3630 | vcpu->arch.pio.string = 0; |
| 3631 | vcpu->arch.pio.down = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3632 | vcpu->arch.pio.rep = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3633 | |
Takuya Yoshikawa | 1976d2d | 2010-02-05 17:52:46 +0900 | [diff] [blame] | 3634 | if (!vcpu->arch.pio.in) { |
| 3635 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3636 | memcpy(vcpu->arch.pio_data, &val, 4); |
| 3637 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3638 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3639 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3640 | complete_pio(vcpu); |
| 3641 | return 1; |
| 3642 | } |
| 3643 | return 0; |
| 3644 | } |
| 3645 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 3646 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3647 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3648 | int size, unsigned long count, int down, |
| 3649 | gva_t address, int rep, unsigned port) |
| 3650 | { |
| 3651 | unsigned now, in_page; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3652 | int ret = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3653 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3654 | trace_kvm_pio(!in, port, size, count); |
| 3655 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3656 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3657 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3658 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3659 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3660 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
| 3661 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 3662 | vcpu->arch.pio.in = in; |
| 3663 | vcpu->arch.pio.string = 1; |
| 3664 | vcpu->arch.pio.down = down; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3665 | vcpu->arch.pio.rep = rep; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3666 | |
| 3667 | if (!count) { |
| 3668 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 3669 | return 1; |
| 3670 | } |
| 3671 | |
| 3672 | if (!down) |
| 3673 | in_page = PAGE_SIZE - offset_in_page(address); |
| 3674 | else |
| 3675 | in_page = offset_in_page(address) + size; |
| 3676 | now = min(count, (unsigned long)in_page / size); |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3677 | if (!now) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3678 | now = 1; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3679 | if (down) { |
| 3680 | /* |
| 3681 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 3682 | */ |
| 3683 | pr_unimpl(vcpu, "guest string pio down\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3684 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3685 | return 1; |
| 3686 | } |
| 3687 | vcpu->run->io.count = now; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3688 | vcpu->arch.pio.cur_count = now; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3689 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3690 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3691 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 3692 | |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3693 | vcpu->arch.pio.guest_gva = address; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3694 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3695 | if (!vcpu->arch.pio.in) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3696 | /* string PIO write */ |
| 3697 | ret = pio_copy_data(vcpu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3698 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3699 | return 1; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3700 | if (ret == 0 && !pio_string_write(vcpu)) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3701 | complete_pio(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3702 | if (vcpu->arch.pio.count == 0) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3703 | ret = 1; |
| 3704 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3705 | } |
| 3706 | /* no string PIO read support yet */ |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3707 | |
| 3708 | return ret; |
| 3709 | } |
| 3710 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
| 3711 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3712 | static void bounce_off(void *info) |
| 3713 | { |
| 3714 | /* nothing */ |
| 3715 | } |
| 3716 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3717 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 3718 | void *data) |
| 3719 | { |
| 3720 | struct cpufreq_freqs *freq = data; |
| 3721 | struct kvm *kvm; |
| 3722 | struct kvm_vcpu *vcpu; |
| 3723 | int i, send_ipi = 0; |
| 3724 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3725 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 3726 | return 0; |
| 3727 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 3728 | return 0; |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 3729 | per_cpu(cpu_tsc_khz, freq->cpu) = freq->new; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3730 | |
| 3731 | spin_lock(&kvm_lock); |
| 3732 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 3733 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3734 | if (vcpu->cpu != freq->cpu) |
| 3735 | continue; |
| 3736 | if (!kvm_request_guest_time_update(vcpu)) |
| 3737 | continue; |
| 3738 | if (vcpu->cpu != smp_processor_id()) |
| 3739 | send_ipi++; |
| 3740 | } |
| 3741 | } |
| 3742 | spin_unlock(&kvm_lock); |
| 3743 | |
| 3744 | if (freq->old < freq->new && send_ipi) { |
| 3745 | /* |
| 3746 | * We upscale the frequency. Must make the guest |
| 3747 | * doesn't see old kvmclock values while running with |
| 3748 | * the new frequency, otherwise we risk the guest sees |
| 3749 | * time go backwards. |
| 3750 | * |
| 3751 | * In case we update the frequency for another cpu |
| 3752 | * (which might be in guest context) send an interrupt |
| 3753 | * to kick the cpu out of guest context. Next time |
| 3754 | * guest context is entered kvmclock will be updated, |
| 3755 | * so the guest will not see stale values. |
| 3756 | */ |
| 3757 | smp_call_function_single(freq->cpu, bounce_off, NULL, 1); |
| 3758 | } |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
| 3762 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
| 3763 | .notifier_call = kvmclock_cpufreq_notifier |
| 3764 | }; |
| 3765 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3766 | static void kvm_timer_init(void) |
| 3767 | { |
| 3768 | int cpu; |
| 3769 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3770 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3771 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 3772 | CPUFREQ_TRANSITION_NOTIFIER); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 3773 | for_each_online_cpu(cpu) { |
| 3774 | unsigned long khz = cpufreq_get(cpu); |
| 3775 | if (!khz) |
| 3776 | khz = tsc_khz; |
| 3777 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 3778 | } |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 3779 | } else { |
| 3780 | for_each_possible_cpu(cpu) |
| 3781 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3782 | } |
| 3783 | } |
| 3784 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3785 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3786 | { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3787 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3788 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 3789 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3790 | if (kvm_x86_ops) { |
| 3791 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3792 | r = -EEXIST; |
| 3793 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | if (!ops->cpu_has_kvm_support()) { |
| 3797 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3798 | r = -EOPNOTSUPP; |
| 3799 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3800 | } |
| 3801 | if (ops->disabled_by_bios()) { |
| 3802 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3803 | r = -EOPNOTSUPP; |
| 3804 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3805 | } |
| 3806 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 3807 | r = kvm_mmu_module_init(); |
| 3808 | if (r) |
| 3809 | goto out; |
| 3810 | |
| 3811 | kvm_init_msr_list(); |
| 3812 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3813 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3814 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 3815 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 3816 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 3817 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3818 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3819 | kvm_timer_init(); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3820 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3821 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3822 | |
| 3823 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3824 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3825 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3826 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3827 | void kvm_arch_exit(void) |
| 3828 | { |
Jan Kiszka | 888d256 | 2009-04-17 19:24:58 +0200 | [diff] [blame] | 3829 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 3830 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 3831 | CPUFREQ_TRANSITION_NOTIFIER); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3832 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3833 | kvm_mmu_module_exit(); |
| 3834 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3835 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3836 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 3837 | { |
| 3838 | ++vcpu->stat.halt_exits; |
| 3839 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 3840 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3841 | return 1; |
| 3842 | } else { |
| 3843 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 3844 | return 0; |
| 3845 | } |
| 3846 | } |
| 3847 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 3848 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3849 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 3850 | unsigned long a1) |
| 3851 | { |
| 3852 | if (is_long_mode(vcpu)) |
| 3853 | return a0; |
| 3854 | else |
| 3855 | return a0 | ((gpa_t)a1 << 32); |
| 3856 | } |
| 3857 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3858 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 3859 | { |
| 3860 | u64 param, ingpa, outgpa, ret; |
| 3861 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 3862 | bool fast, longmode; |
| 3863 | int cs_db, cs_l; |
| 3864 | |
| 3865 | /* |
| 3866 | * hypercall generates UD from non zero cpl and real mode |
| 3867 | * per HYPER-V spec |
| 3868 | */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3869 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3870 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
| 3874 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 3875 | longmode = is_long_mode(vcpu) && cs_l == 1; |
| 3876 | |
| 3877 | if (!longmode) { |
Gleb Natapov | ccd4693 | 2010-01-19 15:06:38 +0200 | [diff] [blame] | 3878 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 3879 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 3880 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 3881 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 3882 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 3883 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3884 | } |
| 3885 | #ifdef CONFIG_X86_64 |
| 3886 | else { |
| 3887 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3888 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3889 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 3890 | } |
| 3891 | #endif |
| 3892 | |
| 3893 | code = param & 0xffff; |
| 3894 | fast = (param >> 16) & 0x1; |
| 3895 | rep_cnt = (param >> 32) & 0xfff; |
| 3896 | rep_idx = (param >> 48) & 0xfff; |
| 3897 | |
| 3898 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 3899 | |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 3900 | switch (code) { |
| 3901 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 3902 | kvm_vcpu_on_spin(vcpu); |
| 3903 | break; |
| 3904 | default: |
| 3905 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 3906 | break; |
| 3907 | } |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3908 | |
| 3909 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 3910 | if (longmode) { |
| 3911 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 3912 | } else { |
| 3913 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 3914 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 3915 | } |
| 3916 | |
| 3917 | return 1; |
| 3918 | } |
| 3919 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3920 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 3921 | { |
| 3922 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3923 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3924 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3925 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 3926 | return kvm_hv_hypercall(vcpu); |
| 3927 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3928 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3929 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3930 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3931 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3932 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3933 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3934 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3935 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3936 | if (!is_long_mode(vcpu)) { |
| 3937 | nr &= 0xFFFFFFFF; |
| 3938 | a0 &= 0xFFFFFFFF; |
| 3939 | a1 &= 0xFFFFFFFF; |
| 3940 | a2 &= 0xFFFFFFFF; |
| 3941 | a3 &= 0xFFFFFFFF; |
| 3942 | } |
| 3943 | |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 3944 | if (kvm_x86_ops->get_cpl(vcpu) != 0) { |
| 3945 | ret = -KVM_EPERM; |
| 3946 | goto out; |
| 3947 | } |
| 3948 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3949 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3950 | case KVM_HC_VAPIC_POLL_IRQ: |
| 3951 | ret = 0; |
| 3952 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3953 | case KVM_HC_MMU_OP: |
| 3954 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 3955 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3956 | default: |
| 3957 | ret = -KVM_ENOSYS; |
| 3958 | break; |
| 3959 | } |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 3960 | out: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3961 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 3962 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3963 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3964 | } |
| 3965 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 3966 | |
| 3967 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 3968 | { |
| 3969 | char instruction[3]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3970 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3971 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3972 | /* |
| 3973 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 3974 | * to ensure that the updated hypercall appears atomically across all |
| 3975 | * VCPUs. |
| 3976 | */ |
| 3977 | kvm_mmu_zap_all(vcpu->kvm); |
| 3978 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3979 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3980 | |
Takuya Yoshikawa | 7edcfac | 2010-02-01 22:11:52 +0900 | [diff] [blame] | 3981 | return emulator_write_emulated(rip, instruction, 3, vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 3985 | { |
| 3986 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 3987 | } |
| 3988 | |
| 3989 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 3990 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3991 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3992 | |
| 3993 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 3994 | } |
| 3995 | |
| 3996 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 3997 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3998 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3999 | |
| 4000 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 4001 | } |
| 4002 | |
| 4003 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 4004 | unsigned long *rflags) |
| 4005 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4006 | kvm_lmsw(vcpu, msw); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4007 | *rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 4011 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4012 | unsigned long value; |
| 4013 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4014 | switch (cr) { |
| 4015 | case 0: |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4016 | value = kvm_read_cr0(vcpu); |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4017 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4018 | case 2: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4019 | value = vcpu->arch.cr2; |
| 4020 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4021 | case 3: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4022 | value = vcpu->arch.cr3; |
| 4023 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4024 | case 4: |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4025 | value = kvm_read_cr4(vcpu); |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4026 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4027 | case 8: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4028 | value = kvm_get_cr8(vcpu); |
| 4029 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4030 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4031 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4032 | return 0; |
| 4033 | } |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4034 | |
| 4035 | return value; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 4039 | unsigned long *rflags) |
| 4040 | { |
| 4041 | switch (cr) { |
| 4042 | case 0: |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4043 | kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4044 | *rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4045 | break; |
| 4046 | case 2: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4047 | vcpu->arch.cr2 = val; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4048 | break; |
| 4049 | case 3: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4050 | kvm_set_cr3(vcpu, val); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4051 | break; |
| 4052 | case 4: |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4053 | kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4054 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4055 | case 8: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4056 | kvm_set_cr8(vcpu, val & 0xfUL); |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4057 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4058 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4059 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4063 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 4064 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4065 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 4066 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4067 | |
| 4068 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4069 | /* 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] | 4070 | for (j = i + 1; ; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4071 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4072 | if (ej->function == e->function) { |
| 4073 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4074 | return j; |
| 4075 | } |
| 4076 | } |
| 4077 | return 0; /* silence gcc, even though control never reaches here */ |
| 4078 | } |
| 4079 | |
| 4080 | /* find an entry with matching function, matching index (if needed), and that |
| 4081 | * should be read next (if it's stateful) */ |
| 4082 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 4083 | u32 function, u32 index) |
| 4084 | { |
| 4085 | if (e->function != function) |
| 4086 | return 0; |
| 4087 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 4088 | return 0; |
| 4089 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4090 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4091 | return 0; |
| 4092 | return 1; |
| 4093 | } |
| 4094 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4095 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 4096 | u32 function, u32 index) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4097 | { |
| 4098 | int i; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4099 | struct kvm_cpuid_entry2 *best = NULL; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4100 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4101 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4102 | struct kvm_cpuid_entry2 *e; |
| 4103 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4104 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4105 | if (is_matching_cpuid_entry(e, function, index)) { |
| 4106 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 4107 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4108 | best = e; |
| 4109 | break; |
| 4110 | } |
| 4111 | /* |
| 4112 | * Both basic or both extended? |
| 4113 | */ |
| 4114 | if (((e->function ^ function) & 0x80000000) == 0) |
| 4115 | if (!best || e->function > best->function) |
| 4116 | best = e; |
| 4117 | } |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4118 | return best; |
| 4119 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4120 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4121 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4122 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
| 4123 | { |
| 4124 | struct kvm_cpuid_entry2 *best; |
| 4125 | |
| 4126 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 4127 | if (best) |
| 4128 | return best->eax & 0xff; |
| 4129 | return 36; |
| 4130 | } |
| 4131 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4132 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 4133 | { |
| 4134 | u32 function, index; |
| 4135 | struct kvm_cpuid_entry2 *best; |
| 4136 | |
| 4137 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4138 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4139 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 4140 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 4141 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 4142 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
| 4143 | best = kvm_find_cpuid_entry(vcpu, function, index); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4144 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4145 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 4146 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 4147 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 4148 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4149 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4150 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4151 | trace_kvm_cpuid(function, |
| 4152 | kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 4153 | kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 4154 | kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 4155 | kvm_register_read(vcpu, VCPU_REGS_RDX)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4156 | } |
| 4157 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4158 | |
| 4159 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4160 | * Check if userspace requested an interrupt window, and that the |
| 4161 | * interrupt window is open. |
| 4162 | * |
| 4163 | * No need to exit to userspace if we already have an interrupt queued. |
| 4164 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4165 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4166 | { |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4167 | return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4168 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 5df5664 | 2009-04-21 17:44:59 +0300 | [diff] [blame] | 4169 | kvm_arch_interrupt_allowed(vcpu)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4170 | } |
| 4171 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4172 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4173 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4174 | struct kvm_run *kvm_run = vcpu->run; |
| 4175 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4176 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4177 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4178 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4179 | if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4180 | kvm_run->ready_for_interrupt_injection = 1; |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4181 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4182 | kvm_run->ready_for_interrupt_injection = |
Gleb Natapov | fa9726b | 2009-05-11 13:35:47 +0300 | [diff] [blame] | 4183 | kvm_arch_interrupt_allowed(vcpu) && |
| 4184 | !kvm_cpu_has_interrupt(vcpu) && |
| 4185 | !kvm_event_needs_reinjection(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4186 | } |
| 4187 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4188 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 4189 | { |
| 4190 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 4191 | struct page *page; |
| 4192 | |
| 4193 | if (!apic || !apic->vapic_addr) |
| 4194 | return; |
| 4195 | |
| 4196 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4197 | |
| 4198 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 4202 | { |
| 4203 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4204 | int idx; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4205 | |
| 4206 | if (!apic || !apic->vapic_addr) |
| 4207 | return; |
| 4208 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4209 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4210 | kvm_release_page_dirty(apic->vapic_page); |
| 4211 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4212 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4213 | } |
| 4214 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4215 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 4216 | { |
| 4217 | int max_irr, tpr; |
| 4218 | |
| 4219 | if (!kvm_x86_ops->update_cr8_intercept) |
| 4220 | return; |
| 4221 | |
Avi Kivity | 88c808f | 2009-08-17 22:49:40 +0300 | [diff] [blame] | 4222 | if (!vcpu->arch.apic) |
| 4223 | return; |
| 4224 | |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4225 | if (!vcpu->arch.apic->vapic_addr) |
| 4226 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 4227 | else |
| 4228 | max_irr = -1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4229 | |
| 4230 | if (max_irr != -1) |
| 4231 | max_irr >>= 4; |
| 4232 | |
| 4233 | tpr = kvm_lapic_get_cr8(vcpu); |
| 4234 | |
| 4235 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); |
| 4236 | } |
| 4237 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4238 | static void inject_pending_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4239 | { |
| 4240 | /* try to reinject previous events if any */ |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4241 | if (vcpu->arch.exception.pending) { |
Avi Kivity | 5c1c85d0 | 2010-03-11 13:01:59 +0200 | [diff] [blame] | 4242 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
| 4243 | vcpu->arch.exception.has_error_code, |
| 4244 | vcpu->arch.exception.error_code); |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4245 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 4246 | vcpu->arch.exception.has_error_code, |
| 4247 | vcpu->arch.exception.error_code); |
| 4248 | return; |
| 4249 | } |
| 4250 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4251 | if (vcpu->arch.nmi_injected) { |
| 4252 | kvm_x86_ops->set_nmi(vcpu); |
| 4253 | return; |
| 4254 | } |
| 4255 | |
| 4256 | if (vcpu->arch.interrupt.pending) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4257 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4258 | return; |
| 4259 | } |
| 4260 | |
| 4261 | /* try to inject new event if pending */ |
| 4262 | if (vcpu->arch.nmi_pending) { |
| 4263 | if (kvm_x86_ops->nmi_allowed(vcpu)) { |
| 4264 | vcpu->arch.nmi_pending = false; |
| 4265 | vcpu->arch.nmi_injected = true; |
| 4266 | kvm_x86_ops->set_nmi(vcpu); |
| 4267 | } |
| 4268 | } else if (kvm_cpu_has_interrupt(vcpu)) { |
| 4269 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4270 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
| 4271 | false); |
| 4272 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4273 | } |
| 4274 | } |
| 4275 | } |
| 4276 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4277 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4278 | { |
| 4279 | int r; |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4280 | bool req_int_win = !irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4281 | vcpu->run->request_interrupt_window; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4282 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 4283 | if (vcpu->requests) |
| 4284 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 4285 | kvm_mmu_unload(vcpu); |
| 4286 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4287 | r = kvm_mmu_reload(vcpu); |
| 4288 | if (unlikely(r)) |
| 4289 | goto out; |
| 4290 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4291 | if (vcpu->requests) { |
| 4292 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 4293 | __kvm_migrate_timers(vcpu); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4294 | if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests)) |
| 4295 | kvm_write_guest_time(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 4296 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
| 4297 | kvm_mmu_sync_roots(vcpu); |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 4298 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 4299 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4300 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
| 4301 | &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4302 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4303 | r = 0; |
| 4304 | goto out; |
| 4305 | } |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4306 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4307 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4308 | r = 0; |
| 4309 | goto out; |
| 4310 | } |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4311 | if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) { |
| 4312 | vcpu->fpu_active = 0; |
| 4313 | kvm_x86_ops->fpu_deactivate(vcpu); |
| 4314 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4315 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4316 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4317 | preempt_disable(); |
| 4318 | |
| 4319 | kvm_x86_ops->prepare_guest_switch(vcpu); |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 4320 | if (vcpu->fpu_active) |
| 4321 | kvm_load_guest_fpu(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4322 | |
| 4323 | local_irq_disable(); |
| 4324 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4325 | clear_bit(KVM_REQ_KICK, &vcpu->requests); |
| 4326 | smp_mb__after_clear_bit(); |
| 4327 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4328 | if (vcpu->requests || need_resched() || signal_pending(current)) { |
Gleb Natapov | c7f0f24 | 2009-07-07 15:27:32 +0300 | [diff] [blame] | 4329 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Avi Kivity | 6c14280 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 4330 | local_irq_enable(); |
| 4331 | preempt_enable(); |
| 4332 | r = 1; |
| 4333 | goto out; |
| 4334 | } |
| 4335 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4336 | inject_pending_event(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4337 | |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4338 | /* enable NMI/IRQ window open exits if needed */ |
| 4339 | if (vcpu->arch.nmi_pending) |
| 4340 | kvm_x86_ops->enable_nmi_window(vcpu); |
| 4341 | else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) |
| 4342 | kvm_x86_ops->enable_irq_window(vcpu); |
| 4343 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4344 | if (kvm_lapic_enabled(vcpu)) { |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4345 | update_cr8_intercept(vcpu); |
| 4346 | kvm_lapic_sync_to_vapic(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4347 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4348 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4349 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4350 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4351 | kvm_guest_enter(); |
| 4352 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4353 | if (unlikely(vcpu->arch.switch_db_regs)) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4354 | set_debugreg(0, 7); |
| 4355 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 4356 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 4357 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 4358 | set_debugreg(vcpu->arch.eff_db[3], 3); |
| 4359 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4360 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4361 | trace_kvm_entry(vcpu->vcpu_id); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4362 | kvm_x86_ops->run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4363 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4364 | /* |
| 4365 | * If the guest has used debug registers, at least dr7 |
| 4366 | * will be disabled while returning to the host. |
| 4367 | * If we don't have active breakpoints in the host, we don't |
| 4368 | * care about the messed up debug address registers. But if |
| 4369 | * we have some of them active, restore the old state. |
| 4370 | */ |
Frederic Weisbecker | 59d8eb5 | 2009-11-10 11:03:12 +0100 | [diff] [blame] | 4371 | if (hw_breakpoint_active()) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4372 | hw_breakpoint_restore(); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4373 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4374 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4375 | local_irq_enable(); |
| 4376 | |
| 4377 | ++vcpu->stat.exits; |
| 4378 | |
| 4379 | /* |
| 4380 | * We must have an instruction between local_irq_enable() and |
| 4381 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 4382 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 4383 | * But we need to prevent reordering, hence this barrier(): |
| 4384 | */ |
| 4385 | barrier(); |
| 4386 | |
| 4387 | kvm_guest_exit(); |
| 4388 | |
| 4389 | preempt_enable(); |
| 4390 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4391 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4392 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4393 | /* |
| 4394 | * Profile KVM exit RIPs: |
| 4395 | */ |
| 4396 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4397 | unsigned long rip = kvm_rip_read(vcpu); |
| 4398 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4399 | } |
| 4400 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4401 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4402 | kvm_lapic_sync_from_vapic(vcpu); |
| 4403 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4404 | r = kvm_x86_ops->handle_exit(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4405 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4406 | return r; |
| 4407 | } |
| 4408 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4409 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4410 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4411 | { |
| 4412 | int r; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4413 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4414 | |
| 4415 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 4416 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 4417 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4418 | kvm_lapic_reset(vcpu); |
Gleb Natapov | 5f17928 | 2008-10-07 15:42:33 +0200 | [diff] [blame] | 4419 | r = kvm_arch_vcpu_reset(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4420 | if (r) |
| 4421 | return r; |
| 4422 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4423 | } |
| 4424 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4425 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4426 | vapic_enter(vcpu); |
| 4427 | |
| 4428 | r = 1; |
| 4429 | while (r > 0) { |
Gleb Natapov | af2152f | 2008-09-22 14:28:53 +0300 | [diff] [blame] | 4430 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4431 | r = vcpu_enter_guest(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4432 | else { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4433 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4434 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4435 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4436 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests)) |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4437 | { |
| 4438 | switch(vcpu->arch.mp_state) { |
| 4439 | case KVM_MP_STATE_HALTED: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4440 | vcpu->arch.mp_state = |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4441 | KVM_MP_STATE_RUNNABLE; |
| 4442 | case KVM_MP_STATE_RUNNABLE: |
| 4443 | break; |
| 4444 | case KVM_MP_STATE_SIPI_RECEIVED: |
| 4445 | default: |
| 4446 | r = -EINTR; |
| 4447 | break; |
| 4448 | } |
| 4449 | } |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4450 | } |
| 4451 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4452 | if (r <= 0) |
| 4453 | break; |
| 4454 | |
| 4455 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
| 4456 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 4457 | kvm_inject_pending_timer_irqs(vcpu); |
| 4458 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4459 | if (dm_request_for_irq_injection(vcpu)) { |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4460 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4461 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4462 | ++vcpu->stat.request_irq_exits; |
| 4463 | } |
| 4464 | if (signal_pending(current)) { |
| 4465 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4466 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4467 | ++vcpu->stat.signal_exits; |
| 4468 | } |
| 4469 | if (need_resched()) { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4470 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4471 | kvm_resched(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4472 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4476 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4477 | post_kvm_run_save(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4478 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4479 | vapic_exit(vcpu); |
| 4480 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4481 | return r; |
| 4482 | } |
| 4483 | |
| 4484 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4485 | { |
| 4486 | int r; |
| 4487 | sigset_t sigsaved; |
| 4488 | |
| 4489 | vcpu_load(vcpu); |
| 4490 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4491 | if (vcpu->sigset_active) |
| 4492 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 4493 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4494 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 4495 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4496 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4497 | r = -EAGAIN; |
| 4498 | goto out; |
| 4499 | } |
| 4500 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4501 | /* re-sync apic's tpr */ |
| 4502 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4503 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4504 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4505 | if (vcpu->arch.pio.cur_count) { |
Gleb Natapov | 7567cae | 2010-03-09 12:01:10 +0200 | [diff] [blame] | 4506 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4507 | r = complete_pio(vcpu); |
Gleb Natapov | 7567cae | 2010-03-09 12:01:10 +0200 | [diff] [blame] | 4508 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4509 | if (r) |
| 4510 | goto out; |
| 4511 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4512 | if (vcpu->mmio_needed) { |
| 4513 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 4514 | vcpu->mmio_read_completed = 1; |
| 4515 | vcpu->mmio_needed = 0; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4516 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4517 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4518 | r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4519 | EMULTYPE_NO_DECODE); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4520 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4521 | if (r == EMULATE_DO_MMIO) { |
| 4522 | /* |
| 4523 | * Read-modify-write. Back to userspace. |
| 4524 | */ |
| 4525 | r = 0; |
| 4526 | goto out; |
| 4527 | } |
| 4528 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4529 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 4530 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 4531 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4532 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4533 | r = __vcpu_run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4534 | |
| 4535 | out: |
| 4536 | if (vcpu->sigset_active) |
| 4537 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 4538 | |
| 4539 | vcpu_put(vcpu); |
| 4540 | return r; |
| 4541 | } |
| 4542 | |
| 4543 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4544 | { |
| 4545 | vcpu_load(vcpu); |
| 4546 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4547 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4548 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4549 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4550 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4551 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 4552 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 4553 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 4554 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4555 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4556 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 4557 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 4558 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 4559 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 4560 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 4561 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 4562 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 4563 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4564 | #endif |
| 4565 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4566 | regs->rip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4567 | regs->rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4568 | |
| 4569 | vcpu_put(vcpu); |
| 4570 | |
| 4571 | return 0; |
| 4572 | } |
| 4573 | |
| 4574 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4575 | { |
| 4576 | vcpu_load(vcpu); |
| 4577 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4578 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 4579 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 4580 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 4581 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 4582 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 4583 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 4584 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 4585 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4586 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4587 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 4588 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 4589 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 4590 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 4591 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 4592 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 4593 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 4594 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4595 | #endif |
| 4596 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4597 | kvm_rip_write(vcpu, regs->rip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4598 | kvm_set_rflags(vcpu, regs->rflags); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4599 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 4600 | vcpu->arch.exception.pending = false; |
| 4601 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4602 | vcpu_put(vcpu); |
| 4603 | |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4607 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 4608 | struct kvm_segment *var, int seg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4609 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 4610 | kvm_x86_ops->get_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4614 | { |
| 4615 | struct kvm_segment cs; |
| 4616 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4617 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4618 | *db = cs.db; |
| 4619 | *l = cs.l; |
| 4620 | } |
| 4621 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 4622 | |
| 4623 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 4624 | struct kvm_sregs *sregs) |
| 4625 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4626 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4627 | |
| 4628 | vcpu_load(vcpu); |
| 4629 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4630 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 4631 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 4632 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 4633 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 4634 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 4635 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4636 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4637 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 4638 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4639 | |
| 4640 | kvm_x86_ops->get_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4641 | sregs->idt.limit = dt.size; |
| 4642 | sregs->idt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4643 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4644 | sregs->gdt.limit = dt.size; |
| 4645 | sregs->gdt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4646 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4647 | sregs->cr0 = kvm_read_cr0(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4648 | sregs->cr2 = vcpu->arch.cr2; |
| 4649 | sregs->cr3 = vcpu->arch.cr3; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4650 | sregs->cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4651 | sregs->cr8 = kvm_get_cr8(vcpu); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4652 | sregs->efer = vcpu->arch.efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4653 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 4654 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4655 | memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4656 | |
Gleb Natapov | 36752c9 | 2009-05-11 13:35:53 +0300 | [diff] [blame] | 4657 | if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft) |
Gleb Natapov | 14d0bc1 | 2009-04-21 17:45:11 +0300 | [diff] [blame] | 4658 | set_bit(vcpu->arch.interrupt.nr, |
| 4659 | (unsigned long *)sregs->interrupt_bitmap); |
Gleb Natapov | 16d7a19 | 2009-04-21 17:45:10 +0300 | [diff] [blame] | 4660 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4661 | vcpu_put(vcpu); |
| 4662 | |
| 4663 | return 0; |
| 4664 | } |
| 4665 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 4666 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 4667 | struct kvm_mp_state *mp_state) |
| 4668 | { |
| 4669 | vcpu_load(vcpu); |
| 4670 | mp_state->mp_state = vcpu->arch.mp_state; |
| 4671 | vcpu_put(vcpu); |
| 4672 | return 0; |
| 4673 | } |
| 4674 | |
| 4675 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 4676 | struct kvm_mp_state *mp_state) |
| 4677 | { |
| 4678 | vcpu_load(vcpu); |
| 4679 | vcpu->arch.mp_state = mp_state->mp_state; |
| 4680 | vcpu_put(vcpu); |
| 4681 | return 0; |
| 4682 | } |
| 4683 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4684 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4685 | struct kvm_segment *var, int seg) |
| 4686 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 4687 | kvm_x86_ops->set_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4688 | } |
| 4689 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4690 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
| 4691 | struct kvm_segment *kvm_desct) |
| 4692 | { |
Akinobu Mita | 46a359e | 2009-07-18 23:58:32 +0900 | [diff] [blame] | 4693 | kvm_desct->base = get_desc_base(seg_desc); |
| 4694 | kvm_desct->limit = get_desc_limit(seg_desc); |
Marcelo Tosatti | c93cd3a | 2008-07-19 19:08:07 -0300 | [diff] [blame] | 4695 | if (seg_desc->g) { |
| 4696 | kvm_desct->limit <<= 12; |
| 4697 | kvm_desct->limit |= 0xfff; |
| 4698 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4699 | kvm_desct->selector = selector; |
| 4700 | kvm_desct->type = seg_desc->type; |
| 4701 | kvm_desct->present = seg_desc->p; |
| 4702 | kvm_desct->dpl = seg_desc->dpl; |
| 4703 | kvm_desct->db = seg_desc->d; |
| 4704 | kvm_desct->s = seg_desc->s; |
| 4705 | kvm_desct->l = seg_desc->l; |
| 4706 | kvm_desct->g = seg_desc->g; |
| 4707 | kvm_desct->avl = seg_desc->avl; |
| 4708 | if (!selector) |
| 4709 | kvm_desct->unusable = 1; |
| 4710 | else |
| 4711 | kvm_desct->unusable = 0; |
| 4712 | kvm_desct->padding = 0; |
| 4713 | } |
| 4714 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4715 | static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu, |
| 4716 | u16 selector, |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4717 | struct desc_ptr *dtable) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4718 | { |
| 4719 | if (selector & 1 << 2) { |
| 4720 | struct kvm_segment kvm_seg; |
| 4721 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4722 | kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4723 | |
| 4724 | if (kvm_seg.unusable) |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4725 | dtable->size = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4726 | else |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4727 | dtable->size = kvm_seg.limit; |
| 4728 | dtable->address = kvm_seg.base; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4729 | } |
| 4730 | else |
| 4731 | kvm_x86_ops->get_gdt(vcpu, dtable); |
| 4732 | } |
| 4733 | |
| 4734 | /* allowed just for 8 bytes segments */ |
| 4735 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 4736 | struct desc_struct *seg_desc) |
| 4737 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4738 | struct desc_ptr dtable; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4739 | u16 index = selector >> 3; |
Takuya Yoshikawa | 6f55048 | 2010-02-18 12:15:00 +0200 | [diff] [blame] | 4740 | int ret; |
| 4741 | u32 err; |
| 4742 | gva_t addr; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4743 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4744 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4745 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4746 | if (dtable.size < index * 8 + 7) { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4747 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); |
Takuya Yoshikawa | c125c60 | 2010-02-01 22:11:04 +0900 | [diff] [blame] | 4748 | return X86EMUL_PROPAGATE_FAULT; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4749 | } |
Takuya Yoshikawa | 6f55048 | 2010-02-18 12:15:00 +0200 | [diff] [blame] | 4750 | addr = dtable.base + index * 8; |
| 4751 | ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc), |
| 4752 | vcpu, &err); |
| 4753 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 4754 | kvm_inject_page_fault(vcpu, addr, err); |
| 4755 | |
| 4756 | return ret; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | /* allowed just for 8 bytes segments */ |
| 4760 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 4761 | struct desc_struct *seg_desc) |
| 4762 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4763 | struct desc_ptr dtable; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4764 | u16 index = selector >> 3; |
| 4765 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4766 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4767 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4768 | if (dtable.size < index * 8 + 7) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4769 | return 1; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4770 | return kvm_write_guest_virt(dtable.address + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4771 | } |
| 4772 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 4773 | static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu, |
| 4774 | struct desc_struct *seg_desc) |
| 4775 | { |
| 4776 | u32 base_addr = get_desc_base(seg_desc); |
| 4777 | |
| 4778 | return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL); |
| 4779 | } |
| 4780 | |
| 4781 | static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4782 | struct desc_struct *seg_desc) |
| 4783 | { |
Akinobu Mita | 46a359e | 2009-07-18 23:58:32 +0900 | [diff] [blame] | 4784 | u32 base_addr = get_desc_base(seg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4785 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 4786 | return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4787 | } |
| 4788 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4789 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) |
| 4790 | { |
| 4791 | struct kvm_segment kvm_seg; |
| 4792 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4793 | kvm_get_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4794 | return kvm_seg.selector; |
| 4795 | } |
| 4796 | |
Harvey Harrison | 2259e3a | 2008-08-22 13:29:17 -0700 | [diff] [blame] | 4797 | static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg) |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4798 | { |
| 4799 | struct kvm_segment segvar = { |
| 4800 | .base = selector << 4, |
| 4801 | .limit = 0xffff, |
| 4802 | .selector = selector, |
| 4803 | .type = 3, |
| 4804 | .present = 1, |
| 4805 | .dpl = 3, |
| 4806 | .db = 0, |
| 4807 | .s = 1, |
| 4808 | .l = 0, |
| 4809 | .g = 0, |
| 4810 | .avl = 0, |
| 4811 | .unusable = 0, |
| 4812 | }; |
| 4813 | kvm_x86_ops->set_segment(vcpu, &segvar, seg); |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4814 | return X86EMUL_CONTINUE; |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4815 | } |
| 4816 | |
Anthony Liguori | c0c7c04 | 2009-08-11 15:57:59 -0500 | [diff] [blame] | 4817 | static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg) |
| 4818 | { |
| 4819 | return (seg != VCPU_SREG_LDTR) && |
| 4820 | (seg != VCPU_SREG_TR) && |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4821 | (kvm_get_rflags(vcpu) & X86_EFLAGS_VM); |
Anthony Liguori | c0c7c04 | 2009-08-11 15:57:59 -0500 | [diff] [blame] | 4822 | } |
| 4823 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4824 | int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4825 | { |
| 4826 | struct kvm_segment kvm_seg; |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4827 | struct desc_struct seg_desc; |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4828 | u8 dpl, rpl, cpl; |
| 4829 | unsigned err_vec = GP_VECTOR; |
| 4830 | u32 err_code = 0; |
| 4831 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 4832 | int ret; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4833 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 4834 | if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu)) |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4835 | return kvm_load_realmode_segment(vcpu, selector, seg); |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4836 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4837 | /* NULL selector is not valid for TR, CS and SS */ |
| 4838 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 4839 | && null_selector) |
| 4840 | goto exception; |
| 4841 | |
| 4842 | /* TR should be in GDT only */ |
| 4843 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 4844 | goto exception; |
| 4845 | |
| 4846 | ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc); |
| 4847 | if (ret) |
| 4848 | return ret; |
| 4849 | |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4850 | seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg); |
Marcelo Tosatti | cb84b55 | 2009-11-11 17:29:49 -0200 | [diff] [blame] | 4851 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4852 | if (null_selector) { /* for NULL selector skip all following checks */ |
| 4853 | kvm_seg.unusable = 1; |
| 4854 | goto load; |
| 4855 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4856 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4857 | err_code = selector & 0xfffc; |
| 4858 | err_vec = GP_VECTOR; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4859 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4860 | /* can't load system descriptor into segment selecor */ |
| 4861 | if (seg <= VCPU_SREG_GS && !kvm_seg.s) |
| 4862 | goto exception; |
| 4863 | |
| 4864 | if (!kvm_seg.present) { |
| 4865 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 4866 | goto exception; |
| 4867 | } |
| 4868 | |
| 4869 | rpl = selector & 3; |
| 4870 | dpl = kvm_seg.dpl; |
| 4871 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 4872 | |
| 4873 | switch (seg) { |
| 4874 | case VCPU_SREG_SS: |
| 4875 | /* |
| 4876 | * segment is not a writable data segment or segment |
| 4877 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 4878 | */ |
| 4879 | if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl) |
| 4880 | goto exception; |
| 4881 | break; |
| 4882 | case VCPU_SREG_CS: |
| 4883 | if (!(kvm_seg.type & 8)) |
| 4884 | goto exception; |
| 4885 | |
| 4886 | if (kvm_seg.type & 4) { |
| 4887 | /* conforming */ |
| 4888 | if (dpl > cpl) |
| 4889 | goto exception; |
| 4890 | } else { |
| 4891 | /* nonconforming */ |
| 4892 | if (rpl > cpl || dpl != cpl) |
| 4893 | goto exception; |
| 4894 | } |
| 4895 | /* CS(RPL) <- CPL */ |
| 4896 | selector = (selector & 0xfffc) | cpl; |
| 4897 | break; |
| 4898 | case VCPU_SREG_TR: |
| 4899 | if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9)) |
| 4900 | goto exception; |
| 4901 | break; |
| 4902 | case VCPU_SREG_LDTR: |
| 4903 | if (kvm_seg.s || kvm_seg.type != 2) |
| 4904 | goto exception; |
| 4905 | break; |
| 4906 | default: /* DS, ES, FS, or GS */ |
| 4907 | /* |
| 4908 | * segment is not a data or readable code segment or |
| 4909 | * ((segment is a data or nonconforming code segment) |
| 4910 | * and (both RPL and CPL > DPL)) |
| 4911 | */ |
| 4912 | if ((kvm_seg.type & 0xa) == 0x8 || |
| 4913 | (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl))) |
| 4914 | goto exception; |
| 4915 | break; |
| 4916 | } |
| 4917 | |
| 4918 | if (!kvm_seg.unusable && kvm_seg.s) { |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4919 | /* mark segment as accessed */ |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4920 | kvm_seg.type |= 1; |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4921 | seg_desc.type |= 1; |
| 4922 | save_guest_segment_descriptor(vcpu, selector, &seg_desc); |
| 4923 | } |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4924 | load: |
| 4925 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 4926 | return X86EMUL_CONTINUE; |
| 4927 | exception: |
| 4928 | kvm_queue_exception_e(vcpu, err_vec, err_code); |
| 4929 | return X86EMUL_PROPAGATE_FAULT; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, |
| 4933 | struct tss_segment_32 *tss) |
| 4934 | { |
| 4935 | tss->cr3 = vcpu->arch.cr3; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4936 | tss->eip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4937 | tss->eflags = kvm_get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4938 | tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4939 | tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4940 | tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4941 | tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4942 | tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 4943 | tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 4944 | tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 4945 | tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4946 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 4947 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 4948 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 4949 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 4950 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); |
| 4951 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); |
| 4952 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4953 | } |
| 4954 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4955 | static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg) |
| 4956 | { |
| 4957 | struct kvm_segment kvm_seg; |
| 4958 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 4959 | kvm_seg.selector = sel; |
| 4960 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 4961 | } |
| 4962 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4963 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, |
| 4964 | struct tss_segment_32 *tss) |
| 4965 | { |
| 4966 | kvm_set_cr3(vcpu, tss->cr3); |
| 4967 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4968 | kvm_rip_write(vcpu, tss->eip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4969 | kvm_set_rflags(vcpu, tss->eflags | 2); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4970 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4971 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax); |
| 4972 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx); |
| 4973 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx); |
| 4974 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx); |
| 4975 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp); |
| 4976 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp); |
| 4977 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi); |
| 4978 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4979 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4980 | /* |
| 4981 | * SDM says that segment selectors are loaded before segment |
| 4982 | * descriptors |
| 4983 | */ |
| 4984 | kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR); |
| 4985 | kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES); |
| 4986 | kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS); |
| 4987 | kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS); |
| 4988 | kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS); |
| 4989 | kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS); |
| 4990 | kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS); |
| 4991 | |
| 4992 | /* |
| 4993 | * Now load segment descriptors. If fault happenes at this stage |
| 4994 | * it is handled in a context of new task |
| 4995 | */ |
| 4996 | if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4997 | return 1; |
| 4998 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4999 | if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5000 | return 1; |
| 5001 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5002 | if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5003 | return 1; |
| 5004 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5005 | if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5006 | return 1; |
| 5007 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5008 | if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5009 | return 1; |
| 5010 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5011 | if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5012 | return 1; |
| 5013 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5014 | if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5015 | return 1; |
| 5016 | return 0; |
| 5017 | } |
| 5018 | |
| 5019 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, |
| 5020 | struct tss_segment_16 *tss) |
| 5021 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5022 | tss->ip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5023 | tss->flag = kvm_get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5024 | tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 5025 | tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 5026 | tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 5027 | tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 5028 | tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 5029 | tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 5030 | tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 5031 | tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5032 | |
| 5033 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 5034 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 5035 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 5036 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 5037 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, |
| 5041 | struct tss_segment_16 *tss) |
| 5042 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5043 | kvm_rip_write(vcpu, tss->ip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5044 | kvm_set_rflags(vcpu, tss->flag | 2); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5045 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax); |
| 5046 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx); |
| 5047 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx); |
| 5048 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx); |
| 5049 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp); |
| 5050 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp); |
| 5051 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si); |
| 5052 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5053 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5054 | /* |
| 5055 | * SDM says that segment selectors are loaded before segment |
| 5056 | * descriptors |
| 5057 | */ |
| 5058 | kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR); |
| 5059 | kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES); |
| 5060 | kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS); |
| 5061 | kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS); |
| 5062 | kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS); |
| 5063 | |
| 5064 | /* |
| 5065 | * Now load segment descriptors. If fault happenes at this stage |
| 5066 | * it is handled in a context of new task |
| 5067 | */ |
| 5068 | if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5069 | return 1; |
| 5070 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5071 | if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5072 | return 1; |
| 5073 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5074 | if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5075 | return 1; |
| 5076 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5077 | if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5078 | return 1; |
| 5079 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5080 | if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5081 | return 1; |
| 5082 | return 0; |
| 5083 | } |
| 5084 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 5085 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5086 | u16 old_tss_sel, u32 old_tss_base, |
| 5087 | struct desc_struct *nseg_desc) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5088 | { |
| 5089 | struct tss_segment_16 tss_segment_16; |
| 5090 | int ret = 0; |
| 5091 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5092 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 5093 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5094 | goto out; |
| 5095 | |
| 5096 | save_state_to_tss16(vcpu, &tss_segment_16); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5097 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5098 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 5099 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5100 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5101 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5102 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc), |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5103 | &tss_segment_16, sizeof tss_segment_16)) |
| 5104 | goto out; |
| 5105 | |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5106 | if (old_tss_sel != 0xffff) { |
| 5107 | tss_segment_16.prev_task_link = old_tss_sel; |
| 5108 | |
| 5109 | if (kvm_write_guest(vcpu->kvm, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5110 | get_tss_base_addr_write(vcpu, nseg_desc), |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5111 | &tss_segment_16.prev_task_link, |
| 5112 | sizeof tss_segment_16.prev_task_link)) |
| 5113 | goto out; |
| 5114 | } |
| 5115 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5116 | if (load_state_from_tss16(vcpu, &tss_segment_16)) |
| 5117 | goto out; |
| 5118 | |
| 5119 | ret = 1; |
| 5120 | out: |
| 5121 | return ret; |
| 5122 | } |
| 5123 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 5124 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5125 | u16 old_tss_sel, u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5126 | struct desc_struct *nseg_desc) |
| 5127 | { |
| 5128 | struct tss_segment_32 tss_segment_32; |
| 5129 | int ret = 0; |
| 5130 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5131 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 5132 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5133 | goto out; |
| 5134 | |
| 5135 | save_state_to_tss32(vcpu, &tss_segment_32); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5136 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5137 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 5138 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5139 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5140 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5141 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc), |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5142 | &tss_segment_32, sizeof tss_segment_32)) |
| 5143 | goto out; |
| 5144 | |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5145 | if (old_tss_sel != 0xffff) { |
| 5146 | tss_segment_32.prev_task_link = old_tss_sel; |
| 5147 | |
| 5148 | if (kvm_write_guest(vcpu->kvm, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5149 | get_tss_base_addr_write(vcpu, nseg_desc), |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5150 | &tss_segment_32.prev_task_link, |
| 5151 | sizeof tss_segment_32.prev_task_link)) |
| 5152 | goto out; |
| 5153 | } |
| 5154 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5155 | if (load_state_from_tss32(vcpu, &tss_segment_32)) |
| 5156 | goto out; |
| 5157 | |
| 5158 | ret = 1; |
| 5159 | out: |
| 5160 | return ret; |
| 5161 | } |
| 5162 | |
| 5163 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) |
| 5164 | { |
| 5165 | struct kvm_segment tr_seg; |
| 5166 | struct desc_struct cseg_desc; |
| 5167 | struct desc_struct nseg_desc; |
| 5168 | int ret = 0; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5169 | u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR); |
| 5170 | u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR); |
Jan Kiszka | e8861cfe | 2010-04-14 16:57:11 +0200 | [diff] [blame] | 5171 | u32 desc_limit; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5172 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5173 | old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5174 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5175 | /* FIXME: Handle errors. Failure to read either TSS or their |
| 5176 | * descriptors should generate a pagefault. |
| 5177 | */ |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5178 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) |
| 5179 | goto out; |
| 5180 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5181 | if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5182 | goto out; |
| 5183 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5184 | if (reason != TASK_SWITCH_IRET) { |
| 5185 | int cpl; |
| 5186 | |
| 5187 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 5188 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { |
| 5189 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 5190 | return 1; |
| 5191 | } |
| 5192 | } |
| 5193 | |
Jan Kiszka | e8861cfe | 2010-04-14 16:57:11 +0200 | [diff] [blame] | 5194 | desc_limit = get_desc_limit(&nseg_desc); |
| 5195 | if (!nseg_desc.p || |
| 5196 | ((desc_limit < 0x67 && (nseg_desc.type & 8)) || |
| 5197 | desc_limit < 0x2b)) { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5198 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); |
| 5199 | return 1; |
| 5200 | } |
| 5201 | |
| 5202 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 5203 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5204 | save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | if (reason == TASK_SWITCH_IRET) { |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5208 | u32 eflags = kvm_get_rflags(vcpu); |
| 5209 | kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5210 | } |
| 5211 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5212 | /* set back link to prev task only if NT bit is set in eflags |
| 5213 | note that old_tss_sel is not used afetr this point */ |
| 5214 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 5215 | old_tss_sel = 0xffff; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5216 | |
| 5217 | if (nseg_desc.type & 8) |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5218 | ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel, |
| 5219 | old_tss_base, &nseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5220 | else |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5221 | ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel, |
| 5222 | old_tss_base, &nseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5223 | |
| 5224 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5225 | u32 eflags = kvm_get_rflags(vcpu); |
| 5226 | kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | if (reason != TASK_SWITCH_IRET) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 5230 | nseg_desc.type |= (1 << 1); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5231 | save_guest_segment_descriptor(vcpu, tss_selector, |
| 5232 | &nseg_desc); |
| 5233 | } |
| 5234 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5235 | kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5236 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); |
| 5237 | tr_seg.type = 11; |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5238 | kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5239 | out: |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5240 | return ret; |
| 5241 | } |
| 5242 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 5243 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5244 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 5245 | struct kvm_sregs *sregs) |
| 5246 | { |
| 5247 | int mmu_reset_needed = 0; |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5248 | int pending_vec, max_bits; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5249 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5250 | |
| 5251 | vcpu_load(vcpu); |
| 5252 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5253 | dt.size = sregs->idt.limit; |
| 5254 | dt.address = sregs->idt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5255 | kvm_x86_ops->set_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5256 | dt.size = sregs->gdt.limit; |
| 5257 | dt.address = sregs->gdt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5258 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 5259 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5260 | vcpu->arch.cr2 = sregs->cr2; |
| 5261 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
Jan Kiszka | dc7e795 | 2009-07-01 20:52:03 +0200 | [diff] [blame] | 5262 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5263 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 5264 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5265 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5266 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5267 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5268 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 5269 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5270 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5271 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 5272 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5273 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5274 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5275 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5276 | if (!is_long_mode(vcpu) && is_pae(vcpu)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5277 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5278 | mmu_reset_needed = 1; |
| 5279 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5280 | |
| 5281 | if (mmu_reset_needed) |
| 5282 | kvm_mmu_reset_context(vcpu); |
| 5283 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5284 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 5285 | pending_vec = find_first_bit( |
| 5286 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 5287 | if (pending_vec < max_bits) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5288 | kvm_queue_interrupt(vcpu, pending_vec, false); |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5289 | pr_debug("Set back pending irq %d\n", pending_vec); |
| 5290 | if (irqchip_in_kernel(vcpu->kvm)) |
| 5291 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5292 | } |
| 5293 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5294 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 5295 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 5296 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 5297 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 5298 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 5299 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5300 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5301 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 5302 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5303 | |
Mikhail Ershov | 5f0269f | 2009-08-03 14:58:25 +0300 | [diff] [blame] | 5304 | update_cr8_intercept(vcpu); |
| 5305 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5306 | /* Older userspace won't unhalt the vcpu on reset. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5307 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5308 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 5309 | !is_protmode(vcpu)) |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5310 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 5311 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5312 | vcpu_put(vcpu); |
| 5313 | |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5317 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 5318 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5319 | { |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5320 | unsigned long rflags; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5321 | int i, r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5322 | |
| 5323 | vcpu_load(vcpu); |
| 5324 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5325 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 5326 | r = -EBUSY; |
| 5327 | if (vcpu->arch.exception.pending) |
| 5328 | goto unlock_out; |
| 5329 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 5330 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5331 | else |
| 5332 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 5333 | } |
| 5334 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5335 | /* |
| 5336 | * Read rflags as long as potentially injected trace flags are still |
| 5337 | * filtered out. |
| 5338 | */ |
| 5339 | rflags = kvm_get_rflags(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5340 | |
| 5341 | vcpu->guest_debug = dbg->control; |
| 5342 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 5343 | vcpu->guest_debug = 0; |
| 5344 | |
| 5345 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5346 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 5347 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
| 5348 | vcpu->arch.switch_db_regs = |
| 5349 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); |
| 5350 | } else { |
| 5351 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 5352 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 5353 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); |
| 5354 | } |
| 5355 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5356 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 5357 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 5358 | get_segment_base(vcpu, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5359 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5360 | /* |
| 5361 | * Trigger an rflags update that will inject or remove the trace |
| 5362 | * flags. |
| 5363 | */ |
| 5364 | kvm_set_rflags(vcpu, rflags); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5365 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5366 | kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 5367 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5368 | r = 0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5369 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5370 | unlock_out: |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5371 | vcpu_put(vcpu); |
| 5372 | |
| 5373 | return r; |
| 5374 | } |
| 5375 | |
| 5376 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5377 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 5378 | * we have asm/x86/processor.h |
| 5379 | */ |
| 5380 | struct fxsave { |
| 5381 | u16 cwd; |
| 5382 | u16 swd; |
| 5383 | u16 twd; |
| 5384 | u16 fop; |
| 5385 | u64 rip; |
| 5386 | u64 rdp; |
| 5387 | u32 mxcsr; |
| 5388 | u32 mxcsr_mask; |
| 5389 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 5390 | #ifdef CONFIG_X86_64 |
| 5391 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 5392 | #else |
| 5393 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 5394 | #endif |
| 5395 | }; |
| 5396 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5397 | /* |
| 5398 | * Translate a guest virtual address to a guest physical address. |
| 5399 | */ |
| 5400 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 5401 | struct kvm_translation *tr) |
| 5402 | { |
| 5403 | unsigned long vaddr = tr->linear_address; |
| 5404 | gpa_t gpa; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5405 | int idx; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5406 | |
| 5407 | vcpu_load(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5408 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5409 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5410 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5411 | tr->physical_address = gpa; |
| 5412 | tr->valid = gpa != UNMAPPED_GVA; |
| 5413 | tr->writeable = 1; |
| 5414 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5415 | vcpu_put(vcpu); |
| 5416 | |
| 5417 | return 0; |
| 5418 | } |
| 5419 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5420 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5421 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5422 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5423 | |
| 5424 | vcpu_load(vcpu); |
| 5425 | |
| 5426 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 5427 | fpu->fcw = fxsave->cwd; |
| 5428 | fpu->fsw = fxsave->swd; |
| 5429 | fpu->ftwx = fxsave->twd; |
| 5430 | fpu->last_opcode = fxsave->fop; |
| 5431 | fpu->last_ip = fxsave->rip; |
| 5432 | fpu->last_dp = fxsave->rdp; |
| 5433 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 5434 | |
| 5435 | vcpu_put(vcpu); |
| 5436 | |
| 5437 | return 0; |
| 5438 | } |
| 5439 | |
| 5440 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5441 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5442 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5443 | |
| 5444 | vcpu_load(vcpu); |
| 5445 | |
| 5446 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 5447 | fxsave->cwd = fpu->fcw; |
| 5448 | fxsave->swd = fpu->fsw; |
| 5449 | fxsave->twd = fpu->ftwx; |
| 5450 | fxsave->fop = fpu->last_opcode; |
| 5451 | fxsave->rip = fpu->last_ip; |
| 5452 | fxsave->rdp = fpu->last_dp; |
| 5453 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 5454 | |
| 5455 | vcpu_put(vcpu); |
| 5456 | |
| 5457 | return 0; |
| 5458 | } |
| 5459 | |
| 5460 | void fx_init(struct kvm_vcpu *vcpu) |
| 5461 | { |
| 5462 | unsigned after_mxcsr_mask; |
| 5463 | |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5464 | /* |
| 5465 | * Touch the fpu the first time in non atomic context as if |
| 5466 | * this is the first fpu instruction the exception handler |
| 5467 | * will fire before the instruction returns and it'll have to |
| 5468 | * allocate ram with GFP_KERNEL. |
| 5469 | */ |
| 5470 | if (!used_math()) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5471 | kvm_fx_save(&vcpu->arch.host_fx_image); |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5472 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5473 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 5474 | preempt_disable(); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5475 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5476 | kvm_fx_finit(); |
| 5477 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5478 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5479 | preempt_enable(); |
| 5480 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5481 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5482 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5483 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
| 5484 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5485 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 5486 | } |
| 5487 | EXPORT_SYMBOL_GPL(fx_init); |
| 5488 | |
| 5489 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 5490 | { |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 5491 | if (vcpu->guest_fpu_loaded) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5492 | return; |
| 5493 | |
| 5494 | vcpu->guest_fpu_loaded = 1; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5495 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5496 | kvm_fx_restore(&vcpu->arch.guest_fx_image); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5497 | trace_kvm_fpu(1); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5498 | } |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5499 | |
| 5500 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 5501 | { |
| 5502 | if (!vcpu->guest_fpu_loaded) |
| 5503 | return; |
| 5504 | |
| 5505 | vcpu->guest_fpu_loaded = 0; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5506 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5507 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 5508 | ++vcpu->stat.fpu_reload; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 5509 | set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5510 | trace_kvm_fpu(0); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5511 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5512 | |
| 5513 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 5514 | { |
Joerg Roedel | 7f1ea20 | 2009-02-25 16:08:31 +0100 | [diff] [blame] | 5515 | if (vcpu->arch.time_page) { |
| 5516 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 5517 | vcpu->arch.time_page = NULL; |
| 5518 | } |
| 5519 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5520 | kvm_x86_ops->vcpu_free(vcpu); |
| 5521 | } |
| 5522 | |
| 5523 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 5524 | unsigned int id) |
| 5525 | { |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5526 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 5527 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5528 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5529 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 5530 | { |
| 5531 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5532 | |
| 5533 | /* We do fxsave: this must be aligned. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5534 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5535 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 5536 | vcpu->arch.mtrr_state.have_fixed = 1; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5537 | vcpu_load(vcpu); |
| 5538 | r = kvm_arch_vcpu_reset(vcpu); |
| 5539 | if (r == 0) |
| 5540 | r = kvm_mmu_setup(vcpu); |
| 5541 | vcpu_put(vcpu); |
| 5542 | if (r < 0) |
| 5543 | goto free_vcpu; |
| 5544 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5545 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5546 | free_vcpu: |
| 5547 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5548 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5549 | } |
| 5550 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 5551 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5552 | { |
| 5553 | vcpu_load(vcpu); |
| 5554 | kvm_mmu_unload(vcpu); |
| 5555 | vcpu_put(vcpu); |
| 5556 | |
| 5557 | kvm_x86_ops->vcpu_free(vcpu); |
| 5558 | } |
| 5559 | |
| 5560 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 5561 | { |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 5562 | vcpu->arch.nmi_pending = false; |
| 5563 | vcpu->arch.nmi_injected = false; |
| 5564 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5565 | vcpu->arch.switch_db_regs = 0; |
| 5566 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
| 5567 | vcpu->arch.dr6 = DR6_FIXED_1; |
| 5568 | vcpu->arch.dr7 = DR7_FIXED_1; |
| 5569 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5570 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 5571 | } |
| 5572 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5573 | int kvm_arch_hardware_enable(void *garbage) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5574 | { |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 5575 | /* |
| 5576 | * Since this may be called from a hotplug notifcation, |
| 5577 | * we can't get the CPU frequency directly. |
| 5578 | */ |
| 5579 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
| 5580 | int cpu = raw_smp_processor_id(); |
| 5581 | per_cpu(cpu_tsc_khz, cpu) = 0; |
| 5582 | } |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 5583 | |
| 5584 | kvm_shared_msr_cpu_online(); |
| 5585 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5586 | return kvm_x86_ops->hardware_enable(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | void kvm_arch_hardware_disable(void *garbage) |
| 5590 | { |
| 5591 | kvm_x86_ops->hardware_disable(garbage); |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 5592 | drop_user_return_notifiers(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | int kvm_arch_hardware_setup(void) |
| 5596 | { |
| 5597 | return kvm_x86_ops->hardware_setup(); |
| 5598 | } |
| 5599 | |
| 5600 | void kvm_arch_hardware_unsetup(void) |
| 5601 | { |
| 5602 | kvm_x86_ops->hardware_unsetup(); |
| 5603 | } |
| 5604 | |
| 5605 | void kvm_arch_check_processor_compat(void *rtn) |
| 5606 | { |
| 5607 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 5608 | } |
| 5609 | |
| 5610 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 5611 | { |
| 5612 | struct page *page; |
| 5613 | struct kvm *kvm; |
| 5614 | int r; |
| 5615 | |
| 5616 | BUG_ON(vcpu->kvm == NULL); |
| 5617 | kvm = vcpu->kvm; |
| 5618 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5619 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5620 | if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu)) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5621 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5622 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5623 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5624 | |
| 5625 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 5626 | if (!page) { |
| 5627 | r = -ENOMEM; |
| 5628 | goto fail; |
| 5629 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5630 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5631 | |
| 5632 | r = kvm_mmu_create(vcpu); |
| 5633 | if (r < 0) |
| 5634 | goto fail_free_pio_data; |
| 5635 | |
| 5636 | if (irqchip_in_kernel(kvm)) { |
| 5637 | r = kvm_create_lapic(vcpu); |
| 5638 | if (r < 0) |
| 5639 | goto fail_mmu_destroy; |
| 5640 | } |
| 5641 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5642 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 5643 | GFP_KERNEL); |
| 5644 | if (!vcpu->arch.mce_banks) { |
| 5645 | r = -ENOMEM; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5646 | goto fail_free_lapic; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5647 | } |
| 5648 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 5649 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5650 | return 0; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5651 | fail_free_lapic: |
| 5652 | kvm_free_lapic(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5653 | fail_mmu_destroy: |
| 5654 | kvm_mmu_destroy(vcpu); |
| 5655 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5656 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5657 | fail: |
| 5658 | return r; |
| 5659 | } |
| 5660 | |
| 5661 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 5662 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5663 | int idx; |
| 5664 | |
Wei Yongjun | 36cb93f | 2010-01-22 14:18:47 +0800 | [diff] [blame] | 5665 | kfree(vcpu->arch.mce_banks); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5666 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5667 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5668 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5669 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5670 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5671 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5672 | |
| 5673 | struct kvm *kvm_arch_create_vm(void) |
| 5674 | { |
| 5675 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 5676 | |
| 5677 | if (!kvm) |
| 5678 | return ERR_PTR(-ENOMEM); |
| 5679 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5680 | kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 5681 | if (!kvm->arch.aliases) { |
| 5682 | kfree(kvm); |
| 5683 | return ERR_PTR(-ENOMEM); |
| 5684 | } |
| 5685 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5686 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 5687 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5688 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 5689 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 5690 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 5691 | |
Marcelo Tosatti | 53f658b3 | 2008-12-11 20:45:05 +0100 | [diff] [blame] | 5692 | rdtscll(kvm->arch.vm_init_tsc); |
| 5693 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5694 | return kvm; |
| 5695 | } |
| 5696 | |
| 5697 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 5698 | { |
| 5699 | vcpu_load(vcpu); |
| 5700 | kvm_mmu_unload(vcpu); |
| 5701 | vcpu_put(vcpu); |
| 5702 | } |
| 5703 | |
| 5704 | static void kvm_free_vcpus(struct kvm *kvm) |
| 5705 | { |
| 5706 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5707 | struct kvm_vcpu *vcpu; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5708 | |
| 5709 | /* |
| 5710 | * Unpin any mmu pages first. |
| 5711 | */ |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5712 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5713 | kvm_unload_vcpu_mmu(vcpu); |
| 5714 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5715 | kvm_arch_vcpu_free(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5716 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5717 | mutex_lock(&kvm->lock); |
| 5718 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 5719 | kvm->vcpus[i] = NULL; |
| 5720 | |
| 5721 | atomic_set(&kvm->online_vcpus, 0); |
| 5722 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5723 | } |
| 5724 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5725 | void kvm_arch_sync_events(struct kvm *kvm) |
| 5726 | { |
Sheng Yang | ba4cef3 | 2009-01-06 10:03:03 +0800 | [diff] [blame] | 5727 | kvm_free_all_assigned_devices(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5728 | } |
| 5729 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5730 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 5731 | { |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 5732 | kvm_iommu_unmap_guest(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 5733 | kvm_free_pit(kvm); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 5734 | kfree(kvm->arch.vpic); |
| 5735 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5736 | kvm_free_vcpus(kvm); |
| 5737 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 5738 | if (kvm->arch.apic_access_page) |
| 5739 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5740 | if (kvm->arch.ept_identity_pagetable) |
| 5741 | put_page(kvm->arch.ept_identity_pagetable); |
Marcelo Tosatti | 6474920 | 2010-01-19 12:45:23 -0200 | [diff] [blame] | 5742 | cleanup_srcu_struct(&kvm->srcu); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5743 | kfree(kvm->arch.aliases); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5744 | kfree(kvm); |
| 5745 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5746 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5747 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 5748 | struct kvm_memory_slot *memslot, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5749 | struct kvm_memory_slot old, |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5750 | struct kvm_userspace_memory_region *mem, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5751 | int user_alloc) |
| 5752 | { |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5753 | int npages = memslot->npages; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5754 | |
| 5755 | /*To keep backward compatibility with older userspace, |
| 5756 | *x86 needs to hanlde !user_alloc case. |
| 5757 | */ |
| 5758 | if (!user_alloc) { |
| 5759 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5760 | unsigned long userspace_addr; |
| 5761 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5762 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5763 | userspace_addr = do_mmap(NULL, 0, |
| 5764 | npages * PAGE_SIZE, |
| 5765 | PROT_READ | PROT_WRITE, |
Avi Kivity | acee3c0 | 2008-08-26 17:22:47 +0300 | [diff] [blame] | 5766 | MAP_PRIVATE | MAP_ANONYMOUS, |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5767 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5768 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5769 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5770 | if (IS_ERR((void *)userspace_addr)) |
| 5771 | return PTR_ERR((void *)userspace_addr); |
| 5772 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5773 | memslot->userspace_addr = userspace_addr; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5777 | |
| 5778 | return 0; |
| 5779 | } |
| 5780 | |
| 5781 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 5782 | struct kvm_userspace_memory_region *mem, |
| 5783 | struct kvm_memory_slot old, |
| 5784 | int user_alloc) |
| 5785 | { |
| 5786 | |
| 5787 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 5788 | |
| 5789 | if (!user_alloc && !old.user_alloc && old.rmap && !npages) { |
| 5790 | int ret; |
| 5791 | |
| 5792 | down_write(¤t->mm->mmap_sem); |
| 5793 | ret = do_munmap(current->mm, old.userspace_addr, |
| 5794 | old.npages * PAGE_SIZE); |
| 5795 | up_write(¤t->mm->mmap_sem); |
| 5796 | if (ret < 0) |
| 5797 | printk(KERN_WARNING |
| 5798 | "kvm_vm_ioctl_set_memory_region: " |
| 5799 | "failed to munmap memory\n"); |
| 5800 | } |
| 5801 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5802 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5803 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5804 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 5805 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 5806 | } |
| 5807 | |
| 5808 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5809 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5810 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5811 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5812 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 5813 | { |
| 5814 | kvm_mmu_zap_all(kvm); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 5815 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5816 | } |
| 5817 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5818 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 5819 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5820 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 5821 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
| 5822 | || vcpu->arch.nmi_pending || |
| 5823 | (kvm_arch_interrupt_allowed(vcpu) && |
| 5824 | kvm_cpu_has_interrupt(vcpu)); |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5825 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5826 | |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5827 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 5828 | { |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5829 | int me; |
| 5830 | int cpu = vcpu->cpu; |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5831 | |
| 5832 | if (waitqueue_active(&vcpu->wq)) { |
| 5833 | wake_up_interruptible(&vcpu->wq); |
| 5834 | ++vcpu->stat.halt_wakeup; |
| 5835 | } |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5836 | |
| 5837 | me = get_cpu(); |
| 5838 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) |
| 5839 | if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests)) |
| 5840 | smp_send_reschedule(cpu); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 5841 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5842 | } |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5843 | |
| 5844 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5845 | { |
| 5846 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 5847 | } |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5848 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5849 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 5850 | { |
| 5851 | unsigned long current_rip = kvm_rip_read(vcpu) + |
| 5852 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 5853 | |
| 5854 | return current_rip == linear_rip; |
| 5855 | } |
| 5856 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 5857 | |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5858 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 5859 | { |
| 5860 | unsigned long rflags; |
| 5861 | |
| 5862 | rflags = kvm_x86_ops->get_rflags(vcpu); |
| 5863 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5864 | rflags &= ~X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5865 | return rflags; |
| 5866 | } |
| 5867 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 5868 | |
| 5869 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 5870 | { |
| 5871 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5872 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5873 | rflags |= X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5874 | kvm_x86_ops->set_rflags(vcpu, rflags); |
| 5875 | } |
| 5876 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 5877 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5878 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
| 5879 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 5880 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 5881 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 5882 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 5883 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 5884 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 5885 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 5886 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 5887 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 5888 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
Joerg Roedel | 2e554e8 | 2010-02-24 18:59:14 +0100 | [diff] [blame] | 5889 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |