Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1 | /* |
Masanari Iida | d755814 | 2012-08-22 18:40:21 +0900 | [diff] [blame] | 2 | * kvm_ia64.c: Basic KVM support On Itanium series processors |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2007, Intel Corporation. |
| 6 | * Xiantao Zhang (xiantao.zhang@intel.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms and conditions of the GNU General Public License, |
| 10 | * version 2, as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 19 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/percpu.h> |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 26 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 28 | #include <linux/smp.h> |
| 29 | #include <linux/kvm_host.h> |
| 30 | #include <linux/kvm.h> |
| 31 | #include <linux/bitops.h> |
| 32 | #include <linux/hrtimer.h> |
| 33 | #include <linux/uaccess.h> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 34 | #include <linux/iommu.h> |
Xiantao Zhang | 2381ad2 | 2008-10-08 08:29:33 +0800 | [diff] [blame] | 35 | #include <linux/intel-iommu.h> |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 36 | #include <linux/pci.h> |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 37 | |
| 38 | #include <asm/pgtable.h> |
| 39 | #include <asm/gcc_intrin.h> |
| 40 | #include <asm/pal.h> |
| 41 | #include <asm/cacheflush.h> |
| 42 | #include <asm/div64.h> |
| 43 | #include <asm/tlb.h> |
Jes Sorensen | 9f72632 | 2008-09-12 14:12:08 +0200 | [diff] [blame] | 44 | #include <asm/elf.h> |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 45 | #include <asm/sn/addrs.h> |
| 46 | #include <asm/sn/clksupport.h> |
| 47 | #include <asm/sn/shub_mmr.h> |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 48 | |
| 49 | #include "misc.h" |
| 50 | #include "vti.h" |
| 51 | #include "iodev.h" |
| 52 | #include "ioapic.h" |
| 53 | #include "lapic.h" |
Xiantao Zhang | 2f74977 | 2008-09-27 11:46:36 +0800 | [diff] [blame] | 54 | #include "irq.h" |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 55 | |
| 56 | static unsigned long kvm_vmm_base; |
| 57 | static unsigned long kvm_vsa_base; |
| 58 | static unsigned long kvm_vm_buffer; |
| 59 | static unsigned long kvm_vm_buffer_size; |
| 60 | unsigned long kvm_vmm_gp; |
| 61 | |
| 62 | static long vp_env_info; |
| 63 | |
| 64 | static struct kvm_vmm_info *kvm_vmm_info; |
| 65 | |
| 66 | static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu); |
| 67 | |
| 68 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 69 | { NULL } |
| 70 | }; |
| 71 | |
Jes Sorensen | c6c9fcd | 2009-02-25 10:38:53 -0600 | [diff] [blame] | 72 | static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu) |
| 73 | { |
| 74 | #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC) |
| 75 | if (vcpu->kvm->arch.is_sn2) |
| 76 | return rtc_time(); |
| 77 | else |
| 78 | #endif |
| 79 | return ia64_getreg(_IA64_REG_AR_ITC); |
| 80 | } |
| 81 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 82 | static void kvm_flush_icache(unsigned long start, unsigned long len) |
| 83 | { |
| 84 | int l; |
| 85 | |
| 86 | for (l = 0; l < (len + 32); l += 32) |
Isaku Yamahata | 7120569 | 2009-03-27 15:11:57 +0900 | [diff] [blame] | 87 | ia64_fc((void *)(start + l)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 88 | |
| 89 | ia64_sync_i(); |
| 90 | ia64_srlz_i(); |
| 91 | } |
| 92 | |
| 93 | static void kvm_flush_tlb_all(void) |
| 94 | { |
| 95 | unsigned long i, j, count0, count1, stride0, stride1, addr; |
| 96 | long flags; |
| 97 | |
| 98 | addr = local_cpu_data->ptce_base; |
| 99 | count0 = local_cpu_data->ptce_count[0]; |
| 100 | count1 = local_cpu_data->ptce_count[1]; |
| 101 | stride0 = local_cpu_data->ptce_stride[0]; |
| 102 | stride1 = local_cpu_data->ptce_stride[1]; |
| 103 | |
| 104 | local_irq_save(flags); |
| 105 | for (i = 0; i < count0; ++i) { |
| 106 | for (j = 0; j < count1; ++j) { |
| 107 | ia64_ptce(addr); |
| 108 | addr += stride1; |
| 109 | } |
| 110 | addr += stride0; |
| 111 | } |
| 112 | local_irq_restore(flags); |
| 113 | ia64_srlz_i(); /* srlz.i implies srlz.d */ |
| 114 | } |
| 115 | |
| 116 | long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler) |
| 117 | { |
| 118 | struct ia64_pal_retval iprv; |
| 119 | |
| 120 | PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva, |
| 121 | (u64)opt_handler); |
| 122 | |
| 123 | return iprv.status; |
| 124 | } |
| 125 | |
| 126 | static DEFINE_SPINLOCK(vp_lock); |
| 127 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 128 | int kvm_arch_hardware_enable(void *garbage) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 129 | { |
| 130 | long status; |
| 131 | long tmp_base; |
| 132 | unsigned long pte; |
| 133 | unsigned long saved_psr; |
| 134 | int slot; |
| 135 | |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 136 | pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 137 | local_irq_save(saved_psr); |
| 138 | slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT); |
Julia Lawall | cab7a1e | 2008-07-22 21:38:18 +0200 | [diff] [blame] | 139 | local_irq_restore(saved_psr); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 140 | if (slot < 0) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 141 | return -EINVAL; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 142 | |
| 143 | spin_lock(&vp_lock); |
| 144 | status = ia64_pal_vp_init_env(kvm_vsa_base ? |
| 145 | VP_INIT_ENV : VP_INIT_ENV_INITALIZE, |
| 146 | __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base); |
| 147 | if (status != 0) { |
Julia Lawall | 3499f4d | 2010-05-26 17:57:05 +0200 | [diff] [blame] | 148 | spin_unlock(&vp_lock); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 149 | printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n"); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 150 | return -EINVAL; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (!kvm_vsa_base) { |
| 154 | kvm_vsa_base = tmp_base; |
| 155 | printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base); |
| 156 | } |
| 157 | spin_unlock(&vp_lock); |
| 158 | ia64_ptr_entry(0x3, slot); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 159 | |
| 160 | return 0; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void kvm_arch_hardware_disable(void *garbage) |
| 164 | { |
| 165 | |
| 166 | long status; |
| 167 | int slot; |
| 168 | unsigned long pte; |
| 169 | unsigned long saved_psr; |
| 170 | unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA); |
| 171 | |
| 172 | pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), |
| 173 | PAGE_KERNEL)); |
| 174 | |
| 175 | local_irq_save(saved_psr); |
| 176 | slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT); |
Julia Lawall | cab7a1e | 2008-07-22 21:38:18 +0200 | [diff] [blame] | 177 | local_irq_restore(saved_psr); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 178 | if (slot < 0) |
| 179 | return; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 180 | |
| 181 | status = ia64_pal_vp_exit_env(host_iva); |
| 182 | if (status) |
| 183 | printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n", |
| 184 | status); |
| 185 | ia64_ptr_entry(0x3, slot); |
| 186 | } |
| 187 | |
| 188 | void kvm_arch_check_processor_compat(void *rtn) |
| 189 | { |
| 190 | *(int *)rtn = 0; |
| 191 | } |
| 192 | |
| 193 | int kvm_dev_ioctl_check_extension(long ext) |
| 194 | { |
| 195 | |
| 196 | int r; |
| 197 | |
| 198 | switch (ext) { |
| 199 | case KVM_CAP_IRQCHIP: |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 200 | case KVM_CAP_MP_STATE: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 201 | case KVM_CAP_IRQ_INJECT_STATUS: |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 202 | r = 1; |
| 203 | break; |
Laurent Vivier | 7f39f8a | 2008-05-30 16:05:57 +0200 | [diff] [blame] | 204 | case KVM_CAP_COALESCED_MMIO: |
| 205 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 206 | break; |
Xiantao Zhang | 2381ad2 | 2008-10-08 08:29:33 +0800 | [diff] [blame] | 207 | case KVM_CAP_IOMMU: |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 208 | r = iommu_present(&pci_bus_type); |
Xiantao Zhang | 2381ad2 | 2008-10-08 08:29:33 +0800 | [diff] [blame] | 209 | break; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 210 | default: |
| 211 | r = 0; |
| 212 | } |
| 213 | return r; |
| 214 | |
| 215 | } |
| 216 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 217 | static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 218 | { |
| 219 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 220 | kvm_run->hw.hardware_exit_reason = 1; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 225 | { |
| 226 | struct kvm_mmio_req *p; |
| 227 | struct kvm_io_device *mmio_dev; |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 228 | int r; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 229 | |
| 230 | p = kvm_get_vcpu_ioreq(vcpu); |
| 231 | |
| 232 | if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS) |
| 233 | goto mmio; |
| 234 | vcpu->mmio_needed = 1; |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 235 | vcpu->mmio_fragments[0].gpa = kvm_run->mmio.phys_addr = p->addr; |
| 236 | vcpu->mmio_fragments[0].len = kvm_run->mmio.len = p->size; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 237 | vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir; |
| 238 | |
| 239 | if (vcpu->mmio_is_write) |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 240 | memcpy(vcpu->arch.mmio_data, &p->data, p->size); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 241 | memcpy(kvm_run->mmio.data, &p->data, p->size); |
| 242 | kvm_run->exit_reason = KVM_EXIT_MMIO; |
| 243 | return 0; |
| 244 | mmio: |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 245 | if (p->dir) |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 246 | r = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, p->addr, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 247 | p->size, &p->data); |
| 248 | else |
Marcelo Tosatti | e93f8a0 | 2009-12-23 14:35:24 -0200 | [diff] [blame] | 249 | r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 250 | p->size, &p->data); |
| 251 | if (r) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 252 | printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr); |
| 253 | p->state = STATE_IORESP_READY; |
| 254 | |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 259 | { |
| 260 | struct exit_ctl_data *p; |
| 261 | |
| 262 | p = kvm_get_exit_data(vcpu); |
| 263 | |
| 264 | if (p->exit_reason == EXIT_REASON_PAL_CALL) |
| 265 | return kvm_pal_emul(vcpu, kvm_run); |
| 266 | else { |
| 267 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 268 | kvm_run->hw.hardware_exit_reason = 2; |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 274 | { |
| 275 | struct exit_ctl_data *p; |
| 276 | |
| 277 | p = kvm_get_exit_data(vcpu); |
| 278 | |
| 279 | if (p->exit_reason == EXIT_REASON_SAL_CALL) { |
| 280 | kvm_sal_emul(vcpu); |
| 281 | return 1; |
| 282 | } else { |
| 283 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 284 | kvm_run->hw.hardware_exit_reason = 3; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 290 | static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector) |
| 291 | { |
| 292 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
| 293 | |
| 294 | if (!test_and_set_bit(vector, &vpd->irr[0])) { |
| 295 | vcpu->arch.irq_new_pending = 1; |
| 296 | kvm_vcpu_kick(vcpu); |
| 297 | return 1; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 302 | /* |
| 303 | * offset: address offset to IPI space. |
| 304 | * value: deliver value. |
| 305 | */ |
| 306 | static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm, |
| 307 | uint64_t vector) |
| 308 | { |
| 309 | switch (dm) { |
| 310 | case SAPIC_FIXED: |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 311 | break; |
| 312 | case SAPIC_NMI: |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 313 | vector = 2; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 314 | break; |
| 315 | case SAPIC_EXTINT: |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 316 | vector = 0; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 317 | break; |
| 318 | case SAPIC_INIT: |
| 319 | case SAPIC_PMI: |
| 320 | default: |
| 321 | printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n"); |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 322 | return; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 323 | } |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 324 | __apic_accept_irq(vcpu, vector); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id, |
| 328 | unsigned long eid) |
| 329 | { |
| 330 | union ia64_lid lid; |
| 331 | int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 332 | struct kvm_vcpu *vcpu; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 333 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 334 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 335 | lid.val = VCPU_LID(vcpu); |
| 336 | if (lid.id == id && lid.eid == eid) |
| 337 | return vcpu; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return NULL; |
| 341 | } |
| 342 | |
| 343 | static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 344 | { |
| 345 | struct exit_ctl_data *p = kvm_get_exit_data(vcpu); |
| 346 | struct kvm_vcpu *target_vcpu; |
| 347 | struct kvm_pt_regs *regs; |
| 348 | union ia64_ipi_a addr = p->u.ipi_data.addr; |
| 349 | union ia64_ipi_d data = p->u.ipi_data.data; |
| 350 | |
| 351 | target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid); |
| 352 | if (!target_vcpu) |
| 353 | return handle_vm_error(vcpu, kvm_run); |
| 354 | |
| 355 | if (!target_vcpu->arch.launched) { |
| 356 | regs = vcpu_regs(target_vcpu); |
| 357 | |
| 358 | regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip; |
| 359 | regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp; |
| 360 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 361 | target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 362 | if (waitqueue_active(&target_vcpu->wq)) |
| 363 | wake_up_interruptible(&target_vcpu->wq); |
| 364 | } else { |
| 365 | vcpu_deliver_ipi(target_vcpu, data.dm, data.vector); |
| 366 | if (target_vcpu != vcpu) |
| 367 | kvm_vcpu_kick(target_vcpu); |
| 368 | } |
| 369 | |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | struct call_data { |
| 374 | struct kvm_ptc_g ptc_g_data; |
| 375 | struct kvm_vcpu *vcpu; |
| 376 | }; |
| 377 | |
| 378 | static void vcpu_global_purge(void *info) |
| 379 | { |
| 380 | struct call_data *p = (struct call_data *)info; |
| 381 | struct kvm_vcpu *vcpu = p->vcpu; |
| 382 | |
| 383 | if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
| 384 | return; |
| 385 | |
| 386 | set_bit(KVM_REQ_PTC_G, &vcpu->requests); |
| 387 | if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) { |
| 388 | vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] = |
| 389 | p->ptc_g_data; |
| 390 | } else { |
| 391 | clear_bit(KVM_REQ_PTC_G, &vcpu->requests); |
| 392 | vcpu->arch.ptc_g_count = 0; |
| 393 | set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 398 | { |
| 399 | struct exit_ctl_data *p = kvm_get_exit_data(vcpu); |
| 400 | struct kvm *kvm = vcpu->kvm; |
| 401 | struct call_data call_data; |
| 402 | int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 403 | struct kvm_vcpu *vcpui; |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 404 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 405 | call_data.ptc_g_data = p->u.ptc_g_data; |
| 406 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 407 | kvm_for_each_vcpu(i, vcpui, kvm) { |
| 408 | if (vcpui->arch.mp_state == KVM_MP_STATE_UNINITIALIZED || |
| 409 | vcpu == vcpui) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 410 | continue; |
| 411 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 412 | if (waitqueue_active(&vcpui->wq)) |
| 413 | wake_up_interruptible(&vcpui->wq); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 414 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 415 | if (vcpui->cpu != -1) { |
| 416 | call_data.vcpu = vcpui; |
| 417 | smp_call_function_single(vcpui->cpu, |
Takashi Iwai | 2f73cca | 2008-07-17 18:09:12 +0200 | [diff] [blame] | 418 | vcpu_global_purge, &call_data, 1); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 419 | } else |
| 420 | printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n"); |
| 421 | |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 427 | { |
| 428 | return 1; |
| 429 | } |
| 430 | |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 431 | static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu) |
| 432 | { |
| 433 | unsigned long pte, rtc_phys_addr, map_addr; |
| 434 | int slot; |
| 435 | |
| 436 | map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT); |
| 437 | rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC; |
| 438 | pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC)); |
| 439 | slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT); |
| 440 | vcpu->arch.sn_rtc_tr_slot = slot; |
| 441 | if (slot < 0) { |
| 442 | printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n"); |
| 443 | slot = 0; |
| 444 | } |
| 445 | return slot; |
| 446 | } |
| 447 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 448 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 449 | { |
| 450 | |
| 451 | ktime_t kt; |
| 452 | long itc_diff; |
| 453 | unsigned long vcpu_now_itc; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 454 | unsigned long expires; |
| 455 | struct hrtimer *p_ht = &vcpu->arch.hlt_timer; |
| 456 | unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec; |
| 457 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
| 458 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 459 | if (irqchip_in_kernel(vcpu->kvm)) { |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 460 | |
Jes Sorensen | c6c9fcd | 2009-02-25 10:38:53 -0600 | [diff] [blame] | 461 | vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset; |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 462 | |
| 463 | if (time_after(vcpu_now_itc, vpd->itm)) { |
| 464 | vcpu->arch.timer_check = 1; |
| 465 | return 1; |
| 466 | } |
| 467 | itc_diff = vpd->itm - vcpu_now_itc; |
| 468 | if (itc_diff < 0) |
| 469 | itc_diff = -itc_diff; |
| 470 | |
| 471 | expires = div64_u64(itc_diff, cyc_per_usec); |
| 472 | kt = ktime_set(0, 1000 * expires); |
| 473 | |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 474 | vcpu->arch.ht_active = 1; |
| 475 | hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS); |
| 476 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 477 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 478 | kvm_vcpu_block(vcpu); |
| 479 | hrtimer_cancel(p_ht); |
| 480 | vcpu->arch.ht_active = 0; |
| 481 | |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 482 | if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) || |
| 483 | kvm_cpu_has_pending_timer(vcpu)) |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 484 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
Gleb Natapov | 09cec75 | 2009-03-23 15:11:44 +0200 | [diff] [blame] | 485 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 486 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 487 | if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 488 | return -EINTR; |
| 489 | return 1; |
| 490 | } else { |
| 491 | printk(KERN_ERR"kvm: Unsupported userspace halt!"); |
| 492 | return 0; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static int handle_vm_shutdown(struct kvm_vcpu *vcpu, |
| 497 | struct kvm_run *kvm_run) |
| 498 | { |
| 499 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, |
| 504 | struct kvm_run *kvm_run) |
| 505 | { |
| 506 | return 1; |
| 507 | } |
| 508 | |
Xiantao Zhang | 7d63797 | 2008-11-21 20:58:11 +0800 | [diff] [blame] | 509 | static int handle_vcpu_debug(struct kvm_vcpu *vcpu, |
| 510 | struct kvm_run *kvm_run) |
| 511 | { |
| 512 | printk("VMM: %s", vcpu->arch.log_buf); |
| 513 | return 1; |
| 514 | } |
| 515 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 516 | static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu, |
| 517 | struct kvm_run *kvm_run) = { |
| 518 | [EXIT_REASON_VM_PANIC] = handle_vm_error, |
| 519 | [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio, |
| 520 | [EXIT_REASON_PAL_CALL] = handle_pal_call, |
| 521 | [EXIT_REASON_SAL_CALL] = handle_sal_call, |
| 522 | [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6, |
| 523 | [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown, |
| 524 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
| 525 | [EXIT_REASON_IPI] = handle_ipi, |
| 526 | [EXIT_REASON_PTC_G] = handle_global_purge, |
Xiantao Zhang | 7d63797 | 2008-11-21 20:58:11 +0800 | [diff] [blame] | 527 | [EXIT_REASON_DEBUG] = handle_vcpu_debug, |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 528 | |
| 529 | }; |
| 530 | |
| 531 | static const int kvm_vti_max_exit_handlers = |
| 532 | sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers); |
| 533 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 534 | static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu) |
| 535 | { |
| 536 | struct exit_ctl_data *p_exit_data; |
| 537 | |
| 538 | p_exit_data = kvm_get_exit_data(vcpu); |
| 539 | return p_exit_data->exit_reason; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * The guest has exited. See if we can fix it or if we need userspace |
| 544 | * assistance. |
| 545 | */ |
| 546 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 547 | { |
| 548 | u32 exit_reason = kvm_get_exit_reason(vcpu); |
| 549 | vcpu->arch.last_exit = exit_reason; |
| 550 | |
| 551 | if (exit_reason < kvm_vti_max_exit_handlers |
| 552 | && kvm_vti_exit_handlers[exit_reason]) |
| 553 | return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run); |
| 554 | else { |
| 555 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 556 | kvm_run->hw.hardware_exit_reason = exit_reason; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static inline void vti_set_rr6(unsigned long rr6) |
| 562 | { |
| 563 | ia64_set_rr(RR6, rr6); |
| 564 | ia64_srlz_i(); |
| 565 | } |
| 566 | |
| 567 | static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu) |
| 568 | { |
| 569 | unsigned long pte; |
| 570 | struct kvm *kvm = vcpu->kvm; |
| 571 | int r; |
| 572 | |
| 573 | /*Insert a pair of tr to map vmm*/ |
| 574 | pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL)); |
| 575 | r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT); |
| 576 | if (r < 0) |
| 577 | goto out; |
| 578 | vcpu->arch.vmm_tr_slot = r; |
| 579 | /*Insert a pairt of tr to map data of vm*/ |
| 580 | pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL)); |
| 581 | r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE, |
| 582 | pte, KVM_VM_DATA_SHIFT); |
| 583 | if (r < 0) |
| 584 | goto out; |
| 585 | vcpu->arch.vm_tr_slot = r; |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 586 | |
| 587 | #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC) |
| 588 | if (kvm->arch.is_sn2) { |
| 589 | r = kvm_sn2_setup_mappings(vcpu); |
| 590 | if (r < 0) |
| 591 | goto out; |
| 592 | } |
| 593 | #endif |
| 594 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 595 | r = 0; |
| 596 | out: |
| 597 | return r; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu) |
| 601 | { |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 602 | struct kvm *kvm = vcpu->kvm; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 603 | ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot); |
| 604 | ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot); |
Jes Sorensen | 0c72ea7 | 2009-02-25 10:38:52 -0600 | [diff] [blame] | 605 | #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC) |
| 606 | if (kvm->arch.is_sn2) |
| 607 | ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot); |
| 608 | #endif |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu) |
| 612 | { |
Jes Sorensen | 4d13c3b | 2009-04-16 16:53:13 +0200 | [diff] [blame] | 613 | unsigned long psr; |
| 614 | int r; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 615 | int cpu = smp_processor_id(); |
| 616 | |
| 617 | if (vcpu->arch.last_run_cpu != cpu || |
| 618 | per_cpu(last_vcpu, cpu) != vcpu) { |
| 619 | per_cpu(last_vcpu, cpu) = vcpu; |
| 620 | vcpu->arch.last_run_cpu = cpu; |
| 621 | kvm_flush_tlb_all(); |
| 622 | } |
| 623 | |
| 624 | vcpu->arch.host_rr6 = ia64_get_rr(RR6); |
| 625 | vti_set_rr6(vcpu->arch.vmm_rr); |
Jes Sorensen | 4d13c3b | 2009-04-16 16:53:13 +0200 | [diff] [blame] | 626 | local_irq_save(psr); |
| 627 | r = kvm_insert_vmm_mapping(vcpu); |
| 628 | local_irq_restore(psr); |
| 629 | return r; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 630 | } |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 631 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 632 | static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu) |
| 633 | { |
| 634 | kvm_purge_vmm_mapping(vcpu); |
| 635 | vti_set_rr6(vcpu->arch.host_rr6); |
| 636 | } |
| 637 | |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 638 | static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 639 | { |
| 640 | union context *host_ctx, *guest_ctx; |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 641 | int r, idx; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 642 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 643 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 644 | |
| 645 | again: |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 646 | if (signal_pending(current)) { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 647 | r = -EINTR; |
| 648 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 649 | goto out; |
| 650 | } |
| 651 | |
Jes Sorensen | d24d2c1 | 2009-04-09 16:38:14 +0200 | [diff] [blame] | 652 | preempt_disable(); |
| 653 | local_irq_disable(); |
| 654 | |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 655 | /*Get host and guest context with guest address space.*/ |
| 656 | host_ctx = kvm_get_host_context(vcpu); |
| 657 | guest_ctx = kvm_get_guest_context(vcpu); |
| 658 | |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 659 | clear_bit(KVM_REQ_KICK, &vcpu->requests); |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 660 | |
| 661 | r = kvm_vcpu_pre_transition(vcpu); |
| 662 | if (r < 0) |
| 663 | goto vcpu_run_fail; |
| 664 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 665 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 666 | vcpu->mode = IN_GUEST_MODE; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 667 | kvm_guest_enter(); |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * Transition to the guest |
| 671 | */ |
| 672 | kvm_vmm_info->tramp_entry(host_ctx, guest_ctx); |
| 673 | |
| 674 | kvm_vcpu_post_transition(vcpu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 675 | |
| 676 | vcpu->arch.launched = 1; |
Marcelo Tosatti | 32f8840 | 2009-05-07 17:55:12 -0300 | [diff] [blame] | 677 | set_bit(KVM_REQ_KICK, &vcpu->requests); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 678 | local_irq_enable(); |
| 679 | |
| 680 | /* |
| 681 | * We must have an instruction between local_irq_enable() and |
| 682 | * kvm_guest_exit(), so the timer interrupt isn't delayed by |
| 683 | * the interrupt shadow. The stat.exits increment will do nicely. |
| 684 | * But we need to prevent reordering, hence this barrier(): |
| 685 | */ |
| 686 | barrier(); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 687 | kvm_guest_exit(); |
Xiao Guangrong | 6b7e2d0 | 2011-01-12 15:40:31 +0800 | [diff] [blame] | 688 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 689 | preempt_enable(); |
| 690 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 691 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 692 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 693 | r = kvm_handle_exit(kvm_run, vcpu); |
| 694 | |
| 695 | if (r > 0) { |
| 696 | if (!need_resched()) |
| 697 | goto again; |
| 698 | } |
| 699 | |
| 700 | out: |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 701 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 702 | if (r > 0) { |
| 703 | kvm_resched(vcpu); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 704 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 705 | goto again; |
| 706 | } |
| 707 | |
| 708 | return r; |
Jes Sorensen | c6b60c6 | 2009-04-16 10:43:48 +0200 | [diff] [blame] | 709 | |
| 710 | vcpu_run_fail: |
| 711 | local_irq_enable(); |
| 712 | preempt_enable(); |
| 713 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 714 | goto out; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static void kvm_set_mmio_data(struct kvm_vcpu *vcpu) |
| 718 | { |
| 719 | struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu); |
| 720 | |
| 721 | if (!vcpu->mmio_is_write) |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 722 | memcpy(&p->data, vcpu->arch.mmio_data, 8); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 723 | p->state = STATE_IORESP_READY; |
| 724 | } |
| 725 | |
| 726 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 727 | { |
| 728 | int r; |
| 729 | sigset_t sigsaved; |
| 730 | |
Xiantao Zhang | a2e4e28 | 2008-10-23 15:02:52 +0800 | [diff] [blame] | 731 | if (vcpu->sigset_active) |
| 732 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 733 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 734 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 735 | kvm_vcpu_block(vcpu); |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 736 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Xiantao Zhang | a2e4e28 | 2008-10-23 15:02:52 +0800 | [diff] [blame] | 737 | r = -EAGAIN; |
| 738 | goto out; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 739 | } |
| 740 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 741 | if (vcpu->mmio_needed) { |
Avi Kivity | f78146b | 2012-04-18 19:22:47 +0300 | [diff] [blame] | 742 | memcpy(vcpu->arch.mmio_data, kvm_run->mmio.data, 8); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 743 | kvm_set_mmio_data(vcpu); |
| 744 | vcpu->mmio_read_completed = 1; |
| 745 | vcpu->mmio_needed = 0; |
| 746 | } |
| 747 | r = __vcpu_run(vcpu, kvm_run); |
Xiantao Zhang | a2e4e28 | 2008-10-23 15:02:52 +0800 | [diff] [blame] | 748 | out: |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 749 | if (vcpu->sigset_active) |
| 750 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 751 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 752 | return r; |
| 753 | } |
| 754 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 755 | struct kvm *kvm_arch_alloc_vm(void) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 756 | { |
| 757 | |
| 758 | struct kvm *kvm; |
| 759 | uint64_t vm_base; |
| 760 | |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 761 | BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE); |
| 762 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 763 | vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE)); |
| 764 | |
| 765 | if (!vm_base) |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 766 | return NULL; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 767 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 768 | memset((void *)vm_base, 0, KVM_VM_DATA_SIZE); |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 769 | kvm = (struct kvm *)(vm_base + |
| 770 | offsetof(struct kvm_vm_data, kvm_vm_struct)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 771 | kvm->arch.vm_base = vm_base; |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 772 | printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 773 | |
| 774 | return kvm; |
| 775 | } |
| 776 | |
Xiao Guangrong | 60f9a9e | 2011-11-24 18:09:11 +0800 | [diff] [blame] | 777 | struct kvm_ia64_io_range { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 778 | unsigned long start; |
| 779 | unsigned long size; |
| 780 | unsigned long type; |
| 781 | }; |
| 782 | |
Xiao Guangrong | 60f9a9e | 2011-11-24 18:09:11 +0800 | [diff] [blame] | 783 | static const struct kvm_ia64_io_range io_ranges[] = { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 784 | {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER}, |
| 785 | {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO}, |
| 786 | {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO}, |
| 787 | {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC}, |
| 788 | {PIB_START, PIB_SIZE, GPFN_PIB}, |
| 789 | }; |
| 790 | |
| 791 | static void kvm_build_io_pmt(struct kvm *kvm) |
| 792 | { |
| 793 | unsigned long i, j; |
| 794 | |
| 795 | /* Mark I/O ranges */ |
| 796 | for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range)); |
| 797 | i++) { |
| 798 | for (j = io_ranges[i].start; |
| 799 | j < io_ranges[i].start + io_ranges[i].size; |
| 800 | j += PAGE_SIZE) |
| 801 | kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT, |
| 802 | io_ranges[i].type, 0); |
| 803 | } |
| 804 | |
| 805 | } |
| 806 | |
| 807 | /*Use unused rids to virtualize guest rid.*/ |
| 808 | #define GUEST_PHYSICAL_RR0 0x1739 |
| 809 | #define GUEST_PHYSICAL_RR4 0x2739 |
| 810 | #define VMM_INIT_RR 0x1660 |
| 811 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 812 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 813 | { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 814 | BUG_ON(!kvm); |
| 815 | |
Carsten Otte | e08b963 | 2012-01-04 10:25:20 +0100 | [diff] [blame] | 816 | if (type) |
| 817 | return -EINVAL; |
| 818 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 819 | kvm->arch.is_sn2 = ia64_platform_is("sn2"); |
| 820 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 821 | kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0; |
| 822 | kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4; |
| 823 | kvm->arch.vmm_init_rr = VMM_INIT_RR; |
| 824 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 825 | /* |
| 826 | *Fill P2M entries for MMIO/IO ranges |
| 827 | */ |
| 828 | kvm_build_io_pmt(kvm); |
| 829 | |
Xiantao Zhang | 2381ad2 | 2008-10-08 08:29:33 +0800 | [diff] [blame] | 830 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 831 | |
| 832 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 833 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 834 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 835 | return 0; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, |
| 839 | struct kvm_irqchip *chip) |
| 840 | { |
| 841 | int r; |
| 842 | |
| 843 | r = 0; |
| 844 | switch (chip->chip_id) { |
| 845 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 846 | r = kvm_get_ioapic(kvm, &chip->chip.ioapic); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 847 | break; |
| 848 | default: |
| 849 | r = -EINVAL; |
| 850 | break; |
| 851 | } |
| 852 | return r; |
| 853 | } |
| 854 | |
| 855 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 856 | { |
| 857 | int r; |
| 858 | |
| 859 | r = 0; |
| 860 | switch (chip->chip_id) { |
| 861 | case KVM_IRQCHIP_IOAPIC: |
Gleb Natapov | eba0226 | 2009-08-24 11:54:25 +0300 | [diff] [blame] | 862 | r = kvm_set_ioapic(kvm, &chip->chip.ioapic); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 863 | break; |
| 864 | default: |
| 865 | r = -EINVAL; |
| 866 | break; |
| 867 | } |
| 868 | return r; |
| 869 | } |
| 870 | |
| 871 | #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x |
| 872 | |
| 873 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 874 | { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 875 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 876 | int i; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 877 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 878 | for (i = 0; i < 16; i++) { |
| 879 | vpd->vgr[i] = regs->vpd.vgr[i]; |
| 880 | vpd->vbgr[i] = regs->vpd.vbgr[i]; |
| 881 | } |
| 882 | for (i = 0; i < 128; i++) |
| 883 | vpd->vcr[i] = regs->vpd.vcr[i]; |
| 884 | vpd->vhpi = regs->vpd.vhpi; |
| 885 | vpd->vnat = regs->vpd.vnat; |
| 886 | vpd->vbnat = regs->vpd.vbnat; |
| 887 | vpd->vpsr = regs->vpd.vpsr; |
| 888 | |
| 889 | vpd->vpr = regs->vpd.vpr; |
| 890 | |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 891 | memcpy(&vcpu->arch.guest, ®s->saved_guest, sizeof(union context)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 892 | |
| 893 | RESTORE_REGS(mp_state); |
| 894 | RESTORE_REGS(vmm_rr); |
| 895 | memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS); |
| 896 | memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS); |
| 897 | RESTORE_REGS(itr_regions); |
| 898 | RESTORE_REGS(dtr_regions); |
| 899 | RESTORE_REGS(tc_regions); |
| 900 | RESTORE_REGS(irq_check); |
| 901 | RESTORE_REGS(itc_check); |
| 902 | RESTORE_REGS(timer_check); |
| 903 | RESTORE_REGS(timer_pending); |
| 904 | RESTORE_REGS(last_itc); |
| 905 | for (i = 0; i < 8; i++) { |
| 906 | vcpu->arch.vrr[i] = regs->vrr[i]; |
| 907 | vcpu->arch.ibr[i] = regs->ibr[i]; |
| 908 | vcpu->arch.dbr[i] = regs->dbr[i]; |
| 909 | } |
| 910 | for (i = 0; i < 4; i++) |
| 911 | vcpu->arch.insvc[i] = regs->insvc[i]; |
| 912 | RESTORE_REGS(xtp); |
| 913 | RESTORE_REGS(metaphysical_rr0); |
| 914 | RESTORE_REGS(metaphysical_rr4); |
| 915 | RESTORE_REGS(metaphysical_saved_rr0); |
| 916 | RESTORE_REGS(metaphysical_saved_rr4); |
| 917 | RESTORE_REGS(fp_psr); |
| 918 | RESTORE_REGS(saved_gp); |
| 919 | |
| 920 | vcpu->arch.irq_new_pending = 1; |
Jes Sorensen | c6c9fcd | 2009-02-25 10:38:53 -0600 | [diff] [blame] | 921 | vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 922 | set_bit(KVM_REQ_RESUME, &vcpu->requests); |
| 923 | |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 924 | return 0; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 925 | } |
| 926 | |
Christoffer Dall | 23d43cf | 2012-07-24 08:51:20 -0400 | [diff] [blame] | 927 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event) |
| 928 | { |
| 929 | if (!irqchip_in_kernel(kvm)) |
| 930 | return -ENXIO; |
| 931 | |
| 932 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 933 | irq_event->irq, irq_event->level); |
| 934 | return 0; |
| 935 | } |
| 936 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 937 | long kvm_arch_vm_ioctl(struct file *filp, |
| 938 | unsigned int ioctl, unsigned long arg) |
| 939 | { |
| 940 | struct kvm *kvm = filp->private_data; |
| 941 | void __user *argp = (void __user *)arg; |
Avi Kivity | 367e131 | 2009-08-26 14:57:07 +0300 | [diff] [blame] | 942 | int r = -ENOTTY; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 943 | |
| 944 | switch (ioctl) { |
| 945 | case KVM_SET_MEMORY_REGION: { |
| 946 | struct kvm_memory_region kvm_mem; |
| 947 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 948 | |
| 949 | r = -EFAULT; |
| 950 | if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem)) |
| 951 | goto out; |
| 952 | kvm_userspace_mem.slot = kvm_mem.slot; |
| 953 | kvm_userspace_mem.flags = kvm_mem.flags; |
| 954 | kvm_userspace_mem.guest_phys_addr = |
| 955 | kvm_mem.guest_phys_addr; |
| 956 | kvm_userspace_mem.memory_size = kvm_mem.memory_size; |
| 957 | r = kvm_vm_ioctl_set_memory_region(kvm, |
| 958 | &kvm_userspace_mem, 0); |
| 959 | if (r) |
| 960 | goto out; |
| 961 | break; |
| 962 | } |
| 963 | case KVM_CREATE_IRQCHIP: |
| 964 | r = -EFAULT; |
| 965 | r = kvm_ioapic_init(kvm); |
| 966 | if (r) |
| 967 | goto out; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 968 | r = kvm_setup_default_irq_routing(kvm); |
| 969 | if (r) { |
Takuya Yoshikawa | 175504c | 2010-12-16 01:41:37 +0900 | [diff] [blame] | 970 | mutex_lock(&kvm->slots_lock); |
Wei Yongjun | 4b7bb92 | 2010-02-09 10:41:56 +0800 | [diff] [blame] | 971 | kvm_ioapic_destroy(kvm); |
Takuya Yoshikawa | 175504c | 2010-12-16 01:41:37 +0900 | [diff] [blame] | 972 | mutex_unlock(&kvm->slots_lock); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 973 | goto out; |
| 974 | } |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 975 | break; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 976 | case KVM_GET_IRQCHIP: { |
| 977 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 978 | struct kvm_irqchip chip; |
| 979 | |
| 980 | r = -EFAULT; |
| 981 | if (copy_from_user(&chip, argp, sizeof chip)) |
| 982 | goto out; |
| 983 | r = -ENXIO; |
| 984 | if (!irqchip_in_kernel(kvm)) |
| 985 | goto out; |
| 986 | r = kvm_vm_ioctl_get_irqchip(kvm, &chip); |
| 987 | if (r) |
| 988 | goto out; |
| 989 | r = -EFAULT; |
| 990 | if (copy_to_user(argp, &chip, sizeof chip)) |
| 991 | goto out; |
| 992 | r = 0; |
| 993 | break; |
| 994 | } |
| 995 | case KVM_SET_IRQCHIP: { |
| 996 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 997 | struct kvm_irqchip chip; |
| 998 | |
| 999 | r = -EFAULT; |
| 1000 | if (copy_from_user(&chip, argp, sizeof chip)) |
| 1001 | goto out; |
| 1002 | r = -ENXIO; |
| 1003 | if (!irqchip_in_kernel(kvm)) |
| 1004 | goto out; |
| 1005 | r = kvm_vm_ioctl_set_irqchip(kvm, &chip); |
| 1006 | if (r) |
| 1007 | goto out; |
| 1008 | r = 0; |
| 1009 | break; |
| 1010 | } |
| 1011 | default: |
| 1012 | ; |
| 1013 | } |
| 1014 | out: |
| 1015 | return r; |
| 1016 | } |
| 1017 | |
| 1018 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 1019 | struct kvm_sregs *sregs) |
| 1020 | { |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
| 1024 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 1025 | struct kvm_sregs *sregs) |
| 1026 | { |
| 1027 | return -EINVAL; |
| 1028 | |
| 1029 | } |
| 1030 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 1031 | struct kvm_translation *tr) |
| 1032 | { |
| 1033 | |
| 1034 | return -EINVAL; |
| 1035 | } |
| 1036 | |
| 1037 | static int kvm_alloc_vmm_area(void) |
| 1038 | { |
| 1039 | if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) { |
| 1040 | kvm_vmm_base = __get_free_pages(GFP_KERNEL, |
| 1041 | get_order(KVM_VMM_SIZE)); |
| 1042 | if (!kvm_vmm_base) |
| 1043 | return -ENOMEM; |
| 1044 | |
| 1045 | memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE); |
| 1046 | kvm_vm_buffer = kvm_vmm_base + VMM_SIZE; |
| 1047 | |
| 1048 | printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n", |
| 1049 | kvm_vmm_base, kvm_vm_buffer); |
| 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | static void kvm_free_vmm_area(void) |
| 1056 | { |
| 1057 | if (kvm_vmm_base) { |
| 1058 | /*Zero this area before free to avoid bits leak!!*/ |
| 1059 | memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE); |
| 1060 | free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE)); |
| 1061 | kvm_vmm_base = 0; |
| 1062 | kvm_vm_buffer = 0; |
| 1063 | kvm_vsa_base = 0; |
| 1064 | } |
| 1065 | } |
| 1066 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1067 | static int vti_init_vpd(struct kvm_vcpu *vcpu) |
| 1068 | { |
| 1069 | int i; |
| 1070 | union cpuid3_t cpuid3; |
| 1071 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
| 1072 | |
| 1073 | if (IS_ERR(vpd)) |
| 1074 | return PTR_ERR(vpd); |
| 1075 | |
| 1076 | /* CPUID init */ |
| 1077 | for (i = 0; i < 5; i++) |
| 1078 | vpd->vcpuid[i] = ia64_get_cpuid(i); |
| 1079 | |
| 1080 | /* Limit the CPUID number to 5 */ |
| 1081 | cpuid3.value = vpd->vcpuid[3]; |
| 1082 | cpuid3.number = 4; /* 5 - 1 */ |
| 1083 | vpd->vcpuid[3] = cpuid3.value; |
| 1084 | |
| 1085 | /*Set vac and vdc fields*/ |
| 1086 | vpd->vac.a_from_int_cr = 1; |
| 1087 | vpd->vac.a_to_int_cr = 1; |
| 1088 | vpd->vac.a_from_psr = 1; |
| 1089 | vpd->vac.a_from_cpuid = 1; |
| 1090 | vpd->vac.a_cover = 1; |
| 1091 | vpd->vac.a_bsw = 1; |
| 1092 | vpd->vac.a_int = 1; |
| 1093 | vpd->vdc.d_vmsw = 1; |
| 1094 | |
| 1095 | /*Set virtual buffer*/ |
| 1096 | vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE; |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | static int vti_create_vp(struct kvm_vcpu *vcpu) |
| 1102 | { |
| 1103 | long ret; |
| 1104 | struct vpd *vpd = vcpu->arch.vpd; |
| 1105 | unsigned long vmm_ivt; |
| 1106 | |
| 1107 | vmm_ivt = kvm_vmm_info->vmm_ivt; |
| 1108 | |
| 1109 | printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt); |
| 1110 | |
| 1111 | ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0); |
| 1112 | |
| 1113 | if (ret) { |
| 1114 | printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n"); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static void init_ptce_info(struct kvm_vcpu *vcpu) |
| 1121 | { |
| 1122 | ia64_ptce_info_t ptce = {0}; |
| 1123 | |
| 1124 | ia64_get_ptce(&ptce); |
| 1125 | vcpu->arch.ptce_base = ptce.base; |
| 1126 | vcpu->arch.ptce_count[0] = ptce.count[0]; |
| 1127 | vcpu->arch.ptce_count[1] = ptce.count[1]; |
| 1128 | vcpu->arch.ptce_stride[0] = ptce.stride[0]; |
| 1129 | vcpu->arch.ptce_stride[1] = ptce.stride[1]; |
| 1130 | } |
| 1131 | |
| 1132 | static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu) |
| 1133 | { |
| 1134 | struct hrtimer *p_ht = &vcpu->arch.hlt_timer; |
| 1135 | |
| 1136 | if (hrtimer_cancel(p_ht)) |
Arjan van de Ven | 18dd36a | 2008-09-01 15:19:11 -0700 | [diff] [blame] | 1137 | hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data) |
| 1141 | { |
| 1142 | struct kvm_vcpu *vcpu; |
| 1143 | wait_queue_head_t *q; |
| 1144 | |
| 1145 | vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer); |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 1146 | q = &vcpu->wq; |
| 1147 | |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 1148 | if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1149 | goto out; |
| 1150 | |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 1151 | if (waitqueue_active(q)) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1152 | wake_up_interruptible(q); |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 1153 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1154 | out: |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 1155 | vcpu->arch.timer_fired = 1; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1156 | vcpu->arch.timer_check = 1; |
| 1157 | return HRTIMER_NORESTART; |
| 1158 | } |
| 1159 | |
| 1160 | #define PALE_RESET_ENTRY 0x80000000ffffffb0UL |
| 1161 | |
Avi Kivity | 3e51570 | 2012-03-05 14:23:29 +0200 | [diff] [blame] | 1162 | bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) |
| 1163 | { |
Avi Kivity | 8281715 | 2012-04-18 19:23:50 +0300 | [diff] [blame] | 1164 | return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL); |
Avi Kivity | 3e51570 | 2012-03-05 14:23:29 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1167 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 1168 | { |
| 1169 | struct kvm_vcpu *v; |
| 1170 | int r; |
| 1171 | int i; |
| 1172 | long itc_offset; |
| 1173 | struct kvm *kvm = vcpu->kvm; |
| 1174 | struct kvm_pt_regs *regs = vcpu_regs(vcpu); |
| 1175 | |
| 1176 | union context *p_ctx = &vcpu->arch.guest; |
| 1177 | struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu); |
| 1178 | |
| 1179 | /*Init vcpu context for first run.*/ |
| 1180 | if (IS_ERR(vmm_vcpu)) |
| 1181 | return PTR_ERR(vmm_vcpu); |
| 1182 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 1183 | if (kvm_vcpu_is_bsp(vcpu)) { |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 1184 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1185 | |
| 1186 | /*Set entry address for first run.*/ |
| 1187 | regs->cr_iip = PALE_RESET_ENTRY; |
| 1188 | |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1189 | /*Initialize itc offset for vcpus*/ |
Jes Sorensen | c6c9fcd | 2009-02-25 10:38:53 -0600 | [diff] [blame] | 1190 | itc_offset = 0UL - kvm_get_itc(vcpu); |
Jes Sorensen | 3032b92 | 2009-05-25 10:22:17 +0200 | [diff] [blame] | 1191 | for (i = 0; i < KVM_MAX_VCPUS; i++) { |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1192 | v = (struct kvm_vcpu *)((char *)vcpu + |
| 1193 | sizeof(struct kvm_vcpu_data) * i); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1194 | v->arch.itc_offset = itc_offset; |
| 1195 | v->arch.last_itc = 0; |
| 1196 | } |
| 1197 | } else |
Avi Kivity | a453529 | 2008-04-13 17:54:35 +0300 | [diff] [blame] | 1198 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1199 | |
| 1200 | r = -ENOMEM; |
| 1201 | vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL); |
| 1202 | if (!vcpu->arch.apic) |
| 1203 | goto out; |
| 1204 | vcpu->arch.apic->vcpu = vcpu; |
| 1205 | |
| 1206 | p_ctx->gr[1] = 0; |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1207 | p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1208 | p_ctx->gr[13] = (unsigned long)vmm_vcpu; |
| 1209 | p_ctx->psr = 0x1008522000UL; |
| 1210 | p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/ |
| 1211 | p_ctx->caller_unat = 0; |
| 1212 | p_ctx->pr = 0x0; |
| 1213 | p_ctx->ar[36] = 0x0; /*unat*/ |
| 1214 | p_ctx->ar[19] = 0x0; /*rnat*/ |
| 1215 | p_ctx->ar[18] = (unsigned long)vmm_vcpu + |
| 1216 | ((sizeof(struct kvm_vcpu)+15) & ~15); |
| 1217 | p_ctx->ar[64] = 0x0; /*pfs*/ |
| 1218 | p_ctx->cr[0] = 0x7e04UL; |
| 1219 | p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt; |
| 1220 | p_ctx->cr[8] = 0x3c; |
| 1221 | |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 1222 | /*Initialize region register*/ |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1223 | p_ctx->rr[0] = 0x30; |
| 1224 | p_ctx->rr[1] = 0x30; |
| 1225 | p_ctx->rr[2] = 0x30; |
| 1226 | p_ctx->rr[3] = 0x30; |
| 1227 | p_ctx->rr[4] = 0x30; |
| 1228 | p_ctx->rr[5] = 0x30; |
| 1229 | p_ctx->rr[7] = 0x30; |
| 1230 | |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 1231 | /*Initialize branch register 0*/ |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1232 | p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry; |
| 1233 | |
| 1234 | vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr; |
| 1235 | vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0; |
| 1236 | vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4; |
| 1237 | |
| 1238 | hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 1239 | vcpu->arch.hlt_timer.function = hlt_timer_fn; |
| 1240 | |
| 1241 | vcpu->arch.last_run_cpu = -1; |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1242 | vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1243 | vcpu->arch.vsa_base = kvm_vsa_base; |
| 1244 | vcpu->arch.__gp = kvm_vmm_gp; |
| 1245 | vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock); |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1246 | vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id); |
| 1247 | vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1248 | init_ptce_info(vcpu); |
| 1249 | |
| 1250 | r = 0; |
| 1251 | out: |
| 1252 | return r; |
| 1253 | } |
| 1254 | |
| 1255 | static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id) |
| 1256 | { |
| 1257 | unsigned long psr; |
| 1258 | int r; |
| 1259 | |
| 1260 | local_irq_save(psr); |
| 1261 | r = kvm_insert_vmm_mapping(vcpu); |
Jes Sorensen | 457459c | 2009-04-16 16:08:29 +0200 | [diff] [blame] | 1262 | local_irq_restore(psr); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1263 | if (r) |
| 1264 | goto fail; |
| 1265 | r = kvm_vcpu_init(vcpu, vcpu->kvm, id); |
| 1266 | if (r) |
| 1267 | goto fail; |
| 1268 | |
| 1269 | r = vti_init_vpd(vcpu); |
| 1270 | if (r) { |
| 1271 | printk(KERN_DEBUG"kvm: vpd init error!!\n"); |
| 1272 | goto uninit; |
| 1273 | } |
| 1274 | |
| 1275 | r = vti_create_vp(vcpu); |
| 1276 | if (r) |
| 1277 | goto uninit; |
| 1278 | |
| 1279 | kvm_purge_vmm_mapping(vcpu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1280 | |
| 1281 | return 0; |
| 1282 | uninit: |
| 1283 | kvm_vcpu_uninit(vcpu); |
| 1284 | fail: |
| 1285 | return r; |
| 1286 | } |
| 1287 | |
| 1288 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, |
| 1289 | unsigned int id) |
| 1290 | { |
| 1291 | struct kvm_vcpu *vcpu; |
| 1292 | unsigned long vm_base = kvm->arch.vm_base; |
| 1293 | int r; |
| 1294 | int cpu; |
| 1295 | |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1296 | BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2); |
| 1297 | |
| 1298 | r = -EINVAL; |
| 1299 | if (id >= KVM_MAX_VCPUS) { |
| 1300 | printk(KERN_ERR"kvm: Can't configure vcpus > %ld", |
| 1301 | KVM_MAX_VCPUS); |
| 1302 | goto fail; |
| 1303 | } |
| 1304 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1305 | r = -ENOMEM; |
| 1306 | if (!vm_base) { |
| 1307 | printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id); |
| 1308 | goto fail; |
| 1309 | } |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1310 | vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data, |
| 1311 | vcpu_data[id].vcpu_struct)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1312 | vcpu->kvm = kvm; |
| 1313 | |
| 1314 | cpu = get_cpu(); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1315 | r = vti_vcpu_setup(vcpu, id); |
| 1316 | put_cpu(); |
| 1317 | |
| 1318 | if (r) { |
| 1319 | printk(KERN_DEBUG"kvm: vcpu_setup error!!\n"); |
| 1320 | goto fail; |
| 1321 | } |
| 1322 | |
| 1323 | return vcpu; |
| 1324 | fail: |
| 1325 | return ERR_PTR(r); |
| 1326 | } |
| 1327 | |
| 1328 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1329 | { |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
Marcelo Tosatti | 42897d8 | 2012-11-27 23:29:02 -0200 | [diff] [blame] | 1333 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 1334 | { |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1338 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1339 | { |
| 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
| 1343 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1344 | { |
| 1345 | return -EINVAL; |
| 1346 | } |
| 1347 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1348 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 1349 | struct kvm_guest_debug *dbg) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1350 | { |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | |
Jan Kiszka | d89f5ef | 2010-11-09 17:02:49 +0100 | [diff] [blame] | 1354 | void kvm_arch_free_vm(struct kvm *kvm) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1355 | { |
| 1356 | unsigned long vm_base = kvm->arch.vm_base; |
| 1357 | |
| 1358 | if (vm_base) { |
| 1359 | memset((void *)vm_base, 0, KVM_VM_DATA_SIZE); |
| 1360 | free_pages(vm_base, get_order(KVM_VM_DATA_SIZE)); |
| 1361 | } |
| 1362 | |
| 1363 | } |
| 1364 | |
| 1365 | static void kvm_release_vm_pages(struct kvm *kvm) |
| 1366 | { |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 1367 | struct kvm_memslots *slots; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1368 | struct kvm_memory_slot *memslot; |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 1369 | int j; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1370 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 1371 | slots = kvm_memslots(kvm); |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 1372 | kvm_for_each_memslot(memslot, slots) { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1373 | for (j = 0; j < memslot->npages; j++) { |
| 1374 | if (memslot->rmap[j]) |
| 1375 | put_page((struct page *)memslot->rmap[j]); |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
Sheng Yang | ad8ba2c | 2009-01-06 10:03:02 +0800 | [diff] [blame] | 1380 | void kvm_arch_sync_events(struct kvm *kvm) |
| 1381 | { |
| 1382 | } |
| 1383 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1384 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 1385 | { |
Xiantao Zhang | 2381ad2 | 2008-10-08 08:29:33 +0800 | [diff] [blame] | 1386 | kvm_iommu_unmap_guest(kvm); |
| 1387 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
| 1388 | kvm_free_all_assigned_devices(kvm); |
| 1389 | #endif |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1390 | kfree(kvm->arch.vioapic); |
| 1391 | kvm_release_vm_pages(kvm); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 1395 | { |
| 1396 | } |
| 1397 | |
| 1398 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1399 | { |
| 1400 | if (cpu != vcpu->cpu) { |
| 1401 | vcpu->cpu = cpu; |
| 1402 | if (vcpu->arch.ht_active) |
| 1403 | kvm_migrate_hlt_timer(vcpu); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | #define SAVE_REGS(_x) regs->_x = vcpu->arch._x |
| 1408 | |
| 1409 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1410 | { |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1411 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 1412 | int i; |
| 1413 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1414 | vcpu_load(vcpu); |
| 1415 | |
| 1416 | for (i = 0; i < 16; i++) { |
| 1417 | regs->vpd.vgr[i] = vpd->vgr[i]; |
| 1418 | regs->vpd.vbgr[i] = vpd->vbgr[i]; |
| 1419 | } |
| 1420 | for (i = 0; i < 128; i++) |
| 1421 | regs->vpd.vcr[i] = vpd->vcr[i]; |
| 1422 | regs->vpd.vhpi = vpd->vhpi; |
| 1423 | regs->vpd.vnat = vpd->vnat; |
| 1424 | regs->vpd.vbnat = vpd->vbnat; |
| 1425 | regs->vpd.vpsr = vpd->vpsr; |
| 1426 | regs->vpd.vpr = vpd->vpr; |
| 1427 | |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 1428 | memcpy(®s->saved_guest, &vcpu->arch.guest, sizeof(union context)); |
| 1429 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1430 | SAVE_REGS(mp_state); |
| 1431 | SAVE_REGS(vmm_rr); |
| 1432 | memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS); |
| 1433 | memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS); |
| 1434 | SAVE_REGS(itr_regions); |
| 1435 | SAVE_REGS(dtr_regions); |
| 1436 | SAVE_REGS(tc_regions); |
| 1437 | SAVE_REGS(irq_check); |
| 1438 | SAVE_REGS(itc_check); |
| 1439 | SAVE_REGS(timer_check); |
| 1440 | SAVE_REGS(timer_pending); |
| 1441 | SAVE_REGS(last_itc); |
| 1442 | for (i = 0; i < 8; i++) { |
| 1443 | regs->vrr[i] = vcpu->arch.vrr[i]; |
| 1444 | regs->ibr[i] = vcpu->arch.ibr[i]; |
| 1445 | regs->dbr[i] = vcpu->arch.dbr[i]; |
| 1446 | } |
| 1447 | for (i = 0; i < 4; i++) |
| 1448 | regs->insvc[i] = vcpu->arch.insvc[i]; |
Jes Sorensen | c6c9fcd | 2009-02-25 10:38:53 -0600 | [diff] [blame] | 1449 | regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1450 | SAVE_REGS(xtp); |
| 1451 | SAVE_REGS(metaphysical_rr0); |
| 1452 | SAVE_REGS(metaphysical_rr4); |
| 1453 | SAVE_REGS(metaphysical_saved_rr0); |
| 1454 | SAVE_REGS(metaphysical_saved_rr4); |
| 1455 | SAVE_REGS(fp_psr); |
| 1456 | SAVE_REGS(saved_gp); |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 1457 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1458 | vcpu_put(vcpu); |
Jes Sorensen | 042b26e | 2008-12-16 16:45:47 +0100 | [diff] [blame] | 1459 | return 0; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1462 | int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu, |
| 1463 | struct kvm_ia64_vcpu_stack *stack) |
| 1464 | { |
| 1465 | memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack)); |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu, |
| 1470 | struct kvm_ia64_vcpu_stack *stack) |
| 1471 | { |
| 1472 | memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu), |
| 1473 | sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu)); |
| 1474 | |
| 1475 | vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1479 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 1480 | { |
| 1481 | |
| 1482 | hrtimer_cancel(&vcpu->arch.hlt_timer); |
| 1483 | kfree(vcpu->arch.apic); |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | long kvm_arch_vcpu_ioctl(struct file *filp, |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1488 | unsigned int ioctl, unsigned long arg) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1489 | { |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1490 | struct kvm_vcpu *vcpu = filp->private_data; |
| 1491 | void __user *argp = (void __user *)arg; |
| 1492 | struct kvm_ia64_vcpu_stack *stack = NULL; |
| 1493 | long r; |
| 1494 | |
| 1495 | switch (ioctl) { |
| 1496 | case KVM_IA64_VCPU_GET_STACK: { |
| 1497 | struct kvm_ia64_vcpu_stack __user *user_stack; |
| 1498 | void __user *first_p = argp; |
| 1499 | |
| 1500 | r = -EFAULT; |
| 1501 | if (copy_from_user(&user_stack, first_p, sizeof(void *))) |
| 1502 | goto out; |
| 1503 | |
| 1504 | if (!access_ok(VERIFY_WRITE, user_stack, |
| 1505 | sizeof(struct kvm_ia64_vcpu_stack))) { |
| 1506 | printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: " |
| 1507 | "Illegal user destination address for stack\n"); |
| 1508 | goto out; |
| 1509 | } |
| 1510 | stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL); |
| 1511 | if (!stack) { |
| 1512 | r = -ENOMEM; |
| 1513 | goto out; |
| 1514 | } |
| 1515 | |
| 1516 | r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack); |
| 1517 | if (r) |
| 1518 | goto out; |
| 1519 | |
| 1520 | if (copy_to_user(user_stack, stack, |
Wei Yongjun | 5c0d092 | 2010-03-12 08:45:39 +0800 | [diff] [blame] | 1521 | sizeof(struct kvm_ia64_vcpu_stack))) { |
| 1522 | r = -EFAULT; |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1523 | goto out; |
Wei Yongjun | 5c0d092 | 2010-03-12 08:45:39 +0800 | [diff] [blame] | 1524 | } |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1525 | |
| 1526 | break; |
| 1527 | } |
| 1528 | case KVM_IA64_VCPU_SET_STACK: { |
| 1529 | struct kvm_ia64_vcpu_stack __user *user_stack; |
| 1530 | void __user *first_p = argp; |
| 1531 | |
| 1532 | r = -EFAULT; |
| 1533 | if (copy_from_user(&user_stack, first_p, sizeof(void *))) |
| 1534 | goto out; |
| 1535 | |
| 1536 | if (!access_ok(VERIFY_READ, user_stack, |
| 1537 | sizeof(struct kvm_ia64_vcpu_stack))) { |
| 1538 | printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: " |
| 1539 | "Illegal user address for stack\n"); |
| 1540 | goto out; |
| 1541 | } |
| 1542 | stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL); |
| 1543 | if (!stack) { |
| 1544 | r = -ENOMEM; |
| 1545 | goto out; |
| 1546 | } |
| 1547 | if (copy_from_user(stack, user_stack, |
| 1548 | sizeof(struct kvm_ia64_vcpu_stack))) |
| 1549 | goto out; |
| 1550 | |
| 1551 | r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack); |
| 1552 | break; |
| 1553 | } |
| 1554 | |
| 1555 | default: |
| 1556 | r = -EINVAL; |
| 1557 | } |
| 1558 | |
| 1559 | out: |
| 1560 | kfree(stack); |
| 1561 | return r; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Carsten Otte | 5b1c149 | 2012-01-04 10:25:23 +0100 | [diff] [blame] | 1564 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 1565 | { |
| 1566 | return VM_FAULT_SIGBUS; |
| 1567 | } |
| 1568 | |
Takuya Yoshikawa | db3fe4e | 2012-02-08 13:02:18 +0900 | [diff] [blame] | 1569 | void kvm_arch_free_memslot(struct kvm_memory_slot *free, |
| 1570 | struct kvm_memory_slot *dont) |
| 1571 | { |
| 1572 | } |
| 1573 | |
| 1574 | int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages) |
| 1575 | { |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 1579 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 1580 | struct kvm_memory_slot *memslot, |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1581 | struct kvm_memory_slot old, |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 1582 | struct kvm_userspace_memory_region *mem, |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1583 | int user_alloc) |
| 1584 | { |
| 1585 | unsigned long i; |
Xiantao Zhang | 1cbea80 | 2008-10-03 14:58:09 +0800 | [diff] [blame] | 1586 | unsigned long pfn; |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 1587 | int npages = memslot->npages; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1588 | unsigned long base_gfn = memslot->base_gfn; |
| 1589 | |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1590 | if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT)) |
| 1591 | return -ENOMEM; |
| 1592 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1593 | for (i = 0; i < npages; i++) { |
Xiantao Zhang | 1cbea80 | 2008-10-03 14:58:09 +0800 | [diff] [blame] | 1594 | pfn = gfn_to_pfn(kvm, base_gfn + i); |
| 1595 | if (!kvm_is_mmio_pfn(pfn)) { |
| 1596 | kvm_set_pmt_entry(kvm, base_gfn + i, |
| 1597 | pfn << PAGE_SHIFT, |
Xiantao Zhang | b010eb5 | 2008-09-28 01:39:46 -0700 | [diff] [blame] | 1598 | _PAGE_AR_RWX | _PAGE_MA_WB); |
Xiantao Zhang | 1cbea80 | 2008-10-03 14:58:09 +0800 | [diff] [blame] | 1599 | memslot->rmap[i] = (unsigned long)pfn_to_page(pfn); |
| 1600 | } else { |
| 1601 | kvm_set_pmt_entry(kvm, base_gfn + i, |
Xiantao Zhang | b010eb5 | 2008-09-28 01:39:46 -0700 | [diff] [blame] | 1602 | GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT), |
Xiantao Zhang | 1cbea80 | 2008-10-03 14:58:09 +0800 | [diff] [blame] | 1603 | _PAGE_MA_UC); |
| 1604 | memslot->rmap[i] = 0; |
| 1605 | } |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Marcelo Tosatti | f7784b8 | 2009-12-23 14:35:18 -0200 | [diff] [blame] | 1611 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 1612 | struct kvm_userspace_memory_region *mem, |
| 1613 | struct kvm_memory_slot old, |
| 1614 | int user_alloc) |
| 1615 | { |
| 1616 | return; |
| 1617 | } |
| 1618 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 1619 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 1620 | { |
Xiantao Zhang | 64f6afb | 2009-04-16 17:59:16 +0800 | [diff] [blame] | 1621 | kvm_flush_remote_tlbs(kvm); |
Marcelo Tosatti | 34d4cb8 | 2008-07-10 20:49:31 -0300 | [diff] [blame] | 1622 | } |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1623 | |
Marcelo Tosatti | 2df72e9 | 2012-08-24 15:54:57 -0300 | [diff] [blame] | 1624 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 1625 | struct kvm_memory_slot *slot) |
| 1626 | { |
| 1627 | kvm_arch_flush_shadow_all(); |
| 1628 | } |
| 1629 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1630 | long kvm_arch_dev_ioctl(struct file *filp, |
Jes Sorensen | e9a999f | 2008-12-18 12:17:51 +0100 | [diff] [blame] | 1631 | unsigned int ioctl, unsigned long arg) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1632 | { |
| 1633 | return -EINVAL; |
| 1634 | } |
| 1635 | |
| 1636 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 1637 | { |
| 1638 | kvm_vcpu_uninit(vcpu); |
| 1639 | } |
| 1640 | |
| 1641 | static int vti_cpu_has_kvm_support(void) |
| 1642 | { |
| 1643 | long avail = 1, status = 1, control = 1; |
| 1644 | long ret; |
| 1645 | |
| 1646 | ret = ia64_pal_proc_get_features(&avail, &status, &control, 0); |
| 1647 | if (ret) |
| 1648 | goto out; |
| 1649 | |
| 1650 | if (!(avail & PAL_PROC_VM_BIT)) |
| 1651 | goto out; |
| 1652 | |
| 1653 | printk(KERN_DEBUG"kvm: Hardware Supports VT\n"); |
| 1654 | |
| 1655 | ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info); |
| 1656 | if (ret) |
| 1657 | goto out; |
| 1658 | printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size); |
| 1659 | |
| 1660 | if (!(vp_env_info & VP_OPCODE)) { |
| 1661 | printk(KERN_WARNING"kvm: No opcode ability on hardware, " |
| 1662 | "vm_env_info:0x%lx\n", vp_env_info); |
| 1663 | } |
| 1664 | |
| 1665 | return 1; |
| 1666 | out: |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
Jes Sorensen | 0b5d7a2 | 2009-02-25 10:38:55 -0600 | [diff] [blame] | 1670 | |
| 1671 | /* |
| 1672 | * On SN2, the ITC isn't stable, so copy in fast path code to use the |
| 1673 | * SN2 RTC, replacing the ITC based default verion. |
| 1674 | */ |
| 1675 | static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info, |
| 1676 | struct module *module) |
| 1677 | { |
| 1678 | unsigned long new_ar, new_ar_sn2; |
| 1679 | unsigned long module_base; |
| 1680 | |
| 1681 | if (!ia64_platform_is("sn2")) |
| 1682 | return; |
| 1683 | |
| 1684 | module_base = (unsigned long)module->module_core; |
| 1685 | |
| 1686 | new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base; |
| 1687 | new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base; |
| 1688 | |
| 1689 | printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC " |
| 1690 | "as source\n"); |
| 1691 | |
| 1692 | /* |
| 1693 | * Copy the SN2 version of mov_ar into place. They are both |
| 1694 | * the same size, so 6 bundles is sufficient (6 * 0x10). |
| 1695 | */ |
| 1696 | memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60); |
| 1697 | } |
| 1698 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1699 | static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info, |
Jes Sorensen | 0b5d7a2 | 2009-02-25 10:38:55 -0600 | [diff] [blame] | 1700 | struct module *module) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1701 | { |
| 1702 | unsigned long module_base; |
| 1703 | unsigned long vmm_size; |
| 1704 | |
| 1705 | unsigned long vmm_offset, func_offset, fdesc_offset; |
| 1706 | struct fdesc *p_fdesc; |
| 1707 | |
| 1708 | BUG_ON(!module); |
| 1709 | |
| 1710 | if (!kvm_vmm_base) { |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 1711 | printk("kvm: kvm area hasn't been initialized yet!!\n"); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1712 | return -EFAULT; |
| 1713 | } |
| 1714 | |
| 1715 | /*Calculate new position of relocated vmm module.*/ |
| 1716 | module_base = (unsigned long)module->module_core; |
| 1717 | vmm_size = module->core_size; |
| 1718 | if (unlikely(vmm_size > KVM_VMM_SIZE)) |
| 1719 | return -EFAULT; |
| 1720 | |
| 1721 | memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size); |
Jes Sorensen | 0b5d7a2 | 2009-02-25 10:38:55 -0600 | [diff] [blame] | 1722 | kvm_patch_vmm(vmm_info, module); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1723 | kvm_flush_icache(kvm_vmm_base, vmm_size); |
| 1724 | |
| 1725 | /*Recalculate kvm_vmm_info based on new VMM*/ |
| 1726 | vmm_offset = vmm_info->vmm_ivt - module_base; |
| 1727 | kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset; |
| 1728 | printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n", |
| 1729 | kvm_vmm_info->vmm_ivt); |
| 1730 | |
| 1731 | fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base; |
| 1732 | kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE + |
| 1733 | fdesc_offset); |
| 1734 | func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base; |
| 1735 | p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset); |
| 1736 | p_fdesc->ip = KVM_VMM_BASE + func_offset; |
| 1737 | p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base); |
| 1738 | |
| 1739 | printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n", |
| 1740 | KVM_VMM_BASE+func_offset); |
| 1741 | |
| 1742 | fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base; |
| 1743 | kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE + |
| 1744 | fdesc_offset); |
| 1745 | func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base; |
| 1746 | p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset); |
| 1747 | p_fdesc->ip = KVM_VMM_BASE + func_offset; |
| 1748 | p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base); |
| 1749 | |
| 1750 | kvm_vmm_gp = p_fdesc->gp; |
| 1751 | |
| 1752 | printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n", |
| 1753 | kvm_vmm_info->vmm_entry); |
| 1754 | printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n", |
| 1755 | KVM_VMM_BASE + func_offset); |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
| 1760 | int kvm_arch_init(void *opaque) |
| 1761 | { |
| 1762 | int r; |
| 1763 | struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque; |
| 1764 | |
| 1765 | if (!vti_cpu_has_kvm_support()) { |
| 1766 | printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n"); |
| 1767 | r = -EOPNOTSUPP; |
| 1768 | goto out; |
| 1769 | } |
| 1770 | |
| 1771 | if (kvm_vmm_info) { |
| 1772 | printk(KERN_ERR "kvm: Already loaded VMM module!\n"); |
| 1773 | r = -EEXIST; |
| 1774 | goto out; |
| 1775 | } |
| 1776 | |
| 1777 | r = -ENOMEM; |
| 1778 | kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL); |
| 1779 | if (!kvm_vmm_info) |
| 1780 | goto out; |
| 1781 | |
| 1782 | if (kvm_alloc_vmm_area()) |
| 1783 | goto out_free0; |
| 1784 | |
| 1785 | r = kvm_relocate_vmm(vmm_info, vmm_info->module); |
| 1786 | if (r) |
| 1787 | goto out_free1; |
| 1788 | |
| 1789 | return 0; |
| 1790 | |
| 1791 | out_free1: |
| 1792 | kvm_free_vmm_area(); |
| 1793 | out_free0: |
| 1794 | kfree(kvm_vmm_info); |
| 1795 | out: |
| 1796 | return r; |
| 1797 | } |
| 1798 | |
| 1799 | void kvm_arch_exit(void) |
| 1800 | { |
| 1801 | kvm_free_vmm_area(); |
| 1802 | kfree(kvm_vmm_info); |
| 1803 | kvm_vmm_info = NULL; |
| 1804 | } |
| 1805 | |
Takuya Yoshikawa | 979586e | 2010-06-23 15:00:29 +0900 | [diff] [blame] | 1806 | static void kvm_ia64_sync_dirty_log(struct kvm *kvm, |
| 1807 | struct kvm_memory_slot *memslot) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1808 | { |
Takuya Yoshikawa | 979586e | 2010-06-23 15:00:29 +0900 | [diff] [blame] | 1809 | int i; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1810 | long base; |
| 1811 | unsigned long n; |
Xiantao Zhang | a917f7af3 | 2008-10-23 14:56:44 +0800 | [diff] [blame] | 1812 | unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base + |
| 1813 | offsetof(struct kvm_vm_data, kvm_mem_dirty_log)); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1814 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1815 | n = kvm_dirty_bitmap_bytes(memslot); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1816 | base = memslot->base_gfn / BITS_PER_LONG; |
| 1817 | |
Takuya Yoshikawa | 4482b06 | 2010-06-23 14:59:29 +0900 | [diff] [blame] | 1818 | spin_lock(&kvm->arch.dirty_log_lock); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1819 | for (i = 0; i < n/sizeof(long); ++i) { |
| 1820 | memslot->dirty_bitmap[i] = dirty_bitmap[base + i]; |
| 1821 | dirty_bitmap[base + i] = 0; |
| 1822 | } |
Takuya Yoshikawa | 4482b06 | 2010-06-23 14:59:29 +0900 | [diff] [blame] | 1823 | spin_unlock(&kvm->arch.dirty_log_lock); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1827 | struct kvm_dirty_log *log) |
| 1828 | { |
| 1829 | int r; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1830 | unsigned long n; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1831 | struct kvm_memory_slot *memslot; |
| 1832 | int is_dirty = 0; |
| 1833 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1834 | mutex_lock(&kvm->slots_lock); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1835 | |
Takuya Yoshikawa | 979586e | 2010-06-23 15:00:29 +0900 | [diff] [blame] | 1836 | r = -EINVAL; |
| 1837 | if (log->slot >= KVM_MEMORY_SLOTS) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1838 | goto out; |
| 1839 | |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 1840 | memslot = id_to_memslot(kvm->memslots, log->slot); |
Takuya Yoshikawa | 979586e | 2010-06-23 15:00:29 +0900 | [diff] [blame] | 1841 | r = -ENOENT; |
| 1842 | if (!memslot->dirty_bitmap) |
| 1843 | goto out; |
| 1844 | |
| 1845 | kvm_ia64_sync_dirty_log(kvm, memslot); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1846 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1847 | if (r) |
| 1848 | goto out; |
| 1849 | |
| 1850 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1851 | if (is_dirty) { |
| 1852 | kvm_flush_remote_tlbs(kvm); |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1853 | n = kvm_dirty_bitmap_bytes(memslot); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1854 | memset(memslot->dirty_bitmap, 0, n); |
| 1855 | } |
| 1856 | r = 0; |
| 1857 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1858 | mutex_unlock(&kvm->slots_lock); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1859 | return r; |
| 1860 | } |
| 1861 | |
| 1862 | int kvm_arch_hardware_setup(void) |
| 1863 | { |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| 1867 | void kvm_arch_hardware_unsetup(void) |
| 1868 | { |
| 1869 | } |
| 1870 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1871 | int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1872 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1873 | return __apic_accept_irq(vcpu, irq->vector); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest) |
| 1877 | { |
| 1878 | return apic->vcpu->vcpu_id == dest; |
| 1879 | } |
| 1880 | |
| 1881 | int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda) |
| 1882 | { |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1886 | int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2) |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1887 | { |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 1888 | return vcpu1->arch.xtp - vcpu2->arch.xtp; |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1889 | } |
| 1890 | |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 1891 | int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, |
| 1892 | int short_hand, int dest, int dest_mode) |
| 1893 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 1894 | struct kvm_lapic *target = vcpu->arch.apic; |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 1895 | return (dest_mode == 0) ? |
| 1896 | kvm_apic_match_physical_addr(target, dest) : |
| 1897 | kvm_apic_match_logical_addr(target, dest); |
| 1898 | } |
| 1899 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1900 | static int find_highest_bits(int *dat) |
| 1901 | { |
| 1902 | u32 bits, bitnum; |
| 1903 | int i; |
| 1904 | |
| 1905 | /* loop for all 256 bits */ |
| 1906 | for (i = 7; i >= 0 ; i--) { |
| 1907 | bits = dat[i]; |
| 1908 | if (bits) { |
| 1909 | bitnum = fls(bits); |
| 1910 | return i * 32 + bitnum - 1; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | return -1; |
| 1915 | } |
| 1916 | |
| 1917 | int kvm_highest_pending_irq(struct kvm_vcpu *vcpu) |
| 1918 | { |
| 1919 | struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd); |
| 1920 | |
| 1921 | if (vpd->irr[0] & (1UL << NMI_VECTOR)) |
| 1922 | return NMI_VECTOR; |
| 1923 | if (vpd->irr[0] & (1UL << ExtINT_VECTOR)) |
| 1924 | return ExtINT_VECTOR; |
| 1925 | |
| 1926 | return find_highest_bits((int *)&vpd->irr[0]); |
| 1927 | } |
| 1928 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 1929 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 1930 | { |
Xiantao Zhang | decc901 | 2008-10-16 15:58:15 +0800 | [diff] [blame] | 1931 | return vcpu->arch.timer_fired; |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 1932 | } |
| 1933 | |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1934 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 1935 | { |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 1936 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) || |
| 1937 | (kvm_highest_pending_irq(vcpu) != -1); |
Xiantao Zhang | b024b79 | 2008-04-01 15:29:29 +0800 | [diff] [blame] | 1938 | } |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1939 | |
Christoffer Dall | b6d3383 | 2012-03-08 16:44:24 -0500 | [diff] [blame] | 1940 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 1941 | { |
| 1942 | return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests)); |
| 1943 | } |
| 1944 | |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1945 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 1946 | struct kvm_mp_state *mp_state) |
| 1947 | { |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1948 | mp_state->mp_state = vcpu->arch.mp_state; |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
| 1951 | |
| 1952 | static int vcpu_reset(struct kvm_vcpu *vcpu) |
| 1953 | { |
| 1954 | int r; |
| 1955 | long psr; |
| 1956 | local_irq_save(psr); |
| 1957 | r = kvm_insert_vmm_mapping(vcpu); |
Jes Sorensen | 43890ae | 2009-04-17 16:43:27 +0200 | [diff] [blame] | 1958 | local_irq_restore(psr); |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1959 | if (r) |
| 1960 | goto fail; |
| 1961 | |
| 1962 | vcpu->arch.launched = 0; |
| 1963 | kvm_arch_vcpu_uninit(vcpu); |
| 1964 | r = kvm_arch_vcpu_init(vcpu); |
| 1965 | if (r) |
| 1966 | goto fail; |
| 1967 | |
| 1968 | kvm_purge_vmm_mapping(vcpu); |
| 1969 | r = 0; |
| 1970 | fail: |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1971 | return r; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 1975 | struct kvm_mp_state *mp_state) |
| 1976 | { |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1977 | int r = 0; |
| 1978 | |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1979 | vcpu->arch.mp_state = mp_state->mp_state; |
| 1980 | if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) |
| 1981 | r = vcpu_reset(vcpu); |
Xiantao Zhang | 8c4b537 | 2008-08-28 09:34:08 +0800 | [diff] [blame] | 1982 | return r; |
Marcelo Tosatti | 62d9f0d | 2008-04-11 13:24:45 -0300 | [diff] [blame] | 1983 | } |