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 |
Avi Kivity | 221d059 | 2010-05-23 18:37:00 +0300 | [diff] [blame] | 9 | * Copyright 2010 Red Hat, Inc. and/or its affilates. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 10 | * |
| 11 | * Authors: |
| 12 | * Avi Kivity <avi@qumranet.com> |
| 13 | * Yaniv Kamay <yaniv@qumranet.com> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 14 | * Amit Shah <amit.shah@qumranet.com> |
| 15 | * Ben-Ami Yassour <benami@il.ibm.com> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 16 | * |
| 17 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 18 | * the COPYING file in the top-level directory. |
| 19 | * |
| 20 | */ |
| 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 23 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 24 | #include "mmu.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 25 | #include "i8254.h" |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 26 | #include "tss.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 27 | #include "kvm_cache_regs.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 28 | #include "x86.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 29 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 30 | #include <linux/clocksource.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 31 | #include <linux/interrupt.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 32 | #include <linux/kvm.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/vmalloc.h> |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 35 | #include <linux/module.h> |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 36 | #include <linux/mman.h> |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 37 | #include <linux/highmem.h> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 38 | #include <linux/iommu.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 39 | #include <linux/intel-iommu.h> |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 40 | #include <linux/cpufreq.h> |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 41 | #include <linux/user-return-notifier.h> |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 42 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 44 | #include <linux/perf_event.h> |
Lai Jiangshan | 7bee342 | 2010-06-02 17:06:03 +0800 | [diff] [blame] | 45 | #include <linux/uaccess.h> |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 46 | #include <trace/events/kvm.h> |
Xiao Guangrong | 2ed152a | 2010-03-10 19:00:43 +0800 | [diff] [blame] | 47 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 48 | #define CREATE_TRACE_POINTS |
| 49 | #include "trace.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 50 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 51 | #include <asm/debugreg.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 52 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 53 | #include <asm/desc.h> |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 54 | #include <asm/mtrr.h> |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 55 | #include <asm/mce.h> |
Sheng Yang | 7cf3085 | 2010-05-17 17:08:27 +0800 | [diff] [blame] | 56 | #include <asm/i387.h> |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 57 | #include <asm/xcr.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 58 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 59 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 60 | #define CR0_RESERVED_BITS \ |
| 61 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 62 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 63 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 64 | #define CR4_RESERVED_BITS \ |
| 65 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 66 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 67 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 68 | | X86_CR4_OSXSAVE \ |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 69 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 70 | |
| 71 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 72 | |
| 73 | #define KVM_MAX_MCE_BANKS 32 |
| 74 | #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P |
| 75 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 76 | /* EFER defaults: |
| 77 | * - enable syscall per default because its emulated by KVM |
| 78 | * - enable LME and LMA per default on 64 bit KVM |
| 79 | */ |
| 80 | #ifdef CONFIG_X86_64 |
| 81 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 82 | #else |
| 83 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 84 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 85 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 86 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 87 | #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] | 88 | |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 89 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 90 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 91 | struct kvm_cpuid_entry2 __user *entries); |
| 92 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 93 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 94 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 95 | |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 96 | int ignore_msrs = 0; |
| 97 | module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR); |
| 98 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 99 | #define KVM_NR_SHARED_MSRS 16 |
| 100 | |
| 101 | struct kvm_shared_msrs_global { |
| 102 | int nr; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 103 | u32 msrs[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | struct kvm_shared_msrs { |
| 107 | struct user_return_notifier urn; |
| 108 | bool registered; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 109 | struct kvm_shared_msr_values { |
| 110 | u64 host; |
| 111 | u64 curr; |
| 112 | } values[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; |
| 116 | static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs); |
| 117 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 118 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 119 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 120 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 121 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 122 | { "invlpg", VCPU_STAT(invlpg) }, |
| 123 | { "exits", VCPU_STAT(exits) }, |
| 124 | { "io_exits", VCPU_STAT(io_exits) }, |
| 125 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 126 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 127 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 128 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 129 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 130 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 131 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 132 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 133 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 134 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 135 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 136 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 137 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 138 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 139 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 140 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 141 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 142 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 143 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 144 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 145 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 146 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 147 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 148 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 149 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 150 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 151 | { NULL } |
| 152 | }; |
| 153 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 154 | u64 __read_mostly host_xcr0; |
| 155 | |
| 156 | static inline u32 bit(int bitno) |
| 157 | { |
| 158 | return 1 << (bitno & 31); |
| 159 | } |
| 160 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 161 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 162 | { |
| 163 | unsigned slot; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 164 | struct kvm_shared_msrs *locals |
| 165 | = container_of(urn, struct kvm_shared_msrs, urn); |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 166 | struct kvm_shared_msr_values *values; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 167 | |
| 168 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 169 | values = &locals->values[slot]; |
| 170 | if (values->host != values->curr) { |
| 171 | wrmsrl(shared_msrs_global.msrs[slot], values->host); |
| 172 | values->curr = values->host; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | locals->registered = false; |
| 176 | user_return_notifier_unregister(urn); |
| 177 | } |
| 178 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 179 | static void shared_msr_update(unsigned slot, u32 msr) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 180 | { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 181 | struct kvm_shared_msrs *smsr; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 182 | u64 value; |
| 183 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 184 | smsr = &__get_cpu_var(shared_msrs); |
| 185 | /* only read, and nobody should modify it at this time, |
| 186 | * so don't need lock */ |
| 187 | if (slot >= shared_msrs_global.nr) { |
| 188 | printk(KERN_ERR "kvm: invalid MSR slot!"); |
| 189 | return; |
| 190 | } |
| 191 | rdmsrl_safe(msr, &value); |
| 192 | smsr->values[slot].host = value; |
| 193 | smsr->values[slot].curr = value; |
| 194 | } |
| 195 | |
| 196 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
| 197 | { |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 198 | if (slot >= shared_msrs_global.nr) |
| 199 | shared_msrs_global.nr = slot + 1; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 200 | shared_msrs_global.msrs[slot] = msr; |
| 201 | /* we need ensured the shared_msr_global have been updated */ |
| 202 | smp_wmb(); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 203 | } |
| 204 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); |
| 205 | |
| 206 | static void kvm_shared_msr_cpu_online(void) |
| 207 | { |
| 208 | unsigned i; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 209 | |
| 210 | for (i = 0; i < shared_msrs_global.nr; ++i) |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 211 | shared_msr_update(i, shared_msrs_global.msrs[i]); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 214 | void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 215 | { |
| 216 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 217 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 218 | if (((value ^ smsr->values[slot].curr) & mask) == 0) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 219 | return; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 220 | smsr->values[slot].curr = value; |
| 221 | wrmsrl(shared_msrs_global.msrs[slot], value); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 222 | if (!smsr->registered) { |
| 223 | smsr->urn.on_user_return = kvm_on_user_return; |
| 224 | user_return_notifier_register(&smsr->urn); |
| 225 | smsr->registered = true; |
| 226 | } |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); |
| 229 | |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 230 | static void drop_user_return_notifiers(void *ignore) |
| 231 | { |
| 232 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 233 | |
| 234 | if (smsr->registered) |
| 235 | kvm_on_user_return(&smsr->urn); |
| 236 | } |
| 237 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 238 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 239 | { |
| 240 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 241 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 242 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 243 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 244 | } |
| 245 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 246 | |
| 247 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 248 | { |
| 249 | /* TODO: reserve bits check */ |
| 250 | if (irqchip_in_kernel(vcpu->kvm)) |
| 251 | kvm_lapic_set_base(vcpu, data); |
| 252 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 253 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 254 | } |
| 255 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 256 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 257 | #define EXCPT_BENIGN 0 |
| 258 | #define EXCPT_CONTRIBUTORY 1 |
| 259 | #define EXCPT_PF 2 |
| 260 | |
| 261 | static int exception_class(int vector) |
| 262 | { |
| 263 | switch (vector) { |
| 264 | case PF_VECTOR: |
| 265 | return EXCPT_PF; |
| 266 | case DE_VECTOR: |
| 267 | case TS_VECTOR: |
| 268 | case NP_VECTOR: |
| 269 | case SS_VECTOR: |
| 270 | case GP_VECTOR: |
| 271 | return EXCPT_CONTRIBUTORY; |
| 272 | default: |
| 273 | break; |
| 274 | } |
| 275 | return EXCPT_BENIGN; |
| 276 | } |
| 277 | |
| 278 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 279 | unsigned nr, bool has_error, u32 error_code, |
| 280 | bool reinject) |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 281 | { |
| 282 | u32 prev_nr; |
| 283 | int class1, class2; |
| 284 | |
| 285 | if (!vcpu->arch.exception.pending) { |
| 286 | queue: |
| 287 | vcpu->arch.exception.pending = true; |
| 288 | vcpu->arch.exception.has_error_code = has_error; |
| 289 | vcpu->arch.exception.nr = nr; |
| 290 | vcpu->arch.exception.error_code = error_code; |
Joerg Roedel | 3f0fd29 | 2010-05-05 16:04:41 +0200 | [diff] [blame] | 291 | vcpu->arch.exception.reinject = reinject; |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | /* to check exception */ |
| 296 | prev_nr = vcpu->arch.exception.nr; |
| 297 | if (prev_nr == DF_VECTOR) { |
| 298 | /* triple fault -> shutdown */ |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 299 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | class1 = exception_class(prev_nr); |
| 303 | class2 = exception_class(nr); |
| 304 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 305 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
| 306 | /* generate double fault per SDM Table 5-5 */ |
| 307 | vcpu->arch.exception.pending = true; |
| 308 | vcpu->arch.exception.has_error_code = true; |
| 309 | vcpu->arch.exception.nr = DF_VECTOR; |
| 310 | vcpu->arch.exception.error_code = 0; |
| 311 | } else |
| 312 | /* replace previous exception with a new one in a hope |
| 313 | that instruction re-execution will regenerate lost |
| 314 | exception */ |
| 315 | goto queue; |
| 316 | } |
| 317 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 318 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 319 | { |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 320 | kvm_multiple_exception(vcpu, nr, false, 0, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 321 | } |
| 322 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 323 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 324 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 325 | { |
| 326 | kvm_multiple_exception(vcpu, nr, false, 0, true); |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); |
| 329 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 330 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 331 | u32 error_code) |
| 332 | { |
| 333 | ++vcpu->stat.pf_guest; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 334 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 335 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 336 | } |
| 337 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 338 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 339 | { |
| 340 | vcpu->arch.nmi_pending = 1; |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 343 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 344 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 345 | { |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 346 | kvm_multiple_exception(vcpu, nr, true, error_code, false); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 347 | } |
| 348 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 349 | |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 350 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 351 | { |
| 352 | kvm_multiple_exception(vcpu, nr, true, error_code, true); |
| 353 | } |
| 354 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); |
| 355 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 356 | /* |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 357 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 358 | * a #GP and return false. |
| 359 | */ |
| 360 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 361 | { |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 362 | if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl) |
| 363 | return true; |
| 364 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 365 | return false; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 366 | } |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * Load the pae pdptrs. Return true is they are all valid. |
| 371 | */ |
| 372 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 373 | { |
| 374 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 375 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 376 | int i; |
| 377 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 378 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 379 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 380 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 381 | offset * sizeof(u64), sizeof(pdpte)); |
| 382 | if (ret < 0) { |
| 383 | ret = 0; |
| 384 | goto out; |
| 385 | } |
| 386 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 387 | if (is_present_gpte(pdpte[i]) && |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 388 | (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 389 | ret = 0; |
| 390 | goto out; |
| 391 | } |
| 392 | } |
| 393 | ret = 1; |
| 394 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 395 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 396 | __set_bit(VCPU_EXREG_PDPTR, |
| 397 | (unsigned long *)&vcpu->arch.regs_avail); |
| 398 | __set_bit(VCPU_EXREG_PDPTR, |
| 399 | (unsigned long *)&vcpu->arch.regs_dirty); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 400 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 401 | |
| 402 | return ret; |
| 403 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 404 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 405 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 406 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 407 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 408 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 409 | bool changed = true; |
| 410 | int r; |
| 411 | |
| 412 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 413 | return false; |
| 414 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 415 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 416 | (unsigned long *)&vcpu->arch.regs_avail)) |
| 417 | return true; |
| 418 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 419 | 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] | 420 | if (r < 0) |
| 421 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 422 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 423 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 424 | |
| 425 | return changed; |
| 426 | } |
| 427 | |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 428 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 429 | { |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 430 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
| 431 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP | |
| 432 | X86_CR0_CD | X86_CR0_NW; |
| 433 | |
Avi Kivity | f9a48e6 | 2010-01-06 19:10:22 +0200 | [diff] [blame] | 434 | cr0 |= X86_CR0_ET; |
| 435 | |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 436 | #ifdef CONFIG_X86_64 |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 437 | if (cr0 & 0xffffffff00000000UL) |
| 438 | return 1; |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 439 | #endif |
| 440 | |
| 441 | cr0 &= ~CR0_RESERVED_BITS; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 442 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 443 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
| 444 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 445 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 446 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
| 447 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 448 | |
| 449 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 450 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 451 | if ((vcpu->arch.efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 452 | int cs_db, cs_l; |
| 453 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 454 | if (!is_pae(vcpu)) |
| 455 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 456 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 457 | if (cs_l) |
| 458 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 459 | } else |
| 460 | #endif |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 461 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) |
| 462 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 466 | |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 467 | if ((cr0 ^ old_cr0) & update_bits) |
| 468 | kvm_mmu_reset_context(vcpu); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 469 | return 0; |
| 470 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 471 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 472 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 473 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 474 | { |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 475 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 476 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 477 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 478 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 479 | int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
| 480 | { |
| 481 | u64 xcr0; |
| 482 | |
| 483 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ |
| 484 | if (index != XCR_XFEATURE_ENABLED_MASK) |
| 485 | return 1; |
| 486 | xcr0 = xcr; |
| 487 | if (kvm_x86_ops->get_cpl(vcpu) != 0) |
| 488 | return 1; |
| 489 | if (!(xcr0 & XSTATE_FP)) |
| 490 | return 1; |
| 491 | if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE)) |
| 492 | return 1; |
| 493 | if (xcr0 & ~host_xcr0) |
| 494 | return 1; |
| 495 | vcpu->arch.xcr0 = xcr0; |
| 496 | vcpu->guest_xcr0_loaded = 0; |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
| 501 | { |
| 502 | if (__kvm_set_xcr(vcpu, index, xcr)) { |
| 503 | kvm_inject_gp(vcpu, 0); |
| 504 | return 1; |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(kvm_set_xcr); |
| 509 | |
| 510 | static bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu) |
| 511 | { |
| 512 | struct kvm_cpuid_entry2 *best; |
| 513 | |
| 514 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 515 | return best && (best->ecx & bit(X86_FEATURE_XSAVE)); |
| 516 | } |
| 517 | |
| 518 | static void update_cpuid(struct kvm_vcpu *vcpu) |
| 519 | { |
| 520 | struct kvm_cpuid_entry2 *best; |
| 521 | |
| 522 | best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 523 | if (!best) |
| 524 | return; |
| 525 | |
| 526 | /* Update OSXSAVE bit */ |
| 527 | if (cpu_has_xsave && best->function == 0x1) { |
| 528 | best->ecx &= ~(bit(X86_FEATURE_OSXSAVE)); |
| 529 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) |
| 530 | best->ecx |= bit(X86_FEATURE_OSXSAVE); |
| 531 | } |
| 532 | } |
| 533 | |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 534 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 535 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 536 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 537 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE; |
| 538 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 539 | if (cr4 & CR4_RESERVED_BITS) |
| 540 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 541 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 542 | if (!guest_cpuid_has_xsave(vcpu) && (cr4 & X86_CR4_OSXSAVE)) |
| 543 | return 1; |
| 544 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 545 | if (is_long_mode(vcpu)) { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 546 | if (!(cr4 & X86_CR4_PAE)) |
| 547 | return 1; |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 548 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 549 | && ((cr4 ^ old_cr4) & pdptr_bits) |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 550 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) |
| 551 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 552 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 553 | if (cr4 & X86_CR4_VMXE) |
| 554 | return 1; |
| 555 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 556 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Sheng Yang | 62ad075 | 2010-05-12 16:40:41 +0800 | [diff] [blame] | 557 | |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 558 | if ((cr4 ^ old_cr4) & pdptr_bits) |
| 559 | kvm_mmu_reset_context(vcpu); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 560 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 561 | if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE) |
| 562 | update_cpuid(vcpu); |
| 563 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 564 | return 0; |
| 565 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 566 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 567 | |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 568 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 569 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 570 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 571 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 572 | kvm_mmu_flush_tlb(vcpu); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 573 | return 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 576 | if (is_long_mode(vcpu)) { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 577 | if (cr3 & CR3_L_MODE_RESERVED_BITS) |
| 578 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 579 | } else { |
| 580 | if (is_pae(vcpu)) { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 581 | if (cr3 & CR3_PAE_RESERVED_BITS) |
| 582 | return 1; |
| 583 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) |
| 584 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 585 | } |
| 586 | /* |
| 587 | * We don't check reserved bits in nonpae mode, because |
| 588 | * this isn't enforced, and VMware depends on this. |
| 589 | */ |
| 590 | } |
| 591 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 592 | /* |
| 593 | * Does the new cr3 value map to physical memory? (Note, we |
| 594 | * catch an invalid cr3 even in real-mode, because it would |
| 595 | * cause trouble later on when we turn on paging anyway.) |
| 596 | * |
| 597 | * A real CPU would silently accept an invalid cr3 and would |
| 598 | * attempt to use it - with largely undefined (and often hard |
| 599 | * to debug) behavior on the guest side. |
| 600 | */ |
| 601 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 602 | return 1; |
| 603 | vcpu->arch.cr3 = cr3; |
| 604 | vcpu->arch.mmu.new_cr3(vcpu); |
| 605 | return 0; |
| 606 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 608 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 609 | int __kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 610 | { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 611 | if (cr8 & CR8_RESERVED_BITS) |
| 612 | return 1; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 613 | if (irqchip_in_kernel(vcpu->kvm)) |
| 614 | kvm_lapic_set_tpr(vcpu, cr8); |
| 615 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 616 | vcpu->arch.cr8 = cr8; |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 621 | { |
| 622 | if (__kvm_set_cr8(vcpu, cr8)) |
| 623 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 624 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 625 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 626 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 627 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 628 | { |
| 629 | if (irqchip_in_kernel(vcpu->kvm)) |
| 630 | return kvm_lapic_get_cr8(vcpu); |
| 631 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 632 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 633 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 634 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 635 | |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 636 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 637 | { |
| 638 | switch (dr) { |
| 639 | case 0 ... 3: |
| 640 | vcpu->arch.db[dr] = val; |
| 641 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 642 | vcpu->arch.eff_db[dr] = val; |
| 643 | break; |
| 644 | case 4: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 645 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
| 646 | return 1; /* #UD */ |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 647 | /* fall through */ |
| 648 | case 6: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 649 | if (val & 0xffffffff00000000ULL) |
| 650 | return -1; /* #GP */ |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 651 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1; |
| 652 | break; |
| 653 | case 5: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 654 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
| 655 | return 1; /* #UD */ |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 656 | /* fall through */ |
| 657 | default: /* 7 */ |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 658 | if (val & 0xffffffff00000000ULL) |
| 659 | return -1; /* #GP */ |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 660 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
| 661 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 662 | kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7); |
| 663 | vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK); |
| 664 | } |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 670 | |
| 671 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
| 672 | { |
| 673 | int res; |
| 674 | |
| 675 | res = __kvm_set_dr(vcpu, dr, val); |
| 676 | if (res > 0) |
| 677 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 678 | else if (res < 0) |
| 679 | kvm_inject_gp(vcpu, 0); |
| 680 | |
| 681 | return res; |
| 682 | } |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 683 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
| 684 | |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 685 | static int _kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 686 | { |
| 687 | switch (dr) { |
| 688 | case 0 ... 3: |
| 689 | *val = vcpu->arch.db[dr]; |
| 690 | break; |
| 691 | case 4: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 692 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 693 | return 1; |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 694 | /* fall through */ |
| 695 | case 6: |
| 696 | *val = vcpu->arch.dr6; |
| 697 | break; |
| 698 | case 5: |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 699 | if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 700 | return 1; |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 701 | /* fall through */ |
| 702 | default: /* 7 */ |
| 703 | *val = vcpu->arch.dr7; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 709 | |
| 710 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
| 711 | { |
| 712 | if (_kvm_get_dr(vcpu, dr, val)) { |
| 713 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 714 | return 1; |
| 715 | } |
| 716 | return 0; |
| 717 | } |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 718 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
| 719 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 720 | /* |
| 721 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 722 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 723 | * |
| 724 | * This list is modified at module load time to reflect the |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 725 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
| 726 | * kvm-specific. Those are put in the beginning of the list. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 727 | */ |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 728 | |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 729 | #define KVM_SAVE_MSRS_BEGIN 7 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 730 | static u32 msrs_to_save[] = { |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 731 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 732 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 733 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 734 | HV_X64_MSR_APIC_ASSIST_PAGE, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 735 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 736 | MSR_STAR, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 737 | #ifdef CONFIG_X86_64 |
| 738 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 739 | #endif |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 740 | 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] | 741 | }; |
| 742 | |
| 743 | static unsigned num_msrs_to_save; |
| 744 | |
| 745 | static u32 emulated_msrs[] = { |
| 746 | MSR_IA32_MISC_ENABLE, |
Avi Kivity | 908e75f | 2010-07-07 14:09:38 +0300 | [diff] [blame] | 747 | MSR_IA32_MCG_STATUS, |
| 748 | MSR_IA32_MCG_CTL, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 749 | }; |
| 750 | |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 751 | static int set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 752 | { |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 753 | u64 old_efer = vcpu->arch.efer; |
| 754 | |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 755 | if (efer & efer_reserved_bits) |
| 756 | return 1; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 757 | |
| 758 | if (is_paging(vcpu) |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 759 | && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) |
| 760 | return 1; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 761 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 762 | if (efer & EFER_FFXSR) { |
| 763 | struct kvm_cpuid_entry2 *feat; |
| 764 | |
| 765 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 766 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) |
| 767 | return 1; |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 770 | if (efer & EFER_SVME) { |
| 771 | struct kvm_cpuid_entry2 *feat; |
| 772 | |
| 773 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 774 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) |
| 775 | return 1; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 778 | efer &= ~EFER_LMA; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 779 | efer |= vcpu->arch.efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 780 | |
Sheng Yang | a3d204e | 2010-05-12 16:40:40 +0800 | [diff] [blame] | 781 | kvm_x86_ops->set_efer(vcpu, efer); |
| 782 | |
Avi Kivity | 9645bb56 | 2009-03-31 11:31:54 +0300 | [diff] [blame] | 783 | vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled; |
| 784 | kvm_mmu_reset_context(vcpu); |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 785 | |
Sheng Yang | aad8270 | 2010-05-12 16:40:42 +0800 | [diff] [blame] | 786 | /* Update reserved bits */ |
| 787 | if ((efer ^ old_efer) & EFER_NX) |
| 788 | kvm_mmu_reset_context(vcpu); |
| 789 | |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 790 | return 0; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 793 | void kvm_enable_efer_bits(u64 mask) |
| 794 | { |
| 795 | efer_reserved_bits &= ~mask; |
| 796 | } |
| 797 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 798 | |
| 799 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 800 | /* |
| 801 | * Writes msr value into into the appropriate "register". |
| 802 | * Returns 0 on success, non-0 otherwise. |
| 803 | * Assumes vcpu_load() was already called. |
| 804 | */ |
| 805 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 806 | { |
| 807 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 808 | } |
| 809 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 810 | /* |
| 811 | * Adapt set_msr() to msr_io()'s calling convention |
| 812 | */ |
| 813 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 814 | { |
| 815 | return kvm_set_msr(vcpu, index, *data); |
| 816 | } |
| 817 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 818 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 819 | { |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 820 | int version; |
| 821 | int r; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 822 | struct pvclock_wall_clock wc; |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 823 | struct timespec boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 824 | |
| 825 | if (!wall_clock) |
| 826 | return; |
| 827 | |
Avi Kivity | 9ed3c44 | 2010-05-04 15:00:37 +0300 | [diff] [blame] | 828 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
| 829 | if (r) |
| 830 | return; |
| 831 | |
| 832 | if (version & 1) |
| 833 | ++version; /* first time write, random junk */ |
| 834 | |
| 835 | ++version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 836 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 837 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 838 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 839 | /* |
| 840 | * The guest calculates current wall clock time by adding |
| 841 | * system time (updated by kvm_write_guest_time below) to the |
| 842 | * wall clock specified here. guest system time equals host |
| 843 | * system time for us, thus we must fill in host boot time here. |
| 844 | */ |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 845 | getboottime(&boot); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 846 | |
| 847 | wc.sec = boot.tv_sec; |
| 848 | wc.nsec = boot.tv_nsec; |
| 849 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 850 | |
| 851 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 852 | |
| 853 | version++; |
| 854 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 855 | } |
| 856 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 857 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 858 | { |
| 859 | uint32_t quotient, remainder; |
| 860 | |
| 861 | /* Don't try to replace with do_div(), this one calculates |
| 862 | * "(dividend << 32) / divisor" */ |
| 863 | __asm__ ( "divl %4" |
| 864 | : "=a" (quotient), "=d" (remainder) |
| 865 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 866 | return quotient; |
| 867 | } |
| 868 | |
| 869 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 870 | { |
| 871 | uint64_t nsecs = 1000000000LL; |
| 872 | int32_t shift = 0; |
| 873 | uint64_t tps64; |
| 874 | uint32_t tps32; |
| 875 | |
| 876 | tps64 = tsc_khz * 1000LL; |
| 877 | while (tps64 > nsecs*2) { |
| 878 | tps64 >>= 1; |
| 879 | shift--; |
| 880 | } |
| 881 | |
| 882 | tps32 = (uint32_t)tps64; |
| 883 | while (tps32 <= (uint32_t)nsecs) { |
| 884 | tps32 <<= 1; |
| 885 | shift++; |
| 886 | } |
| 887 | |
| 888 | hv_clock->tsc_shift = shift; |
| 889 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 890 | |
| 891 | 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] | 892 | __func__, tsc_khz, hv_clock->tsc_shift, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 893 | hv_clock->tsc_to_system_mul); |
| 894 | } |
| 895 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 896 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
| 897 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 898 | static inline int kvm_tsc_changes_freq(void) |
| 899 | { |
| 900 | int cpu = get_cpu(); |
| 901 | int ret = !boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && |
| 902 | cpufreq_quick_get(cpu) != 0; |
| 903 | put_cpu(); |
| 904 | return ret; |
| 905 | } |
| 906 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 907 | void kvm_write_tsc(struct kvm_vcpu *vcpu, u64 data) |
| 908 | { |
| 909 | struct kvm *kvm = vcpu->kvm; |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 910 | u64 offset, ns, elapsed; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 911 | unsigned long flags; |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 912 | struct timespec ts; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 913 | |
| 914 | spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
| 915 | offset = data - native_read_tsc(); |
Zachary Amsden | f38e098 | 2010-08-19 22:07:20 -1000 | [diff] [blame] | 916 | ktime_get_ts(&ts); |
| 917 | monotonic_to_bootbased(&ts); |
| 918 | ns = timespec_to_ns(&ts); |
| 919 | elapsed = ns - kvm->arch.last_tsc_nsec; |
| 920 | |
| 921 | /* |
| 922 | * Special case: identical write to TSC within 5 seconds of |
| 923 | * another CPU is interpreted as an attempt to synchronize |
| 924 | * (the 5 seconds is to accomodate host load / swapping). |
| 925 | * |
| 926 | * In that case, for a reliable TSC, we can match TSC offsets, |
| 927 | * or make a best guest using kernel_ns value. |
| 928 | */ |
| 929 | if (data == kvm->arch.last_tsc_write && elapsed < 5ULL * NSEC_PER_SEC) { |
| 930 | if (!check_tsc_unstable()) { |
| 931 | offset = kvm->arch.last_tsc_offset; |
| 932 | pr_debug("kvm: matched tsc offset for %llu\n", data); |
| 933 | } else { |
| 934 | u64 tsc_delta = elapsed * __get_cpu_var(cpu_tsc_khz); |
| 935 | tsc_delta = tsc_delta / USEC_PER_SEC; |
| 936 | offset += tsc_delta; |
| 937 | pr_debug("kvm: adjusted tsc offset by %llu\n", tsc_delta); |
| 938 | } |
| 939 | ns = kvm->arch.last_tsc_nsec; |
| 940 | } |
| 941 | kvm->arch.last_tsc_nsec = ns; |
| 942 | kvm->arch.last_tsc_write = data; |
| 943 | kvm->arch.last_tsc_offset = offset; |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 944 | kvm_x86_ops->write_tsc_offset(vcpu, offset); |
| 945 | spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
| 946 | |
| 947 | /* Reset of TSC must disable overshoot protection below */ |
| 948 | vcpu->arch.hv_clock.tsc_timestamp = 0; |
| 949 | } |
| 950 | EXPORT_SYMBOL_GPL(kvm_write_tsc); |
| 951 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 952 | static int kvm_write_guest_time(struct kvm_vcpu *v) |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 953 | { |
| 954 | struct timespec ts; |
| 955 | unsigned long flags; |
| 956 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 957 | void *shared_kaddr; |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 958 | unsigned long this_tsc_khz; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 959 | |
| 960 | if ((!vcpu->time_page)) |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 961 | return 0; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 962 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 963 | /* Keep irq disabled to prevent changes to the clock */ |
| 964 | local_irq_save(flags); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 965 | 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] | 966 | ktime_get_ts(&ts); |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 967 | monotonic_to_bootbased(&ts); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 968 | this_tsc_khz = __get_cpu_var(cpu_tsc_khz); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 969 | local_irq_restore(flags); |
| 970 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 971 | if (unlikely(this_tsc_khz == 0)) { |
| 972 | kvm_make_request(KVM_REQ_KVMCLOCK_UPDATE, v); |
| 973 | return 1; |
| 974 | } |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 975 | |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame^] | 976 | if (unlikely(vcpu->hw_tsc_khz != this_tsc_khz)) { |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 977 | kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame^] | 978 | vcpu->hw_tsc_khz = this_tsc_khz; |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* With all the info we got, fill in the values */ |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 982 | vcpu->hv_clock.system_time = ts.tv_nsec + |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 983 | (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset; |
| 984 | |
Glauber Costa | 371bcf6 | 2010-05-11 12:17:46 -0400 | [diff] [blame] | 985 | vcpu->hv_clock.flags = 0; |
| 986 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 987 | /* |
| 988 | * The interface expects us to write an even number signaling that the |
| 989 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 990 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 991 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 992 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 993 | |
| 994 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 995 | |
| 996 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 997 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 998 | |
| 999 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 1000 | |
| 1001 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 1002 | return 0; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1005 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) |
| 1006 | { |
| 1007 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 1008 | |
| 1009 | if (!vcpu->time_page) |
| 1010 | return 0; |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1011 | kvm_make_request(KVM_REQ_KVMCLOCK_UPDATE, v); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1012 | return 1; |
| 1013 | } |
| 1014 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1015 | static bool msr_mtrr_valid(unsigned msr) |
| 1016 | { |
| 1017 | switch (msr) { |
| 1018 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 1019 | case MSR_MTRRfix64K_00000: |
| 1020 | case MSR_MTRRfix16K_80000: |
| 1021 | case MSR_MTRRfix16K_A0000: |
| 1022 | case MSR_MTRRfix4K_C0000: |
| 1023 | case MSR_MTRRfix4K_C8000: |
| 1024 | case MSR_MTRRfix4K_D0000: |
| 1025 | case MSR_MTRRfix4K_D8000: |
| 1026 | case MSR_MTRRfix4K_E0000: |
| 1027 | case MSR_MTRRfix4K_E8000: |
| 1028 | case MSR_MTRRfix4K_F0000: |
| 1029 | case MSR_MTRRfix4K_F8000: |
| 1030 | case MSR_MTRRdefType: |
| 1031 | case MSR_IA32_CR_PAT: |
| 1032 | return true; |
| 1033 | case 0x2f8: |
| 1034 | return true; |
| 1035 | } |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 1039 | static bool valid_pat_type(unsigned t) |
| 1040 | { |
| 1041 | return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */ |
| 1042 | } |
| 1043 | |
| 1044 | static bool valid_mtrr_type(unsigned t) |
| 1045 | { |
| 1046 | return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */ |
| 1047 | } |
| 1048 | |
| 1049 | static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1050 | { |
| 1051 | int i; |
| 1052 | |
| 1053 | if (!msr_mtrr_valid(msr)) |
| 1054 | return false; |
| 1055 | |
| 1056 | if (msr == MSR_IA32_CR_PAT) { |
| 1057 | for (i = 0; i < 8; i++) |
| 1058 | if (!valid_pat_type((data >> (i * 8)) & 0xff)) |
| 1059 | return false; |
| 1060 | return true; |
| 1061 | } else if (msr == MSR_MTRRdefType) { |
| 1062 | if (data & ~0xcff) |
| 1063 | return false; |
| 1064 | return valid_mtrr_type(data & 0xff); |
| 1065 | } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { |
| 1066 | for (i = 0; i < 8 ; i++) |
| 1067 | if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) |
| 1068 | return false; |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | /* variable MTRRs */ |
| 1073 | return valid_mtrr_type(data & 0xff); |
| 1074 | } |
| 1075 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1076 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1077 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1078 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 1079 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 1080 | if (!mtrr_valid(vcpu, msr, data)) |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1081 | return 1; |
| 1082 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1083 | if (msr == MSR_MTRRdefType) { |
| 1084 | vcpu->arch.mtrr_state.def_type = data; |
| 1085 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; |
| 1086 | } else if (msr == MSR_MTRRfix64K_00000) |
| 1087 | p[0] = data; |
| 1088 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 1089 | p[1 + msr - MSR_MTRRfix16K_80000] = data; |
| 1090 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 1091 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; |
| 1092 | else if (msr == MSR_IA32_CR_PAT) |
| 1093 | vcpu->arch.pat = data; |
| 1094 | else { /* Variable MTRRs */ |
| 1095 | int idx, is_mtrr_mask; |
| 1096 | u64 *pt; |
| 1097 | |
| 1098 | idx = (msr - 0x200) / 2; |
| 1099 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 1100 | if (!is_mtrr_mask) |
| 1101 | pt = |
| 1102 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 1103 | else |
| 1104 | pt = |
| 1105 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 1106 | *pt = data; |
| 1107 | } |
| 1108 | |
| 1109 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1112 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1113 | static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1114 | { |
| 1115 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 1116 | unsigned bank_num = mcg_cap & 0xff; |
| 1117 | |
| 1118 | switch (msr) { |
| 1119 | case MSR_IA32_MCG_STATUS: |
| 1120 | vcpu->arch.mcg_status = data; |
| 1121 | break; |
| 1122 | case MSR_IA32_MCG_CTL: |
| 1123 | if (!(mcg_cap & MCG_CTL_P)) |
| 1124 | return 1; |
| 1125 | if (data != 0 && data != ~(u64)0) |
| 1126 | return -1; |
| 1127 | vcpu->arch.mcg_ctl = data; |
| 1128 | break; |
| 1129 | default: |
| 1130 | if (msr >= MSR_IA32_MC0_CTL && |
| 1131 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1132 | u32 offset = msr - MSR_IA32_MC0_CTL; |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 1133 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 1134 | * some Linux kernels though clear bit 10 in bank 4 to |
| 1135 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 1136 | * this to avoid an uncatched #GP in the guest |
| 1137 | */ |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1138 | if ((offset & 0x3) == 0 && |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 1139 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1140 | return -1; |
| 1141 | vcpu->arch.mce_banks[offset] = data; |
| 1142 | break; |
| 1143 | } |
| 1144 | return 1; |
| 1145 | } |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1149 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 1150 | { |
| 1151 | struct kvm *kvm = vcpu->kvm; |
| 1152 | int lm = is_long_mode(vcpu); |
| 1153 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 1154 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 1155 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 1156 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 1157 | u32 page_num = data & ~PAGE_MASK; |
| 1158 | u64 page_addr = data & PAGE_MASK; |
| 1159 | u8 *page; |
| 1160 | int r; |
| 1161 | |
| 1162 | r = -E2BIG; |
| 1163 | if (page_num >= blob_size) |
| 1164 | goto out; |
| 1165 | r = -ENOMEM; |
| 1166 | page = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 1167 | if (!page) |
| 1168 | goto out; |
| 1169 | r = -EFAULT; |
| 1170 | if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE)) |
| 1171 | goto out_free; |
| 1172 | if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE)) |
| 1173 | goto out_free; |
| 1174 | r = 0; |
| 1175 | out_free: |
| 1176 | kfree(page); |
| 1177 | out: |
| 1178 | return r; |
| 1179 | } |
| 1180 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1181 | static bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 1182 | { |
| 1183 | return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 1184 | } |
| 1185 | |
| 1186 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 1187 | { |
| 1188 | bool r = false; |
| 1189 | switch (msr) { |
| 1190 | case HV_X64_MSR_GUEST_OS_ID: |
| 1191 | case HV_X64_MSR_HYPERCALL: |
| 1192 | r = true; |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | return r; |
| 1197 | } |
| 1198 | |
| 1199 | static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1200 | { |
| 1201 | struct kvm *kvm = vcpu->kvm; |
| 1202 | |
| 1203 | switch (msr) { |
| 1204 | case HV_X64_MSR_GUEST_OS_ID: |
| 1205 | kvm->arch.hv_guest_os_id = data; |
| 1206 | /* setting guest os id to zero disables hypercall page */ |
| 1207 | if (!kvm->arch.hv_guest_os_id) |
| 1208 | kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 1209 | break; |
| 1210 | case HV_X64_MSR_HYPERCALL: { |
| 1211 | u64 gfn; |
| 1212 | unsigned long addr; |
| 1213 | u8 instructions[4]; |
| 1214 | |
| 1215 | /* if guest os id is not set hypercall should remain disabled */ |
| 1216 | if (!kvm->arch.hv_guest_os_id) |
| 1217 | break; |
| 1218 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1219 | kvm->arch.hv_hypercall = data; |
| 1220 | break; |
| 1221 | } |
| 1222 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 1223 | addr = gfn_to_hva(kvm, gfn); |
| 1224 | if (kvm_is_error_hva(addr)) |
| 1225 | return 1; |
| 1226 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 1227 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 1228 | if (copy_to_user((void __user *)addr, instructions, 4)) |
| 1229 | return 1; |
| 1230 | kvm->arch.hv_hypercall = data; |
| 1231 | break; |
| 1232 | } |
| 1233 | default: |
| 1234 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1235 | "data 0x%llx\n", msr, data); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1242 | { |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1243 | switch (msr) { |
| 1244 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 1245 | unsigned long addr; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1246 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1247 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 1248 | vcpu->arch.hv_vapic = data; |
| 1249 | break; |
| 1250 | } |
| 1251 | addr = gfn_to_hva(vcpu->kvm, data >> |
| 1252 | HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT); |
| 1253 | if (kvm_is_error_hva(addr)) |
| 1254 | return 1; |
| 1255 | if (clear_user((void __user *)addr, PAGE_SIZE)) |
| 1256 | return 1; |
| 1257 | vcpu->arch.hv_vapic = data; |
| 1258 | break; |
| 1259 | } |
| 1260 | case HV_X64_MSR_EOI: |
| 1261 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1262 | case HV_X64_MSR_ICR: |
| 1263 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1264 | case HV_X64_MSR_TPR: |
| 1265 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
| 1266 | default: |
| 1267 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1268 | "data 0x%llx\n", msr, data); |
| 1269 | return 1; |
| 1270 | } |
| 1271 | |
| 1272 | return 0; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1275 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1276 | { |
| 1277 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1278 | case MSR_EFER: |
Roedel, Joerg | b69e8ca | 2010-05-06 11:38:43 +0200 | [diff] [blame] | 1279 | return set_efer(vcpu, data); |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1280 | case MSR_K7_HWCR: |
| 1281 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
Joerg Roedel | 8249402 | 2010-02-24 18:59:16 +0100 | [diff] [blame] | 1282 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1283 | if (data != 0) { |
| 1284 | pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 1285 | data); |
| 1286 | return 1; |
| 1287 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1288 | break; |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1289 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 1290 | if (data != 0) { |
| 1291 | pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 1292 | "0x%llx\n", data); |
| 1293 | return 1; |
| 1294 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1295 | break; |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1296 | case MSR_AMD64_NB_CFG: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 1297 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1298 | case MSR_IA32_DEBUGCTLMSR: |
| 1299 | if (!data) { |
| 1300 | /* We support the non-activated case already */ |
| 1301 | break; |
| 1302 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 1303 | /* Values other than LBR and BTF are vendor-specific, |
| 1304 | thus reserved and should throw a #GP */ |
| 1305 | return 1; |
| 1306 | } |
| 1307 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 1308 | __func__, data); |
| 1309 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1310 | case MSR_IA32_UCODE_REV: |
| 1311 | case MSR_IA32_UCODE_WRITE: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1312 | case MSR_VM_HSAVE_PA: |
Andre Przywara | 6098ca9 | 2009-07-03 16:00:14 +0200 | [diff] [blame] | 1313 | case MSR_AMD64_PATCH_LOADER: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1314 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1315 | case 0x200 ... 0x2ff: |
| 1316 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1317 | case MSR_IA32_APICBASE: |
| 1318 | kvm_set_apic_base(vcpu, data); |
| 1319 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1320 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1321 | return kvm_x2apic_msr_write(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1322 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1323 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1324 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1325 | case MSR_KVM_WALL_CLOCK_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1326 | case MSR_KVM_WALL_CLOCK: |
| 1327 | vcpu->kvm->arch.wall_clock = data; |
| 1328 | kvm_write_wall_clock(vcpu->kvm, data); |
| 1329 | break; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1330 | case MSR_KVM_SYSTEM_TIME_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1331 | case MSR_KVM_SYSTEM_TIME: { |
| 1332 | if (vcpu->arch.time_page) { |
| 1333 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 1334 | vcpu->arch.time_page = NULL; |
| 1335 | } |
| 1336 | |
| 1337 | vcpu->arch.time = data; |
| 1338 | |
| 1339 | /* we verify if the enable bit is set... */ |
| 1340 | if (!(data & 1)) |
| 1341 | break; |
| 1342 | |
| 1343 | /* ...but clean it before doing the actual write */ |
| 1344 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 1345 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1346 | vcpu->arch.time_page = |
| 1347 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1348 | |
| 1349 | if (is_error_page(vcpu->arch.time_page)) { |
| 1350 | kvm_release_page_clean(vcpu->arch.time_page); |
| 1351 | vcpu->arch.time_page = NULL; |
| 1352 | } |
| 1353 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1354 | kvm_request_guest_time_update(vcpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1355 | break; |
| 1356 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1357 | case MSR_IA32_MCG_CTL: |
| 1358 | case MSR_IA32_MCG_STATUS: |
| 1359 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1360 | return set_msr_mce(vcpu, msr, data); |
Andre Przywara | 71db602 | 2009-06-12 22:01:29 +0200 | [diff] [blame] | 1361 | |
| 1362 | /* Performance counters are not protected by a CPUID bit, |
| 1363 | * so we should check all of them in the generic path for the sake of |
| 1364 | * cross vendor migration. |
| 1365 | * Writing a zero into the event select MSRs disables them, |
| 1366 | * which we perfectly emulate ;-). Any other value should be at least |
| 1367 | * reported, some guests depend on them. |
| 1368 | */ |
| 1369 | case MSR_P6_EVNTSEL0: |
| 1370 | case MSR_P6_EVNTSEL1: |
| 1371 | case MSR_K7_EVNTSEL0: |
| 1372 | case MSR_K7_EVNTSEL1: |
| 1373 | case MSR_K7_EVNTSEL2: |
| 1374 | case MSR_K7_EVNTSEL3: |
| 1375 | if (data != 0) |
| 1376 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1377 | "0x%x data 0x%llx\n", msr, data); |
| 1378 | break; |
| 1379 | /* at least RHEL 4 unconditionally writes to the perfctr registers, |
| 1380 | * so we ignore writes to make it happy. |
| 1381 | */ |
| 1382 | case MSR_P6_PERFCTR0: |
| 1383 | case MSR_P6_PERFCTR1: |
| 1384 | case MSR_K7_PERFCTR0: |
| 1385 | case MSR_K7_PERFCTR1: |
| 1386 | case MSR_K7_PERFCTR2: |
| 1387 | case MSR_K7_PERFCTR3: |
| 1388 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1389 | "0x%x data 0x%llx\n", msr, data); |
| 1390 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1391 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1392 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1393 | int r; |
| 1394 | mutex_lock(&vcpu->kvm->lock); |
| 1395 | r = set_msr_hyperv_pw(vcpu, msr, data); |
| 1396 | mutex_unlock(&vcpu->kvm->lock); |
| 1397 | return r; |
| 1398 | } else |
| 1399 | return set_msr_hyperv(vcpu, msr, data); |
| 1400 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1401 | default: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1402 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 1403 | return xen_hvm_config(vcpu, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1404 | if (!ignore_msrs) { |
| 1405 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", |
| 1406 | msr, data); |
| 1407 | return 1; |
| 1408 | } else { |
| 1409 | pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", |
| 1410 | msr, data); |
| 1411 | break; |
| 1412 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1417 | |
| 1418 | |
| 1419 | /* |
| 1420 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1421 | * Returns 0 on success, non-0 otherwise. |
| 1422 | * Assumes vcpu_load() was already called. |
| 1423 | */ |
| 1424 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1425 | { |
| 1426 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 1427 | } |
| 1428 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1429 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1430 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1431 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 1432 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1433 | if (!msr_mtrr_valid(msr)) |
| 1434 | return 1; |
| 1435 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1436 | if (msr == MSR_MTRRdefType) |
| 1437 | *pdata = vcpu->arch.mtrr_state.def_type + |
| 1438 | (vcpu->arch.mtrr_state.enabled << 10); |
| 1439 | else if (msr == MSR_MTRRfix64K_00000) |
| 1440 | *pdata = p[0]; |
| 1441 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 1442 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; |
| 1443 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 1444 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; |
| 1445 | else if (msr == MSR_IA32_CR_PAT) |
| 1446 | *pdata = vcpu->arch.pat; |
| 1447 | else { /* Variable MTRRs */ |
| 1448 | int idx, is_mtrr_mask; |
| 1449 | u64 *pt; |
| 1450 | |
| 1451 | idx = (msr - 0x200) / 2; |
| 1452 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 1453 | if (!is_mtrr_mask) |
| 1454 | pt = |
| 1455 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 1456 | else |
| 1457 | pt = |
| 1458 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 1459 | *pdata = *pt; |
| 1460 | } |
| 1461 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1465 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1466 | { |
| 1467 | u64 data; |
| 1468 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 1469 | unsigned bank_num = mcg_cap & 0xff; |
| 1470 | |
| 1471 | switch (msr) { |
| 1472 | case MSR_IA32_P5_MC_ADDR: |
| 1473 | case MSR_IA32_P5_MC_TYPE: |
| 1474 | data = 0; |
| 1475 | break; |
| 1476 | case MSR_IA32_MCG_CAP: |
| 1477 | data = vcpu->arch.mcg_cap; |
| 1478 | break; |
| 1479 | case MSR_IA32_MCG_CTL: |
| 1480 | if (!(mcg_cap & MCG_CTL_P)) |
| 1481 | return 1; |
| 1482 | data = vcpu->arch.mcg_ctl; |
| 1483 | break; |
| 1484 | case MSR_IA32_MCG_STATUS: |
| 1485 | data = vcpu->arch.mcg_status; |
| 1486 | break; |
| 1487 | default: |
| 1488 | if (msr >= MSR_IA32_MC0_CTL && |
| 1489 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1490 | u32 offset = msr - MSR_IA32_MC0_CTL; |
| 1491 | data = vcpu->arch.mce_banks[offset]; |
| 1492 | break; |
| 1493 | } |
| 1494 | return 1; |
| 1495 | } |
| 1496 | *pdata = data; |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1500 | static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1501 | { |
| 1502 | u64 data = 0; |
| 1503 | struct kvm *kvm = vcpu->kvm; |
| 1504 | |
| 1505 | switch (msr) { |
| 1506 | case HV_X64_MSR_GUEST_OS_ID: |
| 1507 | data = kvm->arch.hv_guest_os_id; |
| 1508 | break; |
| 1509 | case HV_X64_MSR_HYPERCALL: |
| 1510 | data = kvm->arch.hv_hypercall; |
| 1511 | break; |
| 1512 | default: |
| 1513 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1514 | return 1; |
| 1515 | } |
| 1516 | |
| 1517 | *pdata = data; |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1522 | { |
| 1523 | u64 data = 0; |
| 1524 | |
| 1525 | switch (msr) { |
| 1526 | case HV_X64_MSR_VP_INDEX: { |
| 1527 | int r; |
| 1528 | struct kvm_vcpu *v; |
| 1529 | kvm_for_each_vcpu(r, v, vcpu->kvm) |
| 1530 | if (v == vcpu) |
| 1531 | data = r; |
| 1532 | break; |
| 1533 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1534 | case HV_X64_MSR_EOI: |
| 1535 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1536 | case HV_X64_MSR_ICR: |
| 1537 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1538 | case HV_X64_MSR_TPR: |
| 1539 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1540 | default: |
| 1541 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1542 | return 1; |
| 1543 | } |
| 1544 | *pdata = data; |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1548 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1549 | { |
| 1550 | u64 data; |
| 1551 | |
| 1552 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1553 | case MSR_IA32_PLATFORM_ID: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1554 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1555 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1556 | case MSR_IA32_DEBUGCTLMSR: |
| 1557 | case MSR_IA32_LASTBRANCHFROMIP: |
| 1558 | case MSR_IA32_LASTBRANCHTOIP: |
| 1559 | case MSR_IA32_LASTINTFROMIP: |
| 1560 | case MSR_IA32_LASTINTTOIP: |
Jaswinder Singh Rajput | 60af2ec | 2009-05-14 11:00:10 +0530 | [diff] [blame] | 1561 | case MSR_K8_SYSCFG: |
| 1562 | case MSR_K7_HWCR: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1563 | case MSR_VM_HSAVE_PA: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1564 | case MSR_P6_PERFCTR0: |
| 1565 | case MSR_P6_PERFCTR1: |
Amit Shah | 7fe29e0 | 2009-03-20 12:39:00 +0530 | [diff] [blame] | 1566 | case MSR_P6_EVNTSEL0: |
| 1567 | case MSR_P6_EVNTSEL1: |
Amit Shah | 9e69962 | 2009-06-15 13:25:34 +0530 | [diff] [blame] | 1568 | case MSR_K7_EVNTSEL0: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1569 | case MSR_K7_PERFCTR0: |
Andre Przywara | 1fdbd48 | 2009-06-24 12:44:34 +0200 | [diff] [blame] | 1570 | case MSR_K8_INT_PENDING_MSG: |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1571 | case MSR_AMD64_NB_CFG: |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1572 | case MSR_FAM10H_MMIO_CONF_BASE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1573 | data = 0; |
| 1574 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1575 | case MSR_MTRRcap: |
| 1576 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 1577 | break; |
| 1578 | case 0x200 ... 0x2ff: |
| 1579 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1580 | case 0xcd: /* fsb frequency */ |
| 1581 | data = 3; |
| 1582 | break; |
| 1583 | case MSR_IA32_APICBASE: |
| 1584 | data = kvm_get_apic_base(vcpu); |
| 1585 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1586 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1587 | return kvm_x2apic_msr_read(vcpu, msr, pdata); |
| 1588 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1589 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1590 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1591 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 1592 | case MSR_IA32_PERF_STATUS: |
| 1593 | /* TSC increment by tick */ |
| 1594 | data = 1000ULL; |
| 1595 | /* CPU multiplier */ |
| 1596 | data |= (((uint64_t)4ULL) << 40); |
| 1597 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1598 | case MSR_EFER: |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1599 | data = vcpu->arch.efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1600 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1601 | case MSR_KVM_WALL_CLOCK: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1602 | case MSR_KVM_WALL_CLOCK_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1603 | data = vcpu->kvm->arch.wall_clock; |
| 1604 | break; |
| 1605 | case MSR_KVM_SYSTEM_TIME: |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 1606 | case MSR_KVM_SYSTEM_TIME_NEW: |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1607 | data = vcpu->arch.time; |
| 1608 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1609 | case MSR_IA32_P5_MC_ADDR: |
| 1610 | case MSR_IA32_P5_MC_TYPE: |
| 1611 | case MSR_IA32_MCG_CAP: |
| 1612 | case MSR_IA32_MCG_CTL: |
| 1613 | case MSR_IA32_MCG_STATUS: |
| 1614 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1615 | return get_msr_mce(vcpu, msr, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1616 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1617 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1618 | int r; |
| 1619 | mutex_lock(&vcpu->kvm->lock); |
| 1620 | r = get_msr_hyperv_pw(vcpu, msr, pdata); |
| 1621 | mutex_unlock(&vcpu->kvm->lock); |
| 1622 | return r; |
| 1623 | } else |
| 1624 | return get_msr_hyperv(vcpu, msr, pdata); |
| 1625 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1626 | default: |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1627 | if (!ignore_msrs) { |
| 1628 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 1629 | return 1; |
| 1630 | } else { |
| 1631 | pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr); |
| 1632 | data = 0; |
| 1633 | } |
| 1634 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1635 | } |
| 1636 | *pdata = data; |
| 1637 | return 0; |
| 1638 | } |
| 1639 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1640 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1641 | /* |
| 1642 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1643 | * |
| 1644 | * @return number of msrs set successfully. |
| 1645 | */ |
| 1646 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 1647 | struct kvm_msr_entry *entries, |
| 1648 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1649 | unsigned index, u64 *data)) |
| 1650 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1651 | int i, idx; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1652 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1653 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1654 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1655 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1656 | break; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1657 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1658 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1659 | return i; |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1664 | * |
| 1665 | * @return number of msrs set successfully. |
| 1666 | */ |
| 1667 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 1668 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1669 | unsigned index, u64 *data), |
| 1670 | int writeback) |
| 1671 | { |
| 1672 | struct kvm_msrs msrs; |
| 1673 | struct kvm_msr_entry *entries; |
| 1674 | int r, n; |
| 1675 | unsigned size; |
| 1676 | |
| 1677 | r = -EFAULT; |
| 1678 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1679 | goto out; |
| 1680 | |
| 1681 | r = -E2BIG; |
| 1682 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1683 | goto out; |
| 1684 | |
| 1685 | r = -ENOMEM; |
| 1686 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
Avi Kivity | 7a73c02 | 2010-07-22 23:24:52 +0300 | [diff] [blame] | 1687 | entries = kmalloc(size, GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1688 | if (!entries) |
| 1689 | goto out; |
| 1690 | |
| 1691 | r = -EFAULT; |
| 1692 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1693 | goto out_free; |
| 1694 | |
| 1695 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 1696 | if (r < 0) |
| 1697 | goto out_free; |
| 1698 | |
| 1699 | r = -EFAULT; |
| 1700 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1701 | goto out_free; |
| 1702 | |
| 1703 | r = n; |
| 1704 | |
| 1705 | out_free: |
Avi Kivity | 7a73c02 | 2010-07-22 23:24:52 +0300 | [diff] [blame] | 1706 | kfree(entries); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1707 | out: |
| 1708 | return r; |
| 1709 | } |
| 1710 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1711 | int kvm_dev_ioctl_check_extension(long ext) |
| 1712 | { |
| 1713 | int r; |
| 1714 | |
| 1715 | switch (ext) { |
| 1716 | case KVM_CAP_IRQCHIP: |
| 1717 | case KVM_CAP_HLT: |
| 1718 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1719 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1720 | case KVM_CAP_EXT_CPUID: |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1721 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1722 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1723 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1724 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 1725 | case KVM_CAP_SYNC_MMU: |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 1726 | case KVM_CAP_REINJECT_CONTROL: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 1727 | case KVM_CAP_IRQ_INJECT_STATUS: |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 1728 | case KVM_CAP_ASSIGN_DEV_IRQ: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1729 | case KVM_CAP_IRQFD: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 1730 | case KVM_CAP_IOEVENTFD: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 1731 | case KVM_CAP_PIT2: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 1732 | case KVM_CAP_PIT_STATE2: |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 1733 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1734 | case KVM_CAP_XEN_HVM: |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 1735 | case KVM_CAP_ADJUST_CLOCK: |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 1736 | case KVM_CAP_VCPU_EVENTS: |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1737 | case KVM_CAP_HYPERV: |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1738 | case KVM_CAP_HYPERV_VAPIC: |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 1739 | case KVM_CAP_HYPERV_SPIN: |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 1740 | case KVM_CAP_PCI_SEGMENT: |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 1741 | case KVM_CAP_DEBUGREGS: |
Jan Kiszka | d2be165 | 2010-02-23 17:47:57 +0100 | [diff] [blame] | 1742 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 1743 | case KVM_CAP_XSAVE: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1744 | r = 1; |
| 1745 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 1746 | case KVM_CAP_COALESCED_MMIO: |
| 1747 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1748 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1749 | case KVM_CAP_VAPIC: |
| 1750 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 1751 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 1752 | case KVM_CAP_NR_VCPUS: |
| 1753 | r = KVM_MAX_VCPUS; |
| 1754 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 1755 | case KVM_CAP_NR_MEMSLOTS: |
| 1756 | r = KVM_MEMORY_SLOTS; |
| 1757 | break; |
Marcelo Tosatti | a68a6a7 | 2009-10-01 19:28:39 -0300 | [diff] [blame] | 1758 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 1759 | r = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 1760 | break; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1761 | case KVM_CAP_IOMMU: |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 1762 | r = iommu_found(); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1763 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1764 | case KVM_CAP_MCE: |
| 1765 | r = KVM_MAX_MCE_BANKS; |
| 1766 | break; |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 1767 | case KVM_CAP_XCRS: |
| 1768 | r = cpu_has_xsave; |
| 1769 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1770 | default: |
| 1771 | r = 0; |
| 1772 | break; |
| 1773 | } |
| 1774 | return r; |
| 1775 | |
| 1776 | } |
| 1777 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1778 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1779 | unsigned int ioctl, unsigned long arg) |
| 1780 | { |
| 1781 | void __user *argp = (void __user *)arg; |
| 1782 | long r; |
| 1783 | |
| 1784 | switch (ioctl) { |
| 1785 | case KVM_GET_MSR_INDEX_LIST: { |
| 1786 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1787 | struct kvm_msr_list msr_list; |
| 1788 | unsigned n; |
| 1789 | |
| 1790 | r = -EFAULT; |
| 1791 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1792 | goto out; |
| 1793 | n = msr_list.nmsrs; |
| 1794 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1795 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1796 | goto out; |
| 1797 | r = -E2BIG; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1798 | if (n < msr_list.nmsrs) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1799 | goto out; |
| 1800 | r = -EFAULT; |
| 1801 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1802 | num_msrs_to_save * sizeof(u32))) |
| 1803 | goto out; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1804 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1805 | &emulated_msrs, |
| 1806 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1807 | goto out; |
| 1808 | r = 0; |
| 1809 | break; |
| 1810 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1811 | case KVM_GET_SUPPORTED_CPUID: { |
| 1812 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1813 | struct kvm_cpuid2 cpuid; |
| 1814 | |
| 1815 | r = -EFAULT; |
| 1816 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1817 | goto out; |
| 1818 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1819 | cpuid_arg->entries); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1820 | if (r) |
| 1821 | goto out; |
| 1822 | |
| 1823 | r = -EFAULT; |
| 1824 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1825 | goto out; |
| 1826 | r = 0; |
| 1827 | break; |
| 1828 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1829 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
| 1830 | u64 mce_cap; |
| 1831 | |
| 1832 | mce_cap = KVM_MCE_CAP_SUPPORTED; |
| 1833 | r = -EFAULT; |
| 1834 | if (copy_to_user(argp, &mce_cap, sizeof mce_cap)) |
| 1835 | goto out; |
| 1836 | r = 0; |
| 1837 | break; |
| 1838 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1839 | default: |
| 1840 | r = -EINVAL; |
| 1841 | } |
| 1842 | out: |
| 1843 | return r; |
| 1844 | } |
| 1845 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1846 | static void wbinvd_ipi(void *garbage) |
| 1847 | { |
| 1848 | wbinvd(); |
| 1849 | } |
| 1850 | |
| 1851 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 1852 | { |
| 1853 | return vcpu->kvm->arch.iommu_domain && |
| 1854 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY); |
| 1855 | } |
| 1856 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1857 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1858 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1859 | /* Address WBINVD may be executed by guest */ |
| 1860 | if (need_emulate_wbinvd(vcpu)) { |
| 1861 | if (kvm_x86_ops->has_wbinvd_exit()) |
| 1862 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
| 1863 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) |
| 1864 | smp_call_function_single(vcpu->cpu, |
| 1865 | wbinvd_ipi, NULL, 1); |
| 1866 | } |
| 1867 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1868 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame^] | 1869 | if (unlikely(vcpu->cpu != cpu)) { |
| 1870 | /* Make sure TSC doesn't go backwards */ |
| 1871 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
| 1872 | native_read_tsc() - vcpu->arch.last_host_tsc; |
| 1873 | if (tsc_delta < 0) |
| 1874 | mark_tsc_unstable("KVM discovered backwards TSC"); |
| 1875 | if (check_tsc_unstable()) |
| 1876 | kvm_x86_ops->adjust_tsc_offset(vcpu, -tsc_delta); |
| 1877 | kvm_migrate_timers(vcpu); |
| 1878 | vcpu->cpu = cpu; |
| 1879 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1883 | { |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1884 | kvm_x86_ops->vcpu_put(vcpu); |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 1885 | kvm_put_guest_fpu(vcpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame^] | 1886 | vcpu->arch.last_host_tsc = native_read_tsc(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1889 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1890 | { |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1891 | unsigned long long efer = 0; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1892 | |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1893 | rdmsrl_safe(MSR_EFER, &efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1894 | return efer & EFER_NX; |
| 1895 | } |
| 1896 | |
| 1897 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1898 | { |
| 1899 | int i; |
| 1900 | struct kvm_cpuid_entry2 *e, *entry; |
| 1901 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1902 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1903 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1904 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1905 | if (e->function == 0x80000001) { |
| 1906 | entry = e; |
| 1907 | break; |
| 1908 | } |
| 1909 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1910 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1911 | entry->edx &= ~(1 << 20); |
| 1912 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1913 | } |
| 1914 | } |
| 1915 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1916 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1917 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1918 | struct kvm_cpuid *cpuid, |
| 1919 | struct kvm_cpuid_entry __user *entries) |
| 1920 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1921 | int r, i; |
| 1922 | struct kvm_cpuid_entry *cpuid_entries; |
| 1923 | |
| 1924 | r = -E2BIG; |
| 1925 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1926 | goto out; |
| 1927 | r = -ENOMEM; |
| 1928 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1929 | if (!cpuid_entries) |
| 1930 | goto out; |
| 1931 | r = -EFAULT; |
| 1932 | if (copy_from_user(cpuid_entries, entries, |
| 1933 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1934 | goto out_free; |
| 1935 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1936 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1937 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1938 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1939 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1940 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1941 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1942 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1943 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1944 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1945 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1946 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1947 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1948 | cpuid_fix_nx_cap(vcpu); |
| 1949 | r = 0; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1950 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1951 | kvm_x86_ops->cpuid_update(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 1952 | update_cpuid(vcpu); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1953 | |
| 1954 | out_free: |
| 1955 | vfree(cpuid_entries); |
| 1956 | out: |
| 1957 | return r; |
| 1958 | } |
| 1959 | |
| 1960 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1961 | struct kvm_cpuid2 *cpuid, |
| 1962 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1963 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1964 | int r; |
| 1965 | |
| 1966 | r = -E2BIG; |
| 1967 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1968 | goto out; |
| 1969 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1970 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1971 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1972 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1973 | vcpu->arch.cpuid_nent = cpuid->nent; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1974 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1975 | kvm_x86_ops->cpuid_update(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 1976 | update_cpuid(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1977 | return 0; |
| 1978 | |
| 1979 | out: |
| 1980 | return r; |
| 1981 | } |
| 1982 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1983 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1984 | struct kvm_cpuid2 *cpuid, |
| 1985 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1986 | { |
| 1987 | int r; |
| 1988 | |
| 1989 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1990 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1991 | goto out; |
| 1992 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1993 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1994 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1995 | goto out; |
| 1996 | return 0; |
| 1997 | |
| 1998 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1999 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2000 | return r; |
| 2001 | } |
| 2002 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2003 | 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] | 2004 | u32 index) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2005 | { |
| 2006 | entry->function = function; |
| 2007 | entry->index = index; |
| 2008 | cpuid_count(entry->function, entry->index, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2009 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2010 | entry->flags = 0; |
| 2011 | } |
| 2012 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2013 | #define F(x) bit(X86_FEATURE_##x) |
| 2014 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2015 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 2016 | u32 index, int *nent, int maxnent) |
| 2017 | { |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2018 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2019 | #ifdef CONFIG_X86_64 |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 2020 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) |
| 2021 | ? F(GBPAGES) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2022 | unsigned f_lm = F(LM); |
| 2023 | #else |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 2024 | unsigned f_gbpages = 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2025 | unsigned f_lm = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2026 | #endif |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2027 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2028 | |
| 2029 | /* cpuid 1.edx */ |
| 2030 | const u32 kvm_supported_word0_x86_features = |
| 2031 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 2032 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 2033 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 2034 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 2035 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | |
| 2036 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 2037 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 2038 | 0 /* HTT, TM, Reserved, PBE */; |
| 2039 | /* cpuid 0x80000001.edx */ |
| 2040 | const u32 kvm_supported_word1_x86_features = |
| 2041 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 2042 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 2043 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 2044 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 2045 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
| 2046 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2047 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2048 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); |
| 2049 | /* cpuid 1.ecx */ |
| 2050 | const u32 kvm_supported_word4_x86_features = |
Sheng Yang | 6c3f604 | 2010-06-22 13:49:21 +0800 | [diff] [blame] | 2051 | F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ | |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 2052 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 2053 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
| 2054 | 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ | |
| 2055 | 0 /* Reserved, DCA */ | F(XMM4_1) | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 2056 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
Sheng Yang | 6c3f604 | 2010-06-22 13:49:21 +0800 | [diff] [blame] | 2057 | 0 /* Reserved, AES */ | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX); |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2058 | /* cpuid 0x80000001.ecx */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2059 | const u32 kvm_supported_word6_x86_features = |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2060 | F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ | |
| 2061 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
| 2062 | F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) | |
| 2063 | 0 /* SKINIT */ | 0 /* WDT */; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2064 | |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2065 | /* all calls to cpuid_count() should be made on the same cpu */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2066 | get_cpu(); |
| 2067 | do_cpuid_1_ent(entry, function, index); |
| 2068 | ++*nent; |
| 2069 | |
| 2070 | switch (function) { |
| 2071 | case 0: |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 2072 | entry->eax = min(entry->eax, (u32)0xd); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2073 | break; |
| 2074 | case 1: |
| 2075 | entry->edx &= kvm_supported_word0_x86_features; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2076 | entry->ecx &= kvm_supported_word4_x86_features; |
Gleb Natapov | 0d1de2d9 | 2009-07-12 16:10:55 +0300 | [diff] [blame] | 2077 | /* we support x2apic emulation even if host does not support |
| 2078 | * it since we emulate x2apic in software */ |
| 2079 | entry->ecx |= F(X2APIC); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2080 | break; |
| 2081 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 2082 | * may return different values. This forces us to get_cpu() before |
| 2083 | * issuing the first command, and also to emulate this annoying behavior |
| 2084 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 2085 | case 2: { |
| 2086 | int t, times = entry->eax & 0xff; |
| 2087 | |
| 2088 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 2089 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2090 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 2091 | do_cpuid_1_ent(&entry[t], function, 0); |
| 2092 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 2093 | ++*nent; |
| 2094 | } |
| 2095 | break; |
| 2096 | } |
| 2097 | /* function 4 and 0xb have additional index. */ |
| 2098 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2099 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2100 | |
| 2101 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2102 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2103 | for (i = 1; *nent < maxnent; ++i) { |
| 2104 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2105 | if (!cache_type) |
| 2106 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2107 | do_cpuid_1_ent(&entry[i], function, i); |
| 2108 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2109 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2110 | ++*nent; |
| 2111 | } |
| 2112 | break; |
| 2113 | } |
| 2114 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2115 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2116 | |
| 2117 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2118 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2119 | for (i = 1; *nent < maxnent; ++i) { |
Nitin A Kamble | 0853d2c | 2008-11-05 15:37:36 -0800 | [diff] [blame] | 2120 | level_type = entry[i - 1].ecx & 0xff00; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2121 | if (!level_type) |
| 2122 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 2123 | do_cpuid_1_ent(&entry[i], function, i); |
| 2124 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2125 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2126 | ++*nent; |
| 2127 | } |
| 2128 | break; |
| 2129 | } |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 2130 | case 0xd: { |
| 2131 | int i; |
| 2132 | |
| 2133 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2134 | for (i = 1; *nent < maxnent; ++i) { |
| 2135 | if (entry[i - 1].eax == 0 && i != 2) |
| 2136 | break; |
| 2137 | do_cpuid_1_ent(&entry[i], function, i); |
| 2138 | entry[i].flags |= |
| 2139 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 2140 | ++*nent; |
| 2141 | } |
| 2142 | break; |
| 2143 | } |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 2144 | case KVM_CPUID_SIGNATURE: { |
| 2145 | char signature[12] = "KVMKVMKVM\0\0"; |
| 2146 | u32 *sigptr = (u32 *)signature; |
| 2147 | entry->eax = 0; |
| 2148 | entry->ebx = sigptr[0]; |
| 2149 | entry->ecx = sigptr[1]; |
| 2150 | entry->edx = sigptr[2]; |
| 2151 | break; |
| 2152 | } |
| 2153 | case KVM_CPUID_FEATURES: |
| 2154 | entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) | |
| 2155 | (1 << KVM_FEATURE_NOP_IO_DELAY) | |
Glauber Costa | 371bcf6 | 2010-05-11 12:17:46 -0400 | [diff] [blame] | 2156 | (1 << KVM_FEATURE_CLOCKSOURCE2) | |
| 2157 | (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT); |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 2158 | entry->ebx = 0; |
| 2159 | entry->ecx = 0; |
| 2160 | entry->edx = 0; |
| 2161 | break; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2162 | case 0x80000000: |
| 2163 | entry->eax = min(entry->eax, 0x8000001a); |
| 2164 | break; |
| 2165 | case 0x80000001: |
| 2166 | entry->edx &= kvm_supported_word1_x86_features; |
| 2167 | entry->ecx &= kvm_supported_word6_x86_features; |
| 2168 | break; |
| 2169 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 2170 | |
| 2171 | kvm_x86_ops->set_supported_cpuid(function, entry); |
| 2172 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2173 | put_cpu(); |
| 2174 | } |
| 2175 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 2176 | #undef F |
| 2177 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 2178 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2179 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2180 | { |
| 2181 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 2182 | int limit, nent = 0, r = -E2BIG; |
| 2183 | u32 func; |
| 2184 | |
| 2185 | if (cpuid->nent < 1) |
| 2186 | goto out; |
Avi Kivity | 6a54435 | 2009-10-04 16:45:13 +0200 | [diff] [blame] | 2187 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 2188 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2189 | r = -ENOMEM; |
| 2190 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 2191 | if (!cpuid_entries) |
| 2192 | goto out; |
| 2193 | |
| 2194 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 2195 | limit = cpuid_entries[0].eax; |
| 2196 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 2197 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2198 | &nent, cpuid->nent); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2199 | r = -E2BIG; |
| 2200 | if (nent >= cpuid->nent) |
| 2201 | goto out_free; |
| 2202 | |
| 2203 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 2204 | limit = cpuid_entries[nent - 1].eax; |
| 2205 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 2206 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2207 | &nent, cpuid->nent); |
Glauber Costa | 84478c8 | 2010-05-11 12:17:43 -0400 | [diff] [blame] | 2208 | |
| 2209 | |
| 2210 | |
| 2211 | r = -E2BIG; |
| 2212 | if (nent >= cpuid->nent) |
| 2213 | goto out_free; |
| 2214 | |
| 2215 | do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent, |
| 2216 | cpuid->nent); |
| 2217 | |
| 2218 | r = -E2BIG; |
| 2219 | if (nent >= cpuid->nent) |
| 2220 | goto out_free; |
| 2221 | |
| 2222 | do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent, |
| 2223 | cpuid->nent); |
| 2224 | |
Mark McLoughlin | cb00764 | 2009-05-12 12:36:44 +0100 | [diff] [blame] | 2225 | r = -E2BIG; |
| 2226 | if (nent >= cpuid->nent) |
| 2227 | goto out_free; |
| 2228 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2229 | r = -EFAULT; |
| 2230 | if (copy_to_user(entries, cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2231 | nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2232 | goto out_free; |
| 2233 | cpuid->nent = nent; |
| 2234 | r = 0; |
| 2235 | |
| 2236 | out_free: |
| 2237 | vfree(cpuid_entries); |
| 2238 | out: |
| 2239 | return r; |
| 2240 | } |
| 2241 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2242 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 2243 | struct kvm_lapic_state *s) |
| 2244 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2245 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 2251 | struct kvm_lapic_state *s) |
| 2252 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2253 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2254 | kvm_apic_post_state_restore(vcpu); |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 2255 | update_cr8_intercept(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2256 | |
| 2257 | return 0; |
| 2258 | } |
| 2259 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2260 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 2261 | struct kvm_interrupt *irq) |
| 2262 | { |
| 2263 | if (irq->irq < 0 || irq->irq >= 256) |
| 2264 | return -EINVAL; |
| 2265 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2266 | return -ENXIO; |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2267 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2268 | kvm_queue_interrupt(vcpu, irq->irq, false); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2269 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2270 | return 0; |
| 2271 | } |
| 2272 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2273 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 2274 | { |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2275 | kvm_inject_nmi(vcpu); |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2276 | |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2280 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 2281 | struct kvm_tpr_access_ctl *tac) |
| 2282 | { |
| 2283 | if (tac->flags) |
| 2284 | return -EINVAL; |
| 2285 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2289 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 2290 | u64 mcg_cap) |
| 2291 | { |
| 2292 | int r; |
| 2293 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 2294 | |
| 2295 | r = -EINVAL; |
Jan Kiszka | a9e38c3e | 2009-10-23 09:37:00 +0200 | [diff] [blame] | 2296 | if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2297 | goto out; |
| 2298 | if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000)) |
| 2299 | goto out; |
| 2300 | r = 0; |
| 2301 | vcpu->arch.mcg_cap = mcg_cap; |
| 2302 | /* Init IA32_MCG_CTL to all 1s */ |
| 2303 | if (mcg_cap & MCG_CTL_P) |
| 2304 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 2305 | /* Init IA32_MCi_CTL to all 1s */ |
| 2306 | for (bank = 0; bank < bank_num; bank++) |
| 2307 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
| 2308 | out: |
| 2309 | return r; |
| 2310 | } |
| 2311 | |
| 2312 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 2313 | struct kvm_x86_mce *mce) |
| 2314 | { |
| 2315 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2316 | unsigned bank_num = mcg_cap & 0xff; |
| 2317 | u64 *banks = vcpu->arch.mce_banks; |
| 2318 | |
| 2319 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 2320 | return -EINVAL; |
| 2321 | /* |
| 2322 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 2323 | * reporting is disabled |
| 2324 | */ |
| 2325 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 2326 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 2327 | return 0; |
| 2328 | banks += 4 * mce->bank; |
| 2329 | /* |
| 2330 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 2331 | * reporting is disabled for the bank |
| 2332 | */ |
| 2333 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 2334 | return 0; |
| 2335 | if (mce->status & MCI_STATUS_UC) { |
| 2336 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2337 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2338 | printk(KERN_DEBUG "kvm: set_mce: " |
| 2339 | "injects mce exception while " |
| 2340 | "previous one is in progress!\n"); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2341 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2342 | return 0; |
| 2343 | } |
| 2344 | if (banks[1] & MCI_STATUS_VAL) |
| 2345 | mce->status |= MCI_STATUS_OVER; |
| 2346 | banks[2] = mce->addr; |
| 2347 | banks[3] = mce->misc; |
| 2348 | vcpu->arch.mcg_status = mce->mcg_status; |
| 2349 | banks[1] = mce->status; |
| 2350 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 2351 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 2352 | || !(banks[1] & MCI_STATUS_UC)) { |
| 2353 | if (banks[1] & MCI_STATUS_VAL) |
| 2354 | mce->status |= MCI_STATUS_OVER; |
| 2355 | banks[2] = mce->addr; |
| 2356 | banks[3] = mce->misc; |
| 2357 | banks[1] = mce->status; |
| 2358 | } else |
| 2359 | banks[1] |= MCI_STATUS_OVER; |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2363 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 2364 | struct kvm_vcpu_events *events) |
| 2365 | { |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2366 | events->exception.injected = |
| 2367 | vcpu->arch.exception.pending && |
| 2368 | !kvm_exception_is_soft(vcpu->arch.exception.nr); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2369 | events->exception.nr = vcpu->arch.exception.nr; |
| 2370 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
| 2371 | events->exception.error_code = vcpu->arch.exception.error_code; |
| 2372 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2373 | events->interrupt.injected = |
| 2374 | vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2375 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2376 | events->interrupt.soft = 0; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2377 | events->interrupt.shadow = |
| 2378 | kvm_x86_ops->get_interrupt_shadow(vcpu, |
| 2379 | KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2380 | |
| 2381 | events->nmi.injected = vcpu->arch.nmi_injected; |
| 2382 | events->nmi.pending = vcpu->arch.nmi_pending; |
| 2383 | events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); |
| 2384 | |
| 2385 | events->sipi_vector = vcpu->arch.sipi_vector; |
| 2386 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2387 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2388 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2389 | | KVM_VCPUEVENT_VALID_SHADOW); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 2393 | struct kvm_vcpu_events *events) |
| 2394 | { |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2395 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2396 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2397 | | KVM_VCPUEVENT_VALID_SHADOW)) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2398 | return -EINVAL; |
| 2399 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2400 | vcpu->arch.exception.pending = events->exception.injected; |
| 2401 | vcpu->arch.exception.nr = events->exception.nr; |
| 2402 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 2403 | vcpu->arch.exception.error_code = events->exception.error_code; |
| 2404 | |
| 2405 | vcpu->arch.interrupt.pending = events->interrupt.injected; |
| 2406 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 2407 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
| 2408 | if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm)) |
| 2409 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2410 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
| 2411 | kvm_x86_ops->set_interrupt_shadow(vcpu, |
| 2412 | events->interrupt.shadow); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2413 | |
| 2414 | vcpu->arch.nmi_injected = events->nmi.injected; |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2415 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 2416 | vcpu->arch.nmi_pending = events->nmi.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2417 | kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked); |
| 2418 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2419 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR) |
| 2420 | vcpu->arch.sipi_vector = events->sipi_vector; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2421 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2422 | return 0; |
| 2423 | } |
| 2424 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2425 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 2426 | struct kvm_debugregs *dbgregs) |
| 2427 | { |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2428 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
| 2429 | dbgregs->dr6 = vcpu->arch.dr6; |
| 2430 | dbgregs->dr7 = vcpu->arch.dr7; |
| 2431 | dbgregs->flags = 0; |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 2435 | struct kvm_debugregs *dbgregs) |
| 2436 | { |
| 2437 | if (dbgregs->flags) |
| 2438 | return -EINVAL; |
| 2439 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2440 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
| 2441 | vcpu->arch.dr6 = dbgregs->dr6; |
| 2442 | vcpu->arch.dr7 = dbgregs->dr7; |
| 2443 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2444 | return 0; |
| 2445 | } |
| 2446 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2447 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
| 2448 | struct kvm_xsave *guest_xsave) |
| 2449 | { |
| 2450 | if (cpu_has_xsave) |
| 2451 | memcpy(guest_xsave->region, |
| 2452 | &vcpu->arch.guest_fpu.state->xsave, |
Xiaotian Feng | f45755b | 2010-08-13 15:19:11 +0800 | [diff] [blame] | 2453 | xstate_size); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2454 | else { |
| 2455 | memcpy(guest_xsave->region, |
| 2456 | &vcpu->arch.guest_fpu.state->fxsave, |
| 2457 | sizeof(struct i387_fxsave_struct)); |
| 2458 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
| 2459 | XSTATE_FPSSE; |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
| 2464 | struct kvm_xsave *guest_xsave) |
| 2465 | { |
| 2466 | u64 xstate_bv = |
| 2467 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; |
| 2468 | |
| 2469 | if (cpu_has_xsave) |
| 2470 | memcpy(&vcpu->arch.guest_fpu.state->xsave, |
Xiaotian Feng | f45755b | 2010-08-13 15:19:11 +0800 | [diff] [blame] | 2471 | guest_xsave->region, xstate_size); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2472 | else { |
| 2473 | if (xstate_bv & ~XSTATE_FPSSE) |
| 2474 | return -EINVAL; |
| 2475 | memcpy(&vcpu->arch.guest_fpu.state->fxsave, |
| 2476 | guest_xsave->region, sizeof(struct i387_fxsave_struct)); |
| 2477 | } |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
| 2481 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, |
| 2482 | struct kvm_xcrs *guest_xcrs) |
| 2483 | { |
| 2484 | if (!cpu_has_xsave) { |
| 2485 | guest_xcrs->nr_xcrs = 0; |
| 2486 | return; |
| 2487 | } |
| 2488 | |
| 2489 | guest_xcrs->nr_xcrs = 1; |
| 2490 | guest_xcrs->flags = 0; |
| 2491 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; |
| 2492 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; |
| 2493 | } |
| 2494 | |
| 2495 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, |
| 2496 | struct kvm_xcrs *guest_xcrs) |
| 2497 | { |
| 2498 | int i, r = 0; |
| 2499 | |
| 2500 | if (!cpu_has_xsave) |
| 2501 | return -EINVAL; |
| 2502 | |
| 2503 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) |
| 2504 | return -EINVAL; |
| 2505 | |
| 2506 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) |
| 2507 | /* Only support XCR0 currently */ |
| 2508 | if (guest_xcrs->xcrs[0].xcr == XCR_XFEATURE_ENABLED_MASK) { |
| 2509 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
| 2510 | guest_xcrs->xcrs[0].value); |
| 2511 | break; |
| 2512 | } |
| 2513 | if (r) |
| 2514 | r = -EINVAL; |
| 2515 | return r; |
| 2516 | } |
| 2517 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2518 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 2519 | unsigned int ioctl, unsigned long arg) |
| 2520 | { |
| 2521 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2522 | void __user *argp = (void __user *)arg; |
| 2523 | int r; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2524 | union { |
| 2525 | struct kvm_lapic_state *lapic; |
| 2526 | struct kvm_xsave *xsave; |
| 2527 | struct kvm_xcrs *xcrs; |
| 2528 | void *buffer; |
| 2529 | } u; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2530 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2531 | u.buffer = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2532 | switch (ioctl) { |
| 2533 | case KVM_GET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2534 | r = -EINVAL; |
| 2535 | if (!vcpu->arch.apic) |
| 2536 | goto out; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2537 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2538 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2539 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2540 | if (!u.lapic) |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2541 | goto out; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2542 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2543 | if (r) |
| 2544 | goto out; |
| 2545 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2546 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2547 | goto out; |
| 2548 | r = 0; |
| 2549 | break; |
| 2550 | } |
| 2551 | case KVM_SET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2552 | r = -EINVAL; |
| 2553 | if (!vcpu->arch.apic) |
| 2554 | goto out; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2555 | u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2556 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2557 | if (!u.lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2558 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2559 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2560 | if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state))) |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2561 | goto out; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2562 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2563 | if (r) |
| 2564 | goto out; |
| 2565 | r = 0; |
| 2566 | break; |
| 2567 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2568 | case KVM_INTERRUPT: { |
| 2569 | struct kvm_interrupt irq; |
| 2570 | |
| 2571 | r = -EFAULT; |
| 2572 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 2573 | goto out; |
| 2574 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 2575 | if (r) |
| 2576 | goto out; |
| 2577 | r = 0; |
| 2578 | break; |
| 2579 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2580 | case KVM_NMI: { |
| 2581 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 2582 | if (r) |
| 2583 | goto out; |
| 2584 | r = 0; |
| 2585 | break; |
| 2586 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2587 | case KVM_SET_CPUID: { |
| 2588 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 2589 | struct kvm_cpuid cpuid; |
| 2590 | |
| 2591 | r = -EFAULT; |
| 2592 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2593 | goto out; |
| 2594 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 2595 | if (r) |
| 2596 | goto out; |
| 2597 | break; |
| 2598 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2599 | case KVM_SET_CPUID2: { |
| 2600 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2601 | struct kvm_cpuid2 cpuid; |
| 2602 | |
| 2603 | r = -EFAULT; |
| 2604 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2605 | goto out; |
| 2606 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2607 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2608 | if (r) |
| 2609 | goto out; |
| 2610 | break; |
| 2611 | } |
| 2612 | case KVM_GET_CPUID2: { |
| 2613 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2614 | struct kvm_cpuid2 cpuid; |
| 2615 | |
| 2616 | r = -EFAULT; |
| 2617 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2618 | goto out; |
| 2619 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2620 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2621 | if (r) |
| 2622 | goto out; |
| 2623 | r = -EFAULT; |
| 2624 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 2625 | goto out; |
| 2626 | r = 0; |
| 2627 | break; |
| 2628 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2629 | case KVM_GET_MSRS: |
| 2630 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 2631 | break; |
| 2632 | case KVM_SET_MSRS: |
| 2633 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 2634 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2635 | case KVM_TPR_ACCESS_REPORTING: { |
| 2636 | struct kvm_tpr_access_ctl tac; |
| 2637 | |
| 2638 | r = -EFAULT; |
| 2639 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 2640 | goto out; |
| 2641 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 2642 | if (r) |
| 2643 | goto out; |
| 2644 | r = -EFAULT; |
| 2645 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 2646 | goto out; |
| 2647 | r = 0; |
| 2648 | break; |
| 2649 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2650 | case KVM_SET_VAPIC_ADDR: { |
| 2651 | struct kvm_vapic_addr va; |
| 2652 | |
| 2653 | r = -EINVAL; |
| 2654 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 2655 | goto out; |
| 2656 | r = -EFAULT; |
| 2657 | if (copy_from_user(&va, argp, sizeof va)) |
| 2658 | goto out; |
| 2659 | r = 0; |
| 2660 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 2661 | break; |
| 2662 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2663 | case KVM_X86_SETUP_MCE: { |
| 2664 | u64 mcg_cap; |
| 2665 | |
| 2666 | r = -EFAULT; |
| 2667 | if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap)) |
| 2668 | goto out; |
| 2669 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 2670 | break; |
| 2671 | } |
| 2672 | case KVM_X86_SET_MCE: { |
| 2673 | struct kvm_x86_mce mce; |
| 2674 | |
| 2675 | r = -EFAULT; |
| 2676 | if (copy_from_user(&mce, argp, sizeof mce)) |
| 2677 | goto out; |
| 2678 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 2679 | break; |
| 2680 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2681 | case KVM_GET_VCPU_EVENTS: { |
| 2682 | struct kvm_vcpu_events events; |
| 2683 | |
| 2684 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 2685 | |
| 2686 | r = -EFAULT; |
| 2687 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 2688 | break; |
| 2689 | r = 0; |
| 2690 | break; |
| 2691 | } |
| 2692 | case KVM_SET_VCPU_EVENTS: { |
| 2693 | struct kvm_vcpu_events events; |
| 2694 | |
| 2695 | r = -EFAULT; |
| 2696 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 2697 | break; |
| 2698 | |
| 2699 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 2700 | break; |
| 2701 | } |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2702 | case KVM_GET_DEBUGREGS: { |
| 2703 | struct kvm_debugregs dbgregs; |
| 2704 | |
| 2705 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 2706 | |
| 2707 | r = -EFAULT; |
| 2708 | if (copy_to_user(argp, &dbgregs, |
| 2709 | sizeof(struct kvm_debugregs))) |
| 2710 | break; |
| 2711 | r = 0; |
| 2712 | break; |
| 2713 | } |
| 2714 | case KVM_SET_DEBUGREGS: { |
| 2715 | struct kvm_debugregs dbgregs; |
| 2716 | |
| 2717 | r = -EFAULT; |
| 2718 | if (copy_from_user(&dbgregs, argp, |
| 2719 | sizeof(struct kvm_debugregs))) |
| 2720 | break; |
| 2721 | |
| 2722 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 2723 | break; |
| 2724 | } |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2725 | case KVM_GET_XSAVE: { |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2726 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2727 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2728 | if (!u.xsave) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2729 | break; |
| 2730 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2731 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2732 | |
| 2733 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2734 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2735 | break; |
| 2736 | r = 0; |
| 2737 | break; |
| 2738 | } |
| 2739 | case KVM_SET_XSAVE: { |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2740 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2741 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2742 | if (!u.xsave) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2743 | break; |
| 2744 | |
| 2745 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2746 | if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave))) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2747 | break; |
| 2748 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2749 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2750 | break; |
| 2751 | } |
| 2752 | case KVM_GET_XCRS: { |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2753 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2754 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2755 | if (!u.xcrs) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2756 | break; |
| 2757 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2758 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2759 | |
| 2760 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2761 | if (copy_to_user(argp, u.xcrs, |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2762 | sizeof(struct kvm_xcrs))) |
| 2763 | break; |
| 2764 | r = 0; |
| 2765 | break; |
| 2766 | } |
| 2767 | case KVM_SET_XCRS: { |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2768 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2769 | r = -ENOMEM; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2770 | if (!u.xcrs) |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2771 | break; |
| 2772 | |
| 2773 | r = -EFAULT; |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2774 | if (copy_from_user(u.xcrs, argp, |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2775 | sizeof(struct kvm_xcrs))) |
| 2776 | break; |
| 2777 | |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2778 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame] | 2779 | break; |
| 2780 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2781 | default: |
| 2782 | r = -EINVAL; |
| 2783 | } |
| 2784 | out: |
Avi Kivity | d1ac91d | 2010-06-20 15:54:43 +0300 | [diff] [blame] | 2785 | kfree(u.buffer); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2786 | return r; |
| 2787 | } |
| 2788 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2789 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 2790 | { |
| 2791 | int ret; |
| 2792 | |
| 2793 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 2794 | return -1; |
| 2795 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 2796 | return ret; |
| 2797 | } |
| 2798 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2799 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 2800 | u64 ident_addr) |
| 2801 | { |
| 2802 | kvm->arch.ept_identity_map_addr = ident_addr; |
| 2803 | return 0; |
| 2804 | } |
| 2805 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2806 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 2807 | u32 kvm_nr_mmu_pages) |
| 2808 | { |
| 2809 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 2810 | return -EINVAL; |
| 2811 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2812 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2813 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2814 | |
| 2815 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2816 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2817 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2818 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2819 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2820 | return 0; |
| 2821 | } |
| 2822 | |
| 2823 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 2824 | { |
Dave Hansen | 39de71e | 2010-08-19 18:11:14 -0700 | [diff] [blame] | 2825 | return kvm->arch.n_max_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2826 | } |
| 2827 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2828 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2829 | { |
| 2830 | int r; |
| 2831 | |
| 2832 | r = 0; |
| 2833 | switch (chip->chip_id) { |
| 2834 | case KVM_IRQCHIP_PIC_MASTER: |
| 2835 | memcpy(&chip->chip.pic, |
| 2836 | &pic_irqchip(kvm)->pics[0], |
| 2837 | sizeof(struct kvm_pic_state)); |
| 2838 | break; |
| 2839 | case KVM_IRQCHIP_PIC_SLAVE: |
| 2840 | memcpy(&chip->chip.pic, |
| 2841 | &pic_irqchip(kvm)->pics[1], |
| 2842 | sizeof(struct kvm_pic_state)); |
| 2843 | break; |
| 2844 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2845 | r = kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2846 | break; |
| 2847 | default: |
| 2848 | r = -EINVAL; |
| 2849 | break; |
| 2850 | } |
| 2851 | return r; |
| 2852 | } |
| 2853 | |
| 2854 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2855 | { |
| 2856 | int r; |
| 2857 | |
| 2858 | r = 0; |
| 2859 | switch (chip->chip_id) { |
| 2860 | case KVM_IRQCHIP_PIC_MASTER: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2861 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2862 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 2863 | &chip->chip.pic, |
| 2864 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2865 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2866 | break; |
| 2867 | case KVM_IRQCHIP_PIC_SLAVE: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2868 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2869 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 2870 | &chip->chip.pic, |
| 2871 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2872 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2873 | break; |
| 2874 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2875 | r = kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2876 | break; |
| 2877 | default: |
| 2878 | r = -EINVAL; |
| 2879 | break; |
| 2880 | } |
| 2881 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 2882 | return r; |
| 2883 | } |
| 2884 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2885 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2886 | { |
| 2887 | int r = 0; |
| 2888 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2889 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2890 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2891 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2892 | return r; |
| 2893 | } |
| 2894 | |
| 2895 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2896 | { |
| 2897 | int r = 0; |
| 2898 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2899 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2900 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2901 | kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); |
| 2902 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2903 | return r; |
| 2904 | } |
| 2905 | |
| 2906 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2907 | { |
| 2908 | int r = 0; |
| 2909 | |
| 2910 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2911 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 2912 | sizeof(ps->channels)); |
| 2913 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 2914 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2915 | return r; |
| 2916 | } |
| 2917 | |
| 2918 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2919 | { |
| 2920 | int r = 0, start = 0; |
| 2921 | u32 prev_legacy, cur_legacy; |
| 2922 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2923 | prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2924 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2925 | if (!prev_legacy && cur_legacy) |
| 2926 | start = 1; |
| 2927 | memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, |
| 2928 | sizeof(kvm->arch.vpit->pit_state.channels)); |
| 2929 | kvm->arch.vpit->pit_state.flags = ps->flags; |
| 2930 | 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] | 2931 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2932 | return r; |
| 2933 | } |
| 2934 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2935 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 2936 | struct kvm_reinject_control *control) |
| 2937 | { |
| 2938 | if (!kvm->arch.vpit) |
| 2939 | return -ENXIO; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2940 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2941 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2942 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2943 | return 0; |
| 2944 | } |
| 2945 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2946 | /* |
| 2947 | * Get (and clear) the dirty memory log for a memory slot. |
| 2948 | */ |
| 2949 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 2950 | struct kvm_dirty_log *log) |
| 2951 | { |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2952 | int r, i; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2953 | struct kvm_memory_slot *memslot; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2954 | unsigned long n; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2955 | unsigned long is_dirty = 0; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2956 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2957 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2958 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2959 | r = -EINVAL; |
| 2960 | if (log->slot >= KVM_MEMORY_SLOTS) |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2961 | goto out; |
| 2962 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2963 | memslot = &kvm->memslots->memslots[log->slot]; |
| 2964 | r = -ENOENT; |
| 2965 | if (!memslot->dirty_bitmap) |
| 2966 | goto out; |
| 2967 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2968 | n = kvm_dirty_bitmap_bytes(memslot); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2969 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2970 | for (i = 0; !is_dirty && i < n/sizeof(long); i++) |
| 2971 | is_dirty = memslot->dirty_bitmap[i]; |
| 2972 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2973 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 2974 | if (is_dirty) { |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2975 | struct kvm_memslots *slots, *old_slots; |
Takuya Yoshikawa | 914ebcc | 2010-04-28 18:50:36 +0900 | [diff] [blame] | 2976 | unsigned long *dirty_bitmap; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2977 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2978 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2979 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2980 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2981 | |
Takuya Yoshikawa | 914ebcc | 2010-04-28 18:50:36 +0900 | [diff] [blame] | 2982 | r = -ENOMEM; |
| 2983 | dirty_bitmap = vmalloc(n); |
| 2984 | if (!dirty_bitmap) |
| 2985 | goto out; |
| 2986 | memset(dirty_bitmap, 0, n); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2987 | |
Takuya Yoshikawa | 914ebcc | 2010-04-28 18:50:36 +0900 | [diff] [blame] | 2988 | r = -ENOMEM; |
| 2989 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 2990 | if (!slots) { |
| 2991 | vfree(dirty_bitmap); |
| 2992 | goto out; |
| 2993 | } |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2994 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 2995 | slots->memslots[log->slot].dirty_bitmap = dirty_bitmap; |
| 2996 | |
| 2997 | old_slots = kvm->memslots; |
| 2998 | rcu_assign_pointer(kvm->memslots, slots); |
| 2999 | synchronize_srcu_expedited(&kvm->srcu); |
| 3000 | dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap; |
| 3001 | kfree(old_slots); |
Takuya Yoshikawa | 914ebcc | 2010-04-28 18:50:36 +0900 | [diff] [blame] | 3002 | |
| 3003 | r = -EFAULT; |
| 3004 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) { |
| 3005 | vfree(dirty_bitmap); |
| 3006 | goto out; |
| 3007 | } |
| 3008 | vfree(dirty_bitmap); |
| 3009 | } else { |
| 3010 | r = -EFAULT; |
| 3011 | if (clear_user(log->dirty_bitmap, n)) |
| 3012 | goto out; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 3013 | } |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 3014 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 3015 | r = 0; |
| 3016 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3017 | mutex_unlock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 3018 | return r; |
| 3019 | } |
| 3020 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3021 | long kvm_arch_vm_ioctl(struct file *filp, |
| 3022 | unsigned int ioctl, unsigned long arg) |
| 3023 | { |
| 3024 | struct kvm *kvm = filp->private_data; |
| 3025 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 3026 | int r = -ENOTTY; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3027 | /* |
| 3028 | * This union makes it completely explicit to gcc-3.x |
| 3029 | * that these two variables' stack usage should be |
| 3030 | * combined, not added together. |
| 3031 | */ |
| 3032 | union { |
| 3033 | struct kvm_pit_state ps; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 3034 | struct kvm_pit_state2 ps2; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 3035 | struct kvm_pit_config pit_config; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3036 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3037 | |
| 3038 | switch (ioctl) { |
| 3039 | case KVM_SET_TSS_ADDR: |
| 3040 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 3041 | if (r < 0) |
| 3042 | goto out; |
| 3043 | break; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3044 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 3045 | u64 ident_addr; |
| 3046 | |
| 3047 | r = -EFAULT; |
| 3048 | if (copy_from_user(&ident_addr, argp, sizeof ident_addr)) |
| 3049 | goto out; |
| 3050 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
| 3051 | if (r < 0) |
| 3052 | goto out; |
| 3053 | break; |
| 3054 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3055 | case KVM_SET_NR_MMU_PAGES: |
| 3056 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 3057 | if (r) |
| 3058 | goto out; |
| 3059 | break; |
| 3060 | case KVM_GET_NR_MMU_PAGES: |
| 3061 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 3062 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3063 | case KVM_CREATE_IRQCHIP: { |
| 3064 | struct kvm_pic *vpic; |
| 3065 | |
| 3066 | mutex_lock(&kvm->lock); |
| 3067 | r = -EEXIST; |
| 3068 | if (kvm->arch.vpic) |
| 3069 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3070 | r = -ENOMEM; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3071 | vpic = kvm_create_pic(kvm); |
| 3072 | if (vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3073 | r = kvm_ioapic_init(kvm); |
| 3074 | if (r) { |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 3075 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, |
| 3076 | &vpic->dev); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3077 | kfree(vpic); |
| 3078 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3079 | } |
| 3080 | } else |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3081 | goto create_irqchip_unlock; |
| 3082 | smp_wmb(); |
| 3083 | kvm->arch.vpic = vpic; |
| 3084 | smp_wmb(); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 3085 | r = kvm_setup_default_irq_routing(kvm); |
| 3086 | if (r) { |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3087 | mutex_lock(&kvm->irq_lock); |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 3088 | kvm_ioapic_destroy(kvm); |
| 3089 | kvm_destroy_pic(kvm); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3090 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 3091 | } |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3092 | create_irqchip_unlock: |
| 3093 | mutex_unlock(&kvm->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3094 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 3095 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 3096 | case KVM_CREATE_PIT: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 3097 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 3098 | goto create_pit; |
| 3099 | case KVM_CREATE_PIT2: |
| 3100 | r = -EFAULT; |
| 3101 | if (copy_from_user(&u.pit_config, argp, |
| 3102 | sizeof(struct kvm_pit_config))) |
| 3103 | goto out; |
| 3104 | create_pit: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3105 | mutex_lock(&kvm->slots_lock); |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 3106 | r = -EEXIST; |
| 3107 | if (kvm->arch.vpit) |
| 3108 | goto create_pit_unlock; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 3109 | r = -ENOMEM; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 3110 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 3111 | if (kvm->arch.vpit) |
| 3112 | r = 0; |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 3113 | create_pit_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3114 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 3115 | break; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3116 | case KVM_IRQ_LINE_STATUS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3117 | case KVM_IRQ_LINE: { |
| 3118 | struct kvm_irq_level irq_event; |
| 3119 | |
| 3120 | r = -EFAULT; |
| 3121 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 3122 | goto out; |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame] | 3123 | r = -ENXIO; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3124 | if (irqchip_in_kernel(kvm)) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3125 | __s32 status; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3126 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 3127 | irq_event.irq, irq_event.level); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3128 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
Wei Yongjun | 160d2f6 | 2010-03-12 10:09:45 +0800 | [diff] [blame] | 3129 | r = -EFAULT; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 3130 | irq_event.status = status; |
| 3131 | if (copy_to_user(argp, &irq_event, |
| 3132 | sizeof irq_event)) |
| 3133 | goto out; |
| 3134 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3135 | r = 0; |
| 3136 | } |
| 3137 | break; |
| 3138 | } |
| 3139 | case KVM_GET_IRQCHIP: { |
| 3140 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3141 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3142 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3143 | r = -ENOMEM; |
| 3144 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3145 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3146 | r = -EFAULT; |
| 3147 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 3148 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3149 | r = -ENXIO; |
| 3150 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3151 | goto get_irqchip_out; |
| 3152 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 3153 | if (r) |
| 3154 | goto get_irqchip_out; |
| 3155 | r = -EFAULT; |
| 3156 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 3157 | goto get_irqchip_out; |
| 3158 | r = 0; |
| 3159 | get_irqchip_out: |
| 3160 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3161 | if (r) |
| 3162 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3163 | break; |
| 3164 | } |
| 3165 | case KVM_SET_IRQCHIP: { |
| 3166 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3167 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3168 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3169 | r = -ENOMEM; |
| 3170 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3171 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3172 | r = -EFAULT; |
| 3173 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 3174 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3175 | r = -ENXIO; |
| 3176 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3177 | goto set_irqchip_out; |
| 3178 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 3179 | if (r) |
| 3180 | goto set_irqchip_out; |
| 3181 | r = 0; |
| 3182 | set_irqchip_out: |
| 3183 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3184 | if (r) |
| 3185 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3186 | break; |
| 3187 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3188 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3189 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3190 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3191 | goto out; |
| 3192 | r = -ENXIO; |
| 3193 | if (!kvm->arch.vpit) |
| 3194 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3195 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3196 | if (r) |
| 3197 | goto out; |
| 3198 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3199 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3200 | goto out; |
| 3201 | r = 0; |
| 3202 | break; |
| 3203 | } |
| 3204 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3205 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3206 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3207 | goto out; |
| 3208 | r = -ENXIO; |
| 3209 | if (!kvm->arch.vpit) |
| 3210 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 3211 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 3212 | if (r) |
| 3213 | goto out; |
| 3214 | r = 0; |
| 3215 | break; |
| 3216 | } |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 3217 | case KVM_GET_PIT2: { |
| 3218 | r = -ENXIO; |
| 3219 | if (!kvm->arch.vpit) |
| 3220 | goto out; |
| 3221 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 3222 | if (r) |
| 3223 | goto out; |
| 3224 | r = -EFAULT; |
| 3225 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 3226 | goto out; |
| 3227 | r = 0; |
| 3228 | break; |
| 3229 | } |
| 3230 | case KVM_SET_PIT2: { |
| 3231 | r = -EFAULT; |
| 3232 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 3233 | goto out; |
| 3234 | r = -ENXIO; |
| 3235 | if (!kvm->arch.vpit) |
| 3236 | goto out; |
| 3237 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
| 3238 | if (r) |
| 3239 | goto out; |
| 3240 | r = 0; |
| 3241 | break; |
| 3242 | } |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 3243 | case KVM_REINJECT_CONTROL: { |
| 3244 | struct kvm_reinject_control control; |
| 3245 | r = -EFAULT; |
| 3246 | if (copy_from_user(&control, argp, sizeof(control))) |
| 3247 | goto out; |
| 3248 | r = kvm_vm_ioctl_reinject(kvm, &control); |
| 3249 | if (r) |
| 3250 | goto out; |
| 3251 | r = 0; |
| 3252 | break; |
| 3253 | } |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 3254 | case KVM_XEN_HVM_CONFIG: { |
| 3255 | r = -EFAULT; |
| 3256 | if (copy_from_user(&kvm->arch.xen_hvm_config, argp, |
| 3257 | sizeof(struct kvm_xen_hvm_config))) |
| 3258 | goto out; |
| 3259 | r = -EINVAL; |
| 3260 | if (kvm->arch.xen_hvm_config.flags) |
| 3261 | goto out; |
| 3262 | r = 0; |
| 3263 | break; |
| 3264 | } |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 3265 | case KVM_SET_CLOCK: { |
| 3266 | struct timespec now; |
| 3267 | struct kvm_clock_data user_ns; |
| 3268 | u64 now_ns; |
| 3269 | s64 delta; |
| 3270 | |
| 3271 | r = -EFAULT; |
| 3272 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 3273 | goto out; |
| 3274 | |
| 3275 | r = -EINVAL; |
| 3276 | if (user_ns.flags) |
| 3277 | goto out; |
| 3278 | |
| 3279 | r = 0; |
| 3280 | ktime_get_ts(&now); |
| 3281 | now_ns = timespec_to_ns(&now); |
| 3282 | delta = user_ns.clock - now_ns; |
| 3283 | kvm->arch.kvmclock_offset = delta; |
| 3284 | break; |
| 3285 | } |
| 3286 | case KVM_GET_CLOCK: { |
| 3287 | struct timespec now; |
| 3288 | struct kvm_clock_data user_ns; |
| 3289 | u64 now_ns; |
| 3290 | |
| 3291 | ktime_get_ts(&now); |
| 3292 | now_ns = timespec_to_ns(&now); |
| 3293 | user_ns.clock = kvm->arch.kvmclock_offset + now_ns; |
| 3294 | user_ns.flags = 0; |
| 3295 | |
| 3296 | r = -EFAULT; |
| 3297 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 3298 | goto out; |
| 3299 | r = 0; |
| 3300 | break; |
| 3301 | } |
| 3302 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3303 | default: |
| 3304 | ; |
| 3305 | } |
| 3306 | out: |
| 3307 | return r; |
| 3308 | } |
| 3309 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 3310 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3311 | { |
| 3312 | u32 dummy[2]; |
| 3313 | unsigned i, j; |
| 3314 | |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 3315 | /* skip the first msrs in the list. KVM-specific */ |
| 3316 | 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] | 3317 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 3318 | continue; |
| 3319 | if (j < i) |
| 3320 | msrs_to_save[j] = msrs_to_save[i]; |
| 3321 | j++; |
| 3322 | } |
| 3323 | num_msrs_to_save = j; |
| 3324 | } |
| 3325 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3326 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 3327 | const void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3328 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3329 | if (vcpu->arch.apic && |
| 3330 | !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v)) |
| 3331 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3332 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3333 | 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] | 3334 | } |
| 3335 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3336 | 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] | 3337 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3338 | if (vcpu->arch.apic && |
| 3339 | !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v)) |
| 3340 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3341 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3342 | 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] | 3343 | } |
| 3344 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3345 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
| 3346 | struct kvm_segment *var, int seg) |
| 3347 | { |
| 3348 | kvm_x86_ops->set_segment(vcpu, var, seg); |
| 3349 | } |
| 3350 | |
| 3351 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 3352 | struct kvm_segment *var, int seg) |
| 3353 | { |
| 3354 | kvm_x86_ops->get_segment(vcpu, var, seg); |
| 3355 | } |
| 3356 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3357 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3358 | { |
| 3359 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3360 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3361 | } |
| 3362 | |
| 3363 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3364 | { |
| 3365 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3366 | access |= PFERR_FETCH_MASK; |
| 3367 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3368 | } |
| 3369 | |
| 3370 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3371 | { |
| 3372 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3373 | access |= PFERR_WRITE_MASK; |
| 3374 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3375 | } |
| 3376 | |
| 3377 | /* uses this to access any guest's mapped memory without checking CPL */ |
| 3378 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3379 | { |
| 3380 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error); |
| 3381 | } |
| 3382 | |
| 3383 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 3384 | struct kvm_vcpu *vcpu, u32 access, |
| 3385 | u32 *error) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3386 | { |
| 3387 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3388 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3389 | |
| 3390 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3391 | 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] | 3392 | unsigned offset = addr & (PAGE_SIZE-1); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3393 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3394 | int ret; |
| 3395 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3396 | if (gpa == UNMAPPED_GVA) { |
| 3397 | r = X86EMUL_PROPAGATE_FAULT; |
| 3398 | goto out; |
| 3399 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3400 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3401 | if (ret < 0) { |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 3402 | r = X86EMUL_IO_NEEDED; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3403 | goto out; |
| 3404 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3405 | |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3406 | bytes -= toread; |
| 3407 | data += toread; |
| 3408 | addr += toread; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3409 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3410 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3411 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3412 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3413 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3414 | /* used for instruction fetching */ |
| 3415 | static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3416 | struct kvm_vcpu *vcpu, u32 *error) |
| 3417 | { |
| 3418 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3419 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, |
| 3420 | access | PFERR_FETCH_MASK, error); |
| 3421 | } |
| 3422 | |
| 3423 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3424 | struct kvm_vcpu *vcpu, u32 *error) |
| 3425 | { |
| 3426 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3427 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
| 3428 | error); |
| 3429 | } |
| 3430 | |
| 3431 | static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes, |
| 3432 | struct kvm_vcpu *vcpu, u32 *error) |
| 3433 | { |
| 3434 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error); |
| 3435 | } |
| 3436 | |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3437 | static int kvm_write_guest_virt_system(gva_t addr, void *val, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3438 | unsigned int bytes, |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3439 | struct kvm_vcpu *vcpu, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3440 | u32 *error) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3441 | { |
| 3442 | void *data = val; |
| 3443 | int r = X86EMUL_CONTINUE; |
| 3444 | |
| 3445 | while (bytes) { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3446 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, |
| 3447 | PFERR_WRITE_MASK, error); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3448 | unsigned offset = addr & (PAGE_SIZE-1); |
| 3449 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 3450 | int ret; |
| 3451 | |
| 3452 | if (gpa == UNMAPPED_GVA) { |
| 3453 | r = X86EMUL_PROPAGATE_FAULT; |
| 3454 | goto out; |
| 3455 | } |
| 3456 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); |
| 3457 | if (ret < 0) { |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 3458 | r = X86EMUL_IO_NEEDED; |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3459 | goto out; |
| 3460 | } |
| 3461 | |
| 3462 | bytes -= towrite; |
| 3463 | data += towrite; |
| 3464 | addr += towrite; |
| 3465 | } |
| 3466 | out: |
| 3467 | return r; |
| 3468 | } |
| 3469 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3470 | static int emulator_read_emulated(unsigned long addr, |
| 3471 | void *val, |
| 3472 | unsigned int bytes, |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3473 | unsigned int *error_code, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3474 | struct kvm_vcpu *vcpu) |
| 3475 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3476 | gpa_t gpa; |
| 3477 | |
| 3478 | if (vcpu->mmio_read_completed) { |
| 3479 | memcpy(val, vcpu->mmio_data, bytes); |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3480 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
| 3481 | vcpu->mmio_phys_addr, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3482 | vcpu->mmio_read_completed = 0; |
| 3483 | return X86EMUL_CONTINUE; |
| 3484 | } |
| 3485 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3486 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, error_code); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3487 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3488 | if (gpa == UNMAPPED_GVA) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3489 | return X86EMUL_PROPAGATE_FAULT; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3490 | |
| 3491 | /* For APIC access vmexit */ |
| 3492 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3493 | goto mmio; |
| 3494 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3495 | if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3496 | == X86EMUL_CONTINUE) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3497 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3498 | |
| 3499 | mmio: |
| 3500 | /* |
| 3501 | * Is this MMIO handled locally? |
| 3502 | */ |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3503 | if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) { |
| 3504 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3505 | return X86EMUL_CONTINUE; |
| 3506 | } |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3507 | |
| 3508 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3509 | |
| 3510 | vcpu->mmio_needed = 1; |
Gleb Natapov | 411c35b | 2010-04-28 19:15:34 +0300 | [diff] [blame] | 3511 | vcpu->run->exit_reason = KVM_EXIT_MMIO; |
| 3512 | vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa; |
| 3513 | vcpu->run->mmio.len = vcpu->mmio_size = bytes; |
| 3514 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3515 | |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 3516 | return X86EMUL_IO_NEEDED; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3517 | } |
| 3518 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3519 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3520 | const void *val, int bytes) |
| 3521 | { |
| 3522 | int ret; |
| 3523 | |
| 3524 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 3525 | if (ret < 0) |
| 3526 | return 0; |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 3527 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3528 | return 1; |
| 3529 | } |
| 3530 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3531 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 3532 | const void *val, |
| 3533 | unsigned int bytes, |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3534 | unsigned int *error_code, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3535 | struct kvm_vcpu *vcpu) |
| 3536 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3537 | gpa_t gpa; |
| 3538 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3539 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3540 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3541 | if (gpa == UNMAPPED_GVA) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3542 | return X86EMUL_PROPAGATE_FAULT; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3543 | |
| 3544 | /* For APIC access vmexit */ |
| 3545 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3546 | goto mmio; |
| 3547 | |
| 3548 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 3549 | return X86EMUL_CONTINUE; |
| 3550 | |
| 3551 | mmio: |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3552 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3553 | /* |
| 3554 | * Is this MMIO handled locally? |
| 3555 | */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3556 | if (!vcpu_mmio_write(vcpu, gpa, bytes, val)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3557 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3558 | |
| 3559 | vcpu->mmio_needed = 1; |
Gleb Natapov | 411c35b | 2010-04-28 19:15:34 +0300 | [diff] [blame] | 3560 | vcpu->run->exit_reason = KVM_EXIT_MMIO; |
| 3561 | vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa; |
| 3562 | vcpu->run->mmio.len = vcpu->mmio_size = bytes; |
| 3563 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1; |
| 3564 | memcpy(vcpu->run->mmio.data, val, bytes); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3565 | |
| 3566 | return X86EMUL_CONTINUE; |
| 3567 | } |
| 3568 | |
| 3569 | int emulator_write_emulated(unsigned long addr, |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3570 | const void *val, |
| 3571 | unsigned int bytes, |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3572 | unsigned int *error_code, |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3573 | struct kvm_vcpu *vcpu) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3574 | { |
| 3575 | /* Crossing a page boundary? */ |
| 3576 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 3577 | int rc, now; |
| 3578 | |
| 3579 | now = -addr & ~PAGE_MASK; |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3580 | rc = emulator_write_emulated_onepage(addr, val, now, error_code, |
| 3581 | vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3582 | if (rc != X86EMUL_CONTINUE) |
| 3583 | return rc; |
| 3584 | addr += now; |
| 3585 | val += now; |
| 3586 | bytes -= now; |
| 3587 | } |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3588 | return emulator_write_emulated_onepage(addr, val, bytes, error_code, |
| 3589 | vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3590 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3591 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3592 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
| 3593 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) |
| 3594 | |
| 3595 | #ifdef CONFIG_X86_64 |
| 3596 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) |
| 3597 | #else |
| 3598 | # define CMPXCHG64(ptr, old, new) \ |
Jan Kiszka | 9749a6c | 2010-03-20 10:14:13 +0100 | [diff] [blame] | 3599 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3600 | #endif |
| 3601 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3602 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 3603 | const void *old, |
| 3604 | const void *new, |
| 3605 | unsigned int bytes, |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3606 | unsigned int *error_code, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3607 | struct kvm_vcpu *vcpu) |
| 3608 | { |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3609 | gpa_t gpa; |
| 3610 | struct page *page; |
| 3611 | char *kaddr; |
| 3612 | bool exchanged; |
| 3613 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3614 | /* guests cmpxchg8b have to be emulated atomically */ |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3615 | if (bytes > 8 || (bytes & (bytes - 1))) |
| 3616 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3617 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3618 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3619 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3620 | if (gpa == UNMAPPED_GVA || |
| 3621 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3622 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3623 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3624 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 3625 | goto emul_write; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3626 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3627 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Wei Yongjun | c19b8bd | 2010-07-15 08:51:58 +0800 | [diff] [blame] | 3628 | if (is_error_page(page)) { |
| 3629 | kvm_release_page_clean(page); |
| 3630 | goto emul_write; |
| 3631 | } |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3632 | |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3633 | kaddr = kmap_atomic(page, KM_USER0); |
| 3634 | kaddr += offset_in_page(gpa); |
| 3635 | switch (bytes) { |
| 3636 | case 1: |
| 3637 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); |
| 3638 | break; |
| 3639 | case 2: |
| 3640 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); |
| 3641 | break; |
| 3642 | case 4: |
| 3643 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); |
| 3644 | break; |
| 3645 | case 8: |
| 3646 | exchanged = CMPXCHG64(kaddr, old, new); |
| 3647 | break; |
| 3648 | default: |
| 3649 | BUG(); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3650 | } |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3651 | kunmap_atomic(kaddr, KM_USER0); |
| 3652 | kvm_release_page_dirty(page); |
| 3653 | |
| 3654 | if (!exchanged) |
| 3655 | return X86EMUL_CMPXCHG_FAILED; |
| 3656 | |
Gleb Natapov | 8f6abd0 | 2010-04-13 10:21:56 +0300 | [diff] [blame] | 3657 | kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1); |
| 3658 | |
| 3659 | return X86EMUL_CONTINUE; |
Avi Kivity | 4a5f48f | 2010-03-15 13:59:55 +0200 | [diff] [blame] | 3660 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3661 | emul_write: |
Avi Kivity | daea3e7 | 2010-03-15 13:59:54 +0200 | [diff] [blame] | 3662 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3663 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 3664 | return emulator_write_emulated(addr, new, bytes, error_code, vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3665 | } |
| 3666 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3667 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
| 3668 | { |
| 3669 | /* TODO: String I/O for in kernel device */ |
| 3670 | int r; |
| 3671 | |
| 3672 | if (vcpu->arch.pio.in) |
| 3673 | r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port, |
| 3674 | vcpu->arch.pio.size, pd); |
| 3675 | else |
| 3676 | r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
| 3677 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 3678 | pd); |
| 3679 | return r; |
| 3680 | } |
| 3681 | |
| 3682 | |
| 3683 | static int emulator_pio_in_emulated(int size, unsigned short port, void *val, |
| 3684 | unsigned int count, struct kvm_vcpu *vcpu) |
| 3685 | { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3686 | if (vcpu->arch.pio.count) |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3687 | goto data_avail; |
| 3688 | |
| 3689 | trace_kvm_pio(1, port, size, 1); |
| 3690 | |
| 3691 | vcpu->arch.pio.port = port; |
| 3692 | vcpu->arch.pio.in = 1; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3693 | vcpu->arch.pio.count = count; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3694 | vcpu->arch.pio.size = size; |
| 3695 | |
| 3696 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
| 3697 | data_avail: |
| 3698 | memcpy(val, vcpu->arch.pio_data, size * count); |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3699 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3700 | return 1; |
| 3701 | } |
| 3702 | |
| 3703 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3704 | vcpu->run->io.direction = KVM_EXIT_IO_IN; |
| 3705 | vcpu->run->io.size = size; |
| 3706 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 3707 | vcpu->run->io.count = count; |
| 3708 | vcpu->run->io.port = port; |
| 3709 | |
| 3710 | return 0; |
| 3711 | } |
| 3712 | |
| 3713 | static int emulator_pio_out_emulated(int size, unsigned short port, |
| 3714 | const void *val, unsigned int count, |
| 3715 | struct kvm_vcpu *vcpu) |
| 3716 | { |
| 3717 | trace_kvm_pio(0, port, size, 1); |
| 3718 | |
| 3719 | vcpu->arch.pio.port = port; |
| 3720 | vcpu->arch.pio.in = 0; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3721 | vcpu->arch.pio.count = count; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3722 | vcpu->arch.pio.size = size; |
| 3723 | |
| 3724 | memcpy(vcpu->arch.pio_data, val, size * count); |
| 3725 | |
| 3726 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 3727 | vcpu->arch.pio.count = 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3728 | return 1; |
| 3729 | } |
| 3730 | |
| 3731 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3732 | vcpu->run->io.direction = KVM_EXIT_IO_OUT; |
| 3733 | vcpu->run->io.size = size; |
| 3734 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 3735 | vcpu->run->io.count = count; |
| 3736 | vcpu->run->io.port = port; |
| 3737 | |
| 3738 | return 0; |
| 3739 | } |
| 3740 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3741 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3742 | { |
| 3743 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 3744 | } |
| 3745 | |
| 3746 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 3747 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3748 | kvm_mmu_invlpg(vcpu, address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3749 | return X86EMUL_CONTINUE; |
| 3750 | } |
| 3751 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3752 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 3753 | { |
| 3754 | if (!need_emulate_wbinvd(vcpu)) |
| 3755 | return X86EMUL_CONTINUE; |
| 3756 | |
| 3757 | if (kvm_x86_ops->has_wbinvd_exit()) { |
| 3758 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
| 3759 | wbinvd_ipi, NULL, 1); |
| 3760 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
| 3761 | } |
| 3762 | wbinvd(); |
| 3763 | return X86EMUL_CONTINUE; |
| 3764 | } |
| 3765 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
| 3766 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3767 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 3768 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3769 | 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] | 3770 | kvm_x86_ops->fpu_activate(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3771 | return X86EMUL_CONTINUE; |
| 3772 | } |
| 3773 | |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3774 | int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3775 | { |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3776 | return _kvm_get_dr(vcpu, dr, dest); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3777 | } |
| 3778 | |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3779 | int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3780 | { |
Gleb Natapov | 338dbc9 | 2010-04-28 19:15:32 +0300 | [diff] [blame] | 3781 | |
| 3782 | return __kvm_set_dr(vcpu, dr, value); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3785 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 3786 | { |
| 3787 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 3788 | } |
| 3789 | |
| 3790 | static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu) |
| 3791 | { |
| 3792 | unsigned long value; |
| 3793 | |
| 3794 | switch (cr) { |
| 3795 | case 0: |
| 3796 | value = kvm_read_cr0(vcpu); |
| 3797 | break; |
| 3798 | case 2: |
| 3799 | value = vcpu->arch.cr2; |
| 3800 | break; |
| 3801 | case 3: |
| 3802 | value = vcpu->arch.cr3; |
| 3803 | break; |
| 3804 | case 4: |
| 3805 | value = kvm_read_cr4(vcpu); |
| 3806 | break; |
| 3807 | case 8: |
| 3808 | value = kvm_get_cr8(vcpu); |
| 3809 | break; |
| 3810 | default: |
| 3811 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
| 3812 | return 0; |
| 3813 | } |
| 3814 | |
| 3815 | return value; |
| 3816 | } |
| 3817 | |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3818 | static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu) |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3819 | { |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3820 | int res = 0; |
| 3821 | |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3822 | switch (cr) { |
| 3823 | case 0: |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 3824 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3825 | break; |
| 3826 | case 2: |
| 3827 | vcpu->arch.cr2 = val; |
| 3828 | break; |
| 3829 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3830 | res = kvm_set_cr3(vcpu, val); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3831 | break; |
| 3832 | case 4: |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 3833 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3834 | break; |
| 3835 | case 8: |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3836 | res = __kvm_set_cr8(vcpu, val & 0xfUL); |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3837 | break; |
| 3838 | default: |
| 3839 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3840 | res = -1; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3841 | } |
Gleb Natapov | 0f12244 | 2010-04-28 19:15:31 +0300 | [diff] [blame] | 3842 | |
| 3843 | return res; |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 3846 | static int emulator_get_cpl(struct kvm_vcpu *vcpu) |
| 3847 | { |
| 3848 | return kvm_x86_ops->get_cpl(vcpu); |
| 3849 | } |
| 3850 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3851 | static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu) |
| 3852 | { |
| 3853 | kvm_x86_ops->get_gdt(vcpu, dt); |
| 3854 | } |
| 3855 | |
Mohammed Gamal | 160ce1f | 2010-08-04 05:44:24 +0300 | [diff] [blame] | 3856 | static void emulator_get_idt(struct desc_ptr *dt, struct kvm_vcpu *vcpu) |
| 3857 | { |
| 3858 | kvm_x86_ops->get_idt(vcpu, dt); |
| 3859 | } |
| 3860 | |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 3861 | static unsigned long emulator_get_cached_segment_base(int seg, |
| 3862 | struct kvm_vcpu *vcpu) |
| 3863 | { |
| 3864 | return get_segment_base(vcpu, seg); |
| 3865 | } |
| 3866 | |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3867 | static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg, |
| 3868 | struct kvm_vcpu *vcpu) |
| 3869 | { |
| 3870 | struct kvm_segment var; |
| 3871 | |
| 3872 | kvm_get_segment(vcpu, &var, seg); |
| 3873 | |
| 3874 | if (var.unusable) |
| 3875 | return false; |
| 3876 | |
| 3877 | if (var.g) |
| 3878 | var.limit >>= 12; |
| 3879 | set_desc_limit(desc, var.limit); |
| 3880 | set_desc_base(desc, (unsigned long)var.base); |
| 3881 | desc->type = var.type; |
| 3882 | desc->s = var.s; |
| 3883 | desc->dpl = var.dpl; |
| 3884 | desc->p = var.present; |
| 3885 | desc->avl = var.avl; |
| 3886 | desc->l = var.l; |
| 3887 | desc->d = var.db; |
| 3888 | desc->g = var.g; |
| 3889 | |
| 3890 | return true; |
| 3891 | } |
| 3892 | |
| 3893 | static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg, |
| 3894 | struct kvm_vcpu *vcpu) |
| 3895 | { |
| 3896 | struct kvm_segment var; |
| 3897 | |
| 3898 | /* needed to preserve selector */ |
| 3899 | kvm_get_segment(vcpu, &var, seg); |
| 3900 | |
| 3901 | var.base = get_desc_base(desc); |
| 3902 | var.limit = get_desc_limit(desc); |
| 3903 | if (desc->g) |
| 3904 | var.limit = (var.limit << 12) | 0xfff; |
| 3905 | var.type = desc->type; |
| 3906 | var.present = desc->p; |
| 3907 | var.dpl = desc->dpl; |
| 3908 | var.db = desc->d; |
| 3909 | var.s = desc->s; |
| 3910 | var.l = desc->l; |
| 3911 | var.g = desc->g; |
| 3912 | var.avl = desc->avl; |
| 3913 | var.present = desc->p; |
| 3914 | var.unusable = !var.present; |
| 3915 | var.padding = 0; |
| 3916 | |
| 3917 | kvm_set_segment(vcpu, &var, seg); |
| 3918 | return; |
| 3919 | } |
| 3920 | |
| 3921 | static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu) |
| 3922 | { |
| 3923 | struct kvm_segment kvm_seg; |
| 3924 | |
| 3925 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 3926 | return kvm_seg.selector; |
| 3927 | } |
| 3928 | |
| 3929 | static void emulator_set_segment_selector(u16 sel, int seg, |
| 3930 | struct kvm_vcpu *vcpu) |
| 3931 | { |
| 3932 | struct kvm_segment kvm_seg; |
| 3933 | |
| 3934 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 3935 | kvm_seg.selector = sel; |
| 3936 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 3937 | } |
| 3938 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3939 | static struct x86_emulate_ops emulate_ops = { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3940 | .read_std = kvm_read_guest_virt_system, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3941 | .write_std = kvm_write_guest_virt_system, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3942 | .fetch = kvm_fetch_guest_virt, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3943 | .read_emulated = emulator_read_emulated, |
| 3944 | .write_emulated = emulator_write_emulated, |
| 3945 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3946 | .pio_in_emulated = emulator_pio_in_emulated, |
| 3947 | .pio_out_emulated = emulator_pio_out_emulated, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3948 | .get_cached_descriptor = emulator_get_cached_descriptor, |
| 3949 | .set_cached_descriptor = emulator_set_cached_descriptor, |
| 3950 | .get_segment_selector = emulator_get_segment_selector, |
| 3951 | .set_segment_selector = emulator_set_segment_selector, |
Gleb Natapov | 5951c44 | 2010-04-28 19:15:29 +0300 | [diff] [blame] | 3952 | .get_cached_segment_base = emulator_get_cached_segment_base, |
Gleb Natapov | 2dafc6c | 2010-03-18 15:20:16 +0200 | [diff] [blame] | 3953 | .get_gdt = emulator_get_gdt, |
Mohammed Gamal | 160ce1f | 2010-08-04 05:44:24 +0300 | [diff] [blame] | 3954 | .get_idt = emulator_get_idt, |
Gleb Natapov | 52a4661 | 2010-03-18 15:20:03 +0200 | [diff] [blame] | 3955 | .get_cr = emulator_get_cr, |
| 3956 | .set_cr = emulator_set_cr, |
Gleb Natapov | 9c53724 | 2010-03-18 15:20:05 +0200 | [diff] [blame] | 3957 | .cpl = emulator_get_cpl, |
Gleb Natapov | 35aa537 | 2010-04-28 19:15:27 +0300 | [diff] [blame] | 3958 | .get_dr = emulator_get_dr, |
| 3959 | .set_dr = emulator_set_dr, |
Gleb Natapov | 3fb1b5d | 2010-04-28 19:15:28 +0300 | [diff] [blame] | 3960 | .set_msr = kvm_set_msr, |
| 3961 | .get_msr = kvm_get_msr, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3962 | }; |
| 3963 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3964 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 3965 | { |
| 3966 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3967 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3968 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 3969 | vcpu->arch.regs_dirty = ~0; |
| 3970 | } |
| 3971 | |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 3972 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
| 3973 | { |
| 3974 | u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu, mask); |
| 3975 | /* |
| 3976 | * an sti; sti; sequence only disable interrupts for the first |
| 3977 | * instruction. So, if the last instruction, be it emulated or |
| 3978 | * not, left the system with the INT_STI flag enabled, it |
| 3979 | * means that the last instruction is an sti. We should not |
| 3980 | * leave the flag on in this case. The same goes for mov ss |
| 3981 | */ |
| 3982 | if (!(int_shadow & mask)) |
| 3983 | kvm_x86_ops->set_interrupt_shadow(vcpu, mask); |
| 3984 | } |
| 3985 | |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 3986 | static void inject_emulated_exception(struct kvm_vcpu *vcpu) |
| 3987 | { |
| 3988 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 3989 | if (ctxt->exception == PF_VECTOR) |
| 3990 | kvm_inject_page_fault(vcpu, ctxt->cr2, ctxt->error_code); |
| 3991 | else if (ctxt->error_code_valid) |
| 3992 | kvm_queue_exception_e(vcpu, ctxt->exception, ctxt->error_code); |
| 3993 | else |
| 3994 | kvm_queue_exception(vcpu, ctxt->exception); |
| 3995 | } |
| 3996 | |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 3997 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
| 3998 | { |
| 3999 | struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode; |
| 4000 | int cs_db, cs_l; |
| 4001 | |
| 4002 | cache_all_regs(vcpu); |
| 4003 | |
| 4004 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 4005 | |
| 4006 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
| 4007 | vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu); |
| 4008 | vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu); |
| 4009 | vcpu->arch.emulate_ctxt.mode = |
| 4010 | (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
| 4011 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
| 4012 | ? X86EMUL_MODE_VM86 : cs_l |
| 4013 | ? X86EMUL_MODE_PROT64 : cs_db |
| 4014 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 4015 | memset(c, 0, sizeof(struct decode_cache)); |
| 4016 | memcpy(c->regs, vcpu->arch.regs, sizeof c->regs); |
| 4017 | } |
| 4018 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4019 | static int handle_emulation_failure(struct kvm_vcpu *vcpu) |
| 4020 | { |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4021 | ++vcpu->stat.insn_emulation_fail; |
| 4022 | trace_kvm_emulate_insn_failed(vcpu); |
| 4023 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4024 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4025 | vcpu->run->internal.ndata = 0; |
| 4026 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4027 | return EMULATE_FAIL; |
| 4028 | } |
| 4029 | |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 4030 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva) |
| 4031 | { |
| 4032 | gpa_t gpa; |
| 4033 | |
Gleb Natapov | 68be080 | 2010-07-14 19:05:45 +0300 | [diff] [blame] | 4034 | if (tdp_enabled) |
| 4035 | return false; |
| 4036 | |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 4037 | /* |
| 4038 | * if emulation was due to access to shadowed page table |
| 4039 | * and it failed try to unshadow page and re-entetr the |
| 4040 | * guest to let CPU execute the instruction. |
| 4041 | */ |
| 4042 | if (kvm_mmu_unprotect_page_virt(vcpu, gva)) |
| 4043 | return true; |
| 4044 | |
| 4045 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL); |
| 4046 | |
| 4047 | if (gpa == UNMAPPED_GVA) |
| 4048 | return true; /* let cpu generate fault */ |
| 4049 | |
| 4050 | if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT))) |
| 4051 | return true; |
| 4052 | |
| 4053 | return false; |
| 4054 | } |
| 4055 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4056 | int emulate_instruction(struct kvm_vcpu *vcpu, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4057 | unsigned long cr2, |
| 4058 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4059 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4060 | { |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 4061 | int r; |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 4062 | struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4063 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 4064 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4065 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4066 | /* |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 4067 | * TODO: fix emulate.c to use guest_read/write_register |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4068 | * instead of direct ->regs accesses, can save hundred cycles |
| 4069 | * on Intel for instructions that don't read/change RSP, for |
| 4070 | * for example. |
| 4071 | */ |
| 4072 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4073 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4074 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 4075 | init_emulate_ctxt(vcpu); |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 4076 | vcpu->arch.emulate_ctxt.interruptibility = 0; |
Gleb Natapov | 54b8486 | 2010-04-28 19:15:44 +0300 | [diff] [blame] | 4077 | vcpu->arch.emulate_ctxt.exception = -1; |
Gleb Natapov | 4fc40f0 | 2010-08-02 12:47:51 +0300 | [diff] [blame] | 4078 | vcpu->arch.emulate_ctxt.perm_ok = false; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4079 | |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 4080 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt); |
Avi Kivity | e46479f | 2010-04-11 13:05:16 +0300 | [diff] [blame] | 4081 | trace_kvm_emulate_insn_start(vcpu); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4082 | |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 4083 | /* Only allow emulation of specific instructions on #UD |
| 4084 | * (namely VMMCALL, sysenter, sysexit, syscall)*/ |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 4085 | if (emulation_type & EMULTYPE_TRAP_UD) { |
| 4086 | if (!c->twobyte) |
| 4087 | return EMULATE_FAIL; |
| 4088 | switch (c->b) { |
| 4089 | case 0x01: /* VMMCALL */ |
| 4090 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
| 4091 | return EMULATE_FAIL; |
| 4092 | break; |
| 4093 | case 0x34: /* sysenter */ |
| 4094 | case 0x35: /* sysexit */ |
| 4095 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 4096 | return EMULATE_FAIL; |
| 4097 | break; |
| 4098 | case 0x05: /* syscall */ |
| 4099 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 4100 | return EMULATE_FAIL; |
| 4101 | break; |
| 4102 | default: |
| 4103 | return EMULATE_FAIL; |
| 4104 | } |
| 4105 | |
| 4106 | if (!(c->modrm_reg == 0 || c->modrm_reg == 3)) |
| 4107 | return EMULATE_FAIL; |
| 4108 | } |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4109 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 4110 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4111 | if (r) { |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 4112 | if (reexecute_instruction(vcpu, cr2)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4113 | return EMULATE_DONE; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4114 | if (emulation_type & EMULTYPE_SKIP) |
| 4115 | return EMULATE_FAIL; |
| 4116 | return handle_emulation_failure(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 4120 | if (emulation_type & EMULTYPE_SKIP) { |
| 4121 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip); |
| 4122 | return EMULATE_DONE; |
| 4123 | } |
| 4124 | |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 4125 | /* this is needed for vmware backdor interface to work since it |
| 4126 | changes registers values during IO operation */ |
| 4127 | memcpy(c->regs, vcpu->arch.regs, sizeof c->regs); |
| 4128 | |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 4129 | restart: |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 4130 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt); |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 4131 | |
| 4132 | if (r) { /* emulation failed */ |
Gleb Natapov | a6f177e | 2010-07-08 12:41:12 +0300 | [diff] [blame] | 4133 | if (reexecute_instruction(vcpu, cr2)) |
Gleb Natapov | c3cd7ff | 2010-04-28 19:15:35 +0300 | [diff] [blame] | 4134 | return EMULATE_DONE; |
| 4135 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4136 | return handle_emulation_failure(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4137 | } |
| 4138 | |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4139 | r = EMULATE_DONE; |
| 4140 | |
| 4141 | if (vcpu->arch.emulate_ctxt.exception >= 0) |
| 4142 | inject_emulated_exception(vcpu); |
| 4143 | else if (vcpu->arch.pio.count) { |
| 4144 | if (!vcpu->arch.pio.in) |
| 4145 | vcpu->arch.pio.count = 0; |
| 4146 | r = EMULATE_DO_MMIO; |
| 4147 | } else if (vcpu->mmio_needed) { |
| 4148 | if (vcpu->mmio_is_write) |
| 4149 | vcpu->mmio_needed = 0; |
| 4150 | r = EMULATE_DO_MMIO; |
| 4151 | } else if (vcpu->arch.emulate_ctxt.restart) |
| 4152 | goto restart; |
| 4153 | |
Gleb Natapov | 95cb229 | 2010-04-28 19:15:43 +0300 | [diff] [blame] | 4154 | toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility); |
Gleb Natapov | ef050dc | 2010-04-28 19:15:40 +0300 | [diff] [blame] | 4155 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 4156 | memcpy(vcpu->arch.regs, c->regs, sizeof c->regs); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 4157 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip); |
Gleb Natapov | 3457e41 | 2010-04-28 19:15:38 +0300 | [diff] [blame] | 4158 | |
Gleb Natapov | e85d28f | 2010-07-29 15:11:52 +0300 | [diff] [blame] | 4159 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 4160 | } |
| 4161 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 4162 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4163 | int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 4164 | { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4165 | unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4166 | int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu); |
| 4167 | /* do not return to emulator after return from userspace */ |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 4168 | vcpu->arch.pio.count = 0; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 4169 | return ret; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 4170 | } |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4171 | EXPORT_SYMBOL_GPL(kvm_fast_pio_out); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 4172 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4173 | static void tsc_bad(void *info) |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4174 | { |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4175 | __get_cpu_var(cpu_tsc_khz) = 0; |
| 4176 | } |
| 4177 | |
| 4178 | static void tsc_khz_changed(void *data) |
| 4179 | { |
| 4180 | struct cpufreq_freqs *freq = data; |
| 4181 | unsigned long khz = 0; |
| 4182 | |
| 4183 | if (data) |
| 4184 | khz = freq->new; |
| 4185 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 4186 | khz = cpufreq_quick_get(raw_smp_processor_id()); |
| 4187 | if (!khz) |
| 4188 | khz = tsc_khz; |
| 4189 | __get_cpu_var(cpu_tsc_khz) = khz; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4190 | } |
| 4191 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4192 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 4193 | void *data) |
| 4194 | { |
| 4195 | struct cpufreq_freqs *freq = data; |
| 4196 | struct kvm *kvm; |
| 4197 | struct kvm_vcpu *vcpu; |
| 4198 | int i, send_ipi = 0; |
| 4199 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4200 | /* |
| 4201 | * We allow guests to temporarily run on slowing clocks, |
| 4202 | * provided we notify them after, or to run on accelerating |
| 4203 | * clocks, provided we notify them before. Thus time never |
| 4204 | * goes backwards. |
| 4205 | * |
| 4206 | * However, we have a problem. We can't atomically update |
| 4207 | * the frequency of a given CPU from this function; it is |
| 4208 | * merely a notifier, which can be called from any CPU. |
| 4209 | * Changing the TSC frequency at arbitrary points in time |
| 4210 | * requires a recomputation of local variables related to |
| 4211 | * the TSC for each VCPU. We must flag these local variables |
| 4212 | * to be updated and be sure the update takes place with the |
| 4213 | * new frequency before any guests proceed. |
| 4214 | * |
| 4215 | * Unfortunately, the combination of hotplug CPU and frequency |
| 4216 | * change creates an intractable locking scenario; the order |
| 4217 | * of when these callouts happen is undefined with respect to |
| 4218 | * CPU hotplug, and they can race with each other. As such, |
| 4219 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is |
| 4220 | * undefined; you can actually have a CPU frequency change take |
| 4221 | * place in between the computation of X and the setting of the |
| 4222 | * variable. To protect against this problem, all updates of |
| 4223 | * the per_cpu tsc_khz variable are done in an interrupt |
| 4224 | * protected IPI, and all callers wishing to update the value |
| 4225 | * must wait for a synchronous IPI to complete (which is trivial |
| 4226 | * if the caller is on the CPU already). This establishes the |
| 4227 | * necessary total order on variable updates. |
| 4228 | * |
| 4229 | * Note that because a guest time update may take place |
| 4230 | * anytime after the setting of the VCPU's request bit, the |
| 4231 | * correct TSC value must be set before the request. However, |
| 4232 | * to ensure the update actually makes it to any guest which |
| 4233 | * starts running in hardware virtualization between the set |
| 4234 | * and the acquisition of the spinlock, we must also ping the |
| 4235 | * CPU after setting the request bit. |
| 4236 | * |
| 4237 | */ |
| 4238 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4239 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 4240 | return 0; |
| 4241 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 4242 | return 0; |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4243 | |
| 4244 | smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4245 | |
| 4246 | spin_lock(&kvm_lock); |
| 4247 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 4248 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4249 | if (vcpu->cpu != freq->cpu) |
| 4250 | continue; |
| 4251 | if (!kvm_request_guest_time_update(vcpu)) |
| 4252 | continue; |
| 4253 | if (vcpu->cpu != smp_processor_id()) |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4254 | send_ipi = 1; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | spin_unlock(&kvm_lock); |
| 4258 | |
| 4259 | if (freq->old < freq->new && send_ipi) { |
| 4260 | /* |
| 4261 | * We upscale the frequency. Must make the guest |
| 4262 | * doesn't see old kvmclock values while running with |
| 4263 | * the new frequency, otherwise we risk the guest sees |
| 4264 | * time go backwards. |
| 4265 | * |
| 4266 | * In case we update the frequency for another cpu |
| 4267 | * (which might be in guest context) send an interrupt |
| 4268 | * to kick the cpu out of guest context. Next time |
| 4269 | * guest context is entered kvmclock will be updated, |
| 4270 | * so the guest will not see stale values. |
| 4271 | */ |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4272 | smp_call_function_single(freq->cpu, tsc_khz_changed, freq, 1); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4273 | } |
| 4274 | return 0; |
| 4275 | } |
| 4276 | |
| 4277 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4278 | .notifier_call = kvmclock_cpufreq_notifier |
| 4279 | }; |
| 4280 | |
| 4281 | static int kvmclock_cpu_notifier(struct notifier_block *nfb, |
| 4282 | unsigned long action, void *hcpu) |
| 4283 | { |
| 4284 | unsigned int cpu = (unsigned long)hcpu; |
| 4285 | |
| 4286 | switch (action) { |
| 4287 | case CPU_ONLINE: |
| 4288 | case CPU_DOWN_FAILED: |
| 4289 | smp_call_function_single(cpu, tsc_khz_changed, NULL, 1); |
| 4290 | break; |
| 4291 | case CPU_DOWN_PREPARE: |
| 4292 | smp_call_function_single(cpu, tsc_bad, NULL, 1); |
| 4293 | break; |
| 4294 | } |
| 4295 | return NOTIFY_OK; |
| 4296 | } |
| 4297 | |
| 4298 | static struct notifier_block kvmclock_cpu_notifier_block = { |
| 4299 | .notifier_call = kvmclock_cpu_notifier, |
| 4300 | .priority = -INT_MAX |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4301 | }; |
| 4302 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4303 | static void kvm_timer_init(void) |
| 4304 | { |
| 4305 | int cpu; |
| 4306 | |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4307 | register_hotcpu_notifier(&kvmclock_cpu_notifier_block); |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4308 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4309 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 4310 | CPUFREQ_TRANSITION_NOTIFIER); |
| 4311 | } |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4312 | for_each_online_cpu(cpu) |
| 4313 | smp_call_function_single(cpu, tsc_khz_changed, NULL, 1); |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4314 | } |
| 4315 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4316 | static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
| 4317 | |
| 4318 | static int kvm_is_in_guest(void) |
| 4319 | { |
| 4320 | return percpu_read(current_vcpu) != NULL; |
| 4321 | } |
| 4322 | |
| 4323 | static int kvm_is_user_mode(void) |
| 4324 | { |
| 4325 | int user_mode = 3; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4326 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4327 | if (percpu_read(current_vcpu)) |
| 4328 | user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4329 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4330 | return user_mode != 0; |
| 4331 | } |
| 4332 | |
| 4333 | static unsigned long kvm_get_guest_ip(void) |
| 4334 | { |
| 4335 | unsigned long ip = 0; |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4336 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4337 | if (percpu_read(current_vcpu)) |
| 4338 | ip = kvm_rip_read(percpu_read(current_vcpu)); |
Zhang, Yanmin | dcf46b9 | 2010-04-20 10:13:58 +0800 | [diff] [blame] | 4339 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4340 | return ip; |
| 4341 | } |
| 4342 | |
| 4343 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
| 4344 | .is_in_guest = kvm_is_in_guest, |
| 4345 | .is_user_mode = kvm_is_user_mode, |
| 4346 | .get_guest_ip = kvm_get_guest_ip, |
| 4347 | }; |
| 4348 | |
| 4349 | void kvm_before_handle_nmi(struct kvm_vcpu *vcpu) |
| 4350 | { |
| 4351 | percpu_write(current_vcpu, vcpu); |
| 4352 | } |
| 4353 | EXPORT_SYMBOL_GPL(kvm_before_handle_nmi); |
| 4354 | |
| 4355 | void kvm_after_handle_nmi(struct kvm_vcpu *vcpu) |
| 4356 | { |
| 4357 | percpu_write(current_vcpu, NULL); |
| 4358 | } |
| 4359 | EXPORT_SYMBOL_GPL(kvm_after_handle_nmi); |
| 4360 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4361 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 4362 | { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4363 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4364 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 4365 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4366 | if (kvm_x86_ops) { |
| 4367 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4368 | r = -EEXIST; |
| 4369 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | if (!ops->cpu_has_kvm_support()) { |
| 4373 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4374 | r = -EOPNOTSUPP; |
| 4375 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4376 | } |
| 4377 | if (ops->disabled_by_bios()) { |
| 4378 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4379 | r = -EOPNOTSUPP; |
| 4380 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4381 | } |
| 4382 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 4383 | r = kvm_mmu_module_init(); |
| 4384 | if (r) |
| 4385 | goto out; |
| 4386 | |
| 4387 | kvm_init_msr_list(); |
| 4388 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4389 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4390 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 4391 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 4392 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4393 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4394 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 4395 | kvm_timer_init(); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4396 | |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4397 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
| 4398 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4399 | if (cpu_has_xsave) |
| 4400 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
| 4401 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4402 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4403 | |
| 4404 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4405 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 4406 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4407 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4408 | void kvm_arch_exit(void) |
| 4409 | { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 4410 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
| 4411 | |
Jan Kiszka | 888d256 | 2009-04-17 19:24:58 +0200 | [diff] [blame] | 4412 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 4413 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 4414 | CPUFREQ_TRANSITION_NOTIFIER); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4415 | unregister_hotcpu_notifier(&kvmclock_cpu_notifier_block); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4416 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 4417 | kvm_mmu_module_exit(); |
| 4418 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 4419 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4420 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 4421 | { |
| 4422 | ++vcpu->stat.halt_exits; |
| 4423 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 4424 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4425 | return 1; |
| 4426 | } else { |
| 4427 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 4428 | return 0; |
| 4429 | } |
| 4430 | } |
| 4431 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 4432 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4433 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 4434 | unsigned long a1) |
| 4435 | { |
| 4436 | if (is_long_mode(vcpu)) |
| 4437 | return a0; |
| 4438 | else |
| 4439 | return a0 | ((gpa_t)a1 << 32); |
| 4440 | } |
| 4441 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4442 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 4443 | { |
| 4444 | u64 param, ingpa, outgpa, ret; |
| 4445 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 4446 | bool fast, longmode; |
| 4447 | int cs_db, cs_l; |
| 4448 | |
| 4449 | /* |
| 4450 | * hypercall generates UD from non zero cpl and real mode |
| 4451 | * per HYPER-V spec |
| 4452 | */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 4453 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4454 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4455 | return 0; |
| 4456 | } |
| 4457 | |
| 4458 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 4459 | longmode = is_long_mode(vcpu) && cs_l == 1; |
| 4460 | |
| 4461 | if (!longmode) { |
Gleb Natapov | ccd4693 | 2010-01-19 15:06:38 +0200 | [diff] [blame] | 4462 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 4463 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 4464 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 4465 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 4466 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 4467 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4468 | } |
| 4469 | #ifdef CONFIG_X86_64 |
| 4470 | else { |
| 4471 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4472 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4473 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 4474 | } |
| 4475 | #endif |
| 4476 | |
| 4477 | code = param & 0xffff; |
| 4478 | fast = (param >> 16) & 0x1; |
| 4479 | rep_cnt = (param >> 32) & 0xfff; |
| 4480 | rep_idx = (param >> 48) & 0xfff; |
| 4481 | |
| 4482 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 4483 | |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 4484 | switch (code) { |
| 4485 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 4486 | kvm_vcpu_on_spin(vcpu); |
| 4487 | break; |
| 4488 | default: |
| 4489 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 4490 | break; |
| 4491 | } |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4492 | |
| 4493 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 4494 | if (longmode) { |
| 4495 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 4496 | } else { |
| 4497 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 4498 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 4499 | } |
| 4500 | |
| 4501 | return 1; |
| 4502 | } |
| 4503 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4504 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 4505 | { |
| 4506 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4507 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4508 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 4509 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 4510 | return kvm_hv_hypercall(vcpu); |
| 4511 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4512 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4513 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4514 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4515 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4516 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4517 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4518 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4519 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4520 | if (!is_long_mode(vcpu)) { |
| 4521 | nr &= 0xFFFFFFFF; |
| 4522 | a0 &= 0xFFFFFFFF; |
| 4523 | a1 &= 0xFFFFFFFF; |
| 4524 | a2 &= 0xFFFFFFFF; |
| 4525 | a3 &= 0xFFFFFFFF; |
| 4526 | } |
| 4527 | |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 4528 | if (kvm_x86_ops->get_cpl(vcpu) != 0) { |
| 4529 | ret = -KVM_EPERM; |
| 4530 | goto out; |
| 4531 | } |
| 4532 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4533 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4534 | case KVM_HC_VAPIC_POLL_IRQ: |
| 4535 | ret = 0; |
| 4536 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4537 | case KVM_HC_MMU_OP: |
| 4538 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 4539 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4540 | default: |
| 4541 | ret = -KVM_ENOSYS; |
| 4542 | break; |
| 4543 | } |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 4544 | out: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4545 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 4546 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 4547 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4548 | } |
| 4549 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 4550 | |
| 4551 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 4552 | { |
| 4553 | char instruction[3]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4554 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4555 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4556 | /* |
| 4557 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 4558 | * to ensure that the updated hypercall appears atomically across all |
| 4559 | * VCPUs. |
| 4560 | */ |
| 4561 | kvm_mmu_zap_all(vcpu->kvm); |
| 4562 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4563 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4564 | |
Gleb Natapov | 8fe681e | 2010-04-28 19:15:37 +0300 | [diff] [blame] | 4565 | return emulator_write_emulated(rip, instruction, 3, NULL, vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4566 | } |
| 4567 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4568 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4569 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4570 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4571 | |
| 4572 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 4573 | } |
| 4574 | |
| 4575 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4576 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4577 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4578 | |
| 4579 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 4580 | } |
| 4581 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4582 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 4583 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4584 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 4585 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4586 | |
| 4587 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4588 | /* 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] | 4589 | for (j = i + 1; ; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4590 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4591 | if (ej->function == e->function) { |
| 4592 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4593 | return j; |
| 4594 | } |
| 4595 | } |
| 4596 | return 0; /* silence gcc, even though control never reaches here */ |
| 4597 | } |
| 4598 | |
| 4599 | /* find an entry with matching function, matching index (if needed), and that |
| 4600 | * should be read next (if it's stateful) */ |
| 4601 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 4602 | u32 function, u32 index) |
| 4603 | { |
| 4604 | if (e->function != function) |
| 4605 | return 0; |
| 4606 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 4607 | return 0; |
| 4608 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4609 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4610 | return 0; |
| 4611 | return 1; |
| 4612 | } |
| 4613 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4614 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 4615 | u32 function, u32 index) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4616 | { |
| 4617 | int i; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4618 | struct kvm_cpuid_entry2 *best = NULL; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4619 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4620 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4621 | struct kvm_cpuid_entry2 *e; |
| 4622 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4623 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4624 | if (is_matching_cpuid_entry(e, function, index)) { |
| 4625 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 4626 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4627 | best = e; |
| 4628 | break; |
| 4629 | } |
| 4630 | /* |
| 4631 | * Both basic or both extended? |
| 4632 | */ |
| 4633 | if (((e->function ^ function) & 0x80000000) == 0) |
| 4634 | if (!best || e->function > best->function) |
| 4635 | best = e; |
| 4636 | } |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4637 | return best; |
| 4638 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4639 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4640 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4641 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
| 4642 | { |
| 4643 | struct kvm_cpuid_entry2 *best; |
| 4644 | |
Avi Kivity | f7a7119 | 2010-04-11 15:33:32 +0300 | [diff] [blame] | 4645 | best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0); |
| 4646 | if (!best || best->eax < 0x80000008) |
| 4647 | goto not_found; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4648 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 4649 | if (best) |
| 4650 | return best->eax & 0xff; |
Avi Kivity | f7a7119 | 2010-04-11 15:33:32 +0300 | [diff] [blame] | 4651 | not_found: |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4652 | return 36; |
| 4653 | } |
| 4654 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4655 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 4656 | { |
| 4657 | u32 function, index; |
| 4658 | struct kvm_cpuid_entry2 *best; |
| 4659 | |
| 4660 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4661 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4662 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 4663 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 4664 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 4665 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
| 4666 | best = kvm_find_cpuid_entry(vcpu, function, index); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4667 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4668 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 4669 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 4670 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 4671 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4672 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4673 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4674 | trace_kvm_cpuid(function, |
| 4675 | kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 4676 | kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 4677 | kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 4678 | kvm_register_read(vcpu, VCPU_REGS_RDX)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4679 | } |
| 4680 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4681 | |
| 4682 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4683 | * Check if userspace requested an interrupt window, and that the |
| 4684 | * interrupt window is open. |
| 4685 | * |
| 4686 | * No need to exit to userspace if we already have an interrupt queued. |
| 4687 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4688 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4689 | { |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4690 | return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4691 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 5df5664 | 2009-04-21 17:44:59 +0300 | [diff] [blame] | 4692 | kvm_arch_interrupt_allowed(vcpu)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4693 | } |
| 4694 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4695 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4696 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4697 | struct kvm_run *kvm_run = vcpu->run; |
| 4698 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4699 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4700 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4701 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4702 | if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4703 | kvm_run->ready_for_interrupt_injection = 1; |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4704 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4705 | kvm_run->ready_for_interrupt_injection = |
Gleb Natapov | fa9726b | 2009-05-11 13:35:47 +0300 | [diff] [blame] | 4706 | kvm_arch_interrupt_allowed(vcpu) && |
| 4707 | !kvm_cpu_has_interrupt(vcpu) && |
| 4708 | !kvm_event_needs_reinjection(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4709 | } |
| 4710 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4711 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 4712 | { |
| 4713 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 4714 | struct page *page; |
| 4715 | |
| 4716 | if (!apic || !apic->vapic_addr) |
| 4717 | return; |
| 4718 | |
| 4719 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4720 | |
| 4721 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 4725 | { |
| 4726 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4727 | int idx; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4728 | |
| 4729 | if (!apic || !apic->vapic_addr) |
| 4730 | return; |
| 4731 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4732 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4733 | kvm_release_page_dirty(apic->vapic_page); |
| 4734 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4735 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4736 | } |
| 4737 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4738 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 4739 | { |
| 4740 | int max_irr, tpr; |
| 4741 | |
| 4742 | if (!kvm_x86_ops->update_cr8_intercept) |
| 4743 | return; |
| 4744 | |
Avi Kivity | 88c808f | 2009-08-17 22:49:40 +0300 | [diff] [blame] | 4745 | if (!vcpu->arch.apic) |
| 4746 | return; |
| 4747 | |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4748 | if (!vcpu->arch.apic->vapic_addr) |
| 4749 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 4750 | else |
| 4751 | max_irr = -1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4752 | |
| 4753 | if (max_irr != -1) |
| 4754 | max_irr >>= 4; |
| 4755 | |
| 4756 | tpr = kvm_lapic_get_cr8(vcpu); |
| 4757 | |
| 4758 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); |
| 4759 | } |
| 4760 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4761 | static void inject_pending_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4762 | { |
| 4763 | /* try to reinject previous events if any */ |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4764 | if (vcpu->arch.exception.pending) { |
Avi Kivity | 5c1c85d0 | 2010-03-11 13:01:59 +0200 | [diff] [blame] | 4765 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
| 4766 | vcpu->arch.exception.has_error_code, |
| 4767 | vcpu->arch.exception.error_code); |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4768 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 4769 | vcpu->arch.exception.has_error_code, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 4770 | vcpu->arch.exception.error_code, |
| 4771 | vcpu->arch.exception.reinject); |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4772 | return; |
| 4773 | } |
| 4774 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4775 | if (vcpu->arch.nmi_injected) { |
| 4776 | kvm_x86_ops->set_nmi(vcpu); |
| 4777 | return; |
| 4778 | } |
| 4779 | |
| 4780 | if (vcpu->arch.interrupt.pending) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4781 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4782 | return; |
| 4783 | } |
| 4784 | |
| 4785 | /* try to inject new event if pending */ |
| 4786 | if (vcpu->arch.nmi_pending) { |
| 4787 | if (kvm_x86_ops->nmi_allowed(vcpu)) { |
| 4788 | vcpu->arch.nmi_pending = false; |
| 4789 | vcpu->arch.nmi_injected = true; |
| 4790 | kvm_x86_ops->set_nmi(vcpu); |
| 4791 | } |
| 4792 | } else if (kvm_cpu_has_interrupt(vcpu)) { |
| 4793 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4794 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
| 4795 | false); |
| 4796 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4797 | } |
| 4798 | } |
| 4799 | } |
| 4800 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4801 | static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu) |
| 4802 | { |
| 4803 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) && |
| 4804 | !vcpu->guest_xcr0_loaded) { |
| 4805 | /* kvm_set_xcr() also depends on this */ |
| 4806 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); |
| 4807 | vcpu->guest_xcr0_loaded = 1; |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) |
| 4812 | { |
| 4813 | if (vcpu->guest_xcr0_loaded) { |
| 4814 | if (vcpu->arch.xcr0 != host_xcr0) |
| 4815 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); |
| 4816 | vcpu->guest_xcr0_loaded = 0; |
| 4817 | } |
| 4818 | } |
| 4819 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4820 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4821 | { |
| 4822 | int r; |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4823 | bool req_int_win = !irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4824 | vcpu->run->request_interrupt_window; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4825 | |
Avi Kivity | 3e00750 | 2010-06-23 14:26:18 +0300 | [diff] [blame] | 4826 | if (vcpu->requests) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4827 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 4828 | kvm_mmu_unload(vcpu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4829 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 4830 | __kvm_migrate_timers(vcpu); |
Zachary Amsden | 8cfdc00 | 2010-08-19 22:07:21 -1000 | [diff] [blame] | 4831 | if (kvm_check_request(KVM_REQ_KVMCLOCK_UPDATE, vcpu)) { |
| 4832 | r = kvm_write_guest_time(vcpu); |
| 4833 | if (unlikely(r)) |
| 4834 | goto out; |
| 4835 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4836 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 4837 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4838 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 4839 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4840 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4841 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4842 | r = 0; |
| 4843 | goto out; |
| 4844 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4845 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4846 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4847 | r = 0; |
| 4848 | goto out; |
| 4849 | } |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4850 | if (kvm_check_request(KVM_REQ_DEACTIVATE_FPU, vcpu)) { |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4851 | vcpu->fpu_active = 0; |
| 4852 | kvm_x86_ops->fpu_deactivate(vcpu); |
| 4853 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4854 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4855 | |
Avi Kivity | 3e00750 | 2010-06-23 14:26:18 +0300 | [diff] [blame] | 4856 | r = kvm_mmu_reload(vcpu); |
| 4857 | if (unlikely(r)) |
| 4858 | goto out; |
| 4859 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4860 | preempt_disable(); |
| 4861 | |
| 4862 | kvm_x86_ops->prepare_guest_switch(vcpu); |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 4863 | if (vcpu->fpu_active) |
| 4864 | kvm_load_guest_fpu(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4865 | kvm_load_guest_xcr0(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4866 | |
Avi Kivity | d94e1dc | 2010-05-03 16:54:48 +0300 | [diff] [blame] | 4867 | atomic_set(&vcpu->guest_mode, 1); |
| 4868 | smp_wmb(); |
| 4869 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4870 | local_irq_disable(); |
| 4871 | |
Avi Kivity | d94e1dc | 2010-05-03 16:54:48 +0300 | [diff] [blame] | 4872 | if (!atomic_read(&vcpu->guest_mode) || vcpu->requests |
| 4873 | || need_resched() || signal_pending(current)) { |
| 4874 | atomic_set(&vcpu->guest_mode, 0); |
| 4875 | smp_wmb(); |
Avi Kivity | 6c142801 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 4876 | local_irq_enable(); |
| 4877 | preempt_enable(); |
| 4878 | r = 1; |
| 4879 | goto out; |
| 4880 | } |
| 4881 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4882 | inject_pending_event(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4883 | |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4884 | /* enable NMI/IRQ window open exits if needed */ |
| 4885 | if (vcpu->arch.nmi_pending) |
| 4886 | kvm_x86_ops->enable_nmi_window(vcpu); |
| 4887 | else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) |
| 4888 | kvm_x86_ops->enable_irq_window(vcpu); |
| 4889 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4890 | if (kvm_lapic_enabled(vcpu)) { |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4891 | update_cr8_intercept(vcpu); |
| 4892 | kvm_lapic_sync_to_vapic(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4893 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4894 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4895 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4896 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4897 | kvm_guest_enter(); |
| 4898 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4899 | if (unlikely(vcpu->arch.switch_db_regs)) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4900 | set_debugreg(0, 7); |
| 4901 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 4902 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 4903 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 4904 | set_debugreg(vcpu->arch.eff_db[3], 3); |
| 4905 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4906 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4907 | trace_kvm_entry(vcpu->vcpu_id); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4908 | kvm_x86_ops->run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4909 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4910 | /* |
| 4911 | * If the guest has used debug registers, at least dr7 |
| 4912 | * will be disabled while returning to the host. |
| 4913 | * If we don't have active breakpoints in the host, we don't |
| 4914 | * care about the messed up debug address registers. But if |
| 4915 | * we have some of them active, restore the old state. |
| 4916 | */ |
Frederic Weisbecker | 59d8eb5 | 2009-11-10 11:03:12 +0100 | [diff] [blame] | 4917 | if (hw_breakpoint_active()) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4918 | hw_breakpoint_restore(); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4919 | |
Avi Kivity | d94e1dc | 2010-05-03 16:54:48 +0300 | [diff] [blame] | 4920 | atomic_set(&vcpu->guest_mode, 0); |
| 4921 | smp_wmb(); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4922 | local_irq_enable(); |
| 4923 | |
| 4924 | ++vcpu->stat.exits; |
| 4925 | |
| 4926 | /* |
| 4927 | * We must have an instruction between local_irq_enable() and |
| 4928 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 4929 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 4930 | * But we need to prevent reordering, hence this barrier(): |
| 4931 | */ |
| 4932 | barrier(); |
| 4933 | |
| 4934 | kvm_guest_exit(); |
| 4935 | |
| 4936 | preempt_enable(); |
| 4937 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4938 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4939 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4940 | /* |
| 4941 | * Profile KVM exit RIPs: |
| 4942 | */ |
| 4943 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4944 | unsigned long rip = kvm_rip_read(vcpu); |
| 4945 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4946 | } |
| 4947 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4948 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4949 | kvm_lapic_sync_from_vapic(vcpu); |
| 4950 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4951 | r = kvm_x86_ops->handle_exit(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4952 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4953 | return r; |
| 4954 | } |
| 4955 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4956 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4957 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4958 | { |
| 4959 | int r; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4960 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4961 | |
| 4962 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 4963 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 4964 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4965 | kvm_lapic_reset(vcpu); |
Gleb Natapov | 5f17928 | 2008-10-07 15:42:33 +0200 | [diff] [blame] | 4966 | r = kvm_arch_vcpu_reset(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4967 | if (r) |
| 4968 | return r; |
| 4969 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4970 | } |
| 4971 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4972 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4973 | vapic_enter(vcpu); |
| 4974 | |
| 4975 | r = 1; |
| 4976 | while (r > 0) { |
Gleb Natapov | af2152f | 2008-09-22 14:28:53 +0300 | [diff] [blame] | 4977 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4978 | r = vcpu_enter_guest(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4979 | else { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4980 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4981 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4982 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 4983 | if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4984 | { |
| 4985 | switch(vcpu->arch.mp_state) { |
| 4986 | case KVM_MP_STATE_HALTED: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4987 | vcpu->arch.mp_state = |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4988 | KVM_MP_STATE_RUNNABLE; |
| 4989 | case KVM_MP_STATE_RUNNABLE: |
| 4990 | break; |
| 4991 | case KVM_MP_STATE_SIPI_RECEIVED: |
| 4992 | default: |
| 4993 | r = -EINTR; |
| 4994 | break; |
| 4995 | } |
| 4996 | } |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4997 | } |
| 4998 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4999 | if (r <= 0) |
| 5000 | break; |
| 5001 | |
| 5002 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
| 5003 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 5004 | kvm_inject_pending_timer_irqs(vcpu); |
| 5005 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5006 | if (dm_request_for_irq_injection(vcpu)) { |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 5007 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5008 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 5009 | ++vcpu->stat.request_irq_exits; |
| 5010 | } |
| 5011 | if (signal_pending(current)) { |
| 5012 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5013 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 5014 | ++vcpu->stat.signal_exits; |
| 5015 | } |
| 5016 | if (need_resched()) { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5017 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 5018 | kvm_resched(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5019 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 5020 | } |
| 5021 | } |
| 5022 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5023 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5024 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 5025 | vapic_exit(vcpu); |
| 5026 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5027 | return r; |
| 5028 | } |
| 5029 | |
| 5030 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 5031 | { |
| 5032 | int r; |
| 5033 | sigset_t sigsaved; |
| 5034 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5035 | if (vcpu->sigset_active) |
| 5036 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 5037 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 5038 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 5039 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 5040 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 5041 | r = -EAGAIN; |
| 5042 | goto out; |
| 5043 | } |
| 5044 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5045 | /* re-sync apic's tpr */ |
| 5046 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 5047 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5048 | |
Gleb Natapov | 92bf974 | 2010-03-18 15:20:28 +0200 | [diff] [blame] | 5049 | if (vcpu->arch.pio.count || vcpu->mmio_needed || |
| 5050 | vcpu->arch.emulate_ctxt.restart) { |
| 5051 | if (vcpu->mmio_needed) { |
| 5052 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 5053 | vcpu->mmio_read_completed = 1; |
| 5054 | vcpu->mmio_needed = 0; |
Gleb Natapov | 7972995 | 2010-03-18 15:20:24 +0200 | [diff] [blame] | 5055 | } |
Gleb Natapov | 7567cae | 2010-03-09 12:01:10 +0200 | [diff] [blame] | 5056 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 5cd2191 | 2010-03-18 15:20:26 +0200 | [diff] [blame] | 5057 | r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5058 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 5059 | if (r != EMULATE_DONE) { |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5060 | r = 0; |
| 5061 | goto out; |
| 5062 | } |
| 5063 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5064 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 5065 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 5066 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5067 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5068 | r = __vcpu_run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5069 | |
| 5070 | out: |
Marcelo Tosatti | f1d86e4 | 2010-05-03 23:04:27 -0300 | [diff] [blame] | 5071 | post_kvm_run_save(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5072 | if (vcpu->sigset_active) |
| 5073 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 5074 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5075 | return r; |
| 5076 | } |
| 5077 | |
| 5078 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 5079 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5080 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 5081 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 5082 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 5083 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 5084 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 5085 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 5086 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 5087 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5088 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5089 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 5090 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 5091 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 5092 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 5093 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 5094 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 5095 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 5096 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5097 | #endif |
| 5098 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5099 | regs->rip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5100 | regs->rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5101 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5102 | return 0; |
| 5103 | } |
| 5104 | |
| 5105 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 5106 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5107 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 5108 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 5109 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 5110 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 5111 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 5112 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 5113 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 5114 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5115 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5116 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 5117 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 5118 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 5119 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 5120 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 5121 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 5122 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 5123 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5124 | #endif |
| 5125 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5126 | kvm_rip_write(vcpu, regs->rip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5127 | kvm_set_rflags(vcpu, regs->rflags); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5128 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 5129 | vcpu->arch.exception.pending = false; |
| 5130 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5131 | return 0; |
| 5132 | } |
| 5133 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5134 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5135 | { |
| 5136 | struct kvm_segment cs; |
| 5137 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5138 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5139 | *db = cs.db; |
| 5140 | *l = cs.l; |
| 5141 | } |
| 5142 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 5143 | |
| 5144 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 5145 | struct kvm_sregs *sregs) |
| 5146 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5147 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5148 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5149 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 5150 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 5151 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 5152 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 5153 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 5154 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5155 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5156 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 5157 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5158 | |
| 5159 | kvm_x86_ops->get_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5160 | sregs->idt.limit = dt.size; |
| 5161 | sregs->idt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5162 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5163 | sregs->gdt.limit = dt.size; |
| 5164 | sregs->gdt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5165 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5166 | sregs->cr0 = kvm_read_cr0(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5167 | sregs->cr2 = vcpu->arch.cr2; |
| 5168 | sregs->cr3 = vcpu->arch.cr3; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5169 | sregs->cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 5170 | sregs->cr8 = kvm_get_cr8(vcpu); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5171 | sregs->efer = vcpu->arch.efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5172 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 5173 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5174 | memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5175 | |
Gleb Natapov | 36752c9 | 2009-05-11 13:35:53 +0300 | [diff] [blame] | 5176 | if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft) |
Gleb Natapov | 14d0bc1 | 2009-04-21 17:45:11 +0300 | [diff] [blame] | 5177 | set_bit(vcpu->arch.interrupt.nr, |
| 5178 | (unsigned long *)sregs->interrupt_bitmap); |
Gleb Natapov | 16d7a19 | 2009-04-21 17:45:10 +0300 | [diff] [blame] | 5179 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5180 | return 0; |
| 5181 | } |
| 5182 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 5183 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 5184 | struct kvm_mp_state *mp_state) |
| 5185 | { |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 5186 | mp_state->mp_state = vcpu->arch.mp_state; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 5191 | struct kvm_mp_state *mp_state) |
| 5192 | { |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 5193 | vcpu->arch.mp_state = mp_state->mp_state; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 5194 | return 0; |
| 5195 | } |
| 5196 | |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5197 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason, |
| 5198 | bool has_error_code, u32 error_code) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5199 | { |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 5200 | struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode; |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 5201 | int ret; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5202 | |
Mohammed Gamal | 8ec4722 | 2010-08-16 00:47:01 +0300 | [diff] [blame] | 5203 | init_emulate_ctxt(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5204 | |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 5205 | ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5206 | tss_selector, reason, has_error_code, |
| 5207 | error_code); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5208 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5209 | if (ret) |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 5210 | return EMULATE_FAIL; |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5211 | |
Gleb Natapov | 4d2179e | 2010-04-28 19:15:42 +0300 | [diff] [blame] | 5212 | memcpy(vcpu->arch.regs, c->regs, sizeof c->regs); |
Gleb Natapov | 95c5588 | 2010-04-28 19:15:39 +0300 | [diff] [blame] | 5213 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip); |
Gleb Natapov | 19d0443 | 2010-04-15 12:29:50 +0300 | [diff] [blame] | 5214 | kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
| 5215 | return EMULATE_DONE; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5216 | } |
| 5217 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 5218 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5219 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 5220 | struct kvm_sregs *sregs) |
| 5221 | { |
| 5222 | int mmu_reset_needed = 0; |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5223 | int pending_vec, max_bits; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5224 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5225 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5226 | dt.size = sregs->idt.limit; |
| 5227 | dt.address = sregs->idt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5228 | kvm_x86_ops->set_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5229 | dt.size = sregs->gdt.limit; |
| 5230 | dt.address = sregs->gdt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5231 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 5232 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5233 | vcpu->arch.cr2 = sregs->cr2; |
| 5234 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
Jan Kiszka | dc7e795 | 2009-07-01 20:52:03 +0200 | [diff] [blame] | 5235 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5236 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 5237 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5238 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5239 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5240 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5241 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 5242 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5243 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5244 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 5245 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5246 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5247 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5248 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5249 | if (!is_long_mode(vcpu) && is_pae(vcpu)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5250 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5251 | mmu_reset_needed = 1; |
| 5252 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5253 | |
| 5254 | if (mmu_reset_needed) |
| 5255 | kvm_mmu_reset_context(vcpu); |
| 5256 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5257 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 5258 | pending_vec = find_first_bit( |
| 5259 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 5260 | if (pending_vec < max_bits) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5261 | kvm_queue_interrupt(vcpu, pending_vec, false); |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5262 | pr_debug("Set back pending irq %d\n", pending_vec); |
| 5263 | if (irqchip_in_kernel(vcpu->kvm)) |
| 5264 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5265 | } |
| 5266 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5267 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 5268 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 5269 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 5270 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 5271 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 5272 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5273 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5274 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 5275 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5276 | |
Mikhail Ershov | 5f0269f | 2009-08-03 14:58:25 +0300 | [diff] [blame] | 5277 | update_cr8_intercept(vcpu); |
| 5278 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5279 | /* Older userspace won't unhalt the vcpu on reset. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5280 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5281 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 5282 | !is_protmode(vcpu)) |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5283 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 5284 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5285 | return 0; |
| 5286 | } |
| 5287 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5288 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 5289 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5290 | { |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5291 | unsigned long rflags; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5292 | int i, r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5293 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5294 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 5295 | r = -EBUSY; |
| 5296 | if (vcpu->arch.exception.pending) |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 5297 | goto out; |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5298 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 5299 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5300 | else |
| 5301 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 5302 | } |
| 5303 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5304 | /* |
| 5305 | * Read rflags as long as potentially injected trace flags are still |
| 5306 | * filtered out. |
| 5307 | */ |
| 5308 | rflags = kvm_get_rflags(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5309 | |
| 5310 | vcpu->guest_debug = dbg->control; |
| 5311 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 5312 | vcpu->guest_debug = 0; |
| 5313 | |
| 5314 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5315 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 5316 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
| 5317 | vcpu->arch.switch_db_regs = |
| 5318 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); |
| 5319 | } else { |
| 5320 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 5321 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 5322 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); |
| 5323 | } |
| 5324 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5325 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 5326 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 5327 | get_segment_base(vcpu, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5328 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5329 | /* |
| 5330 | * Trigger an rflags update that will inject or remove the trace |
| 5331 | * flags. |
| 5332 | */ |
| 5333 | kvm_set_rflags(vcpu, rflags); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5334 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5335 | kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 5336 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5337 | r = 0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5338 | |
Avi Kivity | 2122ff5 | 2010-05-13 11:25:04 +0300 | [diff] [blame] | 5339 | out: |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5340 | |
| 5341 | return r; |
| 5342 | } |
| 5343 | |
| 5344 | /* |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5345 | * Translate a guest virtual address to a guest physical address. |
| 5346 | */ |
| 5347 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 5348 | struct kvm_translation *tr) |
| 5349 | { |
| 5350 | unsigned long vaddr = tr->linear_address; |
| 5351 | gpa_t gpa; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5352 | int idx; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5353 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5354 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5355 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5356 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5357 | tr->physical_address = gpa; |
| 5358 | tr->valid = gpa != UNMAPPED_GVA; |
| 5359 | tr->writeable = 1; |
| 5360 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5361 | |
| 5362 | return 0; |
| 5363 | } |
| 5364 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5365 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5366 | { |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5367 | struct i387_fxsave_struct *fxsave = |
| 5368 | &vcpu->arch.guest_fpu.state->fxsave; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5369 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5370 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 5371 | fpu->fcw = fxsave->cwd; |
| 5372 | fpu->fsw = fxsave->swd; |
| 5373 | fpu->ftwx = fxsave->twd; |
| 5374 | fpu->last_opcode = fxsave->fop; |
| 5375 | fpu->last_ip = fxsave->rip; |
| 5376 | fpu->last_dp = fxsave->rdp; |
| 5377 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 5378 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5379 | return 0; |
| 5380 | } |
| 5381 | |
| 5382 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5383 | { |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5384 | struct i387_fxsave_struct *fxsave = |
| 5385 | &vcpu->arch.guest_fpu.state->fxsave; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5386 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5387 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 5388 | fxsave->cwd = fpu->fcw; |
| 5389 | fxsave->swd = fpu->fsw; |
| 5390 | fxsave->twd = fpu->ftwx; |
| 5391 | fxsave->fop = fpu->last_opcode; |
| 5392 | fxsave->rip = fpu->last_ip; |
| 5393 | fxsave->rdp = fpu->last_dp; |
| 5394 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 5395 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5396 | return 0; |
| 5397 | } |
| 5398 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 5399 | int fx_init(struct kvm_vcpu *vcpu) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5400 | { |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 5401 | int err; |
| 5402 | |
| 5403 | err = fpu_alloc(&vcpu->arch.guest_fpu); |
| 5404 | if (err) |
| 5405 | return err; |
| 5406 | |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5407 | fpu_finit(&vcpu->arch.guest_fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5408 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5409 | /* |
| 5410 | * Ensure guest xcr0 is valid for loading |
| 5411 | */ |
| 5412 | vcpu->arch.xcr0 = XSTATE_FP; |
| 5413 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5414 | vcpu->arch.cr0 |= X86_CR0_ET; |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 5415 | |
| 5416 | return 0; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5417 | } |
| 5418 | EXPORT_SYMBOL_GPL(fx_init); |
| 5419 | |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5420 | static void fx_free(struct kvm_vcpu *vcpu) |
| 5421 | { |
| 5422 | fpu_free(&vcpu->arch.guest_fpu); |
| 5423 | } |
| 5424 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5425 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 5426 | { |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 5427 | if (vcpu->guest_fpu_loaded) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5428 | return; |
| 5429 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5430 | /* |
| 5431 | * Restore all possible states in the guest, |
| 5432 | * and assume host would use all available bits. |
| 5433 | * Guest xcr0 would be loaded later. |
| 5434 | */ |
| 5435 | kvm_put_guest_xcr0(vcpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5436 | vcpu->guest_fpu_loaded = 1; |
Sheng Yang | 7cf3085 | 2010-05-17 17:08:27 +0800 | [diff] [blame] | 5437 | unlazy_fpu(current); |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5438 | fpu_restore_checking(&vcpu->arch.guest_fpu); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5439 | trace_kvm_fpu(1); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5440 | } |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5441 | |
| 5442 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 5443 | { |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5444 | kvm_put_guest_xcr0(vcpu); |
| 5445 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5446 | if (!vcpu->guest_fpu_loaded) |
| 5447 | return; |
| 5448 | |
| 5449 | vcpu->guest_fpu_loaded = 0; |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5450 | fpu_save_init(&vcpu->arch.guest_fpu); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 5451 | ++vcpu->stat.fpu_reload; |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 5452 | kvm_make_request(KVM_REQ_DEACTIVATE_FPU, vcpu); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5453 | trace_kvm_fpu(0); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5454 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5455 | |
| 5456 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 5457 | { |
Joerg Roedel | 7f1ea20 | 2009-02-25 16:08:31 +0100 | [diff] [blame] | 5458 | if (vcpu->arch.time_page) { |
| 5459 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 5460 | vcpu->arch.time_page = NULL; |
| 5461 | } |
| 5462 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5463 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5464 | fx_free(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5465 | kvm_x86_ops->vcpu_free(vcpu); |
| 5466 | } |
| 5467 | |
| 5468 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 5469 | unsigned int id) |
| 5470 | { |
Zachary Amsden | 6755bae | 2010-08-19 22:07:22 -1000 | [diff] [blame] | 5471 | if (check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
| 5472 | printk_once(KERN_WARNING |
| 5473 | "kvm: SMP vm created on host with unstable TSC; " |
| 5474 | "guest TSC will not be reliable\n"); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5475 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 5476 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5477 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5478 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 5479 | { |
| 5480 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5481 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 5482 | vcpu->arch.mtrr_state.have_fixed = 1; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5483 | vcpu_load(vcpu); |
| 5484 | r = kvm_arch_vcpu_reset(vcpu); |
| 5485 | if (r == 0) |
| 5486 | r = kvm_mmu_setup(vcpu); |
| 5487 | vcpu_put(vcpu); |
| 5488 | if (r < 0) |
| 5489 | goto free_vcpu; |
| 5490 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5491 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5492 | free_vcpu: |
| 5493 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5494 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5495 | } |
| 5496 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 5497 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5498 | { |
| 5499 | vcpu_load(vcpu); |
| 5500 | kvm_mmu_unload(vcpu); |
| 5501 | vcpu_put(vcpu); |
| 5502 | |
Sheng Yang | 9891883 | 2010-05-17 17:08:28 +0800 | [diff] [blame] | 5503 | fx_free(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5504 | kvm_x86_ops->vcpu_free(vcpu); |
| 5505 | } |
| 5506 | |
| 5507 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 5508 | { |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 5509 | vcpu->arch.nmi_pending = false; |
| 5510 | vcpu->arch.nmi_injected = false; |
| 5511 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5512 | vcpu->arch.switch_db_regs = 0; |
| 5513 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
| 5514 | vcpu->arch.dr6 = DR6_FIXED_1; |
| 5515 | vcpu->arch.dr7 = DR7_FIXED_1; |
| 5516 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5517 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 5518 | } |
| 5519 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5520 | int kvm_arch_hardware_enable(void *garbage) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5521 | { |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 5522 | kvm_shared_msr_cpu_online(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5523 | return kvm_x86_ops->hardware_enable(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | void kvm_arch_hardware_disable(void *garbage) |
| 5527 | { |
| 5528 | kvm_x86_ops->hardware_disable(garbage); |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 5529 | drop_user_return_notifiers(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | int kvm_arch_hardware_setup(void) |
| 5533 | { |
| 5534 | return kvm_x86_ops->hardware_setup(); |
| 5535 | } |
| 5536 | |
| 5537 | void kvm_arch_hardware_unsetup(void) |
| 5538 | { |
| 5539 | kvm_x86_ops->hardware_unsetup(); |
| 5540 | } |
| 5541 | |
| 5542 | void kvm_arch_check_processor_compat(void *rtn) |
| 5543 | { |
| 5544 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 5545 | } |
| 5546 | |
| 5547 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 5548 | { |
| 5549 | struct page *page; |
| 5550 | struct kvm *kvm; |
| 5551 | int r; |
| 5552 | |
| 5553 | BUG_ON(vcpu->kvm == NULL); |
| 5554 | kvm = vcpu->kvm; |
| 5555 | |
Avi Kivity | 9aabc88f | 2010-07-29 15:11:50 +0300 | [diff] [blame] | 5556 | vcpu->arch.emulate_ctxt.ops = &emulate_ops; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5557 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5558 | if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu)) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5559 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5560 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5561 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5562 | |
| 5563 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 5564 | if (!page) { |
| 5565 | r = -ENOMEM; |
| 5566 | goto fail; |
| 5567 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5568 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5569 | |
| 5570 | r = kvm_mmu_create(vcpu); |
| 5571 | if (r < 0) |
| 5572 | goto fail_free_pio_data; |
| 5573 | |
| 5574 | if (irqchip_in_kernel(kvm)) { |
| 5575 | r = kvm_create_lapic(vcpu); |
| 5576 | if (r < 0) |
| 5577 | goto fail_mmu_destroy; |
| 5578 | } |
| 5579 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5580 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 5581 | GFP_KERNEL); |
| 5582 | if (!vcpu->arch.mce_banks) { |
| 5583 | r = -ENOMEM; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5584 | goto fail_free_lapic; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5585 | } |
| 5586 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 5587 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5588 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL)) |
| 5589 | goto fail_free_mce_banks; |
| 5590 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5591 | return 0; |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5592 | fail_free_mce_banks: |
| 5593 | kfree(vcpu->arch.mce_banks); |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5594 | fail_free_lapic: |
| 5595 | kvm_free_lapic(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5596 | fail_mmu_destroy: |
| 5597 | kvm_mmu_destroy(vcpu); |
| 5598 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5599 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5600 | fail: |
| 5601 | return r; |
| 5602 | } |
| 5603 | |
| 5604 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 5605 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5606 | int idx; |
| 5607 | |
Wei Yongjun | 36cb93f | 2010-01-22 14:18:47 +0800 | [diff] [blame] | 5608 | kfree(vcpu->arch.mce_banks); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5609 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5610 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5611 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5612 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5613 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5614 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5615 | |
| 5616 | struct kvm *kvm_arch_create_vm(void) |
| 5617 | { |
| 5618 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 5619 | |
| 5620 | if (!kvm) |
| 5621 | return ERR_PTR(-ENOMEM); |
| 5622 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5623 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 5624 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5625 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 5626 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 5627 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 5628 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 5629 | spin_lock_init(&kvm->arch.tsc_write_lock); |
| 5630 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5631 | return kvm; |
| 5632 | } |
| 5633 | |
| 5634 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 5635 | { |
| 5636 | vcpu_load(vcpu); |
| 5637 | kvm_mmu_unload(vcpu); |
| 5638 | vcpu_put(vcpu); |
| 5639 | } |
| 5640 | |
| 5641 | static void kvm_free_vcpus(struct kvm *kvm) |
| 5642 | { |
| 5643 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5644 | struct kvm_vcpu *vcpu; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5645 | |
| 5646 | /* |
| 5647 | * Unpin any mmu pages first. |
| 5648 | */ |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5649 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5650 | kvm_unload_vcpu_mmu(vcpu); |
| 5651 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5652 | kvm_arch_vcpu_free(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5653 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5654 | mutex_lock(&kvm->lock); |
| 5655 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 5656 | kvm->vcpus[i] = NULL; |
| 5657 | |
| 5658 | atomic_set(&kvm->online_vcpus, 0); |
| 5659 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5660 | } |
| 5661 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5662 | void kvm_arch_sync_events(struct kvm *kvm) |
| 5663 | { |
Sheng Yang | ba4cef3 | 2009-01-06 10:03:03 +0800 | [diff] [blame] | 5664 | kvm_free_all_assigned_devices(kvm); |
Xiao Guangrong | aea924f | 2010-07-10 17:37:56 +0800 | [diff] [blame] | 5665 | kvm_free_pit(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5666 | } |
| 5667 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5668 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 5669 | { |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 5670 | kvm_iommu_unmap_guest(kvm); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 5671 | kfree(kvm->arch.vpic); |
| 5672 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5673 | kvm_free_vcpus(kvm); |
| 5674 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 5675 | if (kvm->arch.apic_access_page) |
| 5676 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5677 | if (kvm->arch.ept_identity_pagetable) |
| 5678 | put_page(kvm->arch.ept_identity_pagetable); |
Marcelo Tosatti | 6474920 | 2010-01-19 12:45:23 -0200 | [diff] [blame] | 5679 | cleanup_srcu_struct(&kvm->srcu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5680 | kfree(kvm); |
| 5681 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5682 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5683 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 5684 | struct kvm_memory_slot *memslot, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5685 | struct kvm_memory_slot old, |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5686 | struct kvm_userspace_memory_region *mem, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5687 | int user_alloc) |
| 5688 | { |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5689 | int npages = memslot->npages; |
Avi Kivity | 7ac7709 | 2010-06-21 10:57:45 +0300 | [diff] [blame] | 5690 | int map_flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 5691 | |
| 5692 | /* Prevent internal slot pages from being moved by fork()/COW. */ |
| 5693 | if (memslot->id >= KVM_MEMORY_SLOTS) |
| 5694 | map_flags = MAP_SHARED | MAP_ANONYMOUS; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5695 | |
| 5696 | /*To keep backward compatibility with older userspace, |
| 5697 | *x86 needs to hanlde !user_alloc case. |
| 5698 | */ |
| 5699 | if (!user_alloc) { |
| 5700 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5701 | unsigned long userspace_addr; |
| 5702 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5703 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5704 | userspace_addr = do_mmap(NULL, 0, |
| 5705 | npages * PAGE_SIZE, |
| 5706 | PROT_READ | PROT_WRITE, |
Avi Kivity | 7ac7709 | 2010-06-21 10:57:45 +0300 | [diff] [blame] | 5707 | map_flags, |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5708 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5709 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5710 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5711 | if (IS_ERR((void *)userspace_addr)) |
| 5712 | return PTR_ERR((void *)userspace_addr); |
| 5713 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5714 | memslot->userspace_addr = userspace_addr; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5718 | |
| 5719 | return 0; |
| 5720 | } |
| 5721 | |
| 5722 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 5723 | struct kvm_userspace_memory_region *mem, |
| 5724 | struct kvm_memory_slot old, |
| 5725 | int user_alloc) |
| 5726 | { |
| 5727 | |
| 5728 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 5729 | |
| 5730 | if (!user_alloc && !old.user_alloc && old.rmap && !npages) { |
| 5731 | int ret; |
| 5732 | |
| 5733 | down_write(¤t->mm->mmap_sem); |
| 5734 | ret = do_munmap(current->mm, old.userspace_addr, |
| 5735 | old.npages * PAGE_SIZE); |
| 5736 | up_write(¤t->mm->mmap_sem); |
| 5737 | if (ret < 0) |
| 5738 | printk(KERN_WARNING |
| 5739 | "kvm_vm_ioctl_set_memory_region: " |
| 5740 | "failed to munmap memory\n"); |
| 5741 | } |
| 5742 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5743 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5744 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5745 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 5746 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 5747 | } |
| 5748 | |
| 5749 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5750 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5751 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5752 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5753 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 5754 | { |
| 5755 | kvm_mmu_zap_all(kvm); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 5756 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5757 | } |
| 5758 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5759 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 5760 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5761 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 5762 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
| 5763 | || vcpu->arch.nmi_pending || |
| 5764 | (kvm_arch_interrupt_allowed(vcpu) && |
| 5765 | kvm_cpu_has_interrupt(vcpu)); |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5766 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5767 | |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5768 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 5769 | { |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5770 | int me; |
| 5771 | int cpu = vcpu->cpu; |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5772 | |
| 5773 | if (waitqueue_active(&vcpu->wq)) { |
| 5774 | wake_up_interruptible(&vcpu->wq); |
| 5775 | ++vcpu->stat.halt_wakeup; |
| 5776 | } |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5777 | |
| 5778 | me = get_cpu(); |
| 5779 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) |
Avi Kivity | d94e1dc | 2010-05-03 16:54:48 +0300 | [diff] [blame] | 5780 | if (atomic_xchg(&vcpu->guest_mode, 0)) |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5781 | smp_send_reschedule(cpu); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 5782 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5783 | } |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5784 | |
| 5785 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5786 | { |
| 5787 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 5788 | } |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5789 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5790 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 5791 | { |
| 5792 | unsigned long current_rip = kvm_rip_read(vcpu) + |
| 5793 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 5794 | |
| 5795 | return current_rip == linear_rip; |
| 5796 | } |
| 5797 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 5798 | |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5799 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 5800 | { |
| 5801 | unsigned long rflags; |
| 5802 | |
| 5803 | rflags = kvm_x86_ops->get_rflags(vcpu); |
| 5804 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5805 | rflags &= ~X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5806 | return rflags; |
| 5807 | } |
| 5808 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 5809 | |
| 5810 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 5811 | { |
| 5812 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5813 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame] | 5814 | rflags |= X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5815 | kvm_x86_ops->set_rflags(vcpu, rflags); |
| 5816 | } |
| 5817 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 5818 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5819 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
| 5820 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 5821 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 5822 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 5823 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 5824 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 5825 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 5826 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 5827 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 5828 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 5829 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
Joerg Roedel | 2e554e8 | 2010-02-24 18:59:14 +0100 | [diff] [blame] | 5830 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |