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