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