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