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