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>
27 #include <linux/slab.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/iommu.h>
35 #include <linux/intel-iommu.h>
36 #include <linux/pci.h>
38 #include <asm/pgtable.h>
39 #include <asm/gcc_intrin.h>
41 #include <asm/cacheflush.h>
42 #include <asm/div64.h>
45 #include <asm/sn/addrs.h>
46 #include <asm/sn/clksupport.h>
47 #include <asm/sn/shub_mmr.h>
56 static unsigned long kvm_vmm_base
;
57 static unsigned long kvm_vsa_base
;
58 static unsigned long kvm_vm_buffer
;
59 static unsigned long kvm_vm_buffer_size
;
60 unsigned long kvm_vmm_gp
;
62 static long vp_env_info
;
64 static struct kvm_vmm_info
*kvm_vmm_info
;
66 static DEFINE_PER_CPU(struct kvm_vcpu
*, last_vcpu
);
68 struct kvm_stats_debugfs_item debugfs_entries
[] = {
72 static unsigned long kvm_get_itc(struct kvm_vcpu
*vcpu
)
74 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
75 if (vcpu
->kvm
->arch
.is_sn2
)
79 return ia64_getreg(_IA64_REG_AR_ITC
);
82 static void kvm_flush_icache(unsigned long start
, unsigned long len
)
86 for (l
= 0; l
< (len
+ 32); l
+= 32)
87 ia64_fc((void *)(start
+ l
));
93 static void kvm_flush_tlb_all(void)
95 unsigned long i
, j
, count0
, count1
, stride0
, stride1
, addr
;
98 addr
= local_cpu_data
->ptce_base
;
99 count0
= local_cpu_data
->ptce_count
[0];
100 count1
= local_cpu_data
->ptce_count
[1];
101 stride0
= local_cpu_data
->ptce_stride
[0];
102 stride1
= local_cpu_data
->ptce_stride
[1];
104 local_irq_save(flags
);
105 for (i
= 0; i
< count0
; ++i
) {
106 for (j
= 0; j
< count1
; ++j
) {
112 local_irq_restore(flags
);
113 ia64_srlz_i(); /* srlz.i implies srlz.d */
116 long ia64_pal_vp_create(u64
*vpd
, u64
*host_iva
, u64
*opt_handler
)
118 struct ia64_pal_retval iprv
;
120 PAL_CALL_STK(iprv
, PAL_VP_CREATE
, (u64
)vpd
, (u64
)host_iva
,
126 static DEFINE_SPINLOCK(vp_lock
);
128 int kvm_arch_hardware_enable(void *garbage
)
133 unsigned long saved_psr
;
136 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
), PAGE_KERNEL
));
137 local_irq_save(saved_psr
);
138 slot
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
139 local_irq_restore(saved_psr
);
144 status
= ia64_pal_vp_init_env(kvm_vsa_base
?
145 VP_INIT_ENV
: VP_INIT_ENV_INITALIZE
,
146 __pa(kvm_vm_buffer
), KVM_VM_BUFFER_BASE
, &tmp_base
);
148 spin_unlock(&vp_lock
);
149 printk(KERN_WARNING
"kvm: Failed to Enable VT Support!!!!\n");
154 kvm_vsa_base
= tmp_base
;
155 printk(KERN_INFO
"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base
);
157 spin_unlock(&vp_lock
);
158 ia64_ptr_entry(0x3, slot
);
163 void kvm_arch_hardware_disable(void *garbage
)
169 unsigned long saved_psr
;
170 unsigned long host_iva
= ia64_getreg(_IA64_REG_CR_IVA
);
172 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
),
175 local_irq_save(saved_psr
);
176 slot
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
177 local_irq_restore(saved_psr
);
181 status
= ia64_pal_vp_exit_env(host_iva
);
183 printk(KERN_DEBUG
"kvm: Failed to disable VT support! :%ld\n",
185 ia64_ptr_entry(0x3, slot
);
188 void kvm_arch_check_processor_compat(void *rtn
)
193 int kvm_dev_ioctl_check_extension(long ext
)
199 case KVM_CAP_IRQCHIP
:
200 case KVM_CAP_MP_STATE
:
201 case KVM_CAP_IRQ_INJECT_STATUS
:
204 case KVM_CAP_COALESCED_MMIO
:
205 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
208 r
= iommu_present(&pci_bus_type
);
217 static int handle_vm_error(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
219 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
220 kvm_run
->hw
.hardware_exit_reason
= 1;
224 static int handle_mmio(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
226 struct kvm_mmio_req
*p
;
227 struct kvm_io_device
*mmio_dev
;
230 p
= kvm_get_vcpu_ioreq(vcpu
);
232 if ((p
->addr
& PAGE_MASK
) == IOAPIC_DEFAULT_BASE_ADDRESS
)
234 vcpu
->mmio_needed
= 1;
235 vcpu
->mmio_phys_addr
= kvm_run
->mmio
.phys_addr
= p
->addr
;
236 vcpu
->mmio_size
= kvm_run
->mmio
.len
= p
->size
;
237 vcpu
->mmio_is_write
= kvm_run
->mmio
.is_write
= !p
->dir
;
239 if (vcpu
->mmio_is_write
)
240 memcpy(vcpu
->mmio_data
, &p
->data
, p
->size
);
241 memcpy(kvm_run
->mmio
.data
, &p
->data
, p
->size
);
242 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
246 r
= kvm_io_bus_read(vcpu
->kvm
, KVM_MMIO_BUS
, p
->addr
,
249 r
= kvm_io_bus_write(vcpu
->kvm
, KVM_MMIO_BUS
, p
->addr
,
252 printk(KERN_ERR
"kvm: No iodevice found! addr:%lx\n", p
->addr
);
253 p
->state
= STATE_IORESP_READY
;
258 static int handle_pal_call(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
260 struct exit_ctl_data
*p
;
262 p
= kvm_get_exit_data(vcpu
);
264 if (p
->exit_reason
== EXIT_REASON_PAL_CALL
)
265 return kvm_pal_emul(vcpu
, kvm_run
);
267 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
268 kvm_run
->hw
.hardware_exit_reason
= 2;
273 static int handle_sal_call(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
275 struct exit_ctl_data
*p
;
277 p
= kvm_get_exit_data(vcpu
);
279 if (p
->exit_reason
== EXIT_REASON_SAL_CALL
) {
283 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
284 kvm_run
->hw
.hardware_exit_reason
= 3;
290 static int __apic_accept_irq(struct kvm_vcpu
*vcpu
, uint64_t vector
)
292 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
294 if (!test_and_set_bit(vector
, &vpd
->irr
[0])) {
295 vcpu
->arch
.irq_new_pending
= 1;
303 * offset: address offset to IPI space.
304 * value: deliver value.
306 static void vcpu_deliver_ipi(struct kvm_vcpu
*vcpu
, uint64_t dm
,
321 printk(KERN_ERR
"kvm: Unimplemented Deliver reserved IPI!\n");
324 __apic_accept_irq(vcpu
, vector
);
327 static struct kvm_vcpu
*lid_to_vcpu(struct kvm
*kvm
, unsigned long id
,
332 struct kvm_vcpu
*vcpu
;
334 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
335 lid
.val
= VCPU_LID(vcpu
);
336 if (lid
.id
== id
&& lid
.eid
== eid
)
343 static int handle_ipi(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
345 struct exit_ctl_data
*p
= kvm_get_exit_data(vcpu
);
346 struct kvm_vcpu
*target_vcpu
;
347 struct kvm_pt_regs
*regs
;
348 union ia64_ipi_a addr
= p
->u
.ipi_data
.addr
;
349 union ia64_ipi_d data
= p
->u
.ipi_data
.data
;
351 target_vcpu
= lid_to_vcpu(vcpu
->kvm
, addr
.id
, addr
.eid
);
353 return handle_vm_error(vcpu
, kvm_run
);
355 if (!target_vcpu
->arch
.launched
) {
356 regs
= vcpu_regs(target_vcpu
);
358 regs
->cr_iip
= vcpu
->kvm
->arch
.rdv_sal_data
.boot_ip
;
359 regs
->r1
= vcpu
->kvm
->arch
.rdv_sal_data
.boot_gp
;
361 target_vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
362 if (waitqueue_active(&target_vcpu
->wq
))
363 wake_up_interruptible(&target_vcpu
->wq
);
365 vcpu_deliver_ipi(target_vcpu
, data
.dm
, data
.vector
);
366 if (target_vcpu
!= vcpu
)
367 kvm_vcpu_kick(target_vcpu
);
374 struct kvm_ptc_g ptc_g_data
;
375 struct kvm_vcpu
*vcpu
;
378 static void vcpu_global_purge(void *info
)
380 struct call_data
*p
= (struct call_data
*)info
;
381 struct kvm_vcpu
*vcpu
= p
->vcpu
;
383 if (test_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
386 set_bit(KVM_REQ_PTC_G
, &vcpu
->requests
);
387 if (vcpu
->arch
.ptc_g_count
< MAX_PTC_G_NUM
) {
388 vcpu
->arch
.ptc_g_data
[vcpu
->arch
.ptc_g_count
++] =
391 clear_bit(KVM_REQ_PTC_G
, &vcpu
->requests
);
392 vcpu
->arch
.ptc_g_count
= 0;
393 set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
);
397 static int handle_global_purge(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
399 struct exit_ctl_data
*p
= kvm_get_exit_data(vcpu
);
400 struct kvm
*kvm
= vcpu
->kvm
;
401 struct call_data call_data
;
403 struct kvm_vcpu
*vcpui
;
405 call_data
.ptc_g_data
= p
->u
.ptc_g_data
;
407 kvm_for_each_vcpu(i
, vcpui
, kvm
) {
408 if (vcpui
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
||
412 if (waitqueue_active(&vcpui
->wq
))
413 wake_up_interruptible(&vcpui
->wq
);
415 if (vcpui
->cpu
!= -1) {
416 call_data
.vcpu
= vcpui
;
417 smp_call_function_single(vcpui
->cpu
,
418 vcpu_global_purge
, &call_data
, 1);
420 printk(KERN_WARNING
"kvm: Uninit vcpu received ipi!\n");
426 static int handle_switch_rr6(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
431 static int kvm_sn2_setup_mappings(struct kvm_vcpu
*vcpu
)
433 unsigned long pte
, rtc_phys_addr
, map_addr
;
436 map_addr
= KVM_VMM_BASE
+ (1UL << KVM_VMM_SHIFT
);
437 rtc_phys_addr
= LOCAL_MMR_OFFSET
| SH_RTC
;
438 pte
= pte_val(mk_pte_phys(rtc_phys_addr
, PAGE_KERNEL_UC
));
439 slot
= ia64_itr_entry(0x3, map_addr
, pte
, PAGE_SHIFT
);
440 vcpu
->arch
.sn_rtc_tr_slot
= slot
;
442 printk(KERN_ERR
"Mayday mayday! RTC mapping failed!\n");
448 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
453 unsigned long vcpu_now_itc
;
454 unsigned long expires
;
455 struct hrtimer
*p_ht
= &vcpu
->arch
.hlt_timer
;
456 unsigned long cyc_per_usec
= local_cpu_data
->cyc_per_usec
;
457 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
459 if (irqchip_in_kernel(vcpu
->kvm
)) {
461 vcpu_now_itc
= kvm_get_itc(vcpu
) + vcpu
->arch
.itc_offset
;
463 if (time_after(vcpu_now_itc
, vpd
->itm
)) {
464 vcpu
->arch
.timer_check
= 1;
467 itc_diff
= vpd
->itm
- vcpu_now_itc
;
469 itc_diff
= -itc_diff
;
471 expires
= div64_u64(itc_diff
, cyc_per_usec
);
472 kt
= ktime_set(0, 1000 * expires
);
474 vcpu
->arch
.ht_active
= 1;
475 hrtimer_start(p_ht
, kt
, HRTIMER_MODE_ABS
);
477 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
478 kvm_vcpu_block(vcpu
);
479 hrtimer_cancel(p_ht
);
480 vcpu
->arch
.ht_active
= 0;
482 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
) ||
483 kvm_cpu_has_pending_timer(vcpu
))
484 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_HALTED
)
485 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
487 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_RUNNABLE
)
491 printk(KERN_ERR
"kvm: Unsupported userspace halt!");
496 static int handle_vm_shutdown(struct kvm_vcpu
*vcpu
,
497 struct kvm_run
*kvm_run
)
499 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
503 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
504 struct kvm_run
*kvm_run
)
509 static int handle_vcpu_debug(struct kvm_vcpu
*vcpu
,
510 struct kvm_run
*kvm_run
)
512 printk("VMM: %s", vcpu
->arch
.log_buf
);
516 static int (*kvm_vti_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
517 struct kvm_run
*kvm_run
) = {
518 [EXIT_REASON_VM_PANIC
] = handle_vm_error
,
519 [EXIT_REASON_MMIO_INSTRUCTION
] = handle_mmio
,
520 [EXIT_REASON_PAL_CALL
] = handle_pal_call
,
521 [EXIT_REASON_SAL_CALL
] = handle_sal_call
,
522 [EXIT_REASON_SWITCH_RR6
] = handle_switch_rr6
,
523 [EXIT_REASON_VM_DESTROY
] = handle_vm_shutdown
,
524 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
525 [EXIT_REASON_IPI
] = handle_ipi
,
526 [EXIT_REASON_PTC_G
] = handle_global_purge
,
527 [EXIT_REASON_DEBUG
] = handle_vcpu_debug
,
531 static const int kvm_vti_max_exit_handlers
=
532 sizeof(kvm_vti_exit_handlers
)/sizeof(*kvm_vti_exit_handlers
);
534 static uint32_t kvm_get_exit_reason(struct kvm_vcpu
*vcpu
)
536 struct exit_ctl_data
*p_exit_data
;
538 p_exit_data
= kvm_get_exit_data(vcpu
);
539 return p_exit_data
->exit_reason
;
543 * The guest has exited. See if we can fix it or if we need userspace
546 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
548 u32 exit_reason
= kvm_get_exit_reason(vcpu
);
549 vcpu
->arch
.last_exit
= exit_reason
;
551 if (exit_reason
< kvm_vti_max_exit_handlers
552 && kvm_vti_exit_handlers
[exit_reason
])
553 return kvm_vti_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
555 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
556 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
561 static inline void vti_set_rr6(unsigned long rr6
)
563 ia64_set_rr(RR6
, rr6
);
567 static int kvm_insert_vmm_mapping(struct kvm_vcpu
*vcpu
)
570 struct kvm
*kvm
= vcpu
->kvm
;
573 /*Insert a pair of tr to map vmm*/
574 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
), PAGE_KERNEL
));
575 r
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
578 vcpu
->arch
.vmm_tr_slot
= r
;
579 /*Insert a pairt of tr to map data of vm*/
580 pte
= pte_val(mk_pte_phys(__pa(kvm
->arch
.vm_base
), PAGE_KERNEL
));
581 r
= ia64_itr_entry(0x3, KVM_VM_DATA_BASE
,
582 pte
, KVM_VM_DATA_SHIFT
);
585 vcpu
->arch
.vm_tr_slot
= r
;
587 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
588 if (kvm
->arch
.is_sn2
) {
589 r
= kvm_sn2_setup_mappings(vcpu
);
600 static void kvm_purge_vmm_mapping(struct kvm_vcpu
*vcpu
)
602 struct kvm
*kvm
= vcpu
->kvm
;
603 ia64_ptr_entry(0x3, vcpu
->arch
.vmm_tr_slot
);
604 ia64_ptr_entry(0x3, vcpu
->arch
.vm_tr_slot
);
605 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
606 if (kvm
->arch
.is_sn2
)
607 ia64_ptr_entry(0x3, vcpu
->arch
.sn_rtc_tr_slot
);
611 static int kvm_vcpu_pre_transition(struct kvm_vcpu
*vcpu
)
615 int cpu
= smp_processor_id();
617 if (vcpu
->arch
.last_run_cpu
!= cpu
||
618 per_cpu(last_vcpu
, cpu
) != vcpu
) {
619 per_cpu(last_vcpu
, cpu
) = vcpu
;
620 vcpu
->arch
.last_run_cpu
= cpu
;
624 vcpu
->arch
.host_rr6
= ia64_get_rr(RR6
);
625 vti_set_rr6(vcpu
->arch
.vmm_rr
);
627 r
= kvm_insert_vmm_mapping(vcpu
);
628 local_irq_restore(psr
);
632 static void kvm_vcpu_post_transition(struct kvm_vcpu
*vcpu
)
634 kvm_purge_vmm_mapping(vcpu
);
635 vti_set_rr6(vcpu
->arch
.host_rr6
);
638 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
640 union context
*host_ctx
, *guest_ctx
;
643 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
646 if (signal_pending(current
)) {
648 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
655 /*Get host and guest context with guest address space.*/
656 host_ctx
= kvm_get_host_context(vcpu
);
657 guest_ctx
= kvm_get_guest_context(vcpu
);
659 clear_bit(KVM_REQ_KICK
, &vcpu
->requests
);
661 r
= kvm_vcpu_pre_transition(vcpu
);
665 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
666 vcpu
->mode
= IN_GUEST_MODE
;
670 * Transition to the guest
672 kvm_vmm_info
->tramp_entry(host_ctx
, guest_ctx
);
674 kvm_vcpu_post_transition(vcpu
);
676 vcpu
->arch
.launched
= 1;
677 set_bit(KVM_REQ_KICK
, &vcpu
->requests
);
681 * We must have an instruction between local_irq_enable() and
682 * kvm_guest_exit(), so the timer interrupt isn't delayed by
683 * the interrupt shadow. The stat.exits increment will do nicely.
684 * But we need to prevent reordering, hence this barrier():
688 vcpu
->mode
= OUTSIDE_GUEST_MODE
;
691 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
693 r
= kvm_handle_exit(kvm_run
, vcpu
);
701 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
704 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
713 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
717 static void kvm_set_mmio_data(struct kvm_vcpu
*vcpu
)
719 struct kvm_mmio_req
*p
= kvm_get_vcpu_ioreq(vcpu
);
721 if (!vcpu
->mmio_is_write
)
722 memcpy(&p
->data
, vcpu
->mmio_data
, 8);
723 p
->state
= STATE_IORESP_READY
;
726 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
731 if (vcpu
->sigset_active
)
732 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
734 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
735 kvm_vcpu_block(vcpu
);
736 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
741 if (vcpu
->mmio_needed
) {
742 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
743 kvm_set_mmio_data(vcpu
);
744 vcpu
->mmio_read_completed
= 1;
745 vcpu
->mmio_needed
= 0;
747 r
= __vcpu_run(vcpu
, kvm_run
);
749 if (vcpu
->sigset_active
)
750 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
755 struct kvm
*kvm_arch_alloc_vm(void)
761 BUG_ON(sizeof(struct kvm
) > KVM_VM_STRUCT_SIZE
);
763 vm_base
= __get_free_pages(GFP_KERNEL
, get_order(KVM_VM_DATA_SIZE
));
768 memset((void *)vm_base
, 0, KVM_VM_DATA_SIZE
);
769 kvm
= (struct kvm
*)(vm_base
+
770 offsetof(struct kvm_vm_data
, kvm_vm_struct
));
771 kvm
->arch
.vm_base
= vm_base
;
772 printk(KERN_DEBUG
"kvm: vm's data area:0x%lx\n", vm_base
);
777 struct kvm_io_range
{
783 static const struct kvm_io_range io_ranges
[] = {
784 {VGA_IO_START
, VGA_IO_SIZE
, GPFN_FRAME_BUFFER
},
785 {MMIO_START
, MMIO_SIZE
, GPFN_LOW_MMIO
},
786 {LEGACY_IO_START
, LEGACY_IO_SIZE
, GPFN_LEGACY_IO
},
787 {IO_SAPIC_START
, IO_SAPIC_SIZE
, GPFN_IOSAPIC
},
788 {PIB_START
, PIB_SIZE
, GPFN_PIB
},
791 static void kvm_build_io_pmt(struct kvm
*kvm
)
795 /* Mark I/O ranges */
796 for (i
= 0; i
< (sizeof(io_ranges
) / sizeof(struct kvm_io_range
));
798 for (j
= io_ranges
[i
].start
;
799 j
< io_ranges
[i
].start
+ io_ranges
[i
].size
;
801 kvm_set_pmt_entry(kvm
, j
>> PAGE_SHIFT
,
802 io_ranges
[i
].type
, 0);
807 /*Use unused rids to virtualize guest rid.*/
808 #define GUEST_PHYSICAL_RR0 0x1739
809 #define GUEST_PHYSICAL_RR4 0x2739
810 #define VMM_INIT_RR 0x1660
812 int kvm_arch_init_vm(struct kvm
*kvm
)
816 kvm
->arch
.is_sn2
= ia64_platform_is("sn2");
818 kvm
->arch
.metaphysical_rr0
= GUEST_PHYSICAL_RR0
;
819 kvm
->arch
.metaphysical_rr4
= GUEST_PHYSICAL_RR4
;
820 kvm
->arch
.vmm_init_rr
= VMM_INIT_RR
;
823 *Fill P2M entries for MMIO/IO ranges
825 kvm_build_io_pmt(kvm
);
827 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
829 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
830 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
835 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
,
836 struct kvm_irqchip
*chip
)
841 switch (chip
->chip_id
) {
842 case KVM_IRQCHIP_IOAPIC
:
843 r
= kvm_get_ioapic(kvm
, &chip
->chip
.ioapic
);
852 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
857 switch (chip
->chip_id
) {
858 case KVM_IRQCHIP_IOAPIC
:
859 r
= kvm_set_ioapic(kvm
, &chip
->chip
.ioapic
);
868 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
870 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
872 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
875 for (i
= 0; i
< 16; i
++) {
876 vpd
->vgr
[i
] = regs
->vpd
.vgr
[i
];
877 vpd
->vbgr
[i
] = regs
->vpd
.vbgr
[i
];
879 for (i
= 0; i
< 128; i
++)
880 vpd
->vcr
[i
] = regs
->vpd
.vcr
[i
];
881 vpd
->vhpi
= regs
->vpd
.vhpi
;
882 vpd
->vnat
= regs
->vpd
.vnat
;
883 vpd
->vbnat
= regs
->vpd
.vbnat
;
884 vpd
->vpsr
= regs
->vpd
.vpsr
;
886 vpd
->vpr
= regs
->vpd
.vpr
;
888 memcpy(&vcpu
->arch
.guest
, ®s
->saved_guest
, sizeof(union context
));
890 RESTORE_REGS(mp_state
);
891 RESTORE_REGS(vmm_rr
);
892 memcpy(vcpu
->arch
.itrs
, regs
->itrs
, sizeof(struct thash_data
) * NITRS
);
893 memcpy(vcpu
->arch
.dtrs
, regs
->dtrs
, sizeof(struct thash_data
) * NDTRS
);
894 RESTORE_REGS(itr_regions
);
895 RESTORE_REGS(dtr_regions
);
896 RESTORE_REGS(tc_regions
);
897 RESTORE_REGS(irq_check
);
898 RESTORE_REGS(itc_check
);
899 RESTORE_REGS(timer_check
);
900 RESTORE_REGS(timer_pending
);
901 RESTORE_REGS(last_itc
);
902 for (i
= 0; i
< 8; i
++) {
903 vcpu
->arch
.vrr
[i
] = regs
->vrr
[i
];
904 vcpu
->arch
.ibr
[i
] = regs
->ibr
[i
];
905 vcpu
->arch
.dbr
[i
] = regs
->dbr
[i
];
907 for (i
= 0; i
< 4; i
++)
908 vcpu
->arch
.insvc
[i
] = regs
->insvc
[i
];
910 RESTORE_REGS(metaphysical_rr0
);
911 RESTORE_REGS(metaphysical_rr4
);
912 RESTORE_REGS(metaphysical_saved_rr0
);
913 RESTORE_REGS(metaphysical_saved_rr4
);
914 RESTORE_REGS(fp_psr
);
915 RESTORE_REGS(saved_gp
);
917 vcpu
->arch
.irq_new_pending
= 1;
918 vcpu
->arch
.itc_offset
= regs
->saved_itc
- kvm_get_itc(vcpu
);
919 set_bit(KVM_REQ_RESUME
, &vcpu
->requests
);
924 long kvm_arch_vm_ioctl(struct file
*filp
,
925 unsigned int ioctl
, unsigned long arg
)
927 struct kvm
*kvm
= filp
->private_data
;
928 void __user
*argp
= (void __user
*)arg
;
932 case KVM_SET_MEMORY_REGION
: {
933 struct kvm_memory_region kvm_mem
;
934 struct kvm_userspace_memory_region kvm_userspace_mem
;
937 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
939 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
940 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
941 kvm_userspace_mem
.guest_phys_addr
=
942 kvm_mem
.guest_phys_addr
;
943 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
944 r
= kvm_vm_ioctl_set_memory_region(kvm
,
945 &kvm_userspace_mem
, 0);
950 case KVM_CREATE_IRQCHIP
:
952 r
= kvm_ioapic_init(kvm
);
955 r
= kvm_setup_default_irq_routing(kvm
);
957 mutex_lock(&kvm
->slots_lock
);
958 kvm_ioapic_destroy(kvm
);
959 mutex_unlock(&kvm
->slots_lock
);
963 case KVM_IRQ_LINE_STATUS
:
965 struct kvm_irq_level irq_event
;
968 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
971 if (irqchip_in_kernel(kvm
)) {
973 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
974 irq_event
.irq
, irq_event
.level
);
975 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
977 irq_event
.status
= status
;
978 if (copy_to_user(argp
, &irq_event
,
986 case KVM_GET_IRQCHIP
: {
987 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
988 struct kvm_irqchip chip
;
991 if (copy_from_user(&chip
, argp
, sizeof chip
))
994 if (!irqchip_in_kernel(kvm
))
996 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1000 if (copy_to_user(argp
, &chip
, sizeof chip
))
1005 case KVM_SET_IRQCHIP
: {
1006 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1007 struct kvm_irqchip chip
;
1010 if (copy_from_user(&chip
, argp
, sizeof chip
))
1013 if (!irqchip_in_kernel(kvm
))
1015 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1028 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1029 struct kvm_sregs
*sregs
)
1034 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1035 struct kvm_sregs
*sregs
)
1040 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1041 struct kvm_translation
*tr
)
1047 static int kvm_alloc_vmm_area(void)
1049 if (!kvm_vmm_base
&& (kvm_vm_buffer_size
< KVM_VM_BUFFER_SIZE
)) {
1050 kvm_vmm_base
= __get_free_pages(GFP_KERNEL
,
1051 get_order(KVM_VMM_SIZE
));
1055 memset((void *)kvm_vmm_base
, 0, KVM_VMM_SIZE
);
1056 kvm_vm_buffer
= kvm_vmm_base
+ VMM_SIZE
;
1058 printk(KERN_DEBUG
"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1059 kvm_vmm_base
, kvm_vm_buffer
);
1065 static void kvm_free_vmm_area(void)
1068 /*Zero this area before free to avoid bits leak!!*/
1069 memset((void *)kvm_vmm_base
, 0, KVM_VMM_SIZE
);
1070 free_pages(kvm_vmm_base
, get_order(KVM_VMM_SIZE
));
1077 static int vti_init_vpd(struct kvm_vcpu
*vcpu
)
1080 union cpuid3_t cpuid3
;
1081 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1084 return PTR_ERR(vpd
);
1087 for (i
= 0; i
< 5; i
++)
1088 vpd
->vcpuid
[i
] = ia64_get_cpuid(i
);
1090 /* Limit the CPUID number to 5 */
1091 cpuid3
.value
= vpd
->vcpuid
[3];
1092 cpuid3
.number
= 4; /* 5 - 1 */
1093 vpd
->vcpuid
[3] = cpuid3
.value
;
1095 /*Set vac and vdc fields*/
1096 vpd
->vac
.a_from_int_cr
= 1;
1097 vpd
->vac
.a_to_int_cr
= 1;
1098 vpd
->vac
.a_from_psr
= 1;
1099 vpd
->vac
.a_from_cpuid
= 1;
1100 vpd
->vac
.a_cover
= 1;
1103 vpd
->vdc
.d_vmsw
= 1;
1105 /*Set virtual buffer*/
1106 vpd
->virt_env_vaddr
= KVM_VM_BUFFER_BASE
;
1111 static int vti_create_vp(struct kvm_vcpu
*vcpu
)
1114 struct vpd
*vpd
= vcpu
->arch
.vpd
;
1115 unsigned long vmm_ivt
;
1117 vmm_ivt
= kvm_vmm_info
->vmm_ivt
;
1119 printk(KERN_DEBUG
"kvm: vcpu:%p,ivt: 0x%lx\n", vcpu
, vmm_ivt
);
1121 ret
= ia64_pal_vp_create((u64
*)vpd
, (u64
*)vmm_ivt
, 0);
1124 printk(KERN_ERR
"kvm: ia64_pal_vp_create failed!\n");
1130 static void init_ptce_info(struct kvm_vcpu
*vcpu
)
1132 ia64_ptce_info_t ptce
= {0};
1134 ia64_get_ptce(&ptce
);
1135 vcpu
->arch
.ptce_base
= ptce
.base
;
1136 vcpu
->arch
.ptce_count
[0] = ptce
.count
[0];
1137 vcpu
->arch
.ptce_count
[1] = ptce
.count
[1];
1138 vcpu
->arch
.ptce_stride
[0] = ptce
.stride
[0];
1139 vcpu
->arch
.ptce_stride
[1] = ptce
.stride
[1];
1142 static void kvm_migrate_hlt_timer(struct kvm_vcpu
*vcpu
)
1144 struct hrtimer
*p_ht
= &vcpu
->arch
.hlt_timer
;
1146 if (hrtimer_cancel(p_ht
))
1147 hrtimer_start_expires(p_ht
, HRTIMER_MODE_ABS
);
1150 static enum hrtimer_restart
hlt_timer_fn(struct hrtimer
*data
)
1152 struct kvm_vcpu
*vcpu
;
1153 wait_queue_head_t
*q
;
1155 vcpu
= container_of(data
, struct kvm_vcpu
, arch
.hlt_timer
);
1158 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_HALTED
)
1161 if (waitqueue_active(q
))
1162 wake_up_interruptible(q
);
1165 vcpu
->arch
.timer_fired
= 1;
1166 vcpu
->arch
.timer_check
= 1;
1167 return HRTIMER_NORESTART
;
1170 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1172 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1178 struct kvm
*kvm
= vcpu
->kvm
;
1179 struct kvm_pt_regs
*regs
= vcpu_regs(vcpu
);
1181 union context
*p_ctx
= &vcpu
->arch
.guest
;
1182 struct kvm_vcpu
*vmm_vcpu
= to_guest(vcpu
->kvm
, vcpu
);
1184 /*Init vcpu context for first run.*/
1185 if (IS_ERR(vmm_vcpu
))
1186 return PTR_ERR(vmm_vcpu
);
1188 if (kvm_vcpu_is_bsp(vcpu
)) {
1189 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
1191 /*Set entry address for first run.*/
1192 regs
->cr_iip
= PALE_RESET_ENTRY
;
1194 /*Initialize itc offset for vcpus*/
1195 itc_offset
= 0UL - kvm_get_itc(vcpu
);
1196 for (i
= 0; i
< KVM_MAX_VCPUS
; i
++) {
1197 v
= (struct kvm_vcpu
*)((char *)vcpu
+
1198 sizeof(struct kvm_vcpu_data
) * i
);
1199 v
->arch
.itc_offset
= itc_offset
;
1200 v
->arch
.last_itc
= 0;
1203 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
1206 vcpu
->arch
.apic
= kzalloc(sizeof(struct kvm_lapic
), GFP_KERNEL
);
1207 if (!vcpu
->arch
.apic
)
1209 vcpu
->arch
.apic
->vcpu
= vcpu
;
1212 p_ctx
->gr
[12] = (unsigned long)((char *)vmm_vcpu
+ KVM_STK_OFFSET
);
1213 p_ctx
->gr
[13] = (unsigned long)vmm_vcpu
;
1214 p_ctx
->psr
= 0x1008522000UL
;
1215 p_ctx
->ar
[40] = FPSR_DEFAULT
; /*fpsr*/
1216 p_ctx
->caller_unat
= 0;
1218 p_ctx
->ar
[36] = 0x0; /*unat*/
1219 p_ctx
->ar
[19] = 0x0; /*rnat*/
1220 p_ctx
->ar
[18] = (unsigned long)vmm_vcpu
+
1221 ((sizeof(struct kvm_vcpu
)+15) & ~15);
1222 p_ctx
->ar
[64] = 0x0; /*pfs*/
1223 p_ctx
->cr
[0] = 0x7e04UL
;
1224 p_ctx
->cr
[2] = (unsigned long)kvm_vmm_info
->vmm_ivt
;
1225 p_ctx
->cr
[8] = 0x3c;
1227 /*Initialize region register*/
1228 p_ctx
->rr
[0] = 0x30;
1229 p_ctx
->rr
[1] = 0x30;
1230 p_ctx
->rr
[2] = 0x30;
1231 p_ctx
->rr
[3] = 0x30;
1232 p_ctx
->rr
[4] = 0x30;
1233 p_ctx
->rr
[5] = 0x30;
1234 p_ctx
->rr
[7] = 0x30;
1236 /*Initialize branch register 0*/
1237 p_ctx
->br
[0] = *(unsigned long *)kvm_vmm_info
->vmm_entry
;
1239 vcpu
->arch
.vmm_rr
= kvm
->arch
.vmm_init_rr
;
1240 vcpu
->arch
.metaphysical_rr0
= kvm
->arch
.metaphysical_rr0
;
1241 vcpu
->arch
.metaphysical_rr4
= kvm
->arch
.metaphysical_rr4
;
1243 hrtimer_init(&vcpu
->arch
.hlt_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1244 vcpu
->arch
.hlt_timer
.function
= hlt_timer_fn
;
1246 vcpu
->arch
.last_run_cpu
= -1;
1247 vcpu
->arch
.vpd
= (struct vpd
*)VPD_BASE(vcpu
->vcpu_id
);
1248 vcpu
->arch
.vsa_base
= kvm_vsa_base
;
1249 vcpu
->arch
.__gp
= kvm_vmm_gp
;
1250 vcpu
->arch
.dirty_log_lock_pa
= __pa(&kvm
->arch
.dirty_log_lock
);
1251 vcpu
->arch
.vhpt
.hash
= (struct thash_data
*)VHPT_BASE(vcpu
->vcpu_id
);
1252 vcpu
->arch
.vtlb
.hash
= (struct thash_data
*)VTLB_BASE(vcpu
->vcpu_id
);
1253 init_ptce_info(vcpu
);
1260 static int vti_vcpu_setup(struct kvm_vcpu
*vcpu
, int id
)
1265 local_irq_save(psr
);
1266 r
= kvm_insert_vmm_mapping(vcpu
);
1267 local_irq_restore(psr
);
1270 r
= kvm_vcpu_init(vcpu
, vcpu
->kvm
, id
);
1274 r
= vti_init_vpd(vcpu
);
1276 printk(KERN_DEBUG
"kvm: vpd init error!!\n");
1280 r
= vti_create_vp(vcpu
);
1284 kvm_purge_vmm_mapping(vcpu
);
1288 kvm_vcpu_uninit(vcpu
);
1293 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
1296 struct kvm_vcpu
*vcpu
;
1297 unsigned long vm_base
= kvm
->arch
.vm_base
;
1301 BUG_ON(sizeof(struct kvm_vcpu
) > VCPU_STRUCT_SIZE
/2);
1304 if (id
>= KVM_MAX_VCPUS
) {
1305 printk(KERN_ERR
"kvm: Can't configure vcpus > %ld",
1312 printk(KERN_ERR
"kvm: Create vcpu[%d] error!\n", id
);
1315 vcpu
= (struct kvm_vcpu
*)(vm_base
+ offsetof(struct kvm_vm_data
,
1316 vcpu_data
[id
].vcpu_struct
));
1320 r
= vti_vcpu_setup(vcpu
, id
);
1324 printk(KERN_DEBUG
"kvm: vcpu_setup error!!\n");
1333 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
1338 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1343 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1348 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
1349 struct kvm_guest_debug
*dbg
)
1354 void kvm_arch_free_vm(struct kvm
*kvm
)
1356 unsigned long vm_base
= kvm
->arch
.vm_base
;
1359 memset((void *)vm_base
, 0, KVM_VM_DATA_SIZE
);
1360 free_pages(vm_base
, get_order(KVM_VM_DATA_SIZE
));
1365 static void kvm_release_vm_pages(struct kvm
*kvm
)
1367 struct kvm_memslots
*slots
;
1368 struct kvm_memory_slot
*memslot
;
1370 unsigned long base_gfn
;
1372 slots
= kvm_memslots(kvm
);
1373 for (i
= 0; i
< slots
->nmemslots
; i
++) {
1374 memslot
= &slots
->memslots
[i
];
1375 base_gfn
= memslot
->base_gfn
;
1377 for (j
= 0; j
< memslot
->npages
; j
++) {
1378 if (memslot
->rmap
[j
])
1379 put_page((struct page
*)memslot
->rmap
[j
]);
1384 void kvm_arch_sync_events(struct kvm
*kvm
)
1388 void kvm_arch_destroy_vm(struct kvm
*kvm
)
1390 kvm_iommu_unmap_guest(kvm
);
1391 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1392 kvm_free_all_assigned_devices(kvm
);
1394 kfree(kvm
->arch
.vioapic
);
1395 kvm_release_vm_pages(kvm
);
1398 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1402 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1404 if (cpu
!= vcpu
->cpu
) {
1406 if (vcpu
->arch
.ht_active
)
1407 kvm_migrate_hlt_timer(vcpu
);
1411 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1413 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1415 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1420 for (i
= 0; i
< 16; i
++) {
1421 regs
->vpd
.vgr
[i
] = vpd
->vgr
[i
];
1422 regs
->vpd
.vbgr
[i
] = vpd
->vbgr
[i
];
1424 for (i
= 0; i
< 128; i
++)
1425 regs
->vpd
.vcr
[i
] = vpd
->vcr
[i
];
1426 regs
->vpd
.vhpi
= vpd
->vhpi
;
1427 regs
->vpd
.vnat
= vpd
->vnat
;
1428 regs
->vpd
.vbnat
= vpd
->vbnat
;
1429 regs
->vpd
.vpsr
= vpd
->vpsr
;
1430 regs
->vpd
.vpr
= vpd
->vpr
;
1432 memcpy(®s
->saved_guest
, &vcpu
->arch
.guest
, sizeof(union context
));
1434 SAVE_REGS(mp_state
);
1436 memcpy(regs
->itrs
, vcpu
->arch
.itrs
, sizeof(struct thash_data
) * NITRS
);
1437 memcpy(regs
->dtrs
, vcpu
->arch
.dtrs
, sizeof(struct thash_data
) * NDTRS
);
1438 SAVE_REGS(itr_regions
);
1439 SAVE_REGS(dtr_regions
);
1440 SAVE_REGS(tc_regions
);
1441 SAVE_REGS(irq_check
);
1442 SAVE_REGS(itc_check
);
1443 SAVE_REGS(timer_check
);
1444 SAVE_REGS(timer_pending
);
1445 SAVE_REGS(last_itc
);
1446 for (i
= 0; i
< 8; i
++) {
1447 regs
->vrr
[i
] = vcpu
->arch
.vrr
[i
];
1448 regs
->ibr
[i
] = vcpu
->arch
.ibr
[i
];
1449 regs
->dbr
[i
] = vcpu
->arch
.dbr
[i
];
1451 for (i
= 0; i
< 4; i
++)
1452 regs
->insvc
[i
] = vcpu
->arch
.insvc
[i
];
1453 regs
->saved_itc
= vcpu
->arch
.itc_offset
+ kvm_get_itc(vcpu
);
1455 SAVE_REGS(metaphysical_rr0
);
1456 SAVE_REGS(metaphysical_rr4
);
1457 SAVE_REGS(metaphysical_saved_rr0
);
1458 SAVE_REGS(metaphysical_saved_rr4
);
1460 SAVE_REGS(saved_gp
);
1466 int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu
*vcpu
,
1467 struct kvm_ia64_vcpu_stack
*stack
)
1469 memcpy(stack
, vcpu
, sizeof(struct kvm_ia64_vcpu_stack
));
1473 int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu
*vcpu
,
1474 struct kvm_ia64_vcpu_stack
*stack
)
1476 memcpy(vcpu
+ 1, &stack
->stack
[0] + sizeof(struct kvm_vcpu
),
1477 sizeof(struct kvm_ia64_vcpu_stack
) - sizeof(struct kvm_vcpu
));
1479 vcpu
->arch
.exit_data
= ((struct kvm_vcpu
*)stack
)->arch
.exit_data
;
1483 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
1486 hrtimer_cancel(&vcpu
->arch
.hlt_timer
);
1487 kfree(vcpu
->arch
.apic
);
1491 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1492 unsigned int ioctl
, unsigned long arg
)
1494 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1495 void __user
*argp
= (void __user
*)arg
;
1496 struct kvm_ia64_vcpu_stack
*stack
= NULL
;
1500 case KVM_IA64_VCPU_GET_STACK
: {
1501 struct kvm_ia64_vcpu_stack __user
*user_stack
;
1502 void __user
*first_p
= argp
;
1505 if (copy_from_user(&user_stack
, first_p
, sizeof(void *)))
1508 if (!access_ok(VERIFY_WRITE
, user_stack
,
1509 sizeof(struct kvm_ia64_vcpu_stack
))) {
1510 printk(KERN_INFO
"KVM_IA64_VCPU_GET_STACK: "
1511 "Illegal user destination address for stack\n");
1514 stack
= kzalloc(sizeof(struct kvm_ia64_vcpu_stack
), GFP_KERNEL
);
1520 r
= kvm_arch_vcpu_ioctl_get_stack(vcpu
, stack
);
1524 if (copy_to_user(user_stack
, stack
,
1525 sizeof(struct kvm_ia64_vcpu_stack
))) {
1532 case KVM_IA64_VCPU_SET_STACK
: {
1533 struct kvm_ia64_vcpu_stack __user
*user_stack
;
1534 void __user
*first_p
= argp
;
1537 if (copy_from_user(&user_stack
, first_p
, sizeof(void *)))
1540 if (!access_ok(VERIFY_READ
, user_stack
,
1541 sizeof(struct kvm_ia64_vcpu_stack
))) {
1542 printk(KERN_INFO
"KVM_IA64_VCPU_SET_STACK: "
1543 "Illegal user address for stack\n");
1546 stack
= kmalloc(sizeof(struct kvm_ia64_vcpu_stack
), GFP_KERNEL
);
1551 if (copy_from_user(stack
, user_stack
,
1552 sizeof(struct kvm_ia64_vcpu_stack
)))
1555 r
= kvm_arch_vcpu_ioctl_set_stack(vcpu
, stack
);
1568 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
1569 struct kvm_memory_slot
*memslot
,
1570 struct kvm_memory_slot old
,
1571 struct kvm_userspace_memory_region
*mem
,
1576 int npages
= memslot
->npages
;
1577 unsigned long base_gfn
= memslot
->base_gfn
;
1579 if (base_gfn
+ npages
> (KVM_MAX_MEM_SIZE
>> PAGE_SHIFT
))
1582 for (i
= 0; i
< npages
; i
++) {
1583 pfn
= gfn_to_pfn(kvm
, base_gfn
+ i
);
1584 if (!kvm_is_mmio_pfn(pfn
)) {
1585 kvm_set_pmt_entry(kvm
, base_gfn
+ i
,
1587 _PAGE_AR_RWX
| _PAGE_MA_WB
);
1588 memslot
->rmap
[i
] = (unsigned long)pfn_to_page(pfn
);
1590 kvm_set_pmt_entry(kvm
, base_gfn
+ i
,
1591 GPFN_PHYS_MMIO
| (pfn
<< PAGE_SHIFT
),
1593 memslot
->rmap
[i
] = 0;
1600 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
1601 struct kvm_userspace_memory_region
*mem
,
1602 struct kvm_memory_slot old
,
1608 void kvm_arch_flush_shadow(struct kvm
*kvm
)
1610 kvm_flush_remote_tlbs(kvm
);
1613 long kvm_arch_dev_ioctl(struct file
*filp
,
1614 unsigned int ioctl
, unsigned long arg
)
1619 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
1621 kvm_vcpu_uninit(vcpu
);
1624 static int vti_cpu_has_kvm_support(void)
1626 long avail
= 1, status
= 1, control
= 1;
1629 ret
= ia64_pal_proc_get_features(&avail
, &status
, &control
, 0);
1633 if (!(avail
& PAL_PROC_VM_BIT
))
1636 printk(KERN_DEBUG
"kvm: Hardware Supports VT\n");
1638 ret
= ia64_pal_vp_env_info(&kvm_vm_buffer_size
, &vp_env_info
);
1641 printk(KERN_DEBUG
"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size
);
1643 if (!(vp_env_info
& VP_OPCODE
)) {
1644 printk(KERN_WARNING
"kvm: No opcode ability on hardware, "
1645 "vm_env_info:0x%lx\n", vp_env_info
);
1655 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1656 * SN2 RTC, replacing the ITC based default verion.
1658 static void kvm_patch_vmm(struct kvm_vmm_info
*vmm_info
,
1659 struct module
*module
)
1661 unsigned long new_ar
, new_ar_sn2
;
1662 unsigned long module_base
;
1664 if (!ia64_platform_is("sn2"))
1667 module_base
= (unsigned long)module
->module_core
;
1669 new_ar
= kvm_vmm_base
+ vmm_info
->patch_mov_ar
- module_base
;
1670 new_ar_sn2
= kvm_vmm_base
+ vmm_info
->patch_mov_ar_sn2
- module_base
;
1672 printk(KERN_INFO
"kvm: Patching ITC emulation to use SGI SN2 RTC "
1676 * Copy the SN2 version of mov_ar into place. They are both
1677 * the same size, so 6 bundles is sufficient (6 * 0x10).
1679 memcpy((void *)new_ar
, (void *)new_ar_sn2
, 0x60);
1682 static int kvm_relocate_vmm(struct kvm_vmm_info
*vmm_info
,
1683 struct module
*module
)
1685 unsigned long module_base
;
1686 unsigned long vmm_size
;
1688 unsigned long vmm_offset
, func_offset
, fdesc_offset
;
1689 struct fdesc
*p_fdesc
;
1693 if (!kvm_vmm_base
) {
1694 printk("kvm: kvm area hasn't been initialized yet!!\n");
1698 /*Calculate new position of relocated vmm module.*/
1699 module_base
= (unsigned long)module
->module_core
;
1700 vmm_size
= module
->core_size
;
1701 if (unlikely(vmm_size
> KVM_VMM_SIZE
))
1704 memcpy((void *)kvm_vmm_base
, (void *)module_base
, vmm_size
);
1705 kvm_patch_vmm(vmm_info
, module
);
1706 kvm_flush_icache(kvm_vmm_base
, vmm_size
);
1708 /*Recalculate kvm_vmm_info based on new VMM*/
1709 vmm_offset
= vmm_info
->vmm_ivt
- module_base
;
1710 kvm_vmm_info
->vmm_ivt
= KVM_VMM_BASE
+ vmm_offset
;
1711 printk(KERN_DEBUG
"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1712 kvm_vmm_info
->vmm_ivt
);
1714 fdesc_offset
= (unsigned long)vmm_info
->vmm_entry
- module_base
;
1715 kvm_vmm_info
->vmm_entry
= (kvm_vmm_entry
*)(KVM_VMM_BASE
+
1717 func_offset
= *(unsigned long *)vmm_info
->vmm_entry
- module_base
;
1718 p_fdesc
= (struct fdesc
*)(kvm_vmm_base
+ fdesc_offset
);
1719 p_fdesc
->ip
= KVM_VMM_BASE
+ func_offset
;
1720 p_fdesc
->gp
= KVM_VMM_BASE
+(p_fdesc
->gp
- module_base
);
1722 printk(KERN_DEBUG
"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1723 KVM_VMM_BASE
+func_offset
);
1725 fdesc_offset
= (unsigned long)vmm_info
->tramp_entry
- module_base
;
1726 kvm_vmm_info
->tramp_entry
= (kvm_tramp_entry
*)(KVM_VMM_BASE
+
1728 func_offset
= *(unsigned long *)vmm_info
->tramp_entry
- module_base
;
1729 p_fdesc
= (struct fdesc
*)(kvm_vmm_base
+ fdesc_offset
);
1730 p_fdesc
->ip
= KVM_VMM_BASE
+ func_offset
;
1731 p_fdesc
->gp
= KVM_VMM_BASE
+ (p_fdesc
->gp
- module_base
);
1733 kvm_vmm_gp
= p_fdesc
->gp
;
1735 printk(KERN_DEBUG
"kvm: Relocated VMM's Entry IP:%p\n",
1736 kvm_vmm_info
->vmm_entry
);
1737 printk(KERN_DEBUG
"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1738 KVM_VMM_BASE
+ func_offset
);
1743 int kvm_arch_init(void *opaque
)
1746 struct kvm_vmm_info
*vmm_info
= (struct kvm_vmm_info
*)opaque
;
1748 if (!vti_cpu_has_kvm_support()) {
1749 printk(KERN_ERR
"kvm: No Hardware Virtualization Support!\n");
1755 printk(KERN_ERR
"kvm: Already loaded VMM module!\n");
1761 kvm_vmm_info
= kzalloc(sizeof(struct kvm_vmm_info
), GFP_KERNEL
);
1765 if (kvm_alloc_vmm_area())
1768 r
= kvm_relocate_vmm(vmm_info
, vmm_info
->module
);
1775 kvm_free_vmm_area();
1777 kfree(kvm_vmm_info
);
1782 void kvm_arch_exit(void)
1784 kvm_free_vmm_area();
1785 kfree(kvm_vmm_info
);
1786 kvm_vmm_info
= NULL
;
1789 static void kvm_ia64_sync_dirty_log(struct kvm
*kvm
,
1790 struct kvm_memory_slot
*memslot
)
1795 unsigned long *dirty_bitmap
= (unsigned long *)(kvm
->arch
.vm_base
+
1796 offsetof(struct kvm_vm_data
, kvm_mem_dirty_log
));
1798 n
= kvm_dirty_bitmap_bytes(memslot
);
1799 base
= memslot
->base_gfn
/ BITS_PER_LONG
;
1801 spin_lock(&kvm
->arch
.dirty_log_lock
);
1802 for (i
= 0; i
< n
/sizeof(long); ++i
) {
1803 memslot
->dirty_bitmap
[i
] = dirty_bitmap
[base
+ i
];
1804 dirty_bitmap
[base
+ i
] = 0;
1806 spin_unlock(&kvm
->arch
.dirty_log_lock
);
1809 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1810 struct kvm_dirty_log
*log
)
1814 struct kvm_memory_slot
*memslot
;
1817 mutex_lock(&kvm
->slots_lock
);
1820 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1823 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
1825 if (!memslot
->dirty_bitmap
)
1828 kvm_ia64_sync_dirty_log(kvm
, memslot
);
1829 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1833 /* If nothing is dirty, don't bother messing with page tables. */
1835 kvm_flush_remote_tlbs(kvm
);
1836 n
= kvm_dirty_bitmap_bytes(memslot
);
1837 memset(memslot
->dirty_bitmap
, 0, n
);
1841 mutex_unlock(&kvm
->slots_lock
);
1845 int kvm_arch_hardware_setup(void)
1850 void kvm_arch_hardware_unsetup(void)
1854 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
1857 int cpu
= vcpu
->cpu
;
1859 if (waitqueue_active(&vcpu
->wq
))
1860 wake_up_interruptible(&vcpu
->wq
);
1863 if (cpu
!= me
&& (unsigned) cpu
< nr_cpu_ids
&& cpu_online(cpu
))
1864 if (!test_and_set_bit(KVM_REQ_KICK
, &vcpu
->requests
))
1865 smp_send_reschedule(cpu
);
1869 int kvm_apic_set_irq(struct kvm_vcpu
*vcpu
, struct kvm_lapic_irq
*irq
)
1871 return __apic_accept_irq(vcpu
, irq
->vector
);
1874 int kvm_apic_match_physical_addr(struct kvm_lapic
*apic
, u16 dest
)
1876 return apic
->vcpu
->vcpu_id
== dest
;
1879 int kvm_apic_match_logical_addr(struct kvm_lapic
*apic
, u8 mda
)
1884 int kvm_apic_compare_prio(struct kvm_vcpu
*vcpu1
, struct kvm_vcpu
*vcpu2
)
1886 return vcpu1
->arch
.xtp
- vcpu2
->arch
.xtp
;
1889 int kvm_apic_match_dest(struct kvm_vcpu
*vcpu
, struct kvm_lapic
*source
,
1890 int short_hand
, int dest
, int dest_mode
)
1892 struct kvm_lapic
*target
= vcpu
->arch
.apic
;
1893 return (dest_mode
== 0) ?
1894 kvm_apic_match_physical_addr(target
, dest
) :
1895 kvm_apic_match_logical_addr(target
, dest
);
1898 static int find_highest_bits(int *dat
)
1903 /* loop for all 256 bits */
1904 for (i
= 7; i
>= 0 ; i
--) {
1908 return i
* 32 + bitnum
- 1;
1915 int kvm_highest_pending_irq(struct kvm_vcpu
*vcpu
)
1917 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1919 if (vpd
->irr
[0] & (1UL << NMI_VECTOR
))
1921 if (vpd
->irr
[0] & (1UL << ExtINT_VECTOR
))
1922 return ExtINT_VECTOR
;
1924 return find_highest_bits((int *)&vpd
->irr
[0]);
1927 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
1929 return vcpu
->arch
.timer_fired
;
1932 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
1934 return (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
) ||
1935 (kvm_highest_pending_irq(vcpu
) != -1);
1938 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
1939 struct kvm_mp_state
*mp_state
)
1941 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
1945 static int vcpu_reset(struct kvm_vcpu
*vcpu
)
1949 local_irq_save(psr
);
1950 r
= kvm_insert_vmm_mapping(vcpu
);
1951 local_irq_restore(psr
);
1955 vcpu
->arch
.launched
= 0;
1956 kvm_arch_vcpu_uninit(vcpu
);
1957 r
= kvm_arch_vcpu_init(vcpu
);
1961 kvm_purge_vmm_mapping(vcpu
);
1967 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
1968 struct kvm_mp_state
*mp_state
)
1972 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
1973 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)
1974 r
= vcpu_reset(vcpu
);