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