Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * derived from drivers/kvm/kvm_main.c |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Qumranet, Inc. |
| 8 | * Copyright IBM Corporation, 2008 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 13 | * Amit Shah <amit.shah@qumranet.com> |
| 14 | * Ben-Ami Yassour <benami@il.ibm.com> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 15 | * |
| 16 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 17 | * the COPYING file in the top-level directory. |
| 18 | * |
| 19 | */ |
| 20 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 21 | #include <linux/kvm_host.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 22 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 23 | #include "mmu.h" |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 24 | #include "i8254.h" |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 25 | #include "tss.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 26 | #include "kvm_cache_regs.h" |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 27 | #include "x86.h" |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 28 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 29 | #include <linux/clocksource.h> |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 31 | #include <linux/kvm.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/vmalloc.h> |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 34 | #include <linux/module.h> |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 35 | #include <linux/mman.h> |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 36 | #include <linux/highmem.h> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 37 | #include <linux/iommu.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 38 | #include <linux/intel-iommu.h> |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 39 | #include <linux/cpufreq.h> |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 40 | #include <linux/user-return-notifier.h> |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 41 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 43 | #include <trace/events/kvm.h> |
| 44 | #undef TRACE_INCLUDE_FILE |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 45 | #define CREATE_TRACE_POINTS |
| 46 | #include "trace.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 47 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 48 | #include <asm/debugreg.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 49 | #include <asm/uaccess.h> |
Zhang Xiantao | d825ed0 | 2007-11-14 20:08:51 +0800 | [diff] [blame] | 50 | #include <asm/msr.h> |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 51 | #include <asm/desc.h> |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 52 | #include <asm/mtrr.h> |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 53 | #include <asm/mce.h> |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 54 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 55 | #define MAX_IO_MSRS 256 |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 56 | #define CR0_RESERVED_BITS \ |
| 57 | (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ |
| 58 | | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ |
| 59 | | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) |
| 60 | #define CR4_RESERVED_BITS \ |
| 61 | (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ |
| 62 | | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ |
| 63 | | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 64 | | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE)) |
| 65 | |
| 66 | #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 67 | |
| 68 | #define KVM_MAX_MCE_BANKS 32 |
| 69 | #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P |
| 70 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 71 | /* EFER defaults: |
| 72 | * - enable syscall per default because its emulated by KVM |
| 73 | * - enable LME and LMA per default on 64 bit KVM |
| 74 | */ |
| 75 | #ifdef CONFIG_X86_64 |
| 76 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL; |
| 77 | #else |
| 78 | static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL; |
| 79 | #endif |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 80 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 81 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 82 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 83 | |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 84 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 85 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
| 86 | struct kvm_cpuid_entry2 __user *entries); |
| 87 | |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 88 | struct kvm_x86_ops *kvm_x86_ops; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
Zhang Xiantao | 97896d0 | 2007-11-14 20:09:30 +0800 | [diff] [blame] | 90 | |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 91 | int ignore_msrs = 0; |
| 92 | module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR); |
| 93 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 94 | #define KVM_NR_SHARED_MSRS 16 |
| 95 | |
| 96 | struct kvm_shared_msrs_global { |
| 97 | int nr; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 98 | u32 msrs[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct kvm_shared_msrs { |
| 102 | struct user_return_notifier urn; |
| 103 | bool registered; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 104 | struct kvm_shared_msr_values { |
| 105 | u64 host; |
| 106 | u64 curr; |
| 107 | } values[KVM_NR_SHARED_MSRS]; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; |
| 111 | static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs); |
| 112 | |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 113 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 114 | { "pf_fixed", VCPU_STAT(pf_fixed) }, |
| 115 | { "pf_guest", VCPU_STAT(pf_guest) }, |
| 116 | { "tlb_flush", VCPU_STAT(tlb_flush) }, |
| 117 | { "invlpg", VCPU_STAT(invlpg) }, |
| 118 | { "exits", VCPU_STAT(exits) }, |
| 119 | { "io_exits", VCPU_STAT(io_exits) }, |
| 120 | { "mmio_exits", VCPU_STAT(mmio_exits) }, |
| 121 | { "signal_exits", VCPU_STAT(signal_exits) }, |
| 122 | { "irq_window", VCPU_STAT(irq_window_exits) }, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 123 | { "nmi_window", VCPU_STAT(nmi_window_exits) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 124 | { "halt_exits", VCPU_STAT(halt_exits) }, |
| 125 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 126 | { "hypercalls", VCPU_STAT(hypercalls) }, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 127 | { "request_irq", VCPU_STAT(request_irq_exits) }, |
| 128 | { "irq_exits", VCPU_STAT(irq_exits) }, |
| 129 | { "host_state_reload", VCPU_STAT(host_state_reload) }, |
| 130 | { "efer_reload", VCPU_STAT(efer_reload) }, |
| 131 | { "fpu_reload", VCPU_STAT(fpu_reload) }, |
| 132 | { "insn_emulation", VCPU_STAT(insn_emulation) }, |
| 133 | { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 134 | { "irq_injections", VCPU_STAT(irq_injections) }, |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 135 | { "nmi_injections", VCPU_STAT(nmi_injections) }, |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 136 | { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, |
| 137 | { "mmu_pte_write", VM_STAT(mmu_pte_write) }, |
| 138 | { "mmu_pte_updated", VM_STAT(mmu_pte_updated) }, |
| 139 | { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) }, |
| 140 | { "mmu_flooded", VM_STAT(mmu_flooded) }, |
| 141 | { "mmu_recycled", VM_STAT(mmu_recycled) }, |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 142 | { "mmu_cache_miss", VM_STAT(mmu_cache_miss) }, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 143 | { "mmu_unsync", VM_STAT(mmu_unsync) }, |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 144 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 145 | { "largepages", VM_STAT(lpages) }, |
Hollis Blanchard | 417bc30 | 2007-10-31 17:24:23 -0500 | [diff] [blame] | 146 | { NULL } |
| 147 | }; |
| 148 | |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 149 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 150 | { |
| 151 | unsigned slot; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 152 | struct kvm_shared_msrs *locals |
| 153 | = container_of(urn, struct kvm_shared_msrs, urn); |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 154 | struct kvm_shared_msr_values *values; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 155 | |
| 156 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 157 | values = &locals->values[slot]; |
| 158 | if (values->host != values->curr) { |
| 159 | wrmsrl(shared_msrs_global.msrs[slot], values->host); |
| 160 | values->curr = values->host; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | locals->registered = false; |
| 164 | user_return_notifier_unregister(urn); |
| 165 | } |
| 166 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 167 | static void shared_msr_update(unsigned slot, u32 msr) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 168 | { |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 169 | struct kvm_shared_msrs *smsr; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 170 | u64 value; |
| 171 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 172 | smsr = &__get_cpu_var(shared_msrs); |
| 173 | /* only read, and nobody should modify it at this time, |
| 174 | * so don't need lock */ |
| 175 | if (slot >= shared_msrs_global.nr) { |
| 176 | printk(KERN_ERR "kvm: invalid MSR slot!"); |
| 177 | return; |
| 178 | } |
| 179 | rdmsrl_safe(msr, &value); |
| 180 | smsr->values[slot].host = value; |
| 181 | smsr->values[slot].curr = value; |
| 182 | } |
| 183 | |
| 184 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
| 185 | { |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 186 | if (slot >= shared_msrs_global.nr) |
| 187 | shared_msrs_global.nr = slot + 1; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 188 | shared_msrs_global.msrs[slot] = msr; |
| 189 | /* we need ensured the shared_msr_global have been updated */ |
| 190 | smp_wmb(); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 191 | } |
| 192 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); |
| 193 | |
| 194 | static void kvm_shared_msr_cpu_online(void) |
| 195 | { |
| 196 | unsigned i; |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 197 | |
| 198 | for (i = 0; i < shared_msrs_global.nr; ++i) |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 199 | shared_msr_update(i, shared_msrs_global.msrs[i]); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 202 | void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 203 | { |
| 204 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 205 | |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 206 | if (((value ^ smsr->values[slot].curr) & mask) == 0) |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 207 | return; |
Sheng Yang | 2bf78fa | 2009-12-18 16:48:44 +0800 | [diff] [blame] | 208 | smsr->values[slot].curr = value; |
| 209 | wrmsrl(shared_msrs_global.msrs[slot], value); |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 210 | if (!smsr->registered) { |
| 211 | smsr->urn.on_user_return = kvm_on_user_return; |
| 212 | user_return_notifier_register(&smsr->urn); |
| 213 | smsr->registered = true; |
| 214 | } |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); |
| 217 | |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 218 | static void drop_user_return_notifiers(void *ignore) |
| 219 | { |
| 220 | struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs); |
| 221 | |
| 222 | if (smsr->registered) |
| 223 | kvm_on_user_return(&smsr->urn); |
| 224 | } |
| 225 | |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 226 | unsigned long segment_base(u16 selector) |
| 227 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 228 | struct desc_ptr gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 229 | struct desc_struct *d; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 230 | unsigned long table_base; |
| 231 | unsigned long v; |
| 232 | |
| 233 | if (selector == 0) |
| 234 | return 0; |
| 235 | |
Akinobu Mita | b792c34 | 2009-07-19 00:00:01 +0900 | [diff] [blame] | 236 | kvm_get_gdt(&gdt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 237 | table_base = gdt.address; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 238 | |
| 239 | if (selector & 4) { /* from ldt */ |
Akinobu Mita | b792c34 | 2009-07-19 00:00:01 +0900 | [diff] [blame] | 240 | u16 ldt_selector = kvm_read_ldt(); |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 241 | |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 242 | table_base = segment_base(ldt_selector); |
| 243 | } |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 244 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
Akinobu Mita | 46a359e | 2009-07-18 23:58:32 +0900 | [diff] [blame] | 245 | v = get_desc_base(d); |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 246 | #ifdef CONFIG_X86_64 |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 247 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 248 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
Carsten Otte | 5fb76f9 | 2007-10-29 16:08:51 +0100 | [diff] [blame] | 249 | #endif |
| 250 | return v; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(segment_base); |
| 253 | |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 254 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 255 | { |
| 256 | if (irqchip_in_kernel(vcpu->kvm)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 257 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 258 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 259 | return vcpu->arch.apic_base; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 260 | } |
| 261 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 262 | |
| 263 | void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) |
| 264 | { |
| 265 | /* TODO: reserve bits check */ |
| 266 | if (irqchip_in_kernel(vcpu->kvm)) |
| 267 | kvm_lapic_set_base(vcpu, data); |
| 268 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 269 | vcpu->arch.apic_base = data; |
Carsten Otte | 6866b83 | 2007-10-29 16:09:10 +0100 | [diff] [blame] | 270 | } |
| 271 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 272 | |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 273 | #define EXCPT_BENIGN 0 |
| 274 | #define EXCPT_CONTRIBUTORY 1 |
| 275 | #define EXCPT_PF 2 |
| 276 | |
| 277 | static int exception_class(int vector) |
| 278 | { |
| 279 | switch (vector) { |
| 280 | case PF_VECTOR: |
| 281 | return EXCPT_PF; |
| 282 | case DE_VECTOR: |
| 283 | case TS_VECTOR: |
| 284 | case NP_VECTOR: |
| 285 | case SS_VECTOR: |
| 286 | case GP_VECTOR: |
| 287 | return EXCPT_CONTRIBUTORY; |
| 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | return EXCPT_BENIGN; |
| 292 | } |
| 293 | |
| 294 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
| 295 | unsigned nr, bool has_error, u32 error_code) |
| 296 | { |
| 297 | u32 prev_nr; |
| 298 | int class1, class2; |
| 299 | |
| 300 | if (!vcpu->arch.exception.pending) { |
| 301 | queue: |
| 302 | vcpu->arch.exception.pending = true; |
| 303 | vcpu->arch.exception.has_error_code = has_error; |
| 304 | vcpu->arch.exception.nr = nr; |
| 305 | vcpu->arch.exception.error_code = error_code; |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | /* to check exception */ |
| 310 | prev_nr = vcpu->arch.exception.nr; |
| 311 | if (prev_nr == DF_VECTOR) { |
| 312 | /* triple fault -> shutdown */ |
| 313 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 314 | return; |
| 315 | } |
| 316 | class1 = exception_class(prev_nr); |
| 317 | class2 = exception_class(nr); |
| 318 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 319 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
| 320 | /* generate double fault per SDM Table 5-5 */ |
| 321 | vcpu->arch.exception.pending = true; |
| 322 | vcpu->arch.exception.has_error_code = true; |
| 323 | vcpu->arch.exception.nr = DF_VECTOR; |
| 324 | vcpu->arch.exception.error_code = 0; |
| 325 | } else |
| 326 | /* replace previous exception with a new one in a hope |
| 327 | that instruction re-execution will regenerate lost |
| 328 | exception */ |
| 329 | goto queue; |
| 330 | } |
| 331 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 332 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 333 | { |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 334 | kvm_multiple_exception(vcpu, nr, false, 0); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 335 | } |
| 336 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 337 | |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 338 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, |
| 339 | u32 error_code) |
| 340 | { |
| 341 | ++vcpu->stat.pf_guest; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 342 | vcpu->arch.cr2 = addr; |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 343 | kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); |
| 344 | } |
| 345 | |
Sheng Yang | 3419ffc | 2008-05-15 09:52:48 +0800 | [diff] [blame] | 346 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 347 | { |
| 348 | vcpu->arch.nmi_pending = 1; |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 351 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 352 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 353 | { |
Eddie Dong | 3fd28fc | 2009-11-19 17:54:07 +0200 | [diff] [blame] | 354 | kvm_multiple_exception(vcpu, nr, true, error_code); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 355 | } |
| 356 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 357 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 358 | /* |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 359 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 360 | * a #GP and return false. |
| 361 | */ |
| 362 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 363 | { |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 364 | if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl) |
| 365 | return true; |
| 366 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 367 | return false; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 368 | } |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 369 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * Load the pae pdptrs. Return true is they are all valid. |
| 373 | */ |
| 374 | int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 375 | { |
| 376 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 377 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 378 | int i; |
| 379 | int ret; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 380 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 381 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 382 | ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte, |
| 383 | offset * sizeof(u64), sizeof(pdpte)); |
| 384 | if (ret < 0) { |
| 385 | ret = 0; |
| 386 | goto out; |
| 387 | } |
| 388 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 389 | if (is_present_gpte(pdpte[i]) && |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 390 | (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 391 | ret = 0; |
| 392 | goto out; |
| 393 | } |
| 394 | } |
| 395 | ret = 1; |
| 396 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 397 | memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 398 | __set_bit(VCPU_EXREG_PDPTR, |
| 399 | (unsigned long *)&vcpu->arch.regs_avail); |
| 400 | __set_bit(VCPU_EXREG_PDPTR, |
| 401 | (unsigned long *)&vcpu->arch.regs_dirty); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 402 | out: |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 403 | |
| 404 | return ret; |
| 405 | } |
Joerg Roedel | cc4b687 | 2008-02-07 13:47:43 +0100 | [diff] [blame] | 406 | EXPORT_SYMBOL_GPL(load_pdptrs); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 407 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 408 | static bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 409 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 410 | u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)]; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 411 | bool changed = true; |
| 412 | int r; |
| 413 | |
| 414 | if (is_long_mode(vcpu) || !is_pae(vcpu)) |
| 415 | return false; |
| 416 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 417 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 418 | (unsigned long *)&vcpu->arch.regs_avail)) |
| 419 | return true; |
| 420 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 421 | 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] | 422 | if (r < 0) |
| 423 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 424 | changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0; |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 425 | out: |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 426 | |
| 427 | return changed; |
| 428 | } |
| 429 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 430 | void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 431 | { |
Avi Kivity | f9a48e6 | 2010-01-06 19:10:22 +0200 | [diff] [blame] | 432 | cr0 |= X86_CR0_ET; |
| 433 | |
Gleb Natapov | ab34482 | 2010-01-21 15:28:46 +0200 | [diff] [blame] | 434 | #ifdef CONFIG_X86_64 |
| 435 | if (cr0 & 0xffffffff00000000UL) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 436 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 437 | return; |
| 438 | } |
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 | |
| 443 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 444 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
| 448 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 449 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
| 454 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 455 | if ((vcpu->arch.efer & EFER_LME)) { |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 456 | int cs_db, cs_l; |
| 457 | |
| 458 | if (!is_pae(vcpu)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 459 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 463 | if (cs_l) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 464 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 465 | return; |
| 466 | |
| 467 | } |
| 468 | } else |
| 469 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 470 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 471 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | |
| 475 | } |
| 476 | |
| 477 | kvm_x86_ops->set_cr0(vcpu, cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 478 | vcpu->arch.cr0 = cr0; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 479 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 480 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 481 | return; |
| 482 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 483 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 484 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 485 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 486 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 487 | kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f)); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 488 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 489 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 490 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 491 | void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 492 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 493 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 494 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE; |
| 495 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 496 | if (cr4 & CR4_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 497 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | if (is_long_mode(vcpu)) { |
| 502 | if (!(cr4 & X86_CR4_PAE)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 503 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 504 | return; |
| 505 | } |
Avi Kivity | a2edf57 | 2009-05-24 22:19:00 +0300 | [diff] [blame] | 506 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 507 | && ((cr4 ^ old_cr4) & pdptr_bits) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 508 | && !load_pdptrs(vcpu, vcpu->arch.cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 509 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (cr4 & X86_CR4_VMXE) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 514 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | kvm_x86_ops->set_cr4(vcpu, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 518 | vcpu->arch.cr4 = cr4; |
Avi Kivity | 5a41acc | 2009-01-11 17:19:35 +0200 | [diff] [blame] | 519 | vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 520 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 521 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 522 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 523 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 524 | void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 525 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 526 | if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 527 | kvm_mmu_sync_roots(vcpu); |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 528 | kvm_mmu_flush_tlb(vcpu); |
| 529 | return; |
| 530 | } |
| 531 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 532 | if (is_long_mode(vcpu)) { |
| 533 | if (cr3 & CR3_L_MODE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 534 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | } else { |
| 538 | if (is_pae(vcpu)) { |
| 539 | if (cr3 & CR3_PAE_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 540 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 544 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | } |
| 548 | /* |
| 549 | * We don't check reserved bits in nonpae mode, because |
| 550 | * this isn't enforced, and VMware depends on this. |
| 551 | */ |
| 552 | } |
| 553 | |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 554 | /* |
| 555 | * Does the new cr3 value map to physical memory? (Note, we |
| 556 | * catch an invalid cr3 even in real-mode, because it would |
| 557 | * cause trouble later on when we turn on paging anyway.) |
| 558 | * |
| 559 | * A real CPU would silently accept an invalid cr3 and would |
| 560 | * attempt to use it - with largely undefined (and often hard |
| 561 | * to debug) behavior on the guest side. |
| 562 | */ |
| 563 | if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT))) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 564 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 565 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 566 | vcpu->arch.cr3 = cr3; |
| 567 | vcpu->arch.mmu.new_cr3(vcpu); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 568 | } |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 569 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 570 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 571 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 572 | void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 573 | { |
| 574 | if (cr8 & CR8_RESERVED_BITS) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 575 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 576 | return; |
| 577 | } |
| 578 | if (irqchip_in_kernel(vcpu->kvm)) |
| 579 | kvm_lapic_set_tpr(vcpu, cr8); |
| 580 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 581 | vcpu->arch.cr8 = cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 582 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 583 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 584 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 585 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 586 | { |
| 587 | if (irqchip_in_kernel(vcpu->kvm)) |
| 588 | return kvm_lapic_get_cr8(vcpu); |
| 589 | else |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 590 | return vcpu->arch.cr8; |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 591 | } |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 592 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
Carsten Otte | a03490e | 2007-10-29 16:09:35 +0100 | [diff] [blame] | 593 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 594 | static inline u32 bit(int bitno) |
| 595 | { |
| 596 | return 1 << (bitno & 31); |
| 597 | } |
| 598 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 599 | /* |
| 600 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 601 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 602 | * |
| 603 | * This list is modified at module load time to reflect the |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 604 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
| 605 | * kvm-specific. Those are put in the beginning of the list. |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 606 | */ |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 607 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 608 | #define KVM_SAVE_MSRS_BEGIN 5 |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 609 | static u32 msrs_to_save[] = { |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 610 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 611 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 612 | HV_X64_MSR_APIC_ASSIST_PAGE, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 613 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 614 | MSR_K6_STAR, |
| 615 | #ifdef CONFIG_X86_64 |
| 616 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 617 | #endif |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 618 | 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] | 619 | }; |
| 620 | |
| 621 | static unsigned num_msrs_to_save; |
| 622 | |
| 623 | static u32 emulated_msrs[] = { |
| 624 | MSR_IA32_MISC_ENABLE, |
| 625 | }; |
| 626 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 627 | static void set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 628 | { |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 629 | if (efer & efer_reserved_bits) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 630 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (is_paging(vcpu) |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 635 | && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 636 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 640 | if (efer & EFER_FFXSR) { |
| 641 | struct kvm_cpuid_entry2 *feat; |
| 642 | |
| 643 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 644 | if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) { |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 645 | kvm_inject_gp(vcpu, 0); |
| 646 | return; |
| 647 | } |
| 648 | } |
| 649 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 650 | if (efer & EFER_SVME) { |
| 651 | struct kvm_cpuid_entry2 *feat; |
| 652 | |
| 653 | feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 654 | if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 655 | kvm_inject_gp(vcpu, 0); |
| 656 | return; |
| 657 | } |
| 658 | } |
| 659 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 660 | kvm_x86_ops->set_efer(vcpu, efer); |
| 661 | |
| 662 | efer &= ~EFER_LMA; |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 663 | efer |= vcpu->arch.efer & EFER_LMA; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 664 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 665 | vcpu->arch.efer = efer; |
Avi Kivity | 9645bb56 | 2009-03-31 11:31:54 +0300 | [diff] [blame] | 666 | |
| 667 | vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled; |
| 668 | kvm_mmu_reset_context(vcpu); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Joerg Roedel | f2b4b7d | 2008-01-31 14:57:37 +0100 | [diff] [blame] | 671 | void kvm_enable_efer_bits(u64 mask) |
| 672 | { |
| 673 | efer_reserved_bits &= ~mask; |
| 674 | } |
| 675 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 676 | |
| 677 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 678 | /* |
| 679 | * Writes msr value into into the appropriate "register". |
| 680 | * Returns 0 on success, non-0 otherwise. |
| 681 | * Assumes vcpu_load() was already called. |
| 682 | */ |
| 683 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 684 | { |
| 685 | return kvm_x86_ops->set_msr(vcpu, msr_index, data); |
| 686 | } |
| 687 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 688 | /* |
| 689 | * Adapt set_msr() to msr_io()'s calling convention |
| 690 | */ |
| 691 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 692 | { |
| 693 | return kvm_set_msr(vcpu, index, *data); |
| 694 | } |
| 695 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 696 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 697 | { |
| 698 | static int version; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 699 | struct pvclock_wall_clock wc; |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 700 | struct timespec boot; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 701 | |
| 702 | if (!wall_clock) |
| 703 | return; |
| 704 | |
| 705 | version++; |
| 706 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 707 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 708 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 709 | /* |
| 710 | * The guest calculates current wall clock time by adding |
| 711 | * system time (updated by kvm_write_guest_time below) to the |
| 712 | * wall clock specified here. guest system time equals host |
| 713 | * system time for us, thus we must fill in host boot time here. |
| 714 | */ |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 715 | getboottime(&boot); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 716 | |
| 717 | wc.sec = boot.tv_sec; |
| 718 | wc.nsec = boot.tv_nsec; |
| 719 | wc.version = version; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 720 | |
| 721 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 722 | |
| 723 | version++; |
| 724 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 725 | } |
| 726 | |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 727 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 728 | { |
| 729 | uint32_t quotient, remainder; |
| 730 | |
| 731 | /* Don't try to replace with do_div(), this one calculates |
| 732 | * "(dividend << 32) / divisor" */ |
| 733 | __asm__ ( "divl %4" |
| 734 | : "=a" (quotient), "=d" (remainder) |
| 735 | : "0" (0), "1" (dividend), "r" (divisor) ); |
| 736 | return quotient; |
| 737 | } |
| 738 | |
| 739 | static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock) |
| 740 | { |
| 741 | uint64_t nsecs = 1000000000LL; |
| 742 | int32_t shift = 0; |
| 743 | uint64_t tps64; |
| 744 | uint32_t tps32; |
| 745 | |
| 746 | tps64 = tsc_khz * 1000LL; |
| 747 | while (tps64 > nsecs*2) { |
| 748 | tps64 >>= 1; |
| 749 | shift--; |
| 750 | } |
| 751 | |
| 752 | tps32 = (uint32_t)tps64; |
| 753 | while (tps32 <= (uint32_t)nsecs) { |
| 754 | tps32 <<= 1; |
| 755 | shift++; |
| 756 | } |
| 757 | |
| 758 | hv_clock->tsc_shift = shift; |
| 759 | hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32); |
| 760 | |
| 761 | 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] | 762 | __func__, tsc_khz, hv_clock->tsc_shift, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 763 | hv_clock->tsc_to_system_mul); |
| 764 | } |
| 765 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 766 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
| 767 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 768 | static void kvm_write_guest_time(struct kvm_vcpu *v) |
| 769 | { |
| 770 | struct timespec ts; |
| 771 | unsigned long flags; |
| 772 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 773 | void *shared_kaddr; |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 774 | unsigned long this_tsc_khz; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 775 | |
| 776 | if ((!vcpu->time_page)) |
| 777 | return; |
| 778 | |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 779 | this_tsc_khz = get_cpu_var(cpu_tsc_khz); |
| 780 | if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) { |
| 781 | kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock); |
| 782 | vcpu->hv_clock_tsc_khz = this_tsc_khz; |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 783 | } |
Avi Kivity | 463656c | 2009-04-12 15:49:07 +0300 | [diff] [blame] | 784 | put_cpu_var(cpu_tsc_khz); |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 785 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 786 | /* Keep irq disabled to prevent changes to the clock */ |
| 787 | local_irq_save(flags); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 788 | 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] | 789 | ktime_get_ts(&ts); |
Jason Wang | 923de3c | 2010-01-27 19:13:49 +0800 | [diff] [blame] | 790 | monotonic_to_bootbased(&ts); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 791 | local_irq_restore(flags); |
| 792 | |
| 793 | /* With all the info we got, fill in the values */ |
| 794 | |
| 795 | vcpu->hv_clock.system_time = ts.tv_nsec + |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 796 | (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset; |
| 797 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 798 | /* |
| 799 | * The interface expects us to write an even number signaling that the |
| 800 | * update is finished. Since the guest won't see the intermediate |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 801 | * state, we just increase by 2 at the end. |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 802 | */ |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 803 | vcpu->hv_clock.version += 2; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 804 | |
| 805 | shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0); |
| 806 | |
| 807 | memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock, |
Gerd Hoffmann | 50d0a0f | 2008-06-03 16:17:31 +0200 | [diff] [blame] | 808 | sizeof(vcpu->hv_clock)); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 809 | |
| 810 | kunmap_atomic(shared_kaddr, KM_USER0); |
| 811 | |
| 812 | mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT); |
| 813 | } |
| 814 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 815 | static int kvm_request_guest_time_update(struct kvm_vcpu *v) |
| 816 | { |
| 817 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 818 | |
| 819 | if (!vcpu->time_page) |
| 820 | return 0; |
| 821 | set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests); |
| 822 | return 1; |
| 823 | } |
| 824 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 825 | static bool msr_mtrr_valid(unsigned msr) |
| 826 | { |
| 827 | switch (msr) { |
| 828 | case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1: |
| 829 | case MSR_MTRRfix64K_00000: |
| 830 | case MSR_MTRRfix16K_80000: |
| 831 | case MSR_MTRRfix16K_A0000: |
| 832 | case MSR_MTRRfix4K_C0000: |
| 833 | case MSR_MTRRfix4K_C8000: |
| 834 | case MSR_MTRRfix4K_D0000: |
| 835 | case MSR_MTRRfix4K_D8000: |
| 836 | case MSR_MTRRfix4K_E0000: |
| 837 | case MSR_MTRRfix4K_E8000: |
| 838 | case MSR_MTRRfix4K_F0000: |
| 839 | case MSR_MTRRfix4K_F8000: |
| 840 | case MSR_MTRRdefType: |
| 841 | case MSR_IA32_CR_PAT: |
| 842 | return true; |
| 843 | case 0x2f8: |
| 844 | return true; |
| 845 | } |
| 846 | return false; |
| 847 | } |
| 848 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 849 | static bool valid_pat_type(unsigned t) |
| 850 | { |
| 851 | return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */ |
| 852 | } |
| 853 | |
| 854 | static bool valid_mtrr_type(unsigned t) |
| 855 | { |
| 856 | return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */ |
| 857 | } |
| 858 | |
| 859 | static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 860 | { |
| 861 | int i; |
| 862 | |
| 863 | if (!msr_mtrr_valid(msr)) |
| 864 | return false; |
| 865 | |
| 866 | if (msr == MSR_IA32_CR_PAT) { |
| 867 | for (i = 0; i < 8; i++) |
| 868 | if (!valid_pat_type((data >> (i * 8)) & 0xff)) |
| 869 | return false; |
| 870 | return true; |
| 871 | } else if (msr == MSR_MTRRdefType) { |
| 872 | if (data & ~0xcff) |
| 873 | return false; |
| 874 | return valid_mtrr_type(data & 0xff); |
| 875 | } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) { |
| 876 | for (i = 0; i < 8 ; i++) |
| 877 | if (!valid_mtrr_type((data >> (i * 8)) & 0xff)) |
| 878 | return false; |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | /* variable MTRRs */ |
| 883 | return valid_mtrr_type(data & 0xff); |
| 884 | } |
| 885 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 886 | static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 887 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 888 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 889 | |
Marcelo Tosatti | d6289b9 | 2009-06-22 15:27:56 -0300 | [diff] [blame] | 890 | if (!mtrr_valid(vcpu, msr, data)) |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 891 | return 1; |
| 892 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 893 | if (msr == MSR_MTRRdefType) { |
| 894 | vcpu->arch.mtrr_state.def_type = data; |
| 895 | vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10; |
| 896 | } else if (msr == MSR_MTRRfix64K_00000) |
| 897 | p[0] = data; |
| 898 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 899 | p[1 + msr - MSR_MTRRfix16K_80000] = data; |
| 900 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 901 | p[3 + msr - MSR_MTRRfix4K_C0000] = data; |
| 902 | else if (msr == MSR_IA32_CR_PAT) |
| 903 | vcpu->arch.pat = data; |
| 904 | else { /* Variable MTRRs */ |
| 905 | int idx, is_mtrr_mask; |
| 906 | u64 *pt; |
| 907 | |
| 908 | idx = (msr - 0x200) / 2; |
| 909 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 910 | if (!is_mtrr_mask) |
| 911 | pt = |
| 912 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 913 | else |
| 914 | pt = |
| 915 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 916 | *pt = data; |
| 917 | } |
| 918 | |
| 919 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 920 | return 0; |
| 921 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 922 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 923 | static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 924 | { |
| 925 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 926 | unsigned bank_num = mcg_cap & 0xff; |
| 927 | |
| 928 | switch (msr) { |
| 929 | case MSR_IA32_MCG_STATUS: |
| 930 | vcpu->arch.mcg_status = data; |
| 931 | break; |
| 932 | case MSR_IA32_MCG_CTL: |
| 933 | if (!(mcg_cap & MCG_CTL_P)) |
| 934 | return 1; |
| 935 | if (data != 0 && data != ~(u64)0) |
| 936 | return -1; |
| 937 | vcpu->arch.mcg_ctl = data; |
| 938 | break; |
| 939 | default: |
| 940 | if (msr >= MSR_IA32_MC0_CTL && |
| 941 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 942 | u32 offset = msr - MSR_IA32_MC0_CTL; |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 943 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 944 | * some Linux kernels though clear bit 10 in bank 4 to |
| 945 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 946 | * this to avoid an uncatched #GP in the guest |
| 947 | */ |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 948 | if ((offset & 0x3) == 0 && |
Andre Przywara | 114be42 | 2010-03-24 17:46:42 +0100 | [diff] [blame] | 949 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 950 | return -1; |
| 951 | vcpu->arch.mce_banks[offset] = data; |
| 952 | break; |
| 953 | } |
| 954 | return 1; |
| 955 | } |
| 956 | return 0; |
| 957 | } |
| 958 | |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 959 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 960 | { |
| 961 | struct kvm *kvm = vcpu->kvm; |
| 962 | int lm = is_long_mode(vcpu); |
| 963 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 964 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 965 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 966 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 967 | u32 page_num = data & ~PAGE_MASK; |
| 968 | u64 page_addr = data & PAGE_MASK; |
| 969 | u8 *page; |
| 970 | int r; |
| 971 | |
| 972 | r = -E2BIG; |
| 973 | if (page_num >= blob_size) |
| 974 | goto out; |
| 975 | r = -ENOMEM; |
| 976 | page = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 977 | if (!page) |
| 978 | goto out; |
| 979 | r = -EFAULT; |
| 980 | if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE)) |
| 981 | goto out_free; |
| 982 | if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE)) |
| 983 | goto out_free; |
| 984 | r = 0; |
| 985 | out_free: |
| 986 | kfree(page); |
| 987 | out: |
| 988 | return r; |
| 989 | } |
| 990 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 991 | static bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 992 | { |
| 993 | return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; |
| 994 | } |
| 995 | |
| 996 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 997 | { |
| 998 | bool r = false; |
| 999 | switch (msr) { |
| 1000 | case HV_X64_MSR_GUEST_OS_ID: |
| 1001 | case HV_X64_MSR_HYPERCALL: |
| 1002 | r = true; |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | return r; |
| 1007 | } |
| 1008 | |
| 1009 | static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1010 | { |
| 1011 | struct kvm *kvm = vcpu->kvm; |
| 1012 | |
| 1013 | switch (msr) { |
| 1014 | case HV_X64_MSR_GUEST_OS_ID: |
| 1015 | kvm->arch.hv_guest_os_id = data; |
| 1016 | /* setting guest os id to zero disables hypercall page */ |
| 1017 | if (!kvm->arch.hv_guest_os_id) |
| 1018 | kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 1019 | break; |
| 1020 | case HV_X64_MSR_HYPERCALL: { |
| 1021 | u64 gfn; |
| 1022 | unsigned long addr; |
| 1023 | u8 instructions[4]; |
| 1024 | |
| 1025 | /* if guest os id is not set hypercall should remain disabled */ |
| 1026 | if (!kvm->arch.hv_guest_os_id) |
| 1027 | break; |
| 1028 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1029 | kvm->arch.hv_hypercall = data; |
| 1030 | break; |
| 1031 | } |
| 1032 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 1033 | addr = gfn_to_hva(kvm, gfn); |
| 1034 | if (kvm_is_error_hva(addr)) |
| 1035 | return 1; |
| 1036 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 1037 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 1038 | if (copy_to_user((void __user *)addr, instructions, 4)) |
| 1039 | return 1; |
| 1040 | kvm->arch.hv_hypercall = data; |
| 1041 | break; |
| 1042 | } |
| 1043 | default: |
| 1044 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1045 | "data 0x%llx\n", msr, data); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1052 | { |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1053 | switch (msr) { |
| 1054 | case HV_X64_MSR_APIC_ASSIST_PAGE: { |
| 1055 | unsigned long addr; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1056 | |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1057 | if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { |
| 1058 | vcpu->arch.hv_vapic = data; |
| 1059 | break; |
| 1060 | } |
| 1061 | addr = gfn_to_hva(vcpu->kvm, data >> |
| 1062 | HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT); |
| 1063 | if (kvm_is_error_hva(addr)) |
| 1064 | return 1; |
| 1065 | if (clear_user((void __user *)addr, PAGE_SIZE)) |
| 1066 | return 1; |
| 1067 | vcpu->arch.hv_vapic = data; |
| 1068 | break; |
| 1069 | } |
| 1070 | case HV_X64_MSR_EOI: |
| 1071 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1072 | case HV_X64_MSR_ICR: |
| 1073 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1074 | case HV_X64_MSR_TPR: |
| 1075 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
| 1076 | default: |
| 1077 | pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x " |
| 1078 | "data 0x%llx\n", msr, data); |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
| 1082 | return 0; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1085 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) |
| 1086 | { |
| 1087 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1088 | case MSR_EFER: |
| 1089 | set_efer(vcpu, data); |
| 1090 | break; |
Andre Przywara | 8f1589d | 2009-06-24 12:44:33 +0200 | [diff] [blame] | 1091 | case MSR_K7_HWCR: |
| 1092 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
| 1093 | if (data != 0) { |
| 1094 | pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 1095 | data); |
| 1096 | return 1; |
| 1097 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1098 | break; |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1099 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 1100 | if (data != 0) { |
| 1101 | pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 1102 | "0x%llx\n", data); |
| 1103 | return 1; |
| 1104 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1105 | break; |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1106 | case MSR_AMD64_NB_CFG: |
Joerg Roedel | c7ac679 | 2008-02-11 20:28:27 +0100 | [diff] [blame] | 1107 | break; |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1108 | case MSR_IA32_DEBUGCTLMSR: |
| 1109 | if (!data) { |
| 1110 | /* We support the non-activated case already */ |
| 1111 | break; |
| 1112 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 1113 | /* Values other than LBR and BTF are vendor-specific, |
| 1114 | thus reserved and should throw a #GP */ |
| 1115 | return 1; |
| 1116 | } |
| 1117 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 1118 | __func__, data); |
| 1119 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1120 | case MSR_IA32_UCODE_REV: |
| 1121 | case MSR_IA32_UCODE_WRITE: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1122 | case MSR_VM_HSAVE_PA: |
Andre Przywara | 6098ca9 | 2009-07-03 16:00:14 +0200 | [diff] [blame] | 1123 | case MSR_AMD64_PATCH_LOADER: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1124 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1125 | case 0x200 ... 0x2ff: |
| 1126 | return set_msr_mtrr(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1127 | case MSR_IA32_APICBASE: |
| 1128 | kvm_set_apic_base(vcpu, data); |
| 1129 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1130 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1131 | return kvm_x2apic_msr_write(vcpu, msr, data); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1132 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1133 | vcpu->arch.ia32_misc_enable_msr = data; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1134 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1135 | case MSR_KVM_WALL_CLOCK: |
| 1136 | vcpu->kvm->arch.wall_clock = data; |
| 1137 | kvm_write_wall_clock(vcpu->kvm, data); |
| 1138 | break; |
| 1139 | case MSR_KVM_SYSTEM_TIME: { |
| 1140 | if (vcpu->arch.time_page) { |
| 1141 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 1142 | vcpu->arch.time_page = NULL; |
| 1143 | } |
| 1144 | |
| 1145 | vcpu->arch.time = data; |
| 1146 | |
| 1147 | /* we verify if the enable bit is set... */ |
| 1148 | if (!(data & 1)) |
| 1149 | break; |
| 1150 | |
| 1151 | /* ...but clean it before doing the actual write */ |
| 1152 | vcpu->arch.time_offset = data & ~(PAGE_MASK | 1); |
| 1153 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1154 | vcpu->arch.time_page = |
| 1155 | gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1156 | |
| 1157 | if (is_error_page(vcpu->arch.time_page)) { |
| 1158 | kvm_release_page_clean(vcpu->arch.time_page); |
| 1159 | vcpu->arch.time_page = NULL; |
| 1160 | } |
| 1161 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1162 | kvm_request_guest_time_update(vcpu); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1163 | break; |
| 1164 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1165 | case MSR_IA32_MCG_CTL: |
| 1166 | case MSR_IA32_MCG_STATUS: |
| 1167 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1168 | return set_msr_mce(vcpu, msr, data); |
Andre Przywara | 71db602 | 2009-06-12 22:01:29 +0200 | [diff] [blame] | 1169 | |
| 1170 | /* Performance counters are not protected by a CPUID bit, |
| 1171 | * so we should check all of them in the generic path for the sake of |
| 1172 | * cross vendor migration. |
| 1173 | * Writing a zero into the event select MSRs disables them, |
| 1174 | * which we perfectly emulate ;-). Any other value should be at least |
| 1175 | * reported, some guests depend on them. |
| 1176 | */ |
| 1177 | case MSR_P6_EVNTSEL0: |
| 1178 | case MSR_P6_EVNTSEL1: |
| 1179 | case MSR_K7_EVNTSEL0: |
| 1180 | case MSR_K7_EVNTSEL1: |
| 1181 | case MSR_K7_EVNTSEL2: |
| 1182 | case MSR_K7_EVNTSEL3: |
| 1183 | if (data != 0) |
| 1184 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1185 | "0x%x data 0x%llx\n", msr, data); |
| 1186 | break; |
| 1187 | /* at least RHEL 4 unconditionally writes to the perfctr registers, |
| 1188 | * so we ignore writes to make it happy. |
| 1189 | */ |
| 1190 | case MSR_P6_PERFCTR0: |
| 1191 | case MSR_P6_PERFCTR1: |
| 1192 | case MSR_K7_PERFCTR0: |
| 1193 | case MSR_K7_PERFCTR1: |
| 1194 | case MSR_K7_PERFCTR2: |
| 1195 | case MSR_K7_PERFCTR3: |
| 1196 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: " |
| 1197 | "0x%x data 0x%llx\n", msr, data); |
| 1198 | break; |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1199 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1200 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1201 | int r; |
| 1202 | mutex_lock(&vcpu->kvm->lock); |
| 1203 | r = set_msr_hyperv_pw(vcpu, msr, data); |
| 1204 | mutex_unlock(&vcpu->kvm->lock); |
| 1205 | return r; |
| 1206 | } else |
| 1207 | return set_msr_hyperv(vcpu, msr, data); |
| 1208 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1209 | default: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1210 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 1211 | return xen_hvm_config(vcpu, data); |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1212 | if (!ignore_msrs) { |
| 1213 | pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", |
| 1214 | msr, data); |
| 1215 | return 1; |
| 1216 | } else { |
| 1217 | pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n", |
| 1218 | msr, data); |
| 1219 | break; |
| 1220 | } |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1221 | } |
| 1222 | return 0; |
| 1223 | } |
| 1224 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 1225 | |
| 1226 | |
| 1227 | /* |
| 1228 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1229 | * Returns 0 on success, non-0 otherwise. |
| 1230 | * Assumes vcpu_load() was already called. |
| 1231 | */ |
| 1232 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1233 | { |
| 1234 | return kvm_x86_ops->get_msr(vcpu, msr_index, pdata); |
| 1235 | } |
| 1236 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1237 | static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1238 | { |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1239 | u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges; |
| 1240 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1241 | if (!msr_mtrr_valid(msr)) |
| 1242 | return 1; |
| 1243 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 1244 | if (msr == MSR_MTRRdefType) |
| 1245 | *pdata = vcpu->arch.mtrr_state.def_type + |
| 1246 | (vcpu->arch.mtrr_state.enabled << 10); |
| 1247 | else if (msr == MSR_MTRRfix64K_00000) |
| 1248 | *pdata = p[0]; |
| 1249 | else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000) |
| 1250 | *pdata = p[1 + msr - MSR_MTRRfix16K_80000]; |
| 1251 | else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000) |
| 1252 | *pdata = p[3 + msr - MSR_MTRRfix4K_C0000]; |
| 1253 | else if (msr == MSR_IA32_CR_PAT) |
| 1254 | *pdata = vcpu->arch.pat; |
| 1255 | else { /* Variable MTRRs */ |
| 1256 | int idx, is_mtrr_mask; |
| 1257 | u64 *pt; |
| 1258 | |
| 1259 | idx = (msr - 0x200) / 2; |
| 1260 | is_mtrr_mask = msr - 0x200 - 2 * idx; |
| 1261 | if (!is_mtrr_mask) |
| 1262 | pt = |
| 1263 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo; |
| 1264 | else |
| 1265 | pt = |
| 1266 | (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo; |
| 1267 | *pdata = *pt; |
| 1268 | } |
| 1269 | |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1273 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1274 | { |
| 1275 | u64 data; |
| 1276 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 1277 | unsigned bank_num = mcg_cap & 0xff; |
| 1278 | |
| 1279 | switch (msr) { |
| 1280 | case MSR_IA32_P5_MC_ADDR: |
| 1281 | case MSR_IA32_P5_MC_TYPE: |
| 1282 | data = 0; |
| 1283 | break; |
| 1284 | case MSR_IA32_MCG_CAP: |
| 1285 | data = vcpu->arch.mcg_cap; |
| 1286 | break; |
| 1287 | case MSR_IA32_MCG_CTL: |
| 1288 | if (!(mcg_cap & MCG_CTL_P)) |
| 1289 | return 1; |
| 1290 | data = vcpu->arch.mcg_ctl; |
| 1291 | break; |
| 1292 | case MSR_IA32_MCG_STATUS: |
| 1293 | data = vcpu->arch.mcg_status; |
| 1294 | break; |
| 1295 | default: |
| 1296 | if (msr >= MSR_IA32_MC0_CTL && |
| 1297 | msr < MSR_IA32_MC0_CTL + 4 * bank_num) { |
| 1298 | u32 offset = msr - MSR_IA32_MC0_CTL; |
| 1299 | data = vcpu->arch.mce_banks[offset]; |
| 1300 | break; |
| 1301 | } |
| 1302 | return 1; |
| 1303 | } |
| 1304 | *pdata = data; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1308 | static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1309 | { |
| 1310 | u64 data = 0; |
| 1311 | struct kvm *kvm = vcpu->kvm; |
| 1312 | |
| 1313 | switch (msr) { |
| 1314 | case HV_X64_MSR_GUEST_OS_ID: |
| 1315 | data = kvm->arch.hv_guest_os_id; |
| 1316 | break; |
| 1317 | case HV_X64_MSR_HYPERCALL: |
| 1318 | data = kvm->arch.hv_hypercall; |
| 1319 | break; |
| 1320 | default: |
| 1321 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1322 | return 1; |
| 1323 | } |
| 1324 | |
| 1325 | *pdata = data; |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1330 | { |
| 1331 | u64 data = 0; |
| 1332 | |
| 1333 | switch (msr) { |
| 1334 | case HV_X64_MSR_VP_INDEX: { |
| 1335 | int r; |
| 1336 | struct kvm_vcpu *v; |
| 1337 | kvm_for_each_vcpu(r, v, vcpu->kvm) |
| 1338 | if (v == vcpu) |
| 1339 | data = r; |
| 1340 | break; |
| 1341 | } |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1342 | case HV_X64_MSR_EOI: |
| 1343 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1344 | case HV_X64_MSR_ICR: |
| 1345 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1346 | case HV_X64_MSR_TPR: |
| 1347 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1348 | default: |
| 1349 | pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1350 | return 1; |
| 1351 | } |
| 1352 | *pdata = data; |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1356 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1357 | { |
| 1358 | u64 data; |
| 1359 | |
| 1360 | switch (msr) { |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1361 | case MSR_IA32_PLATFORM_ID: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1362 | case MSR_IA32_UCODE_REV: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1363 | case MSR_IA32_EBL_CR_POWERON: |
Alexander Graf | b5e2fec | 2008-07-22 08:00:45 +0200 | [diff] [blame] | 1364 | case MSR_IA32_DEBUGCTLMSR: |
| 1365 | case MSR_IA32_LASTBRANCHFROMIP: |
| 1366 | case MSR_IA32_LASTBRANCHTOIP: |
| 1367 | case MSR_IA32_LASTINTFROMIP: |
| 1368 | case MSR_IA32_LASTINTTOIP: |
Jaswinder Singh Rajput | 60af2ec | 2009-05-14 11:00:10 +0530 | [diff] [blame] | 1369 | case MSR_K8_SYSCFG: |
| 1370 | case MSR_K7_HWCR: |
Avi Kivity | 61a6bd6 | 2008-12-29 17:32:28 +0200 | [diff] [blame] | 1371 | case MSR_VM_HSAVE_PA: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1372 | case MSR_P6_PERFCTR0: |
| 1373 | case MSR_P6_PERFCTR1: |
Amit Shah | 7fe29e0 | 2009-03-20 12:39:00 +0530 | [diff] [blame] | 1374 | case MSR_P6_EVNTSEL0: |
| 1375 | case MSR_P6_EVNTSEL1: |
Amit Shah | 9e69962 | 2009-06-15 13:25:34 +0530 | [diff] [blame] | 1376 | case MSR_K7_EVNTSEL0: |
Amit Shah | 1f3ee61 | 2009-06-30 16:24:28 +0530 | [diff] [blame] | 1377 | case MSR_K7_PERFCTR0: |
Andre Przywara | 1fdbd48 | 2009-06-24 12:44:34 +0200 | [diff] [blame] | 1378 | case MSR_K8_INT_PENDING_MSG: |
Andre Przywara | c323c0e | 2009-06-24 15:37:05 +0200 | [diff] [blame] | 1379 | case MSR_AMD64_NB_CFG: |
Andre Przywara | f7c6d14 | 2009-07-02 15:04:14 +0200 | [diff] [blame] | 1380 | case MSR_FAM10H_MMIO_CONF_BASE: |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1381 | data = 0; |
| 1382 | break; |
Avi Kivity | 9ba075a | 2008-05-26 20:06:35 +0300 | [diff] [blame] | 1383 | case MSR_MTRRcap: |
| 1384 | data = 0x500 | KVM_NR_VAR_MTRR; |
| 1385 | break; |
| 1386 | case 0x200 ... 0x2ff: |
| 1387 | return get_msr_mtrr(vcpu, msr, pdata); |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1388 | case 0xcd: /* fsb frequency */ |
| 1389 | data = 3; |
| 1390 | break; |
| 1391 | case MSR_IA32_APICBASE: |
| 1392 | data = kvm_get_apic_base(vcpu); |
| 1393 | break; |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1394 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
| 1395 | return kvm_x2apic_msr_read(vcpu, msr, pdata); |
| 1396 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1397 | case MSR_IA32_MISC_ENABLE: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1398 | data = vcpu->arch.ia32_misc_enable_msr; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1399 | break; |
Alexander Graf | 847f0ad | 2008-02-21 12:11:01 +0100 | [diff] [blame] | 1400 | case MSR_IA32_PERF_STATUS: |
| 1401 | /* TSC increment by tick */ |
| 1402 | data = 1000ULL; |
| 1403 | /* CPU multiplier */ |
| 1404 | data |= (((uint64_t)4ULL) << 40); |
| 1405 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1406 | case MSR_EFER: |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1407 | data = vcpu->arch.efer; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1408 | break; |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 1409 | case MSR_KVM_WALL_CLOCK: |
| 1410 | data = vcpu->kvm->arch.wall_clock; |
| 1411 | break; |
| 1412 | case MSR_KVM_SYSTEM_TIME: |
| 1413 | data = vcpu->arch.time; |
| 1414 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1415 | case MSR_IA32_P5_MC_ADDR: |
| 1416 | case MSR_IA32_P5_MC_TYPE: |
| 1417 | case MSR_IA32_MCG_CAP: |
| 1418 | case MSR_IA32_MCG_CTL: |
| 1419 | case MSR_IA32_MCG_STATUS: |
| 1420 | case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1: |
| 1421 | return get_msr_mce(vcpu, msr, pdata); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1422 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
| 1423 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1424 | int r; |
| 1425 | mutex_lock(&vcpu->kvm->lock); |
| 1426 | r = get_msr_hyperv_pw(vcpu, msr, pdata); |
| 1427 | mutex_unlock(&vcpu->kvm->lock); |
| 1428 | return r; |
| 1429 | } else |
| 1430 | return get_msr_hyperv(vcpu, msr, pdata); |
| 1431 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1432 | default: |
Andre Przywara | ed85c06 | 2009-06-25 12:36:49 +0200 | [diff] [blame] | 1433 | if (!ignore_msrs) { |
| 1434 | pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr); |
| 1435 | return 1; |
| 1436 | } else { |
| 1437 | pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr); |
| 1438 | data = 0; |
| 1439 | } |
| 1440 | break; |
Carsten Otte | 15c4a64 | 2007-10-30 18:44:17 +0100 | [diff] [blame] | 1441 | } |
| 1442 | *pdata = data; |
| 1443 | return 0; |
| 1444 | } |
| 1445 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 1446 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1447 | /* |
| 1448 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 1449 | * |
| 1450 | * @return number of msrs set successfully. |
| 1451 | */ |
| 1452 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 1453 | struct kvm_msr_entry *entries, |
| 1454 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1455 | unsigned index, u64 *data)) |
| 1456 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1457 | int i, idx; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1458 | |
| 1459 | vcpu_load(vcpu); |
| 1460 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1461 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1462 | for (i = 0; i < msrs->nmsrs; ++i) |
| 1463 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 1464 | break; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 1465 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1466 | |
| 1467 | vcpu_put(vcpu); |
| 1468 | |
| 1469 | return i; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 1474 | * |
| 1475 | * @return number of msrs set successfully. |
| 1476 | */ |
| 1477 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 1478 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 1479 | unsigned index, u64 *data), |
| 1480 | int writeback) |
| 1481 | { |
| 1482 | struct kvm_msrs msrs; |
| 1483 | struct kvm_msr_entry *entries; |
| 1484 | int r, n; |
| 1485 | unsigned size; |
| 1486 | |
| 1487 | r = -EFAULT; |
| 1488 | if (copy_from_user(&msrs, user_msrs, sizeof msrs)) |
| 1489 | goto out; |
| 1490 | |
| 1491 | r = -E2BIG; |
| 1492 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 1493 | goto out; |
| 1494 | |
| 1495 | r = -ENOMEM; |
| 1496 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 1497 | entries = vmalloc(size); |
| 1498 | if (!entries) |
| 1499 | goto out; |
| 1500 | |
| 1501 | r = -EFAULT; |
| 1502 | if (copy_from_user(entries, user_msrs->entries, size)) |
| 1503 | goto out_free; |
| 1504 | |
| 1505 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 1506 | if (r < 0) |
| 1507 | goto out_free; |
| 1508 | |
| 1509 | r = -EFAULT; |
| 1510 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 1511 | goto out_free; |
| 1512 | |
| 1513 | r = n; |
| 1514 | |
| 1515 | out_free: |
| 1516 | vfree(entries); |
| 1517 | out: |
| 1518 | return r; |
| 1519 | } |
| 1520 | |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1521 | int kvm_dev_ioctl_check_extension(long ext) |
| 1522 | { |
| 1523 | int r; |
| 1524 | |
| 1525 | switch (ext) { |
| 1526 | case KVM_CAP_IRQCHIP: |
| 1527 | case KVM_CAP_HLT: |
| 1528 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1529 | case KVM_CAP_SET_TSS_ADDR: |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1530 | case KVM_CAP_EXT_CPUID: |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1531 | case KVM_CAP_CLOCKSOURCE: |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 1532 | case KVM_CAP_PIT: |
Marcelo Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 1533 | case KVM_CAP_NOP_IO_DELAY: |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1534 | case KVM_CAP_MP_STATE: |
Avi Kivity | ed84862 | 2008-07-29 11:30:57 +0300 | [diff] [blame] | 1535 | case KVM_CAP_SYNC_MMU: |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 1536 | case KVM_CAP_REINJECT_CONTROL: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 1537 | case KVM_CAP_IRQ_INJECT_STATUS: |
Sheng Yang | e56d532 | 2009-03-12 21:45:39 +0800 | [diff] [blame] | 1538 | case KVM_CAP_ASSIGN_DEV_IRQ: |
Gregory Haskins | 721eecbf | 2009-05-20 10:30:49 -0400 | [diff] [blame] | 1539 | case KVM_CAP_IRQFD: |
Gregory Haskins | d34e6b1 | 2009-07-07 17:08:49 -0400 | [diff] [blame] | 1540 | case KVM_CAP_IOEVENTFD: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 1541 | case KVM_CAP_PIT2: |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 1542 | case KVM_CAP_PIT_STATE2: |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 1543 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 1544 | case KVM_CAP_XEN_HVM: |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 1545 | case KVM_CAP_ADJUST_CLOCK: |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 1546 | case KVM_CAP_VCPU_EVENTS: |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 1547 | case KVM_CAP_HYPERV: |
Gleb Natapov | 10388a0 | 2010-01-17 15:51:23 +0200 | [diff] [blame] | 1548 | case KVM_CAP_HYPERV_VAPIC: |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 1549 | case KVM_CAP_HYPERV_SPIN: |
Zhai, Edwin | ab9f4ec | 2010-01-29 14:38:44 +0800 | [diff] [blame] | 1550 | case KVM_CAP_PCI_SEGMENT: |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 1551 | case KVM_CAP_DEBUGREGS: |
Jan Kiszka | d2be165 | 2010-02-23 17:47:57 +0100 | [diff] [blame] | 1552 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1553 | r = 1; |
| 1554 | break; |
Laurent Vivier | 542472b | 2008-05-30 16:05:55 +0200 | [diff] [blame] | 1555 | case KVM_CAP_COALESCED_MMIO: |
| 1556 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 1557 | break; |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1558 | case KVM_CAP_VAPIC: |
| 1559 | r = !kvm_x86_ops->cpu_has_accelerated_tpr(); |
| 1560 | break; |
Avi Kivity | f725230 | 2008-02-20 11:53:16 +0200 | [diff] [blame] | 1561 | case KVM_CAP_NR_VCPUS: |
| 1562 | r = KVM_MAX_VCPUS; |
| 1563 | break; |
Avi Kivity | a988b91 | 2008-02-20 11:59:20 +0200 | [diff] [blame] | 1564 | case KVM_CAP_NR_MEMSLOTS: |
| 1565 | r = KVM_MEMORY_SLOTS; |
| 1566 | break; |
Marcelo Tosatti | a68a6a7 | 2009-10-01 19:28:39 -0300 | [diff] [blame] | 1567 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 1568 | r = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 1569 | break; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1570 | case KVM_CAP_IOMMU: |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 1571 | r = iommu_found(); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 1572 | break; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1573 | case KVM_CAP_MCE: |
| 1574 | r = KVM_MAX_MCE_BANKS; |
| 1575 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1576 | default: |
| 1577 | r = 0; |
| 1578 | break; |
| 1579 | } |
| 1580 | return r; |
| 1581 | |
| 1582 | } |
| 1583 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1584 | long kvm_arch_dev_ioctl(struct file *filp, |
| 1585 | unsigned int ioctl, unsigned long arg) |
| 1586 | { |
| 1587 | void __user *argp = (void __user *)arg; |
| 1588 | long r; |
| 1589 | |
| 1590 | switch (ioctl) { |
| 1591 | case KVM_GET_MSR_INDEX_LIST: { |
| 1592 | struct kvm_msr_list __user *user_msr_list = argp; |
| 1593 | struct kvm_msr_list msr_list; |
| 1594 | unsigned n; |
| 1595 | |
| 1596 | r = -EFAULT; |
| 1597 | if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list)) |
| 1598 | goto out; |
| 1599 | n = msr_list.nmsrs; |
| 1600 | msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs); |
| 1601 | if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list)) |
| 1602 | goto out; |
| 1603 | r = -E2BIG; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1604 | if (n < msr_list.nmsrs) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1605 | goto out; |
| 1606 | r = -EFAULT; |
| 1607 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 1608 | num_msrs_to_save * sizeof(u32))) |
| 1609 | goto out; |
Jan Kiszka | e125e7b | 2009-07-02 21:45:47 +0200 | [diff] [blame] | 1610 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1611 | &emulated_msrs, |
| 1612 | ARRAY_SIZE(emulated_msrs) * sizeof(u32))) |
| 1613 | goto out; |
| 1614 | r = 0; |
| 1615 | break; |
| 1616 | } |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1617 | case KVM_GET_SUPPORTED_CPUID: { |
| 1618 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 1619 | struct kvm_cpuid2 cpuid; |
| 1620 | |
| 1621 | r = -EFAULT; |
| 1622 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 1623 | goto out; |
| 1624 | r = kvm_dev_ioctl_get_supported_cpuid(&cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1625 | cpuid_arg->entries); |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1626 | if (r) |
| 1627 | goto out; |
| 1628 | |
| 1629 | r = -EFAULT; |
| 1630 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 1631 | goto out; |
| 1632 | r = 0; |
| 1633 | break; |
| 1634 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 1635 | case KVM_X86_GET_MCE_CAP_SUPPORTED: { |
| 1636 | u64 mce_cap; |
| 1637 | |
| 1638 | mce_cap = KVM_MCE_CAP_SUPPORTED; |
| 1639 | r = -EFAULT; |
| 1640 | if (copy_to_user(argp, &mce_cap, sizeof mce_cap)) |
| 1641 | goto out; |
| 1642 | r = 0; |
| 1643 | break; |
| 1644 | } |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1645 | default: |
| 1646 | r = -EINVAL; |
| 1647 | } |
| 1648 | out: |
| 1649 | return r; |
| 1650 | } |
| 1651 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1652 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1653 | { |
| 1654 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 1655 | if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) { |
| 1656 | unsigned long khz = cpufreq_quick_get(cpu); |
| 1657 | if (!khz) |
| 1658 | khz = tsc_khz; |
| 1659 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 1660 | } |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 1661 | kvm_request_guest_time_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1665 | { |
Amit Shah | 9327fd1 | 2007-11-15 18:38:46 +0200 | [diff] [blame] | 1666 | kvm_put_guest_fpu(vcpu); |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1667 | kvm_x86_ops->vcpu_put(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1670 | static int is_efer_nx(void) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1671 | { |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1672 | unsigned long long efer = 0; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1673 | |
Avi Kivity | e286e86 | 2009-05-03 18:50:55 +0300 | [diff] [blame] | 1674 | rdmsrl_safe(MSR_EFER, &efer); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1675 | return efer & EFER_NX; |
| 1676 | } |
| 1677 | |
| 1678 | static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu) |
| 1679 | { |
| 1680 | int i; |
| 1681 | struct kvm_cpuid_entry2 *e, *entry; |
| 1682 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1683 | entry = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1684 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
| 1685 | e = &vcpu->arch.cpuid_entries[i]; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1686 | if (e->function == 0x80000001) { |
| 1687 | entry = e; |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1691 | if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1692 | entry->edx &= ~(1 << 20); |
| 1693 | printk(KERN_INFO "kvm: guest NX capability removed\n"); |
| 1694 | } |
| 1695 | } |
| 1696 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1697 | /* when an old userspace process fills a new kernel module */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1698 | static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, |
| 1699 | struct kvm_cpuid *cpuid, |
| 1700 | struct kvm_cpuid_entry __user *entries) |
| 1701 | { |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1702 | int r, i; |
| 1703 | struct kvm_cpuid_entry *cpuid_entries; |
| 1704 | |
| 1705 | r = -E2BIG; |
| 1706 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1707 | goto out; |
| 1708 | r = -ENOMEM; |
| 1709 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); |
| 1710 | if (!cpuid_entries) |
| 1711 | goto out; |
| 1712 | r = -EFAULT; |
| 1713 | if (copy_from_user(cpuid_entries, entries, |
| 1714 | cpuid->nent * sizeof(struct kvm_cpuid_entry))) |
| 1715 | goto out_free; |
| 1716 | for (i = 0; i < cpuid->nent; i++) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1717 | vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; |
| 1718 | vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; |
| 1719 | vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; |
| 1720 | vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; |
| 1721 | vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx; |
| 1722 | vcpu->arch.cpuid_entries[i].index = 0; |
| 1723 | vcpu->arch.cpuid_entries[i].flags = 0; |
| 1724 | vcpu->arch.cpuid_entries[i].padding[0] = 0; |
| 1725 | vcpu->arch.cpuid_entries[i].padding[1] = 0; |
| 1726 | vcpu->arch.cpuid_entries[i].padding[2] = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1727 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1728 | vcpu->arch.cpuid_nent = cpuid->nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1729 | cpuid_fix_nx_cap(vcpu); |
| 1730 | r = 0; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1731 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1732 | kvm_x86_ops->cpuid_update(vcpu); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1733 | |
| 1734 | out_free: |
| 1735 | vfree(cpuid_entries); |
| 1736 | out: |
| 1737 | return r; |
| 1738 | } |
| 1739 | |
| 1740 | static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1741 | struct kvm_cpuid2 *cpuid, |
| 1742 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1743 | { |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1744 | int r; |
| 1745 | |
| 1746 | r = -E2BIG; |
| 1747 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1748 | goto out; |
| 1749 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1750 | if (copy_from_user(&vcpu->arch.cpuid_entries, entries, |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1751 | cpuid->nent * sizeof(struct kvm_cpuid_entry2))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1752 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1753 | vcpu->arch.cpuid_nent = cpuid->nent; |
Gleb Natapov | fc61b80 | 2009-07-05 17:39:35 +0300 | [diff] [blame] | 1754 | kvm_apic_set_version(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 1755 | kvm_x86_ops->cpuid_update(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1756 | return 0; |
| 1757 | |
| 1758 | out: |
| 1759 | return r; |
| 1760 | } |
| 1761 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1762 | static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1763 | struct kvm_cpuid2 *cpuid, |
| 1764 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1765 | { |
| 1766 | int r; |
| 1767 | |
| 1768 | r = -E2BIG; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1769 | if (cpuid->nent < vcpu->arch.cpuid_nent) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1770 | goto out; |
| 1771 | r = -EFAULT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1772 | if (copy_to_user(entries, &vcpu->arch.cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1773 | vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1774 | goto out; |
| 1775 | return 0; |
| 1776 | |
| 1777 | out: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1778 | cpuid->nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1779 | return r; |
| 1780 | } |
| 1781 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1782 | 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] | 1783 | u32 index) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1784 | { |
| 1785 | entry->function = function; |
| 1786 | entry->index = index; |
| 1787 | cpuid_count(entry->function, entry->index, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1788 | &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1789 | entry->flags = 0; |
| 1790 | } |
| 1791 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1792 | #define F(x) bit(X86_FEATURE_##x) |
| 1793 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1794 | static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, |
| 1795 | u32 index, int *nent, int maxnent) |
| 1796 | { |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1797 | unsigned f_nx = is_efer_nx() ? F(NX) : 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1798 | #ifdef CONFIG_X86_64 |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1799 | unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL) |
| 1800 | ? F(GBPAGES) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1801 | unsigned f_lm = F(LM); |
| 1802 | #else |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 1803 | unsigned f_gbpages = 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1804 | unsigned f_lm = 0; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1805 | #endif |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1806 | unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1807 | |
| 1808 | /* cpuid 1.edx */ |
| 1809 | const u32 kvm_supported_word0_x86_features = |
| 1810 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1811 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1812 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) | |
| 1813 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1814 | F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) | |
| 1815 | 0 /* Reserved, DS, ACPI */ | F(MMX) | |
| 1816 | F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) | |
| 1817 | 0 /* HTT, TM, Reserved, PBE */; |
| 1818 | /* cpuid 0x80000001.edx */ |
| 1819 | const u32 kvm_supported_word1_x86_features = |
| 1820 | F(FPU) | F(VME) | F(DE) | F(PSE) | |
| 1821 | F(TSC) | F(MSR) | F(PAE) | F(MCE) | |
| 1822 | F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) | |
| 1823 | F(MTRR) | F(PGE) | F(MCA) | F(CMOV) | |
| 1824 | F(PAT) | F(PSE36) | 0 /* Reserved */ | |
| 1825 | f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1826 | F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1827 | 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW); |
| 1828 | /* cpuid 1.ecx */ |
| 1829 | const u32 kvm_supported_word4_x86_features = |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1830 | F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ | |
| 1831 | 0 /* DS-CPL, VMX, SMX, EST */ | |
| 1832 | 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ | |
| 1833 | 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ | |
| 1834 | 0 /* Reserved, DCA */ | F(XMM4_1) | |
Gleb Natapov | 0105d1a | 2009-07-05 17:39:36 +0300 | [diff] [blame] | 1835 | F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) | |
Avi Kivity | d149c73 | 2009-05-10 14:41:56 +0300 | [diff] [blame] | 1836 | 0 /* Reserved, XSAVE, OSXSAVE */; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1837 | /* cpuid 0x80000001.ecx */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1838 | const u32 kvm_supported_word6_x86_features = |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1839 | F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ | |
| 1840 | F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) | |
| 1841 | F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) | |
| 1842 | 0 /* SKINIT */ | 0 /* WDT */; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1843 | |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1844 | /* all calls to cpuid_count() should be made on the same cpu */ |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1845 | get_cpu(); |
| 1846 | do_cpuid_1_ent(entry, function, index); |
| 1847 | ++*nent; |
| 1848 | |
| 1849 | switch (function) { |
| 1850 | case 0: |
| 1851 | entry->eax = min(entry->eax, (u32)0xb); |
| 1852 | break; |
| 1853 | case 1: |
| 1854 | entry->edx &= kvm_supported_word0_x86_features; |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1855 | entry->ecx &= kvm_supported_word4_x86_features; |
Gleb Natapov | 0d1de2d9 | 2009-07-12 16:10:55 +0300 | [diff] [blame] | 1856 | /* we support x2apic emulation even if host does not support |
| 1857 | * it since we emulate x2apic in software */ |
| 1858 | entry->ecx |= F(X2APIC); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1859 | break; |
| 1860 | /* function 2 entries are STATEFUL. That is, repeated cpuid commands |
| 1861 | * may return different values. This forces us to get_cpu() before |
| 1862 | * issuing the first command, and also to emulate this annoying behavior |
| 1863 | * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */ |
| 1864 | case 2: { |
| 1865 | int t, times = entry->eax & 0xff; |
| 1866 | |
| 1867 | entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
Nitin A Kamble | 0fdf8e5 | 2008-11-05 15:56:21 -0800 | [diff] [blame] | 1868 | entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1869 | for (t = 1; t < times && *nent < maxnent; ++t) { |
| 1870 | do_cpuid_1_ent(&entry[t], function, 0); |
| 1871 | entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC; |
| 1872 | ++*nent; |
| 1873 | } |
| 1874 | break; |
| 1875 | } |
| 1876 | /* function 4 and 0xb have additional index. */ |
| 1877 | case 4: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1878 | int i, cache_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1879 | |
| 1880 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1881 | /* read more entries until cache_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1882 | for (i = 1; *nent < maxnent; ++i) { |
| 1883 | cache_type = entry[i - 1].eax & 0x1f; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1884 | if (!cache_type) |
| 1885 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1886 | do_cpuid_1_ent(&entry[i], function, i); |
| 1887 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1888 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1889 | ++*nent; |
| 1890 | } |
| 1891 | break; |
| 1892 | } |
| 1893 | case 0xb: { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1894 | int i, level_type; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1895 | |
| 1896 | entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1897 | /* read more entries until level_type is zero */ |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1898 | for (i = 1; *nent < maxnent; ++i) { |
Nitin A Kamble | 0853d2c | 2008-11-05 15:37:36 -0800 | [diff] [blame] | 1899 | level_type = entry[i - 1].ecx & 0xff00; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1900 | if (!level_type) |
| 1901 | break; |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 1902 | do_cpuid_1_ent(&entry[i], function, i); |
| 1903 | entry[i].flags |= |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1904 | KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
| 1905 | ++*nent; |
| 1906 | } |
| 1907 | break; |
| 1908 | } |
| 1909 | case 0x80000000: |
| 1910 | entry->eax = min(entry->eax, 0x8000001a); |
| 1911 | break; |
| 1912 | case 0x80000001: |
| 1913 | entry->edx &= kvm_supported_word1_x86_features; |
| 1914 | entry->ecx &= kvm_supported_word6_x86_features; |
| 1915 | break; |
| 1916 | } |
| 1917 | put_cpu(); |
| 1918 | } |
| 1919 | |
Avi Kivity | 7faa4ee | 2009-05-10 13:55:35 +0300 | [diff] [blame] | 1920 | #undef F |
| 1921 | |
Avi Kivity | 674eea0 | 2008-02-11 18:37:23 +0200 | [diff] [blame] | 1922 | static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1923 | struct kvm_cpuid_entry2 __user *entries) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1924 | { |
| 1925 | struct kvm_cpuid_entry2 *cpuid_entries; |
| 1926 | int limit, nent = 0, r = -E2BIG; |
| 1927 | u32 func; |
| 1928 | |
| 1929 | if (cpuid->nent < 1) |
| 1930 | goto out; |
Avi Kivity | 6a54435 | 2009-10-04 16:45:13 +0200 | [diff] [blame] | 1931 | if (cpuid->nent > KVM_MAX_CPUID_ENTRIES) |
| 1932 | cpuid->nent = KVM_MAX_CPUID_ENTRIES; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1933 | r = -ENOMEM; |
| 1934 | cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent); |
| 1935 | if (!cpuid_entries) |
| 1936 | goto out; |
| 1937 | |
| 1938 | do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent); |
| 1939 | limit = cpuid_entries[0].eax; |
| 1940 | for (func = 1; func <= limit && nent < cpuid->nent; ++func) |
| 1941 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1942 | &nent, cpuid->nent); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1943 | r = -E2BIG; |
| 1944 | if (nent >= cpuid->nent) |
| 1945 | goto out_free; |
| 1946 | |
| 1947 | do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent); |
| 1948 | limit = cpuid_entries[nent - 1].eax; |
| 1949 | for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func) |
| 1950 | do_cpuid_ent(&cpuid_entries[nent], func, 0, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1951 | &nent, cpuid->nent); |
Mark McLoughlin | cb00764 | 2009-05-12 12:36:44 +0100 | [diff] [blame] | 1952 | r = -E2BIG; |
| 1953 | if (nent >= cpuid->nent) |
| 1954 | goto out_free; |
| 1955 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1956 | r = -EFAULT; |
| 1957 | if (copy_to_user(entries, cpuid_entries, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 1958 | nent * sizeof(struct kvm_cpuid_entry2))) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 1959 | goto out_free; |
| 1960 | cpuid->nent = nent; |
| 1961 | r = 0; |
| 1962 | |
| 1963 | out_free: |
| 1964 | vfree(cpuid_entries); |
| 1965 | out: |
| 1966 | return r; |
| 1967 | } |
| 1968 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1969 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 1970 | struct kvm_lapic_state *s) |
| 1971 | { |
| 1972 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1973 | memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1974 | vcpu_put(vcpu); |
| 1975 | |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
| 1979 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 1980 | struct kvm_lapic_state *s) |
| 1981 | { |
| 1982 | vcpu_load(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1983 | memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1984 | kvm_apic_post_state_restore(vcpu); |
Gleb Natapov | cb142eb | 2009-08-09 15:17:40 +0300 | [diff] [blame] | 1985 | update_cr8_intercept(vcpu); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1986 | vcpu_put(vcpu); |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 1991 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 1992 | struct kvm_interrupt *irq) |
| 1993 | { |
| 1994 | if (irq->irq < 0 || irq->irq >= 256) |
| 1995 | return -EINVAL; |
| 1996 | if (irqchip_in_kernel(vcpu->kvm)) |
| 1997 | return -ENXIO; |
| 1998 | vcpu_load(vcpu); |
| 1999 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2000 | kvm_queue_interrupt(vcpu, irq->irq, false); |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2001 | |
| 2002 | vcpu_put(vcpu); |
| 2003 | |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2007 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 2008 | { |
| 2009 | vcpu_load(vcpu); |
| 2010 | kvm_inject_nmi(vcpu); |
| 2011 | vcpu_put(vcpu); |
| 2012 | |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2016 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 2017 | struct kvm_tpr_access_ctl *tac) |
| 2018 | { |
| 2019 | if (tac->flags) |
| 2020 | return -EINVAL; |
| 2021 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2025 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 2026 | u64 mcg_cap) |
| 2027 | { |
| 2028 | int r; |
| 2029 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 2030 | |
| 2031 | r = -EINVAL; |
Jan Kiszka | a9e38c3e | 2009-10-23 09:37:00 +0200 | [diff] [blame] | 2032 | if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS) |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2033 | goto out; |
| 2034 | if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000)) |
| 2035 | goto out; |
| 2036 | r = 0; |
| 2037 | vcpu->arch.mcg_cap = mcg_cap; |
| 2038 | /* Init IA32_MCG_CTL to all 1s */ |
| 2039 | if (mcg_cap & MCG_CTL_P) |
| 2040 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 2041 | /* Init IA32_MCi_CTL to all 1s */ |
| 2042 | for (bank = 0; bank < bank_num; bank++) |
| 2043 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
| 2044 | out: |
| 2045 | return r; |
| 2046 | } |
| 2047 | |
| 2048 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 2049 | struct kvm_x86_mce *mce) |
| 2050 | { |
| 2051 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2052 | unsigned bank_num = mcg_cap & 0xff; |
| 2053 | u64 *banks = vcpu->arch.mce_banks; |
| 2054 | |
| 2055 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 2056 | return -EINVAL; |
| 2057 | /* |
| 2058 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 2059 | * reporting is disabled |
| 2060 | */ |
| 2061 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 2062 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 2063 | return 0; |
| 2064 | banks += 4 * mce->bank; |
| 2065 | /* |
| 2066 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 2067 | * reporting is disabled for the bank |
| 2068 | */ |
| 2069 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 2070 | return 0; |
| 2071 | if (mce->status & MCI_STATUS_UC) { |
| 2072 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2073 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2074 | printk(KERN_DEBUG "kvm: set_mce: " |
| 2075 | "injects mce exception while " |
| 2076 | "previous one is in progress!\n"); |
| 2077 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); |
| 2078 | return 0; |
| 2079 | } |
| 2080 | if (banks[1] & MCI_STATUS_VAL) |
| 2081 | mce->status |= MCI_STATUS_OVER; |
| 2082 | banks[2] = mce->addr; |
| 2083 | banks[3] = mce->misc; |
| 2084 | vcpu->arch.mcg_status = mce->mcg_status; |
| 2085 | banks[1] = mce->status; |
| 2086 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 2087 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 2088 | || !(banks[1] & MCI_STATUS_UC)) { |
| 2089 | if (banks[1] & MCI_STATUS_VAL) |
| 2090 | mce->status |= MCI_STATUS_OVER; |
| 2091 | banks[2] = mce->addr; |
| 2092 | banks[3] = mce->misc; |
| 2093 | banks[1] = mce->status; |
| 2094 | } else |
| 2095 | banks[1] |= MCI_STATUS_OVER; |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2099 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 2100 | struct kvm_vcpu_events *events) |
| 2101 | { |
| 2102 | vcpu_load(vcpu); |
| 2103 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2104 | events->exception.injected = |
| 2105 | vcpu->arch.exception.pending && |
| 2106 | !kvm_exception_is_soft(vcpu->arch.exception.nr); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2107 | events->exception.nr = vcpu->arch.exception.nr; |
| 2108 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
| 2109 | events->exception.error_code = vcpu->arch.exception.error_code; |
| 2110 | |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2111 | events->interrupt.injected = |
| 2112 | vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2113 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
Jan Kiszka | 03b82a3 | 2010-02-15 10:45:41 +0100 | [diff] [blame] | 2114 | events->interrupt.soft = 0; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2115 | events->interrupt.shadow = |
| 2116 | kvm_x86_ops->get_interrupt_shadow(vcpu, |
| 2117 | KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2118 | |
| 2119 | events->nmi.injected = vcpu->arch.nmi_injected; |
| 2120 | events->nmi.pending = vcpu->arch.nmi_pending; |
| 2121 | events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu); |
| 2122 | |
| 2123 | events->sipi_vector = vcpu->arch.sipi_vector; |
| 2124 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2125 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2126 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2127 | | KVM_VCPUEVENT_VALID_SHADOW); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2128 | |
| 2129 | vcpu_put(vcpu); |
| 2130 | } |
| 2131 | |
| 2132 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 2133 | struct kvm_vcpu_events *events) |
| 2134 | { |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2135 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2136 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 2137 | | KVM_VCPUEVENT_VALID_SHADOW)) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2138 | return -EINVAL; |
| 2139 | |
| 2140 | vcpu_load(vcpu); |
| 2141 | |
| 2142 | vcpu->arch.exception.pending = events->exception.injected; |
| 2143 | vcpu->arch.exception.nr = events->exception.nr; |
| 2144 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 2145 | vcpu->arch.exception.error_code = events->exception.error_code; |
| 2146 | |
| 2147 | vcpu->arch.interrupt.pending = events->interrupt.injected; |
| 2148 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 2149 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
| 2150 | if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm)) |
| 2151 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2152 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
| 2153 | kvm_x86_ops->set_interrupt_shadow(vcpu, |
| 2154 | events->interrupt.shadow); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2155 | |
| 2156 | vcpu->arch.nmi_injected = events->nmi.injected; |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2157 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 2158 | vcpu->arch.nmi_pending = events->nmi.pending; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2159 | kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked); |
| 2160 | |
Jan Kiszka | dab4b91 | 2009-12-06 18:24:15 +0100 | [diff] [blame] | 2161 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR) |
| 2162 | vcpu->arch.sipi_vector = events->sipi_vector; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2163 | |
| 2164 | vcpu_put(vcpu); |
| 2165 | |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2169 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 2170 | struct kvm_debugregs *dbgregs) |
| 2171 | { |
| 2172 | vcpu_load(vcpu); |
| 2173 | |
| 2174 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
| 2175 | dbgregs->dr6 = vcpu->arch.dr6; |
| 2176 | dbgregs->dr7 = vcpu->arch.dr7; |
| 2177 | dbgregs->flags = 0; |
| 2178 | |
| 2179 | vcpu_put(vcpu); |
| 2180 | } |
| 2181 | |
| 2182 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 2183 | struct kvm_debugregs *dbgregs) |
| 2184 | { |
| 2185 | if (dbgregs->flags) |
| 2186 | return -EINVAL; |
| 2187 | |
| 2188 | vcpu_load(vcpu); |
| 2189 | |
| 2190 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
| 2191 | vcpu->arch.dr6 = dbgregs->dr6; |
| 2192 | vcpu->arch.dr7 = dbgregs->dr7; |
| 2193 | |
| 2194 | vcpu_put(vcpu); |
| 2195 | |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2199 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 2200 | unsigned int ioctl, unsigned long arg) |
| 2201 | { |
| 2202 | struct kvm_vcpu *vcpu = filp->private_data; |
| 2203 | void __user *argp = (void __user *)arg; |
| 2204 | int r; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2205 | struct kvm_lapic_state *lapic = NULL; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2206 | |
| 2207 | switch (ioctl) { |
| 2208 | case KVM_GET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2209 | r = -EINVAL; |
| 2210 | if (!vcpu->arch.apic) |
| 2211 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2212 | lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2213 | |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2214 | r = -ENOMEM; |
| 2215 | if (!lapic) |
| 2216 | goto out; |
| 2217 | r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2218 | if (r) |
| 2219 | goto out; |
| 2220 | r = -EFAULT; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2221 | if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2222 | goto out; |
| 2223 | r = 0; |
| 2224 | break; |
| 2225 | } |
| 2226 | case KVM_SET_LAPIC: { |
Marcelo Tosatti | 2204ae3 | 2009-10-29 13:44:16 -0200 | [diff] [blame] | 2227 | r = -EINVAL; |
| 2228 | if (!vcpu->arch.apic) |
| 2229 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2230 | lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); |
| 2231 | r = -ENOMEM; |
| 2232 | if (!lapic) |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2233 | goto out; |
Dave Hansen | b772ff3 | 2008-08-11 10:01:47 -0700 | [diff] [blame] | 2234 | r = -EFAULT; |
| 2235 | if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) |
| 2236 | goto out; |
| 2237 | r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2238 | if (r) |
| 2239 | goto out; |
| 2240 | r = 0; |
| 2241 | break; |
| 2242 | } |
Zhang Xiantao | f77bc6a | 2007-11-21 04:36:41 +0800 | [diff] [blame] | 2243 | case KVM_INTERRUPT: { |
| 2244 | struct kvm_interrupt irq; |
| 2245 | |
| 2246 | r = -EFAULT; |
| 2247 | if (copy_from_user(&irq, argp, sizeof irq)) |
| 2248 | goto out; |
| 2249 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 2250 | if (r) |
| 2251 | goto out; |
| 2252 | r = 0; |
| 2253 | break; |
| 2254 | } |
Jan Kiszka | c4abb7c | 2008-09-26 09:30:55 +0200 | [diff] [blame] | 2255 | case KVM_NMI: { |
| 2256 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 2257 | if (r) |
| 2258 | goto out; |
| 2259 | r = 0; |
| 2260 | break; |
| 2261 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2262 | case KVM_SET_CPUID: { |
| 2263 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 2264 | struct kvm_cpuid cpuid; |
| 2265 | |
| 2266 | r = -EFAULT; |
| 2267 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2268 | goto out; |
| 2269 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 2270 | if (r) |
| 2271 | goto out; |
| 2272 | break; |
| 2273 | } |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2274 | case KVM_SET_CPUID2: { |
| 2275 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2276 | struct kvm_cpuid2 cpuid; |
| 2277 | |
| 2278 | r = -EFAULT; |
| 2279 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2280 | goto out; |
| 2281 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2282 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2283 | if (r) |
| 2284 | goto out; |
| 2285 | break; |
| 2286 | } |
| 2287 | case KVM_GET_CPUID2: { |
| 2288 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 2289 | struct kvm_cpuid2 cpuid; |
| 2290 | |
| 2291 | r = -EFAULT; |
| 2292 | if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid)) |
| 2293 | goto out; |
| 2294 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 2295 | cpuid_arg->entries); |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 2296 | if (r) |
| 2297 | goto out; |
| 2298 | r = -EFAULT; |
| 2299 | if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid)) |
| 2300 | goto out; |
| 2301 | r = 0; |
| 2302 | break; |
| 2303 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2304 | case KVM_GET_MSRS: |
| 2305 | r = msr_io(vcpu, argp, kvm_get_msr, 1); |
| 2306 | break; |
| 2307 | case KVM_SET_MSRS: |
| 2308 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 2309 | break; |
Avi Kivity | b209749f | 2007-10-22 16:50:39 +0200 | [diff] [blame] | 2310 | case KVM_TPR_ACCESS_REPORTING: { |
| 2311 | struct kvm_tpr_access_ctl tac; |
| 2312 | |
| 2313 | r = -EFAULT; |
| 2314 | if (copy_from_user(&tac, argp, sizeof tac)) |
| 2315 | goto out; |
| 2316 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 2317 | if (r) |
| 2318 | goto out; |
| 2319 | r = -EFAULT; |
| 2320 | if (copy_to_user(argp, &tac, sizeof tac)) |
| 2321 | goto out; |
| 2322 | r = 0; |
| 2323 | break; |
| 2324 | }; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 2325 | case KVM_SET_VAPIC_ADDR: { |
| 2326 | struct kvm_vapic_addr va; |
| 2327 | |
| 2328 | r = -EINVAL; |
| 2329 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 2330 | goto out; |
| 2331 | r = -EFAULT; |
| 2332 | if (copy_from_user(&va, argp, sizeof va)) |
| 2333 | goto out; |
| 2334 | r = 0; |
| 2335 | kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 2336 | break; |
| 2337 | } |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 2338 | case KVM_X86_SETUP_MCE: { |
| 2339 | u64 mcg_cap; |
| 2340 | |
| 2341 | r = -EFAULT; |
| 2342 | if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap)) |
| 2343 | goto out; |
| 2344 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 2345 | break; |
| 2346 | } |
| 2347 | case KVM_X86_SET_MCE: { |
| 2348 | struct kvm_x86_mce mce; |
| 2349 | |
| 2350 | r = -EFAULT; |
| 2351 | if (copy_from_user(&mce, argp, sizeof mce)) |
| 2352 | goto out; |
| 2353 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 2354 | break; |
| 2355 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2356 | case KVM_GET_VCPU_EVENTS: { |
| 2357 | struct kvm_vcpu_events events; |
| 2358 | |
| 2359 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 2360 | |
| 2361 | r = -EFAULT; |
| 2362 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 2363 | break; |
| 2364 | r = 0; |
| 2365 | break; |
| 2366 | } |
| 2367 | case KVM_SET_VCPU_EVENTS: { |
| 2368 | struct kvm_vcpu_events events; |
| 2369 | |
| 2370 | r = -EFAULT; |
| 2371 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 2372 | break; |
| 2373 | |
| 2374 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 2375 | break; |
| 2376 | } |
Jan Kiszka | a1efbe7 | 2010-02-15 10:45:43 +0100 | [diff] [blame] | 2377 | case KVM_GET_DEBUGREGS: { |
| 2378 | struct kvm_debugregs dbgregs; |
| 2379 | |
| 2380 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 2381 | |
| 2382 | r = -EFAULT; |
| 2383 | if (copy_to_user(argp, &dbgregs, |
| 2384 | sizeof(struct kvm_debugregs))) |
| 2385 | break; |
| 2386 | r = 0; |
| 2387 | break; |
| 2388 | } |
| 2389 | case KVM_SET_DEBUGREGS: { |
| 2390 | struct kvm_debugregs dbgregs; |
| 2391 | |
| 2392 | r = -EFAULT; |
| 2393 | if (copy_from_user(&dbgregs, argp, |
| 2394 | sizeof(struct kvm_debugregs))) |
| 2395 | break; |
| 2396 | |
| 2397 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 2398 | break; |
| 2399 | } |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2400 | default: |
| 2401 | r = -EINVAL; |
| 2402 | } |
| 2403 | out: |
Wei Yongjun | 7a6ce84 | 2009-03-31 16:47:44 +0800 | [diff] [blame] | 2404 | kfree(lapic); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 2405 | return r; |
| 2406 | } |
| 2407 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2408 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 2409 | { |
| 2410 | int ret; |
| 2411 | |
| 2412 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 2413 | return -1; |
| 2414 | ret = kvm_x86_ops->set_tss_addr(kvm, addr); |
| 2415 | return ret; |
| 2416 | } |
| 2417 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2418 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 2419 | u64 ident_addr) |
| 2420 | { |
| 2421 | kvm->arch.ept_identity_map_addr = ident_addr; |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2425 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
| 2426 | u32 kvm_nr_mmu_pages) |
| 2427 | { |
| 2428 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 2429 | return -EINVAL; |
| 2430 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2431 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2432 | spin_lock(&kvm->mmu_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2433 | |
| 2434 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2435 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2436 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2437 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2438 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
| 2443 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2444 | return kvm->arch.n_alloc_mmu_pages; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2445 | } |
| 2446 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2447 | gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn) |
| 2448 | { |
| 2449 | int i; |
| 2450 | struct kvm_mem_alias *alias; |
| 2451 | struct kvm_mem_aliases *aliases; |
| 2452 | |
| 2453 | aliases = rcu_dereference(kvm->arch.aliases); |
| 2454 | |
| 2455 | for (i = 0; i < aliases->naliases; ++i) { |
| 2456 | alias = &aliases->aliases[i]; |
| 2457 | if (alias->flags & KVM_ALIAS_INVALID) |
| 2458 | continue; |
| 2459 | if (gfn >= alias->base_gfn |
| 2460 | && gfn < alias->base_gfn + alias->npages) |
| 2461 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2462 | } |
| 2463 | return gfn; |
| 2464 | } |
| 2465 | |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2466 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
| 2467 | { |
| 2468 | int i; |
| 2469 | struct kvm_mem_alias *alias; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2470 | struct kvm_mem_aliases *aliases; |
| 2471 | |
| 2472 | aliases = rcu_dereference(kvm->arch.aliases); |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2473 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2474 | for (i = 0; i < aliases->naliases; ++i) { |
| 2475 | alias = &aliases->aliases[i]; |
Zhang Xiantao | e9f85cd | 2007-11-22 11:20:33 +0800 | [diff] [blame] | 2476 | if (gfn >= alias->base_gfn |
| 2477 | && gfn < alias->base_gfn + alias->npages) |
| 2478 | return alias->target_gfn + gfn - alias->base_gfn; |
| 2479 | } |
| 2480 | return gfn; |
| 2481 | } |
| 2482 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2483 | /* |
| 2484 | * Set a new alias region. Aliases map a portion of physical memory into |
| 2485 | * another portion. This is useful for memory windows, for example the PC |
| 2486 | * VGA region. |
| 2487 | */ |
| 2488 | static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm, |
| 2489 | struct kvm_memory_alias *alias) |
| 2490 | { |
| 2491 | int r, n; |
| 2492 | struct kvm_mem_alias *p; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2493 | struct kvm_mem_aliases *aliases, *old_aliases; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2494 | |
| 2495 | r = -EINVAL; |
| 2496 | /* General sanity checks */ |
| 2497 | if (alias->memory_size & (PAGE_SIZE - 1)) |
| 2498 | goto out; |
| 2499 | if (alias->guest_phys_addr & (PAGE_SIZE - 1)) |
| 2500 | goto out; |
| 2501 | if (alias->slot >= KVM_ALIAS_SLOTS) |
| 2502 | goto out; |
| 2503 | if (alias->guest_phys_addr + alias->memory_size |
| 2504 | < alias->guest_phys_addr) |
| 2505 | goto out; |
| 2506 | if (alias->target_phys_addr + alias->memory_size |
| 2507 | < alias->target_phys_addr) |
| 2508 | goto out; |
| 2509 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2510 | r = -ENOMEM; |
| 2511 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2512 | if (!aliases) |
| 2513 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2514 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2515 | mutex_lock(&kvm->slots_lock); |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2516 | |
| 2517 | /* invalidate any gfn reference in case of deletion/shrinking */ |
| 2518 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
| 2519 | aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID; |
| 2520 | old_aliases = kvm->arch.aliases; |
| 2521 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2522 | synchronize_srcu_expedited(&kvm->srcu); |
| 2523 | kvm_mmu_zap_all(kvm); |
| 2524 | kfree(old_aliases); |
| 2525 | |
| 2526 | r = -ENOMEM; |
| 2527 | aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 2528 | if (!aliases) |
| 2529 | goto out_unlock; |
| 2530 | |
| 2531 | memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases)); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2532 | |
| 2533 | p = &aliases->aliases[alias->slot]; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2534 | p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT; |
| 2535 | p->npages = alias->memory_size >> PAGE_SHIFT; |
| 2536 | p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT; |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2537 | p->flags &= ~(KVM_ALIAS_INVALID); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2538 | |
| 2539 | for (n = KVM_ALIAS_SLOTS; n > 0; --n) |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2540 | if (aliases->aliases[n - 1].npages) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2541 | break; |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 2542 | aliases->naliases = n; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2543 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2544 | old_aliases = kvm->arch.aliases; |
| 2545 | rcu_assign_pointer(kvm->arch.aliases, aliases); |
| 2546 | synchronize_srcu_expedited(&kvm->srcu); |
| 2547 | kfree(old_aliases); |
| 2548 | r = 0; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2549 | |
Marcelo Tosatti | a983fb2 | 2009-12-23 14:35:23 -0200 | [diff] [blame] | 2550 | out_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2551 | mutex_unlock(&kvm->slots_lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2552 | out: |
| 2553 | return r; |
| 2554 | } |
| 2555 | |
| 2556 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2557 | { |
| 2558 | int r; |
| 2559 | |
| 2560 | r = 0; |
| 2561 | switch (chip->chip_id) { |
| 2562 | case KVM_IRQCHIP_PIC_MASTER: |
| 2563 | memcpy(&chip->chip.pic, |
| 2564 | &pic_irqchip(kvm)->pics[0], |
| 2565 | sizeof(struct kvm_pic_state)); |
| 2566 | break; |
| 2567 | case KVM_IRQCHIP_PIC_SLAVE: |
| 2568 | memcpy(&chip->chip.pic, |
| 2569 | &pic_irqchip(kvm)->pics[1], |
| 2570 | sizeof(struct kvm_pic_state)); |
| 2571 | break; |
| 2572 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2573 | r = kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2574 | break; |
| 2575 | default: |
| 2576 | r = -EINVAL; |
| 2577 | break; |
| 2578 | } |
| 2579 | return r; |
| 2580 | } |
| 2581 | |
| 2582 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 2583 | { |
| 2584 | int r; |
| 2585 | |
| 2586 | r = 0; |
| 2587 | switch (chip->chip_id) { |
| 2588 | case KVM_IRQCHIP_PIC_MASTER: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2589 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2590 | memcpy(&pic_irqchip(kvm)->pics[0], |
| 2591 | &chip->chip.pic, |
| 2592 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2593 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2594 | break; |
| 2595 | case KVM_IRQCHIP_PIC_SLAVE: |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2596 | raw_spin_lock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2597 | memcpy(&pic_irqchip(kvm)->pics[1], |
| 2598 | &chip->chip.pic, |
| 2599 | sizeof(struct kvm_pic_state)); |
Thomas Gleixner | fa8273e | 2010-02-17 14:00:41 +0000 | [diff] [blame] | 2600 | raw_spin_unlock(&pic_irqchip(kvm)->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2601 | break; |
| 2602 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 2603 | r = kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2604 | break; |
| 2605 | default: |
| 2606 | r = -EINVAL; |
| 2607 | break; |
| 2608 | } |
| 2609 | kvm_pic_update_irq(pic_irqchip(kvm)); |
| 2610 | return r; |
| 2611 | } |
| 2612 | |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2613 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2614 | { |
| 2615 | int r = 0; |
| 2616 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2617 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2618 | memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state)); |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2619 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2620 | return r; |
| 2621 | } |
| 2622 | |
| 2623 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 2624 | { |
| 2625 | int r = 0; |
| 2626 | |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2627 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2628 | memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2629 | kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); |
| 2630 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2631 | return r; |
| 2632 | } |
| 2633 | |
| 2634 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2635 | { |
| 2636 | int r = 0; |
| 2637 | |
| 2638 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2639 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 2640 | sizeof(ps->channels)); |
| 2641 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 2642 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 2643 | return r; |
| 2644 | } |
| 2645 | |
| 2646 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 2647 | { |
| 2648 | int r = 0, start = 0; |
| 2649 | u32 prev_legacy, cur_legacy; |
| 2650 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 2651 | prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2652 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 2653 | if (!prev_legacy && cur_legacy) |
| 2654 | start = 1; |
| 2655 | memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, |
| 2656 | sizeof(kvm->arch.vpit->pit_state.channels)); |
| 2657 | kvm->arch.vpit->pit_state.flags = ps->flags; |
| 2658 | 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] | 2659 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2660 | return r; |
| 2661 | } |
| 2662 | |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2663 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 2664 | struct kvm_reinject_control *control) |
| 2665 | { |
| 2666 | if (!kvm->arch.vpit) |
| 2667 | return -ENXIO; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2668 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2669 | kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject; |
Marcelo Tosatti | 894a9c5 | 2009-06-23 15:05:14 -0300 | [diff] [blame] | 2670 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2671 | return 0; |
| 2672 | } |
| 2673 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2674 | /* |
| 2675 | * Get (and clear) the dirty memory log for a memory slot. |
| 2676 | */ |
| 2677 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 2678 | struct kvm_dirty_log *log) |
| 2679 | { |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2680 | int r, i; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2681 | struct kvm_memory_slot *memslot; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2682 | unsigned long n; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2683 | unsigned long is_dirty = 0; |
| 2684 | unsigned long *dirty_bitmap = NULL; |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2685 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2686 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2687 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2688 | r = -EINVAL; |
| 2689 | if (log->slot >= KVM_MEMORY_SLOTS) |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2690 | goto out; |
| 2691 | |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2692 | memslot = &kvm->memslots->memslots[log->slot]; |
| 2693 | r = -ENOENT; |
| 2694 | if (!memslot->dirty_bitmap) |
| 2695 | goto out; |
| 2696 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 2697 | n = kvm_dirty_bitmap_bytes(memslot); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2698 | |
| 2699 | r = -ENOMEM; |
| 2700 | dirty_bitmap = vmalloc(n); |
| 2701 | if (!dirty_bitmap) |
| 2702 | goto out; |
| 2703 | memset(dirty_bitmap, 0, n); |
| 2704 | |
| 2705 | for (i = 0; !is_dirty && i < n/sizeof(long); i++) |
| 2706 | is_dirty = memslot->dirty_bitmap[i]; |
| 2707 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2708 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 2709 | if (is_dirty) { |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2710 | struct kvm_memslots *slots, *old_slots; |
| 2711 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2712 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2713 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 2714 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2715 | |
| 2716 | slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL); |
| 2717 | if (!slots) |
| 2718 | goto out_free; |
| 2719 | |
| 2720 | memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots)); |
| 2721 | slots->memslots[log->slot].dirty_bitmap = dirty_bitmap; |
| 2722 | |
| 2723 | old_slots = kvm->memslots; |
| 2724 | rcu_assign_pointer(kvm->memslots, slots); |
| 2725 | synchronize_srcu_expedited(&kvm->srcu); |
| 2726 | dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap; |
| 2727 | kfree(old_slots); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2728 | } |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2729 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2730 | r = 0; |
Marcelo Tosatti | b050b01 | 2009-12-23 14:35:22 -0200 | [diff] [blame] | 2731 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n)) |
| 2732 | r = -EFAULT; |
| 2733 | out_free: |
| 2734 | vfree(dirty_bitmap); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2735 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2736 | mutex_unlock(&kvm->slots_lock); |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 2737 | return r; |
| 2738 | } |
| 2739 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2740 | long kvm_arch_vm_ioctl(struct file *filp, |
| 2741 | unsigned int ioctl, unsigned long arg) |
| 2742 | { |
| 2743 | struct kvm *kvm = filp->private_data; |
| 2744 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 2745 | int r = -ENOTTY; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2746 | /* |
| 2747 | * This union makes it completely explicit to gcc-3.x |
| 2748 | * that these two variables' stack usage should be |
| 2749 | * combined, not added together. |
| 2750 | */ |
| 2751 | union { |
| 2752 | struct kvm_pit_state ps; |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2753 | struct kvm_pit_state2 ps2; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2754 | struct kvm_memory_alias alias; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2755 | struct kvm_pit_config pit_config; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2756 | } u; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2757 | |
| 2758 | switch (ioctl) { |
| 2759 | case KVM_SET_TSS_ADDR: |
| 2760 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 2761 | if (r < 0) |
| 2762 | goto out; |
| 2763 | break; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2764 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 2765 | u64 ident_addr; |
| 2766 | |
| 2767 | r = -EFAULT; |
| 2768 | if (copy_from_user(&ident_addr, argp, sizeof ident_addr)) |
| 2769 | goto out; |
| 2770 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
| 2771 | if (r < 0) |
| 2772 | goto out; |
| 2773 | break; |
| 2774 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2775 | case KVM_SET_MEMORY_REGION: { |
| 2776 | struct kvm_memory_region kvm_mem; |
| 2777 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2778 | |
| 2779 | r = -EFAULT; |
| 2780 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 2781 | goto out; |
| 2782 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 2783 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 2784 | kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr; |
| 2785 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 2786 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2787 | if (r) |
| 2788 | goto out; |
| 2789 | break; |
| 2790 | } |
| 2791 | case KVM_SET_NR_MMU_PAGES: |
| 2792 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 2793 | if (r) |
| 2794 | goto out; |
| 2795 | break; |
| 2796 | case KVM_GET_NR_MMU_PAGES: |
| 2797 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 2798 | break; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2799 | case KVM_SET_MEMORY_ALIAS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2800 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2801 | if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias))) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2802 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2803 | r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2804 | if (r) |
| 2805 | goto out; |
| 2806 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2807 | case KVM_CREATE_IRQCHIP: { |
| 2808 | struct kvm_pic *vpic; |
| 2809 | |
| 2810 | mutex_lock(&kvm->lock); |
| 2811 | r = -EEXIST; |
| 2812 | if (kvm->arch.vpic) |
| 2813 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2814 | r = -ENOMEM; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2815 | vpic = kvm_create_pic(kvm); |
| 2816 | if (vpic) { |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2817 | r = kvm_ioapic_init(kvm); |
| 2818 | if (r) { |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2819 | kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, |
| 2820 | &vpic->dev); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2821 | kfree(vpic); |
| 2822 | goto create_irqchip_unlock; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2823 | } |
| 2824 | } else |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2825 | goto create_irqchip_unlock; |
| 2826 | smp_wmb(); |
| 2827 | kvm->arch.vpic = vpic; |
| 2828 | smp_wmb(); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2829 | r = kvm_setup_default_irq_routing(kvm); |
| 2830 | if (r) { |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2831 | mutex_lock(&kvm->irq_lock); |
Wei Yongjun | 72bb2fc | 2010-02-09 10:33:03 +0800 | [diff] [blame] | 2832 | kvm_ioapic_destroy(kvm); |
| 2833 | kvm_destroy_pic(kvm); |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2834 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 2835 | } |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2836 | create_irqchip_unlock: |
| 2837 | mutex_unlock(&kvm->lock); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2838 | break; |
Marcelo Tosatti | 3ddea12 | 2009-10-29 13:44:15 -0200 | [diff] [blame] | 2839 | } |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2840 | case KVM_CREATE_PIT: |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2841 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 2842 | goto create_pit; |
| 2843 | case KVM_CREATE_PIT2: |
| 2844 | r = -EFAULT; |
| 2845 | if (copy_from_user(&u.pit_config, argp, |
| 2846 | sizeof(struct kvm_pit_config))) |
| 2847 | goto out; |
| 2848 | create_pit: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2849 | mutex_lock(&kvm->slots_lock); |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2850 | r = -EEXIST; |
| 2851 | if (kvm->arch.vpit) |
| 2852 | goto create_pit_unlock; |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2853 | r = -ENOMEM; |
Jan Kiszka | c5ff41c | 2009-05-14 22:42:53 +0200 | [diff] [blame] | 2854 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2855 | if (kvm->arch.vpit) |
| 2856 | r = 0; |
Avi Kivity | 269e05e | 2009-01-05 15:21:42 +0200 | [diff] [blame] | 2857 | create_pit_unlock: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2858 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 2859 | break; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2860 | case KVM_IRQ_LINE_STATUS: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2861 | case KVM_IRQ_LINE: { |
| 2862 | struct kvm_irq_level irq_event; |
| 2863 | |
| 2864 | r = -EFAULT; |
| 2865 | if (copy_from_user(&irq_event, argp, sizeof irq_event)) |
| 2866 | goto out; |
| 2867 | if (irqchip_in_kernel(kvm)) { |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2868 | __s32 status; |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2869 | status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 2870 | irq_event.irq, irq_event.level); |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 2871 | if (ioctl == KVM_IRQ_LINE_STATUS) { |
| 2872 | irq_event.status = status; |
| 2873 | if (copy_to_user(argp, &irq_event, |
| 2874 | sizeof irq_event)) |
| 2875 | goto out; |
| 2876 | } |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2877 | r = 0; |
| 2878 | } |
| 2879 | break; |
| 2880 | } |
| 2881 | case KVM_GET_IRQCHIP: { |
| 2882 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2883 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2884 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2885 | r = -ENOMEM; |
| 2886 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2887 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2888 | r = -EFAULT; |
| 2889 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2890 | goto get_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2891 | r = -ENXIO; |
| 2892 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2893 | goto get_irqchip_out; |
| 2894 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 2895 | if (r) |
| 2896 | goto get_irqchip_out; |
| 2897 | r = -EFAULT; |
| 2898 | if (copy_to_user(argp, chip, sizeof *chip)) |
| 2899 | goto get_irqchip_out; |
| 2900 | r = 0; |
| 2901 | get_irqchip_out: |
| 2902 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2903 | if (r) |
| 2904 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | case KVM_SET_IRQCHIP: { |
| 2908 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2909 | struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2910 | |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2911 | r = -ENOMEM; |
| 2912 | if (!chip) |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2913 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2914 | r = -EFAULT; |
| 2915 | if (copy_from_user(chip, argp, sizeof *chip)) |
| 2916 | goto set_irqchip_out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2917 | r = -ENXIO; |
| 2918 | if (!irqchip_in_kernel(kvm)) |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2919 | goto set_irqchip_out; |
| 2920 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
| 2921 | if (r) |
| 2922 | goto set_irqchip_out; |
| 2923 | r = 0; |
| 2924 | set_irqchip_out: |
| 2925 | kfree(chip); |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2926 | if (r) |
| 2927 | goto out; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 2928 | break; |
| 2929 | } |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2930 | case KVM_GET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2931 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2932 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2933 | goto out; |
| 2934 | r = -ENXIO; |
| 2935 | if (!kvm->arch.vpit) |
| 2936 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2937 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2938 | if (r) |
| 2939 | goto out; |
| 2940 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2941 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2942 | goto out; |
| 2943 | r = 0; |
| 2944 | break; |
| 2945 | } |
| 2946 | case KVM_SET_PIT: { |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2947 | r = -EFAULT; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2948 | if (copy_from_user(&u.ps, argp, sizeof u.ps)) |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2949 | goto out; |
| 2950 | r = -ENXIO; |
| 2951 | if (!kvm->arch.vpit) |
| 2952 | goto out; |
Dave Hansen | f0d6627 | 2008-08-11 10:01:45 -0700 | [diff] [blame] | 2953 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Sheng Yang | e0f63cb | 2008-03-04 00:50:59 +0800 | [diff] [blame] | 2954 | if (r) |
| 2955 | goto out; |
| 2956 | r = 0; |
| 2957 | break; |
| 2958 | } |
Beth Kon | e9f4275 | 2009-07-07 11:50:38 -0400 | [diff] [blame] | 2959 | case KVM_GET_PIT2: { |
| 2960 | r = -ENXIO; |
| 2961 | if (!kvm->arch.vpit) |
| 2962 | goto out; |
| 2963 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 2964 | if (r) |
| 2965 | goto out; |
| 2966 | r = -EFAULT; |
| 2967 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 2968 | goto out; |
| 2969 | r = 0; |
| 2970 | break; |
| 2971 | } |
| 2972 | case KVM_SET_PIT2: { |
| 2973 | r = -EFAULT; |
| 2974 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 2975 | goto out; |
| 2976 | r = -ENXIO; |
| 2977 | if (!kvm->arch.vpit) |
| 2978 | goto out; |
| 2979 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
| 2980 | if (r) |
| 2981 | goto out; |
| 2982 | r = 0; |
| 2983 | break; |
| 2984 | } |
Marcelo Tosatti | 52d939a | 2008-12-30 15:55:06 -0200 | [diff] [blame] | 2985 | case KVM_REINJECT_CONTROL: { |
| 2986 | struct kvm_reinject_control control; |
| 2987 | r = -EFAULT; |
| 2988 | if (copy_from_user(&control, argp, sizeof(control))) |
| 2989 | goto out; |
| 2990 | r = kvm_vm_ioctl_reinject(kvm, &control); |
| 2991 | if (r) |
| 2992 | goto out; |
| 2993 | r = 0; |
| 2994 | break; |
| 2995 | } |
Ed Swierk | ffde22a | 2009-10-15 15:21:43 -0700 | [diff] [blame] | 2996 | case KVM_XEN_HVM_CONFIG: { |
| 2997 | r = -EFAULT; |
| 2998 | if (copy_from_user(&kvm->arch.xen_hvm_config, argp, |
| 2999 | sizeof(struct kvm_xen_hvm_config))) |
| 3000 | goto out; |
| 3001 | r = -EINVAL; |
| 3002 | if (kvm->arch.xen_hvm_config.flags) |
| 3003 | goto out; |
| 3004 | r = 0; |
| 3005 | break; |
| 3006 | } |
Glauber Costa | afbcf7a | 2009-10-16 15:28:36 -0400 | [diff] [blame] | 3007 | case KVM_SET_CLOCK: { |
| 3008 | struct timespec now; |
| 3009 | struct kvm_clock_data user_ns; |
| 3010 | u64 now_ns; |
| 3011 | s64 delta; |
| 3012 | |
| 3013 | r = -EFAULT; |
| 3014 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 3015 | goto out; |
| 3016 | |
| 3017 | r = -EINVAL; |
| 3018 | if (user_ns.flags) |
| 3019 | goto out; |
| 3020 | |
| 3021 | r = 0; |
| 3022 | ktime_get_ts(&now); |
| 3023 | now_ns = timespec_to_ns(&now); |
| 3024 | delta = user_ns.clock - now_ns; |
| 3025 | kvm->arch.kvmclock_offset = delta; |
| 3026 | break; |
| 3027 | } |
| 3028 | case KVM_GET_CLOCK: { |
| 3029 | struct timespec now; |
| 3030 | struct kvm_clock_data user_ns; |
| 3031 | u64 now_ns; |
| 3032 | |
| 3033 | ktime_get_ts(&now); |
| 3034 | now_ns = timespec_to_ns(&now); |
| 3035 | user_ns.clock = kvm->arch.kvmclock_offset + now_ns; |
| 3036 | user_ns.flags = 0; |
| 3037 | |
| 3038 | r = -EFAULT; |
| 3039 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 3040 | goto out; |
| 3041 | r = 0; |
| 3042 | break; |
| 3043 | } |
| 3044 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 3045 | default: |
| 3046 | ; |
| 3047 | } |
| 3048 | out: |
| 3049 | return r; |
| 3050 | } |
| 3051 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 3052 | static void kvm_init_msr_list(void) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3053 | { |
| 3054 | u32 dummy[2]; |
| 3055 | unsigned i, j; |
| 3056 | |
Glauber Costa | e3267cb | 2009-10-06 13:24:50 -0400 | [diff] [blame] | 3057 | /* skip the first msrs in the list. KVM-specific */ |
| 3058 | 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] | 3059 | if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0) |
| 3060 | continue; |
| 3061 | if (j < i) |
| 3062 | msrs_to_save[j] = msrs_to_save[i]; |
| 3063 | j++; |
| 3064 | } |
| 3065 | num_msrs_to_save = j; |
| 3066 | } |
| 3067 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3068 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 3069 | const void *v) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3070 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3071 | if (vcpu->arch.apic && |
| 3072 | !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v)) |
| 3073 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3074 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3075 | 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] | 3076 | } |
| 3077 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3078 | 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] | 3079 | { |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3080 | if (vcpu->arch.apic && |
| 3081 | !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v)) |
| 3082 | return 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3083 | |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3084 | 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] | 3085 | } |
| 3086 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3087 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3088 | { |
| 3089 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3090 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3091 | } |
| 3092 | |
| 3093 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3094 | { |
| 3095 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3096 | access |= PFERR_FETCH_MASK; |
| 3097 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3098 | } |
| 3099 | |
| 3100 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3101 | { |
| 3102 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3103 | access |= PFERR_WRITE_MASK; |
| 3104 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error); |
| 3105 | } |
| 3106 | |
| 3107 | /* uses this to access any guest's mapped memory without checking CPL */ |
| 3108 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error) |
| 3109 | { |
| 3110 | return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error); |
| 3111 | } |
| 3112 | |
| 3113 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 3114 | struct kvm_vcpu *vcpu, u32 access, |
| 3115 | u32 *error) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3116 | { |
| 3117 | void *data = val; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3118 | int r = X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3119 | |
| 3120 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3121 | 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] | 3122 | unsigned offset = addr & (PAGE_SIZE-1); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3123 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3124 | int ret; |
| 3125 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3126 | if (gpa == UNMAPPED_GVA) { |
| 3127 | r = X86EMUL_PROPAGATE_FAULT; |
| 3128 | goto out; |
| 3129 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3130 | ret = kvm_read_guest(vcpu->kvm, gpa, data, toread); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3131 | if (ret < 0) { |
| 3132 | r = X86EMUL_UNHANDLEABLE; |
| 3133 | goto out; |
| 3134 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3135 | |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3136 | bytes -= toread; |
| 3137 | data += toread; |
| 3138 | addr += toread; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3139 | } |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3140 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3141 | return r; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3142 | } |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3143 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3144 | /* used for instruction fetching */ |
| 3145 | static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3146 | struct kvm_vcpu *vcpu, u32 *error) |
| 3147 | { |
| 3148 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3149 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, |
| 3150 | access | PFERR_FETCH_MASK, error); |
| 3151 | } |
| 3152 | |
| 3153 | static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes, |
| 3154 | struct kvm_vcpu *vcpu, u32 *error) |
| 3155 | { |
| 3156 | u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
| 3157 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
| 3158 | error); |
| 3159 | } |
| 3160 | |
| 3161 | static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes, |
| 3162 | struct kvm_vcpu *vcpu, u32 *error) |
| 3163 | { |
| 3164 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error); |
| 3165 | } |
| 3166 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 3167 | static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3168 | struct kvm_vcpu *vcpu, u32 *error) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3169 | { |
| 3170 | void *data = val; |
| 3171 | int r = X86EMUL_CONTINUE; |
| 3172 | |
| 3173 | while (bytes) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3174 | gpa_t gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, error); |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3175 | unsigned offset = addr & (PAGE_SIZE-1); |
| 3176 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 3177 | int ret; |
| 3178 | |
| 3179 | if (gpa == UNMAPPED_GVA) { |
| 3180 | r = X86EMUL_PROPAGATE_FAULT; |
| 3181 | goto out; |
| 3182 | } |
| 3183 | ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite); |
| 3184 | if (ret < 0) { |
| 3185 | r = X86EMUL_UNHANDLEABLE; |
| 3186 | goto out; |
| 3187 | } |
| 3188 | |
| 3189 | bytes -= towrite; |
| 3190 | data += towrite; |
| 3191 | addr += towrite; |
| 3192 | } |
| 3193 | out: |
| 3194 | return r; |
| 3195 | } |
| 3196 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3197 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3198 | static int emulator_read_emulated(unsigned long addr, |
| 3199 | void *val, |
| 3200 | unsigned int bytes, |
| 3201 | struct kvm_vcpu *vcpu) |
| 3202 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3203 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3204 | u32 error_code; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3205 | |
| 3206 | if (vcpu->mmio_read_completed) { |
| 3207 | memcpy(val, vcpu->mmio_data, bytes); |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3208 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
| 3209 | vcpu->mmio_phys_addr, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3210 | vcpu->mmio_read_completed = 0; |
| 3211 | return X86EMUL_CONTINUE; |
| 3212 | } |
| 3213 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3214 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code); |
| 3215 | |
| 3216 | if (gpa == UNMAPPED_GVA) { |
| 3217 | kvm_inject_page_fault(vcpu, addr, error_code); |
| 3218 | return X86EMUL_PROPAGATE_FAULT; |
| 3219 | } |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3220 | |
| 3221 | /* For APIC access vmexit */ |
| 3222 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3223 | goto mmio; |
| 3224 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3225 | if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL) |
Izik Eidus | 77c2002 | 2008-12-29 01:42:19 +0200 | [diff] [blame] | 3226 | == X86EMUL_CONTINUE) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3227 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3228 | |
| 3229 | mmio: |
| 3230 | /* |
| 3231 | * Is this MMIO handled locally? |
| 3232 | */ |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3233 | if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) { |
| 3234 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3235 | return X86EMUL_CONTINUE; |
| 3236 | } |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3237 | |
| 3238 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3239 | |
| 3240 | vcpu->mmio_needed = 1; |
| 3241 | vcpu->mmio_phys_addr = gpa; |
| 3242 | vcpu->mmio_size = bytes; |
| 3243 | vcpu->mmio_is_write = 0; |
| 3244 | |
| 3245 | return X86EMUL_UNHANDLEABLE; |
| 3246 | } |
| 3247 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3248 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3249 | const void *val, int bytes) |
| 3250 | { |
| 3251 | int ret; |
| 3252 | |
| 3253 | ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes); |
| 3254 | if (ret < 0) |
| 3255 | return 0; |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 3256 | kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1); |
Avi Kivity | 9f81128 | 2008-03-02 14:06:05 +0200 | [diff] [blame] | 3257 | return 1; |
| 3258 | } |
| 3259 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3260 | static int emulator_write_emulated_onepage(unsigned long addr, |
| 3261 | const void *val, |
| 3262 | unsigned int bytes, |
| 3263 | struct kvm_vcpu *vcpu) |
| 3264 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3265 | gpa_t gpa; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3266 | u32 error_code; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3267 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3268 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3269 | |
| 3270 | if (gpa == UNMAPPED_GVA) { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3271 | kvm_inject_page_fault(vcpu, addr, error_code); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3272 | return X86EMUL_PROPAGATE_FAULT; |
| 3273 | } |
| 3274 | |
| 3275 | /* For APIC access vmexit */ |
| 3276 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3277 | goto mmio; |
| 3278 | |
| 3279 | if (emulator_write_phys(vcpu, gpa, val, bytes)) |
| 3280 | return X86EMUL_CONTINUE; |
| 3281 | |
| 3282 | mmio: |
Avi Kivity | aec51dc | 2009-07-01 16:01:02 +0300 | [diff] [blame] | 3283 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3284 | /* |
| 3285 | * Is this MMIO handled locally? |
| 3286 | */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3287 | if (!vcpu_mmio_write(vcpu, gpa, bytes, val)) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3288 | return X86EMUL_CONTINUE; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3289 | |
| 3290 | vcpu->mmio_needed = 1; |
| 3291 | vcpu->mmio_phys_addr = gpa; |
| 3292 | vcpu->mmio_size = bytes; |
| 3293 | vcpu->mmio_is_write = 1; |
| 3294 | memcpy(vcpu->mmio_data, val, bytes); |
| 3295 | |
| 3296 | return X86EMUL_CONTINUE; |
| 3297 | } |
| 3298 | |
| 3299 | int emulator_write_emulated(unsigned long addr, |
| 3300 | const void *val, |
| 3301 | unsigned int bytes, |
| 3302 | struct kvm_vcpu *vcpu) |
| 3303 | { |
| 3304 | /* Crossing a page boundary? */ |
| 3305 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 3306 | int rc, now; |
| 3307 | |
| 3308 | now = -addr & ~PAGE_MASK; |
| 3309 | rc = emulator_write_emulated_onepage(addr, val, now, vcpu); |
| 3310 | if (rc != X86EMUL_CONTINUE) |
| 3311 | return rc; |
| 3312 | addr += now; |
| 3313 | val += now; |
| 3314 | bytes -= now; |
| 3315 | } |
| 3316 | return emulator_write_emulated_onepage(addr, val, bytes, vcpu); |
| 3317 | } |
| 3318 | EXPORT_SYMBOL_GPL(emulator_write_emulated); |
| 3319 | |
| 3320 | static int emulator_cmpxchg_emulated(unsigned long addr, |
| 3321 | const void *old, |
| 3322 | const void *new, |
| 3323 | unsigned int bytes, |
| 3324 | struct kvm_vcpu *vcpu) |
| 3325 | { |
Marcin Slusarz | 9f51e24 | 2009-08-09 21:54:00 +0200 | [diff] [blame] | 3326 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3327 | #ifndef CONFIG_X86_64 |
| 3328 | /* guests cmpxchg8b have to be emulated atomically */ |
| 3329 | if (bytes == 8) { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3330 | gpa_t gpa; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3331 | struct page *page; |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 3332 | char *kaddr; |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3333 | u64 val; |
| 3334 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3335 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3336 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3337 | if (gpa == UNMAPPED_GVA || |
| 3338 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 3339 | goto emul_write; |
| 3340 | |
| 3341 | if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK)) |
| 3342 | goto emul_write; |
| 3343 | |
| 3344 | val = *(u64 *)new; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3345 | |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3346 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3347 | |
Andrew Morton | c0b49b0 | 2008-02-04 22:27:18 -0800 | [diff] [blame] | 3348 | kaddr = kmap_atomic(page, KM_USER0); |
| 3349 | set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val); |
| 3350 | kunmap_atomic(kaddr, KM_USER0); |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3351 | kvm_release_page_dirty(page); |
| 3352 | } |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3353 | emul_write: |
Marcelo Tosatti | 2bacc55 | 2007-12-12 10:46:12 -0500 | [diff] [blame] | 3354 | #endif |
| 3355 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3356 | return emulator_write_emulated(addr, new, bytes, vcpu); |
| 3357 | } |
| 3358 | |
| 3359 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3360 | { |
| 3361 | return kvm_x86_ops->get_segment_base(vcpu, seg); |
| 3362 | } |
| 3363 | |
| 3364 | int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address) |
| 3365 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3366 | kvm_mmu_invlpg(vcpu, address); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3367 | return X86EMUL_CONTINUE; |
| 3368 | } |
| 3369 | |
| 3370 | int emulate_clts(struct kvm_vcpu *vcpu) |
| 3371 | { |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3372 | 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] | 3373 | kvm_x86_ops->fpu_activate(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3374 | return X86EMUL_CONTINUE; |
| 3375 | } |
| 3376 | |
| 3377 | int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest) |
| 3378 | { |
Jan Kiszka | c76de35 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 3379 | return kvm_x86_ops->get_dr(ctxt->vcpu, dr, dest); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value) |
| 3383 | { |
| 3384 | unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3385 | |
Jan Kiszka | c76de35 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 3386 | return kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) |
| 3390 | { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3391 | u8 opcodes[4]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3392 | unsigned long rip = kvm_rip_read(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3393 | unsigned long rip_linear; |
| 3394 | |
Avi Kivity | f76c710 | 2008-06-13 22:45:42 +0300 | [diff] [blame] | 3395 | if (!printk_ratelimit()) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3396 | return; |
| 3397 | |
Glauber Costa | 25be460 | 2008-06-10 10:46:53 -0300 | [diff] [blame] | 3398 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); |
| 3399 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3400 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3401 | |
| 3402 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", |
| 3403 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3404 | } |
| 3405 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); |
| 3406 | |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 3407 | static struct x86_emulate_ops emulate_ops = { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3408 | .read_std = kvm_read_guest_virt_system, |
| 3409 | .fetch = kvm_fetch_guest_virt, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3410 | .read_emulated = emulator_read_emulated, |
| 3411 | .write_emulated = emulator_write_emulated, |
| 3412 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 3413 | }; |
| 3414 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3415 | static void cache_all_regs(struct kvm_vcpu *vcpu) |
| 3416 | { |
| 3417 | kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3418 | kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3419 | kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 3420 | vcpu->arch.regs_dirty = ~0; |
| 3421 | } |
| 3422 | |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3423 | int emulate_instruction(struct kvm_vcpu *vcpu, |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3424 | unsigned long cr2, |
| 3425 | u16 error_code, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3426 | int emulation_type) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3427 | { |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3428 | int r, shadow_mask; |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3429 | struct decode_cache *c; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3430 | struct kvm_run *run = vcpu->run; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3431 | |
Avi Kivity | 26eef70 | 2008-07-03 14:59:22 +0300 | [diff] [blame] | 3432 | kvm_clear_exception_queue(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3433 | vcpu->arch.mmio_fault_cr2 = cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3434 | /* |
Avi Kivity | 56e8231 | 2009-08-12 15:04:37 +0300 | [diff] [blame] | 3435 | * TODO: fix emulate.c to use guest_read/write_register |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3436 | * instead of direct ->regs accesses, can save hundred cycles |
| 3437 | * on Intel for instructions that don't read/change RSP, for |
| 3438 | * for example. |
| 3439 | */ |
| 3440 | cache_all_regs(vcpu); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3441 | |
| 3442 | vcpu->mmio_is_write = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3443 | vcpu->arch.pio.string = 0; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3444 | |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3445 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3446 | int cs_db, cs_l; |
| 3447 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 3448 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3449 | vcpu->arch.emulate_ctxt.vcpu = vcpu; |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 3450 | vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3451 | vcpu->arch.emulate_ctxt.mode = |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3452 | (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3453 | (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM) |
Gleb Natapov | a004475 | 2010-02-10 14:21:31 +0200 | [diff] [blame] | 3454 | ? X86EMUL_MODE_VM86 : cs_l |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3455 | ? X86EMUL_MODE_PROT64 : cs_db |
| 3456 | ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16; |
| 3457 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3458 | r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3459 | |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3460 | /* Only allow emulation of specific instructions on #UD |
| 3461 | * (namely VMMCALL, sysenter, sysexit, syscall)*/ |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3462 | c = &vcpu->arch.emulate_ctxt.decode; |
Andre Przywara | 0cb5762 | 2009-06-17 15:50:31 +0200 | [diff] [blame] | 3463 | if (emulation_type & EMULTYPE_TRAP_UD) { |
| 3464 | if (!c->twobyte) |
| 3465 | return EMULATE_FAIL; |
| 3466 | switch (c->b) { |
| 3467 | case 0x01: /* VMMCALL */ |
| 3468 | if (c->modrm_mod != 3 || c->modrm_rm != 1) |
| 3469 | return EMULATE_FAIL; |
| 3470 | break; |
| 3471 | case 0x34: /* sysenter */ |
| 3472 | case 0x35: /* sysexit */ |
| 3473 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3474 | return EMULATE_FAIL; |
| 3475 | break; |
| 3476 | case 0x05: /* syscall */ |
| 3477 | if (c->modrm_mod != 0 || c->modrm_rm != 0) |
| 3478 | return EMULATE_FAIL; |
| 3479 | break; |
| 3480 | default: |
| 3481 | return EMULATE_FAIL; |
| 3482 | } |
| 3483 | |
| 3484 | if (!(c->modrm_reg == 0 || c->modrm_reg == 3)) |
| 3485 | return EMULATE_FAIL; |
| 3486 | } |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 3487 | |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3488 | ++vcpu->stat.insn_emulation; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3489 | if (r) { |
Avi Kivity | f2b5756 | 2007-11-18 15:17:51 +0200 | [diff] [blame] | 3490 | ++vcpu->stat.insn_emulation_fail; |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3491 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 3492 | return EMULATE_DONE; |
| 3493 | return EMULATE_FAIL; |
| 3494 | } |
| 3495 | } |
| 3496 | |
Gleb Natapov | ba8afb6 | 2009-04-12 13:36:57 +0300 | [diff] [blame] | 3497 | if (emulation_type & EMULTYPE_SKIP) { |
| 3498 | kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip); |
| 3499 | return EMULATE_DONE; |
| 3500 | } |
| 3501 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3502 | r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops); |
Glauber Costa | 310b5d3 | 2009-05-12 16:21:06 -0400 | [diff] [blame] | 3503 | shadow_mask = vcpu->arch.emulate_ctxt.interruptibility; |
| 3504 | |
| 3505 | if (r == 0) |
| 3506 | kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3507 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3508 | if (vcpu->arch.pio.string) |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3509 | return EMULATE_DO_MMIO; |
| 3510 | |
Gleb Natapov | 112592d | 2010-02-21 15:00:47 +0200 | [diff] [blame] | 3511 | if (r || vcpu->mmio_is_write) { |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3512 | run->exit_reason = KVM_EXIT_MMIO; |
| 3513 | run->mmio.phys_addr = vcpu->mmio_phys_addr; |
| 3514 | memcpy(run->mmio.data, vcpu->mmio_data, 8); |
| 3515 | run->mmio.len = vcpu->mmio_size; |
| 3516 | run->mmio.is_write = vcpu->mmio_is_write; |
| 3517 | } |
| 3518 | |
| 3519 | if (r) { |
| 3520 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
| 3521 | return EMULATE_DONE; |
| 3522 | if (!vcpu->mmio_needed) { |
| 3523 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 3524 | return EMULATE_FAIL; |
| 3525 | } |
| 3526 | return EMULATE_DO_MMIO; |
| 3527 | } |
| 3528 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 3529 | kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags); |
Carsten Otte | bbd9b64 | 2007-10-30 18:44:21 +0100 | [diff] [blame] | 3530 | |
| 3531 | if (vcpu->mmio_is_write) { |
| 3532 | vcpu->mmio_needed = 0; |
| 3533 | return EMULATE_DO_MMIO; |
| 3534 | } |
| 3535 | |
| 3536 | return EMULATE_DONE; |
| 3537 | } |
| 3538 | EXPORT_SYMBOL_GPL(emulate_instruction); |
| 3539 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3540 | static int pio_copy_data(struct kvm_vcpu *vcpu) |
| 3541 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3542 | void *p = vcpu->arch.pio_data; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3543 | gva_t q = vcpu->arch.pio.guest_gva; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3544 | unsigned bytes; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3545 | int ret; |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3546 | u32 error_code; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3547 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3548 | bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count; |
| 3549 | if (vcpu->arch.pio.in) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3550 | ret = kvm_write_guest_virt(q, p, bytes, vcpu, &error_code); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3551 | else |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3552 | ret = kvm_read_guest_virt(q, p, bytes, vcpu, &error_code); |
| 3553 | |
| 3554 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 3555 | kvm_inject_page_fault(vcpu, q, error_code); |
| 3556 | |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3557 | return ret; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | int complete_pio(struct kvm_vcpu *vcpu) |
| 3561 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3562 | struct kvm_pio_request *io = &vcpu->arch.pio; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3563 | long delta; |
| 3564 | int r; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3565 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3566 | |
| 3567 | if (!io->string) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3568 | if (io->in) { |
| 3569 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3570 | memcpy(&val, vcpu->arch.pio_data, io->size); |
| 3571 | kvm_register_write(vcpu, VCPU_REGS_RAX, val); |
| 3572 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3573 | } else { |
| 3574 | if (io->in) { |
| 3575 | r = pio_copy_data(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3576 | if (r) |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3577 | goto out; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | delta = 1; |
| 3581 | if (io->rep) { |
| 3582 | delta *= io->cur_count; |
| 3583 | /* |
| 3584 | * The size of the register should really depend on |
| 3585 | * current address size. |
| 3586 | */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3587 | val = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3588 | val -= delta; |
| 3589 | kvm_register_write(vcpu, VCPU_REGS_RCX, val); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3590 | } |
| 3591 | if (io->down) |
| 3592 | delta = -delta; |
| 3593 | delta *= io->size; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3594 | if (io->in) { |
| 3595 | val = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 3596 | val += delta; |
| 3597 | kvm_register_write(vcpu, VCPU_REGS_RDI, val); |
| 3598 | } else { |
| 3599 | val = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 3600 | val += delta; |
| 3601 | kvm_register_write(vcpu, VCPU_REGS_RSI, val); |
| 3602 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3603 | } |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3604 | out: |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3605 | io->count -= io->cur_count; |
| 3606 | io->cur_count = 0; |
| 3607 | |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3611 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3612 | { |
| 3613 | /* TODO: String I/O for in kernel device */ |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3614 | int r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3615 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3616 | if (vcpu->arch.pio.in) |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3617 | r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3618 | vcpu->arch.pio.size, pd); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3619 | else |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3620 | r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
| 3621 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 3622 | pd); |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3623 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3624 | } |
| 3625 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3626 | static int pio_string_write(struct kvm_vcpu *vcpu) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3627 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3628 | struct kvm_pio_request *io = &vcpu->arch.pio; |
| 3629 | void *pd = vcpu->arch.pio_data; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3630 | int i, r = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3631 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3632 | for (i = 0; i < io->cur_count; i++) { |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 3633 | if (kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3634 | io->port, io->size, pd)) { |
| 3635 | r = -EOPNOTSUPP; |
| 3636 | break; |
| 3637 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3638 | pd += io->size; |
| 3639 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3640 | return r; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3641 | } |
| 3642 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3643 | int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3644 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3645 | unsigned long val; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3646 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3647 | trace_kvm_pio(!in, port, size, 1); |
| 3648 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3649 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3650 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3651 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3652 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3653 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1; |
| 3654 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 3655 | vcpu->arch.pio.in = in; |
| 3656 | vcpu->arch.pio.string = 0; |
| 3657 | vcpu->arch.pio.down = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3658 | vcpu->arch.pio.rep = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3659 | |
Takuya Yoshikawa | 1976d2d | 2010-02-05 17:52:46 +0900 | [diff] [blame] | 3660 | if (!vcpu->arch.pio.in) { |
| 3661 | val = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3662 | memcpy(vcpu->arch.pio_data, &val, 4); |
| 3663 | } |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3664 | |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3665 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3666 | complete_pio(vcpu); |
| 3667 | return 1; |
| 3668 | } |
| 3669 | return 0; |
| 3670 | } |
| 3671 | EXPORT_SYMBOL_GPL(kvm_emulate_pio); |
| 3672 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3673 | int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in, |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3674 | int size, unsigned long count, int down, |
| 3675 | gva_t address, int rep, unsigned port) |
| 3676 | { |
| 3677 | unsigned now, in_page; |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3678 | int ret = 0; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3679 | |
Gleb Natapov | f850e2e | 2010-02-10 14:21:33 +0200 | [diff] [blame] | 3680 | trace_kvm_pio(!in, port, size, count); |
| 3681 | |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3682 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 3683 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3684 | vcpu->run->io.size = vcpu->arch.pio.size = size; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3685 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3686 | vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count; |
| 3687 | vcpu->run->io.port = vcpu->arch.pio.port = port; |
| 3688 | vcpu->arch.pio.in = in; |
| 3689 | vcpu->arch.pio.string = 1; |
| 3690 | vcpu->arch.pio.down = down; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3691 | vcpu->arch.pio.rep = rep; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3692 | |
| 3693 | if (!count) { |
| 3694 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 3695 | return 1; |
| 3696 | } |
| 3697 | |
| 3698 | if (!down) |
| 3699 | in_page = PAGE_SIZE - offset_in_page(address); |
| 3700 | else |
| 3701 | in_page = offset_in_page(address) + size; |
| 3702 | now = min(count, (unsigned long)in_page / size); |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3703 | if (!now) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3704 | now = 1; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3705 | if (down) { |
| 3706 | /* |
| 3707 | * String I/O in reverse. Yuck. Kill the guest, fix later. |
| 3708 | */ |
| 3709 | pr_unimpl(vcpu, "guest string pio down\n"); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3710 | kvm_inject_gp(vcpu, 0); |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3711 | return 1; |
| 3712 | } |
| 3713 | vcpu->run->io.count = now; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3714 | vcpu->arch.pio.cur_count = now; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3715 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3716 | if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3717 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
| 3718 | |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3719 | vcpu->arch.pio.guest_gva = address; |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3720 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3721 | if (!vcpu->arch.pio.in) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3722 | /* string PIO write */ |
| 3723 | ret = pio_copy_data(vcpu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3724 | if (ret == X86EMUL_PROPAGATE_FAULT) |
Izik Eidus | 0f34607 | 2008-12-29 01:42:20 +0200 | [diff] [blame] | 3725 | return 1; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3726 | if (ret == 0 && !pio_string_write(vcpu)) { |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3727 | complete_pio(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3728 | if (vcpu->arch.pio.count == 0) |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3729 | ret = 1; |
| 3730 | } |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 3731 | } |
| 3732 | /* no string PIO read support yet */ |
Carsten Otte | de7d789 | 2007-10-30 18:44:25 +0100 | [diff] [blame] | 3733 | |
| 3734 | return ret; |
| 3735 | } |
| 3736 | EXPORT_SYMBOL_GPL(kvm_emulate_pio_string); |
| 3737 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3738 | static void bounce_off(void *info) |
| 3739 | { |
| 3740 | /* nothing */ |
| 3741 | } |
| 3742 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3743 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 3744 | void *data) |
| 3745 | { |
| 3746 | struct cpufreq_freqs *freq = data; |
| 3747 | struct kvm *kvm; |
| 3748 | struct kvm_vcpu *vcpu; |
| 3749 | int i, send_ipi = 0; |
| 3750 | |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3751 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 3752 | return 0; |
| 3753 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 3754 | return 0; |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 3755 | per_cpu(cpu_tsc_khz, freq->cpu) = freq->new; |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3756 | |
| 3757 | spin_lock(&kvm_lock); |
| 3758 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 3759 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3760 | if (vcpu->cpu != freq->cpu) |
| 3761 | continue; |
| 3762 | if (!kvm_request_guest_time_update(vcpu)) |
| 3763 | continue; |
| 3764 | if (vcpu->cpu != smp_processor_id()) |
| 3765 | send_ipi++; |
| 3766 | } |
| 3767 | } |
| 3768 | spin_unlock(&kvm_lock); |
| 3769 | |
| 3770 | if (freq->old < freq->new && send_ipi) { |
| 3771 | /* |
| 3772 | * We upscale the frequency. Must make the guest |
| 3773 | * doesn't see old kvmclock values while running with |
| 3774 | * the new frequency, otherwise we risk the guest sees |
| 3775 | * time go backwards. |
| 3776 | * |
| 3777 | * In case we update the frequency for another cpu |
| 3778 | * (which might be in guest context) send an interrupt |
| 3779 | * to kick the cpu out of guest context. Next time |
| 3780 | * guest context is entered kvmclock will be updated, |
| 3781 | * so the guest will not see stale values. |
| 3782 | */ |
| 3783 | smp_call_function_single(freq->cpu, bounce_off, NULL, 1); |
| 3784 | } |
| 3785 | return 0; |
| 3786 | } |
| 3787 | |
| 3788 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
| 3789 | .notifier_call = kvmclock_cpufreq_notifier |
| 3790 | }; |
| 3791 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3792 | static void kvm_timer_init(void) |
| 3793 | { |
| 3794 | int cpu; |
| 3795 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3796 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3797 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 3798 | CPUFREQ_TRANSITION_NOTIFIER); |
Zachary Amsden | 6b7d7e7 | 2009-10-09 16:26:08 -1000 | [diff] [blame] | 3799 | for_each_online_cpu(cpu) { |
| 3800 | unsigned long khz = cpufreq_get(cpu); |
| 3801 | if (!khz) |
| 3802 | khz = tsc_khz; |
| 3803 | per_cpu(cpu_tsc_khz, cpu) = khz; |
| 3804 | } |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 3805 | } else { |
| 3806 | for_each_possible_cpu(cpu) |
| 3807 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3811 | int kvm_arch_init(void *opaque) |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3812 | { |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3813 | int r; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3814 | struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque; |
| 3815 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3816 | if (kvm_x86_ops) { |
| 3817 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3818 | r = -EEXIST; |
| 3819 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | if (!ops->cpu_has_kvm_support()) { |
| 3823 | printk(KERN_ERR "kvm: no hardware support\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3824 | r = -EOPNOTSUPP; |
| 3825 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3826 | } |
| 3827 | if (ops->disabled_by_bios()) { |
| 3828 | printk(KERN_ERR "kvm: disabled by bios\n"); |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3829 | r = -EOPNOTSUPP; |
| 3830 | goto out; |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3831 | } |
| 3832 | |
Avi Kivity | 97db56c | 2008-01-13 13:23:56 +0200 | [diff] [blame] | 3833 | r = kvm_mmu_module_init(); |
| 3834 | if (r) |
| 3835 | goto out; |
| 3836 | |
| 3837 | kvm_init_msr_list(); |
| 3838 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3839 | kvm_x86_ops = ops; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3840 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 3841 | kvm_mmu_set_base_ptes(PT_PRESENT_MASK); |
| 3842 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 3843 | PT_DIRTY_MASK, PT64_NX_MASK, 0); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3844 | |
Zachary Amsden | b820cc0 | 2009-09-29 11:38:34 -1000 | [diff] [blame] | 3845 | kvm_timer_init(); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 3846 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3847 | return 0; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3848 | |
| 3849 | out: |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3850 | return r; |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 3851 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3852 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3853 | void kvm_arch_exit(void) |
| 3854 | { |
Jan Kiszka | 888d256 | 2009-04-17 19:24:58 +0200 | [diff] [blame] | 3855 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 3856 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 3857 | CPUFREQ_TRANSITION_NOTIFIER); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3858 | kvm_x86_ops = NULL; |
Zhang Xiantao | 56c6d28 | 2007-11-18 20:43:21 +0800 | [diff] [blame] | 3859 | kvm_mmu_module_exit(); |
| 3860 | } |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 3861 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3862 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 3863 | { |
| 3864 | ++vcpu->stat.halt_exits; |
| 3865 | if (irqchip_in_kernel(vcpu->kvm)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 3866 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3867 | return 1; |
| 3868 | } else { |
| 3869 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 3870 | return 0; |
| 3871 | } |
| 3872 | } |
| 3873 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 3874 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3875 | static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, |
| 3876 | unsigned long a1) |
| 3877 | { |
| 3878 | if (is_long_mode(vcpu)) |
| 3879 | return a0; |
| 3880 | else |
| 3881 | return a0 | ((gpa_t)a1 << 32); |
| 3882 | } |
| 3883 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3884 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 3885 | { |
| 3886 | u64 param, ingpa, outgpa, ret; |
| 3887 | uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; |
| 3888 | bool fast, longmode; |
| 3889 | int cs_db, cs_l; |
| 3890 | |
| 3891 | /* |
| 3892 | * hypercall generates UD from non zero cpl and real mode |
| 3893 | * per HYPER-V spec |
| 3894 | */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3895 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3896 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
| 3900 | kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 3901 | longmode = is_long_mode(vcpu) && cs_l == 1; |
| 3902 | |
| 3903 | if (!longmode) { |
Gleb Natapov | ccd4693 | 2010-01-19 15:06:38 +0200 | [diff] [blame] | 3904 | param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | |
| 3905 | (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); |
| 3906 | ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | |
| 3907 | (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); |
| 3908 | outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | |
| 3909 | (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3910 | } |
| 3911 | #ifdef CONFIG_X86_64 |
| 3912 | else { |
| 3913 | param = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3914 | ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3915 | outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 3916 | } |
| 3917 | #endif |
| 3918 | |
| 3919 | code = param & 0xffff; |
| 3920 | fast = (param >> 16) & 0x1; |
| 3921 | rep_cnt = (param >> 32) & 0xfff; |
| 3922 | rep_idx = (param >> 48) & 0xfff; |
| 3923 | |
| 3924 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 3925 | |
Gleb Natapov | c25bc16 | 2010-01-17 15:51:24 +0200 | [diff] [blame] | 3926 | switch (code) { |
| 3927 | case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: |
| 3928 | kvm_vcpu_on_spin(vcpu); |
| 3929 | break; |
| 3930 | default: |
| 3931 | res = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 3932 | break; |
| 3933 | } |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3934 | |
| 3935 | ret = res | (((u64)rep_done & 0xfff) << 32); |
| 3936 | if (longmode) { |
| 3937 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
| 3938 | } else { |
| 3939 | kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); |
| 3940 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); |
| 3941 | } |
| 3942 | |
| 3943 | return 1; |
| 3944 | } |
| 3945 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3946 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 3947 | { |
| 3948 | unsigned long nr, a0, a1, a2, a3, ret; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3949 | int r = 1; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3950 | |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 3951 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 3952 | return kvm_hv_hypercall(vcpu); |
| 3953 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3954 | nr = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 3955 | a0 = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 3956 | a1 = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3957 | a2 = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 3958 | a3 = kvm_register_read(vcpu, VCPU_REGS_RSI); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3959 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3960 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3961 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3962 | if (!is_long_mode(vcpu)) { |
| 3963 | nr &= 0xFFFFFFFF; |
| 3964 | a0 &= 0xFFFFFFFF; |
| 3965 | a1 &= 0xFFFFFFFF; |
| 3966 | a2 &= 0xFFFFFFFF; |
| 3967 | a3 &= 0xFFFFFFFF; |
| 3968 | } |
| 3969 | |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 3970 | if (kvm_x86_ops->get_cpl(vcpu) != 0) { |
| 3971 | ret = -KVM_EPERM; |
| 3972 | goto out; |
| 3973 | } |
| 3974 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3975 | switch (nr) { |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 3976 | case KVM_HC_VAPIC_POLL_IRQ: |
| 3977 | ret = 0; |
| 3978 | break; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3979 | case KVM_HC_MMU_OP: |
| 3980 | r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret); |
| 3981 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3982 | default: |
| 3983 | ret = -KVM_ENOSYS; |
| 3984 | break; |
| 3985 | } |
Jan Kiszka | 07708c4 | 2009-08-03 18:43:28 +0200 | [diff] [blame] | 3986 | out: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3987 | kvm_register_write(vcpu, VCPU_REGS_RAX, ret); |
Amit Shah | f11c3a8 | 2008-02-21 01:00:30 +0530 | [diff] [blame] | 3988 | ++vcpu->stat.hypercalls; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3989 | return r; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3990 | } |
| 3991 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 3992 | |
| 3993 | int kvm_fix_hypercall(struct kvm_vcpu *vcpu) |
| 3994 | { |
| 3995 | char instruction[3]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3996 | unsigned long rip = kvm_rip_read(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3997 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 3998 | /* |
| 3999 | * Blow out the MMU to ensure that no other VCPU has an active mapping |
| 4000 | * to ensure that the updated hypercall appears atomically across all |
| 4001 | * VCPUs. |
| 4002 | */ |
| 4003 | kvm_mmu_zap_all(vcpu->kvm); |
| 4004 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4005 | kvm_x86_ops->patch_hypercall(vcpu, instruction); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4006 | |
Takuya Yoshikawa | 7edcfac | 2010-02-01 22:11:52 +0900 | [diff] [blame] | 4007 | return emulator_write_emulated(rip, instruction, 3, vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 4011 | { |
| 4012 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 4013 | } |
| 4014 | |
| 4015 | void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4016 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4017 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4018 | |
| 4019 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 4020 | } |
| 4021 | |
| 4022 | void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base) |
| 4023 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4024 | struct desc_ptr dt = { limit, base }; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4025 | |
| 4026 | kvm_x86_ops->set_idt(vcpu, &dt); |
| 4027 | } |
| 4028 | |
| 4029 | void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw, |
| 4030 | unsigned long *rflags) |
| 4031 | { |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4032 | kvm_lmsw(vcpu, msw); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4033 | *rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr) |
| 4037 | { |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4038 | unsigned long value; |
| 4039 | |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4040 | switch (cr) { |
| 4041 | case 0: |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4042 | value = kvm_read_cr0(vcpu); |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4043 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4044 | case 2: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4045 | value = vcpu->arch.cr2; |
| 4046 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4047 | case 3: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4048 | value = vcpu->arch.cr3; |
| 4049 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4050 | case 4: |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4051 | value = kvm_read_cr4(vcpu); |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4052 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4053 | case 8: |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4054 | value = kvm_get_cr8(vcpu); |
| 4055 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4056 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4057 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4058 | return 0; |
| 4059 | } |
Joerg Roedel | 54e445c | 2008-04-30 17:56:02 +0200 | [diff] [blame] | 4060 | |
| 4061 | return value; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, |
| 4065 | unsigned long *rflags) |
| 4066 | { |
| 4067 | switch (cr) { |
| 4068 | case 0: |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4069 | kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4070 | *rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4071 | break; |
| 4072 | case 2: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4073 | vcpu->arch.cr2 = val; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4074 | break; |
| 4075 | case 3: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4076 | kvm_set_cr3(vcpu, val); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4077 | break; |
| 4078 | case 4: |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4079 | kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4080 | break; |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4081 | case 8: |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4082 | kvm_set_cr8(vcpu, val & 0xfUL); |
Joerg Roedel | 152ff9b | 2007-12-06 15:46:52 +0100 | [diff] [blame] | 4083 | break; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4084 | default: |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4085 | vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4086 | } |
| 4087 | } |
| 4088 | |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4089 | static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i) |
| 4090 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4091 | struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i]; |
| 4092 | int j, nent = vcpu->arch.cpuid_nent; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4093 | |
| 4094 | e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4095 | /* 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] | 4096 | for (j = i + 1; ; j = (j + 1) % nent) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4097 | struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4098 | if (ej->function == e->function) { |
| 4099 | ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; |
| 4100 | return j; |
| 4101 | } |
| 4102 | } |
| 4103 | return 0; /* silence gcc, even though control never reaches here */ |
| 4104 | } |
| 4105 | |
| 4106 | /* find an entry with matching function, matching index (if needed), and that |
| 4107 | * should be read next (if it's stateful) */ |
| 4108 | static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e, |
| 4109 | u32 function, u32 index) |
| 4110 | { |
| 4111 | if (e->function != function) |
| 4112 | return 0; |
| 4113 | if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index) |
| 4114 | return 0; |
| 4115 | if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && |
Amit Shah | 1935547 | 2009-01-14 16:56:00 +0000 | [diff] [blame] | 4116 | !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT)) |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4117 | return 0; |
| 4118 | return 1; |
| 4119 | } |
| 4120 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4121 | struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, |
| 4122 | u32 function, u32 index) |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4123 | { |
| 4124 | int i; |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4125 | struct kvm_cpuid_entry2 *best = NULL; |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4126 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4127 | for (i = 0; i < vcpu->arch.cpuid_nent; ++i) { |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4128 | struct kvm_cpuid_entry2 *e; |
| 4129 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4130 | e = &vcpu->arch.cpuid_entries[i]; |
Dan Kenigsberg | 0771671 | 2007-11-21 17:10:04 +0200 | [diff] [blame] | 4131 | if (is_matching_cpuid_entry(e, function, index)) { |
| 4132 | if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) |
| 4133 | move_to_next_stateful_cpuid_entry(vcpu, i); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4134 | best = e; |
| 4135 | break; |
| 4136 | } |
| 4137 | /* |
| 4138 | * Both basic or both extended? |
| 4139 | */ |
| 4140 | if (((e->function ^ function) & 0x80000000) == 0) |
| 4141 | if (!best || e->function > best->function) |
| 4142 | best = e; |
| 4143 | } |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4144 | return best; |
| 4145 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4146 | EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry); |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4147 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 4148 | int cpuid_maxphyaddr(struct kvm_vcpu *vcpu) |
| 4149 | { |
| 4150 | struct kvm_cpuid_entry2 *best; |
| 4151 | |
| 4152 | best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0); |
| 4153 | if (best) |
| 4154 | return best->eax & 0xff; |
| 4155 | return 36; |
| 4156 | } |
| 4157 | |
Alexander Graf | d801747 | 2008-11-25 20:17:11 +0100 | [diff] [blame] | 4158 | void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) |
| 4159 | { |
| 4160 | u32 function, index; |
| 4161 | struct kvm_cpuid_entry2 *best; |
| 4162 | |
| 4163 | function = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4164 | index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4165 | kvm_register_write(vcpu, VCPU_REGS_RAX, 0); |
| 4166 | kvm_register_write(vcpu, VCPU_REGS_RBX, 0); |
| 4167 | kvm_register_write(vcpu, VCPU_REGS_RCX, 0); |
| 4168 | kvm_register_write(vcpu, VCPU_REGS_RDX, 0); |
| 4169 | best = kvm_find_cpuid_entry(vcpu, function, index); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4170 | if (best) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4171 | kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax); |
| 4172 | kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx); |
| 4173 | kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx); |
| 4174 | kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4175 | } |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4176 | kvm_x86_ops->skip_emulated_instruction(vcpu); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4177 | trace_kvm_cpuid(function, |
| 4178 | kvm_register_read(vcpu, VCPU_REGS_RAX), |
| 4179 | kvm_register_read(vcpu, VCPU_REGS_RBX), |
| 4180 | kvm_register_read(vcpu, VCPU_REGS_RCX), |
| 4181 | kvm_register_read(vcpu, VCPU_REGS_RDX)); |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 4182 | } |
| 4183 | EXPORT_SYMBOL_GPL(kvm_emulate_cpuid); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 4184 | |
| 4185 | /* |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4186 | * Check if userspace requested an interrupt window, and that the |
| 4187 | * interrupt window is open. |
| 4188 | * |
| 4189 | * No need to exit to userspace if we already have an interrupt queued. |
| 4190 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4191 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4192 | { |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4193 | return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4194 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 5df5664 | 2009-04-21 17:44:59 +0300 | [diff] [blame] | 4195 | kvm_arch_interrupt_allowed(vcpu)); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4196 | } |
| 4197 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4198 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4199 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4200 | struct kvm_run *kvm_run = vcpu->run; |
| 4201 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4202 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4203 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4204 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4205 | if (irqchip_in_kernel(vcpu->kvm)) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4206 | kvm_run->ready_for_interrupt_injection = 1; |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 4207 | else |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4208 | kvm_run->ready_for_interrupt_injection = |
Gleb Natapov | fa9726b | 2009-05-11 13:35:47 +0300 | [diff] [blame] | 4209 | kvm_arch_interrupt_allowed(vcpu) && |
| 4210 | !kvm_cpu_has_interrupt(vcpu) && |
| 4211 | !kvm_event_needs_reinjection(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4212 | } |
| 4213 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4214 | static void vapic_enter(struct kvm_vcpu *vcpu) |
| 4215 | { |
| 4216 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 4217 | struct page *page; |
| 4218 | |
| 4219 | if (!apic || !apic->vapic_addr) |
| 4220 | return; |
| 4221 | |
| 4222 | page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4223 | |
| 4224 | vcpu->arch.apic->vapic_page = page; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | static void vapic_exit(struct kvm_vcpu *vcpu) |
| 4228 | { |
| 4229 | struct kvm_lapic *apic = vcpu->arch.apic; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4230 | int idx; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4231 | |
| 4232 | if (!apic || !apic->vapic_addr) |
| 4233 | return; |
| 4234 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4235 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4236 | kvm_release_page_dirty(apic->vapic_page); |
| 4237 | mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4238 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4239 | } |
| 4240 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4241 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 4242 | { |
| 4243 | int max_irr, tpr; |
| 4244 | |
| 4245 | if (!kvm_x86_ops->update_cr8_intercept) |
| 4246 | return; |
| 4247 | |
Avi Kivity | 88c808f | 2009-08-17 22:49:40 +0300 | [diff] [blame] | 4248 | if (!vcpu->arch.apic) |
| 4249 | return; |
| 4250 | |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4251 | if (!vcpu->arch.apic->vapic_addr) |
| 4252 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 4253 | else |
| 4254 | max_irr = -1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4255 | |
| 4256 | if (max_irr != -1) |
| 4257 | max_irr >>= 4; |
| 4258 | |
| 4259 | tpr = kvm_lapic_get_cr8(vcpu); |
| 4260 | |
| 4261 | kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr); |
| 4262 | } |
| 4263 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4264 | static void inject_pending_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4265 | { |
| 4266 | /* try to reinject previous events if any */ |
Gleb Natapov | b59bb7b | 2009-07-09 15:33:51 +0300 | [diff] [blame] | 4267 | if (vcpu->arch.exception.pending) { |
| 4268 | kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr, |
| 4269 | vcpu->arch.exception.has_error_code, |
| 4270 | vcpu->arch.exception.error_code); |
| 4271 | return; |
| 4272 | } |
| 4273 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4274 | if (vcpu->arch.nmi_injected) { |
| 4275 | kvm_x86_ops->set_nmi(vcpu); |
| 4276 | return; |
| 4277 | } |
| 4278 | |
| 4279 | if (vcpu->arch.interrupt.pending) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4280 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4281 | return; |
| 4282 | } |
| 4283 | |
| 4284 | /* try to inject new event if pending */ |
| 4285 | if (vcpu->arch.nmi_pending) { |
| 4286 | if (kvm_x86_ops->nmi_allowed(vcpu)) { |
| 4287 | vcpu->arch.nmi_pending = false; |
| 4288 | vcpu->arch.nmi_injected = true; |
| 4289 | kvm_x86_ops->set_nmi(vcpu); |
| 4290 | } |
| 4291 | } else if (kvm_cpu_has_interrupt(vcpu)) { |
| 4292 | if (kvm_x86_ops->interrupt_allowed(vcpu)) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4293 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), |
| 4294 | false); |
| 4295 | kvm_x86_ops->set_irq(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | } |
| 4299 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4300 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4301 | { |
| 4302 | int r; |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4303 | bool req_int_win = !irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4304 | vcpu->run->request_interrupt_window; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4305 | |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 4306 | if (vcpu->requests) |
| 4307 | if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) |
| 4308 | kvm_mmu_unload(vcpu); |
| 4309 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4310 | r = kvm_mmu_reload(vcpu); |
| 4311 | if (unlikely(r)) |
| 4312 | goto out; |
| 4313 | |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4314 | if (vcpu->requests) { |
| 4315 | if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests)) |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 4316 | __kvm_migrate_timers(vcpu); |
Gerd Hoffmann | c807660 | 2009-02-04 17:52:04 +0100 | [diff] [blame] | 4317 | if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests)) |
| 4318 | kvm_write_guest_time(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 4319 | if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests)) |
| 4320 | kvm_mmu_sync_roots(vcpu); |
Marcelo Tosatti | d4acf7e | 2008-06-06 16:37:35 -0300 | [diff] [blame] | 4321 | if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 4322 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4323 | if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS, |
| 4324 | &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4325 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4326 | r = 0; |
| 4327 | goto out; |
| 4328 | } |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4329 | if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4330 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Joerg Roedel | 71c4dfa | 2008-02-26 16:49:16 +0100 | [diff] [blame] | 4331 | r = 0; |
| 4332 | goto out; |
| 4333 | } |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4334 | if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) { |
| 4335 | vcpu->fpu_active = 0; |
| 4336 | kvm_x86_ops->fpu_deactivate(vcpu); |
| 4337 | } |
Avi Kivity | 2f52d58 | 2008-01-16 12:49:30 +0200 | [diff] [blame] | 4338 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4339 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4340 | preempt_disable(); |
| 4341 | |
| 4342 | kvm_x86_ops->prepare_guest_switch(vcpu); |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 4343 | if (vcpu->fpu_active) |
| 4344 | kvm_load_guest_fpu(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4345 | |
| 4346 | local_irq_disable(); |
| 4347 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4348 | clear_bit(KVM_REQ_KICK, &vcpu->requests); |
| 4349 | smp_mb__after_clear_bit(); |
| 4350 | |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4351 | if (vcpu->requests || need_resched() || signal_pending(current)) { |
Gleb Natapov | c7f0f24 | 2009-07-07 15:27:32 +0300 | [diff] [blame] | 4352 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Avi Kivity | 6c142801 | 2008-01-15 18:27:32 +0200 | [diff] [blame] | 4353 | local_irq_enable(); |
| 4354 | preempt_enable(); |
| 4355 | r = 1; |
| 4356 | goto out; |
| 4357 | } |
| 4358 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4359 | inject_pending_event(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4360 | |
Gleb Natapov | 6a8b1d1 | 2009-05-11 13:35:51 +0300 | [diff] [blame] | 4361 | /* enable NMI/IRQ window open exits if needed */ |
| 4362 | if (vcpu->arch.nmi_pending) |
| 4363 | kvm_x86_ops->enable_nmi_window(vcpu); |
| 4364 | else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) |
| 4365 | kvm_x86_ops->enable_irq_window(vcpu); |
| 4366 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4367 | if (kvm_lapic_enabled(vcpu)) { |
Gleb Natapov | 8db3baa | 2009-05-11 13:35:54 +0300 | [diff] [blame] | 4368 | update_cr8_intercept(vcpu); |
| 4369 | kvm_lapic_sync_to_vapic(vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4370 | } |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4371 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4372 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4373 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4374 | kvm_guest_enter(); |
| 4375 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4376 | if (unlikely(vcpu->arch.switch_db_regs)) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4377 | set_debugreg(0, 7); |
| 4378 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 4379 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 4380 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 4381 | set_debugreg(vcpu->arch.eff_db[3], 3); |
| 4382 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4383 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4384 | trace_kvm_entry(vcpu->vcpu_id); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4385 | kvm_x86_ops->run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4386 | |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4387 | /* |
| 4388 | * If the guest has used debug registers, at least dr7 |
| 4389 | * will be disabled while returning to the host. |
| 4390 | * If we don't have active breakpoints in the host, we don't |
| 4391 | * care about the messed up debug address registers. But if |
| 4392 | * we have some of them active, restore the old state. |
| 4393 | */ |
Frederic Weisbecker | 59d8eb5 | 2009-11-10 11:03:12 +0100 | [diff] [blame] | 4394 | if (hw_breakpoint_active()) |
Frederic Weisbecker | 24f1e32c | 2009-09-09 19:22:48 +0200 | [diff] [blame] | 4395 | hw_breakpoint_restore(); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4396 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 4397 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4398 | local_irq_enable(); |
| 4399 | |
| 4400 | ++vcpu->stat.exits; |
| 4401 | |
| 4402 | /* |
| 4403 | * We must have an instruction between local_irq_enable() and |
| 4404 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 4405 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 4406 | * But we need to prevent reordering, hence this barrier(): |
| 4407 | */ |
| 4408 | barrier(); |
| 4409 | |
| 4410 | kvm_guest_exit(); |
| 4411 | |
| 4412 | preempt_enable(); |
| 4413 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4414 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4415 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4416 | /* |
| 4417 | * Profile KVM exit RIPs: |
| 4418 | */ |
| 4419 | if (unlikely(prof_on == KVM_PROFILING)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4420 | unsigned long rip = kvm_rip_read(vcpu); |
| 4421 | profile_hit(KVM_PROFILING, (void *)rip); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4422 | } |
| 4423 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4424 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4425 | kvm_lapic_sync_from_vapic(vcpu); |
| 4426 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4427 | r = kvm_x86_ops->handle_exit(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4428 | out: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4429 | return r; |
| 4430 | } |
| 4431 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4432 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4433 | static int __vcpu_run(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4434 | { |
| 4435 | int r; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4436 | struct kvm *kvm = vcpu->kvm; |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4437 | |
| 4438 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) { |
Jan Kiszka | 1b10bf3 | 2008-09-30 10:41:06 +0200 | [diff] [blame] | 4439 | pr_debug("vcpu %d received sipi with vector # %x\n", |
| 4440 | vcpu->vcpu_id, vcpu->arch.sipi_vector); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4441 | kvm_lapic_reset(vcpu); |
Gleb Natapov | 5f17928 | 2008-10-07 15:42:33 +0200 | [diff] [blame] | 4442 | r = kvm_arch_vcpu_reset(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4443 | if (r) |
| 4444 | return r; |
| 4445 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4446 | } |
| 4447 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4448 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4449 | vapic_enter(vcpu); |
| 4450 | |
| 4451 | r = 1; |
| 4452 | while (r > 0) { |
Gleb Natapov | af2152f | 2008-09-22 14:28:53 +0300 | [diff] [blame] | 4453 | if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4454 | r = vcpu_enter_guest(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4455 | else { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4456 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4457 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4458 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4459 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests)) |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4460 | { |
| 4461 | switch(vcpu->arch.mp_state) { |
| 4462 | case KVM_MP_STATE_HALTED: |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4463 | vcpu->arch.mp_state = |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4464 | KVM_MP_STATE_RUNNABLE; |
| 4465 | case KVM_MP_STATE_RUNNABLE: |
| 4466 | break; |
| 4467 | case KVM_MP_STATE_SIPI_RECEIVED: |
| 4468 | default: |
| 4469 | r = -EINTR; |
| 4470 | break; |
| 4471 | } |
| 4472 | } |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4473 | } |
| 4474 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4475 | if (r <= 0) |
| 4476 | break; |
| 4477 | |
| 4478 | clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests); |
| 4479 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 4480 | kvm_inject_pending_timer_irqs(vcpu); |
| 4481 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4482 | if (dm_request_for_irq_injection(vcpu)) { |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4483 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4484 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4485 | ++vcpu->stat.request_irq_exits; |
| 4486 | } |
| 4487 | if (signal_pending(current)) { |
| 4488 | r = -EINTR; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4489 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4490 | ++vcpu->stat.signal_exits; |
| 4491 | } |
| 4492 | if (need_resched()) { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4493 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 4494 | kvm_resched(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4495 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4499 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4500 | post_kvm_run_save(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4501 | |
Avi Kivity | b93463a | 2007-10-25 16:52:32 +0200 | [diff] [blame] | 4502 | vapic_exit(vcpu); |
| 4503 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4504 | return r; |
| 4505 | } |
| 4506 | |
| 4507 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 4508 | { |
| 4509 | int r; |
| 4510 | sigset_t sigsaved; |
| 4511 | |
| 4512 | vcpu_load(vcpu); |
| 4513 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4514 | if (vcpu->sigset_active) |
| 4515 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 4516 | |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4517 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 4518 | kvm_vcpu_block(vcpu); |
Marcelo Tosatti | d769017 | 2008-09-08 15:23:48 -0300 | [diff] [blame] | 4519 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Avi Kivity | ac9f6dc | 2008-07-06 15:48:31 +0300 | [diff] [blame] | 4520 | r = -EAGAIN; |
| 4521 | goto out; |
| 4522 | } |
| 4523 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4524 | /* re-sync apic's tpr */ |
| 4525 | if (!irqchip_in_kernel(vcpu->kvm)) |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4526 | kvm_set_cr8(vcpu, kvm_run->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4527 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4528 | if (vcpu->arch.pio.cur_count) { |
Gleb Natapov | 7567cae | 2010-03-09 12:01:10 +0200 | [diff] [blame] | 4529 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4530 | r = complete_pio(vcpu); |
Gleb Natapov | 7567cae | 2010-03-09 12:01:10 +0200 | [diff] [blame] | 4531 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4532 | if (r) |
| 4533 | goto out; |
| 4534 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4535 | if (vcpu->mmio_needed) { |
| 4536 | memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8); |
| 4537 | vcpu->mmio_read_completed = 1; |
| 4538 | vcpu->mmio_needed = 0; |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4539 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4540 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4541 | r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0, |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 4542 | EMULTYPE_NO_DECODE); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 4543 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4544 | if (r == EMULATE_DO_MMIO) { |
| 4545 | /* |
| 4546 | * Read-modify-write. Back to userspace. |
| 4547 | */ |
| 4548 | r = 0; |
| 4549 | goto out; |
| 4550 | } |
| 4551 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4552 | if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) |
| 4553 | kvm_register_write(vcpu, VCPU_REGS_RAX, |
| 4554 | kvm_run->hypercall.ret); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4555 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4556 | r = __vcpu_run(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4557 | |
| 4558 | out: |
| 4559 | if (vcpu->sigset_active) |
| 4560 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 4561 | |
| 4562 | vcpu_put(vcpu); |
| 4563 | return r; |
| 4564 | } |
| 4565 | |
| 4566 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4567 | { |
| 4568 | vcpu_load(vcpu); |
| 4569 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4570 | regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4571 | regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4572 | regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4573 | regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4574 | regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 4575 | regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
| 4576 | regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 4577 | regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4578 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4579 | regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8); |
| 4580 | regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9); |
| 4581 | regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10); |
| 4582 | regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11); |
| 4583 | regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12); |
| 4584 | regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13); |
| 4585 | regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14); |
| 4586 | regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4587 | #endif |
| 4588 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4589 | regs->rip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4590 | regs->rflags = kvm_get_rflags(vcpu); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4591 | |
| 4592 | vcpu_put(vcpu); |
| 4593 | |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
| 4597 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 4598 | { |
| 4599 | vcpu_load(vcpu); |
| 4600 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4601 | kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax); |
| 4602 | kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx); |
| 4603 | kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx); |
| 4604 | kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx); |
| 4605 | kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi); |
| 4606 | kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi); |
| 4607 | kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp); |
| 4608 | kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4609 | #ifdef CONFIG_X86_64 |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4610 | kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8); |
| 4611 | kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9); |
| 4612 | kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10); |
| 4613 | kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11); |
| 4614 | kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12); |
| 4615 | kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13); |
| 4616 | kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14); |
| 4617 | kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4618 | #endif |
| 4619 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4620 | kvm_rip_write(vcpu, regs->rip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4621 | kvm_set_rflags(vcpu, regs->rflags); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4622 | |
Jan Kiszka | b4f14ab | 2008-04-30 17:59:04 +0200 | [diff] [blame] | 4623 | vcpu->arch.exception.pending = false; |
| 4624 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4625 | vcpu_put(vcpu); |
| 4626 | |
| 4627 | return 0; |
| 4628 | } |
| 4629 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4630 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 4631 | struct kvm_segment *var, int seg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4632 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 4633 | kvm_x86_ops->get_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4637 | { |
| 4638 | struct kvm_segment cs; |
| 4639 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4640 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4641 | *db = cs.db; |
| 4642 | *l = cs.l; |
| 4643 | } |
| 4644 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 4645 | |
| 4646 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 4647 | struct kvm_sregs *sregs) |
| 4648 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4649 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4650 | |
| 4651 | vcpu_load(vcpu); |
| 4652 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4653 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 4654 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 4655 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 4656 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 4657 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 4658 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4659 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4660 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 4661 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4662 | |
| 4663 | kvm_x86_ops->get_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4664 | sregs->idt.limit = dt.size; |
| 4665 | sregs->idt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4666 | kvm_x86_ops->get_gdt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4667 | sregs->gdt.limit = dt.size; |
| 4668 | sregs->gdt.base = dt.address; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4669 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4670 | sregs->cr0 = kvm_read_cr0(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4671 | sregs->cr2 = vcpu->arch.cr2; |
| 4672 | sregs->cr3 = vcpu->arch.cr3; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4673 | sregs->cr4 = kvm_read_cr4(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4674 | sregs->cr8 = kvm_get_cr8(vcpu); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4675 | sregs->efer = vcpu->arch.efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4676 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 4677 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 4678 | memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4679 | |
Gleb Natapov | 36752c9 | 2009-05-11 13:35:53 +0300 | [diff] [blame] | 4680 | if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft) |
Gleb Natapov | 14d0bc1 | 2009-04-21 17:45:11 +0300 | [diff] [blame] | 4681 | set_bit(vcpu->arch.interrupt.nr, |
| 4682 | (unsigned long *)sregs->interrupt_bitmap); |
Gleb Natapov | 16d7a19 | 2009-04-21 17:45:10 +0300 | [diff] [blame] | 4683 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4684 | vcpu_put(vcpu); |
| 4685 | |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 4689 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 4690 | struct kvm_mp_state *mp_state) |
| 4691 | { |
| 4692 | vcpu_load(vcpu); |
| 4693 | mp_state->mp_state = vcpu->arch.mp_state; |
| 4694 | vcpu_put(vcpu); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | |
| 4698 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 4699 | struct kvm_mp_state *mp_state) |
| 4700 | { |
| 4701 | vcpu_load(vcpu); |
| 4702 | vcpu->arch.mp_state = mp_state->mp_state; |
| 4703 | vcpu_put(vcpu); |
| 4704 | return 0; |
| 4705 | } |
| 4706 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4707 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4708 | struct kvm_segment *var, int seg) |
| 4709 | { |
Harvey Harrison | 14af3f3 | 2008-02-19 10:25:50 -0800 | [diff] [blame] | 4710 | kvm_x86_ops->set_segment(vcpu, var, seg); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 4711 | } |
| 4712 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4713 | static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector, |
| 4714 | struct kvm_segment *kvm_desct) |
| 4715 | { |
Akinobu Mita | 46a359e | 2009-07-18 23:58:32 +0900 | [diff] [blame] | 4716 | kvm_desct->base = get_desc_base(seg_desc); |
| 4717 | kvm_desct->limit = get_desc_limit(seg_desc); |
Marcelo Tosatti | c93cd3a | 2008-07-19 19:08:07 -0300 | [diff] [blame] | 4718 | if (seg_desc->g) { |
| 4719 | kvm_desct->limit <<= 12; |
| 4720 | kvm_desct->limit |= 0xfff; |
| 4721 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4722 | kvm_desct->selector = selector; |
| 4723 | kvm_desct->type = seg_desc->type; |
| 4724 | kvm_desct->present = seg_desc->p; |
| 4725 | kvm_desct->dpl = seg_desc->dpl; |
| 4726 | kvm_desct->db = seg_desc->d; |
| 4727 | kvm_desct->s = seg_desc->s; |
| 4728 | kvm_desct->l = seg_desc->l; |
| 4729 | kvm_desct->g = seg_desc->g; |
| 4730 | kvm_desct->avl = seg_desc->avl; |
| 4731 | if (!selector) |
| 4732 | kvm_desct->unusable = 1; |
| 4733 | else |
| 4734 | kvm_desct->unusable = 0; |
| 4735 | kvm_desct->padding = 0; |
| 4736 | } |
| 4737 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4738 | static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu, |
| 4739 | u16 selector, |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4740 | struct desc_ptr *dtable) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4741 | { |
| 4742 | if (selector & 1 << 2) { |
| 4743 | struct kvm_segment kvm_seg; |
| 4744 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4745 | kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4746 | |
| 4747 | if (kvm_seg.unusable) |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4748 | dtable->size = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4749 | else |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4750 | dtable->size = kvm_seg.limit; |
| 4751 | dtable->address = kvm_seg.base; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4752 | } |
| 4753 | else |
| 4754 | kvm_x86_ops->get_gdt(vcpu, dtable); |
| 4755 | } |
| 4756 | |
| 4757 | /* allowed just for 8 bytes segments */ |
| 4758 | static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 4759 | struct desc_struct *seg_desc) |
| 4760 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4761 | struct desc_ptr dtable; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4762 | u16 index = selector >> 3; |
Takuya Yoshikawa | 6f55048 | 2010-02-18 12:15:00 +0200 | [diff] [blame] | 4763 | int ret; |
| 4764 | u32 err; |
| 4765 | gva_t addr; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4766 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4767 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4768 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4769 | if (dtable.size < index * 8 + 7) { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4770 | kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc); |
Takuya Yoshikawa | c125c60 | 2010-02-01 22:11:04 +0900 | [diff] [blame] | 4771 | return X86EMUL_PROPAGATE_FAULT; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4772 | } |
Takuya Yoshikawa | 6f55048 | 2010-02-18 12:15:00 +0200 | [diff] [blame] | 4773 | addr = dtable.base + index * 8; |
| 4774 | ret = kvm_read_guest_virt_system(addr, seg_desc, sizeof(*seg_desc), |
| 4775 | vcpu, &err); |
| 4776 | if (ret == X86EMUL_PROPAGATE_FAULT) |
| 4777 | kvm_inject_page_fault(vcpu, addr, err); |
| 4778 | |
| 4779 | return ret; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | /* allowed just for 8 bytes segments */ |
| 4783 | static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, |
| 4784 | struct desc_struct *seg_desc) |
| 4785 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4786 | struct desc_ptr dtable; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4787 | u16 index = selector >> 3; |
| 4788 | |
Amit Shah | b8222ad | 2008-10-22 16:39:47 +0530 | [diff] [blame] | 4789 | get_segment_descriptor_dtable(vcpu, selector, &dtable); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4790 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4791 | if (dtable.size < index * 8 + 7) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4792 | return 1; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4793 | return kvm_write_guest_virt(dtable.address + index*8, seg_desc, sizeof(*seg_desc), vcpu, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4794 | } |
| 4795 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 4796 | static gpa_t get_tss_base_addr_write(struct kvm_vcpu *vcpu, |
| 4797 | struct desc_struct *seg_desc) |
| 4798 | { |
| 4799 | u32 base_addr = get_desc_base(seg_desc); |
| 4800 | |
| 4801 | return kvm_mmu_gva_to_gpa_write(vcpu, base_addr, NULL); |
| 4802 | } |
| 4803 | |
| 4804 | static gpa_t get_tss_base_addr_read(struct kvm_vcpu *vcpu, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4805 | struct desc_struct *seg_desc) |
| 4806 | { |
Akinobu Mita | 46a359e | 2009-07-18 23:58:32 +0900 | [diff] [blame] | 4807 | u32 base_addr = get_desc_base(seg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4808 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 4809 | return kvm_mmu_gva_to_gpa_read(vcpu, base_addr, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4810 | } |
| 4811 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4812 | static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg) |
| 4813 | { |
| 4814 | struct kvm_segment kvm_seg; |
| 4815 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 4816 | kvm_get_segment(vcpu, &kvm_seg, seg); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4817 | return kvm_seg.selector; |
| 4818 | } |
| 4819 | |
Harvey Harrison | 2259e3a | 2008-08-22 13:29:17 -0700 | [diff] [blame] | 4820 | static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg) |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4821 | { |
| 4822 | struct kvm_segment segvar = { |
| 4823 | .base = selector << 4, |
| 4824 | .limit = 0xffff, |
| 4825 | .selector = selector, |
| 4826 | .type = 3, |
| 4827 | .present = 1, |
| 4828 | .dpl = 3, |
| 4829 | .db = 0, |
| 4830 | .s = 1, |
| 4831 | .l = 0, |
| 4832 | .g = 0, |
| 4833 | .avl = 0, |
| 4834 | .unusable = 0, |
| 4835 | }; |
| 4836 | kvm_x86_ops->set_segment(vcpu, &segvar, seg); |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4837 | return X86EMUL_CONTINUE; |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4838 | } |
| 4839 | |
Anthony Liguori | c0c7c04 | 2009-08-11 15:57:59 -0500 | [diff] [blame] | 4840 | static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg) |
| 4841 | { |
| 4842 | return (seg != VCPU_SREG_LDTR) && |
| 4843 | (seg != VCPU_SREG_TR) && |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4844 | (kvm_get_rflags(vcpu) & X86_EFLAGS_VM); |
Anthony Liguori | c0c7c04 | 2009-08-11 15:57:59 -0500 | [diff] [blame] | 4845 | } |
| 4846 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4847 | int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4848 | { |
| 4849 | struct kvm_segment kvm_seg; |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4850 | struct desc_struct seg_desc; |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4851 | u8 dpl, rpl, cpl; |
| 4852 | unsigned err_vec = GP_VECTOR; |
| 4853 | u32 err_code = 0; |
| 4854 | bool null_selector = !(selector & ~0x3); /* 0000-0003 are null */ |
| 4855 | int ret; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4856 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 4857 | if (is_vm86_segment(vcpu, seg) || !is_protmode(vcpu)) |
Avi Kivity | f4bbd9a | 2008-08-20 15:51:42 +0300 | [diff] [blame] | 4858 | return kvm_load_realmode_segment(vcpu, selector, seg); |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4859 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4860 | /* NULL selector is not valid for TR, CS and SS */ |
| 4861 | if ((seg == VCPU_SREG_CS || seg == VCPU_SREG_SS || seg == VCPU_SREG_TR) |
| 4862 | && null_selector) |
| 4863 | goto exception; |
| 4864 | |
| 4865 | /* TR should be in GDT only */ |
| 4866 | if (seg == VCPU_SREG_TR && (selector & (1 << 2))) |
| 4867 | goto exception; |
| 4868 | |
| 4869 | ret = load_guest_segment_descriptor(vcpu, selector, &seg_desc); |
| 4870 | if (ret) |
| 4871 | return ret; |
| 4872 | |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4873 | seg_desct_to_kvm_desct(&seg_desc, selector, &kvm_seg); |
Marcelo Tosatti | cb84b55 | 2009-11-11 17:29:49 -0200 | [diff] [blame] | 4874 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4875 | if (null_selector) { /* for NULL selector skip all following checks */ |
| 4876 | kvm_seg.unusable = 1; |
| 4877 | goto load; |
| 4878 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4879 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4880 | err_code = selector & 0xfffc; |
| 4881 | err_vec = GP_VECTOR; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4882 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4883 | /* can't load system descriptor into segment selecor */ |
| 4884 | if (seg <= VCPU_SREG_GS && !kvm_seg.s) |
| 4885 | goto exception; |
| 4886 | |
| 4887 | if (!kvm_seg.present) { |
| 4888 | err_vec = (seg == VCPU_SREG_SS) ? SS_VECTOR : NP_VECTOR; |
| 4889 | goto exception; |
| 4890 | } |
| 4891 | |
| 4892 | rpl = selector & 3; |
| 4893 | dpl = kvm_seg.dpl; |
| 4894 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 4895 | |
| 4896 | switch (seg) { |
| 4897 | case VCPU_SREG_SS: |
| 4898 | /* |
| 4899 | * segment is not a writable data segment or segment |
| 4900 | * selector's RPL != CPL or segment selector's RPL != CPL |
| 4901 | */ |
| 4902 | if (rpl != cpl || (kvm_seg.type & 0xa) != 0x2 || dpl != cpl) |
| 4903 | goto exception; |
| 4904 | break; |
| 4905 | case VCPU_SREG_CS: |
| 4906 | if (!(kvm_seg.type & 8)) |
| 4907 | goto exception; |
| 4908 | |
| 4909 | if (kvm_seg.type & 4) { |
| 4910 | /* conforming */ |
| 4911 | if (dpl > cpl) |
| 4912 | goto exception; |
| 4913 | } else { |
| 4914 | /* nonconforming */ |
| 4915 | if (rpl > cpl || dpl != cpl) |
| 4916 | goto exception; |
| 4917 | } |
| 4918 | /* CS(RPL) <- CPL */ |
| 4919 | selector = (selector & 0xfffc) | cpl; |
| 4920 | break; |
| 4921 | case VCPU_SREG_TR: |
| 4922 | if (kvm_seg.s || (kvm_seg.type != 1 && kvm_seg.type != 9)) |
| 4923 | goto exception; |
| 4924 | break; |
| 4925 | case VCPU_SREG_LDTR: |
| 4926 | if (kvm_seg.s || kvm_seg.type != 2) |
| 4927 | goto exception; |
| 4928 | break; |
| 4929 | default: /* DS, ES, FS, or GS */ |
| 4930 | /* |
| 4931 | * segment is not a data or readable code segment or |
| 4932 | * ((segment is a data or nonconforming code segment) |
| 4933 | * and (both RPL and CPL > DPL)) |
| 4934 | */ |
| 4935 | if ((kvm_seg.type & 0xa) == 0x8 || |
| 4936 | (((kvm_seg.type & 0xc) != 0xc) && (rpl > dpl && cpl > dpl))) |
| 4937 | goto exception; |
| 4938 | break; |
| 4939 | } |
| 4940 | |
| 4941 | if (!kvm_seg.unusable && kvm_seg.s) { |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4942 | /* mark segment as accessed */ |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4943 | kvm_seg.type |= 1; |
Gleb Natapov | e01c242 | 2010-01-25 12:01:04 +0200 | [diff] [blame] | 4944 | seg_desc.type |= 1; |
| 4945 | save_guest_segment_descriptor(vcpu, selector, &seg_desc); |
| 4946 | } |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4947 | load: |
| 4948 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 4949 | return X86EMUL_CONTINUE; |
| 4950 | exception: |
| 4951 | kvm_queue_exception_e(vcpu, err_vec, err_code); |
| 4952 | return X86EMUL_PROPAGATE_FAULT; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | static void save_state_to_tss32(struct kvm_vcpu *vcpu, |
| 4956 | struct tss_segment_32 *tss) |
| 4957 | { |
| 4958 | tss->cr3 = vcpu->arch.cr3; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4959 | tss->eip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4960 | tss->eflags = kvm_get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4961 | tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 4962 | tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4963 | tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 4964 | tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 4965 | tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 4966 | tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 4967 | tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 4968 | tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4969 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 4970 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 4971 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 4972 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 4973 | tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS); |
| 4974 | tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS); |
| 4975 | tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4976 | } |
| 4977 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 4978 | static void kvm_load_segment_selector(struct kvm_vcpu *vcpu, u16 sel, int seg) |
| 4979 | { |
| 4980 | struct kvm_segment kvm_seg; |
| 4981 | kvm_get_segment(vcpu, &kvm_seg, seg); |
| 4982 | kvm_seg.selector = sel; |
| 4983 | kvm_set_segment(vcpu, &kvm_seg, seg); |
| 4984 | } |
| 4985 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4986 | static int load_state_from_tss32(struct kvm_vcpu *vcpu, |
| 4987 | struct tss_segment_32 *tss) |
| 4988 | { |
| 4989 | kvm_set_cr3(vcpu, tss->cr3); |
| 4990 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4991 | kvm_rip_write(vcpu, tss->eip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 4992 | kvm_set_rflags(vcpu, tss->eflags | 2); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4993 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4994 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax); |
| 4995 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx); |
| 4996 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx); |
| 4997 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx); |
| 4998 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp); |
| 4999 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp); |
| 5000 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi); |
| 5001 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5002 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5003 | /* |
| 5004 | * SDM says that segment selectors are loaded before segment |
| 5005 | * descriptors |
| 5006 | */ |
| 5007 | kvm_load_segment_selector(vcpu, tss->ldt_selector, VCPU_SREG_LDTR); |
| 5008 | kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES); |
| 5009 | kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS); |
| 5010 | kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS); |
| 5011 | kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS); |
| 5012 | kvm_load_segment_selector(vcpu, tss->fs, VCPU_SREG_FS); |
| 5013 | kvm_load_segment_selector(vcpu, tss->gs, VCPU_SREG_GS); |
| 5014 | |
| 5015 | /* |
| 5016 | * Now load segment descriptors. If fault happenes at this stage |
| 5017 | * it is handled in a context of new task |
| 5018 | */ |
| 5019 | if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5020 | return 1; |
| 5021 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5022 | if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5023 | return 1; |
| 5024 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5025 | if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5026 | return 1; |
| 5027 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5028 | if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5029 | return 1; |
| 5030 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5031 | if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5032 | return 1; |
| 5033 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5034 | if (kvm_load_segment_descriptor(vcpu, tss->fs, VCPU_SREG_FS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5035 | return 1; |
| 5036 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5037 | if (kvm_load_segment_descriptor(vcpu, tss->gs, VCPU_SREG_GS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5038 | return 1; |
| 5039 | return 0; |
| 5040 | } |
| 5041 | |
| 5042 | static void save_state_to_tss16(struct kvm_vcpu *vcpu, |
| 5043 | struct tss_segment_16 *tss) |
| 5044 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5045 | tss->ip = kvm_rip_read(vcpu); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5046 | tss->flag = kvm_get_rflags(vcpu); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5047 | tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX); |
| 5048 | tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 5049 | tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX); |
| 5050 | tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX); |
| 5051 | tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 5052 | tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP); |
| 5053 | tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI); |
| 5054 | tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5055 | |
| 5056 | tss->es = get_segment_selector(vcpu, VCPU_SREG_ES); |
| 5057 | tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS); |
| 5058 | tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS); |
| 5059 | tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS); |
| 5060 | tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | static int load_state_from_tss16(struct kvm_vcpu *vcpu, |
| 5064 | struct tss_segment_16 *tss) |
| 5065 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5066 | kvm_rip_write(vcpu, tss->ip); |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5067 | kvm_set_rflags(vcpu, tss->flag | 2); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5068 | kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax); |
| 5069 | kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx); |
| 5070 | kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx); |
| 5071 | kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx); |
| 5072 | kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp); |
| 5073 | kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp); |
| 5074 | kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si); |
| 5075 | kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5076 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5077 | /* |
| 5078 | * SDM says that segment selectors are loaded before segment |
| 5079 | * descriptors |
| 5080 | */ |
| 5081 | kvm_load_segment_selector(vcpu, tss->ldt, VCPU_SREG_LDTR); |
| 5082 | kvm_load_segment_selector(vcpu, tss->es, VCPU_SREG_ES); |
| 5083 | kvm_load_segment_selector(vcpu, tss->cs, VCPU_SREG_CS); |
| 5084 | kvm_load_segment_selector(vcpu, tss->ss, VCPU_SREG_SS); |
| 5085 | kvm_load_segment_selector(vcpu, tss->ds, VCPU_SREG_DS); |
| 5086 | |
| 5087 | /* |
| 5088 | * Now load segment descriptors. If fault happenes at this stage |
| 5089 | * it is handled in a context of new task |
| 5090 | */ |
| 5091 | if (kvm_load_segment_descriptor(vcpu, tss->ldt, VCPU_SREG_LDTR)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5092 | return 1; |
| 5093 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5094 | if (kvm_load_segment_descriptor(vcpu, tss->es, VCPU_SREG_ES)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5095 | return 1; |
| 5096 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5097 | if (kvm_load_segment_descriptor(vcpu, tss->cs, VCPU_SREG_CS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5098 | return 1; |
| 5099 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5100 | if (kvm_load_segment_descriptor(vcpu, tss->ss, VCPU_SREG_SS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5101 | return 1; |
| 5102 | |
Gleb Natapov | c697518 | 2010-02-18 12:15:01 +0200 | [diff] [blame] | 5103 | if (kvm_load_segment_descriptor(vcpu, tss->ds, VCPU_SREG_DS)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5104 | return 1; |
| 5105 | return 0; |
| 5106 | } |
| 5107 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 5108 | static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector, |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5109 | u16 old_tss_sel, u32 old_tss_base, |
| 5110 | struct desc_struct *nseg_desc) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5111 | { |
| 5112 | struct tss_segment_16 tss_segment_16; |
| 5113 | int ret = 0; |
| 5114 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5115 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 5116 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5117 | goto out; |
| 5118 | |
| 5119 | save_state_to_tss16(vcpu, &tss_segment_16); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5120 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5121 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16, |
| 5122 | sizeof tss_segment_16)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5123 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5124 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5125 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc), |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5126 | &tss_segment_16, sizeof tss_segment_16)) |
| 5127 | goto out; |
| 5128 | |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5129 | if (old_tss_sel != 0xffff) { |
| 5130 | tss_segment_16.prev_task_link = old_tss_sel; |
| 5131 | |
| 5132 | if (kvm_write_guest(vcpu->kvm, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5133 | get_tss_base_addr_write(vcpu, nseg_desc), |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5134 | &tss_segment_16.prev_task_link, |
| 5135 | sizeof tss_segment_16.prev_task_link)) |
| 5136 | goto out; |
| 5137 | } |
| 5138 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5139 | if (load_state_from_tss16(vcpu, &tss_segment_16)) |
| 5140 | goto out; |
| 5141 | |
| 5142 | ret = 1; |
| 5143 | out: |
| 5144 | return ret; |
| 5145 | } |
| 5146 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 5147 | static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector, |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5148 | u16 old_tss_sel, u32 old_tss_base, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5149 | struct desc_struct *nseg_desc) |
| 5150 | { |
| 5151 | struct tss_segment_32 tss_segment_32; |
| 5152 | int ret = 0; |
| 5153 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5154 | if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 5155 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5156 | goto out; |
| 5157 | |
| 5158 | save_state_to_tss32(vcpu, &tss_segment_32); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5159 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5160 | if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32, |
| 5161 | sizeof tss_segment_32)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5162 | goto out; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5163 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5164 | if (kvm_read_guest(vcpu->kvm, get_tss_base_addr_read(vcpu, nseg_desc), |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5165 | &tss_segment_32, sizeof tss_segment_32)) |
| 5166 | goto out; |
| 5167 | |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5168 | if (old_tss_sel != 0xffff) { |
| 5169 | tss_segment_32.prev_task_link = old_tss_sel; |
| 5170 | |
| 5171 | if (kvm_write_guest(vcpu->kvm, |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5172 | get_tss_base_addr_write(vcpu, nseg_desc), |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5173 | &tss_segment_32.prev_task_link, |
| 5174 | sizeof tss_segment_32.prev_task_link)) |
| 5175 | goto out; |
| 5176 | } |
| 5177 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5178 | if (load_state_from_tss32(vcpu, &tss_segment_32)) |
| 5179 | goto out; |
| 5180 | |
| 5181 | ret = 1; |
| 5182 | out: |
| 5183 | return ret; |
| 5184 | } |
| 5185 | |
| 5186 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason) |
| 5187 | { |
| 5188 | struct kvm_segment tr_seg; |
| 5189 | struct desc_struct cseg_desc; |
| 5190 | struct desc_struct nseg_desc; |
| 5191 | int ret = 0; |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5192 | u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR); |
| 5193 | u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR); |
Jan Kiszka | e8861cfe | 2010-04-14 16:57:11 +0200 | [diff] [blame] | 5194 | u32 desc_limit; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5195 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5196 | old_tss_base = kvm_mmu_gva_to_gpa_write(vcpu, old_tss_base, NULL); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5197 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5198 | /* FIXME: Handle errors. Failure to read either TSS or their |
| 5199 | * descriptors should generate a pagefault. |
| 5200 | */ |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5201 | if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc)) |
| 5202 | goto out; |
| 5203 | |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5204 | if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc)) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5205 | goto out; |
| 5206 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5207 | if (reason != TASK_SWITCH_IRET) { |
| 5208 | int cpl; |
| 5209 | |
| 5210 | cpl = kvm_x86_ops->get_cpl(vcpu); |
| 5211 | if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) { |
| 5212 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 5213 | return 1; |
| 5214 | } |
| 5215 | } |
| 5216 | |
Jan Kiszka | e8861cfe | 2010-04-14 16:57:11 +0200 | [diff] [blame] | 5217 | desc_limit = get_desc_limit(&nseg_desc); |
| 5218 | if (!nseg_desc.p || |
| 5219 | ((desc_limit < 0x67 && (nseg_desc.type & 8)) || |
| 5220 | desc_limit < 0x2b)) { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5221 | kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc); |
| 5222 | return 1; |
| 5223 | } |
| 5224 | |
| 5225 | if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 5226 | cseg_desc.type &= ~(1 << 1); //clear the B flag |
Marcelo Tosatti | 34198bf8 | 2008-07-16 19:07:11 -0300 | [diff] [blame] | 5227 | save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | if (reason == TASK_SWITCH_IRET) { |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5231 | u32 eflags = kvm_get_rflags(vcpu); |
| 5232 | kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5233 | } |
| 5234 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5235 | /* set back link to prev task only if NT bit is set in eflags |
| 5236 | note that old_tss_sel is not used afetr this point */ |
| 5237 | if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE) |
| 5238 | old_tss_sel = 0xffff; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5239 | |
| 5240 | if (nseg_desc.type & 8) |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5241 | ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel, |
| 5242 | old_tss_base, &nseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5243 | else |
Gleb Natapov | b237ac3 | 2009-03-30 16:03:24 +0300 | [diff] [blame] | 5244 | ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel, |
| 5245 | old_tss_base, &nseg_desc); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5246 | |
| 5247 | if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) { |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5248 | u32 eflags = kvm_get_rflags(vcpu); |
| 5249 | kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | if (reason != TASK_SWITCH_IRET) { |
Izik Eidus | 3fe913e | 2008-04-28 18:23:52 +0300 | [diff] [blame] | 5253 | nseg_desc.type |= (1 << 1); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5254 | save_guest_segment_descriptor(vcpu, tss_selector, |
| 5255 | &nseg_desc); |
| 5256 | } |
| 5257 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5258 | kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0(vcpu) | X86_CR0_TS); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5259 | seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg); |
| 5260 | tr_seg.type = 11; |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5261 | kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5262 | out: |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5263 | return ret; |
| 5264 | } |
| 5265 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 5266 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5267 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 5268 | struct kvm_sregs *sregs) |
| 5269 | { |
| 5270 | int mmu_reset_needed = 0; |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5271 | int pending_vec, max_bits; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5272 | struct desc_ptr dt; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5273 | |
| 5274 | vcpu_load(vcpu); |
| 5275 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5276 | dt.size = sregs->idt.limit; |
| 5277 | dt.address = sregs->idt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5278 | kvm_x86_ops->set_idt(vcpu, &dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5279 | dt.size = sregs->gdt.limit; |
| 5280 | dt.address = sregs->gdt.base; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5281 | kvm_x86_ops->set_gdt(vcpu, &dt); |
| 5282 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5283 | vcpu->arch.cr2 = sregs->cr2; |
| 5284 | mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; |
Jan Kiszka | dc7e795 | 2009-07-01 20:52:03 +0200 | [diff] [blame] | 5285 | vcpu->arch.cr3 = sregs->cr3; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5286 | |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 5287 | kvm_set_cr8(vcpu, sregs->cr8); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5288 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5289 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5290 | kvm_x86_ops->set_efer(vcpu, sregs->efer); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5291 | kvm_set_apic_base(vcpu, sregs->apic_base); |
| 5292 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5293 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5294 | kvm_x86_ops->set_cr0(vcpu, sregs->cr0); |
Paul Knowles | d730616 | 2008-02-06 11:02:35 +0000 | [diff] [blame] | 5295 | vcpu->arch.cr0 = sregs->cr0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5296 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5297 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5298 | kvm_x86_ops->set_cr4(vcpu, sregs->cr4); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5299 | if (!is_long_mode(vcpu) && is_pae(vcpu)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5300 | load_pdptrs(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5301 | mmu_reset_needed = 1; |
| 5302 | } |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5303 | |
| 5304 | if (mmu_reset_needed) |
| 5305 | kvm_mmu_reset_context(vcpu); |
| 5306 | |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5307 | max_bits = (sizeof sregs->interrupt_bitmap) << 3; |
| 5308 | pending_vec = find_first_bit( |
| 5309 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 5310 | if (pending_vec < max_bits) { |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5311 | kvm_queue_interrupt(vcpu, pending_vec, false); |
Gleb Natapov | 923c61b | 2009-05-11 13:35:48 +0300 | [diff] [blame] | 5312 | pr_debug("Set back pending irq %d\n", pending_vec); |
| 5313 | if (irqchip_in_kernel(vcpu->kvm)) |
| 5314 | kvm_pic_clear_isr_ack(vcpu->kvm); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5315 | } |
| 5316 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5317 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 5318 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 5319 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 5320 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 5321 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 5322 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5323 | |
Guillaume Thouvenin | 3e6e0aa | 2008-05-27 10:18:46 +0200 | [diff] [blame] | 5324 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 5325 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5326 | |
Mikhail Ershov | 5f0269f | 2009-08-03 14:58:25 +0300 | [diff] [blame] | 5327 | update_cr8_intercept(vcpu); |
| 5328 | |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5329 | /* Older userspace won't unhalt the vcpu on reset. */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5330 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5331 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 5332 | !is_protmode(vcpu)) |
Marcelo Tosatti | 9c3e4aa | 2008-09-10 16:40:55 -0300 | [diff] [blame] | 5333 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 5334 | |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5335 | vcpu_put(vcpu); |
| 5336 | |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5340 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 5341 | struct kvm_guest_debug *dbg) |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5342 | { |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5343 | unsigned long rflags; |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5344 | int i, r; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5345 | |
| 5346 | vcpu_load(vcpu); |
| 5347 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5348 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 5349 | r = -EBUSY; |
| 5350 | if (vcpu->arch.exception.pending) |
| 5351 | goto unlock_out; |
| 5352 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 5353 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5354 | else |
| 5355 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 5356 | } |
| 5357 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5358 | /* |
| 5359 | * Read rflags as long as potentially injected trace flags are still |
| 5360 | * filtered out. |
| 5361 | */ |
| 5362 | rflags = kvm_get_rflags(vcpu); |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5363 | |
| 5364 | vcpu->guest_debug = dbg->control; |
| 5365 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 5366 | vcpu->guest_debug = 0; |
| 5367 | |
| 5368 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5369 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 5370 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
| 5371 | vcpu->arch.switch_db_regs = |
| 5372 | (dbg->arch.debugreg[7] & DR7_BP_EN_MASK); |
| 5373 | } else { |
| 5374 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 5375 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 5376 | vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK); |
| 5377 | } |
| 5378 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5379 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 5380 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 5381 | get_segment_base(vcpu, VCPU_SREG_CS); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5382 | |
Jan Kiszka | 91586a3 | 2009-10-05 13:07:21 +0200 | [diff] [blame] | 5383 | /* |
| 5384 | * Trigger an rflags update that will inject or remove the trace |
| 5385 | * flags. |
| 5386 | */ |
| 5387 | kvm_set_rflags(vcpu, rflags); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5388 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 5389 | kvm_x86_ops->set_guest_debug(vcpu, dbg); |
| 5390 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5391 | r = 0; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5392 | |
Jan Kiszka | 4f926bf2 | 2009-10-30 12:46:59 +0100 | [diff] [blame] | 5393 | unlock_out: |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 5394 | vcpu_put(vcpu); |
| 5395 | |
| 5396 | return r; |
| 5397 | } |
| 5398 | |
| 5399 | /* |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5400 | * fxsave fpu state. Taken from x86_64/processor.h. To be killed when |
| 5401 | * we have asm/x86/processor.h |
| 5402 | */ |
| 5403 | struct fxsave { |
| 5404 | u16 cwd; |
| 5405 | u16 swd; |
| 5406 | u16 twd; |
| 5407 | u16 fop; |
| 5408 | u64 rip; |
| 5409 | u64 rdp; |
| 5410 | u32 mxcsr; |
| 5411 | u32 mxcsr_mask; |
| 5412 | u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */ |
| 5413 | #ifdef CONFIG_X86_64 |
| 5414 | u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */ |
| 5415 | #else |
| 5416 | u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */ |
| 5417 | #endif |
| 5418 | }; |
| 5419 | |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5420 | /* |
| 5421 | * Translate a guest virtual address to a guest physical address. |
| 5422 | */ |
| 5423 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 5424 | struct kvm_translation *tr) |
| 5425 | { |
| 5426 | unsigned long vaddr = tr->linear_address; |
| 5427 | gpa_t gpa; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5428 | int idx; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5429 | |
| 5430 | vcpu_load(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5431 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 5432 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5433 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5434 | tr->physical_address = gpa; |
| 5435 | tr->valid = gpa != UNMAPPED_GVA; |
| 5436 | tr->writeable = 1; |
| 5437 | tr->usermode = 0; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 5438 | vcpu_put(vcpu); |
| 5439 | |
| 5440 | return 0; |
| 5441 | } |
| 5442 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5443 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5444 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5445 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5446 | |
| 5447 | vcpu_load(vcpu); |
| 5448 | |
| 5449 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 5450 | fpu->fcw = fxsave->cwd; |
| 5451 | fpu->fsw = fxsave->swd; |
| 5452 | fpu->ftwx = fxsave->twd; |
| 5453 | fpu->last_opcode = fxsave->fop; |
| 5454 | fpu->last_ip = fxsave->rip; |
| 5455 | fpu->last_dp = fxsave->rdp; |
| 5456 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space); |
| 5457 | |
| 5458 | vcpu_put(vcpu); |
| 5459 | |
| 5460 | return 0; |
| 5461 | } |
| 5462 | |
| 5463 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 5464 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5465 | struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5466 | |
| 5467 | vcpu_load(vcpu); |
| 5468 | |
| 5469 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 5470 | fxsave->cwd = fpu->fcw; |
| 5471 | fxsave->swd = fpu->fsw; |
| 5472 | fxsave->twd = fpu->ftwx; |
| 5473 | fxsave->fop = fpu->last_opcode; |
| 5474 | fxsave->rip = fpu->last_ip; |
| 5475 | fxsave->rdp = fpu->last_dp; |
| 5476 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space); |
| 5477 | |
| 5478 | vcpu_put(vcpu); |
| 5479 | |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | void fx_init(struct kvm_vcpu *vcpu) |
| 5484 | { |
| 5485 | unsigned after_mxcsr_mask; |
| 5486 | |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5487 | /* |
| 5488 | * Touch the fpu the first time in non atomic context as if |
| 5489 | * this is the first fpu instruction the exception handler |
| 5490 | * will fire before the instruction returns and it'll have to |
| 5491 | * allocate ram with GFP_KERNEL. |
| 5492 | */ |
| 5493 | if (!used_math()) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5494 | kvm_fx_save(&vcpu->arch.host_fx_image); |
Andrea Arcangeli | bc1a34f | 2008-05-01 18:43:33 +0200 | [diff] [blame] | 5495 | |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5496 | /* Initialize guest FPU by resetting ours and saving into guest's */ |
| 5497 | preempt_disable(); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5498 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5499 | kvm_fx_finit(); |
| 5500 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5501 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5502 | preempt_enable(); |
| 5503 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5504 | vcpu->arch.cr0 |= X86_CR0_ET; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5505 | after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5506 | vcpu->arch.guest_fx_image.mxcsr = 0x1f80; |
| 5507 | memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask, |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5508 | 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask); |
| 5509 | } |
| 5510 | EXPORT_SYMBOL_GPL(fx_init); |
| 5511 | |
| 5512 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 5513 | { |
Avi Kivity | 2608d7a | 2010-01-21 15:31:45 +0200 | [diff] [blame] | 5514 | if (vcpu->guest_fpu_loaded) |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5515 | return; |
| 5516 | |
| 5517 | vcpu->guest_fpu_loaded = 1; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5518 | kvm_fx_save(&vcpu->arch.host_fx_image); |
| 5519 | kvm_fx_restore(&vcpu->arch.guest_fx_image); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5520 | trace_kvm_fpu(1); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5521 | } |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5522 | |
| 5523 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 5524 | { |
| 5525 | if (!vcpu->guest_fpu_loaded) |
| 5526 | return; |
| 5527 | |
| 5528 | vcpu->guest_fpu_loaded = 0; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 5529 | kvm_fx_save(&vcpu->arch.guest_fx_image); |
| 5530 | kvm_fx_restore(&vcpu->arch.host_fx_image); |
Avi Kivity | f096ed8 | 2007-11-18 13:54:33 +0200 | [diff] [blame] | 5531 | ++vcpu->stat.fpu_reload; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 5532 | set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests); |
Avi Kivity | 0c04851 | 2010-01-21 15:31:52 +0200 | [diff] [blame] | 5533 | trace_kvm_fpu(0); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 5534 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5535 | |
| 5536 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 5537 | { |
Joerg Roedel | 7f1ea20 | 2009-02-25 16:08:31 +0100 | [diff] [blame] | 5538 | if (vcpu->arch.time_page) { |
| 5539 | kvm_release_page_dirty(vcpu->arch.time_page); |
| 5540 | vcpu->arch.time_page = NULL; |
| 5541 | } |
| 5542 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5543 | kvm_x86_ops->vcpu_free(vcpu); |
| 5544 | } |
| 5545 | |
| 5546 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 5547 | unsigned int id) |
| 5548 | { |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5549 | return kvm_x86_ops->vcpu_create(kvm, id); |
| 5550 | } |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5551 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5552 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 5553 | { |
| 5554 | int r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5555 | |
| 5556 | /* We do fxsave: this must be aligned. */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5557 | BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5558 | |
Sheng Yang | 0bed3b5 | 2008-10-09 16:01:54 +0800 | [diff] [blame] | 5559 | vcpu->arch.mtrr_state.have_fixed = 1; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5560 | vcpu_load(vcpu); |
| 5561 | r = kvm_arch_vcpu_reset(vcpu); |
| 5562 | if (r == 0) |
| 5563 | r = kvm_mmu_setup(vcpu); |
| 5564 | vcpu_put(vcpu); |
| 5565 | if (r < 0) |
| 5566 | goto free_vcpu; |
| 5567 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5568 | return 0; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5569 | free_vcpu: |
| 5570 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 5571 | return r; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5572 | } |
| 5573 | |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 5574 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5575 | { |
| 5576 | vcpu_load(vcpu); |
| 5577 | kvm_mmu_unload(vcpu); |
| 5578 | vcpu_put(vcpu); |
| 5579 | |
| 5580 | kvm_x86_ops->vcpu_free(vcpu); |
| 5581 | } |
| 5582 | |
| 5583 | int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu) |
| 5584 | { |
Jan Kiszka | 448fa4a | 2008-09-26 09:30:48 +0200 | [diff] [blame] | 5585 | vcpu->arch.nmi_pending = false; |
| 5586 | vcpu->arch.nmi_injected = false; |
| 5587 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5588 | vcpu->arch.switch_db_regs = 0; |
| 5589 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
| 5590 | vcpu->arch.dr6 = DR6_FIXED_1; |
| 5591 | vcpu->arch.dr7 = DR7_FIXED_1; |
| 5592 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5593 | return kvm_x86_ops->vcpu_reset(vcpu); |
| 5594 | } |
| 5595 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5596 | int kvm_arch_hardware_enable(void *garbage) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5597 | { |
Zachary Amsden | 0cca790 | 2009-09-29 11:38:35 -1000 | [diff] [blame] | 5598 | /* |
| 5599 | * Since this may be called from a hotplug notifcation, |
| 5600 | * we can't get the CPU frequency directly. |
| 5601 | */ |
| 5602 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
| 5603 | int cpu = raw_smp_processor_id(); |
| 5604 | per_cpu(cpu_tsc_khz, cpu) = 0; |
| 5605 | } |
Avi Kivity | 18863bd | 2009-09-07 11:12:18 +0300 | [diff] [blame] | 5606 | |
| 5607 | kvm_shared_msr_cpu_online(); |
| 5608 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 5609 | return kvm_x86_ops->hardware_enable(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | void kvm_arch_hardware_disable(void *garbage) |
| 5613 | { |
| 5614 | kvm_x86_ops->hardware_disable(garbage); |
Avi Kivity | 3548bab | 2009-11-28 14:18:47 +0200 | [diff] [blame] | 5615 | drop_user_return_notifiers(garbage); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | int kvm_arch_hardware_setup(void) |
| 5619 | { |
| 5620 | return kvm_x86_ops->hardware_setup(); |
| 5621 | } |
| 5622 | |
| 5623 | void kvm_arch_hardware_unsetup(void) |
| 5624 | { |
| 5625 | kvm_x86_ops->hardware_unsetup(); |
| 5626 | } |
| 5627 | |
| 5628 | void kvm_arch_check_processor_compat(void *rtn) |
| 5629 | { |
| 5630 | kvm_x86_ops->check_processor_compatibility(rtn); |
| 5631 | } |
| 5632 | |
| 5633 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 5634 | { |
| 5635 | struct page *page; |
| 5636 | struct kvm *kvm; |
| 5637 | int r; |
| 5638 | |
| 5639 | BUG_ON(vcpu->kvm == NULL); |
| 5640 | kvm = vcpu->kvm; |
| 5641 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5642 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 5643 | if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu)) |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5644 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5645 | else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5646 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5647 | |
| 5648 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 5649 | if (!page) { |
| 5650 | r = -ENOMEM; |
| 5651 | goto fail; |
| 5652 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5653 | vcpu->arch.pio_data = page_address(page); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5654 | |
| 5655 | r = kvm_mmu_create(vcpu); |
| 5656 | if (r < 0) |
| 5657 | goto fail_free_pio_data; |
| 5658 | |
| 5659 | if (irqchip_in_kernel(kvm)) { |
| 5660 | r = kvm_create_lapic(vcpu); |
| 5661 | if (r < 0) |
| 5662 | goto fail_mmu_destroy; |
| 5663 | } |
| 5664 | |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5665 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 5666 | GFP_KERNEL); |
| 5667 | if (!vcpu->arch.mce_banks) { |
| 5668 | r = -ENOMEM; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5669 | goto fail_free_lapic; |
Huang Ying | 890ca9a | 2009-05-11 16:48:15 +0800 | [diff] [blame] | 5670 | } |
| 5671 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 5672 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5673 | return 0; |
Wei Yongjun | 443c39b | 2010-01-22 14:21:29 +0800 | [diff] [blame] | 5674 | fail_free_lapic: |
| 5675 | kvm_free_lapic(vcpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5676 | fail_mmu_destroy: |
| 5677 | kvm_mmu_destroy(vcpu); |
| 5678 | fail_free_pio_data: |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5679 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5680 | fail: |
| 5681 | return r; |
| 5682 | } |
| 5683 | |
| 5684 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 5685 | { |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5686 | int idx; |
| 5687 | |
Wei Yongjun | 36cb93f | 2010-01-22 14:18:47 +0800 | [diff] [blame] | 5688 | kfree(vcpu->arch.mce_banks); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5689 | kvm_free_lapic(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5690 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5691 | kvm_mmu_destroy(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 5692 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5693 | free_page((unsigned long)vcpu->arch.pio_data); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 5694 | } |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5695 | |
| 5696 | struct kvm *kvm_arch_create_vm(void) |
| 5697 | { |
| 5698 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
| 5699 | |
| 5700 | if (!kvm) |
| 5701 | return ERR_PTR(-ENOMEM); |
| 5702 | |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5703 | kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL); |
| 5704 | if (!kvm->arch.aliases) { |
| 5705 | kfree(kvm); |
| 5706 | return ERR_PTR(-ENOMEM); |
| 5707 | } |
| 5708 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5709 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
Ben-Ami Yassour | 4d5c5d0 | 2008-07-28 19:26:26 +0300 | [diff] [blame] | 5710 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5711 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 5712 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 5713 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 5714 | |
Marcelo Tosatti | 53f658b3 | 2008-12-11 20:45:05 +0100 | [diff] [blame] | 5715 | rdtscll(kvm->arch.vm_init_tsc); |
| 5716 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5717 | return kvm; |
| 5718 | } |
| 5719 | |
| 5720 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 5721 | { |
| 5722 | vcpu_load(vcpu); |
| 5723 | kvm_mmu_unload(vcpu); |
| 5724 | vcpu_put(vcpu); |
| 5725 | } |
| 5726 | |
| 5727 | static void kvm_free_vcpus(struct kvm *kvm) |
| 5728 | { |
| 5729 | unsigned int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5730 | struct kvm_vcpu *vcpu; |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5731 | |
| 5732 | /* |
| 5733 | * Unpin any mmu pages first. |
| 5734 | */ |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5735 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5736 | kvm_unload_vcpu_mmu(vcpu); |
| 5737 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5738 | kvm_arch_vcpu_free(vcpu); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5739 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 5740 | mutex_lock(&kvm->lock); |
| 5741 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 5742 | kvm->vcpus[i] = NULL; |
| 5743 | |
| 5744 | atomic_set(&kvm->online_vcpus, 0); |
| 5745 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5746 | } |
| 5747 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5748 | void kvm_arch_sync_events(struct kvm *kvm) |
| 5749 | { |
Sheng Yang | ba4cef3 | 2009-01-06 10:03:03 +0800 | [diff] [blame] | 5750 | kvm_free_all_assigned_devices(kvm); |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 5751 | } |
| 5752 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5753 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 5754 | { |
Sheng Yang | 6eb5581 | 2008-10-31 12:37:41 +0800 | [diff] [blame] | 5755 | kvm_iommu_unmap_guest(kvm); |
Sheng Yang | 7837699 | 2008-01-28 05:10:22 +0800 | [diff] [blame] | 5756 | kvm_free_pit(kvm); |
Zhang Xiantao | d7deeeb0 | 2007-12-14 10:17:34 +0800 | [diff] [blame] | 5757 | kfree(kvm->arch.vpic); |
| 5758 | kfree(kvm->arch.vioapic); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5759 | kvm_free_vcpus(kvm); |
| 5760 | kvm_free_physmem(kvm); |
Avi Kivity | 3d45830 | 2008-03-25 11:26:13 +0200 | [diff] [blame] | 5761 | if (kvm->arch.apic_access_page) |
| 5762 | put_page(kvm->arch.apic_access_page); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5763 | if (kvm->arch.ept_identity_pagetable) |
| 5764 | put_page(kvm->arch.ept_identity_pagetable); |
Marcelo Tosatti | 6474920 | 2010-01-19 12:45:23 -0200 | [diff] [blame] | 5765 | cleanup_srcu_struct(&kvm->srcu); |
Marcelo Tosatti | fef9cce | 2009-12-23 14:35:17 -0200 | [diff] [blame] | 5766 | kfree(kvm->arch.aliases); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 5767 | kfree(kvm); |
| 5768 | } |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5769 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5770 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 5771 | struct kvm_memory_slot *memslot, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5772 | struct kvm_memory_slot old, |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5773 | struct kvm_userspace_memory_region *mem, |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5774 | int user_alloc) |
| 5775 | { |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5776 | int npages = memslot->npages; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5777 | |
| 5778 | /*To keep backward compatibility with older userspace, |
| 5779 | *x86 needs to hanlde !user_alloc case. |
| 5780 | */ |
| 5781 | if (!user_alloc) { |
| 5782 | if (npages && !old.rmap) { |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5783 | unsigned long userspace_addr; |
| 5784 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5785 | down_write(¤t->mm->mmap_sem); |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5786 | userspace_addr = do_mmap(NULL, 0, |
| 5787 | npages * PAGE_SIZE, |
| 5788 | PROT_READ | PROT_WRITE, |
Avi Kivity | acee3c0 | 2008-08-26 17:22:47 +0300 | [diff] [blame] | 5789 | MAP_PRIVATE | MAP_ANONYMOUS, |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5790 | 0); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5791 | up_write(¤t->mm->mmap_sem); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5792 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5793 | if (IS_ERR((void *)userspace_addr)) |
| 5794 | return PTR_ERR((void *)userspace_addr); |
| 5795 | |
Andrea Arcangeli | 604b38a | 2008-07-25 16:32:03 +0200 | [diff] [blame] | 5796 | memslot->userspace_addr = userspace_addr; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5797 | } |
| 5798 | } |
| 5799 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 5800 | |
| 5801 | return 0; |
| 5802 | } |
| 5803 | |
| 5804 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 5805 | struct kvm_userspace_memory_region *mem, |
| 5806 | struct kvm_memory_slot old, |
| 5807 | int user_alloc) |
| 5808 | { |
| 5809 | |
| 5810 | int npages = mem->memory_size >> PAGE_SHIFT; |
| 5811 | |
| 5812 | if (!user_alloc && !old.user_alloc && old.rmap && !npages) { |
| 5813 | int ret; |
| 5814 | |
| 5815 | down_write(¤t->mm->mmap_sem); |
| 5816 | ret = do_munmap(current->mm, old.userspace_addr, |
| 5817 | old.npages * PAGE_SIZE); |
| 5818 | up_write(¤t->mm->mmap_sem); |
| 5819 | if (ret < 0) |
| 5820 | printk(KERN_WARNING |
| 5821 | "kvm_vm_ioctl_set_memory_region: " |
| 5822 | "failed to munmap memory\n"); |
| 5823 | } |
| 5824 | |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5825 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 5826 | if (!kvm->arch.n_requested_mmu_pages) { |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5827 | unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm); |
| 5828 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 5829 | } |
| 5830 | |
| 5831 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
Marcelo Tosatti | 7c8a83b | 2009-05-12 18:55:43 -0300 | [diff] [blame] | 5832 | spin_unlock(&kvm->mmu_lock); |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 5833 | } |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5834 | |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5835 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 5836 | { |
| 5837 | kvm_mmu_zap_all(kvm); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 5838 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 5839 | } |
| 5840 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5841 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 5842 | { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 5843 | return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 5844 | || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED |
| 5845 | || vcpu->arch.nmi_pending || |
| 5846 | (kvm_arch_interrupt_allowed(vcpu) && |
| 5847 | kvm_cpu_has_interrupt(vcpu)); |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 5848 | } |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5849 | |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5850 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) |
| 5851 | { |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5852 | int me; |
| 5853 | int cpu = vcpu->cpu; |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5854 | |
| 5855 | if (waitqueue_active(&vcpu->wq)) { |
| 5856 | wake_up_interruptible(&vcpu->wq); |
| 5857 | ++vcpu->stat.halt_wakeup; |
| 5858 | } |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 5859 | |
| 5860 | me = get_cpu(); |
| 5861 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) |
| 5862 | if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests)) |
| 5863 | smp_send_reschedule(cpu); |
Marcelo Tosatti | e9571ed | 2008-04-11 15:01:22 -0300 | [diff] [blame] | 5864 | put_cpu(); |
Zhang Xiantao | 5736199 | 2007-12-17 14:21:40 +0800 | [diff] [blame] | 5865 | } |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5866 | |
| 5867 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5868 | { |
| 5869 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 5870 | } |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5871 | |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5872 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 5873 | { |
| 5874 | unsigned long current_rip = kvm_rip_read(vcpu) + |
| 5875 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 5876 | |
| 5877 | return current_rip == linear_rip; |
| 5878 | } |
| 5879 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 5880 | |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5881 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 5882 | { |
| 5883 | unsigned long rflags; |
| 5884 | |
| 5885 | rflags = kvm_x86_ops->get_rflags(vcpu); |
| 5886 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame^] | 5887 | rflags &= ~X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5888 | return rflags; |
| 5889 | } |
| 5890 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 5891 | |
| 5892 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 5893 | { |
| 5894 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
Jan Kiszka | f92653e | 2010-02-23 17:47:55 +0100 | [diff] [blame] | 5895 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
Jan Kiszka | c310bac | 2010-02-23 17:47:58 +0100 | [diff] [blame^] | 5896 | rflags |= X86_EFLAGS_TF; |
Jan Kiszka | 94fe45d | 2009-10-18 13:24:44 +0200 | [diff] [blame] | 5897 | kvm_x86_ops->set_rflags(vcpu, rflags); |
| 5898 | } |
| 5899 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 5900 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5901 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
| 5902 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 5903 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 5904 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 5905 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 5906 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 5907 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 5908 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 5909 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 5910 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 5911 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |