2 * kvm_ia64.c: Basic KVM suppport On Itanium series processors
5 * Copyright (C) 2007, Intel Corporation.
6 * Xiantao Zhang (xiantao.zhang@intel.com)
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.
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
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.
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.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 #include <linux/intel-iommu.h>
36 #include <asm/pgtable.h>
37 #include <asm/gcc_intrin.h>
39 #include <asm/cacheflush.h>
40 #include <asm/div64.h>
51 static unsigned long kvm_vmm_base;
52 static unsigned long kvm_vsa_base;
53 static unsigned long kvm_vm_buffer;
54 static unsigned long kvm_vm_buffer_size;
55 unsigned long kvm_vmm_gp;
57 static long vp_env_info;
59 static struct kvm_vmm_info *kvm_vmm_info;
61 static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
63 struct kvm_stats_debugfs_item debugfs_entries[] = {
67 static void kvm_flush_icache(unsigned long start, unsigned long len)
71 for (l = 0; l < (len + 32); l += 32)
78 static void kvm_flush_tlb_all(void)
80 unsigned long i, j, count0, count1, stride0, stride1, addr;
83 addr = local_cpu_data->ptce_base;
84 count0 = local_cpu_data->ptce_count[0];
85 count1 = local_cpu_data->ptce_count[1];
86 stride0 = local_cpu_data->ptce_stride[0];
87 stride1 = local_cpu_data->ptce_stride[1];
89 local_irq_save(flags);
90 for (i = 0; i < count0; ++i) {
91 for (j = 0; j < count1; ++j) {
97 local_irq_restore(flags);
98 ia64_srlz_i(); /* srlz.i implies srlz.d */
101 long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
103 struct ia64_pal_retval iprv;
105 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
111 static DEFINE_SPINLOCK(vp_lock);
113 void kvm_arch_hardware_enable(void *garbage)
118 unsigned long saved_psr;
121 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
123 local_irq_save(saved_psr);
124 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
125 local_irq_restore(saved_psr);
130 status = ia64_pal_vp_init_env(kvm_vsa_base ?
131 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
132 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
134 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
139 kvm_vsa_base = tmp_base;
140 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
142 spin_unlock(&vp_lock);
143 ia64_ptr_entry(0x3, slot);
146 void kvm_arch_hardware_disable(void *garbage)
152 unsigned long saved_psr;
153 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
155 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
158 local_irq_save(saved_psr);
159 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
160 local_irq_restore(saved_psr);
164 status = ia64_pal_vp_exit_env(host_iva);
166 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
168 ia64_ptr_entry(0x3, slot);
171 void kvm_arch_check_processor_compat(void *rtn)
176 int kvm_dev_ioctl_check_extension(long ext)
182 case KVM_CAP_IRQCHIP:
183 case KVM_CAP_USER_MEMORY:
184 case KVM_CAP_MP_STATE:
188 case KVM_CAP_COALESCED_MMIO:
189 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
192 r = intel_iommu_found();
201 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
202 gpa_t addr, int len, int is_write)
204 struct kvm_io_device *dev;
206 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
211 static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
213 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
214 kvm_run->hw.hardware_exit_reason = 1;
218 static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
220 struct kvm_mmio_req *p;
221 struct kvm_io_device *mmio_dev;
223 p = kvm_get_vcpu_ioreq(vcpu);
225 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
227 vcpu->mmio_needed = 1;
228 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
229 vcpu->mmio_size = kvm_run->mmio.len = p->size;
230 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
232 if (vcpu->mmio_is_write)
233 memcpy(vcpu->mmio_data, &p->data, p->size);
234 memcpy(kvm_run->mmio.data, &p->data, p->size);
235 kvm_run->exit_reason = KVM_EXIT_MMIO;
238 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
241 kvm_iodevice_write(mmio_dev, p->addr, p->size,
244 kvm_iodevice_read(mmio_dev, p->addr, p->size,
248 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
249 p->state = STATE_IORESP_READY;
254 static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
256 struct exit_ctl_data *p;
258 p = kvm_get_exit_data(vcpu);
260 if (p->exit_reason == EXIT_REASON_PAL_CALL)
261 return kvm_pal_emul(vcpu, kvm_run);
263 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
264 kvm_run->hw.hardware_exit_reason = 2;
269 static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
271 struct exit_ctl_data *p;
273 p = kvm_get_exit_data(vcpu);
275 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
279 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
280 kvm_run->hw.hardware_exit_reason = 3;
287 * offset: address offset to IPI space.
288 * value: deliver value.
290 static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
295 kvm_apic_set_irq(vcpu, vector, 0);
298 kvm_apic_set_irq(vcpu, 2, 0);
301 kvm_apic_set_irq(vcpu, 0, 0);
306 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
311 static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
317 for (i = 0; i < KVM_MAX_VCPUS; i++) {
319 lid.val = VCPU_LID(kvm->vcpus[i]);
320 if (lid.id == id && lid.eid == eid)
321 return kvm->vcpus[i];
328 static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
330 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
331 struct kvm_vcpu *target_vcpu;
332 struct kvm_pt_regs *regs;
333 union ia64_ipi_a addr = p->u.ipi_data.addr;
334 union ia64_ipi_d data = p->u.ipi_data.data;
336 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
338 return handle_vm_error(vcpu, kvm_run);
340 if (!target_vcpu->arch.launched) {
341 regs = vcpu_regs(target_vcpu);
343 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
344 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
346 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
347 if (waitqueue_active(&target_vcpu->wq))
348 wake_up_interruptible(&target_vcpu->wq);
350 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
351 if (target_vcpu != vcpu)
352 kvm_vcpu_kick(target_vcpu);
359 struct kvm_ptc_g ptc_g_data;
360 struct kvm_vcpu *vcpu;
363 static void vcpu_global_purge(void *info)
365 struct call_data *p = (struct call_data *)info;
366 struct kvm_vcpu *vcpu = p->vcpu;
368 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
371 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
372 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
373 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
376 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
377 vcpu->arch.ptc_g_count = 0;
378 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
382 static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
384 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
385 struct kvm *kvm = vcpu->kvm;
386 struct call_data call_data;
389 call_data.ptc_g_data = p->u.ptc_g_data;
391 for (i = 0; i < KVM_MAX_VCPUS; i++) {
392 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
393 KVM_MP_STATE_UNINITIALIZED ||
394 vcpu == kvm->vcpus[i])
397 if (waitqueue_active(&kvm->vcpus[i]->wq))
398 wake_up_interruptible(&kvm->vcpus[i]->wq);
400 if (kvm->vcpus[i]->cpu != -1) {
401 call_data.vcpu = kvm->vcpus[i];
402 smp_call_function_single(kvm->vcpus[i]->cpu,
403 vcpu_global_purge, &call_data, 1);
405 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
411 static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
416 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
421 unsigned long vcpu_now_itc;
422 unsigned long expires;
423 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
424 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
425 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
427 if (irqchip_in_kernel(vcpu->kvm)) {
429 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
431 if (time_after(vcpu_now_itc, vpd->itm)) {
432 vcpu->arch.timer_check = 1;
435 itc_diff = vpd->itm - vcpu_now_itc;
437 itc_diff = -itc_diff;
439 expires = div64_u64(itc_diff, cyc_per_usec);
440 kt = ktime_set(0, 1000 * expires);
442 down_read(&vcpu->kvm->slots_lock);
443 vcpu->arch.ht_active = 1;
444 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
446 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
447 kvm_vcpu_block(vcpu);
448 hrtimer_cancel(p_ht);
449 vcpu->arch.ht_active = 0;
451 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
452 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
453 vcpu->arch.mp_state =
454 KVM_MP_STATE_RUNNABLE;
455 up_read(&vcpu->kvm->slots_lock);
457 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
461 printk(KERN_ERR"kvm: Unsupported userspace halt!");
466 static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
467 struct kvm_run *kvm_run)
469 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
473 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
474 struct kvm_run *kvm_run)
479 static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
480 struct kvm_run *kvm_run) = {
481 [EXIT_REASON_VM_PANIC] = handle_vm_error,
482 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
483 [EXIT_REASON_PAL_CALL] = handle_pal_call,
484 [EXIT_REASON_SAL_CALL] = handle_sal_call,
485 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
486 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
487 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
488 [EXIT_REASON_IPI] = handle_ipi,
489 [EXIT_REASON_PTC_G] = handle_global_purge,
493 static const int kvm_vti_max_exit_handlers =
494 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
496 static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
498 struct exit_ctl_data *p_exit_data;
500 p_exit_data = kvm_get_exit_data(vcpu);
501 return p_exit_data->exit_reason;
505 * The guest has exited. See if we can fix it or if we need userspace
508 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
510 u32 exit_reason = kvm_get_exit_reason(vcpu);
511 vcpu->arch.last_exit = exit_reason;
513 if (exit_reason < kvm_vti_max_exit_handlers
514 && kvm_vti_exit_handlers[exit_reason])
515 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
517 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
518 kvm_run->hw.hardware_exit_reason = exit_reason;
523 static inline void vti_set_rr6(unsigned long rr6)
525 ia64_set_rr(RR6, rr6);
529 static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
532 struct kvm *kvm = vcpu->kvm;
535 /*Insert a pair of tr to map vmm*/
536 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
537 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
540 vcpu->arch.vmm_tr_slot = r;
541 /*Insert a pairt of tr to map data of vm*/
542 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
543 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
544 pte, KVM_VM_DATA_SHIFT);
547 vcpu->arch.vm_tr_slot = r;
554 static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
557 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
558 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
562 static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
564 int cpu = smp_processor_id();
566 if (vcpu->arch.last_run_cpu != cpu ||
567 per_cpu(last_vcpu, cpu) != vcpu) {
568 per_cpu(last_vcpu, cpu) = vcpu;
569 vcpu->arch.last_run_cpu = cpu;
573 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
574 vti_set_rr6(vcpu->arch.vmm_rr);
575 return kvm_insert_vmm_mapping(vcpu);
577 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
579 kvm_purge_vmm_mapping(vcpu);
580 vti_set_rr6(vcpu->arch.host_rr6);
583 static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
585 union context *host_ctx, *guest_ctx;
588 /*Get host and guest context with guest address space.*/
589 host_ctx = kvm_get_host_context(vcpu);
590 guest_ctx = kvm_get_guest_context(vcpu);
592 r = kvm_vcpu_pre_transition(vcpu);
595 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
596 kvm_vcpu_post_transition(vcpu);
602 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
610 if (signal_pending(current)) {
614 kvm_run->exit_reason = KVM_EXIT_INTR;
618 vcpu->guest_mode = 1;
620 down_read(&vcpu->kvm->slots_lock);
621 r = vti_vcpu_run(vcpu, kvm_run);
625 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
629 vcpu->arch.launched = 1;
630 vcpu->guest_mode = 0;
634 * We must have an instruction between local_irq_enable() and
635 * kvm_guest_exit(), so the timer interrupt isn't delayed by
636 * the interrupt shadow. The stat.exits increment will do nicely.
637 * But we need to prevent reordering, hence this barrier():
641 up_read(&vcpu->kvm->slots_lock);
644 r = kvm_handle_exit(kvm_run, vcpu);
660 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
662 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
664 if (!vcpu->mmio_is_write)
665 memcpy(&p->data, vcpu->mmio_data, 8);
666 p->state = STATE_IORESP_READY;
669 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
676 if (vcpu->sigset_active)
677 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
679 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
680 kvm_vcpu_block(vcpu);
681 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
686 if (vcpu->mmio_needed) {
687 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
688 kvm_set_mmio_data(vcpu);
689 vcpu->mmio_read_completed = 1;
690 vcpu->mmio_needed = 0;
692 r = __vcpu_run(vcpu, kvm_run);
694 if (vcpu->sigset_active)
695 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
702 * Allocate 16M memory for every vm to hold its specific data.
703 * Its memory map is defined in kvm_host.h.
705 static struct kvm *kvm_alloc_kvm(void)
711 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
714 return ERR_PTR(-ENOMEM);
715 printk(KERN_DEBUG"kvm: VM data's base Address:0x%lx\n", vm_base);
717 /* Zero all pages before use! */
718 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
720 kvm = (struct kvm *)(vm_base + KVM_VM_OFS);
721 kvm->arch.vm_base = vm_base;
726 struct kvm_io_range {
732 static const struct kvm_io_range io_ranges[] = {
733 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
734 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
735 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
736 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
737 {PIB_START, PIB_SIZE, GPFN_PIB},
740 static void kvm_build_io_pmt(struct kvm *kvm)
744 /* Mark I/O ranges */
745 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
747 for (j = io_ranges[i].start;
748 j < io_ranges[i].start + io_ranges[i].size;
750 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
751 io_ranges[i].type, 0);
756 /*Use unused rids to virtualize guest rid.*/
757 #define GUEST_PHYSICAL_RR0 0x1739
758 #define GUEST_PHYSICAL_RR4 0x2739
759 #define VMM_INIT_RR 0x1660
761 static void kvm_init_vm(struct kvm *kvm)
767 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
768 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
769 kvm->arch.vmm_init_rr = VMM_INIT_RR;
771 vm_base = kvm->arch.vm_base;
773 kvm->arch.vhpt_base = vm_base + KVM_VHPT_OFS;
774 kvm->arch.vtlb_base = vm_base + KVM_VTLB_OFS;
775 kvm->arch.vpd_base = vm_base + KVM_VPD_OFS;
779 *Fill P2M entries for MMIO/IO ranges
781 kvm_build_io_pmt(kvm);
783 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
785 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
786 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
789 struct kvm *kvm_arch_create_vm(void)
791 struct kvm *kvm = kvm_alloc_kvm();
794 return ERR_PTR(-ENOMEM);
801 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
802 struct kvm_irqchip *chip)
807 switch (chip->chip_id) {
808 case KVM_IRQCHIP_IOAPIC:
809 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
810 sizeof(struct kvm_ioapic_state));
819 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
824 switch (chip->chip_id) {
825 case KVM_IRQCHIP_IOAPIC:
826 memcpy(ioapic_irqchip(kvm),
828 sizeof(struct kvm_ioapic_state));
837 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
839 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
842 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
847 for (i = 0; i < 16; i++) {
848 vpd->vgr[i] = regs->vpd.vgr[i];
849 vpd->vbgr[i] = regs->vpd.vbgr[i];
851 for (i = 0; i < 128; i++)
852 vpd->vcr[i] = regs->vpd.vcr[i];
853 vpd->vhpi = regs->vpd.vhpi;
854 vpd->vnat = regs->vpd.vnat;
855 vpd->vbnat = regs->vpd.vbnat;
856 vpd->vpsr = regs->vpd.vpsr;
858 vpd->vpr = regs->vpd.vpr;
861 r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
862 sizeof(union context));
865 r = copy_from_user(vcpu + 1, regs->saved_stack +
866 sizeof(struct kvm_vcpu),
867 IA64_STK_OFFSET - sizeof(struct kvm_vcpu));
870 vcpu->arch.exit_data =
871 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
873 RESTORE_REGS(mp_state);
874 RESTORE_REGS(vmm_rr);
875 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
876 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
877 RESTORE_REGS(itr_regions);
878 RESTORE_REGS(dtr_regions);
879 RESTORE_REGS(tc_regions);
880 RESTORE_REGS(irq_check);
881 RESTORE_REGS(itc_check);
882 RESTORE_REGS(timer_check);
883 RESTORE_REGS(timer_pending);
884 RESTORE_REGS(last_itc);
885 for (i = 0; i < 8; i++) {
886 vcpu->arch.vrr[i] = regs->vrr[i];
887 vcpu->arch.ibr[i] = regs->ibr[i];
888 vcpu->arch.dbr[i] = regs->dbr[i];
890 for (i = 0; i < 4; i++)
891 vcpu->arch.insvc[i] = regs->insvc[i];
893 RESTORE_REGS(metaphysical_rr0);
894 RESTORE_REGS(metaphysical_rr4);
895 RESTORE_REGS(metaphysical_saved_rr0);
896 RESTORE_REGS(metaphysical_saved_rr4);
897 RESTORE_REGS(fp_psr);
898 RESTORE_REGS(saved_gp);
900 vcpu->arch.irq_new_pending = 1;
901 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
902 set_bit(KVM_REQ_RESUME, &vcpu->requests);
910 long kvm_arch_vm_ioctl(struct file *filp,
911 unsigned int ioctl, unsigned long arg)
913 struct kvm *kvm = filp->private_data;
914 void __user *argp = (void __user *)arg;
918 case KVM_SET_MEMORY_REGION: {
919 struct kvm_memory_region kvm_mem;
920 struct kvm_userspace_memory_region kvm_userspace_mem;
923 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
925 kvm_userspace_mem.slot = kvm_mem.slot;
926 kvm_userspace_mem.flags = kvm_mem.flags;
927 kvm_userspace_mem.guest_phys_addr =
928 kvm_mem.guest_phys_addr;
929 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
930 r = kvm_vm_ioctl_set_memory_region(kvm,
931 &kvm_userspace_mem, 0);
936 case KVM_CREATE_IRQCHIP:
938 r = kvm_ioapic_init(kvm);
943 struct kvm_irq_level irq_event;
946 if (copy_from_user(&irq_event, argp, sizeof irq_event))
948 if (irqchip_in_kernel(kvm)) {
949 mutex_lock(&kvm->lock);
950 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
951 irq_event.irq, irq_event.level);
952 mutex_unlock(&kvm->lock);
957 case KVM_GET_IRQCHIP: {
958 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
959 struct kvm_irqchip chip;
962 if (copy_from_user(&chip, argp, sizeof chip))
965 if (!irqchip_in_kernel(kvm))
967 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
971 if (copy_to_user(argp, &chip, sizeof chip))
976 case KVM_SET_IRQCHIP: {
977 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
978 struct kvm_irqchip chip;
981 if (copy_from_user(&chip, argp, sizeof chip))
984 if (!irqchip_in_kernel(kvm))
986 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
999 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1000 struct kvm_sregs *sregs)
1005 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1006 struct kvm_sregs *sregs)
1011 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1012 struct kvm_translation *tr)
1018 static int kvm_alloc_vmm_area(void)
1020 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1021 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1022 get_order(KVM_VMM_SIZE));
1026 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1027 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1029 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1030 kvm_vmm_base, kvm_vm_buffer);
1036 static void kvm_free_vmm_area(void)
1039 /*Zero this area before free to avoid bits leak!!*/
1040 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1041 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1048 static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1052 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1055 union cpuid3_t cpuid3;
1056 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1059 return PTR_ERR(vpd);
1062 for (i = 0; i < 5; i++)
1063 vpd->vcpuid[i] = ia64_get_cpuid(i);
1065 /* Limit the CPUID number to 5 */
1066 cpuid3.value = vpd->vcpuid[3];
1067 cpuid3.number = 4; /* 5 - 1 */
1068 vpd->vcpuid[3] = cpuid3.value;
1070 /*Set vac and vdc fields*/
1071 vpd->vac.a_from_int_cr = 1;
1072 vpd->vac.a_to_int_cr = 1;
1073 vpd->vac.a_from_psr = 1;
1074 vpd->vac.a_from_cpuid = 1;
1075 vpd->vac.a_cover = 1;
1078 vpd->vdc.d_vmsw = 1;
1080 /*Set virtual buffer*/
1081 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1086 static int vti_create_vp(struct kvm_vcpu *vcpu)
1089 struct vpd *vpd = vcpu->arch.vpd;
1090 unsigned long vmm_ivt;
1092 vmm_ivt = kvm_vmm_info->vmm_ivt;
1094 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1096 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1099 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1105 static void init_ptce_info(struct kvm_vcpu *vcpu)
1107 ia64_ptce_info_t ptce = {0};
1109 ia64_get_ptce(&ptce);
1110 vcpu->arch.ptce_base = ptce.base;
1111 vcpu->arch.ptce_count[0] = ptce.count[0];
1112 vcpu->arch.ptce_count[1] = ptce.count[1];
1113 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1114 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1117 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1119 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1121 if (hrtimer_cancel(p_ht))
1122 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
1125 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1127 struct kvm_vcpu *vcpu;
1128 wait_queue_head_t *q;
1130 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1133 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1136 if (waitqueue_active(q))
1137 wake_up_interruptible(q);
1140 vcpu->arch.timer_fired = 1;
1141 vcpu->arch.timer_check = 1;
1142 return HRTIMER_NORESTART;
1145 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1147 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1153 struct kvm *kvm = vcpu->kvm;
1154 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1156 union context *p_ctx = &vcpu->arch.guest;
1157 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1159 /*Init vcpu context for first run.*/
1160 if (IS_ERR(vmm_vcpu))
1161 return PTR_ERR(vmm_vcpu);
1163 if (vcpu->vcpu_id == 0) {
1164 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1166 /*Set entry address for first run.*/
1167 regs->cr_iip = PALE_RESET_ENTRY;
1169 /*Initilize itc offset for vcpus*/
1170 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1171 for (i = 0; i < MAX_VCPU_NUM; i++) {
1172 v = (struct kvm_vcpu *)((char *)vcpu + VCPU_SIZE * i);
1173 v->arch.itc_offset = itc_offset;
1174 v->arch.last_itc = 0;
1177 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1180 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1181 if (!vcpu->arch.apic)
1183 vcpu->arch.apic->vcpu = vcpu;
1186 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + IA64_STK_OFFSET);
1187 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1188 p_ctx->psr = 0x1008522000UL;
1189 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1190 p_ctx->caller_unat = 0;
1192 p_ctx->ar[36] = 0x0; /*unat*/
1193 p_ctx->ar[19] = 0x0; /*rnat*/
1194 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1195 ((sizeof(struct kvm_vcpu)+15) & ~15);
1196 p_ctx->ar[64] = 0x0; /*pfs*/
1197 p_ctx->cr[0] = 0x7e04UL;
1198 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1199 p_ctx->cr[8] = 0x3c;
1201 /*Initilize region register*/
1202 p_ctx->rr[0] = 0x30;
1203 p_ctx->rr[1] = 0x30;
1204 p_ctx->rr[2] = 0x30;
1205 p_ctx->rr[3] = 0x30;
1206 p_ctx->rr[4] = 0x30;
1207 p_ctx->rr[5] = 0x30;
1208 p_ctx->rr[7] = 0x30;
1210 /*Initilize branch register 0*/
1211 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1213 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1214 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1215 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1217 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1218 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1220 vcpu->arch.last_run_cpu = -1;
1221 vcpu->arch.vpd = (struct vpd *)VPD_ADDR(vcpu->vcpu_id);
1222 vcpu->arch.vsa_base = kvm_vsa_base;
1223 vcpu->arch.__gp = kvm_vmm_gp;
1224 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1225 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_ADDR(vcpu->vcpu_id);
1226 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_ADDR(vcpu->vcpu_id);
1227 init_ptce_info(vcpu);
1234 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1239 local_irq_save(psr);
1240 r = kvm_insert_vmm_mapping(vcpu);
1243 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1247 r = vti_init_vpd(vcpu);
1249 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1253 r = vti_create_vp(vcpu);
1257 kvm_purge_vmm_mapping(vcpu);
1258 local_irq_restore(psr);
1262 kvm_vcpu_uninit(vcpu);
1264 local_irq_restore(psr);
1268 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1271 struct kvm_vcpu *vcpu;
1272 unsigned long vm_base = kvm->arch.vm_base;
1278 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1281 vcpu = (struct kvm_vcpu *)(vm_base + KVM_VCPU_OFS + VCPU_SIZE * id);
1285 vti_vcpu_load(vcpu, cpu);
1286 r = vti_vcpu_setup(vcpu, id);
1290 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1299 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1304 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1309 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1314 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1315 struct kvm_debug_guest *dbg)
1320 static void free_kvm(struct kvm *kvm)
1322 unsigned long vm_base = kvm->arch.vm_base;
1325 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1326 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1331 static void kvm_release_vm_pages(struct kvm *kvm)
1333 struct kvm_memory_slot *memslot;
1335 unsigned long base_gfn;
1337 for (i = 0; i < kvm->nmemslots; i++) {
1338 memslot = &kvm->memslots[i];
1339 base_gfn = memslot->base_gfn;
1341 for (j = 0; j < memslot->npages; j++) {
1342 if (memslot->rmap[j])
1343 put_page((struct page *)memslot->rmap[j]);
1348 void kvm_arch_destroy_vm(struct kvm *kvm)
1350 kvm_iommu_unmap_guest(kvm);
1351 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1352 kvm_free_all_assigned_devices(kvm);
1354 kfree(kvm->arch.vioapic);
1355 kvm_release_vm_pages(kvm);
1356 kvm_free_physmem(kvm);
1360 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1364 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1366 if (cpu != vcpu->cpu) {
1368 if (vcpu->arch.ht_active)
1369 kvm_migrate_hlt_timer(vcpu);
1373 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1375 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1379 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1382 for (i = 0; i < 16; i++) {
1383 regs->vpd.vgr[i] = vpd->vgr[i];
1384 regs->vpd.vbgr[i] = vpd->vbgr[i];
1386 for (i = 0; i < 128; i++)
1387 regs->vpd.vcr[i] = vpd->vcr[i];
1388 regs->vpd.vhpi = vpd->vhpi;
1389 regs->vpd.vnat = vpd->vnat;
1390 regs->vpd.vbnat = vpd->vbnat;
1391 regs->vpd.vpsr = vpd->vpsr;
1392 regs->vpd.vpr = vpd->vpr;
1395 r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1396 sizeof(union context));
1399 r = copy_to_user(regs->saved_stack, (void *)vcpu, IA64_STK_OFFSET);
1402 SAVE_REGS(mp_state);
1404 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1405 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1406 SAVE_REGS(itr_regions);
1407 SAVE_REGS(dtr_regions);
1408 SAVE_REGS(tc_regions);
1409 SAVE_REGS(irq_check);
1410 SAVE_REGS(itc_check);
1411 SAVE_REGS(timer_check);
1412 SAVE_REGS(timer_pending);
1413 SAVE_REGS(last_itc);
1414 for (i = 0; i < 8; i++) {
1415 regs->vrr[i] = vcpu->arch.vrr[i];
1416 regs->ibr[i] = vcpu->arch.ibr[i];
1417 regs->dbr[i] = vcpu->arch.dbr[i];
1419 for (i = 0; i < 4; i++)
1420 regs->insvc[i] = vcpu->arch.insvc[i];
1421 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1423 SAVE_REGS(metaphysical_rr0);
1424 SAVE_REGS(metaphysical_rr4);
1425 SAVE_REGS(metaphysical_saved_rr0);
1426 SAVE_REGS(metaphysical_saved_rr4);
1428 SAVE_REGS(saved_gp);
1435 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1438 hrtimer_cancel(&vcpu->arch.hlt_timer);
1439 kfree(vcpu->arch.apic);
1443 long kvm_arch_vcpu_ioctl(struct file *filp,
1444 unsigned int ioctl, unsigned long arg)
1449 int kvm_arch_set_memory_region(struct kvm *kvm,
1450 struct kvm_userspace_memory_region *mem,
1451 struct kvm_memory_slot old,
1456 int npages = mem->memory_size >> PAGE_SHIFT;
1457 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1458 unsigned long base_gfn = memslot->base_gfn;
1460 for (i = 0; i < npages; i++) {
1461 pfn = gfn_to_pfn(kvm, base_gfn + i);
1462 if (!kvm_is_mmio_pfn(pfn)) {
1463 kvm_set_pmt_entry(kvm, base_gfn + i,
1465 _PAGE_AR_RWX | _PAGE_MA_WB);
1466 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1468 kvm_set_pmt_entry(kvm, base_gfn + i,
1469 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1471 memslot->rmap[i] = 0;
1478 void kvm_arch_flush_shadow(struct kvm *kvm)
1482 long kvm_arch_dev_ioctl(struct file *filp,
1483 unsigned int ioctl, unsigned long arg)
1488 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1490 kvm_vcpu_uninit(vcpu);
1493 static int vti_cpu_has_kvm_support(void)
1495 long avail = 1, status = 1, control = 1;
1498 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1502 if (!(avail & PAL_PROC_VM_BIT))
1505 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1507 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1510 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1512 if (!(vp_env_info & VP_OPCODE)) {
1513 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1514 "vm_env_info:0x%lx\n", vp_env_info);
1522 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1523 struct module *module)
1525 unsigned long module_base;
1526 unsigned long vmm_size;
1528 unsigned long vmm_offset, func_offset, fdesc_offset;
1529 struct fdesc *p_fdesc;
1533 if (!kvm_vmm_base) {
1534 printk("kvm: kvm area hasn't been initilized yet!!\n");
1538 /*Calculate new position of relocated vmm module.*/
1539 module_base = (unsigned long)module->module_core;
1540 vmm_size = module->core_size;
1541 if (unlikely(vmm_size > KVM_VMM_SIZE))
1544 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1545 kvm_flush_icache(kvm_vmm_base, vmm_size);
1547 /*Recalculate kvm_vmm_info based on new VMM*/
1548 vmm_offset = vmm_info->vmm_ivt - module_base;
1549 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1550 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1551 kvm_vmm_info->vmm_ivt);
1553 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1554 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1556 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1557 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1558 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1559 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1561 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1562 KVM_VMM_BASE+func_offset);
1564 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1565 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1567 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1568 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1569 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1570 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1572 kvm_vmm_gp = p_fdesc->gp;
1574 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1575 kvm_vmm_info->vmm_entry);
1576 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1577 KVM_VMM_BASE + func_offset);
1582 int kvm_arch_init(void *opaque)
1585 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1587 if (!vti_cpu_has_kvm_support()) {
1588 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1594 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1600 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1604 if (kvm_alloc_vmm_area())
1607 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1614 kvm_free_vmm_area();
1616 kfree(kvm_vmm_info);
1621 void kvm_arch_exit(void)
1623 kvm_free_vmm_area();
1624 kfree(kvm_vmm_info);
1625 kvm_vmm_info = NULL;
1628 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1629 struct kvm_dirty_log *log)
1631 struct kvm_memory_slot *memslot;
1634 unsigned long *dirty_bitmap = (unsigned long *)((void *)kvm - KVM_VM_OFS
1635 + KVM_MEM_DIRTY_LOG_OFS);
1638 if (log->slot >= KVM_MEMORY_SLOTS)
1641 memslot = &kvm->memslots[log->slot];
1643 if (!memslot->dirty_bitmap)
1646 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1647 base = memslot->base_gfn / BITS_PER_LONG;
1649 for (i = 0; i < n/sizeof(long); ++i) {
1650 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1651 dirty_bitmap[base + i] = 0;
1658 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1659 struct kvm_dirty_log *log)
1663 struct kvm_memory_slot *memslot;
1666 spin_lock(&kvm->arch.dirty_log_lock);
1668 r = kvm_ia64_sync_dirty_log(kvm, log);
1672 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1676 /* If nothing is dirty, don't bother messing with page tables. */
1678 kvm_flush_remote_tlbs(kvm);
1679 memslot = &kvm->memslots[log->slot];
1680 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1681 memset(memslot->dirty_bitmap, 0, n);
1685 spin_unlock(&kvm->arch.dirty_log_lock);
1689 int kvm_arch_hardware_setup(void)
1694 void kvm_arch_hardware_unsetup(void)
1698 static void vcpu_kick_intr(void *info)
1701 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1702 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1706 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1708 int ipi_pcpu = vcpu->cpu;
1709 int cpu = get_cpu();
1711 if (waitqueue_active(&vcpu->wq))
1712 wake_up_interruptible(&vcpu->wq);
1714 if (vcpu->guest_mode && cpu != ipi_pcpu)
1715 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1719 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1722 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1724 if (!test_and_set_bit(vec, &vpd->irr[0])) {
1725 vcpu->arch.irq_new_pending = 1;
1726 kvm_vcpu_kick(vcpu);
1732 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1734 return apic->vcpu->vcpu_id == dest;
1737 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1742 struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
1743 unsigned long bitmap)
1745 struct kvm_vcpu *lvcpu = kvm->vcpus[0];
1748 for (i = 1; i < KVM_MAX_VCPUS; i++) {
1751 if (lvcpu->arch.xtp > kvm->vcpus[i]->arch.xtp)
1752 lvcpu = kvm->vcpus[i];
1758 static int find_highest_bits(int *dat)
1763 /* loop for all 256 bits */
1764 for (i = 7; i >= 0 ; i--) {
1768 return i * 32 + bitnum - 1;
1775 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1777 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1779 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1781 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1782 return ExtINT_VECTOR;
1784 return find_highest_bits((int *)&vpd->irr[0]);
1787 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1789 if (kvm_highest_pending_irq(vcpu) != -1)
1794 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1796 return vcpu->arch.timer_fired;
1799 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1804 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1806 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1809 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1810 struct kvm_mp_state *mp_state)
1813 mp_state->mp_state = vcpu->arch.mp_state;
1818 static int vcpu_reset(struct kvm_vcpu *vcpu)
1822 local_irq_save(psr);
1823 r = kvm_insert_vmm_mapping(vcpu);
1827 vcpu->arch.launched = 0;
1828 kvm_arch_vcpu_uninit(vcpu);
1829 r = kvm_arch_vcpu_init(vcpu);
1833 kvm_purge_vmm_mapping(vcpu);
1836 local_irq_restore(psr);
1840 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1841 struct kvm_mp_state *mp_state)
1846 vcpu->arch.mp_state = mp_state->mp_state;
1847 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1848 r = vcpu_reset(vcpu);