KVM: MMU: Adjust pte accessors to explicitly indicate guest or shadow pte
[linux/fpc-iii.git] / arch / ia64 / kvm / kvm-ia64.c
blob8dde36953af3b00081a18d0c90f0a1d197dd052d
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 unsigned long psr;
623 int r;
624 int cpu = smp_processor_id();
626 if (vcpu->arch.last_run_cpu != cpu ||
627 per_cpu(last_vcpu, cpu) != vcpu) {
628 per_cpu(last_vcpu, cpu) = vcpu;
629 vcpu->arch.last_run_cpu = cpu;
630 kvm_flush_tlb_all();
633 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
634 vti_set_rr6(vcpu->arch.vmm_rr);
635 local_irq_save(psr);
636 r = kvm_insert_vmm_mapping(vcpu);
637 local_irq_restore(psr);
638 return r;
641 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
643 kvm_purge_vmm_mapping(vcpu);
644 vti_set_rr6(vcpu->arch.host_rr6);
647 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
649 union context *host_ctx, *guest_ctx;
650 int r;
653 * down_read() may sleep and return with interrupts enabled
655 down_read(&vcpu->kvm->slots_lock);
657 again:
658 if (signal_pending(current)) {
659 r = -EINTR;
660 kvm_run->exit_reason = KVM_EXIT_INTR;
661 goto out;
664 preempt_disable();
665 local_irq_disable();
667 /*Get host and guest context with guest address space.*/
668 host_ctx = kvm_get_host_context(vcpu);
669 guest_ctx = kvm_get_guest_context(vcpu);
671 clear_bit(KVM_REQ_KICK, &vcpu->requests);
673 r = kvm_vcpu_pre_transition(vcpu);
674 if (r < 0)
675 goto vcpu_run_fail;
677 up_read(&vcpu->kvm->slots_lock);
678 kvm_guest_enter();
681 * Transition to the guest
683 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
685 kvm_vcpu_post_transition(vcpu);
687 vcpu->arch.launched = 1;
688 set_bit(KVM_REQ_KICK, &vcpu->requests);
689 local_irq_enable();
692 * We must have an instruction between local_irq_enable() and
693 * kvm_guest_exit(), so the timer interrupt isn't delayed by
694 * the interrupt shadow. The stat.exits increment will do nicely.
695 * But we need to prevent reordering, hence this barrier():
697 barrier();
698 kvm_guest_exit();
699 preempt_enable();
701 down_read(&vcpu->kvm->slots_lock);
703 r = kvm_handle_exit(kvm_run, vcpu);
705 if (r > 0) {
706 if (!need_resched())
707 goto again;
710 out:
711 up_read(&vcpu->kvm->slots_lock);
712 if (r > 0) {
713 kvm_resched(vcpu);
714 down_read(&vcpu->kvm->slots_lock);
715 goto again;
718 return r;
720 vcpu_run_fail:
721 local_irq_enable();
722 preempt_enable();
723 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
724 goto out;
727 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
729 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
731 if (!vcpu->mmio_is_write)
732 memcpy(&p->data, vcpu->mmio_data, 8);
733 p->state = STATE_IORESP_READY;
736 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
738 int r;
739 sigset_t sigsaved;
741 vcpu_load(vcpu);
743 if (vcpu->sigset_active)
744 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
746 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
747 kvm_vcpu_block(vcpu);
748 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
749 r = -EAGAIN;
750 goto out;
753 if (vcpu->mmio_needed) {
754 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
755 kvm_set_mmio_data(vcpu);
756 vcpu->mmio_read_completed = 1;
757 vcpu->mmio_needed = 0;
759 r = __vcpu_run(vcpu, kvm_run);
760 out:
761 if (vcpu->sigset_active)
762 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
764 vcpu_put(vcpu);
765 return r;
768 static struct kvm *kvm_alloc_kvm(void)
771 struct kvm *kvm;
772 uint64_t vm_base;
774 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
776 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
778 if (!vm_base)
779 return ERR_PTR(-ENOMEM);
781 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
782 kvm = (struct kvm *)(vm_base +
783 offsetof(struct kvm_vm_data, kvm_vm_struct));
784 kvm->arch.vm_base = vm_base;
785 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
787 return kvm;
790 struct kvm_io_range {
791 unsigned long start;
792 unsigned long size;
793 unsigned long type;
796 static const struct kvm_io_range io_ranges[] = {
797 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
798 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
799 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
800 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
801 {PIB_START, PIB_SIZE, GPFN_PIB},
804 static void kvm_build_io_pmt(struct kvm *kvm)
806 unsigned long i, j;
808 /* Mark I/O ranges */
809 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
810 i++) {
811 for (j = io_ranges[i].start;
812 j < io_ranges[i].start + io_ranges[i].size;
813 j += PAGE_SIZE)
814 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
815 io_ranges[i].type, 0);
820 /*Use unused rids to virtualize guest rid.*/
821 #define GUEST_PHYSICAL_RR0 0x1739
822 #define GUEST_PHYSICAL_RR4 0x2739
823 #define VMM_INIT_RR 0x1660
825 static void kvm_init_vm(struct kvm *kvm)
827 BUG_ON(!kvm);
829 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
830 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
831 kvm->arch.vmm_init_rr = VMM_INIT_RR;
834 *Fill P2M entries for MMIO/IO ranges
836 kvm_build_io_pmt(kvm);
838 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
840 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
841 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
844 struct kvm *kvm_arch_create_vm(void)
846 struct kvm *kvm = kvm_alloc_kvm();
848 if (IS_ERR(kvm))
849 return ERR_PTR(-ENOMEM);
851 kvm->arch.is_sn2 = ia64_platform_is("sn2");
853 kvm_init_vm(kvm);
855 kvm->arch.online_vcpus = 0;
857 return kvm;
861 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
862 struct kvm_irqchip *chip)
864 int r;
866 r = 0;
867 switch (chip->chip_id) {
868 case KVM_IRQCHIP_IOAPIC:
869 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
870 sizeof(struct kvm_ioapic_state));
871 break;
872 default:
873 r = -EINVAL;
874 break;
876 return r;
879 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
881 int r;
883 r = 0;
884 switch (chip->chip_id) {
885 case KVM_IRQCHIP_IOAPIC:
886 memcpy(ioapic_irqchip(kvm),
887 &chip->chip.ioapic,
888 sizeof(struct kvm_ioapic_state));
889 break;
890 default:
891 r = -EINVAL;
892 break;
894 return r;
897 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
899 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
901 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
902 int i;
904 vcpu_load(vcpu);
906 for (i = 0; i < 16; i++) {
907 vpd->vgr[i] = regs->vpd.vgr[i];
908 vpd->vbgr[i] = regs->vpd.vbgr[i];
910 for (i = 0; i < 128; i++)
911 vpd->vcr[i] = regs->vpd.vcr[i];
912 vpd->vhpi = regs->vpd.vhpi;
913 vpd->vnat = regs->vpd.vnat;
914 vpd->vbnat = regs->vpd.vbnat;
915 vpd->vpsr = regs->vpd.vpsr;
917 vpd->vpr = regs->vpd.vpr;
919 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
921 RESTORE_REGS(mp_state);
922 RESTORE_REGS(vmm_rr);
923 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
924 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
925 RESTORE_REGS(itr_regions);
926 RESTORE_REGS(dtr_regions);
927 RESTORE_REGS(tc_regions);
928 RESTORE_REGS(irq_check);
929 RESTORE_REGS(itc_check);
930 RESTORE_REGS(timer_check);
931 RESTORE_REGS(timer_pending);
932 RESTORE_REGS(last_itc);
933 for (i = 0; i < 8; i++) {
934 vcpu->arch.vrr[i] = regs->vrr[i];
935 vcpu->arch.ibr[i] = regs->ibr[i];
936 vcpu->arch.dbr[i] = regs->dbr[i];
938 for (i = 0; i < 4; i++)
939 vcpu->arch.insvc[i] = regs->insvc[i];
940 RESTORE_REGS(xtp);
941 RESTORE_REGS(metaphysical_rr0);
942 RESTORE_REGS(metaphysical_rr4);
943 RESTORE_REGS(metaphysical_saved_rr0);
944 RESTORE_REGS(metaphysical_saved_rr4);
945 RESTORE_REGS(fp_psr);
946 RESTORE_REGS(saved_gp);
948 vcpu->arch.irq_new_pending = 1;
949 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
950 set_bit(KVM_REQ_RESUME, &vcpu->requests);
952 vcpu_put(vcpu);
954 return 0;
957 long kvm_arch_vm_ioctl(struct file *filp,
958 unsigned int ioctl, unsigned long arg)
960 struct kvm *kvm = filp->private_data;
961 void __user *argp = (void __user *)arg;
962 int r = -EINVAL;
964 switch (ioctl) {
965 case KVM_SET_MEMORY_REGION: {
966 struct kvm_memory_region kvm_mem;
967 struct kvm_userspace_memory_region kvm_userspace_mem;
969 r = -EFAULT;
970 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
971 goto out;
972 kvm_userspace_mem.slot = kvm_mem.slot;
973 kvm_userspace_mem.flags = kvm_mem.flags;
974 kvm_userspace_mem.guest_phys_addr =
975 kvm_mem.guest_phys_addr;
976 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
977 r = kvm_vm_ioctl_set_memory_region(kvm,
978 &kvm_userspace_mem, 0);
979 if (r)
980 goto out;
981 break;
983 case KVM_CREATE_IRQCHIP:
984 r = -EFAULT;
985 r = kvm_ioapic_init(kvm);
986 if (r)
987 goto out;
988 r = kvm_setup_default_irq_routing(kvm);
989 if (r) {
990 kfree(kvm->arch.vioapic);
991 goto out;
993 break;
994 case KVM_IRQ_LINE_STATUS:
995 case KVM_IRQ_LINE: {
996 struct kvm_irq_level irq_event;
998 r = -EFAULT;
999 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1000 goto out;
1001 if (irqchip_in_kernel(kvm)) {
1002 __s32 status;
1003 mutex_lock(&kvm->irq_lock);
1004 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1005 irq_event.irq, irq_event.level);
1006 mutex_unlock(&kvm->irq_lock);
1007 if (ioctl == KVM_IRQ_LINE_STATUS) {
1008 irq_event.status = status;
1009 if (copy_to_user(argp, &irq_event,
1010 sizeof irq_event))
1011 goto out;
1013 r = 0;
1015 break;
1017 case KVM_GET_IRQCHIP: {
1018 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1019 struct kvm_irqchip chip;
1021 r = -EFAULT;
1022 if (copy_from_user(&chip, argp, sizeof chip))
1023 goto out;
1024 r = -ENXIO;
1025 if (!irqchip_in_kernel(kvm))
1026 goto out;
1027 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1028 if (r)
1029 goto out;
1030 r = -EFAULT;
1031 if (copy_to_user(argp, &chip, sizeof chip))
1032 goto out;
1033 r = 0;
1034 break;
1036 case KVM_SET_IRQCHIP: {
1037 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1038 struct kvm_irqchip chip;
1040 r = -EFAULT;
1041 if (copy_from_user(&chip, argp, sizeof chip))
1042 goto out;
1043 r = -ENXIO;
1044 if (!irqchip_in_kernel(kvm))
1045 goto out;
1046 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1047 if (r)
1048 goto out;
1049 r = 0;
1050 break;
1052 default:
1055 out:
1056 return r;
1059 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1060 struct kvm_sregs *sregs)
1062 return -EINVAL;
1065 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1066 struct kvm_sregs *sregs)
1068 return -EINVAL;
1071 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1072 struct kvm_translation *tr)
1075 return -EINVAL;
1078 static int kvm_alloc_vmm_area(void)
1080 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1081 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1082 get_order(KVM_VMM_SIZE));
1083 if (!kvm_vmm_base)
1084 return -ENOMEM;
1086 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1087 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1089 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1090 kvm_vmm_base, kvm_vm_buffer);
1093 return 0;
1096 static void kvm_free_vmm_area(void)
1098 if (kvm_vmm_base) {
1099 /*Zero this area before free to avoid bits leak!!*/
1100 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1101 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1102 kvm_vmm_base = 0;
1103 kvm_vm_buffer = 0;
1104 kvm_vsa_base = 0;
1108 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1110 int i;
1111 union cpuid3_t cpuid3;
1112 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1114 if (IS_ERR(vpd))
1115 return PTR_ERR(vpd);
1117 /* CPUID init */
1118 for (i = 0; i < 5; i++)
1119 vpd->vcpuid[i] = ia64_get_cpuid(i);
1121 /* Limit the CPUID number to 5 */
1122 cpuid3.value = vpd->vcpuid[3];
1123 cpuid3.number = 4; /* 5 - 1 */
1124 vpd->vcpuid[3] = cpuid3.value;
1126 /*Set vac and vdc fields*/
1127 vpd->vac.a_from_int_cr = 1;
1128 vpd->vac.a_to_int_cr = 1;
1129 vpd->vac.a_from_psr = 1;
1130 vpd->vac.a_from_cpuid = 1;
1131 vpd->vac.a_cover = 1;
1132 vpd->vac.a_bsw = 1;
1133 vpd->vac.a_int = 1;
1134 vpd->vdc.d_vmsw = 1;
1136 /*Set virtual buffer*/
1137 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1139 return 0;
1142 static int vti_create_vp(struct kvm_vcpu *vcpu)
1144 long ret;
1145 struct vpd *vpd = vcpu->arch.vpd;
1146 unsigned long vmm_ivt;
1148 vmm_ivt = kvm_vmm_info->vmm_ivt;
1150 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1152 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1154 if (ret) {
1155 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1156 return -EINVAL;
1158 return 0;
1161 static void init_ptce_info(struct kvm_vcpu *vcpu)
1163 ia64_ptce_info_t ptce = {0};
1165 ia64_get_ptce(&ptce);
1166 vcpu->arch.ptce_base = ptce.base;
1167 vcpu->arch.ptce_count[0] = ptce.count[0];
1168 vcpu->arch.ptce_count[1] = ptce.count[1];
1169 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1170 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1173 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1175 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1177 if (hrtimer_cancel(p_ht))
1178 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
1181 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1183 struct kvm_vcpu *vcpu;
1184 wait_queue_head_t *q;
1186 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1187 q = &vcpu->wq;
1189 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1190 goto out;
1192 if (waitqueue_active(q))
1193 wake_up_interruptible(q);
1195 out:
1196 vcpu->arch.timer_fired = 1;
1197 vcpu->arch.timer_check = 1;
1198 return HRTIMER_NORESTART;
1201 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1203 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1205 struct kvm_vcpu *v;
1206 int r;
1207 int i;
1208 long itc_offset;
1209 struct kvm *kvm = vcpu->kvm;
1210 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1212 union context *p_ctx = &vcpu->arch.guest;
1213 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1215 /*Init vcpu context for first run.*/
1216 if (IS_ERR(vmm_vcpu))
1217 return PTR_ERR(vmm_vcpu);
1219 if (vcpu->vcpu_id == 0) {
1220 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1222 /*Set entry address for first run.*/
1223 regs->cr_iip = PALE_RESET_ENTRY;
1225 /*Initialize itc offset for vcpus*/
1226 itc_offset = 0UL - kvm_get_itc(vcpu);
1227 for (i = 0; i < KVM_MAX_VCPUS; i++) {
1228 v = (struct kvm_vcpu *)((char *)vcpu +
1229 sizeof(struct kvm_vcpu_data) * i);
1230 v->arch.itc_offset = itc_offset;
1231 v->arch.last_itc = 0;
1233 } else
1234 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1236 r = -ENOMEM;
1237 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1238 if (!vcpu->arch.apic)
1239 goto out;
1240 vcpu->arch.apic->vcpu = vcpu;
1242 p_ctx->gr[1] = 0;
1243 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
1244 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1245 p_ctx->psr = 0x1008522000UL;
1246 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1247 p_ctx->caller_unat = 0;
1248 p_ctx->pr = 0x0;
1249 p_ctx->ar[36] = 0x0; /*unat*/
1250 p_ctx->ar[19] = 0x0; /*rnat*/
1251 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1252 ((sizeof(struct kvm_vcpu)+15) & ~15);
1253 p_ctx->ar[64] = 0x0; /*pfs*/
1254 p_ctx->cr[0] = 0x7e04UL;
1255 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1256 p_ctx->cr[8] = 0x3c;
1258 /*Initilize region register*/
1259 p_ctx->rr[0] = 0x30;
1260 p_ctx->rr[1] = 0x30;
1261 p_ctx->rr[2] = 0x30;
1262 p_ctx->rr[3] = 0x30;
1263 p_ctx->rr[4] = 0x30;
1264 p_ctx->rr[5] = 0x30;
1265 p_ctx->rr[7] = 0x30;
1267 /*Initilize branch register 0*/
1268 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1270 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1271 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1272 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1274 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1275 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1277 vcpu->arch.last_run_cpu = -1;
1278 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
1279 vcpu->arch.vsa_base = kvm_vsa_base;
1280 vcpu->arch.__gp = kvm_vmm_gp;
1281 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1282 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1283 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
1284 init_ptce_info(vcpu);
1286 r = 0;
1287 out:
1288 return r;
1291 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1293 unsigned long psr;
1294 int r;
1296 local_irq_save(psr);
1297 r = kvm_insert_vmm_mapping(vcpu);
1298 local_irq_restore(psr);
1299 if (r)
1300 goto fail;
1301 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1302 if (r)
1303 goto fail;
1305 r = vti_init_vpd(vcpu);
1306 if (r) {
1307 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1308 goto uninit;
1311 r = vti_create_vp(vcpu);
1312 if (r)
1313 goto uninit;
1315 kvm_purge_vmm_mapping(vcpu);
1317 return 0;
1318 uninit:
1319 kvm_vcpu_uninit(vcpu);
1320 fail:
1321 return r;
1324 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1325 unsigned int id)
1327 struct kvm_vcpu *vcpu;
1328 unsigned long vm_base = kvm->arch.vm_base;
1329 int r;
1330 int cpu;
1332 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1334 r = -EINVAL;
1335 if (id >= KVM_MAX_VCPUS) {
1336 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1337 KVM_MAX_VCPUS);
1338 goto fail;
1341 r = -ENOMEM;
1342 if (!vm_base) {
1343 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1344 goto fail;
1346 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1347 vcpu_data[id].vcpu_struct));
1348 vcpu->kvm = kvm;
1350 cpu = get_cpu();
1351 r = vti_vcpu_setup(vcpu, id);
1352 put_cpu();
1354 if (r) {
1355 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1356 goto fail;
1359 kvm->arch.online_vcpus++;
1361 return vcpu;
1362 fail:
1363 return ERR_PTR(r);
1366 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1368 return 0;
1371 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1373 return -EINVAL;
1376 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1378 return -EINVAL;
1381 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1382 struct kvm_guest_debug *dbg)
1384 return -EINVAL;
1387 static void free_kvm(struct kvm *kvm)
1389 unsigned long vm_base = kvm->arch.vm_base;
1391 if (vm_base) {
1392 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1393 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1398 static void kvm_release_vm_pages(struct kvm *kvm)
1400 struct kvm_memory_slot *memslot;
1401 int i, j;
1402 unsigned long base_gfn;
1404 for (i = 0; i < kvm->nmemslots; i++) {
1405 memslot = &kvm->memslots[i];
1406 base_gfn = memslot->base_gfn;
1408 for (j = 0; j < memslot->npages; j++) {
1409 if (memslot->rmap[j])
1410 put_page((struct page *)memslot->rmap[j]);
1415 void kvm_arch_sync_events(struct kvm *kvm)
1419 void kvm_arch_destroy_vm(struct kvm *kvm)
1421 kvm_iommu_unmap_guest(kvm);
1422 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1423 kvm_free_all_assigned_devices(kvm);
1424 #endif
1425 kfree(kvm->arch.vioapic);
1426 kvm_release_vm_pages(kvm);
1427 kvm_free_physmem(kvm);
1428 free_kvm(kvm);
1431 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1435 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1437 if (cpu != vcpu->cpu) {
1438 vcpu->cpu = cpu;
1439 if (vcpu->arch.ht_active)
1440 kvm_migrate_hlt_timer(vcpu);
1444 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1446 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1448 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1449 int i;
1451 vcpu_load(vcpu);
1453 for (i = 0; i < 16; i++) {
1454 regs->vpd.vgr[i] = vpd->vgr[i];
1455 regs->vpd.vbgr[i] = vpd->vbgr[i];
1457 for (i = 0; i < 128; i++)
1458 regs->vpd.vcr[i] = vpd->vcr[i];
1459 regs->vpd.vhpi = vpd->vhpi;
1460 regs->vpd.vnat = vpd->vnat;
1461 regs->vpd.vbnat = vpd->vbnat;
1462 regs->vpd.vpsr = vpd->vpsr;
1463 regs->vpd.vpr = vpd->vpr;
1465 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1467 SAVE_REGS(mp_state);
1468 SAVE_REGS(vmm_rr);
1469 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1470 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1471 SAVE_REGS(itr_regions);
1472 SAVE_REGS(dtr_regions);
1473 SAVE_REGS(tc_regions);
1474 SAVE_REGS(irq_check);
1475 SAVE_REGS(itc_check);
1476 SAVE_REGS(timer_check);
1477 SAVE_REGS(timer_pending);
1478 SAVE_REGS(last_itc);
1479 for (i = 0; i < 8; i++) {
1480 regs->vrr[i] = vcpu->arch.vrr[i];
1481 regs->ibr[i] = vcpu->arch.ibr[i];
1482 regs->dbr[i] = vcpu->arch.dbr[i];
1484 for (i = 0; i < 4; i++)
1485 regs->insvc[i] = vcpu->arch.insvc[i];
1486 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
1487 SAVE_REGS(xtp);
1488 SAVE_REGS(metaphysical_rr0);
1489 SAVE_REGS(metaphysical_rr4);
1490 SAVE_REGS(metaphysical_saved_rr0);
1491 SAVE_REGS(metaphysical_saved_rr4);
1492 SAVE_REGS(fp_psr);
1493 SAVE_REGS(saved_gp);
1495 vcpu_put(vcpu);
1496 return 0;
1499 int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1500 struct kvm_ia64_vcpu_stack *stack)
1502 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1503 return 0;
1506 int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1507 struct kvm_ia64_vcpu_stack *stack)
1509 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1510 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1512 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1513 return 0;
1516 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1519 hrtimer_cancel(&vcpu->arch.hlt_timer);
1520 kfree(vcpu->arch.apic);
1524 long kvm_arch_vcpu_ioctl(struct file *filp,
1525 unsigned int ioctl, unsigned long arg)
1527 struct kvm_vcpu *vcpu = filp->private_data;
1528 void __user *argp = (void __user *)arg;
1529 struct kvm_ia64_vcpu_stack *stack = NULL;
1530 long r;
1532 switch (ioctl) {
1533 case KVM_IA64_VCPU_GET_STACK: {
1534 struct kvm_ia64_vcpu_stack __user *user_stack;
1535 void __user *first_p = argp;
1537 r = -EFAULT;
1538 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1539 goto out;
1541 if (!access_ok(VERIFY_WRITE, user_stack,
1542 sizeof(struct kvm_ia64_vcpu_stack))) {
1543 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1544 "Illegal user destination address for stack\n");
1545 goto out;
1547 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1548 if (!stack) {
1549 r = -ENOMEM;
1550 goto out;
1553 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1554 if (r)
1555 goto out;
1557 if (copy_to_user(user_stack, stack,
1558 sizeof(struct kvm_ia64_vcpu_stack)))
1559 goto out;
1561 break;
1563 case KVM_IA64_VCPU_SET_STACK: {
1564 struct kvm_ia64_vcpu_stack __user *user_stack;
1565 void __user *first_p = argp;
1567 r = -EFAULT;
1568 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1569 goto out;
1571 if (!access_ok(VERIFY_READ, user_stack,
1572 sizeof(struct kvm_ia64_vcpu_stack))) {
1573 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1574 "Illegal user address for stack\n");
1575 goto out;
1577 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1578 if (!stack) {
1579 r = -ENOMEM;
1580 goto out;
1582 if (copy_from_user(stack, user_stack,
1583 sizeof(struct kvm_ia64_vcpu_stack)))
1584 goto out;
1586 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1587 break;
1590 default:
1591 r = -EINVAL;
1594 out:
1595 kfree(stack);
1596 return r;
1599 int kvm_arch_set_memory_region(struct kvm *kvm,
1600 struct kvm_userspace_memory_region *mem,
1601 struct kvm_memory_slot old,
1602 int user_alloc)
1604 unsigned long i;
1605 unsigned long pfn;
1606 int npages = mem->memory_size >> PAGE_SHIFT;
1607 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1608 unsigned long base_gfn = memslot->base_gfn;
1610 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1611 return -ENOMEM;
1613 for (i = 0; i < npages; i++) {
1614 pfn = gfn_to_pfn(kvm, base_gfn + i);
1615 if (!kvm_is_mmio_pfn(pfn)) {
1616 kvm_set_pmt_entry(kvm, base_gfn + i,
1617 pfn << PAGE_SHIFT,
1618 _PAGE_AR_RWX | _PAGE_MA_WB);
1619 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1620 } else {
1621 kvm_set_pmt_entry(kvm, base_gfn + i,
1622 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1623 _PAGE_MA_UC);
1624 memslot->rmap[i] = 0;
1628 return 0;
1631 void kvm_arch_flush_shadow(struct kvm *kvm)
1633 kvm_flush_remote_tlbs(kvm);
1636 long kvm_arch_dev_ioctl(struct file *filp,
1637 unsigned int ioctl, unsigned long arg)
1639 return -EINVAL;
1642 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1644 kvm_vcpu_uninit(vcpu);
1647 static int vti_cpu_has_kvm_support(void)
1649 long avail = 1, status = 1, control = 1;
1650 long ret;
1652 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1653 if (ret)
1654 goto out;
1656 if (!(avail & PAL_PROC_VM_BIT))
1657 goto out;
1659 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1661 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1662 if (ret)
1663 goto out;
1664 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1666 if (!(vp_env_info & VP_OPCODE)) {
1667 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1668 "vm_env_info:0x%lx\n", vp_env_info);
1671 return 1;
1672 out:
1673 return 0;
1678 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1679 * SN2 RTC, replacing the ITC based default verion.
1681 static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1682 struct module *module)
1684 unsigned long new_ar, new_ar_sn2;
1685 unsigned long module_base;
1687 if (!ia64_platform_is("sn2"))
1688 return;
1690 module_base = (unsigned long)module->module_core;
1692 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1693 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1695 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1696 "as source\n");
1699 * Copy the SN2 version of mov_ar into place. They are both
1700 * the same size, so 6 bundles is sufficient (6 * 0x10).
1702 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1705 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1706 struct module *module)
1708 unsigned long module_base;
1709 unsigned long vmm_size;
1711 unsigned long vmm_offset, func_offset, fdesc_offset;
1712 struct fdesc *p_fdesc;
1714 BUG_ON(!module);
1716 if (!kvm_vmm_base) {
1717 printk("kvm: kvm area hasn't been initilized yet!!\n");
1718 return -EFAULT;
1721 /*Calculate new position of relocated vmm module.*/
1722 module_base = (unsigned long)module->module_core;
1723 vmm_size = module->core_size;
1724 if (unlikely(vmm_size > KVM_VMM_SIZE))
1725 return -EFAULT;
1727 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1728 kvm_patch_vmm(vmm_info, module);
1729 kvm_flush_icache(kvm_vmm_base, vmm_size);
1731 /*Recalculate kvm_vmm_info based on new VMM*/
1732 vmm_offset = vmm_info->vmm_ivt - module_base;
1733 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1734 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1735 kvm_vmm_info->vmm_ivt);
1737 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1738 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1739 fdesc_offset);
1740 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1741 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1742 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1743 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1745 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1746 KVM_VMM_BASE+func_offset);
1748 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1749 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1750 fdesc_offset);
1751 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1752 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1753 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1754 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1756 kvm_vmm_gp = p_fdesc->gp;
1758 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1759 kvm_vmm_info->vmm_entry);
1760 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1761 KVM_VMM_BASE + func_offset);
1763 return 0;
1766 int kvm_arch_init(void *opaque)
1768 int r;
1769 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1771 if (!vti_cpu_has_kvm_support()) {
1772 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1773 r = -EOPNOTSUPP;
1774 goto out;
1777 if (kvm_vmm_info) {
1778 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1779 r = -EEXIST;
1780 goto out;
1783 r = -ENOMEM;
1784 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1785 if (!kvm_vmm_info)
1786 goto out;
1788 if (kvm_alloc_vmm_area())
1789 goto out_free0;
1791 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1792 if (r)
1793 goto out_free1;
1795 return 0;
1797 out_free1:
1798 kvm_free_vmm_area();
1799 out_free0:
1800 kfree(kvm_vmm_info);
1801 out:
1802 return r;
1805 void kvm_arch_exit(void)
1807 kvm_free_vmm_area();
1808 kfree(kvm_vmm_info);
1809 kvm_vmm_info = NULL;
1812 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1813 struct kvm_dirty_log *log)
1815 struct kvm_memory_slot *memslot;
1816 int r, i;
1817 long n, base;
1818 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1819 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
1821 r = -EINVAL;
1822 if (log->slot >= KVM_MEMORY_SLOTS)
1823 goto out;
1825 memslot = &kvm->memslots[log->slot];
1826 r = -ENOENT;
1827 if (!memslot->dirty_bitmap)
1828 goto out;
1830 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1831 base = memslot->base_gfn / BITS_PER_LONG;
1833 for (i = 0; i < n/sizeof(long); ++i) {
1834 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1835 dirty_bitmap[base + i] = 0;
1837 r = 0;
1838 out:
1839 return r;
1842 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1843 struct kvm_dirty_log *log)
1845 int r;
1846 int n;
1847 struct kvm_memory_slot *memslot;
1848 int is_dirty = 0;
1850 spin_lock(&kvm->arch.dirty_log_lock);
1852 r = kvm_ia64_sync_dirty_log(kvm, log);
1853 if (r)
1854 goto out;
1856 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1857 if (r)
1858 goto out;
1860 /* If nothing is dirty, don't bother messing with page tables. */
1861 if (is_dirty) {
1862 kvm_flush_remote_tlbs(kvm);
1863 memslot = &kvm->memslots[log->slot];
1864 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1865 memset(memslot->dirty_bitmap, 0, n);
1867 r = 0;
1868 out:
1869 spin_unlock(&kvm->arch.dirty_log_lock);
1870 return r;
1873 int kvm_arch_hardware_setup(void)
1875 return 0;
1878 void kvm_arch_hardware_unsetup(void)
1882 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1884 int me;
1885 int cpu = vcpu->cpu;
1887 if (waitqueue_active(&vcpu->wq))
1888 wake_up_interruptible(&vcpu->wq);
1890 me = get_cpu();
1891 if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
1892 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
1893 smp_send_reschedule(cpu);
1894 put_cpu();
1897 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
1899 return __apic_accept_irq(vcpu, irq->vector);
1902 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1904 return apic->vcpu->vcpu_id == dest;
1907 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1909 return 0;
1912 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
1914 return vcpu1->arch.xtp - vcpu2->arch.xtp;
1917 int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1918 int short_hand, int dest, int dest_mode)
1920 struct kvm_lapic *target = vcpu->arch.apic;
1921 return (dest_mode == 0) ?
1922 kvm_apic_match_physical_addr(target, dest) :
1923 kvm_apic_match_logical_addr(target, dest);
1926 static int find_highest_bits(int *dat)
1928 u32 bits, bitnum;
1929 int i;
1931 /* loop for all 256 bits */
1932 for (i = 7; i >= 0 ; i--) {
1933 bits = dat[i];
1934 if (bits) {
1935 bitnum = fls(bits);
1936 return i * 32 + bitnum - 1;
1940 return -1;
1943 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1945 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1947 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1948 return NMI_VECTOR;
1949 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1950 return ExtINT_VECTOR;
1952 return find_highest_bits((int *)&vpd->irr[0]);
1955 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1957 if (kvm_highest_pending_irq(vcpu) != -1)
1958 return 1;
1959 return 0;
1962 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
1964 /* do real check here */
1965 return 1;
1968 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1970 return vcpu->arch.timer_fired;
1973 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1975 return gfn;
1978 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1980 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1983 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1984 struct kvm_mp_state *mp_state)
1986 vcpu_load(vcpu);
1987 mp_state->mp_state = vcpu->arch.mp_state;
1988 vcpu_put(vcpu);
1989 return 0;
1992 static int vcpu_reset(struct kvm_vcpu *vcpu)
1994 int r;
1995 long psr;
1996 local_irq_save(psr);
1997 r = kvm_insert_vmm_mapping(vcpu);
1998 local_irq_restore(psr);
1999 if (r)
2000 goto fail;
2002 vcpu->arch.launched = 0;
2003 kvm_arch_vcpu_uninit(vcpu);
2004 r = kvm_arch_vcpu_init(vcpu);
2005 if (r)
2006 goto fail;
2008 kvm_purge_vmm_mapping(vcpu);
2009 r = 0;
2010 fail:
2011 return r;
2014 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2015 struct kvm_mp_state *mp_state)
2017 int r = 0;
2019 vcpu_load(vcpu);
2020 vcpu->arch.mp_state = mp_state->mp_state;
2021 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
2022 r = vcpu_reset(vcpu);
2023 vcpu_put(vcpu);
2024 return r;