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;
388 call_data.ptc_g_data = p->u.ptc_g_data;
390 for (i = 0; i < KVM_MAX_VCPUS; i++) {
391 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
392 KVM_MP_STATE_UNINITIALIZED ||
393 vcpu == kvm->vcpus[i])
396 if (waitqueue_active(&kvm->vcpus[i]->wq))
397 wake_up_interruptible(&kvm->vcpus[i]->wq);
399 if (kvm->vcpus[i]->cpu != -1) {
400 call_data.vcpu = kvm->vcpus[i];
401 smp_call_function_single(kvm->vcpus[i]->cpu,
402 vcpu_global_purge, &call_data, 1);
404 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
410 static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
415 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
420 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 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
429 if (time_after(vcpu_now_itc, vpd->itm)) {
430 vcpu->arch.timer_check = 1;
433 itc_diff = vpd->itm - vcpu_now_itc;
435 itc_diff = -itc_diff;
437 expires = div64_u64(itc_diff, cyc_per_usec);
438 kt = ktime_set(0, 1000 * expires);
439 vcpu->arch.ht_active = 1;
440 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
442 if (irqchip_in_kernel(vcpu->kvm)) {
443 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
444 kvm_vcpu_block(vcpu);
445 hrtimer_cancel(p_ht);
446 vcpu->arch.ht_active = 0;
448 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
452 printk(KERN_ERR"kvm: Unsupported userspace halt!");
457 static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
458 struct kvm_run *kvm_run)
460 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
464 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
465 struct kvm_run *kvm_run)
470 static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
471 struct kvm_run *kvm_run) = {
472 [EXIT_REASON_VM_PANIC] = handle_vm_error,
473 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
474 [EXIT_REASON_PAL_CALL] = handle_pal_call,
475 [EXIT_REASON_SAL_CALL] = handle_sal_call,
476 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
477 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
478 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
479 [EXIT_REASON_IPI] = handle_ipi,
480 [EXIT_REASON_PTC_G] = handle_global_purge,
484 static const int kvm_vti_max_exit_handlers =
485 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
487 static void kvm_prepare_guest_switch(struct kvm_vcpu *vcpu)
491 static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
493 struct exit_ctl_data *p_exit_data;
495 p_exit_data = kvm_get_exit_data(vcpu);
496 return p_exit_data->exit_reason;
500 * The guest has exited. See if we can fix it or if we need userspace
503 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
505 u32 exit_reason = kvm_get_exit_reason(vcpu);
506 vcpu->arch.last_exit = exit_reason;
508 if (exit_reason < kvm_vti_max_exit_handlers
509 && kvm_vti_exit_handlers[exit_reason])
510 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
512 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
513 kvm_run->hw.hardware_exit_reason = exit_reason;
518 static inline void vti_set_rr6(unsigned long rr6)
520 ia64_set_rr(RR6, rr6);
524 static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
527 struct kvm *kvm = vcpu->kvm;
530 /*Insert a pair of tr to map vmm*/
531 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
532 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
535 vcpu->arch.vmm_tr_slot = r;
536 /*Insert a pairt of tr to map data of vm*/
537 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
538 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
539 pte, KVM_VM_DATA_SHIFT);
542 vcpu->arch.vm_tr_slot = r;
549 static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
552 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
553 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
557 static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
559 int cpu = smp_processor_id();
561 if (vcpu->arch.last_run_cpu != cpu ||
562 per_cpu(last_vcpu, cpu) != vcpu) {
563 per_cpu(last_vcpu, cpu) = vcpu;
564 vcpu->arch.last_run_cpu = cpu;
568 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
569 vti_set_rr6(vcpu->arch.vmm_rr);
570 return kvm_insert_vmm_mapping(vcpu);
572 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
574 kvm_purge_vmm_mapping(vcpu);
575 vti_set_rr6(vcpu->arch.host_rr6);
578 static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
580 union context *host_ctx, *guest_ctx;
583 /*Get host and guest context with guest address space.*/
584 host_ctx = kvm_get_host_context(vcpu);
585 guest_ctx = kvm_get_guest_context(vcpu);
587 r = kvm_vcpu_pre_transition(vcpu);
590 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
591 kvm_vcpu_post_transition(vcpu);
597 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
604 kvm_prepare_guest_switch(vcpu);
607 if (signal_pending(current)) {
611 kvm_run->exit_reason = KVM_EXIT_INTR;
615 vcpu->guest_mode = 1;
618 r = vti_vcpu_run(vcpu, kvm_run);
622 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
626 vcpu->arch.launched = 1;
627 vcpu->guest_mode = 0;
631 * We must have an instruction between local_irq_enable() and
632 * kvm_guest_exit(), so the timer interrupt isn't delayed by
633 * the interrupt shadow. The stat.exits increment will do nicely.
634 * But we need to prevent reordering, hence this barrier():
642 r = kvm_handle_exit(kvm_run, vcpu);
658 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
660 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
662 if (!vcpu->mmio_is_write)
663 memcpy(&p->data, vcpu->mmio_data, 8);
664 p->state = STATE_IORESP_READY;
667 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
674 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
675 kvm_vcpu_block(vcpu);
680 if (vcpu->sigset_active)
681 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
683 if (vcpu->mmio_needed) {
684 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
685 kvm_set_mmio_data(vcpu);
686 vcpu->mmio_read_completed = 1;
687 vcpu->mmio_needed = 0;
689 r = __vcpu_run(vcpu, kvm_run);
691 if (vcpu->sigset_active)
692 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
699 * Allocate 16M memory for every vm to hold its specific data.
700 * Its memory map is defined in kvm_host.h.
702 static struct kvm *kvm_alloc_kvm(void)
708 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
711 return ERR_PTR(-ENOMEM);
712 printk(KERN_DEBUG"kvm: VM data's base Address:0x%lx\n", vm_base);
714 /* Zero all pages before use! */
715 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
717 kvm = (struct kvm *)(vm_base + KVM_VM_OFS);
718 kvm->arch.vm_base = vm_base;
723 struct kvm_io_range {
729 static const struct kvm_io_range io_ranges[] = {
730 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
731 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
732 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
733 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
734 {PIB_START, PIB_SIZE, GPFN_PIB},
737 static void kvm_build_io_pmt(struct kvm *kvm)
741 /* Mark I/O ranges */
742 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
744 for (j = io_ranges[i].start;
745 j < io_ranges[i].start + io_ranges[i].size;
747 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
748 io_ranges[i].type, 0);
753 /*Use unused rids to virtualize guest rid.*/
754 #define GUEST_PHYSICAL_RR0 0x1739
755 #define GUEST_PHYSICAL_RR4 0x2739
756 #define VMM_INIT_RR 0x1660
758 static void kvm_init_vm(struct kvm *kvm)
764 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
765 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
766 kvm->arch.vmm_init_rr = VMM_INIT_RR;
768 vm_base = kvm->arch.vm_base;
770 kvm->arch.vhpt_base = vm_base + KVM_VHPT_OFS;
771 kvm->arch.vtlb_base = vm_base + KVM_VTLB_OFS;
772 kvm->arch.vpd_base = vm_base + KVM_VPD_OFS;
776 *Fill P2M entries for MMIO/IO ranges
778 kvm_build_io_pmt(kvm);
780 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
783 struct kvm *kvm_arch_create_vm(void)
785 struct kvm *kvm = kvm_alloc_kvm();
788 return ERR_PTR(-ENOMEM);
795 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
796 struct kvm_irqchip *chip)
801 switch (chip->chip_id) {
802 case KVM_IRQCHIP_IOAPIC:
803 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
804 sizeof(struct kvm_ioapic_state));
813 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
818 switch (chip->chip_id) {
819 case KVM_IRQCHIP_IOAPIC:
820 memcpy(ioapic_irqchip(kvm),
822 sizeof(struct kvm_ioapic_state));
831 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
833 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
836 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
841 for (i = 0; i < 16; i++) {
842 vpd->vgr[i] = regs->vpd.vgr[i];
843 vpd->vbgr[i] = regs->vpd.vbgr[i];
845 for (i = 0; i < 128; i++)
846 vpd->vcr[i] = regs->vpd.vcr[i];
847 vpd->vhpi = regs->vpd.vhpi;
848 vpd->vnat = regs->vpd.vnat;
849 vpd->vbnat = regs->vpd.vbnat;
850 vpd->vpsr = regs->vpd.vpsr;
852 vpd->vpr = regs->vpd.vpr;
855 r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
856 sizeof(union context));
859 r = copy_from_user(vcpu + 1, regs->saved_stack +
860 sizeof(struct kvm_vcpu),
861 IA64_STK_OFFSET - sizeof(struct kvm_vcpu));
864 vcpu->arch.exit_data =
865 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
867 RESTORE_REGS(mp_state);
868 RESTORE_REGS(vmm_rr);
869 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
870 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
871 RESTORE_REGS(itr_regions);
872 RESTORE_REGS(dtr_regions);
873 RESTORE_REGS(tc_regions);
874 RESTORE_REGS(irq_check);
875 RESTORE_REGS(itc_check);
876 RESTORE_REGS(timer_check);
877 RESTORE_REGS(timer_pending);
878 RESTORE_REGS(last_itc);
879 for (i = 0; i < 8; i++) {
880 vcpu->arch.vrr[i] = regs->vrr[i];
881 vcpu->arch.ibr[i] = regs->ibr[i];
882 vcpu->arch.dbr[i] = regs->dbr[i];
884 for (i = 0; i < 4; i++)
885 vcpu->arch.insvc[i] = regs->insvc[i];
887 RESTORE_REGS(metaphysical_rr0);
888 RESTORE_REGS(metaphysical_rr4);
889 RESTORE_REGS(metaphysical_saved_rr0);
890 RESTORE_REGS(metaphysical_saved_rr4);
891 RESTORE_REGS(fp_psr);
892 RESTORE_REGS(saved_gp);
894 vcpu->arch.irq_new_pending = 1;
895 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
896 set_bit(KVM_REQ_RESUME, &vcpu->requests);
904 long kvm_arch_vm_ioctl(struct file *filp,
905 unsigned int ioctl, unsigned long arg)
907 struct kvm *kvm = filp->private_data;
908 void __user *argp = (void __user *)arg;
912 case KVM_SET_MEMORY_REGION: {
913 struct kvm_memory_region kvm_mem;
914 struct kvm_userspace_memory_region kvm_userspace_mem;
917 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
919 kvm_userspace_mem.slot = kvm_mem.slot;
920 kvm_userspace_mem.flags = kvm_mem.flags;
921 kvm_userspace_mem.guest_phys_addr =
922 kvm_mem.guest_phys_addr;
923 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
924 r = kvm_vm_ioctl_set_memory_region(kvm,
925 &kvm_userspace_mem, 0);
930 case KVM_CREATE_IRQCHIP:
932 r = kvm_ioapic_init(kvm);
937 struct kvm_irq_level irq_event;
940 if (copy_from_user(&irq_event, argp, sizeof irq_event))
942 if (irqchip_in_kernel(kvm)) {
943 mutex_lock(&kvm->lock);
944 kvm_ioapic_set_irq(kvm->arch.vioapic,
947 mutex_unlock(&kvm->lock);
952 case KVM_GET_IRQCHIP: {
953 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
954 struct kvm_irqchip chip;
957 if (copy_from_user(&chip, argp, sizeof chip))
960 if (!irqchip_in_kernel(kvm))
962 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
966 if (copy_to_user(argp, &chip, sizeof chip))
971 case KVM_SET_IRQCHIP: {
972 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
973 struct kvm_irqchip chip;
976 if (copy_from_user(&chip, argp, sizeof chip))
979 if (!irqchip_in_kernel(kvm))
981 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
994 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
995 struct kvm_sregs *sregs)
1000 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1001 struct kvm_sregs *sregs)
1006 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1007 struct kvm_translation *tr)
1013 static int kvm_alloc_vmm_area(void)
1015 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1016 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1017 get_order(KVM_VMM_SIZE));
1021 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1022 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1024 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1025 kvm_vmm_base, kvm_vm_buffer);
1031 static void kvm_free_vmm_area(void)
1034 /*Zero this area before free to avoid bits leak!!*/
1035 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1036 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1043 static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1047 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1050 union cpuid3_t cpuid3;
1051 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1054 return PTR_ERR(vpd);
1057 for (i = 0; i < 5; i++)
1058 vpd->vcpuid[i] = ia64_get_cpuid(i);
1060 /* Limit the CPUID number to 5 */
1061 cpuid3.value = vpd->vcpuid[3];
1062 cpuid3.number = 4; /* 5 - 1 */
1063 vpd->vcpuid[3] = cpuid3.value;
1065 /*Set vac and vdc fields*/
1066 vpd->vac.a_from_int_cr = 1;
1067 vpd->vac.a_to_int_cr = 1;
1068 vpd->vac.a_from_psr = 1;
1069 vpd->vac.a_from_cpuid = 1;
1070 vpd->vac.a_cover = 1;
1073 vpd->vdc.d_vmsw = 1;
1075 /*Set virtual buffer*/
1076 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1081 static int vti_create_vp(struct kvm_vcpu *vcpu)
1084 struct vpd *vpd = vcpu->arch.vpd;
1085 unsigned long vmm_ivt;
1087 vmm_ivt = kvm_vmm_info->vmm_ivt;
1089 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1091 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1094 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1100 static void init_ptce_info(struct kvm_vcpu *vcpu)
1102 ia64_ptce_info_t ptce = {0};
1104 ia64_get_ptce(&ptce);
1105 vcpu->arch.ptce_base = ptce.base;
1106 vcpu->arch.ptce_count[0] = ptce.count[0];
1107 vcpu->arch.ptce_count[1] = ptce.count[1];
1108 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1109 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1112 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1114 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1116 if (hrtimer_cancel(p_ht))
1117 hrtimer_start(p_ht, p_ht->expires, HRTIMER_MODE_ABS);
1120 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1122 struct kvm_vcpu *vcpu;
1123 wait_queue_head_t *q;
1125 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1126 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1130 if (waitqueue_active(q)) {
1131 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1132 wake_up_interruptible(q);
1135 vcpu->arch.timer_check = 1;
1136 return HRTIMER_NORESTART;
1139 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1141 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1147 struct kvm *kvm = vcpu->kvm;
1148 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1150 union context *p_ctx = &vcpu->arch.guest;
1151 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1153 /*Init vcpu context for first run.*/
1154 if (IS_ERR(vmm_vcpu))
1155 return PTR_ERR(vmm_vcpu);
1157 if (vcpu->vcpu_id == 0) {
1158 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1160 /*Set entry address for first run.*/
1161 regs->cr_iip = PALE_RESET_ENTRY;
1163 /*Initilize itc offset for vcpus*/
1164 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1165 for (i = 0; i < MAX_VCPU_NUM; i++) {
1166 v = (struct kvm_vcpu *)((char *)vcpu + VCPU_SIZE * i);
1167 v->arch.itc_offset = itc_offset;
1168 v->arch.last_itc = 0;
1171 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1174 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1175 if (!vcpu->arch.apic)
1177 vcpu->arch.apic->vcpu = vcpu;
1180 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + IA64_STK_OFFSET);
1181 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1182 p_ctx->psr = 0x1008522000UL;
1183 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1184 p_ctx->caller_unat = 0;
1186 p_ctx->ar[36] = 0x0; /*unat*/
1187 p_ctx->ar[19] = 0x0; /*rnat*/
1188 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1189 ((sizeof(struct kvm_vcpu)+15) & ~15);
1190 p_ctx->ar[64] = 0x0; /*pfs*/
1191 p_ctx->cr[0] = 0x7e04UL;
1192 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1193 p_ctx->cr[8] = 0x3c;
1195 /*Initilize region register*/
1196 p_ctx->rr[0] = 0x30;
1197 p_ctx->rr[1] = 0x30;
1198 p_ctx->rr[2] = 0x30;
1199 p_ctx->rr[3] = 0x30;
1200 p_ctx->rr[4] = 0x30;
1201 p_ctx->rr[5] = 0x30;
1202 p_ctx->rr[7] = 0x30;
1204 /*Initilize branch register 0*/
1205 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1207 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1208 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1209 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1211 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1212 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1214 vcpu->arch.last_run_cpu = -1;
1215 vcpu->arch.vpd = (struct vpd *)VPD_ADDR(vcpu->vcpu_id);
1216 vcpu->arch.vsa_base = kvm_vsa_base;
1217 vcpu->arch.__gp = kvm_vmm_gp;
1218 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1219 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_ADDR(vcpu->vcpu_id);
1220 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_ADDR(vcpu->vcpu_id);
1221 init_ptce_info(vcpu);
1228 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1233 local_irq_save(psr);
1234 r = kvm_insert_vmm_mapping(vcpu);
1237 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1241 r = vti_init_vpd(vcpu);
1243 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1247 r = vti_create_vp(vcpu);
1251 kvm_purge_vmm_mapping(vcpu);
1252 local_irq_restore(psr);
1256 kvm_vcpu_uninit(vcpu);
1258 local_irq_restore(psr);
1262 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1265 struct kvm_vcpu *vcpu;
1266 unsigned long vm_base = kvm->arch.vm_base;
1272 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1275 vcpu = (struct kvm_vcpu *)(vm_base + KVM_VCPU_OFS + VCPU_SIZE * id);
1279 vti_vcpu_load(vcpu, cpu);
1280 r = vti_vcpu_setup(vcpu, id);
1284 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1293 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1298 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1303 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1308 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1309 struct kvm_debug_guest *dbg)
1314 static void free_kvm(struct kvm *kvm)
1316 unsigned long vm_base = kvm->arch.vm_base;
1319 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1320 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1325 static void kvm_release_vm_pages(struct kvm *kvm)
1327 struct kvm_memory_slot *memslot;
1329 unsigned long base_gfn;
1331 for (i = 0; i < kvm->nmemslots; i++) {
1332 memslot = &kvm->memslots[i];
1333 base_gfn = memslot->base_gfn;
1335 for (j = 0; j < memslot->npages; j++) {
1336 if (memslot->rmap[j])
1337 put_page((struct page *)memslot->rmap[j]);
1342 void kvm_arch_destroy_vm(struct kvm *kvm)
1344 kvm_iommu_unmap_guest(kvm);
1345 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1346 kvm_free_all_assigned_devices(kvm);
1348 kfree(kvm->arch.vioapic);
1349 kvm_release_vm_pages(kvm);
1350 kvm_free_physmem(kvm);
1354 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1358 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1360 if (cpu != vcpu->cpu) {
1362 if (vcpu->arch.ht_active)
1363 kvm_migrate_hlt_timer(vcpu);
1367 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1369 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1373 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1376 for (i = 0; i < 16; i++) {
1377 regs->vpd.vgr[i] = vpd->vgr[i];
1378 regs->vpd.vbgr[i] = vpd->vbgr[i];
1380 for (i = 0; i < 128; i++)
1381 regs->vpd.vcr[i] = vpd->vcr[i];
1382 regs->vpd.vhpi = vpd->vhpi;
1383 regs->vpd.vnat = vpd->vnat;
1384 regs->vpd.vbnat = vpd->vbnat;
1385 regs->vpd.vpsr = vpd->vpsr;
1386 regs->vpd.vpr = vpd->vpr;
1389 r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1390 sizeof(union context));
1393 r = copy_to_user(regs->saved_stack, (void *)vcpu, IA64_STK_OFFSET);
1396 SAVE_REGS(mp_state);
1398 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1399 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1400 SAVE_REGS(itr_regions);
1401 SAVE_REGS(dtr_regions);
1402 SAVE_REGS(tc_regions);
1403 SAVE_REGS(irq_check);
1404 SAVE_REGS(itc_check);
1405 SAVE_REGS(timer_check);
1406 SAVE_REGS(timer_pending);
1407 SAVE_REGS(last_itc);
1408 for (i = 0; i < 8; i++) {
1409 regs->vrr[i] = vcpu->arch.vrr[i];
1410 regs->ibr[i] = vcpu->arch.ibr[i];
1411 regs->dbr[i] = vcpu->arch.dbr[i];
1413 for (i = 0; i < 4; i++)
1414 regs->insvc[i] = vcpu->arch.insvc[i];
1415 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1417 SAVE_REGS(metaphysical_rr0);
1418 SAVE_REGS(metaphysical_rr4);
1419 SAVE_REGS(metaphysical_saved_rr0);
1420 SAVE_REGS(metaphysical_saved_rr4);
1422 SAVE_REGS(saved_gp);
1429 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1432 hrtimer_cancel(&vcpu->arch.hlt_timer);
1433 kfree(vcpu->arch.apic);
1437 long kvm_arch_vcpu_ioctl(struct file *filp,
1438 unsigned int ioctl, unsigned long arg)
1443 int kvm_arch_set_memory_region(struct kvm *kvm,
1444 struct kvm_userspace_memory_region *mem,
1445 struct kvm_memory_slot old,
1450 int npages = mem->memory_size >> PAGE_SHIFT;
1451 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1452 unsigned long base_gfn = memslot->base_gfn;
1454 for (i = 0; i < npages; i++) {
1455 pfn = gfn_to_pfn(kvm, base_gfn + i);
1456 if (!kvm_is_mmio_pfn(pfn)) {
1457 kvm_set_pmt_entry(kvm, base_gfn + i,
1459 _PAGE_AR_RWX | _PAGE_MA_WB);
1460 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1462 kvm_set_pmt_entry(kvm, base_gfn + i,
1463 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1465 memslot->rmap[i] = 0;
1472 void kvm_arch_flush_shadow(struct kvm *kvm)
1476 long kvm_arch_dev_ioctl(struct file *filp,
1477 unsigned int ioctl, unsigned long arg)
1482 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1484 kvm_vcpu_uninit(vcpu);
1487 static int vti_cpu_has_kvm_support(void)
1489 long avail = 1, status = 1, control = 1;
1492 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1496 if (!(avail & PAL_PROC_VM_BIT))
1499 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1501 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1504 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1506 if (!(vp_env_info & VP_OPCODE)) {
1507 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1508 "vm_env_info:0x%lx\n", vp_env_info);
1516 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1517 struct module *module)
1519 unsigned long module_base;
1520 unsigned long vmm_size;
1522 unsigned long vmm_offset, func_offset, fdesc_offset;
1523 struct fdesc *p_fdesc;
1527 if (!kvm_vmm_base) {
1528 printk("kvm: kvm area hasn't been initilized yet!!\n");
1532 /*Calculate new position of relocated vmm module.*/
1533 module_base = (unsigned long)module->module_core;
1534 vmm_size = module->core_size;
1535 if (unlikely(vmm_size > KVM_VMM_SIZE))
1538 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1539 kvm_flush_icache(kvm_vmm_base, vmm_size);
1541 /*Recalculate kvm_vmm_info based on new VMM*/
1542 vmm_offset = vmm_info->vmm_ivt - module_base;
1543 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1544 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1545 kvm_vmm_info->vmm_ivt);
1547 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1548 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1550 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1551 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1552 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1553 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1555 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1556 KVM_VMM_BASE+func_offset);
1558 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1559 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1561 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1562 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1563 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1564 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1566 kvm_vmm_gp = p_fdesc->gp;
1568 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1569 kvm_vmm_info->vmm_entry);
1570 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1571 KVM_VMM_BASE + func_offset);
1576 int kvm_arch_init(void *opaque)
1579 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1581 if (!vti_cpu_has_kvm_support()) {
1582 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1588 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1594 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1598 if (kvm_alloc_vmm_area())
1601 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1608 kvm_free_vmm_area();
1610 kfree(kvm_vmm_info);
1615 void kvm_arch_exit(void)
1617 kvm_free_vmm_area();
1618 kfree(kvm_vmm_info);
1619 kvm_vmm_info = NULL;
1622 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1623 struct kvm_dirty_log *log)
1625 struct kvm_memory_slot *memslot;
1628 unsigned long *dirty_bitmap = (unsigned long *)((void *)kvm - KVM_VM_OFS
1629 + KVM_MEM_DIRTY_LOG_OFS);
1632 if (log->slot >= KVM_MEMORY_SLOTS)
1635 memslot = &kvm->memslots[log->slot];
1637 if (!memslot->dirty_bitmap)
1640 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1641 base = memslot->base_gfn / BITS_PER_LONG;
1643 for (i = 0; i < n/sizeof(long); ++i) {
1644 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1645 dirty_bitmap[base + i] = 0;
1652 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1653 struct kvm_dirty_log *log)
1657 struct kvm_memory_slot *memslot;
1660 spin_lock(&kvm->arch.dirty_log_lock);
1662 r = kvm_ia64_sync_dirty_log(kvm, log);
1666 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1670 /* If nothing is dirty, don't bother messing with page tables. */
1672 kvm_flush_remote_tlbs(kvm);
1673 memslot = &kvm->memslots[log->slot];
1674 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1675 memset(memslot->dirty_bitmap, 0, n);
1679 spin_unlock(&kvm->arch.dirty_log_lock);
1683 int kvm_arch_hardware_setup(void)
1688 void kvm_arch_hardware_unsetup(void)
1692 static void vcpu_kick_intr(void *info)
1695 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1696 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1700 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1702 int ipi_pcpu = vcpu->cpu;
1704 if (waitqueue_active(&vcpu->wq))
1705 wake_up_interruptible(&vcpu->wq);
1707 if (vcpu->guest_mode)
1708 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1711 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1714 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1716 if (!test_and_set_bit(vec, &vpd->irr[0])) {
1717 vcpu->arch.irq_new_pending = 1;
1718 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
1719 kvm_vcpu_kick(vcpu);
1720 else if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) {
1721 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1722 if (waitqueue_active(&vcpu->wq))
1723 wake_up_interruptible(&vcpu->wq);
1730 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1732 return apic->vcpu->vcpu_id == dest;
1735 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1740 struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
1741 unsigned long bitmap)
1743 struct kvm_vcpu *lvcpu = kvm->vcpus[0];
1746 for (i = 1; i < KVM_MAX_VCPUS; i++) {
1749 if (lvcpu->arch.xtp > kvm->vcpus[i]->arch.xtp)
1750 lvcpu = kvm->vcpus[i];
1756 static int find_highest_bits(int *dat)
1761 /* loop for all 256 bits */
1762 for (i = 7; i >= 0 ; i--) {
1766 return i * 32 + bitnum - 1;
1773 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1775 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1777 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1779 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1780 return ExtINT_VECTOR;
1782 return find_highest_bits((int *)&vpd->irr[0]);
1785 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1787 if (kvm_highest_pending_irq(vcpu) != -1)
1792 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1797 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1802 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1804 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1807 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1808 struct kvm_mp_state *mp_state)
1811 mp_state->mp_state = vcpu->arch.mp_state;
1816 static int vcpu_reset(struct kvm_vcpu *vcpu)
1820 local_irq_save(psr);
1821 r = kvm_insert_vmm_mapping(vcpu);
1825 vcpu->arch.launched = 0;
1826 kvm_arch_vcpu_uninit(vcpu);
1827 r = kvm_arch_vcpu_init(vcpu);
1831 kvm_purge_vmm_mapping(vcpu);
1834 local_irq_restore(psr);
1838 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1839 struct kvm_mp_state *mp_state)
1844 vcpu->arch.mp_state = mp_state->mp_state;
1845 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1846 r = vcpu_reset(vcpu);