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