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/iommu.h>
35 #include <linux/intel-iommu.h>
37 #include <asm/pgtable.h>
38 #include <asm/gcc_intrin.h>
40 #include <asm/cacheflush.h>
41 #include <asm/div64.h>
52 static unsigned long kvm_vmm_base
;
53 static unsigned long kvm_vsa_base
;
54 static unsigned long kvm_vm_buffer
;
55 static unsigned long kvm_vm_buffer_size
;
56 unsigned long kvm_vmm_gp
;
58 static long vp_env_info
;
60 static struct kvm_vmm_info
*kvm_vmm_info
;
62 static DEFINE_PER_CPU(struct kvm_vcpu
*, last_vcpu
);
64 struct kvm_stats_debugfs_item debugfs_entries
[] = {
68 static void kvm_flush_icache(unsigned long start
, unsigned long len
)
72 for (l
= 0; l
< (len
+ 32); l
+= 32)
73 ia64_fc((void *)(start
+ l
));
79 static void kvm_flush_tlb_all(void)
81 unsigned long i
, j
, count0
, count1
, stride0
, stride1
, addr
;
84 addr
= local_cpu_data
->ptce_base
;
85 count0
= local_cpu_data
->ptce_count
[0];
86 count1
= local_cpu_data
->ptce_count
[1];
87 stride0
= local_cpu_data
->ptce_stride
[0];
88 stride1
= local_cpu_data
->ptce_stride
[1];
90 local_irq_save(flags
);
91 for (i
= 0; i
< count0
; ++i
) {
92 for (j
= 0; j
< count1
; ++j
) {
98 local_irq_restore(flags
);
99 ia64_srlz_i(); /* srlz.i implies srlz.d */
102 long ia64_pal_vp_create(u64
*vpd
, u64
*host_iva
, u64
*opt_handler
)
104 struct ia64_pal_retval iprv
;
106 PAL_CALL_STK(iprv
, PAL_VP_CREATE
, (u64
)vpd
, (u64
)host_iva
,
112 static DEFINE_SPINLOCK(vp_lock
);
114 void kvm_arch_hardware_enable(void *garbage
)
119 unsigned long saved_psr
;
122 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
),
124 local_irq_save(saved_psr
);
125 slot
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
126 local_irq_restore(saved_psr
);
131 status
= ia64_pal_vp_init_env(kvm_vsa_base
?
132 VP_INIT_ENV
: VP_INIT_ENV_INITALIZE
,
133 __pa(kvm_vm_buffer
), KVM_VM_BUFFER_BASE
, &tmp_base
);
135 printk(KERN_WARNING
"kvm: Failed to Enable VT Support!!!!\n");
140 kvm_vsa_base
= tmp_base
;
141 printk(KERN_INFO
"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base
);
143 spin_unlock(&vp_lock
);
144 ia64_ptr_entry(0x3, slot
);
147 void kvm_arch_hardware_disable(void *garbage
)
153 unsigned long saved_psr
;
154 unsigned long host_iva
= ia64_getreg(_IA64_REG_CR_IVA
);
156 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
),
159 local_irq_save(saved_psr
);
160 slot
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
161 local_irq_restore(saved_psr
);
165 status
= ia64_pal_vp_exit_env(host_iva
);
167 printk(KERN_DEBUG
"kvm: Failed to disable VT support! :%ld\n",
169 ia64_ptr_entry(0x3, slot
);
172 void kvm_arch_check_processor_compat(void *rtn
)
177 int kvm_dev_ioctl_check_extension(long ext
)
183 case KVM_CAP_IRQCHIP
:
184 case KVM_CAP_MP_STATE
:
185 case KVM_CAP_IRQ_INJECT_STATUS
:
188 case KVM_CAP_COALESCED_MMIO
:
189 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
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
->arch
.online_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
->arch
.online_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 vcpu
->arch
.ht_active
= 1;
443 hrtimer_start(p_ht
, kt
, HRTIMER_MODE_ABS
);
445 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
446 kvm_vcpu_block(vcpu
);
447 hrtimer_cancel(p_ht
);
448 vcpu
->arch
.ht_active
= 0;
450 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
))
451 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_HALTED
)
452 vcpu
->arch
.mp_state
=
453 KVM_MP_STATE_RUNNABLE
;
455 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_RUNNABLE
)
459 printk(KERN_ERR
"kvm: Unsupported userspace halt!");
464 static int handle_vm_shutdown(struct kvm_vcpu
*vcpu
,
465 struct kvm_run
*kvm_run
)
467 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
471 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
472 struct kvm_run
*kvm_run
)
477 static int handle_vcpu_debug(struct kvm_vcpu
*vcpu
,
478 struct kvm_run
*kvm_run
)
480 printk("VMM: %s", vcpu
->arch
.log_buf
);
484 static int (*kvm_vti_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
485 struct kvm_run
*kvm_run
) = {
486 [EXIT_REASON_VM_PANIC
] = handle_vm_error
,
487 [EXIT_REASON_MMIO_INSTRUCTION
] = handle_mmio
,
488 [EXIT_REASON_PAL_CALL
] = handle_pal_call
,
489 [EXIT_REASON_SAL_CALL
] = handle_sal_call
,
490 [EXIT_REASON_SWITCH_RR6
] = handle_switch_rr6
,
491 [EXIT_REASON_VM_DESTROY
] = handle_vm_shutdown
,
492 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
493 [EXIT_REASON_IPI
] = handle_ipi
,
494 [EXIT_REASON_PTC_G
] = handle_global_purge
,
495 [EXIT_REASON_DEBUG
] = handle_vcpu_debug
,
499 static const int kvm_vti_max_exit_handlers
=
500 sizeof(kvm_vti_exit_handlers
)/sizeof(*kvm_vti_exit_handlers
);
502 static uint32_t kvm_get_exit_reason(struct kvm_vcpu
*vcpu
)
504 struct exit_ctl_data
*p_exit_data
;
506 p_exit_data
= kvm_get_exit_data(vcpu
);
507 return p_exit_data
->exit_reason
;
511 * The guest has exited. See if we can fix it or if we need userspace
514 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
516 u32 exit_reason
= kvm_get_exit_reason(vcpu
);
517 vcpu
->arch
.last_exit
= exit_reason
;
519 if (exit_reason
< kvm_vti_max_exit_handlers
520 && kvm_vti_exit_handlers
[exit_reason
])
521 return kvm_vti_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
523 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
524 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
529 static inline void vti_set_rr6(unsigned long rr6
)
531 ia64_set_rr(RR6
, rr6
);
535 static int kvm_insert_vmm_mapping(struct kvm_vcpu
*vcpu
)
538 struct kvm
*kvm
= vcpu
->kvm
;
541 /*Insert a pair of tr to map vmm*/
542 pte
= pte_val(mk_pte_phys(__pa(kvm_vmm_base
), PAGE_KERNEL
));
543 r
= ia64_itr_entry(0x3, KVM_VMM_BASE
, pte
, KVM_VMM_SHIFT
);
546 vcpu
->arch
.vmm_tr_slot
= r
;
547 /*Insert a pairt of tr to map data of vm*/
548 pte
= pte_val(mk_pte_phys(__pa(kvm
->arch
.vm_base
), PAGE_KERNEL
));
549 r
= ia64_itr_entry(0x3, KVM_VM_DATA_BASE
,
550 pte
, KVM_VM_DATA_SHIFT
);
553 vcpu
->arch
.vm_tr_slot
= r
;
560 static void kvm_purge_vmm_mapping(struct kvm_vcpu
*vcpu
)
563 ia64_ptr_entry(0x3, vcpu
->arch
.vmm_tr_slot
);
564 ia64_ptr_entry(0x3, vcpu
->arch
.vm_tr_slot
);
568 static int kvm_vcpu_pre_transition(struct kvm_vcpu
*vcpu
)
570 int cpu
= smp_processor_id();
572 if (vcpu
->arch
.last_run_cpu
!= cpu
||
573 per_cpu(last_vcpu
, cpu
) != vcpu
) {
574 per_cpu(last_vcpu
, cpu
) = vcpu
;
575 vcpu
->arch
.last_run_cpu
= cpu
;
579 vcpu
->arch
.host_rr6
= ia64_get_rr(RR6
);
580 vti_set_rr6(vcpu
->arch
.vmm_rr
);
581 return kvm_insert_vmm_mapping(vcpu
);
583 static void kvm_vcpu_post_transition(struct kvm_vcpu
*vcpu
)
585 kvm_purge_vmm_mapping(vcpu
);
586 vti_set_rr6(vcpu
->arch
.host_rr6
);
589 static int vti_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
591 union context
*host_ctx
, *guest_ctx
;
594 /*Get host and guest context with guest address space.*/
595 host_ctx
= kvm_get_host_context(vcpu
);
596 guest_ctx
= kvm_get_guest_context(vcpu
);
598 r
= kvm_vcpu_pre_transition(vcpu
);
601 kvm_vmm_info
->tramp_entry(host_ctx
, guest_ctx
);
602 kvm_vcpu_post_transition(vcpu
);
608 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
616 if (signal_pending(current
)) {
620 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
624 vcpu
->guest_mode
= 1;
626 down_read(&vcpu
->kvm
->slots_lock
);
627 r
= vti_vcpu_run(vcpu
, kvm_run
);
631 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
635 vcpu
->arch
.launched
= 1;
636 vcpu
->guest_mode
= 0;
640 * We must have an instruction between local_irq_enable() and
641 * kvm_guest_exit(), so the timer interrupt isn't delayed by
642 * the interrupt shadow. The stat.exits increment will do nicely.
643 * But we need to prevent reordering, hence this barrier():
647 up_read(&vcpu
->kvm
->slots_lock
);
650 r
= kvm_handle_exit(kvm_run
, vcpu
);
666 static void kvm_set_mmio_data(struct kvm_vcpu
*vcpu
)
668 struct kvm_mmio_req
*p
= kvm_get_vcpu_ioreq(vcpu
);
670 if (!vcpu
->mmio_is_write
)
671 memcpy(&p
->data
, vcpu
->mmio_data
, 8);
672 p
->state
= STATE_IORESP_READY
;
675 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
682 if (vcpu
->sigset_active
)
683 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
685 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
686 kvm_vcpu_block(vcpu
);
687 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
692 if (vcpu
->mmio_needed
) {
693 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
694 kvm_set_mmio_data(vcpu
);
695 vcpu
->mmio_read_completed
= 1;
696 vcpu
->mmio_needed
= 0;
698 r
= __vcpu_run(vcpu
, kvm_run
);
700 if (vcpu
->sigset_active
)
701 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
707 static struct kvm
*kvm_alloc_kvm(void)
713 BUG_ON(sizeof(struct kvm
) > KVM_VM_STRUCT_SIZE
);
715 vm_base
= __get_free_pages(GFP_KERNEL
, get_order(KVM_VM_DATA_SIZE
));
718 return ERR_PTR(-ENOMEM
);
720 memset((void *)vm_base
, 0, KVM_VM_DATA_SIZE
);
721 kvm
= (struct kvm
*)(vm_base
+
722 offsetof(struct kvm_vm_data
, kvm_vm_struct
));
723 kvm
->arch
.vm_base
= vm_base
;
724 printk(KERN_DEBUG
"kvm: vm's data area:0x%lx\n", vm_base
);
729 struct kvm_io_range
{
735 static const struct kvm_io_range io_ranges
[] = {
736 {VGA_IO_START
, VGA_IO_SIZE
, GPFN_FRAME_BUFFER
},
737 {MMIO_START
, MMIO_SIZE
, GPFN_LOW_MMIO
},
738 {LEGACY_IO_START
, LEGACY_IO_SIZE
, GPFN_LEGACY_IO
},
739 {IO_SAPIC_START
, IO_SAPIC_SIZE
, GPFN_IOSAPIC
},
740 {PIB_START
, PIB_SIZE
, GPFN_PIB
},
743 static void kvm_build_io_pmt(struct kvm
*kvm
)
747 /* Mark I/O ranges */
748 for (i
= 0; i
< (sizeof(io_ranges
) / sizeof(struct kvm_io_range
));
750 for (j
= io_ranges
[i
].start
;
751 j
< io_ranges
[i
].start
+ io_ranges
[i
].size
;
753 kvm_set_pmt_entry(kvm
, j
>> PAGE_SHIFT
,
754 io_ranges
[i
].type
, 0);
759 /*Use unused rids to virtualize guest rid.*/
760 #define GUEST_PHYSICAL_RR0 0x1739
761 #define GUEST_PHYSICAL_RR4 0x2739
762 #define VMM_INIT_RR 0x1660
764 static void kvm_init_vm(struct kvm
*kvm
)
768 kvm
->arch
.metaphysical_rr0
= GUEST_PHYSICAL_RR0
;
769 kvm
->arch
.metaphysical_rr4
= GUEST_PHYSICAL_RR4
;
770 kvm
->arch
.vmm_init_rr
= VMM_INIT_RR
;
773 *Fill P2M entries for MMIO/IO ranges
775 kvm_build_io_pmt(kvm
);
777 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
779 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
780 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
783 struct kvm
*kvm_arch_create_vm(void)
785 struct kvm
*kvm
= kvm_alloc_kvm();
788 return ERR_PTR(-ENOMEM
);
791 kvm
->arch
.online_vcpus
= 0;
797 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
,
798 struct kvm_irqchip
*chip
)
803 switch (chip
->chip_id
) {
804 case KVM_IRQCHIP_IOAPIC
:
805 memcpy(&chip
->chip
.ioapic
, ioapic_irqchip(kvm
),
806 sizeof(struct kvm_ioapic_state
));
815 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
820 switch (chip
->chip_id
) {
821 case KVM_IRQCHIP_IOAPIC
:
822 memcpy(ioapic_irqchip(kvm
),
824 sizeof(struct kvm_ioapic_state
));
833 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
835 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
837 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
842 for (i
= 0; i
< 16; i
++) {
843 vpd
->vgr
[i
] = regs
->vpd
.vgr
[i
];
844 vpd
->vbgr
[i
] = regs
->vpd
.vbgr
[i
];
846 for (i
= 0; i
< 128; i
++)
847 vpd
->vcr
[i
] = regs
->vpd
.vcr
[i
];
848 vpd
->vhpi
= regs
->vpd
.vhpi
;
849 vpd
->vnat
= regs
->vpd
.vnat
;
850 vpd
->vbnat
= regs
->vpd
.vbnat
;
851 vpd
->vpsr
= regs
->vpd
.vpsr
;
853 vpd
->vpr
= regs
->vpd
.vpr
;
855 memcpy(&vcpu
->arch
.guest
, ®s
->saved_guest
, sizeof(union context
));
857 RESTORE_REGS(mp_state
);
858 RESTORE_REGS(vmm_rr
);
859 memcpy(vcpu
->arch
.itrs
, regs
->itrs
, sizeof(struct thash_data
) * NITRS
);
860 memcpy(vcpu
->arch
.dtrs
, regs
->dtrs
, sizeof(struct thash_data
) * NDTRS
);
861 RESTORE_REGS(itr_regions
);
862 RESTORE_REGS(dtr_regions
);
863 RESTORE_REGS(tc_regions
);
864 RESTORE_REGS(irq_check
);
865 RESTORE_REGS(itc_check
);
866 RESTORE_REGS(timer_check
);
867 RESTORE_REGS(timer_pending
);
868 RESTORE_REGS(last_itc
);
869 for (i
= 0; i
< 8; i
++) {
870 vcpu
->arch
.vrr
[i
] = regs
->vrr
[i
];
871 vcpu
->arch
.ibr
[i
] = regs
->ibr
[i
];
872 vcpu
->arch
.dbr
[i
] = regs
->dbr
[i
];
874 for (i
= 0; i
< 4; i
++)
875 vcpu
->arch
.insvc
[i
] = regs
->insvc
[i
];
877 RESTORE_REGS(metaphysical_rr0
);
878 RESTORE_REGS(metaphysical_rr4
);
879 RESTORE_REGS(metaphysical_saved_rr0
);
880 RESTORE_REGS(metaphysical_saved_rr4
);
881 RESTORE_REGS(fp_psr
);
882 RESTORE_REGS(saved_gp
);
884 vcpu
->arch
.irq_new_pending
= 1;
885 vcpu
->arch
.itc_offset
= regs
->saved_itc
- ia64_getreg(_IA64_REG_AR_ITC
);
886 set_bit(KVM_REQ_RESUME
, &vcpu
->requests
);
893 long kvm_arch_vm_ioctl(struct file
*filp
,
894 unsigned int ioctl
, unsigned long arg
)
896 struct kvm
*kvm
= filp
->private_data
;
897 void __user
*argp
= (void __user
*)arg
;
901 case KVM_SET_MEMORY_REGION
: {
902 struct kvm_memory_region kvm_mem
;
903 struct kvm_userspace_memory_region kvm_userspace_mem
;
906 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
908 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
909 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
910 kvm_userspace_mem
.guest_phys_addr
=
911 kvm_mem
.guest_phys_addr
;
912 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
913 r
= kvm_vm_ioctl_set_memory_region(kvm
,
914 &kvm_userspace_mem
, 0);
919 case KVM_CREATE_IRQCHIP
:
921 r
= kvm_ioapic_init(kvm
);
924 r
= kvm_setup_default_irq_routing(kvm
);
926 kfree(kvm
->arch
.vioapic
);
930 case KVM_IRQ_LINE_STATUS
:
932 struct kvm_irq_level irq_event
;
935 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
937 if (irqchip_in_kernel(kvm
)) {
939 mutex_lock(&kvm
->lock
);
940 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
941 irq_event
.irq
, irq_event
.level
);
942 mutex_unlock(&kvm
->lock
);
943 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
944 irq_event
.status
= status
;
945 if (copy_to_user(argp
, &irq_event
,
953 case KVM_GET_IRQCHIP
: {
954 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
955 struct kvm_irqchip chip
;
958 if (copy_from_user(&chip
, argp
, sizeof chip
))
961 if (!irqchip_in_kernel(kvm
))
963 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
967 if (copy_to_user(argp
, &chip
, sizeof chip
))
972 case KVM_SET_IRQCHIP
: {
973 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
974 struct kvm_irqchip chip
;
977 if (copy_from_user(&chip
, argp
, sizeof chip
))
980 if (!irqchip_in_kernel(kvm
))
982 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
995 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
996 struct kvm_sregs
*sregs
)
1001 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1002 struct kvm_sregs
*sregs
)
1007 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1008 struct kvm_translation
*tr
)
1014 static int kvm_alloc_vmm_area(void)
1016 if (!kvm_vmm_base
&& (kvm_vm_buffer_size
< KVM_VM_BUFFER_SIZE
)) {
1017 kvm_vmm_base
= __get_free_pages(GFP_KERNEL
,
1018 get_order(KVM_VMM_SIZE
));
1022 memset((void *)kvm_vmm_base
, 0, KVM_VMM_SIZE
);
1023 kvm_vm_buffer
= kvm_vmm_base
+ VMM_SIZE
;
1025 printk(KERN_DEBUG
"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1026 kvm_vmm_base
, kvm_vm_buffer
);
1032 static void kvm_free_vmm_area(void)
1035 /*Zero this area before free to avoid bits leak!!*/
1036 memset((void *)kvm_vmm_base
, 0, KVM_VMM_SIZE
);
1037 free_pages(kvm_vmm_base
, get_order(KVM_VMM_SIZE
));
1044 static void vti_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1048 static int vti_init_vpd(struct kvm_vcpu
*vcpu
)
1051 union cpuid3_t cpuid3
;
1052 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1055 return PTR_ERR(vpd
);
1058 for (i
= 0; i
< 5; i
++)
1059 vpd
->vcpuid
[i
] = ia64_get_cpuid(i
);
1061 /* Limit the CPUID number to 5 */
1062 cpuid3
.value
= vpd
->vcpuid
[3];
1063 cpuid3
.number
= 4; /* 5 - 1 */
1064 vpd
->vcpuid
[3] = cpuid3
.value
;
1066 /*Set vac and vdc fields*/
1067 vpd
->vac
.a_from_int_cr
= 1;
1068 vpd
->vac
.a_to_int_cr
= 1;
1069 vpd
->vac
.a_from_psr
= 1;
1070 vpd
->vac
.a_from_cpuid
= 1;
1071 vpd
->vac
.a_cover
= 1;
1074 vpd
->vdc
.d_vmsw
= 1;
1076 /*Set virtual buffer*/
1077 vpd
->virt_env_vaddr
= KVM_VM_BUFFER_BASE
;
1082 static int vti_create_vp(struct kvm_vcpu
*vcpu
)
1085 struct vpd
*vpd
= vcpu
->arch
.vpd
;
1086 unsigned long vmm_ivt
;
1088 vmm_ivt
= kvm_vmm_info
->vmm_ivt
;
1090 printk(KERN_DEBUG
"kvm: vcpu:%p,ivt: 0x%lx\n", vcpu
, vmm_ivt
);
1092 ret
= ia64_pal_vp_create((u64
*)vpd
, (u64
*)vmm_ivt
, 0);
1095 printk(KERN_ERR
"kvm: ia64_pal_vp_create failed!\n");
1101 static void init_ptce_info(struct kvm_vcpu
*vcpu
)
1103 ia64_ptce_info_t ptce
= {0};
1105 ia64_get_ptce(&ptce
);
1106 vcpu
->arch
.ptce_base
= ptce
.base
;
1107 vcpu
->arch
.ptce_count
[0] = ptce
.count
[0];
1108 vcpu
->arch
.ptce_count
[1] = ptce
.count
[1];
1109 vcpu
->arch
.ptce_stride
[0] = ptce
.stride
[0];
1110 vcpu
->arch
.ptce_stride
[1] = ptce
.stride
[1];
1113 static void kvm_migrate_hlt_timer(struct kvm_vcpu
*vcpu
)
1115 struct hrtimer
*p_ht
= &vcpu
->arch
.hlt_timer
;
1117 if (hrtimer_cancel(p_ht
))
1118 hrtimer_start_expires(p_ht
, HRTIMER_MODE_ABS
);
1121 static enum hrtimer_restart
hlt_timer_fn(struct hrtimer
*data
)
1123 struct kvm_vcpu
*vcpu
;
1124 wait_queue_head_t
*q
;
1126 vcpu
= container_of(data
, struct kvm_vcpu
, arch
.hlt_timer
);
1129 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_HALTED
)
1132 if (waitqueue_active(q
))
1133 wake_up_interruptible(q
);
1136 vcpu
->arch
.timer_fired
= 1;
1137 vcpu
->arch
.timer_check
= 1;
1138 return HRTIMER_NORESTART
;
1141 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1143 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1149 struct kvm
*kvm
= vcpu
->kvm
;
1150 struct kvm_pt_regs
*regs
= vcpu_regs(vcpu
);
1152 union context
*p_ctx
= &vcpu
->arch
.guest
;
1153 struct kvm_vcpu
*vmm_vcpu
= to_guest(vcpu
->kvm
, vcpu
);
1155 /*Init vcpu context for first run.*/
1156 if (IS_ERR(vmm_vcpu
))
1157 return PTR_ERR(vmm_vcpu
);
1159 if (vcpu
->vcpu_id
== 0) {
1160 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
1162 /*Set entry address for first run.*/
1163 regs
->cr_iip
= PALE_RESET_ENTRY
;
1165 /*Initialize itc offset for vcpus*/
1166 itc_offset
= 0UL - ia64_getreg(_IA64_REG_AR_ITC
);
1167 for (i
= 0; i
< kvm
->arch
.online_vcpus
; i
++) {
1168 v
= (struct kvm_vcpu
*)((char *)vcpu
+
1169 sizeof(struct kvm_vcpu_data
) * i
);
1170 v
->arch
.itc_offset
= itc_offset
;
1171 v
->arch
.last_itc
= 0;
1174 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
1177 vcpu
->arch
.apic
= kzalloc(sizeof(struct kvm_lapic
), GFP_KERNEL
);
1178 if (!vcpu
->arch
.apic
)
1180 vcpu
->arch
.apic
->vcpu
= vcpu
;
1183 p_ctx
->gr
[12] = (unsigned long)((char *)vmm_vcpu
+ KVM_STK_OFFSET
);
1184 p_ctx
->gr
[13] = (unsigned long)vmm_vcpu
;
1185 p_ctx
->psr
= 0x1008522000UL
;
1186 p_ctx
->ar
[40] = FPSR_DEFAULT
; /*fpsr*/
1187 p_ctx
->caller_unat
= 0;
1189 p_ctx
->ar
[36] = 0x0; /*unat*/
1190 p_ctx
->ar
[19] = 0x0; /*rnat*/
1191 p_ctx
->ar
[18] = (unsigned long)vmm_vcpu
+
1192 ((sizeof(struct kvm_vcpu
)+15) & ~15);
1193 p_ctx
->ar
[64] = 0x0; /*pfs*/
1194 p_ctx
->cr
[0] = 0x7e04UL
;
1195 p_ctx
->cr
[2] = (unsigned long)kvm_vmm_info
->vmm_ivt
;
1196 p_ctx
->cr
[8] = 0x3c;
1198 /*Initilize region register*/
1199 p_ctx
->rr
[0] = 0x30;
1200 p_ctx
->rr
[1] = 0x30;
1201 p_ctx
->rr
[2] = 0x30;
1202 p_ctx
->rr
[3] = 0x30;
1203 p_ctx
->rr
[4] = 0x30;
1204 p_ctx
->rr
[5] = 0x30;
1205 p_ctx
->rr
[7] = 0x30;
1207 /*Initilize branch register 0*/
1208 p_ctx
->br
[0] = *(unsigned long *)kvm_vmm_info
->vmm_entry
;
1210 vcpu
->arch
.vmm_rr
= kvm
->arch
.vmm_init_rr
;
1211 vcpu
->arch
.metaphysical_rr0
= kvm
->arch
.metaphysical_rr0
;
1212 vcpu
->arch
.metaphysical_rr4
= kvm
->arch
.metaphysical_rr4
;
1214 hrtimer_init(&vcpu
->arch
.hlt_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1215 vcpu
->arch
.hlt_timer
.function
= hlt_timer_fn
;
1217 vcpu
->arch
.last_run_cpu
= -1;
1218 vcpu
->arch
.vpd
= (struct vpd
*)VPD_BASE(vcpu
->vcpu_id
);
1219 vcpu
->arch
.vsa_base
= kvm_vsa_base
;
1220 vcpu
->arch
.__gp
= kvm_vmm_gp
;
1221 vcpu
->arch
.dirty_log_lock_pa
= __pa(&kvm
->arch
.dirty_log_lock
);
1222 vcpu
->arch
.vhpt
.hash
= (struct thash_data
*)VHPT_BASE(vcpu
->vcpu_id
);
1223 vcpu
->arch
.vtlb
.hash
= (struct thash_data
*)VTLB_BASE(vcpu
->vcpu_id
);
1224 init_ptce_info(vcpu
);
1231 static int vti_vcpu_setup(struct kvm_vcpu
*vcpu
, int id
)
1236 local_irq_save(psr
);
1237 r
= kvm_insert_vmm_mapping(vcpu
);
1240 r
= kvm_vcpu_init(vcpu
, vcpu
->kvm
, id
);
1244 r
= vti_init_vpd(vcpu
);
1246 printk(KERN_DEBUG
"kvm: vpd init error!!\n");
1250 r
= vti_create_vp(vcpu
);
1254 kvm_purge_vmm_mapping(vcpu
);
1255 local_irq_restore(psr
);
1259 kvm_vcpu_uninit(vcpu
);
1261 local_irq_restore(psr
);
1265 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
1268 struct kvm_vcpu
*vcpu
;
1269 unsigned long vm_base
= kvm
->arch
.vm_base
;
1273 BUG_ON(sizeof(struct kvm_vcpu
) > VCPU_STRUCT_SIZE
/2);
1276 if (id
>= KVM_MAX_VCPUS
) {
1277 printk(KERN_ERR
"kvm: Can't configure vcpus > %ld",
1284 printk(KERN_ERR
"kvm: Create vcpu[%d] error!\n", id
);
1287 vcpu
= (struct kvm_vcpu
*)(vm_base
+ offsetof(struct kvm_vm_data
,
1288 vcpu_data
[id
].vcpu_struct
));
1292 vti_vcpu_load(vcpu
, cpu
);
1293 r
= vti_vcpu_setup(vcpu
, id
);
1297 printk(KERN_DEBUG
"kvm: vcpu_setup error!!\n");
1301 kvm
->arch
.online_vcpus
++;
1308 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
1313 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1318 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1323 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
1324 struct kvm_guest_debug
*dbg
)
1329 static void free_kvm(struct kvm
*kvm
)
1331 unsigned long vm_base
= kvm
->arch
.vm_base
;
1334 memset((void *)vm_base
, 0, KVM_VM_DATA_SIZE
);
1335 free_pages(vm_base
, get_order(KVM_VM_DATA_SIZE
));
1340 static void kvm_release_vm_pages(struct kvm
*kvm
)
1342 struct kvm_memory_slot
*memslot
;
1344 unsigned long base_gfn
;
1346 for (i
= 0; i
< kvm
->nmemslots
; i
++) {
1347 memslot
= &kvm
->memslots
[i
];
1348 base_gfn
= memslot
->base_gfn
;
1350 for (j
= 0; j
< memslot
->npages
; j
++) {
1351 if (memslot
->rmap
[j
])
1352 put_page((struct page
*)memslot
->rmap
[j
]);
1357 void kvm_arch_sync_events(struct kvm
*kvm
)
1361 void kvm_arch_destroy_vm(struct kvm
*kvm
)
1363 kvm_iommu_unmap_guest(kvm
);
1364 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1365 kvm_free_all_assigned_devices(kvm
);
1367 kfree(kvm
->arch
.vioapic
);
1368 kvm_release_vm_pages(kvm
);
1369 kvm_free_physmem(kvm
);
1373 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1377 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1379 if (cpu
!= vcpu
->cpu
) {
1381 if (vcpu
->arch
.ht_active
)
1382 kvm_migrate_hlt_timer(vcpu
);
1386 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1388 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1390 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1395 for (i
= 0; i
< 16; i
++) {
1396 regs
->vpd
.vgr
[i
] = vpd
->vgr
[i
];
1397 regs
->vpd
.vbgr
[i
] = vpd
->vbgr
[i
];
1399 for (i
= 0; i
< 128; i
++)
1400 regs
->vpd
.vcr
[i
] = vpd
->vcr
[i
];
1401 regs
->vpd
.vhpi
= vpd
->vhpi
;
1402 regs
->vpd
.vnat
= vpd
->vnat
;
1403 regs
->vpd
.vbnat
= vpd
->vbnat
;
1404 regs
->vpd
.vpsr
= vpd
->vpsr
;
1405 regs
->vpd
.vpr
= vpd
->vpr
;
1407 memcpy(®s
->saved_guest
, &vcpu
->arch
.guest
, sizeof(union context
));
1409 SAVE_REGS(mp_state
);
1411 memcpy(regs
->itrs
, vcpu
->arch
.itrs
, sizeof(struct thash_data
) * NITRS
);
1412 memcpy(regs
->dtrs
, vcpu
->arch
.dtrs
, sizeof(struct thash_data
) * NDTRS
);
1413 SAVE_REGS(itr_regions
);
1414 SAVE_REGS(dtr_regions
);
1415 SAVE_REGS(tc_regions
);
1416 SAVE_REGS(irq_check
);
1417 SAVE_REGS(itc_check
);
1418 SAVE_REGS(timer_check
);
1419 SAVE_REGS(timer_pending
);
1420 SAVE_REGS(last_itc
);
1421 for (i
= 0; i
< 8; i
++) {
1422 regs
->vrr
[i
] = vcpu
->arch
.vrr
[i
];
1423 regs
->ibr
[i
] = vcpu
->arch
.ibr
[i
];
1424 regs
->dbr
[i
] = vcpu
->arch
.dbr
[i
];
1426 for (i
= 0; i
< 4; i
++)
1427 regs
->insvc
[i
] = vcpu
->arch
.insvc
[i
];
1428 regs
->saved_itc
= vcpu
->arch
.itc_offset
+ ia64_getreg(_IA64_REG_AR_ITC
);
1430 SAVE_REGS(metaphysical_rr0
);
1431 SAVE_REGS(metaphysical_rr4
);
1432 SAVE_REGS(metaphysical_saved_rr0
);
1433 SAVE_REGS(metaphysical_saved_rr4
);
1435 SAVE_REGS(saved_gp
);
1441 int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu
*vcpu
,
1442 struct kvm_ia64_vcpu_stack
*stack
)
1444 memcpy(stack
, vcpu
, sizeof(struct kvm_ia64_vcpu_stack
));
1448 int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu
*vcpu
,
1449 struct kvm_ia64_vcpu_stack
*stack
)
1451 memcpy(vcpu
+ 1, &stack
->stack
[0] + sizeof(struct kvm_vcpu
),
1452 sizeof(struct kvm_ia64_vcpu_stack
) - sizeof(struct kvm_vcpu
));
1454 vcpu
->arch
.exit_data
= ((struct kvm_vcpu
*)stack
)->arch
.exit_data
;
1458 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
1461 hrtimer_cancel(&vcpu
->arch
.hlt_timer
);
1462 kfree(vcpu
->arch
.apic
);
1466 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1467 unsigned int ioctl
, unsigned long arg
)
1469 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1470 void __user
*argp
= (void __user
*)arg
;
1471 struct kvm_ia64_vcpu_stack
*stack
= NULL
;
1475 case KVM_IA64_VCPU_GET_STACK
: {
1476 struct kvm_ia64_vcpu_stack __user
*user_stack
;
1477 void __user
*first_p
= argp
;
1480 if (copy_from_user(&user_stack
, first_p
, sizeof(void *)))
1483 if (!access_ok(VERIFY_WRITE
, user_stack
,
1484 sizeof(struct kvm_ia64_vcpu_stack
))) {
1485 printk(KERN_INFO
"KVM_IA64_VCPU_GET_STACK: "
1486 "Illegal user destination address for stack\n");
1489 stack
= kzalloc(sizeof(struct kvm_ia64_vcpu_stack
), GFP_KERNEL
);
1495 r
= kvm_arch_vcpu_ioctl_get_stack(vcpu
, stack
);
1499 if (copy_to_user(user_stack
, stack
,
1500 sizeof(struct kvm_ia64_vcpu_stack
)))
1505 case KVM_IA64_VCPU_SET_STACK
: {
1506 struct kvm_ia64_vcpu_stack __user
*user_stack
;
1507 void __user
*first_p
= argp
;
1510 if (copy_from_user(&user_stack
, first_p
, sizeof(void *)))
1513 if (!access_ok(VERIFY_READ
, user_stack
,
1514 sizeof(struct kvm_ia64_vcpu_stack
))) {
1515 printk(KERN_INFO
"KVM_IA64_VCPU_SET_STACK: "
1516 "Illegal user address for stack\n");
1519 stack
= kmalloc(sizeof(struct kvm_ia64_vcpu_stack
), GFP_KERNEL
);
1524 if (copy_from_user(stack
, user_stack
,
1525 sizeof(struct kvm_ia64_vcpu_stack
)))
1528 r
= kvm_arch_vcpu_ioctl_set_stack(vcpu
, stack
);
1541 int kvm_arch_set_memory_region(struct kvm
*kvm
,
1542 struct kvm_userspace_memory_region
*mem
,
1543 struct kvm_memory_slot old
,
1548 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
1549 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
1550 unsigned long base_gfn
= memslot
->base_gfn
;
1552 if (base_gfn
+ npages
> (KVM_MAX_MEM_SIZE
>> PAGE_SHIFT
))
1555 for (i
= 0; i
< npages
; i
++) {
1556 pfn
= gfn_to_pfn(kvm
, base_gfn
+ i
);
1557 if (!kvm_is_mmio_pfn(pfn
)) {
1558 kvm_set_pmt_entry(kvm
, base_gfn
+ i
,
1560 _PAGE_AR_RWX
| _PAGE_MA_WB
);
1561 memslot
->rmap
[i
] = (unsigned long)pfn_to_page(pfn
);
1563 kvm_set_pmt_entry(kvm
, base_gfn
+ i
,
1564 GPFN_PHYS_MMIO
| (pfn
<< PAGE_SHIFT
),
1566 memslot
->rmap
[i
] = 0;
1573 void kvm_arch_flush_shadow(struct kvm
*kvm
)
1577 long kvm_arch_dev_ioctl(struct file
*filp
,
1578 unsigned int ioctl
, unsigned long arg
)
1583 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
1585 kvm_vcpu_uninit(vcpu
);
1588 static int vti_cpu_has_kvm_support(void)
1590 long avail
= 1, status
= 1, control
= 1;
1593 ret
= ia64_pal_proc_get_features(&avail
, &status
, &control
, 0);
1597 if (!(avail
& PAL_PROC_VM_BIT
))
1600 printk(KERN_DEBUG
"kvm: Hardware Supports VT\n");
1602 ret
= ia64_pal_vp_env_info(&kvm_vm_buffer_size
, &vp_env_info
);
1605 printk(KERN_DEBUG
"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size
);
1607 if (!(vp_env_info
& VP_OPCODE
)) {
1608 printk(KERN_WARNING
"kvm: No opcode ability on hardware, "
1609 "vm_env_info:0x%lx\n", vp_env_info
);
1617 static int kvm_relocate_vmm(struct kvm_vmm_info
*vmm_info
,
1618 struct module
*module
)
1620 unsigned long module_base
;
1621 unsigned long vmm_size
;
1623 unsigned long vmm_offset
, func_offset
, fdesc_offset
;
1624 struct fdesc
*p_fdesc
;
1628 if (!kvm_vmm_base
) {
1629 printk("kvm: kvm area hasn't been initilized yet!!\n");
1633 /*Calculate new position of relocated vmm module.*/
1634 module_base
= (unsigned long)module
->module_core
;
1635 vmm_size
= module
->core_size
;
1636 if (unlikely(vmm_size
> KVM_VMM_SIZE
))
1639 memcpy((void *)kvm_vmm_base
, (void *)module_base
, vmm_size
);
1640 kvm_flush_icache(kvm_vmm_base
, vmm_size
);
1642 /*Recalculate kvm_vmm_info based on new VMM*/
1643 vmm_offset
= vmm_info
->vmm_ivt
- module_base
;
1644 kvm_vmm_info
->vmm_ivt
= KVM_VMM_BASE
+ vmm_offset
;
1645 printk(KERN_DEBUG
"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1646 kvm_vmm_info
->vmm_ivt
);
1648 fdesc_offset
= (unsigned long)vmm_info
->vmm_entry
- module_base
;
1649 kvm_vmm_info
->vmm_entry
= (kvm_vmm_entry
*)(KVM_VMM_BASE
+
1651 func_offset
= *(unsigned long *)vmm_info
->vmm_entry
- module_base
;
1652 p_fdesc
= (struct fdesc
*)(kvm_vmm_base
+ fdesc_offset
);
1653 p_fdesc
->ip
= KVM_VMM_BASE
+ func_offset
;
1654 p_fdesc
->gp
= KVM_VMM_BASE
+(p_fdesc
->gp
- module_base
);
1656 printk(KERN_DEBUG
"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1657 KVM_VMM_BASE
+func_offset
);
1659 fdesc_offset
= (unsigned long)vmm_info
->tramp_entry
- module_base
;
1660 kvm_vmm_info
->tramp_entry
= (kvm_tramp_entry
*)(KVM_VMM_BASE
+
1662 func_offset
= *(unsigned long *)vmm_info
->tramp_entry
- module_base
;
1663 p_fdesc
= (struct fdesc
*)(kvm_vmm_base
+ fdesc_offset
);
1664 p_fdesc
->ip
= KVM_VMM_BASE
+ func_offset
;
1665 p_fdesc
->gp
= KVM_VMM_BASE
+ (p_fdesc
->gp
- module_base
);
1667 kvm_vmm_gp
= p_fdesc
->gp
;
1669 printk(KERN_DEBUG
"kvm: Relocated VMM's Entry IP:%p\n",
1670 kvm_vmm_info
->vmm_entry
);
1671 printk(KERN_DEBUG
"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1672 KVM_VMM_BASE
+ func_offset
);
1677 int kvm_arch_init(void *opaque
)
1680 struct kvm_vmm_info
*vmm_info
= (struct kvm_vmm_info
*)opaque
;
1682 if (!vti_cpu_has_kvm_support()) {
1683 printk(KERN_ERR
"kvm: No Hardware Virtualization Support!\n");
1689 printk(KERN_ERR
"kvm: Already loaded VMM module!\n");
1695 kvm_vmm_info
= kzalloc(sizeof(struct kvm_vmm_info
), GFP_KERNEL
);
1699 if (kvm_alloc_vmm_area())
1702 r
= kvm_relocate_vmm(vmm_info
, vmm_info
->module
);
1709 kvm_free_vmm_area();
1711 kfree(kvm_vmm_info
);
1716 void kvm_arch_exit(void)
1718 kvm_free_vmm_area();
1719 kfree(kvm_vmm_info
);
1720 kvm_vmm_info
= NULL
;
1723 static int kvm_ia64_sync_dirty_log(struct kvm
*kvm
,
1724 struct kvm_dirty_log
*log
)
1726 struct kvm_memory_slot
*memslot
;
1729 unsigned long *dirty_bitmap
= (unsigned long *)(kvm
->arch
.vm_base
+
1730 offsetof(struct kvm_vm_data
, kvm_mem_dirty_log
));
1733 if (log
->slot
>= KVM_MEMORY_SLOTS
)
1736 memslot
= &kvm
->memslots
[log
->slot
];
1738 if (!memslot
->dirty_bitmap
)
1741 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1742 base
= memslot
->base_gfn
/ BITS_PER_LONG
;
1744 for (i
= 0; i
< n
/sizeof(long); ++i
) {
1745 memslot
->dirty_bitmap
[i
] = dirty_bitmap
[base
+ i
];
1746 dirty_bitmap
[base
+ i
] = 0;
1753 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1754 struct kvm_dirty_log
*log
)
1758 struct kvm_memory_slot
*memslot
;
1761 spin_lock(&kvm
->arch
.dirty_log_lock
);
1763 r
= kvm_ia64_sync_dirty_log(kvm
, log
);
1767 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1771 /* If nothing is dirty, don't bother messing with page tables. */
1773 kvm_flush_remote_tlbs(kvm
);
1774 memslot
= &kvm
->memslots
[log
->slot
];
1775 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1776 memset(memslot
->dirty_bitmap
, 0, n
);
1780 spin_unlock(&kvm
->arch
.dirty_log_lock
);
1784 int kvm_arch_hardware_setup(void)
1789 void kvm_arch_hardware_unsetup(void)
1793 static void vcpu_kick_intr(void *info
)
1796 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
1797 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
1801 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
1803 int ipi_pcpu
= vcpu
->cpu
;
1804 int cpu
= get_cpu();
1806 if (waitqueue_active(&vcpu
->wq
))
1807 wake_up_interruptible(&vcpu
->wq
);
1809 if (vcpu
->guest_mode
&& cpu
!= ipi_pcpu
)
1810 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0);
1814 int kvm_apic_set_irq(struct kvm_vcpu
*vcpu
, u8 vec
, u8 trig
)
1817 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1819 if (!test_and_set_bit(vec
, &vpd
->irr
[0])) {
1820 vcpu
->arch
.irq_new_pending
= 1;
1821 kvm_vcpu_kick(vcpu
);
1827 int kvm_apic_match_physical_addr(struct kvm_lapic
*apic
, u16 dest
)
1829 return apic
->vcpu
->vcpu_id
== dest
;
1832 int kvm_apic_match_logical_addr(struct kvm_lapic
*apic
, u8 mda
)
1837 struct kvm_vcpu
*kvm_get_lowest_prio_vcpu(struct kvm
*kvm
, u8 vector
,
1838 unsigned long bitmap
)
1840 struct kvm_vcpu
*lvcpu
= kvm
->vcpus
[0];
1843 for (i
= 1; i
< kvm
->arch
.online_vcpus
; i
++) {
1846 if (lvcpu
->arch
.xtp
> kvm
->vcpus
[i
]->arch
.xtp
)
1847 lvcpu
= kvm
->vcpus
[i
];
1853 static int find_highest_bits(int *dat
)
1858 /* loop for all 256 bits */
1859 for (i
= 7; i
>= 0 ; i
--) {
1863 return i
* 32 + bitnum
- 1;
1870 int kvm_highest_pending_irq(struct kvm_vcpu
*vcpu
)
1872 struct vpd
*vpd
= to_host(vcpu
->kvm
, vcpu
->arch
.vpd
);
1874 if (vpd
->irr
[0] & (1UL << NMI_VECTOR
))
1876 if (vpd
->irr
[0] & (1UL << ExtINT_VECTOR
))
1877 return ExtINT_VECTOR
;
1879 return find_highest_bits((int *)&vpd
->irr
[0]);
1882 int kvm_cpu_has_interrupt(struct kvm_vcpu
*vcpu
)
1884 if (kvm_highest_pending_irq(vcpu
) != -1)
1889 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
1891 return vcpu
->arch
.timer_fired
;
1894 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1899 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
1901 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
;
1904 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
1905 struct kvm_mp_state
*mp_state
)
1908 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
1913 static int vcpu_reset(struct kvm_vcpu
*vcpu
)
1917 local_irq_save(psr
);
1918 r
= kvm_insert_vmm_mapping(vcpu
);
1922 vcpu
->arch
.launched
= 0;
1923 kvm_arch_vcpu_uninit(vcpu
);
1924 r
= kvm_arch_vcpu_init(vcpu
);
1928 kvm_purge_vmm_mapping(vcpu
);
1931 local_irq_restore(psr
);
1935 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
1936 struct kvm_mp_state
*mp_state
)
1941 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
1942 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)
1943 r
= vcpu_reset(vcpu
);