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