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