70eb829767f4601f5ea36fc91a087d80505fd0e3
[linux-2.6.git] / arch / ia64 / kvm / kvm-ia64.c
1 /*
2  * kvm_ia64.c: Basic KVM suppport On Itanium series processors
3  *
4  *
5  *      Copyright (C) 2007, Intel Corporation.
6  *      Xiantao Zhang  (xiantao.zhang@intel.com)
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
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/intel-iommu.h>
35
36 #include <asm/pgtable.h>
37 #include <asm/gcc_intrin.h>
38 #include <asm/pal.h>
39 #include <asm/cacheflush.h>
40 #include <asm/div64.h>
41 #include <asm/tlb.h>
42 #include <asm/elf.h>
43
44 #include "misc.h"
45 #include "vti.h"
46 #include "iodev.h"
47 #include "ioapic.h"
48 #include "lapic.h"
49 #include "irq.h"
50
51 static unsigned long kvm_vmm_base;
52 static unsigned long kvm_vsa_base;
53 static unsigned long kvm_vm_buffer;
54 static unsigned long kvm_vm_buffer_size;
55 unsigned long kvm_vmm_gp;
56
57 static long vp_env_info;
58
59 static struct kvm_vmm_info *kvm_vmm_info;
60
61 static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
62
63 struct kvm_stats_debugfs_item debugfs_entries[] = {
64         { NULL }
65 };
66
67 static void kvm_flush_icache(unsigned long start, unsigned long len)
68 {
69         int l;
70
71         for (l = 0; l < (len + 32); l += 32)
72                 ia64_fc(start + l);
73
74         ia64_sync_i();
75         ia64_srlz_i();
76 }
77
78 static void kvm_flush_tlb_all(void)
79 {
80         unsigned long i, j, count0, count1, stride0, stride1, addr;
81         long flags;
82
83         addr    = local_cpu_data->ptce_base;
84         count0  = local_cpu_data->ptce_count[0];
85         count1  = local_cpu_data->ptce_count[1];
86         stride0 = local_cpu_data->ptce_stride[0];
87         stride1 = local_cpu_data->ptce_stride[1];
88
89         local_irq_save(flags);
90         for (i = 0; i < count0; ++i) {
91                 for (j = 0; j < count1; ++j) {
92                         ia64_ptce(addr);
93                         addr += stride1;
94                 }
95                 addr += stride0;
96         }
97         local_irq_restore(flags);
98         ia64_srlz_i();                  /* srlz.i implies srlz.d */
99 }
100
101 long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
102 {
103         struct ia64_pal_retval iprv;
104
105         PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
106                         (u64)opt_handler);
107
108         return iprv.status;
109 }
110
111 static  DEFINE_SPINLOCK(vp_lock);
112
113 void kvm_arch_hardware_enable(void *garbage)
114 {
115         long  status;
116         long  tmp_base;
117         unsigned long pte;
118         unsigned long saved_psr;
119         int slot;
120
121         pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
122                                 PAGE_KERNEL));
123         local_irq_save(saved_psr);
124         slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
125         local_irq_restore(saved_psr);
126         if (slot < 0)
127                 return;
128
129         spin_lock(&vp_lock);
130         status = ia64_pal_vp_init_env(kvm_vsa_base ?
131                                 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
132                         __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
133         if (status != 0) {
134                 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
135                 return ;
136         }
137
138         if (!kvm_vsa_base) {
139                 kvm_vsa_base = tmp_base;
140                 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
141         }
142         spin_unlock(&vp_lock);
143         ia64_ptr_entry(0x3, slot);
144 }
145
146 void kvm_arch_hardware_disable(void *garbage)
147 {
148
149         long status;
150         int slot;
151         unsigned long pte;
152         unsigned long saved_psr;
153         unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
154
155         pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
156                                 PAGE_KERNEL));
157
158         local_irq_save(saved_psr);
159         slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
160         local_irq_restore(saved_psr);
161         if (slot < 0)
162                 return;
163
164         status = ia64_pal_vp_exit_env(host_iva);
165         if (status)
166                 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
167                                 status);
168         ia64_ptr_entry(0x3, slot);
169 }
170
171 void kvm_arch_check_processor_compat(void *rtn)
172 {
173         *(int *)rtn = 0;
174 }
175
176 int kvm_dev_ioctl_check_extension(long ext)
177 {
178
179         int r;
180
181         switch (ext) {
182         case KVM_CAP_IRQCHIP:
183         case KVM_CAP_USER_MEMORY:
184         case KVM_CAP_MP_STATE:
185
186                 r = 1;
187                 break;
188         case KVM_CAP_COALESCED_MMIO:
189                 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
190                 break;
191         case KVM_CAP_IOMMU:
192                 r = intel_iommu_found();
193                 break;
194         default:
195                 r = 0;
196         }
197         return r;
198
199 }
200
201 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
202                                         gpa_t addr, int len, int is_write)
203 {
204         struct kvm_io_device *dev;
205
206         dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
207
208         return dev;
209 }
210
211 static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
212 {
213         kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
214         kvm_run->hw.hardware_exit_reason = 1;
215         return 0;
216 }
217
218 static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
219 {
220         struct kvm_mmio_req *p;
221         struct kvm_io_device *mmio_dev;
222
223         p = kvm_get_vcpu_ioreq(vcpu);
224
225         if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
226                 goto mmio;
227         vcpu->mmio_needed = 1;
228         vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
229         vcpu->mmio_size = kvm_run->mmio.len = p->size;
230         vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
231
232         if (vcpu->mmio_is_write)
233                 memcpy(vcpu->mmio_data, &p->data, p->size);
234         memcpy(kvm_run->mmio.data, &p->data, p->size);
235         kvm_run->exit_reason = KVM_EXIT_MMIO;
236         return 0;
237 mmio:
238         mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
239         if (mmio_dev) {
240                 if (!p->dir)
241                         kvm_iodevice_write(mmio_dev, p->addr, p->size,
242                                                 &p->data);
243                 else
244                         kvm_iodevice_read(mmio_dev, p->addr, p->size,
245                                                 &p->data);
246
247         } else
248                 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
249         p->state = STATE_IORESP_READY;
250
251         return 1;
252 }
253
254 static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
255 {
256         struct exit_ctl_data *p;
257
258         p = kvm_get_exit_data(vcpu);
259
260         if (p->exit_reason == EXIT_REASON_PAL_CALL)
261                 return kvm_pal_emul(vcpu, kvm_run);
262         else {
263                 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
264                 kvm_run->hw.hardware_exit_reason = 2;
265                 return 0;
266         }
267 }
268
269 static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
270 {
271         struct exit_ctl_data *p;
272
273         p = kvm_get_exit_data(vcpu);
274
275         if (p->exit_reason == EXIT_REASON_SAL_CALL) {
276                 kvm_sal_emul(vcpu);
277                 return 1;
278         } else {
279                 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
280                 kvm_run->hw.hardware_exit_reason = 3;
281                 return 0;
282         }
283
284 }
285
286 /*
287  *  offset: address offset to IPI space.
288  *  value:  deliver value.
289  */
290 static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
291                                 uint64_t vector)
292 {
293         switch (dm) {
294         case SAPIC_FIXED:
295                 kvm_apic_set_irq(vcpu, vector, 0);
296                 break;
297         case SAPIC_NMI:
298                 kvm_apic_set_irq(vcpu, 2, 0);
299                 break;
300         case SAPIC_EXTINT:
301                 kvm_apic_set_irq(vcpu, 0, 0);
302                 break;
303         case SAPIC_INIT:
304         case SAPIC_PMI:
305         default:
306                 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
307                 break;
308         }
309 }
310
311 static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
312                         unsigned long eid)
313 {
314         union ia64_lid lid;
315         int i;
316
317         for (i = 0; i < KVM_MAX_VCPUS; i++) {
318                 if (kvm->vcpus[i]) {
319                         lid.val = VCPU_LID(kvm->vcpus[i]);
320                         if (lid.id == id && lid.eid == eid)
321                                 return kvm->vcpus[i];
322                 }
323         }
324
325         return NULL;
326 }
327
328 static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
329 {
330         struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
331         struct kvm_vcpu *target_vcpu;
332         struct kvm_pt_regs *regs;
333         union ia64_ipi_a addr = p->u.ipi_data.addr;
334         union ia64_ipi_d data = p->u.ipi_data.data;
335
336         target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
337         if (!target_vcpu)
338                 return handle_vm_error(vcpu, kvm_run);
339
340         if (!target_vcpu->arch.launched) {
341                 regs = vcpu_regs(target_vcpu);
342
343                 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
344                 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
345
346                 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
347                 if (waitqueue_active(&target_vcpu->wq))
348                         wake_up_interruptible(&target_vcpu->wq);
349         } else {
350                 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
351                 if (target_vcpu != vcpu)
352                         kvm_vcpu_kick(target_vcpu);
353         }
354
355         return 1;
356 }
357
358 struct call_data {
359         struct kvm_ptc_g ptc_g_data;
360         struct kvm_vcpu *vcpu;
361 };
362
363 static void vcpu_global_purge(void *info)
364 {
365         struct call_data *p = (struct call_data *)info;
366         struct kvm_vcpu *vcpu = p->vcpu;
367
368         if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
369                 return;
370
371         set_bit(KVM_REQ_PTC_G, &vcpu->requests);
372         if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
373                 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
374                                                         p->ptc_g_data;
375         } else {
376                 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
377                 vcpu->arch.ptc_g_count = 0;
378                 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
379         }
380 }
381
382 static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
383 {
384         struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
385         struct kvm *kvm = vcpu->kvm;
386         struct call_data call_data;
387         int i;
388
389         call_data.ptc_g_data = p->u.ptc_g_data;
390
391         for (i = 0; i < KVM_MAX_VCPUS; i++) {
392                 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
393                                                 KVM_MP_STATE_UNINITIALIZED ||
394                                         vcpu == kvm->vcpus[i])
395                         continue;
396
397                 if (waitqueue_active(&kvm->vcpus[i]->wq))
398                         wake_up_interruptible(&kvm->vcpus[i]->wq);
399
400                 if (kvm->vcpus[i]->cpu != -1) {
401                         call_data.vcpu = kvm->vcpus[i];
402                         smp_call_function_single(kvm->vcpus[i]->cpu,
403                                         vcpu_global_purge, &call_data, 1);
404                 } else
405                         printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
406
407         }
408         return 1;
409 }
410
411 static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
412 {
413         return 1;
414 }
415
416 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
417 {
418
419         ktime_t kt;
420         long itc_diff;
421         unsigned long vcpu_now_itc;
422         unsigned long expires;
423         struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
424         unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
425         struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
426
427         if (irqchip_in_kernel(vcpu->kvm)) {
428
429                 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
430
431                 if (time_after(vcpu_now_itc, vpd->itm)) {
432                         vcpu->arch.timer_check = 1;
433                         return 1;
434                 }
435                 itc_diff = vpd->itm - vcpu_now_itc;
436                 if (itc_diff < 0)
437                         itc_diff = -itc_diff;
438
439                 expires = div64_u64(itc_diff, cyc_per_usec);
440                 kt = ktime_set(0, 1000 * expires);
441
442                 vcpu->arch.ht_active = 1;
443                 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
444
445                 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
446                 kvm_vcpu_block(vcpu);
447                 hrtimer_cancel(p_ht);
448                 vcpu->arch.ht_active = 0;
449
450                 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
451                         if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
452                                 vcpu->arch.mp_state =
453                                         KVM_MP_STATE_RUNNABLE;
454
455                 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
456                         return -EINTR;
457                 return 1;
458         } else {
459                 printk(KERN_ERR"kvm: Unsupported userspace halt!");
460                 return 0;
461         }
462 }
463
464 static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
465                 struct kvm_run *kvm_run)
466 {
467         kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
468         return 0;
469 }
470
471 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
472                 struct kvm_run *kvm_run)
473 {
474         return 1;
475 }
476
477 static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
478                 struct kvm_run *kvm_run) = {
479         [EXIT_REASON_VM_PANIC]              = handle_vm_error,
480         [EXIT_REASON_MMIO_INSTRUCTION]      = handle_mmio,
481         [EXIT_REASON_PAL_CALL]              = handle_pal_call,
482         [EXIT_REASON_SAL_CALL]              = handle_sal_call,
483         [EXIT_REASON_SWITCH_RR6]            = handle_switch_rr6,
484         [EXIT_REASON_VM_DESTROY]            = handle_vm_shutdown,
485         [EXIT_REASON_EXTERNAL_INTERRUPT]    = handle_external_interrupt,
486         [EXIT_REASON_IPI]                   = handle_ipi,
487         [EXIT_REASON_PTC_G]                 = handle_global_purge,
488
489 };
490
491 static const int kvm_vti_max_exit_handlers =
492                 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
493
494 static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
495 {
496         struct exit_ctl_data *p_exit_data;
497
498         p_exit_data = kvm_get_exit_data(vcpu);
499         return p_exit_data->exit_reason;
500 }
501
502 /*
503  * The guest has exited.  See if we can fix it or if we need userspace
504  * assistance.
505  */
506 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
507 {
508         u32 exit_reason = kvm_get_exit_reason(vcpu);
509         vcpu->arch.last_exit = exit_reason;
510
511         if (exit_reason < kvm_vti_max_exit_handlers
512                         && kvm_vti_exit_handlers[exit_reason])
513                 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
514         else {
515                 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
516                 kvm_run->hw.hardware_exit_reason = exit_reason;
517         }
518         return 0;
519 }
520
521 static inline void vti_set_rr6(unsigned long rr6)
522 {
523         ia64_set_rr(RR6, rr6);
524         ia64_srlz_i();
525 }
526
527 static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
528 {
529         unsigned long pte;
530         struct kvm *kvm = vcpu->kvm;
531         int r;
532
533         /*Insert a pair of tr to map vmm*/
534         pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
535         r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
536         if (r < 0)
537                 goto out;
538         vcpu->arch.vmm_tr_slot = r;
539         /*Insert a pairt of tr to map data of vm*/
540         pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
541         r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
542                                         pte, KVM_VM_DATA_SHIFT);
543         if (r < 0)
544                 goto out;
545         vcpu->arch.vm_tr_slot = r;
546         r = 0;
547 out:
548         return r;
549
550 }
551
552 static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
553 {
554
555         ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
556         ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
557
558 }
559
560 static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
561 {
562         int cpu = smp_processor_id();
563
564         if (vcpu->arch.last_run_cpu != cpu ||
565                         per_cpu(last_vcpu, cpu) != vcpu) {
566                 per_cpu(last_vcpu, cpu) = vcpu;
567                 vcpu->arch.last_run_cpu = cpu;
568                 kvm_flush_tlb_all();
569         }
570
571         vcpu->arch.host_rr6 = ia64_get_rr(RR6);
572         vti_set_rr6(vcpu->arch.vmm_rr);
573         return kvm_insert_vmm_mapping(vcpu);
574 }
575 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
576 {
577         kvm_purge_vmm_mapping(vcpu);
578         vti_set_rr6(vcpu->arch.host_rr6);
579 }
580
581 static int  vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
582 {
583         union context *host_ctx, *guest_ctx;
584         int r;
585
586         /*Get host and guest context with guest address space.*/
587         host_ctx = kvm_get_host_context(vcpu);
588         guest_ctx = kvm_get_guest_context(vcpu);
589
590         r = kvm_vcpu_pre_transition(vcpu);
591         if (r < 0)
592                 goto out;
593         kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
594         kvm_vcpu_post_transition(vcpu);
595         r = 0;
596 out:
597         return r;
598 }
599
600 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
601 {
602         int r;
603
604 again:
605         preempt_disable();
606         local_irq_disable();
607
608         if (signal_pending(current)) {
609                 local_irq_enable();
610                 preempt_enable();
611                 r = -EINTR;
612                 kvm_run->exit_reason = KVM_EXIT_INTR;
613                 goto out;
614         }
615
616         vcpu->guest_mode = 1;
617         kvm_guest_enter();
618         down_read(&vcpu->kvm->slots_lock);
619         r = vti_vcpu_run(vcpu, kvm_run);
620         if (r < 0) {
621                 local_irq_enable();
622                 preempt_enable();
623                 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
624                 goto out;
625         }
626
627         vcpu->arch.launched = 1;
628         vcpu->guest_mode = 0;
629         local_irq_enable();
630
631         /*
632          * We must have an instruction between local_irq_enable() and
633          * kvm_guest_exit(), so the timer interrupt isn't delayed by
634          * the interrupt shadow.  The stat.exits increment will do nicely.
635          * But we need to prevent reordering, hence this barrier():
636          */
637         barrier();
638         kvm_guest_exit();
639         up_read(&vcpu->kvm->slots_lock);
640         preempt_enable();
641
642         r = kvm_handle_exit(kvm_run, vcpu);
643
644         if (r > 0) {
645                 if (!need_resched())
646                         goto again;
647         }
648
649 out:
650         if (r > 0) {
651                 kvm_resched(vcpu);
652                 goto again;
653         }
654
655         return r;
656 }
657
658 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
659 {
660         struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
661
662         if (!vcpu->mmio_is_write)
663                 memcpy(&p->data, vcpu->mmio_data, 8);
664         p->state = STATE_IORESP_READY;
665 }
666
667 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
668 {
669         int r;
670         sigset_t sigsaved;
671
672         vcpu_load(vcpu);
673
674         if (vcpu->sigset_active)
675                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
676
677         if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
678                 kvm_vcpu_block(vcpu);
679                 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
680                 r = -EAGAIN;
681                 goto out;
682         }
683
684         if (vcpu->mmio_needed) {
685                 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
686                 kvm_set_mmio_data(vcpu);
687                 vcpu->mmio_read_completed = 1;
688                 vcpu->mmio_needed = 0;
689         }
690         r = __vcpu_run(vcpu, kvm_run);
691 out:
692         if (vcpu->sigset_active)
693                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
694
695         vcpu_put(vcpu);
696         return r;
697 }
698
699 static struct kvm *kvm_alloc_kvm(void)
700 {
701
702         struct kvm *kvm;
703         uint64_t  vm_base;
704
705         BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
706
707         vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
708
709         if (!vm_base)
710                 return ERR_PTR(-ENOMEM);
711
712         memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
713         kvm = (struct kvm *)(vm_base +
714                         offsetof(struct kvm_vm_data, kvm_vm_struct));
715         kvm->arch.vm_base = vm_base;
716         printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
717
718         return kvm;
719 }
720
721 struct kvm_io_range {
722         unsigned long start;
723         unsigned long size;
724         unsigned long type;
725 };
726
727 static const struct kvm_io_range io_ranges[] = {
728         {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
729         {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
730         {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
731         {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
732         {PIB_START, PIB_SIZE, GPFN_PIB},
733 };
734
735 static void kvm_build_io_pmt(struct kvm *kvm)
736 {
737         unsigned long i, j;
738
739         /* Mark I/O ranges */
740         for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
741                                                         i++) {
742                 for (j = io_ranges[i].start;
743                                 j < io_ranges[i].start + io_ranges[i].size;
744                                 j += PAGE_SIZE)
745                         kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
746                                         io_ranges[i].type, 0);
747         }
748
749 }
750
751 /*Use unused rids to virtualize guest rid.*/
752 #define GUEST_PHYSICAL_RR0      0x1739
753 #define GUEST_PHYSICAL_RR4      0x2739
754 #define VMM_INIT_RR             0x1660
755
756 static void kvm_init_vm(struct kvm *kvm)
757 {
758         BUG_ON(!kvm);
759
760         kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
761         kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
762         kvm->arch.vmm_init_rr = VMM_INIT_RR;
763
764         /*
765          *Fill P2M entries for MMIO/IO ranges
766          */
767         kvm_build_io_pmt(kvm);
768
769         INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
770
771         /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
772         set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
773 }
774
775 struct  kvm *kvm_arch_create_vm(void)
776 {
777         struct kvm *kvm = kvm_alloc_kvm();
778
779         if (IS_ERR(kvm))
780                 return ERR_PTR(-ENOMEM);
781         kvm_init_vm(kvm);
782
783         return kvm;
784
785 }
786
787 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
788                                         struct kvm_irqchip *chip)
789 {
790         int r;
791
792         r = 0;
793         switch (chip->chip_id) {
794         case KVM_IRQCHIP_IOAPIC:
795                 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
796                                 sizeof(struct kvm_ioapic_state));
797                 break;
798         default:
799                 r = -EINVAL;
800                 break;
801         }
802         return r;
803 }
804
805 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
806 {
807         int r;
808
809         r = 0;
810         switch (chip->chip_id) {
811         case KVM_IRQCHIP_IOAPIC:
812                 memcpy(ioapic_irqchip(kvm),
813                                 &chip->chip.ioapic,
814                                 sizeof(struct kvm_ioapic_state));
815                 break;
816         default:
817                 r = -EINVAL;
818                 break;
819         }
820         return r;
821 }
822
823 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
824
825 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
826 {
827         int i;
828         struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
829         int r;
830
831         vcpu_load(vcpu);
832
833         for (i = 0; i < 16; i++) {
834                 vpd->vgr[i] = regs->vpd.vgr[i];
835                 vpd->vbgr[i] = regs->vpd.vbgr[i];
836         }
837         for (i = 0; i < 128; i++)
838                 vpd->vcr[i] = regs->vpd.vcr[i];
839         vpd->vhpi = regs->vpd.vhpi;
840         vpd->vnat = regs->vpd.vnat;
841         vpd->vbnat = regs->vpd.vbnat;
842         vpd->vpsr = regs->vpd.vpsr;
843
844         vpd->vpr = regs->vpd.vpr;
845
846         r = -EFAULT;
847         r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
848                                                 sizeof(union context));
849         if (r)
850                 goto out;
851         r = copy_from_user(vcpu + 1, regs->saved_stack +
852                         sizeof(struct kvm_vcpu),
853                         KVM_STK_OFFSET - sizeof(struct kvm_vcpu));
854         if (r)
855                 goto out;
856         vcpu->arch.exit_data =
857                 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
858
859         RESTORE_REGS(mp_state);
860         RESTORE_REGS(vmm_rr);
861         memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
862         memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
863         RESTORE_REGS(itr_regions);
864         RESTORE_REGS(dtr_regions);
865         RESTORE_REGS(tc_regions);
866         RESTORE_REGS(irq_check);
867         RESTORE_REGS(itc_check);
868         RESTORE_REGS(timer_check);
869         RESTORE_REGS(timer_pending);
870         RESTORE_REGS(last_itc);
871         for (i = 0; i < 8; i++) {
872                 vcpu->arch.vrr[i] = regs->vrr[i];
873                 vcpu->arch.ibr[i] = regs->ibr[i];
874                 vcpu->arch.dbr[i] = regs->dbr[i];
875         }
876         for (i = 0; i < 4; i++)
877                 vcpu->arch.insvc[i] = regs->insvc[i];
878         RESTORE_REGS(xtp);
879         RESTORE_REGS(metaphysical_rr0);
880         RESTORE_REGS(metaphysical_rr4);
881         RESTORE_REGS(metaphysical_saved_rr0);
882         RESTORE_REGS(metaphysical_saved_rr4);
883         RESTORE_REGS(fp_psr);
884         RESTORE_REGS(saved_gp);
885
886         vcpu->arch.irq_new_pending = 1;
887         vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
888         set_bit(KVM_REQ_RESUME, &vcpu->requests);
889
890         vcpu_put(vcpu);
891         r = 0;
892 out:
893         return r;
894 }
895
896 long kvm_arch_vm_ioctl(struct file *filp,
897                 unsigned int ioctl, unsigned long arg)
898 {
899         struct kvm *kvm = filp->private_data;
900         void __user *argp = (void __user *)arg;
901         int r = -EINVAL;
902
903         switch (ioctl) {
904         case KVM_SET_MEMORY_REGION: {
905                 struct kvm_memory_region kvm_mem;
906                 struct kvm_userspace_memory_region kvm_userspace_mem;
907
908                 r = -EFAULT;
909                 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
910                         goto out;
911                 kvm_userspace_mem.slot = kvm_mem.slot;
912                 kvm_userspace_mem.flags = kvm_mem.flags;
913                 kvm_userspace_mem.guest_phys_addr =
914                                         kvm_mem.guest_phys_addr;
915                 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
916                 r = kvm_vm_ioctl_set_memory_region(kvm,
917                                         &kvm_userspace_mem, 0);
918                 if (r)
919                         goto out;
920                 break;
921                 }
922         case KVM_CREATE_IRQCHIP:
923                 r = -EFAULT;
924                 r = kvm_ioapic_init(kvm);
925                 if (r)
926                         goto out;
927                 break;
928         case KVM_IRQ_LINE: {
929                 struct kvm_irq_level irq_event;
930
931                 r = -EFAULT;
932                 if (copy_from_user(&irq_event, argp, sizeof irq_event))
933                         goto out;
934                 if (irqchip_in_kernel(kvm)) {
935                         mutex_lock(&kvm->lock);
936                         kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
937                                     irq_event.irq, irq_event.level);
938                         mutex_unlock(&kvm->lock);
939                         r = 0;
940                 }
941                 break;
942                 }
943         case KVM_GET_IRQCHIP: {
944                 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
945                 struct kvm_irqchip chip;
946
947                 r = -EFAULT;
948                 if (copy_from_user(&chip, argp, sizeof chip))
949                                 goto out;
950                 r = -ENXIO;
951                 if (!irqchip_in_kernel(kvm))
952                         goto out;
953                 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
954                 if (r)
955                         goto out;
956                 r = -EFAULT;
957                 if (copy_to_user(argp, &chip, sizeof chip))
958                                 goto out;
959                 r = 0;
960                 break;
961                 }
962         case KVM_SET_IRQCHIP: {
963                 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
964                 struct kvm_irqchip chip;
965
966                 r = -EFAULT;
967                 if (copy_from_user(&chip, argp, sizeof chip))
968                                 goto out;
969                 r = -ENXIO;
970                 if (!irqchip_in_kernel(kvm))
971                         goto out;
972                 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
973                 if (r)
974                         goto out;
975                 r = 0;
976                 break;
977                 }
978         default:
979                 ;
980         }
981 out:
982         return r;
983 }
984
985 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
986                 struct kvm_sregs *sregs)
987 {
988         return -EINVAL;
989 }
990
991 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
992                 struct kvm_sregs *sregs)
993 {
994         return -EINVAL;
995
996 }
997 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
998                 struct kvm_translation *tr)
999 {
1000
1001         return -EINVAL;
1002 }
1003
1004 static int kvm_alloc_vmm_area(void)
1005 {
1006         if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1007                 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1008                                 get_order(KVM_VMM_SIZE));
1009                 if (!kvm_vmm_base)
1010                         return -ENOMEM;
1011
1012                 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1013                 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1014
1015                 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1016                                 kvm_vmm_base, kvm_vm_buffer);
1017         }
1018
1019         return 0;
1020 }
1021
1022 static void kvm_free_vmm_area(void)
1023 {
1024         if (kvm_vmm_base) {
1025                 /*Zero this area before free to avoid bits leak!!*/
1026                 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1027                 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1028                 kvm_vmm_base  = 0;
1029                 kvm_vm_buffer = 0;
1030                 kvm_vsa_base = 0;
1031         }
1032 }
1033
1034 static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1035 {
1036 }
1037
1038 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1039 {
1040         int i;
1041         union cpuid3_t cpuid3;
1042         struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1043
1044         if (IS_ERR(vpd))
1045                 return PTR_ERR(vpd);
1046
1047         /* CPUID init */
1048         for (i = 0; i < 5; i++)
1049                 vpd->vcpuid[i] = ia64_get_cpuid(i);
1050
1051         /* Limit the CPUID number to 5 */
1052         cpuid3.value = vpd->vcpuid[3];
1053         cpuid3.number = 4;      /* 5 - 1 */
1054         vpd->vcpuid[3] = cpuid3.value;
1055
1056         /*Set vac and vdc fields*/
1057         vpd->vac.a_from_int_cr = 1;
1058         vpd->vac.a_to_int_cr = 1;
1059         vpd->vac.a_from_psr = 1;
1060         vpd->vac.a_from_cpuid = 1;
1061         vpd->vac.a_cover = 1;
1062         vpd->vac.a_bsw = 1;
1063         vpd->vac.a_int = 1;
1064         vpd->vdc.d_vmsw = 1;
1065
1066         /*Set virtual buffer*/
1067         vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1068
1069         return 0;
1070 }
1071
1072 static int vti_create_vp(struct kvm_vcpu *vcpu)
1073 {
1074         long ret;
1075         struct vpd *vpd = vcpu->arch.vpd;
1076         unsigned long  vmm_ivt;
1077
1078         vmm_ivt = kvm_vmm_info->vmm_ivt;
1079
1080         printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1081
1082         ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1083
1084         if (ret) {
1085                 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1086                 return -EINVAL;
1087         }
1088         return 0;
1089 }
1090
1091 static void init_ptce_info(struct kvm_vcpu *vcpu)
1092 {
1093         ia64_ptce_info_t ptce = {0};
1094
1095         ia64_get_ptce(&ptce);
1096         vcpu->arch.ptce_base = ptce.base;
1097         vcpu->arch.ptce_count[0] = ptce.count[0];
1098         vcpu->arch.ptce_count[1] = ptce.count[1];
1099         vcpu->arch.ptce_stride[0] = ptce.stride[0];
1100         vcpu->arch.ptce_stride[1] = ptce.stride[1];
1101 }
1102
1103 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1104 {
1105         struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1106
1107         if (hrtimer_cancel(p_ht))
1108                 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
1109 }
1110
1111 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1112 {
1113         struct kvm_vcpu *vcpu;
1114         wait_queue_head_t *q;
1115
1116         vcpu  = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1117         q = &vcpu->wq;
1118
1119         if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1120                 goto out;
1121
1122         if (waitqueue_active(q))
1123                 wake_up_interruptible(q);
1124
1125 out:
1126         vcpu->arch.timer_fired = 1;
1127         vcpu->arch.timer_check = 1;
1128         return HRTIMER_NORESTART;
1129 }
1130
1131 #define PALE_RESET_ENTRY    0x80000000ffffffb0UL
1132
1133 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1134 {
1135         struct kvm_vcpu *v;
1136         int r;
1137         int i;
1138         long itc_offset;
1139         struct kvm *kvm = vcpu->kvm;
1140         struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1141
1142         union context *p_ctx = &vcpu->arch.guest;
1143         struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1144
1145         /*Init vcpu context for first run.*/
1146         if (IS_ERR(vmm_vcpu))
1147                 return PTR_ERR(vmm_vcpu);
1148
1149         if (vcpu->vcpu_id == 0) {
1150                 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1151
1152                 /*Set entry address for first run.*/
1153                 regs->cr_iip = PALE_RESET_ENTRY;
1154
1155                 /*Initialize itc offset for vcpus*/
1156                 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1157                 for (i = 0; i < KVM_MAX_VCPUS; i++) {
1158                         v = (struct kvm_vcpu *)((char *)vcpu +
1159                                         sizeof(struct kvm_vcpu_data) * i);
1160                         v->arch.itc_offset = itc_offset;
1161                         v->arch.last_itc = 0;
1162                 }
1163         } else
1164                 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1165
1166         r = -ENOMEM;
1167         vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1168         if (!vcpu->arch.apic)
1169                 goto out;
1170         vcpu->arch.apic->vcpu = vcpu;
1171
1172         p_ctx->gr[1] = 0;
1173         p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
1174         p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1175         p_ctx->psr = 0x1008522000UL;
1176         p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1177         p_ctx->caller_unat = 0;
1178         p_ctx->pr = 0x0;
1179         p_ctx->ar[36] = 0x0; /*unat*/
1180         p_ctx->ar[19] = 0x0; /*rnat*/
1181         p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1182                                 ((sizeof(struct kvm_vcpu)+15) & ~15);
1183         p_ctx->ar[64] = 0x0; /*pfs*/
1184         p_ctx->cr[0] = 0x7e04UL;
1185         p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1186         p_ctx->cr[8] = 0x3c;
1187
1188         /*Initilize region register*/
1189         p_ctx->rr[0] = 0x30;
1190         p_ctx->rr[1] = 0x30;
1191         p_ctx->rr[2] = 0x30;
1192         p_ctx->rr[3] = 0x30;
1193         p_ctx->rr[4] = 0x30;
1194         p_ctx->rr[5] = 0x30;
1195         p_ctx->rr[7] = 0x30;
1196
1197         /*Initilize branch register 0*/
1198         p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1199
1200         vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1201         vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1202         vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1203
1204         hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1205         vcpu->arch.hlt_timer.function = hlt_timer_fn;
1206
1207         vcpu->arch.last_run_cpu = -1;
1208         vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
1209         vcpu->arch.vsa_base = kvm_vsa_base;
1210         vcpu->arch.__gp = kvm_vmm_gp;
1211         vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1212         vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1213         vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
1214         init_ptce_info(vcpu);
1215
1216         r = 0;
1217 out:
1218         return r;
1219 }
1220
1221 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1222 {
1223         unsigned long psr;
1224         int r;
1225
1226         local_irq_save(psr);
1227         r = kvm_insert_vmm_mapping(vcpu);
1228         if (r)
1229                 goto fail;
1230         r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1231         if (r)
1232                 goto fail;
1233
1234         r = vti_init_vpd(vcpu);
1235         if (r) {
1236                 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1237                 goto uninit;
1238         }
1239
1240         r = vti_create_vp(vcpu);
1241         if (r)
1242                 goto uninit;
1243
1244         kvm_purge_vmm_mapping(vcpu);
1245         local_irq_restore(psr);
1246
1247         return 0;
1248 uninit:
1249         kvm_vcpu_uninit(vcpu);
1250 fail:
1251         local_irq_restore(psr);
1252         return r;
1253 }
1254
1255 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1256                 unsigned int id)
1257 {
1258         struct kvm_vcpu *vcpu;
1259         unsigned long vm_base = kvm->arch.vm_base;
1260         int r;
1261         int cpu;
1262
1263         BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1264
1265         r = -EINVAL;
1266         if (id >= KVM_MAX_VCPUS) {
1267                 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1268                                 KVM_MAX_VCPUS);
1269                 goto fail;
1270         }
1271
1272         r = -ENOMEM;
1273         if (!vm_base) {
1274                 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1275                 goto fail;
1276         }
1277         vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1278                                         vcpu_data[id].vcpu_struct));
1279         vcpu->kvm = kvm;
1280
1281         cpu = get_cpu();
1282         vti_vcpu_load(vcpu, cpu);
1283         r = vti_vcpu_setup(vcpu, id);
1284         put_cpu();
1285
1286         if (r) {
1287                 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1288                 goto fail;
1289         }
1290
1291         return vcpu;
1292 fail:
1293         return ERR_PTR(r);
1294 }
1295
1296 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1297 {
1298         return 0;
1299 }
1300
1301 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1302 {
1303         return -EINVAL;
1304 }
1305
1306 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1307 {
1308         return -EINVAL;
1309 }
1310
1311 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1312                 struct kvm_debug_guest *dbg)
1313 {
1314         return -EINVAL;
1315 }
1316
1317 static void free_kvm(struct kvm *kvm)
1318 {
1319         unsigned long vm_base = kvm->arch.vm_base;
1320
1321         if (vm_base) {
1322                 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1323                 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1324         }
1325
1326 }
1327
1328 static void kvm_release_vm_pages(struct kvm *kvm)
1329 {
1330         struct kvm_memory_slot *memslot;
1331         int i, j;
1332         unsigned long base_gfn;
1333
1334         for (i = 0; i < kvm->nmemslots; i++) {
1335                 memslot = &kvm->memslots[i];
1336                 base_gfn = memslot->base_gfn;
1337
1338                 for (j = 0; j < memslot->npages; j++) {
1339                         if (memslot->rmap[j])
1340                                 put_page((struct page *)memslot->rmap[j]);
1341                 }
1342         }
1343 }
1344
1345 void kvm_arch_destroy_vm(struct kvm *kvm)
1346 {
1347         kvm_iommu_unmap_guest(kvm);
1348 #ifdef  KVM_CAP_DEVICE_ASSIGNMENT
1349         kvm_free_all_assigned_devices(kvm);
1350 #endif
1351         kfree(kvm->arch.vioapic);
1352         kvm_release_vm_pages(kvm);
1353         kvm_free_physmem(kvm);
1354         free_kvm(kvm);
1355 }
1356
1357 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1358 {
1359 }
1360
1361 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1362 {
1363         if (cpu != vcpu->cpu) {
1364                 vcpu->cpu = cpu;
1365                 if (vcpu->arch.ht_active)
1366                         kvm_migrate_hlt_timer(vcpu);
1367         }
1368 }
1369
1370 #define SAVE_REGS(_x)   regs->_x = vcpu->arch._x
1371
1372 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1373 {
1374         int i;
1375         int r;
1376         struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1377         vcpu_load(vcpu);
1378
1379         for (i = 0; i < 16; i++) {
1380                 regs->vpd.vgr[i] = vpd->vgr[i];
1381                 regs->vpd.vbgr[i] = vpd->vbgr[i];
1382         }
1383         for (i = 0; i < 128; i++)
1384                 regs->vpd.vcr[i] = vpd->vcr[i];
1385         regs->vpd.vhpi = vpd->vhpi;
1386         regs->vpd.vnat = vpd->vnat;
1387         regs->vpd.vbnat = vpd->vbnat;
1388         regs->vpd.vpsr = vpd->vpsr;
1389         regs->vpd.vpr = vpd->vpr;
1390
1391         r = -EFAULT;
1392         r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1393                                         sizeof(union context));
1394         if (r)
1395                 goto out;
1396         r = copy_to_user(regs->saved_stack, (void *)vcpu, KVM_STK_OFFSET);
1397         if (r)
1398                 goto out;
1399         SAVE_REGS(mp_state);
1400         SAVE_REGS(vmm_rr);
1401         memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1402         memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1403         SAVE_REGS(itr_regions);
1404         SAVE_REGS(dtr_regions);
1405         SAVE_REGS(tc_regions);
1406         SAVE_REGS(irq_check);
1407         SAVE_REGS(itc_check);
1408         SAVE_REGS(timer_check);
1409         SAVE_REGS(timer_pending);
1410         SAVE_REGS(last_itc);
1411         for (i = 0; i < 8; i++) {
1412                 regs->vrr[i] = vcpu->arch.vrr[i];
1413                 regs->ibr[i] = vcpu->arch.ibr[i];
1414                 regs->dbr[i] = vcpu->arch.dbr[i];
1415         }
1416         for (i = 0; i < 4; i++)
1417                 regs->insvc[i] = vcpu->arch.insvc[i];
1418         regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1419         SAVE_REGS(xtp);
1420         SAVE_REGS(metaphysical_rr0);
1421         SAVE_REGS(metaphysical_rr4);
1422         SAVE_REGS(metaphysical_saved_rr0);
1423         SAVE_REGS(metaphysical_saved_rr4);
1424         SAVE_REGS(fp_psr);
1425         SAVE_REGS(saved_gp);
1426         vcpu_put(vcpu);
1427         r = 0;
1428 out:
1429         return r;
1430 }
1431
1432 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1433 {
1434
1435         hrtimer_cancel(&vcpu->arch.hlt_timer);
1436         kfree(vcpu->arch.apic);
1437 }
1438
1439
1440 long kvm_arch_vcpu_ioctl(struct file *filp,
1441                 unsigned int ioctl, unsigned long arg)
1442 {
1443         return -EINVAL;
1444 }
1445
1446 int kvm_arch_set_memory_region(struct kvm *kvm,
1447                 struct kvm_userspace_memory_region *mem,
1448                 struct kvm_memory_slot old,
1449                 int user_alloc)
1450 {
1451         unsigned long i;
1452         unsigned long pfn;
1453         int npages = mem->memory_size >> PAGE_SHIFT;
1454         struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1455         unsigned long base_gfn = memslot->base_gfn;
1456
1457         if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1458                 return -ENOMEM;
1459
1460         for (i = 0; i < npages; i++) {
1461                 pfn = gfn_to_pfn(kvm, base_gfn + i);
1462                 if (!kvm_is_mmio_pfn(pfn)) {
1463                         kvm_set_pmt_entry(kvm, base_gfn + i,
1464                                         pfn << PAGE_SHIFT,
1465                                 _PAGE_AR_RWX | _PAGE_MA_WB);
1466                         memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1467                 } else {
1468                         kvm_set_pmt_entry(kvm, base_gfn + i,
1469                                         GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1470                                         _PAGE_MA_UC);
1471                         memslot->rmap[i] = 0;
1472                         }
1473         }
1474
1475         return 0;
1476 }
1477
1478 void kvm_arch_flush_shadow(struct kvm *kvm)
1479 {
1480 }
1481
1482 long kvm_arch_dev_ioctl(struct file *filp,
1483                 unsigned int ioctl, unsigned long arg)
1484 {
1485         return -EINVAL;
1486 }
1487
1488 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1489 {
1490         kvm_vcpu_uninit(vcpu);
1491 }
1492
1493 static int vti_cpu_has_kvm_support(void)
1494 {
1495         long  avail = 1, status = 1, control = 1;
1496         long ret;
1497
1498         ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1499         if (ret)
1500                 goto out;
1501
1502         if (!(avail & PAL_PROC_VM_BIT))
1503                 goto out;
1504
1505         printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1506
1507         ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1508         if (ret)
1509                 goto out;
1510         printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1511
1512         if (!(vp_env_info & VP_OPCODE)) {
1513                 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1514                                 "vm_env_info:0x%lx\n", vp_env_info);
1515         }
1516
1517         return 1;
1518 out:
1519         return 0;
1520 }
1521
1522 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1523                                                 struct module *module)
1524 {
1525         unsigned long module_base;
1526         unsigned long vmm_size;
1527
1528         unsigned long vmm_offset, func_offset, fdesc_offset;
1529         struct fdesc *p_fdesc;
1530
1531         BUG_ON(!module);
1532
1533         if (!kvm_vmm_base) {
1534                 printk("kvm: kvm area hasn't been initilized yet!!\n");
1535                 return -EFAULT;
1536         }
1537
1538         /*Calculate new position of relocated vmm module.*/
1539         module_base = (unsigned long)module->module_core;
1540         vmm_size = module->core_size;
1541         if (unlikely(vmm_size > KVM_VMM_SIZE))
1542                 return -EFAULT;
1543
1544         memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1545         kvm_flush_icache(kvm_vmm_base, vmm_size);
1546
1547         /*Recalculate kvm_vmm_info based on new VMM*/
1548         vmm_offset = vmm_info->vmm_ivt - module_base;
1549         kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1550         printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1551                         kvm_vmm_info->vmm_ivt);
1552
1553         fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1554         kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1555                                                         fdesc_offset);
1556         func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1557         p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1558         p_fdesc->ip = KVM_VMM_BASE + func_offset;
1559         p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1560
1561         printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1562                         KVM_VMM_BASE+func_offset);
1563
1564         fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1565         kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1566                         fdesc_offset);
1567         func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1568         p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1569         p_fdesc->ip = KVM_VMM_BASE + func_offset;
1570         p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1571
1572         kvm_vmm_gp = p_fdesc->gp;
1573
1574         printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1575                                                 kvm_vmm_info->vmm_entry);
1576         printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1577                                                 KVM_VMM_BASE + func_offset);
1578
1579         return 0;
1580 }
1581
1582 int kvm_arch_init(void *opaque)
1583 {
1584         int r;
1585         struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1586
1587         if (!vti_cpu_has_kvm_support()) {
1588                 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1589                 r = -EOPNOTSUPP;
1590                 goto out;
1591         }
1592
1593         if (kvm_vmm_info) {
1594                 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1595                 r = -EEXIST;
1596                 goto out;
1597         }
1598
1599         r = -ENOMEM;
1600         kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1601         if (!kvm_vmm_info)
1602                 goto out;
1603
1604         if (kvm_alloc_vmm_area())
1605                 goto out_free0;
1606
1607         r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1608         if (r)
1609                 goto out_free1;
1610
1611         return 0;
1612
1613 out_free1:
1614         kvm_free_vmm_area();
1615 out_free0:
1616         kfree(kvm_vmm_info);
1617 out:
1618         return r;
1619 }
1620
1621 void kvm_arch_exit(void)
1622 {
1623         kvm_free_vmm_area();
1624         kfree(kvm_vmm_info);
1625         kvm_vmm_info = NULL;
1626 }
1627
1628 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1629                 struct kvm_dirty_log *log)
1630 {
1631         struct kvm_memory_slot *memslot;
1632         int r, i;
1633         long n, base;
1634         unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1635                         offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
1636
1637         r = -EINVAL;
1638         if (log->slot >= KVM_MEMORY_SLOTS)
1639                 goto out;
1640
1641         memslot = &kvm->memslots[log->slot];
1642         r = -ENOENT;
1643         if (!memslot->dirty_bitmap)
1644                 goto out;
1645
1646         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1647         base = memslot->base_gfn / BITS_PER_LONG;
1648
1649         for (i = 0; i < n/sizeof(long); ++i) {
1650                 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1651                 dirty_bitmap[base + i] = 0;
1652         }
1653         r = 0;
1654 out:
1655         return r;
1656 }
1657
1658 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1659                 struct kvm_dirty_log *log)
1660 {
1661         int r;
1662         int n;
1663         struct kvm_memory_slot *memslot;
1664         int is_dirty = 0;
1665
1666         spin_lock(&kvm->arch.dirty_log_lock);
1667
1668         r = kvm_ia64_sync_dirty_log(kvm, log);
1669         if (r)
1670                 goto out;
1671
1672         r = kvm_get_dirty_log(kvm, log, &is_dirty);
1673         if (r)
1674                 goto out;
1675
1676         /* If nothing is dirty, don't bother messing with page tables. */
1677         if (is_dirty) {
1678                 kvm_flush_remote_tlbs(kvm);
1679                 memslot = &kvm->memslots[log->slot];
1680                 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1681                 memset(memslot->dirty_bitmap, 0, n);
1682         }
1683         r = 0;
1684 out:
1685         spin_unlock(&kvm->arch.dirty_log_lock);
1686         return r;
1687 }
1688
1689 int kvm_arch_hardware_setup(void)
1690 {
1691         return 0;
1692 }
1693
1694 void kvm_arch_hardware_unsetup(void)
1695 {
1696 }
1697
1698 static void vcpu_kick_intr(void *info)
1699 {
1700 #ifdef DEBUG
1701         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1702         printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1703 #endif
1704 }
1705
1706 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1707 {
1708         int ipi_pcpu = vcpu->cpu;
1709         int cpu = get_cpu();
1710
1711         if (waitqueue_active(&vcpu->wq))
1712                 wake_up_interruptible(&vcpu->wq);
1713
1714         if (vcpu->guest_mode && cpu != ipi_pcpu)
1715                 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1716         put_cpu();
1717 }
1718
1719 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1720 {
1721
1722         struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1723
1724         if (!test_and_set_bit(vec, &vpd->irr[0])) {
1725                 vcpu->arch.irq_new_pending = 1;
1726                 kvm_vcpu_kick(vcpu);
1727                 return 1;
1728         }
1729         return 0;
1730 }
1731
1732 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1733 {
1734         return apic->vcpu->vcpu_id == dest;
1735 }
1736
1737 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1738 {
1739         return 0;
1740 }
1741
1742 struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
1743                                        unsigned long bitmap)
1744 {
1745         struct kvm_vcpu *lvcpu = kvm->vcpus[0];
1746         int i;
1747
1748         for (i = 1; i < KVM_MAX_VCPUS; i++) {
1749                 if (!kvm->vcpus[i])
1750                         continue;
1751                 if (lvcpu->arch.xtp > kvm->vcpus[i]->arch.xtp)
1752                         lvcpu = kvm->vcpus[i];
1753         }
1754
1755         return lvcpu;
1756 }
1757
1758 static int find_highest_bits(int *dat)
1759 {
1760         u32  bits, bitnum;
1761         int i;
1762
1763         /* loop for all 256 bits */
1764         for (i = 7; i >= 0 ; i--) {
1765                 bits = dat[i];
1766                 if (bits) {
1767                         bitnum = fls(bits);
1768                         return i * 32 + bitnum - 1;
1769                 }
1770         }
1771
1772         return -1;
1773 }
1774
1775 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1776 {
1777     struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1778
1779     if (vpd->irr[0] & (1UL << NMI_VECTOR))
1780                 return NMI_VECTOR;
1781     if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1782                 return ExtINT_VECTOR;
1783
1784     return find_highest_bits((int *)&vpd->irr[0]);
1785 }
1786
1787 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1788 {
1789         if (kvm_highest_pending_irq(vcpu) != -1)
1790                 return 1;
1791         return 0;
1792 }
1793
1794 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1795 {
1796         return vcpu->arch.timer_fired;
1797 }
1798
1799 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1800 {
1801         return gfn;
1802 }
1803
1804 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1805 {
1806         return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1807 }
1808
1809 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1810                                     struct kvm_mp_state *mp_state)
1811 {
1812         vcpu_load(vcpu);
1813         mp_state->mp_state = vcpu->arch.mp_state;
1814         vcpu_put(vcpu);
1815         return 0;
1816 }
1817
1818 static int vcpu_reset(struct kvm_vcpu *vcpu)
1819 {
1820         int r;
1821         long psr;
1822         local_irq_save(psr);
1823         r = kvm_insert_vmm_mapping(vcpu);
1824         if (r)
1825                 goto fail;
1826
1827         vcpu->arch.launched = 0;
1828         kvm_arch_vcpu_uninit(vcpu);
1829         r = kvm_arch_vcpu_init(vcpu);
1830         if (r)
1831                 goto fail;
1832
1833         kvm_purge_vmm_mapping(vcpu);
1834         r = 0;
1835 fail:
1836         local_irq_restore(psr);
1837         return r;
1838 }
1839
1840 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1841                                     struct kvm_mp_state *mp_state)
1842 {
1843         int r = 0;
1844
1845         vcpu_load(vcpu);
1846         vcpu->arch.mp_state = mp_state->mp_state;
1847         if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1848                 r = vcpu_reset(vcpu);
1849         vcpu_put(vcpu);
1850         return r;
1851 }