KVM: Timer event should not unconditionally unhalt vcpu.
[linux-2.6/verdex.git] / arch / ia64 / kvm / kvm-ia64.c
blobd2a90fd505b00e7430bd20ae6eb574ce388abc36
1 /*
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
15 * more details.
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>
27 #include <linux/fs.h>
28 #include <linux/smp.h>
29 #include <linux/kvm_host.h>
30 #include <linux/kvm.h>
31 #include <linux/bitops.h>
32 #include <linux/hrtimer.h>
33 #include <linux/uaccess.h>
34 #include <linux/iommu.h>
35 #include <linux/intel-iommu.h>
37 #include <asm/pgtable.h>
38 #include <asm/gcc_intrin.h>
39 #include <asm/pal.h>
40 #include <asm/cacheflush.h>
41 #include <asm/div64.h>
42 #include <asm/tlb.h>
43 #include <asm/elf.h>
44 #include <asm/sn/addrs.h>
45 #include <asm/sn/clksupport.h>
46 #include <asm/sn/shub_mmr.h>
48 #include "misc.h"
49 #include "vti.h"
50 #include "iodev.h"
51 #include "ioapic.h"
52 #include "lapic.h"
53 #include "irq.h"
55 static unsigned long kvm_vmm_base;
56 static unsigned long kvm_vsa_base;
57 static unsigned long kvm_vm_buffer;
58 static unsigned long kvm_vm_buffer_size;
59 unsigned long kvm_vmm_gp;
61 static long vp_env_info;
63 static struct kvm_vmm_info *kvm_vmm_info;
65 static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
67 struct kvm_stats_debugfs_item debugfs_entries[] = {
68 { NULL }
71 static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu)
73 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
74 if (vcpu->kvm->arch.is_sn2)
75 return rtc_time();
76 else
77 #endif
78 return ia64_getreg(_IA64_REG_AR_ITC);
81 static void kvm_flush_icache(unsigned long start, unsigned long len)
83 int l;
85 for (l = 0; l < (len + 32); l += 32)
86 ia64_fc((void *)(start + l));
88 ia64_sync_i();
89 ia64_srlz_i();
92 static void kvm_flush_tlb_all(void)
94 unsigned long i, j, count0, count1, stride0, stride1, addr;
95 long flags;
97 addr = local_cpu_data->ptce_base;
98 count0 = local_cpu_data->ptce_count[0];
99 count1 = local_cpu_data->ptce_count[1];
100 stride0 = local_cpu_data->ptce_stride[0];
101 stride1 = local_cpu_data->ptce_stride[1];
103 local_irq_save(flags);
104 for (i = 0; i < count0; ++i) {
105 for (j = 0; j < count1; ++j) {
106 ia64_ptce(addr);
107 addr += stride1;
109 addr += stride0;
111 local_irq_restore(flags);
112 ia64_srlz_i(); /* srlz.i implies srlz.d */
115 long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
117 struct ia64_pal_retval iprv;
119 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
120 (u64)opt_handler);
122 return iprv.status;
125 static DEFINE_SPINLOCK(vp_lock);
127 void kvm_arch_hardware_enable(void *garbage)
129 long status;
130 long tmp_base;
131 unsigned long pte;
132 unsigned long saved_psr;
133 int slot;
135 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
136 local_irq_save(saved_psr);
137 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
138 local_irq_restore(saved_psr);
139 if (slot < 0)
140 return;
142 spin_lock(&vp_lock);
143 status = ia64_pal_vp_init_env(kvm_vsa_base ?
144 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
145 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
146 if (status != 0) {
147 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
148 return ;
151 if (!kvm_vsa_base) {
152 kvm_vsa_base = tmp_base;
153 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
155 spin_unlock(&vp_lock);
156 ia64_ptr_entry(0x3, slot);
159 void kvm_arch_hardware_disable(void *garbage)
162 long status;
163 int slot;
164 unsigned long pte;
165 unsigned long saved_psr;
166 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
168 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
169 PAGE_KERNEL));
171 local_irq_save(saved_psr);
172 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
173 local_irq_restore(saved_psr);
174 if (slot < 0)
175 return;
177 status = ia64_pal_vp_exit_env(host_iva);
178 if (status)
179 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
180 status);
181 ia64_ptr_entry(0x3, slot);
184 void kvm_arch_check_processor_compat(void *rtn)
186 *(int *)rtn = 0;
189 int kvm_dev_ioctl_check_extension(long ext)
192 int r;
194 switch (ext) {
195 case KVM_CAP_IRQCHIP:
196 case KVM_CAP_MP_STATE:
197 case KVM_CAP_IRQ_INJECT_STATUS:
198 r = 1;
199 break;
200 case KVM_CAP_COALESCED_MMIO:
201 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
202 break;
203 case KVM_CAP_IOMMU:
204 r = iommu_found();
205 break;
206 default:
207 r = 0;
209 return r;
213 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
214 gpa_t addr, int len, int is_write)
216 struct kvm_io_device *dev;
218 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
220 return dev;
223 static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
225 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
226 kvm_run->hw.hardware_exit_reason = 1;
227 return 0;
230 static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
232 struct kvm_mmio_req *p;
233 struct kvm_io_device *mmio_dev;
235 p = kvm_get_vcpu_ioreq(vcpu);
237 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
238 goto mmio;
239 vcpu->mmio_needed = 1;
240 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
241 vcpu->mmio_size = kvm_run->mmio.len = p->size;
242 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
244 if (vcpu->mmio_is_write)
245 memcpy(vcpu->mmio_data, &p->data, p->size);
246 memcpy(kvm_run->mmio.data, &p->data, p->size);
247 kvm_run->exit_reason = KVM_EXIT_MMIO;
248 return 0;
249 mmio:
250 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
251 if (mmio_dev) {
252 if (!p->dir)
253 kvm_iodevice_write(mmio_dev, p->addr, p->size,
254 &p->data);
255 else
256 kvm_iodevice_read(mmio_dev, p->addr, p->size,
257 &p->data);
259 } else
260 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
261 p->state = STATE_IORESP_READY;
263 return 1;
266 static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
268 struct exit_ctl_data *p;
270 p = kvm_get_exit_data(vcpu);
272 if (p->exit_reason == EXIT_REASON_PAL_CALL)
273 return kvm_pal_emul(vcpu, kvm_run);
274 else {
275 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
276 kvm_run->hw.hardware_exit_reason = 2;
277 return 0;
281 static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
283 struct exit_ctl_data *p;
285 p = kvm_get_exit_data(vcpu);
287 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
288 kvm_sal_emul(vcpu);
289 return 1;
290 } else {
291 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
292 kvm_run->hw.hardware_exit_reason = 3;
293 return 0;
298 static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
300 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
302 if (!test_and_set_bit(vector, &vpd->irr[0])) {
303 vcpu->arch.irq_new_pending = 1;
304 kvm_vcpu_kick(vcpu);
305 return 1;
307 return 0;
311 * offset: address offset to IPI space.
312 * value: deliver value.
314 static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
315 uint64_t vector)
317 switch (dm) {
318 case SAPIC_FIXED:
319 break;
320 case SAPIC_NMI:
321 vector = 2;
322 break;
323 case SAPIC_EXTINT:
324 vector = 0;
325 break;
326 case SAPIC_INIT:
327 case SAPIC_PMI:
328 default:
329 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
330 return;
332 __apic_accept_irq(vcpu, vector);
335 static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
336 unsigned long eid)
338 union ia64_lid lid;
339 int i;
341 for (i = 0; i < kvm->arch.online_vcpus; i++) {
342 if (kvm->vcpus[i]) {
343 lid.val = VCPU_LID(kvm->vcpus[i]);
344 if (lid.id == id && lid.eid == eid)
345 return kvm->vcpus[i];
349 return NULL;
352 static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
354 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
355 struct kvm_vcpu *target_vcpu;
356 struct kvm_pt_regs *regs;
357 union ia64_ipi_a addr = p->u.ipi_data.addr;
358 union ia64_ipi_d data = p->u.ipi_data.data;
360 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
361 if (!target_vcpu)
362 return handle_vm_error(vcpu, kvm_run);
364 if (!target_vcpu->arch.launched) {
365 regs = vcpu_regs(target_vcpu);
367 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
368 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
370 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
371 if (waitqueue_active(&target_vcpu->wq))
372 wake_up_interruptible(&target_vcpu->wq);
373 } else {
374 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
375 if (target_vcpu != vcpu)
376 kvm_vcpu_kick(target_vcpu);
379 return 1;
382 struct call_data {
383 struct kvm_ptc_g ptc_g_data;
384 struct kvm_vcpu *vcpu;
387 static void vcpu_global_purge(void *info)
389 struct call_data *p = (struct call_data *)info;
390 struct kvm_vcpu *vcpu = p->vcpu;
392 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
393 return;
395 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
396 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
397 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
398 p->ptc_g_data;
399 } else {
400 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
401 vcpu->arch.ptc_g_count = 0;
402 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
406 static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
408 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
409 struct kvm *kvm = vcpu->kvm;
410 struct call_data call_data;
411 int i;
413 call_data.ptc_g_data = p->u.ptc_g_data;
415 for (i = 0; i < kvm->arch.online_vcpus; i++) {
416 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
417 KVM_MP_STATE_UNINITIALIZED ||
418 vcpu == kvm->vcpus[i])
419 continue;
421 if (waitqueue_active(&kvm->vcpus[i]->wq))
422 wake_up_interruptible(&kvm->vcpus[i]->wq);
424 if (kvm->vcpus[i]->cpu != -1) {
425 call_data.vcpu = kvm->vcpus[i];
426 smp_call_function_single(kvm->vcpus[i]->cpu,
427 vcpu_global_purge, &call_data, 1);
428 } else
429 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
432 return 1;
435 static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
437 return 1;
440 static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
442 unsigned long pte, rtc_phys_addr, map_addr;
443 int slot;
445 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
446 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
447 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
448 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
449 vcpu->arch.sn_rtc_tr_slot = slot;
450 if (slot < 0) {
451 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
452 slot = 0;
454 return slot;
457 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
460 ktime_t kt;
461 long itc_diff;
462 unsigned long vcpu_now_itc;
463 unsigned long expires;
464 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
465 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
466 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
468 if (irqchip_in_kernel(vcpu->kvm)) {
470 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
472 if (time_after(vcpu_now_itc, vpd->itm)) {
473 vcpu->arch.timer_check = 1;
474 return 1;
476 itc_diff = vpd->itm - vcpu_now_itc;
477 if (itc_diff < 0)
478 itc_diff = -itc_diff;
480 expires = div64_u64(itc_diff, cyc_per_usec);
481 kt = ktime_set(0, 1000 * expires);
483 vcpu->arch.ht_active = 1;
484 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
486 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
487 kvm_vcpu_block(vcpu);
488 hrtimer_cancel(p_ht);
489 vcpu->arch.ht_active = 0;
491 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) ||
492 kvm_cpu_has_pending_timer(vcpu))
493 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
494 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
496 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
497 return -EINTR;
498 return 1;
499 } else {
500 printk(KERN_ERR"kvm: Unsupported userspace halt!");
501 return 0;
505 static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
506 struct kvm_run *kvm_run)
508 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
509 return 0;
512 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
513 struct kvm_run *kvm_run)
515 return 1;
518 static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
519 struct kvm_run *kvm_run)
521 printk("VMM: %s", vcpu->arch.log_buf);
522 return 1;
525 static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
526 struct kvm_run *kvm_run) = {
527 [EXIT_REASON_VM_PANIC] = handle_vm_error,
528 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
529 [EXIT_REASON_PAL_CALL] = handle_pal_call,
530 [EXIT_REASON_SAL_CALL] = handle_sal_call,
531 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
532 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
533 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
534 [EXIT_REASON_IPI] = handle_ipi,
535 [EXIT_REASON_PTC_G] = handle_global_purge,
536 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
540 static const int kvm_vti_max_exit_handlers =
541 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
543 static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
545 struct exit_ctl_data *p_exit_data;
547 p_exit_data = kvm_get_exit_data(vcpu);
548 return p_exit_data->exit_reason;
552 * The guest has exited. See if we can fix it or if we need userspace
553 * assistance.
555 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
557 u32 exit_reason = kvm_get_exit_reason(vcpu);
558 vcpu->arch.last_exit = exit_reason;
560 if (exit_reason < kvm_vti_max_exit_handlers
561 && kvm_vti_exit_handlers[exit_reason])
562 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
563 else {
564 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
565 kvm_run->hw.hardware_exit_reason = exit_reason;
567 return 0;
570 static inline void vti_set_rr6(unsigned long rr6)
572 ia64_set_rr(RR6, rr6);
573 ia64_srlz_i();
576 static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
578 unsigned long pte;
579 struct kvm *kvm = vcpu->kvm;
580 int r;
582 /*Insert a pair of tr to map vmm*/
583 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
584 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
585 if (r < 0)
586 goto out;
587 vcpu->arch.vmm_tr_slot = r;
588 /*Insert a pairt of tr to map data of vm*/
589 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
590 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
591 pte, KVM_VM_DATA_SHIFT);
592 if (r < 0)
593 goto out;
594 vcpu->arch.vm_tr_slot = r;
596 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
597 if (kvm->arch.is_sn2) {
598 r = kvm_sn2_setup_mappings(vcpu);
599 if (r < 0)
600 goto out;
602 #endif
604 r = 0;
605 out:
606 return r;
609 static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
611 struct kvm *kvm = vcpu->kvm;
612 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
613 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
614 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
615 if (kvm->arch.is_sn2)
616 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
617 #endif
620 static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
622 int cpu = smp_processor_id();
624 if (vcpu->arch.last_run_cpu != cpu ||
625 per_cpu(last_vcpu, cpu) != vcpu) {
626 per_cpu(last_vcpu, cpu) = vcpu;
627 vcpu->arch.last_run_cpu = cpu;
628 kvm_flush_tlb_all();
631 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
632 vti_set_rr6(vcpu->arch.vmm_rr);
633 return kvm_insert_vmm_mapping(vcpu);
635 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
637 kvm_purge_vmm_mapping(vcpu);
638 vti_set_rr6(vcpu->arch.host_rr6);
641 static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
643 union context *host_ctx, *guest_ctx;
644 int r;
646 /*Get host and guest context with guest address space.*/
647 host_ctx = kvm_get_host_context(vcpu);
648 guest_ctx = kvm_get_guest_context(vcpu);
650 r = kvm_vcpu_pre_transition(vcpu);
651 if (r < 0)
652 goto out;
653 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
654 kvm_vcpu_post_transition(vcpu);
655 r = 0;
656 out:
657 return r;
660 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
662 int r;
664 again:
665 if (signal_pending(current)) {
666 r = -EINTR;
667 kvm_run->exit_reason = KVM_EXIT_INTR;
668 goto out;
672 * down_read() may sleep and return with interrupts enabled
674 down_read(&vcpu->kvm->slots_lock);
676 preempt_disable();
677 local_irq_disable();
679 vcpu->guest_mode = 1;
680 kvm_guest_enter();
681 r = vti_vcpu_run(vcpu, kvm_run);
682 if (r < 0) {
683 local_irq_enable();
684 preempt_enable();
685 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
686 goto out;
689 vcpu->arch.launched = 1;
690 vcpu->guest_mode = 0;
691 local_irq_enable();
694 * We must have an instruction between local_irq_enable() and
695 * kvm_guest_exit(), so the timer interrupt isn't delayed by
696 * the interrupt shadow. The stat.exits increment will do nicely.
697 * But we need to prevent reordering, hence this barrier():
699 barrier();
700 kvm_guest_exit();
701 up_read(&vcpu->kvm->slots_lock);
702 preempt_enable();
704 r = kvm_handle_exit(kvm_run, vcpu);
706 if (r > 0) {
707 if (!need_resched())
708 goto again;
711 out:
712 if (r > 0) {
713 kvm_resched(vcpu);
714 goto again;
717 return r;
720 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
722 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
724 if (!vcpu->mmio_is_write)
725 memcpy(&p->data, vcpu->mmio_data, 8);
726 p->state = STATE_IORESP_READY;
729 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
731 int r;
732 sigset_t sigsaved;
734 vcpu_load(vcpu);
736 if (vcpu->sigset_active)
737 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
739 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
740 kvm_vcpu_block(vcpu);
741 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
742 r = -EAGAIN;
743 goto out;
746 if (vcpu->mmio_needed) {
747 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
748 kvm_set_mmio_data(vcpu);
749 vcpu->mmio_read_completed = 1;
750 vcpu->mmio_needed = 0;
752 r = __vcpu_run(vcpu, kvm_run);
753 out:
754 if (vcpu->sigset_active)
755 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
757 vcpu_put(vcpu);
758 return r;
761 static struct kvm *kvm_alloc_kvm(void)
764 struct kvm *kvm;
765 uint64_t vm_base;
767 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
769 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
771 if (!vm_base)
772 return ERR_PTR(-ENOMEM);
774 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
775 kvm = (struct kvm *)(vm_base +
776 offsetof(struct kvm_vm_data, kvm_vm_struct));
777 kvm->arch.vm_base = vm_base;
778 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
780 return kvm;
783 struct kvm_io_range {
784 unsigned long start;
785 unsigned long size;
786 unsigned long type;
789 static const struct kvm_io_range io_ranges[] = {
790 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
791 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
792 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
793 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
794 {PIB_START, PIB_SIZE, GPFN_PIB},
797 static void kvm_build_io_pmt(struct kvm *kvm)
799 unsigned long i, j;
801 /* Mark I/O ranges */
802 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
803 i++) {
804 for (j = io_ranges[i].start;
805 j < io_ranges[i].start + io_ranges[i].size;
806 j += PAGE_SIZE)
807 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
808 io_ranges[i].type, 0);
813 /*Use unused rids to virtualize guest rid.*/
814 #define GUEST_PHYSICAL_RR0 0x1739
815 #define GUEST_PHYSICAL_RR4 0x2739
816 #define VMM_INIT_RR 0x1660
818 static void kvm_init_vm(struct kvm *kvm)
820 BUG_ON(!kvm);
822 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
823 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
824 kvm->arch.vmm_init_rr = VMM_INIT_RR;
827 *Fill P2M entries for MMIO/IO ranges
829 kvm_build_io_pmt(kvm);
831 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
833 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
834 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
837 struct kvm *kvm_arch_create_vm(void)
839 struct kvm *kvm = kvm_alloc_kvm();
841 if (IS_ERR(kvm))
842 return ERR_PTR(-ENOMEM);
844 kvm->arch.is_sn2 = ia64_platform_is("sn2");
846 kvm_init_vm(kvm);
848 kvm->arch.online_vcpus = 0;
850 return kvm;
854 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
855 struct kvm_irqchip *chip)
857 int r;
859 r = 0;
860 switch (chip->chip_id) {
861 case KVM_IRQCHIP_IOAPIC:
862 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
863 sizeof(struct kvm_ioapic_state));
864 break;
865 default:
866 r = -EINVAL;
867 break;
869 return r;
872 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
874 int r;
876 r = 0;
877 switch (chip->chip_id) {
878 case KVM_IRQCHIP_IOAPIC:
879 memcpy(ioapic_irqchip(kvm),
880 &chip->chip.ioapic,
881 sizeof(struct kvm_ioapic_state));
882 break;
883 default:
884 r = -EINVAL;
885 break;
887 return r;
890 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
892 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
894 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
895 int i;
897 vcpu_load(vcpu);
899 for (i = 0; i < 16; i++) {
900 vpd->vgr[i] = regs->vpd.vgr[i];
901 vpd->vbgr[i] = regs->vpd.vbgr[i];
903 for (i = 0; i < 128; i++)
904 vpd->vcr[i] = regs->vpd.vcr[i];
905 vpd->vhpi = regs->vpd.vhpi;
906 vpd->vnat = regs->vpd.vnat;
907 vpd->vbnat = regs->vpd.vbnat;
908 vpd->vpsr = regs->vpd.vpsr;
910 vpd->vpr = regs->vpd.vpr;
912 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
914 RESTORE_REGS(mp_state);
915 RESTORE_REGS(vmm_rr);
916 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
917 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
918 RESTORE_REGS(itr_regions);
919 RESTORE_REGS(dtr_regions);
920 RESTORE_REGS(tc_regions);
921 RESTORE_REGS(irq_check);
922 RESTORE_REGS(itc_check);
923 RESTORE_REGS(timer_check);
924 RESTORE_REGS(timer_pending);
925 RESTORE_REGS(last_itc);
926 for (i = 0; i < 8; i++) {
927 vcpu->arch.vrr[i] = regs->vrr[i];
928 vcpu->arch.ibr[i] = regs->ibr[i];
929 vcpu->arch.dbr[i] = regs->dbr[i];
931 for (i = 0; i < 4; i++)
932 vcpu->arch.insvc[i] = regs->insvc[i];
933 RESTORE_REGS(xtp);
934 RESTORE_REGS(metaphysical_rr0);
935 RESTORE_REGS(metaphysical_rr4);
936 RESTORE_REGS(metaphysical_saved_rr0);
937 RESTORE_REGS(metaphysical_saved_rr4);
938 RESTORE_REGS(fp_psr);
939 RESTORE_REGS(saved_gp);
941 vcpu->arch.irq_new_pending = 1;
942 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
943 set_bit(KVM_REQ_RESUME, &vcpu->requests);
945 vcpu_put(vcpu);
947 return 0;
950 long kvm_arch_vm_ioctl(struct file *filp,
951 unsigned int ioctl, unsigned long arg)
953 struct kvm *kvm = filp->private_data;
954 void __user *argp = (void __user *)arg;
955 int r = -EINVAL;
957 switch (ioctl) {
958 case KVM_SET_MEMORY_REGION: {
959 struct kvm_memory_region kvm_mem;
960 struct kvm_userspace_memory_region kvm_userspace_mem;
962 r = -EFAULT;
963 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
964 goto out;
965 kvm_userspace_mem.slot = kvm_mem.slot;
966 kvm_userspace_mem.flags = kvm_mem.flags;
967 kvm_userspace_mem.guest_phys_addr =
968 kvm_mem.guest_phys_addr;
969 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
970 r = kvm_vm_ioctl_set_memory_region(kvm,
971 &kvm_userspace_mem, 0);
972 if (r)
973 goto out;
974 break;
976 case KVM_CREATE_IRQCHIP:
977 r = -EFAULT;
978 r = kvm_ioapic_init(kvm);
979 if (r)
980 goto out;
981 r = kvm_setup_default_irq_routing(kvm);
982 if (r) {
983 kfree(kvm->arch.vioapic);
984 goto out;
986 break;
987 case KVM_IRQ_LINE_STATUS:
988 case KVM_IRQ_LINE: {
989 struct kvm_irq_level irq_event;
991 r = -EFAULT;
992 if (copy_from_user(&irq_event, argp, sizeof irq_event))
993 goto out;
994 if (irqchip_in_kernel(kvm)) {
995 __s32 status;
996 mutex_lock(&kvm->lock);
997 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
998 irq_event.irq, irq_event.level);
999 mutex_unlock(&kvm->lock);
1000 if (ioctl == KVM_IRQ_LINE_STATUS) {
1001 irq_event.status = status;
1002 if (copy_to_user(argp, &irq_event,
1003 sizeof irq_event))
1004 goto out;
1006 r = 0;
1008 break;
1010 case KVM_GET_IRQCHIP: {
1011 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1012 struct kvm_irqchip chip;
1014 r = -EFAULT;
1015 if (copy_from_user(&chip, argp, sizeof chip))
1016 goto out;
1017 r = -ENXIO;
1018 if (!irqchip_in_kernel(kvm))
1019 goto out;
1020 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1021 if (r)
1022 goto out;
1023 r = -EFAULT;
1024 if (copy_to_user(argp, &chip, sizeof chip))
1025 goto out;
1026 r = 0;
1027 break;
1029 case KVM_SET_IRQCHIP: {
1030 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1031 struct kvm_irqchip chip;
1033 r = -EFAULT;
1034 if (copy_from_user(&chip, argp, sizeof chip))
1035 goto out;
1036 r = -ENXIO;
1037 if (!irqchip_in_kernel(kvm))
1038 goto out;
1039 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1040 if (r)
1041 goto out;
1042 r = 0;
1043 break;
1045 default:
1048 out:
1049 return r;
1052 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1053 struct kvm_sregs *sregs)
1055 return -EINVAL;
1058 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1059 struct kvm_sregs *sregs)
1061 return -EINVAL;
1064 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1065 struct kvm_translation *tr)
1068 return -EINVAL;
1071 static int kvm_alloc_vmm_area(void)
1073 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1074 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1075 get_order(KVM_VMM_SIZE));
1076 if (!kvm_vmm_base)
1077 return -ENOMEM;
1079 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1080 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1082 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1083 kvm_vmm_base, kvm_vm_buffer);
1086 return 0;
1089 static void kvm_free_vmm_area(void)
1091 if (kvm_vmm_base) {
1092 /*Zero this area before free to avoid bits leak!!*/
1093 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1094 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1095 kvm_vmm_base = 0;
1096 kvm_vm_buffer = 0;
1097 kvm_vsa_base = 0;
1101 static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1105 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1107 int i;
1108 union cpuid3_t cpuid3;
1109 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1111 if (IS_ERR(vpd))
1112 return PTR_ERR(vpd);
1114 /* CPUID init */
1115 for (i = 0; i < 5; i++)
1116 vpd->vcpuid[i] = ia64_get_cpuid(i);
1118 /* Limit the CPUID number to 5 */
1119 cpuid3.value = vpd->vcpuid[3];
1120 cpuid3.number = 4; /* 5 - 1 */
1121 vpd->vcpuid[3] = cpuid3.value;
1123 /*Set vac and vdc fields*/
1124 vpd->vac.a_from_int_cr = 1;
1125 vpd->vac.a_to_int_cr = 1;
1126 vpd->vac.a_from_psr = 1;
1127 vpd->vac.a_from_cpuid = 1;
1128 vpd->vac.a_cover = 1;
1129 vpd->vac.a_bsw = 1;
1130 vpd->vac.a_int = 1;
1131 vpd->vdc.d_vmsw = 1;
1133 /*Set virtual buffer*/
1134 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1136 return 0;
1139 static int vti_create_vp(struct kvm_vcpu *vcpu)
1141 long ret;
1142 struct vpd *vpd = vcpu->arch.vpd;
1143 unsigned long vmm_ivt;
1145 vmm_ivt = kvm_vmm_info->vmm_ivt;
1147 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1149 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1151 if (ret) {
1152 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1153 return -EINVAL;
1155 return 0;
1158 static void init_ptce_info(struct kvm_vcpu *vcpu)
1160 ia64_ptce_info_t ptce = {0};
1162 ia64_get_ptce(&ptce);
1163 vcpu->arch.ptce_base = ptce.base;
1164 vcpu->arch.ptce_count[0] = ptce.count[0];
1165 vcpu->arch.ptce_count[1] = ptce.count[1];
1166 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1167 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1170 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1172 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1174 if (hrtimer_cancel(p_ht))
1175 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
1178 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1180 struct kvm_vcpu *vcpu;
1181 wait_queue_head_t *q;
1183 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1184 q = &vcpu->wq;
1186 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1187 goto out;
1189 if (waitqueue_active(q))
1190 wake_up_interruptible(q);
1192 out:
1193 vcpu->arch.timer_fired = 1;
1194 vcpu->arch.timer_check = 1;
1195 return HRTIMER_NORESTART;
1198 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1200 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1202 struct kvm_vcpu *v;
1203 int r;
1204 int i;
1205 long itc_offset;
1206 struct kvm *kvm = vcpu->kvm;
1207 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1209 union context *p_ctx = &vcpu->arch.guest;
1210 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1212 /*Init vcpu context for first run.*/
1213 if (IS_ERR(vmm_vcpu))
1214 return PTR_ERR(vmm_vcpu);
1216 if (vcpu->vcpu_id == 0) {
1217 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1219 /*Set entry address for first run.*/
1220 regs->cr_iip = PALE_RESET_ENTRY;
1222 /*Initialize itc offset for vcpus*/
1223 itc_offset = 0UL - kvm_get_itc(vcpu);
1224 for (i = 0; i < kvm->arch.online_vcpus; i++) {
1225 v = (struct kvm_vcpu *)((char *)vcpu +
1226 sizeof(struct kvm_vcpu_data) * i);
1227 v->arch.itc_offset = itc_offset;
1228 v->arch.last_itc = 0;
1230 } else
1231 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1233 r = -ENOMEM;
1234 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1235 if (!vcpu->arch.apic)
1236 goto out;
1237 vcpu->arch.apic->vcpu = vcpu;
1239 p_ctx->gr[1] = 0;
1240 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
1241 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1242 p_ctx->psr = 0x1008522000UL;
1243 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1244 p_ctx->caller_unat = 0;
1245 p_ctx->pr = 0x0;
1246 p_ctx->ar[36] = 0x0; /*unat*/
1247 p_ctx->ar[19] = 0x0; /*rnat*/
1248 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1249 ((sizeof(struct kvm_vcpu)+15) & ~15);
1250 p_ctx->ar[64] = 0x0; /*pfs*/
1251 p_ctx->cr[0] = 0x7e04UL;
1252 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1253 p_ctx->cr[8] = 0x3c;
1255 /*Initilize region register*/
1256 p_ctx->rr[0] = 0x30;
1257 p_ctx->rr[1] = 0x30;
1258 p_ctx->rr[2] = 0x30;
1259 p_ctx->rr[3] = 0x30;
1260 p_ctx->rr[4] = 0x30;
1261 p_ctx->rr[5] = 0x30;
1262 p_ctx->rr[7] = 0x30;
1264 /*Initilize branch register 0*/
1265 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1267 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1268 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1269 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1271 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1272 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1274 vcpu->arch.last_run_cpu = -1;
1275 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
1276 vcpu->arch.vsa_base = kvm_vsa_base;
1277 vcpu->arch.__gp = kvm_vmm_gp;
1278 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1279 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1280 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
1281 init_ptce_info(vcpu);
1283 r = 0;
1284 out:
1285 return r;
1288 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1290 unsigned long psr;
1291 int r;
1293 local_irq_save(psr);
1294 r = kvm_insert_vmm_mapping(vcpu);
1295 if (r)
1296 goto fail;
1297 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1298 if (r)
1299 goto fail;
1301 r = vti_init_vpd(vcpu);
1302 if (r) {
1303 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1304 goto uninit;
1307 r = vti_create_vp(vcpu);
1308 if (r)
1309 goto uninit;
1311 kvm_purge_vmm_mapping(vcpu);
1312 local_irq_restore(psr);
1314 return 0;
1315 uninit:
1316 kvm_vcpu_uninit(vcpu);
1317 fail:
1318 local_irq_restore(psr);
1319 return r;
1322 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1323 unsigned int id)
1325 struct kvm_vcpu *vcpu;
1326 unsigned long vm_base = kvm->arch.vm_base;
1327 int r;
1328 int cpu;
1330 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1332 r = -EINVAL;
1333 if (id >= KVM_MAX_VCPUS) {
1334 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1335 KVM_MAX_VCPUS);
1336 goto fail;
1339 r = -ENOMEM;
1340 if (!vm_base) {
1341 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1342 goto fail;
1344 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1345 vcpu_data[id].vcpu_struct));
1346 vcpu->kvm = kvm;
1348 cpu = get_cpu();
1349 vti_vcpu_load(vcpu, cpu);
1350 r = vti_vcpu_setup(vcpu, id);
1351 put_cpu();
1353 if (r) {
1354 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1355 goto fail;
1358 kvm->arch.online_vcpus++;
1360 return vcpu;
1361 fail:
1362 return ERR_PTR(r);
1365 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1367 return 0;
1370 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1372 return -EINVAL;
1375 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1377 return -EINVAL;
1380 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1381 struct kvm_guest_debug *dbg)
1383 return -EINVAL;
1386 static void free_kvm(struct kvm *kvm)
1388 unsigned long vm_base = kvm->arch.vm_base;
1390 if (vm_base) {
1391 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1392 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1397 static void kvm_release_vm_pages(struct kvm *kvm)
1399 struct kvm_memory_slot *memslot;
1400 int i, j;
1401 unsigned long base_gfn;
1403 for (i = 0; i < kvm->nmemslots; i++) {
1404 memslot = &kvm->memslots[i];
1405 base_gfn = memslot->base_gfn;
1407 for (j = 0; j < memslot->npages; j++) {
1408 if (memslot->rmap[j])
1409 put_page((struct page *)memslot->rmap[j]);
1414 void kvm_arch_sync_events(struct kvm *kvm)
1418 void kvm_arch_destroy_vm(struct kvm *kvm)
1420 kvm_iommu_unmap_guest(kvm);
1421 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1422 kvm_free_all_assigned_devices(kvm);
1423 #endif
1424 kfree(kvm->arch.vioapic);
1425 kvm_release_vm_pages(kvm);
1426 kvm_free_physmem(kvm);
1427 free_kvm(kvm);
1430 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1434 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1436 if (cpu != vcpu->cpu) {
1437 vcpu->cpu = cpu;
1438 if (vcpu->arch.ht_active)
1439 kvm_migrate_hlt_timer(vcpu);
1443 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1445 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1447 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1448 int i;
1450 vcpu_load(vcpu);
1452 for (i = 0; i < 16; i++) {
1453 regs->vpd.vgr[i] = vpd->vgr[i];
1454 regs->vpd.vbgr[i] = vpd->vbgr[i];
1456 for (i = 0; i < 128; i++)
1457 regs->vpd.vcr[i] = vpd->vcr[i];
1458 regs->vpd.vhpi = vpd->vhpi;
1459 regs->vpd.vnat = vpd->vnat;
1460 regs->vpd.vbnat = vpd->vbnat;
1461 regs->vpd.vpsr = vpd->vpsr;
1462 regs->vpd.vpr = vpd->vpr;
1464 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1466 SAVE_REGS(mp_state);
1467 SAVE_REGS(vmm_rr);
1468 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1469 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1470 SAVE_REGS(itr_regions);
1471 SAVE_REGS(dtr_regions);
1472 SAVE_REGS(tc_regions);
1473 SAVE_REGS(irq_check);
1474 SAVE_REGS(itc_check);
1475 SAVE_REGS(timer_check);
1476 SAVE_REGS(timer_pending);
1477 SAVE_REGS(last_itc);
1478 for (i = 0; i < 8; i++) {
1479 regs->vrr[i] = vcpu->arch.vrr[i];
1480 regs->ibr[i] = vcpu->arch.ibr[i];
1481 regs->dbr[i] = vcpu->arch.dbr[i];
1483 for (i = 0; i < 4; i++)
1484 regs->insvc[i] = vcpu->arch.insvc[i];
1485 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
1486 SAVE_REGS(xtp);
1487 SAVE_REGS(metaphysical_rr0);
1488 SAVE_REGS(metaphysical_rr4);
1489 SAVE_REGS(metaphysical_saved_rr0);
1490 SAVE_REGS(metaphysical_saved_rr4);
1491 SAVE_REGS(fp_psr);
1492 SAVE_REGS(saved_gp);
1494 vcpu_put(vcpu);
1495 return 0;
1498 int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1499 struct kvm_ia64_vcpu_stack *stack)
1501 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1502 return 0;
1505 int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1506 struct kvm_ia64_vcpu_stack *stack)
1508 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1509 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1511 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1512 return 0;
1515 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1518 hrtimer_cancel(&vcpu->arch.hlt_timer);
1519 kfree(vcpu->arch.apic);
1523 long kvm_arch_vcpu_ioctl(struct file *filp,
1524 unsigned int ioctl, unsigned long arg)
1526 struct kvm_vcpu *vcpu = filp->private_data;
1527 void __user *argp = (void __user *)arg;
1528 struct kvm_ia64_vcpu_stack *stack = NULL;
1529 long r;
1531 switch (ioctl) {
1532 case KVM_IA64_VCPU_GET_STACK: {
1533 struct kvm_ia64_vcpu_stack __user *user_stack;
1534 void __user *first_p = argp;
1536 r = -EFAULT;
1537 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1538 goto out;
1540 if (!access_ok(VERIFY_WRITE, user_stack,
1541 sizeof(struct kvm_ia64_vcpu_stack))) {
1542 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1543 "Illegal user destination address for stack\n");
1544 goto out;
1546 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1547 if (!stack) {
1548 r = -ENOMEM;
1549 goto out;
1552 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1553 if (r)
1554 goto out;
1556 if (copy_to_user(user_stack, stack,
1557 sizeof(struct kvm_ia64_vcpu_stack)))
1558 goto out;
1560 break;
1562 case KVM_IA64_VCPU_SET_STACK: {
1563 struct kvm_ia64_vcpu_stack __user *user_stack;
1564 void __user *first_p = argp;
1566 r = -EFAULT;
1567 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1568 goto out;
1570 if (!access_ok(VERIFY_READ, user_stack,
1571 sizeof(struct kvm_ia64_vcpu_stack))) {
1572 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1573 "Illegal user address for stack\n");
1574 goto out;
1576 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1577 if (!stack) {
1578 r = -ENOMEM;
1579 goto out;
1581 if (copy_from_user(stack, user_stack,
1582 sizeof(struct kvm_ia64_vcpu_stack)))
1583 goto out;
1585 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1586 break;
1589 default:
1590 r = -EINVAL;
1593 out:
1594 kfree(stack);
1595 return r;
1598 int kvm_arch_set_memory_region(struct kvm *kvm,
1599 struct kvm_userspace_memory_region *mem,
1600 struct kvm_memory_slot old,
1601 int user_alloc)
1603 unsigned long i;
1604 unsigned long pfn;
1605 int npages = mem->memory_size >> PAGE_SHIFT;
1606 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1607 unsigned long base_gfn = memslot->base_gfn;
1609 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1610 return -ENOMEM;
1612 for (i = 0; i < npages; i++) {
1613 pfn = gfn_to_pfn(kvm, base_gfn + i);
1614 if (!kvm_is_mmio_pfn(pfn)) {
1615 kvm_set_pmt_entry(kvm, base_gfn + i,
1616 pfn << PAGE_SHIFT,
1617 _PAGE_AR_RWX | _PAGE_MA_WB);
1618 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1619 } else {
1620 kvm_set_pmt_entry(kvm, base_gfn + i,
1621 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1622 _PAGE_MA_UC);
1623 memslot->rmap[i] = 0;
1627 return 0;
1630 void kvm_arch_flush_shadow(struct kvm *kvm)
1634 long kvm_arch_dev_ioctl(struct file *filp,
1635 unsigned int ioctl, unsigned long arg)
1637 return -EINVAL;
1640 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1642 kvm_vcpu_uninit(vcpu);
1645 static int vti_cpu_has_kvm_support(void)
1647 long avail = 1, status = 1, control = 1;
1648 long ret;
1650 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1651 if (ret)
1652 goto out;
1654 if (!(avail & PAL_PROC_VM_BIT))
1655 goto out;
1657 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1659 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1660 if (ret)
1661 goto out;
1662 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1664 if (!(vp_env_info & VP_OPCODE)) {
1665 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1666 "vm_env_info:0x%lx\n", vp_env_info);
1669 return 1;
1670 out:
1671 return 0;
1676 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1677 * SN2 RTC, replacing the ITC based default verion.
1679 static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1680 struct module *module)
1682 unsigned long new_ar, new_ar_sn2;
1683 unsigned long module_base;
1685 if (!ia64_platform_is("sn2"))
1686 return;
1688 module_base = (unsigned long)module->module_core;
1690 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1691 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1693 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1694 "as source\n");
1697 * Copy the SN2 version of mov_ar into place. They are both
1698 * the same size, so 6 bundles is sufficient (6 * 0x10).
1700 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1703 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1704 struct module *module)
1706 unsigned long module_base;
1707 unsigned long vmm_size;
1709 unsigned long vmm_offset, func_offset, fdesc_offset;
1710 struct fdesc *p_fdesc;
1712 BUG_ON(!module);
1714 if (!kvm_vmm_base) {
1715 printk("kvm: kvm area hasn't been initilized yet!!\n");
1716 return -EFAULT;
1719 /*Calculate new position of relocated vmm module.*/
1720 module_base = (unsigned long)module->module_core;
1721 vmm_size = module->core_size;
1722 if (unlikely(vmm_size > KVM_VMM_SIZE))
1723 return -EFAULT;
1725 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1726 kvm_patch_vmm(vmm_info, module);
1727 kvm_flush_icache(kvm_vmm_base, vmm_size);
1729 /*Recalculate kvm_vmm_info based on new VMM*/
1730 vmm_offset = vmm_info->vmm_ivt - module_base;
1731 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1732 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1733 kvm_vmm_info->vmm_ivt);
1735 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1736 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1737 fdesc_offset);
1738 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1739 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1740 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1741 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1743 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1744 KVM_VMM_BASE+func_offset);
1746 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1747 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1748 fdesc_offset);
1749 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1750 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1751 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1752 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1754 kvm_vmm_gp = p_fdesc->gp;
1756 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1757 kvm_vmm_info->vmm_entry);
1758 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1759 KVM_VMM_BASE + func_offset);
1761 return 0;
1764 int kvm_arch_init(void *opaque)
1766 int r;
1767 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1769 if (!vti_cpu_has_kvm_support()) {
1770 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1771 r = -EOPNOTSUPP;
1772 goto out;
1775 if (kvm_vmm_info) {
1776 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1777 r = -EEXIST;
1778 goto out;
1781 r = -ENOMEM;
1782 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1783 if (!kvm_vmm_info)
1784 goto out;
1786 if (kvm_alloc_vmm_area())
1787 goto out_free0;
1789 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1790 if (r)
1791 goto out_free1;
1793 return 0;
1795 out_free1:
1796 kvm_free_vmm_area();
1797 out_free0:
1798 kfree(kvm_vmm_info);
1799 out:
1800 return r;
1803 void kvm_arch_exit(void)
1805 kvm_free_vmm_area();
1806 kfree(kvm_vmm_info);
1807 kvm_vmm_info = NULL;
1810 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1811 struct kvm_dirty_log *log)
1813 struct kvm_memory_slot *memslot;
1814 int r, i;
1815 long n, base;
1816 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1817 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
1819 r = -EINVAL;
1820 if (log->slot >= KVM_MEMORY_SLOTS)
1821 goto out;
1823 memslot = &kvm->memslots[log->slot];
1824 r = -ENOENT;
1825 if (!memslot->dirty_bitmap)
1826 goto out;
1828 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1829 base = memslot->base_gfn / BITS_PER_LONG;
1831 for (i = 0; i < n/sizeof(long); ++i) {
1832 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1833 dirty_bitmap[base + i] = 0;
1835 r = 0;
1836 out:
1837 return r;
1840 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1841 struct kvm_dirty_log *log)
1843 int r;
1844 int n;
1845 struct kvm_memory_slot *memslot;
1846 int is_dirty = 0;
1848 spin_lock(&kvm->arch.dirty_log_lock);
1850 r = kvm_ia64_sync_dirty_log(kvm, log);
1851 if (r)
1852 goto out;
1854 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1855 if (r)
1856 goto out;
1858 /* If nothing is dirty, don't bother messing with page tables. */
1859 if (is_dirty) {
1860 kvm_flush_remote_tlbs(kvm);
1861 memslot = &kvm->memslots[log->slot];
1862 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1863 memset(memslot->dirty_bitmap, 0, n);
1865 r = 0;
1866 out:
1867 spin_unlock(&kvm->arch.dirty_log_lock);
1868 return r;
1871 int kvm_arch_hardware_setup(void)
1873 return 0;
1876 void kvm_arch_hardware_unsetup(void)
1880 static void vcpu_kick_intr(void *info)
1882 #ifdef DEBUG
1883 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1884 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1885 #endif
1888 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1890 int ipi_pcpu = vcpu->cpu;
1891 int cpu = get_cpu();
1893 if (waitqueue_active(&vcpu->wq))
1894 wake_up_interruptible(&vcpu->wq);
1896 if (vcpu->guest_mode && cpu != ipi_pcpu)
1897 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1898 put_cpu();
1901 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
1903 return __apic_accept_irq(vcpu, irq->vector);
1906 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1908 return apic->vcpu->vcpu_id == dest;
1911 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1913 return 0;
1916 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
1918 return vcpu1->arch.xtp - vcpu2->arch.xtp;
1921 int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1922 int short_hand, int dest, int dest_mode)
1924 struct kvm_lapic *target = vcpu->arch.apic;
1925 return (dest_mode == 0) ?
1926 kvm_apic_match_physical_addr(target, dest) :
1927 kvm_apic_match_logical_addr(target, dest);
1930 static int find_highest_bits(int *dat)
1932 u32 bits, bitnum;
1933 int i;
1935 /* loop for all 256 bits */
1936 for (i = 7; i >= 0 ; i--) {
1937 bits = dat[i];
1938 if (bits) {
1939 bitnum = fls(bits);
1940 return i * 32 + bitnum - 1;
1944 return -1;
1947 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1949 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1951 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1952 return NMI_VECTOR;
1953 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1954 return ExtINT_VECTOR;
1956 return find_highest_bits((int *)&vpd->irr[0]);
1959 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1961 if (kvm_highest_pending_irq(vcpu) != -1)
1962 return 1;
1963 return 0;
1966 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1968 return vcpu->arch.timer_fired;
1971 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1973 return gfn;
1976 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1978 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1981 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1982 struct kvm_mp_state *mp_state)
1984 vcpu_load(vcpu);
1985 mp_state->mp_state = vcpu->arch.mp_state;
1986 vcpu_put(vcpu);
1987 return 0;
1990 static int vcpu_reset(struct kvm_vcpu *vcpu)
1992 int r;
1993 long psr;
1994 local_irq_save(psr);
1995 r = kvm_insert_vmm_mapping(vcpu);
1996 if (r)
1997 goto fail;
1999 vcpu->arch.launched = 0;
2000 kvm_arch_vcpu_uninit(vcpu);
2001 r = kvm_arch_vcpu_init(vcpu);
2002 if (r)
2003 goto fail;
2005 kvm_purge_vmm_mapping(vcpu);
2006 r = 0;
2007 fail:
2008 local_irq_restore(psr);
2009 return r;
2012 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2013 struct kvm_mp_state *mp_state)
2015 int r = 0;
2017 vcpu_load(vcpu);
2018 vcpu->arch.mp_state = mp_state->mp_state;
2019 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
2020 r = vcpu_reset(vcpu);
2021 vcpu_put(vcpu);
2022 return r;